22 #define requestPatternStringMaxLength(queryStringLength) "%" QUOTE(queryStringLength) "[^ ] HTTP/%*d.%*d\r\n"
23 #define headerPatternStringMaxLength(headerLength, valueLength) "%" QUOTE(headerLength) "[^:]:% %" QUOTE(valueLength) "[^\r]\r\n"
76 void HTTPServer::onReceive(uint16_t len,
const byte* data)
78 if (stage >= HTTP_SERVER_STAGE_RESPONSE)
85 if (stage >= HTTP_SERVER_STAGE_RESPONSE)
100 if (requestPattern.signaled)
102 if (c ==
'\r' || c ==
'\n')
107 stage = HTTP_SERVER_STAGE_BODY;
111 if (stage >= HTTP_SERVER_STAGE_RESPONSE)
116 if (stage >= HTTP_SERVER_STAGE_RESPONSE)
129 if (scanner.scan(c, headerName, headerValue))
143 if (stage == HTTP_SERVER_STAGE_QUERY_STRING)
145 if (scanner.scan(c, queryString))
149 if (stage >= HTTP_SERVER_STAGE_RESPONSE)
153 scanner.setPattern(headerPattern);
154 stage = HTTP_SERVER_STAGE_HEADERS;
161 stage = HTTP_SERVER_STAGE_QUERY_STRING;
172 void HTTPServer::resetParser()
174 scanner.setPattern(requestPattern);
177 stage = HTTP_SERVER_STAGE_INIT;
181 void HTTPServer::onConnectRequest()
185 void HTTPServer::onConnect()
187 ACTRACE(
"HTTP connection open");
191 void HTTPServer::onClose()
193 ACTRACE(
"HTTP connection closed by peer");
205 stage = HTTP_SERVER_STAGE_RESPONSE;
206 String code(statusCode);
207 write(F(
"HTTP/1.1 % %\r\n"), &code, &message);
227 write(F(
"%:%\r\n"), &headerName, &headerValue);
238 writeHeader((__FlashStringHelper*)headerName, headerValue);
259 String contentTypeStr((__FlashStringHelper*)contentType);
268 stage = HTTP_SERVER_STAGE_RESPONSE_BODY;
277 if (stage < HTTP_SERVER_STAGE_RESPONSE_BODY)
283 stage = HTTP_SERVER_STAGE_RESPONSE_END;
#define HTTP_SERVER_HEADER_NAME_MAX_LENGTH
max buffer to hold a header name, e.g.
nint16_t localPort
local TCP or UDP port
void writeHeader(const String &headerName, const String &headerValue)
Writes an HTTP header to the ongoing response stream.
void beginResponse(uint16_t statusCode, const String &message="")
Sends out the response line, e.g.
uint8_t httpMethod
Indicates what HTTP method was used in this request.
uint16_t contentLength
captured length of the incoming request, in bytes.
static const char requestPatternString[]
virtual void onBodyBegin()
Called when all HTTP headers have been received and the body of the request is about to arrive...
uint16_t write(uint16_t len, const byte *data)
In the case of TCP, writes the given data buffer to the socket.
void push()
Sets the PSH TCP flag and also sends data in the outgoing buffer immediately.
void listen()
Starts listening on the local port indicated by the localPort property.
void beginResponse_P(uint16_t statusCode, PGM_P message)
Sends out the response line, e.g.
virtual void onHeaderReceived(const char *headerName, const char *headerValue)
Called once for each header in the request, as they arrive.
void beginResponseBody()
Indicates to the client that all headers have been written and that the response body follows...
void writeContentTypeHeader(const String &contentType)
Convenient function to write the Content-Type header.
#define headerPatternStringMaxLength(headerLength, valueLength)
void writeContentTypeHeader_P(PGM_P contentType)
Convenient function to write the Content-Type header.
virtual void onRequestEnd()
Called after all the body has been received
virtual void onBodyReceived(uint16_t len, const byte *data)
Called once for each fragment of the body that is received.
void endResponse()
Ends the connection and finalizes the response.
virtual void onRequest(char *queryString)
Called immediately after the first line that contains the HTTP method and query string is received ...
#define HTTP_SERVER_HEADER_VALUE_MAX_LENGTH
max buffer to hold a header value, e.g.
#define requestPatternStringMaxLength(queryStringLength)
void writeHeader_P(PGM_P headerName, const String &headerValue)
Writes an HTTP header to the ongoing response stream.
void close()
Attempts to gracefully close a connection.
void accept()
Accepts a connection request that has been received on the port this instance was listening on ...
ACROSS_MODULE("HTTPServer")
#define HTTP_SERVER_QUERY_STRING_MAX_LENGTH
max buffer to hold the entire query string, e.g.
static const char headerPatternString[]