David Fletcher / Mbed 2 deprecated CC3000WebServer

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

HTTP Server core

HTTP Server core

This module implements the HTTP server core. More...

Enumerations

enum  HttpConnectionState {
  Inactive, RequestMethod, RequestHeaders, DropCurrentHeader,
  RequestData, Processing, ResponseData, ResponseComplete
}
 

This enumeration defines all possible states for a connection.

More...
enum  HttpHandler { None, HttpStatic, HttpDynamic }
 

This enumeration defines all possible request-handler modules.

More...

Functions

static void HttpCore_InitWebServer ()
 Initialize the server's global state structure.
static void HttpCore_CloseConnection (uint16 uConnection)
 Close a connection and clean up its state.
static int HttpCore_HandleRequestPacket (uint16 uConnection, struct HttpBlob packet)
 The main state machine to handle an HTTP transaction Every received data packet for a connection is passed to this function for parsing and handling.
static int HttpCore_HandleMethodLine (uint16 uConnection, struct HttpBlob line)
 This function handles connection initial state Parse the first header line as a method header.
static int HttpCore_HandleHeaderLine (uint16 uConnection, struct HttpBlob line)
 Handle The HTTP headers (after method) one by one If an empty header is received then the headers section is complete Searches for the headers tokens.
static int HttpCore_HandleRequestData (uint16 uConnection, struct HttpBlob *pData)
 Handles request data for this transaction Behaves the same for POST and GET methods - If content length header was present then read the content for further processing If the content is too long then ignore it.
static void HttpCore_ProcessNotFound (uint16 uConnection)
 Returns HTTP 404 not found response.
static int HttpIsValidSocket (SOCKET sock)
 Detect all error conditions from a sockets API return value, such as accpet() Note: This is different in Windows and CC3000 Returns nonzero if valid socket, or zero if invalid socket.
void HttpServerInitAndRun ()
 Initialize and start the HTTP server.
static void HttpCore_ResetConnection (uint16 uConnection)
 Reset open connection after finishing HTPP transaction.
static int HttpCore_GetNextLine (uint16 uConnection, struct HttpBlob *pCurrentLocation, struct HttpBlob *pLine)
 Getting the next line in the HTTP headers section This function is called to get the header lines one by one until an empty line is encountered which means the end of the header section The input is the connection and the remaining blob of the received packet.
static int HttpCore_SendPacket (uint16 uConnection, struct HttpBlob buffer)
 Sends the input blob over the connection socket.
static void HttpResponse_AddCharToResponseHeaders (char ch)
 Add char to the response buffer.
static void HttpResponse_AddNumberToResponseHeaders (uint32 num)
 Add uint32 number to the response buffer.
static void HttpResponse_AddStringToResponseHeaders (char *str, uint16 len)
 Add a string to the response buffer.
static void HttpResponse_AddHeaderLine (char *headerName, uint16 headerNameLen, char *headerValue, uint16 headerValueLen)
 Add header line to response buffer Adds a line to the header with the provided name value pair Precondition to this function is that g_state.packetSendSize and g_state.packetSend are correct.
static void HttpResponse_AddHeaderLineNumValue (char *headerName, uint16 uHeaderNameLen, uint32 headerValue)
 Add Header line to response buffer Adds a line to the header with the provided name value pair when the value is numeric precondition to this function is that g_state.packetSendSize and g_state.packetSend are correct.
static void HttpStatusString (uint16 uHttpStatus, struct HttpBlob *status)
 Returns status string according to status code.
void HttpResponse_Headers (uint16 uConnection, uint16 uHttpStatus, uint16 uFlags, uint32 uContentLength, struct HttpBlob contentType, struct HttpBlob location)
 Respond with the specified HTTP status and headers.
void HttpResponse_GetPacketSendBuffer (struct HttpBlob *pPacketSendBuffer)
 Retrieves the pointer and size of the packet-send buffer This function should be called by content handlers that wish to use the already-allocated packet-send buffer in calls to HttpResponse_Content()
void HttpResponse_Content (uint16 uConnection, struct HttpBlob content)
 Send response content to the client.
void HttpResponse_CannedRedirect (uint16 uConnection, struct HttpBlob location, uint16 bPermanent)
 Sends a canned response, with an HTTP redirect This function should be called *instead* of HttpResponse_Status(), HttpResponse_Headers() and HttpResponse_Content()
void HttpResponse_CannedError (uint16 uConnection, uint16 uHttpStatus)
 Sends a canned response, with an error message This function should be called *instead* of HttpResponse_Status(), HttpResponse_Headers() and HttpResponse_Content()

