friendev EtherDune TCP/IP library
|
Provides an easy way to build a web server. More...
#include <HTTPServer.h>
Public Member Functions | |
HTTPServer () | |
void | listen (uint16_t port=80) |
Starts listening on the specified TCP port. More... | |
void | beginResponse (uint16_t statusCode, const String &message="") |
Sends out the response line, e.g. More... | |
void | beginResponse_P (uint16_t statusCode, PGM_P message) |
Sends out the response line, e.g. More... | |
void | writeHeader (const String &headerName, const String &headerValue) |
Writes an HTTP header to the ongoing response stream. More... | |
void | writeHeader_P (PGM_P headerName, const String &headerValue) |
Writes an HTTP header to the ongoing response stream. More... | |
void | writeContentTypeHeader (const String &contentType) |
Convenient function to write the Content-Type header. More... | |
void | writeContentTypeHeader_P (PGM_P contentType) |
Convenient function to write the Content-Type header. More... | |
void | beginResponseBody () |
Indicates to the client that all headers have been written and that the response body follows. More... | |
void | endResponse () |
Ends the connection and finalizes the response. More... | |
virtual void | onBodyReceived (uint16_t len, const byte *data) |
Called once for each fragment of the body that is received. More... | |
virtual void | onHeaderReceived (const char *headerName, const char *headerValue) |
Called once for each header in the request, as they arrive. More... | |
virtual void | onBodyBegin () |
Called when all HTTP headers have been received and the body of the request is about to arrive. More... | |
virtual void | onRequest (char *queryString) |
Called immediately after the first line that contains the HTTP method and query string is received More... | |
virtual void | onRequestEnd () |
Called after all the body has been received More... | |
![]() | |
TCPSocket () | |
void | connect () |
Initiates a TCP connection to remoteIP and remotePort. More... | |
void | close () |
Attempts to gracefully close a connection. More... | |
void | terminate () |
Immediately shuts down the socket and makes it available for a new task. More... | |
void | listen () |
Starts listening on the local port indicated by the localPort property. More... | |
void | accept () |
Accepts a connection request that has been received on the port this instance was listening on More... | |
void | accept (TCPSocket &listener) |
Accepts a connection request that was sent to another TCPSocket instance. More... | |
void | push () |
Sets the PSH TCP flag and also sends data in the outgoing buffer immediately. More... | |
virtual void | onTerminate () |
Called when the socket is ready to be reused. More... | |
![]() | |
uint16_t | write (uint16_t len, const byte *data) |
In the case of TCP, writes the given data buffer to the socket. More... | |
uint16_t | write (const String &s) |
In the case of TCP, writes the given String to the socket, not including the null-terminating character. More... | |
uint16_t | write (const __FlashStringHelper *pattern,...) |
Writes out the specified PROGMEM string that may contain the % escape character. More... | |
template<class T > | |
uint16_t | write (const T &message) |
Writes out the binary representation of the parameter to the socket. More... | |
![]() | |
uint8_t | getState () |
Public Attributes | |
uint8_t | httpMethod |
Indicates what HTTP method was used in this request. More... | |
uint16_t | contentLength |
captured length of the incoming request, in bytes. More... | |
![]() | |
nint16_t | remotePort |
remote TCP or UDP port More... | |
nint16_t | localPort |
local TCP or UDP port More... | |
IPAddress | remoteIP |
remote IP address to connect to (TCP) or send the next packet to (UDP) More... | |
Additional Inherited Members | |
![]() | |
void | prepareIPPacket () |
![]() | |
NetworkService () | |
~NetworkService () | |
virtual void | onDNSResolve (uint8_t status, uint16_t identification, const IPAddress &ip) |
Called on each network service every time a DNS response is received. More... | |
![]() | |
void | setState (uint8_t newState, uint8_t timeout) |
Changes to a new state More... | |
![]() | |
static uint16_t | calcPseudoHeaderChecksum (uint8_t protocol, uint16_t length) |
Calculates the TCP or UDP pseudo header checksum. More... | |
static uint16_t | calcTCPChecksum (bool options, uint16_t dataLength, uint16_t dataChecksum) |
Calculates the TCP checksum. More... | |
static uint16_t | calcUDPChecksum (uint16_t dataLength, uint16_t dataChecksum) |
Calculates the UDP checksum. More... | |
static bool | verifyUDPTCPChecksum () |
Verifies if the UDP or TCP checksum of the current packet is correct. More... | |
![]() | |
static bool | sendIPPacket (uint16_t length) |
Puts the current in-memory packet in the network More... | |
static void | prepareIPPacket (const IPAddress &remoteIP) |
Sets up common IP header values for all outgoing IP packets and calculates the IP header checksum More... | |
static bool | sameLAN (IPAddress &dst) |
Determines whether the given IP is in the same subnet as localIP More... | |
static bool | begin (uint8_t cspin) |
Initializes EtherDune and the underlying hardware More... | |
static void | loop () |
Gives processing time to EtherDune so that it can check for incoming packets or send queued packets. More... | |
static DNSClient & | DNS () |
Obtains access to the DNS service singleton instance. More... | |
![]() | |
static void | writeByte (byte b) |
static void | writeByte (uint16_t dst, byte b) |
static void | writeBuf (uint16_t dst, uint16_t len, const byte *data) |
static void | writeBuf (uint16_t len, const byte *data) |
static uint16_t | hardwareChecksum (uint16_t src, uint16_t len) |
static uint16_t | hardwareChecksumRxOffset (uint16_t offset, uint16_t len) |
static void | moveMem (uint16_t dest, uint16_t src, uint16_t len) |
static void | readBuf (uint16_t src, uint16_t len, byte *data) |
static void | readBuf (uint16_t len, byte *data) |
static byte | readByte (uint16_t src) |
static void | packetSend (uint16_t len) |
static void | packetSend (uint16_t len, const byte *data) |
static void | loadSample () |
static void | loadAll () |
static void | release () |
static uint8_t | begin (uint8_t cspin) |
static void | loop () |
static void | enableBroadcast () |
static bool | isLinkUp () |
Determines whether the network link is ready More... | |
![]() | |
SharedBuffer | buffer |
output buffer for this socket More... | |
![]() | |
uint8_t | stateTimer |
state timer, in ticks More... | |
uint8_t | state |
state code More... | |
![]() | |
static uint8_t | srcPort_L_count = 0 |
self-incrementing counter for local ports. More... | |
![]() | |
static EthBuffer | packet |
in-memory packet buffer currently being processed. More... | |
static ARPService | ARP |
ARP singleton instance. More... | |
static MACAddress | localMAC |
Ethernet MAC address. More... | |
static IPAddress | localIP = { 0, 0, 0, 0 } |
IP address of this application. More... | |
static IPAddress | gatewayIP = { 0, 0, 0, 0 } |
IP address of the gateway in this network. More... | |
static IPAddress | netmask = { 0, 0, 0, 0 } |
Subnet mask. More... | |
static IPAddress | dnsIP = { 0, 0, 0, 0 } |
IP address of the DNS server to use. More... | |
Provides an easy way to build a web server.
This class implements the basics of a web server engine
To use this class, create a derived class and override the appropriate virtual functions to receive notifications about the progress of the incoming request and capture the needed data for your application
This class can only serve one request at a time. Combine with TCPListener to be able to "spawn" children sockets that serve each request independently. (see HTTPServerDemo_MultipleClients.ino example)
The flow of calls works as follows:
Depending on what information your server is interested in in order to be able to service the request, you may initiate your response at any of the above event handlers. For example if you are only interested in the query string you can override onRequest() only and build your response there on the spot.
To build a response, call the following functions: 1.- beginResponse() or beginResponse_P(), to send a status code and optional message 2.- Any number of calls to writeHeader() overload, if any, to send the different HTTP response headers. 3.- beginResponseBody() to mark the beginning of the response body. 4.- Finally, call endResponse() to indicate the other party the response is over.
See HTTPServerDemo.ino and HTTPServerDemo_MultipleClients.ino for examples on how to use this class.
Definition at line 57 of file HTTPServer.h.
HTTPServer::HTTPServer | ( | ) |
Definition at line 61 of file HTTPServer.cpp.
void HTTPServer::beginResponse | ( | uint16_t | statusCode, |
const String & | message = "" |
||
) |
Sends out the response line, e.g.
HTTP/1.1 200 OK
statusCode | HTTP status code to send, see HTTP response codes for a list of possible values |
message | HTTP message to send as status description, as a String or regular C string |
Definition at line 203 of file HTTPServer.cpp.
void HTTPServer::beginResponse_P | ( | uint16_t | statusCode, |
PGM_P | message | ||
) |
Sends out the response line, e.g.
HTTP/1.1 404 Not Found
statusCode | HTTP status code to send, see HTTP response codes for a list of possible values |
message | HTTP message to send as status description, as a PROGMEM string |
Definition at line 214 of file HTTPServer.cpp.
void HTTPServer::beginResponseBody | ( | ) |
Indicates to the client that all headers have been written and that the response body follows.
Definition at line 266 of file HTTPServer.cpp.
void HTTPServer::endResponse | ( | ) |
Ends the connection and finalizes the response.
Definition at line 275 of file HTTPServer.cpp.
void HTTPServer::listen | ( | uint16_t | port = 80 | ) |
Starts listening on the specified TCP port.
port | TCP port to listen on |
Definition at line 70 of file HTTPServer.cpp.
|
virtual |
Called when all HTTP headers have been received and the body of the request is about to arrive.
This call gives an opportunity to configure scanner FlowScanner HTTPServer::scanner or other parser to process the body as it arrives.
Definition at line 49 of file HTTPServer.cpp.
|
virtual |
Called once for each fragment of the body that is received.
len | length of the buffer that contains the body portion received |
data | pointer to the buffer containing the body data |
Definition at line 34 of file HTTPServer.cpp.
|
virtual |
Called once for each header in the request, as they arrive.
headerName | pointer to string containing the header name, e.g. Accept |
headerValue | pointer to string containing the header value, e.g. text/html |
The maximum length of headerName
and headerValue
is limited to HTTP_SERVER_HEADER_NAME_MAX_LENGTH and HTTP_SERVER_HEADER_VALUE_MAX_LENGTH respectively. see HTTPServer class configuration for more information
Definition at line 43 of file HTTPServer.cpp.
|
virtual |
Called immediately after the first line that contains the HTTP method and query string is received
queryString | pointer to the request's query string. The length of this string is limited to HTTP_SERVER_QUERY_STRING_MAX_LENGTH configuration value. see HTTPServer class configuration for more information. |
Definition at line 55 of file HTTPServer.cpp.
|
virtual |
Called after all the body has been received
Definition at line 59 of file HTTPServer.cpp.
void HTTPServer::writeContentTypeHeader | ( | const String & | contentType | ) |
Convenient function to write the Content-Type header.
Use only after beginResponse() has been called to initiate the HTTP response.
contentType | String / regular C string representing the content type, e.g., application/json |
Definition at line 246 of file HTTPServer.cpp.
void HTTPServer::writeContentTypeHeader_P | ( | PGM_P | contentType | ) |
Convenient function to write the Content-Type header.
Use only after beginResponse() has been called to initiate the HTTP response.
contentType | PROGMEM string representing the content type, e.g., application/json . See Content-Type constants for a list of pre-defined constants you can use |
Definition at line 257 of file HTTPServer.cpp.
void HTTPServer::writeHeader | ( | const String & | headerName, |
const String & | headerValue | ||
) |
Writes an HTTP header to the ongoing response stream.
Use only after beginResponse() has been called to initiate the HTTP response.
headerName | Name of the header, e.g. Last-Modified , as a String object or regular null-terminated C string |
headerValue | The header value, e.g. Sat, 14 Feb 2015 01:55:08 GMT |
Definition at line 225 of file HTTPServer.cpp.
void HTTPServer::writeHeader_P | ( | PGM_P | headerName, |
const String & | headerValue | ||
) |
Writes an HTTP header to the ongoing response stream.
Use only after beginResponse() has been called to initiate the HTTP response.
headerName | Name of the header, e.g. Last-Modified , as a PROGMEM string |
headerValue | The header value, e.g. Sat, 14 Feb 2015 01:55:08 GMT |
Definition at line 236 of file HTTPServer.cpp.
uint16_t HTTPServer::contentLength |
captured length of the incoming request, in bytes.
Valid only after onBodyBegin() is called. contentLength
will be zero if no Content-Length
header was present in the request.
Definition at line 107 of file HTTPServer.h.
uint8_t HTTPServer::httpMethod |
Indicates what HTTP method was used in this request.
Valid only after onRequest() has been called. see HTTP method constants for a list of possible values.
Definition at line 104 of file HTTPServer.h.