Variables

__no_init struct HttpGlobalState g_state
 The global state of the HTTP server.

Detailed Description

This module implements the HTTP server core.


Enumeration Type Documentation

This enumeration defines all possible states for a connection.

Enumerator:
Inactive 

Connection is inactive. The connection's socket should be INVALID_SOCKET_HANDLE.

RequestMethod 

Waiting for packet(s) with the request method.

RequestHeaders 

Waiting for packet(s) with request headers.

DropCurrentHeader 

Currently receiving a header which is too long - Drop it and continue waiting for more headers.

RequestData 

Done parsing headers, waiting for POST data pakcets.

Processing 

Request is at the hands of the content module (static and/or dynamic), waiting for response headers.

ResponseData 

Response headers have been sent. Sending response content.

ResponseComplete 

Response complete. Possibility of another request.

Definition at line 63 of file HttpCore.cpp.

This enumeration defines all possible request-handler modules.

Enumerator:
None 

No module is going to process this request (use the default 404 error)

HttpStatic 

The HttpStatic module is going to process this request.

HttpDynamic 

The HttpDynamic module is going to process this request.

Definition at line 86 of file HttpCore.cpp.


Function Documentation

static void HttpCore_CloseConnection ( uint16  uConnection ) [static]

Close a connection and clean up its state.

Definition at line 465 of file HttpCore.cpp.

static int HttpCore_GetNextLine ( uint16  uConnection,
struct HttpBlob pCurrentLocation,
struct HttpBlob pLine 
) [static]

Getting the next line in the HTTP headers section This function is called to get the header lines one by one until an empty line is encountered which means the end of the header section The input is the connection and the remaining blob of the received packet.

Returns:
zero if the whole packet was handled, and need to wait for more data (pLine is not set to anything yet) negative if some error occurred, and the connection should be closed. positive if successful. In this case pCurrentLocation is advanced to skip the line and pLine returns the next line, or NULL and 0 if it should be discarded

Definition at line 489 of file HttpCore.cpp.

static int HttpCore_HandleHeaderLine ( uint16  uConnection,
struct HttpBlob  line 
) [static]

Handle The HTTP headers (after method) one by one If an empty header is received then the headers section is complete Searches for the headers tokens.

If important data is found then it is saved in the connection object

returns nonzero if sucessful

Definition at line 858 of file HttpCore.cpp.

static int HttpCore_HandleMethodLine ( uint16  uConnection,
struct HttpBlob  line 
) [static]

This function handles connection initial state Parse the first header line as a method header.

Method line should be in the form: GET /resource.html HTTP/1.1

Returns:
nonzero if success

Definition at line 770 of file HttpCore.cpp.

static int HttpCore_HandleRequestData ( uint16  uConnection,
struct HttpBlob pData 
) [static]

Handles request data for this transaction Behaves the same for POST and GET methods - If content length header was present then read the content for further processing If the content is too long then ignore it.

Returns:
1 if successful, pData is updated to skip the handled data 0 if all data is consumed and need to read more data negative if an error occurs and the connection should be closed.

Definition at line 948 of file HttpCore.cpp.

static int HttpCore_HandleRequestPacket ( uint16  uConnection,
struct HttpBlob  packet 
) [static]

The main state machine to handle an HTTP transaction Every received data packet for a connection is passed to this function for parsing and handling.

If there is an error the connection will be closed in the end of the transaction It will also be closed if "connection: close" header is received or HTTP version is 1.0

Returns:
zero to close the connection nonzero if packet was consumed successfully, and the connection can handle more data

Definition at line 654 of file HttpCore.cpp.

static void HttpCore_InitWebServer (  ) [static]

Initialize the server's global state structure.

Definition at line 441 of file HttpCore.cpp.

static void HttpCore_ProcessNotFound ( uint16  uConnection ) [static]

Returns HTTP 404 not found response.

Definition at line 991 of file HttpCore.cpp.

static void HttpCore_ResetConnection ( uint16  uConnection ) [static]

Reset open connection after finishing HTPP transaction.

Definition at line 428 of file HttpCore.cpp.

static int HttpCore_SendPacket ( uint16  uConnection,
struct HttpBlob  buffer 
) [static]

Sends the input blob over the connection socket.

Definition at line 1000 of file HttpCore.cpp.

static int HttpIsValidSocket ( SOCKET  sock ) [static]

Detect all error conditions from a sockets API return value, such as accpet() Note: This is different in Windows and CC3000 Returns nonzero if valid socket, or zero if invalid socket.

Definition at line 177 of file HttpCore.cpp.

static void HttpResponse_AddCharToResponseHeaders ( char  ch ) [static]

Add char to the response buffer.

Definition at line 1035 of file HttpCore.cpp.

static void HttpResponse_AddHeaderLine ( char *  headerName,
uint16  headerNameLen,
char *  headerValue,
uint16  headerValueLen 
) [static]

Add header line to response buffer Adds a line to the header with the provided name value pair Precondition to this function is that g_state.packetSendSize and g_state.packetSend are correct.

Definition at line 1069 of file HttpCore.cpp.

static void HttpResponse_AddHeaderLineNumValue ( char *  headerName,
uint16  uHeaderNameLen,
uint32  headerValue 
) [static]

Add Header line to response buffer Adds a line to the header with the provided name value pair when the value is numeric precondition to this function is that g_state.packetSendSize and g_state.packetSend are correct.

Definition at line 1083 of file HttpCore.cpp.

static void HttpResponse_AddNumberToResponseHeaders ( uint32  num ) [static]

Add uint32 number to the response buffer.

Definition at line 1045 of file HttpCore.cpp.

static void HttpResponse_AddStringToResponseHeaders ( char *  str,
uint16  len 
) [static]

Add a string to the response buffer.

Definition at line 1058 of file HttpCore.cpp.

void HttpResponse_CannedError ( uint16  uConnection,
uint16  uHttpStatus 
)

Sends a canned response, with an error message This function should be called *instead* of HttpResponse_Status(), HttpResponse_Headers() and HttpResponse_Content()

Parameters:
uConnectionThe connection number, as it appears in the HttpRequest structure
uHttpStatusThe HTTP error status. Must be one of HTTP_STATUS_ERROR_*

Definition at line 1247 of file HttpCore.cpp.

void HttpResponse_CannedRedirect ( uint16  uConnection,
struct HttpBlob  location,
uint16  bPermanent 
)

Sends a canned response, with an HTTP redirect This function should be called *instead* of HttpResponse_Status(), HttpResponse_Headers() and HttpResponse_Content()

Parameters:
uConnectionThe connection number, as it appears in the HttpRequest structure
pLocationThe redirect URL
bPermanentzero for temporary redirect, nonzero for permanent redirect

Definition at line 1239 of file HttpCore.cpp.

void HttpResponse_Content ( uint16  uConnection,
struct HttpBlob  content 
)

Send response content to the client.

This function may be called more than once, until all the content is sent.

Parameters:
uConnectionThe connection number, as it appears in the HttpRequest structure
contentContent blob to send to the client.

Definition at line 1223 of file HttpCore.cpp.

void HttpResponse_GetPacketSendBuffer ( struct HttpBlob pPacketSendBuffer )

Retrieves the pointer and size of the packet-send buffer This function should be called by content handlers that wish to use the already-allocated packet-send buffer in calls to HttpResponse_Content()

Parameters:
[out]pPacketSendBufferReturns the pointer and size of the packet-send buffer

Definition at line 1217 of file HttpCore.cpp.

void HttpResponse_Headers ( uint16  uConnection,
uint16  uHttpStatus,
uint16  uFlags,
uint32  uContentLength,
struct HttpBlob  contentType,
struct HttpBlob  location 
)

Respond with the specified HTTP status and headers.

Parameters:
uConnectionThe connection number, as it appears in the HttpRequest structure
uHttpStatusThe HTTP status number to response with. Must be one of HTTP_STATUS_*
uFlagsFlags which are manifested in the response headers. See HTTP_RESPONSE_FLAG_*
uContentLengthThe total length of content which will be sent via HttpResponse_Content()
contentTypeThe content type string, or NULL to omit the content type
locationA string which will be used for the Location header, or NULL to omit the Location header

Definition at line 1128 of file HttpCore.cpp.

void HttpServerInitAndRun (  )

Initialize and start the HTTP server.

The Wifi interface of the CC3000 chip should be initialized by now, and connected to the network

Definition at line 196 of file HttpCore.cpp.

static void HttpStatusString ( uint16  uHttpStatus,
struct HttpBlob status 
) [static]

Returns status string according to status code.

Definition at line 1095 of file HttpCore.cpp.


Variable Documentation

__no_init struct HttpGlobalState g_state

The global state of the HTTP server.

Definition at line 151 of file HttpCore.cpp.