httplib
The HTTPlib is a collection of classes to handle HTTP Headers (reading / creating them). They are used by the webserver, so normally there
is no need to use them as long as you don't use IWebserverPassthrough. There it could become in handy using the HTTPlib classes to handle the
HTTP header stuff. After setting the version number and the HTTP result (or the HTTP request method), the header is prepared to be send.
You can set what ever field you want to and send that header directly by getting the buffer to the byte-stream and the size of it. So there
is no need for any other function to prepare a http header. Just set the version, the request or result, what ever field you want to set more
(it will always be a good idea to set at least the content length, even if it will be 0) and send the header.
File information
Classes
HTTPLib
class HTTPLib {
public:
static inline const char * GetHTTPRequestMethodName(http_request_method_t method);
inline static const http_result_t * HTTPResultFromStatusNumber(int number);
inline static const http_result_t * HTTPResultFromStatusNumberStr(const char * numStr);
};
This class actually is only for wrapping the types and constants used by the HTTP lib (beside the one helper function described here). The other parts of
that class are described in the data types section of this document (see below).
Public functions
GetHTTPRequestMethodName
This inline function simply is for getting the name (as string) for a given HTTP method.
Parameters
http_request_method_t method | The method id to get the name for. |
Return Values
The name of the method as string.
HTTPResultFromStatusNumber
This inline function simply is for converting the integer status code to http_result_t *.
Parameters
int number | The status code as int. |
Return Values
The corresponding http_result_t *.
HTTPResultFromStatusNumberStr
This inline function simply is for converting a char * "integer" status code to http_result_t *.
Parameters
const char * numStr | The status code as char * which contains numbers. |
Return Values
The corresponding http_result_t *.
class HTTPHeader {
public:
HTTPHeader();
~HTTPHeader();
void SetHTTPVersion(int major, int minor);
void SetHTTPResult(const HTTPLib::http_result_t * result);
void SetContentSize(ulong64 size);
void SetContentRange(ulong64 start, ulong64 end, ulong64 completeSize);
void SetContentType(const char * contentType);
void SetEncoding(HTTPLib::http_encoding_t enc);
void SetETag(const char * etag);
void SetConnection(HTTPLib::http_connection_t connection);
void SetUpgrade(HTTPLib::http_upgrade_t upgrade);
void SetWebsocketAcceptKey(const char * key);
void SetCustomField(const char * field);
void SetCustomField(const char * field, const char * value);
void SetDate();
void SetRequest(HTTPLib::http_request_method_t req, const char * resource);
const char * GetHeaderData() { return buffer; }
size_t GetHeaderSize() { return contentSize; }
};
This class is used for creating an HTTP header asresponse or request. If you want to parse an HTTP Header, HTTPParse must be used.
Public functions
SetHTTPVersion
Sets the major and minor version of the HTTP protocol you want to use. If a header will be created as a response, the HTTP version requested can be obtained
from an HTTPParser instance by calling HTTPParser::GetMajorVersion() or HTTPParser::GetMinorVersion() and set the same version for the response as used for
the request.
This function must be called before using any other function, or the header you're going to build up will be incorrect. However, if you need to build a HTTP
header multiple times for response to requests, you can reuse a HTTPHeader instance, setting the version only once and reuse it by setting the HTTP result
code at first.
Parameters
int major | The major HTTP version to set. |
int minor | The minor HTTP version to set. |
SetHTTPResult
Set the result code for the HTTP header, to response to a HTTP request. This function will reset all other, previous set header data (except the HTTP version).
So by setting the result code first n already existing HTTPHeader object can be reused for further response header creation.
Parameters
HTTPLib::http_result_t * result | Pointer to the result code. Must be one of the HTTPLib::HTTP_* constants (see below). |
SetContentSize
Set the content-length field of the header. It should be set every time, regardless if the size is 0 (for no content) or not.
Parameters
ulong64 size | The content length to set. |
SetContentRange
Set the Content-Range field of the header. The start and end values are numbered from 0. The range field will be encoded with the given
complete size in the format "start-end/completeSize".
Parameters
ulong64 start | The start of the range. |
ulong64 end | The end of the range. |
ulong64 completeSize | The complete size of the requested resource. |
SetContentType
Set the mime-type of the content field of the header.
Parameters
const char * contentType | The mime-type of the content to set (e. g. "text/html; charset=utf-8"). |
SetContentType
Set the mime-type of the content field of the header.
Parameters
const char * contentType | The mime-type of the content to set (e. g. "text/html; charset=utf-8"). |
SetContSetEncodingentType
Set the encoding of the content. For now, there are only two encoding type: HTTP_ENCODING_NONE and HTTP_ENCODING_GZIP if the content is in gzip compressed format.
Parameters
HTTPLib::http_encoding_t enc | The encoding type to set. |
SetETag
Set the etag. The etag field will be used to support browser side caching of data.
Parameters
const char * etag | The etag to set. |
SetConnection
Set the connection type.
Parameters
HTTPLib::http_connection_t connection | The type of the connection (HTTP_CONNECTION_NONE, HTTP_CONNECTION_KEEP_ALIVE, HTTP_CONNECTION_UPGRADE). |
SetUpgrade
Set the upgrade type for connections defined with HTTP_CONNECTION_UPGRADE. For now, only HTTP_UPGRADE_WEBSOCKET is supported.
Parameters
HTTPLib::http_upgrade_t upgrade | The upgrade type for the connection. |
SetWebsocketAcceptKey
Set the key to accept incomming websocket connection (only for response headers).
Parameters
const char * key | The key to set. |
SetCustomField (overloaded)
Set a custom header field. In this version of the function, the header field with the value must be given with the HTTP header conform format ("FieldName: Value").
Custom header field means not only totally user defined header fields, but also every protocol defined header field not covered by any of the other Set*() functions.
Parameters
const char * key | The headerfield with the value to set. |
SetCustomField (overloaded)
Set a custom header field. In this version, the header field anem and the value can be given seperately. Custom header field means not only totally user defined
header fields, but also every protocol defined header field not covered by any of the other Set*() functions.
Parameters
const char * field | The name of the headerfield to set. |
const char * key | The value for that field. |
SetDate
Set the date-time field for the HTTP header, using the current time.
SetRequest
(For request only) Set the request the header is for. This function resets all internal fields, starting to build up a new header.
Parameters
HTTPLib::http_request_method_t method | The HTTP request method to build the header for. |
const char * resource | The resource to request. |
GetHeaderData
Return Value
The http header in completely build up version, ready to send.
GetHeaderSize
Return Value
The length of the HTTP header.
HTTPParser
class HTTPParser {
public:
typedef enum {
HTTP_PARSE_DONE,
HTTP_PARSE_NEED_DATA,
HTTP_PARSE_CANCEL,
HTTP_PARSE_ERROR
} parseresult_t;
HTTPParser();
~HTTPParser();
size_t ParseData(char * data, size_t len, UHTTPParser * user = nullptr);
parseresult_t GetParseResult() const { return (parseresult_t)parseResult; }
HTTPLib::http_encoding_t GetAcceptEncoding() const { return acceptEncoding; }
HTTPLib::http_request_method_t GetRequestMethod() const { return reqMethod; }
const char * GetRequestTarget() const { return requestTarget; }
const char * GetRequestParameters() const { return requestParameters; }
int GetMajorVersion() const { return httpVersion >> 8; }
int GetMinorVersion() const { return httpVersion & 0x00FF; }
int GetVersion() const { return httpVersion; }
const HTTPHeaderField * GetHeaderField(const char * fieldName) const;
ulong64 GetContentLength() const { return contentLength; }
bool IsConnectionType(HTTPLib::http_connection_t ct) const { return (connectionType & ct) != 0; }
bool IsChunkedTransfer();
const char * GetCookie() const { return cookie; }
const char * GetETag() const { return etag; }
const char * GetContentType() const { return contentType; }
const char * GetHost() const { return host; }
HTTPLib::http_upgrade_t GetUpgrade() const { return upgradeType; }
const HTTPRange * GetRange() const { return rangeListStart; }
size_t GetRangeCount() const { return rangeCount; }
HTTPLib::http_encoding_t GetEncoding() const { return encoding; }
const char * GetWebsocketKey() const { return websocketKey; }
int GetWebsocketVersion() const { return websocketVersion; }
const char * GetOrigin() const { return origin; }
const HTTPLib::http_result_t * GetHTTPResultCode() const { return httpResultCode; }
size_t GetHeaderSize() const { return 0; }
char * MakeHeaderCopy() const { return nullptr; };
void GetFieldListCopy(byte * & buffer, size_t & bufferSize) const;
};
This class will be used to parse a byte stream for a HTTP header and extract the fields / data from the header. Note that the byte stream
must begin with a valid HTTP header, or an error will be returned.
Public functions
ParseData
Parsed the given data with the given length. You have to first call this function and then call GetParseResult() to check the status of the parsing process.
Everything will be copied to an inside buffer, so the given buffer can be reused to receive additional header data to parse.
Parameters
char * data | The data to parse. |
size_t len | The number of bytes inside the given data buffer. |
UHTTPParser * user |
An UHTTPParser instance that will be called after the request line had been parsed. This helps to define, wheter
parsing should continue or not. See UHTTPParser for more information.
|
Callbacks
If the header indicates a HTTP request and if user is not nullptr, UHTTPParser::HTTPParserValidateRequest() will be called after parsing the first line (which is the request
line), so that the request can be accepted or recjected before continue parsing the rest of the header.
Return Values
The number of bytes processed from the given data buffer. If less then len, the value should be added to data to acces the data behind the header inside the byte stream.
GetParseResult
Return Value
The result of the last ParseData() call. The result determines how to continue and will be one of the following value:
- HTTP_PARSE_DONE: Parsing had been finished succesfully. You can now access the values of the parsed header.
- HTTP_PARSE_NEED_DATA: The end of the given bytestream had been reached without reaching the end of the header. ParseData() must be called again with the continouse data of the byte stream.
- HTTP_PARSE_ERROR: An error occures during parsing. Noramly that means, that a correct header could not been found or that there was some error inside the header structure. In this case, the bytestream should be discarded, because there is no way of recovering the broken header and correctly interpreting whatever data follow.
GetAcceptEncoding
Return Value
Returns the encoding the other requested server or requesting client side will accept. For now, HTTPlib only support the GZIP encoding (HTTP_ENCODING_GZIP).
GetRequestMethod
Return Value
Returns the request method of the header. Will be one of the following values:
- HTTP_NONE
- HTTP_GET
- HTTP_HEAD
- HTTP_POST
- HTTP_PUT
- HTTP_CONNECT
- HTTP_OPTIONS
- HTTP_TRACE
- HTTP_PROPFIND
- HTTP_MKCOL
- HTTP_COPY
- HTTP_MOVE
- HTTP_DELETE
GetRequestTarget
Return Value
Retruns the requested target aka requested resource.
GetMajorVersion
Return Value
Retruns the major version of the HTTP protocol for the request.
GetMinorVersion
Return Value
Retruns the minor version of the HTTP protocol for the request.
GetHeaderField
Use this function to ask for a specified header field. Use this if the headerfield you need canot be accessed through the functions of the mostly used
header fields provided by HTTPParser.
Parameters
const char * fieldName | The name of the header field to get. |
Return Value
If the header field canot be found, the function returns nullptr. Else a HTTPHeaderField instance will be returned, providing the detail of the field. That instance
canot be deleted and will completely managed by HTTPParser itself.
GetContentLength
Return Value
(For response only) Reports the content length defined by the header. This will be the amount of data to read from the byte stream to completely process the response. Can be 0.
Remarks
The return value of 0 doesn't mean, that there is no further data to process. The data send with a response can also be chunk encoded. Check IsChunkedTransfer() to get
that information.
IsConnectionType
Can be used to check if the parsed header defines a special type of connection.
Parameters
HTTPLib::http_connection_t ct | The connection type to check for. |
Return Value
True, of the conenction type given is defined in the header, else false.
Remarks
The value for ct must be HTTP_CONNECTION_NONE, HTTP_CONNECTION_KEEP_ALIVE or HTTP_CONNECTION_UPGRADE. See the data types section below for details.
IsChunkedTransfer
Return Value
True, if there is data available in chunk encoded format. reading and handling the cunks will be the applications responsebility.
GetCookie
Return Value
Returns the value of the cookie header field.
GetETag
Return Value
Returns the value of the etag header field.
GetContentType
Return Value
Returns the content type (aka mime-type) declared by the header.
GetHost
Return Value
Returns the host reported by the header.
GetUpgrade
Return Value
Returns the upgrade type (if there is any). For now, the HTTPlib only supports HTTP_UPGRADE_WEBSOCKET.
GetRange
Return Value
Returns an HTTPRange object to access the values given by the range header field. If there is no range request, nullptr will be returned.
The returned object is the start of a linked list of ranges. See HTTPRange for more information.
GetEncoding
Return Value
Returns the value of the headers content-encoding field.
GetWebsocketKey
Return Value
Returns the websocket key.
GetWebsocketVersion
Return Value
Returns the requested websocket version.
GetOrigin
Return Value
Returns the value of the origin field.
GetHTTPResultCode
Return Value
For response headers, the function returns the HTTP result code of the previous request. If the incomming header was a request and there was an error during parsing the header,
the function returns the HTTP result code to send as an answer.
GetFieldListCopy
Makes a copy of the field list from the header.
Parameters
byte * & buffer | (Out) A reference to a byte * variable the buffer with the copy will be written to. |
size_t & bufferSize | (Out) A reference to a size_t variable the size of the buffer with the copy will be written to. |
Return Value
After creating the copy, the pointer to the malloced buffer will be stored int the variable bufffer, and the size of it in bufferSize. The caller is responsible for
releasing the buffer if no longer needed by calling free(buffer).
UHTTPParser
class UHTTPParser {
public:
virtual ~UHTTPParser() {}
virtual bool HTTPParserValidateRequest(class HTTPParser * httpParser) = 0;
};
This class is for passing a user to HTTPParser::ParseData(). After parsing the request line, UHTTPParser::HTTPParserValidateRequest() will be called. The app can
check the request and validate it the HTTPParser continues with parsing the rest of the header. This can be done by checking the values of the given HTTPParser
instance. Note that only the request type and the requested resource will be available at this point, but no header fields.
Public functions
HTTPParserValidateRequest
Will be called after parsing the request line to let the app decide, whether to continue parsing or not.
Parameters
HTTPParser * httpParser | The calling HTTPParser object. |
Return Value
True to continue parsing, false to cancel it.
Remarks
When canceling the parsing process, the app must take care of eating up all unused data to clean the byte stream (if the same byte stream should be used for parsing
the next header).
class HTTPHeaderField {
public:
const char * GetValue(size_t idx) const { return values[idx]; }
size_t GetValueCount() const { return valueCount; }
};
Represents a HTTP header field and its values. Even if a field can have more values (that means, that field appears mutltiple times in the header), there also can be
multiple values set, seperated by ';'. Those will be handled as one value only, so an app must seperate them if needed. An header field can be obtained by calling
HTTPParser::GetHeaderField().
Public functions
GetValue
Returns the value of the header field.
Parameters
size_t idx | The index of the value to get. Must be between 0 and GetValueCount(). |
Return Value
The value of the header.
Remarks
Don't manipulate that string (even not by typecasting). If there is need to edit the items, a copy of that return value mast be made.
GetValueCount
Return Value
The number of values available for that field.
HTTPRange
class HTTPRange {
public:
HTTPRange() { this->next = NULL; }
virtual ~HTTPRange() {}
enum {
RANGE_NONE = 0x00,
RANGE_START_END = 0x01,
RANGE_START_ONLY = 0x02,
RANGE_LAST_BYTES = 0x03
} rangeType;
size_t start;
size_t end;
HTTPRange * next;
};
Represents the values of ranges send with an HTTP header. An instance of HTTPRange will be returned by HTTPParser::GetRange(). The retunred instance is the start of a linked list.
So you must not call delete on it! HTTPParser itself will take care of releasing the objects when needed. The class itself has no functions. You have access directly to the fields
of the class.
start
The start of the range given by the HTTP header.
end
The end of the range given by the HTTP header.
rangeType
The type of the range. Depending on the type the values of start and end need to be interpreted. rangeType can be one of the following:
- RANGE_NONE: only used internally for initialization.
- RANGE_START_END: a range with start and end, with the values in the fields start and end.
- RANGE_START_ONLY: only the start had been given. So the application should return everything from of the from the byte given in start to the end of the file. The value of end is undefined and should be ignored.
- RANGE_LAST_BYTES: only the last bytes of a file should be retunred. The number of the bytes to return are set in start. The value of end is undefined and should be ignored.
Data types
Defines / Statics
#define CLRF "\r\n"
#define HTTP_GET_KEY "GET"
#define HTTP_HEAD_KEY "HEAD"
#define HTTP_POST_KEY "POST"
#define HTTP_PUT_KEY "PUT"
#define HTTP_CONNECT_KEY "CONNECT"
#define HTTP_OPTIONS_KEY "OPTIONS"
#define HTTP_TRACE_KEY "TRACE"
#define HTTP_PROPFIND_KEY "PROPFIND"
#define HTTP_MKCOL_KEY "MKCOL"
#define HTTP_COPY_KEY "COPY"
#define HTTP_MOVE_KEY "MOVE"
#define HTTP_DELETE_KEY "DELETE"
#define HTTP_LOCK_KEY "LOCK"
#define HTTP_UNLOCK_KEY "UNLOCK"
#define HTTP_PROPPATCH_KEY "PROPPATCH"
#define HTTP_SYSCLIENT_KEY "SYSCLIENT"
#define HTTP_VERSION_1_0 0x0100
#define HTTP_VERSION_1_1 0x0101
CLRF | The end of line, used inside the HTTP header. The Header ends with on empty line, which is a line that only holds CLRF. |
HTTP_GET_KEY | The keyword for a GET request. |
HTTP_HEAD_KEY | The keyword for a HEAD request. |
HTTP_POST_KEY | The keyword for a POST request. |
HTTP_PUT_KEY | The keyword for a PUT request. |
HTTP_CONNECT_KEY | The keyword for a CONNECT request. |
HTTP_OPTIONS_KEY | The keyword for a OPTIONS request. |
HTTP_TRACE_KEY | The keyword for a TRACE request. |
HTTP_PROPFIND_KEY | The keyword for a PROPFIND request. |
HTTP_MKCOL_KEY | The keyword for a MKCOL request. |
HTTP_COPY_KEY | The keyword for a COPY request. |
HTTP_MOVE_KEY | The keyword for a MOVE request. |
HTTP_DELETE_KEY | The keyword for a DELETE request. |
HTTP_LOCK_KEY | The keyword for a LOCK request. |
HTTP_UNLOCK_KEY | The keyword for a UNLOCK request. |
HTTP_PROPPATCH_KEY | The keyword for a PROPPATCH request. |
HTTP_SYSCLIENT_KEY | Internally AppPlatform specified request - don't use it. |
HTTP_VERSION_1_0 | Dword constant for HTTP 1.0. |
HTTP_VERSION_1_1 | Dword constant for HTTP 1.1. |
http_request_method_t
typedef enum {
HTTP_NONE,
HTTP_GET,
HTTP_HEAD,
HTTP_POST,
HTTP_PUT,
HTTP_CONNECT,
HTTP_OPTIONS,
HTTP_TRACE,
HTTP_PROPFIND,
HTTP_MKCOL,
HTTP_COPY,
HTTP_MOVE,
HTTP_DELETE,
HTTP_LOCK,
HTTP_UNLOCK,
HTTP_PROPPATCH,
HTTP_SYSCLIENT
} http_request_method_t;
This enum is used to define the method to use for a HTTP request and must be one of the following:
HTTP_NONE | (NO HTTP VALUE!!!) Dummy value to initialize variable. |
HTTP_GET | Transfer a current representation of the target resource. |
HTTP_HEAD | Same as GET, but only transfer the status line and header section. |
HTTP_POST | Perform resource - specific processing on the request payload. |
HTTP_PUT | (WebDAV) Replace all current representations of the target resource with the request payload. |
HTTP_CONNECT | Establish a tunnel to the server identified by the target resource. |
HTTP_OPTIONS | (WebDAV) Describe the communication options for the target resource. |
HTTP_TRACE | (WebDAV) Perform a message loop - back test along the path to the target resource. |
HTTP_PROPFIND | (WebDAV) Used to revceive properties for the given resource. |
HTTP_MKCOL | (WebDAV) Creates a new collection at the specified location. |
HTTP_COPY | (WebDAV) Copy the given resource to a specificed location. |
HTTP_MOVE | (WebDAV) Moves the given resource to a specified location. |
HTTP_DELETE | (WebDAV) Deletes the given resource. |
HTTP_LOCK | (WebDAV) Locks the given resource. |
HTTP_UNLOCK | (WebDAV) Unlocks the previously locked resource. |
HTTP_PROPPATCH | (WebDAV) Patches the properties of the given resource. |
HTTP_SYSCLIENT | (Used internally only) Sysclient request to authenticate localsocket requests - internally used. |
http_connection_t
typedef enum {
HTTP_CONNECTION_NONE = 0x00,
HTTP_CONNECTION_KEEP_ALIVE = 0x01,
HTTP_CONNECTION_CLOSE = 0x02,
HTTP_CONNECTION_UPGRADE = 0x04
} http_connection_t;
This enum describes the type of a connection and must be one of the following:
HTTP_CONNECTION_NONE | The connection has no special state. This is the default. |
HTTP_CONNECTION_KEEP_ALIVE | The connection is defiend to keep alive. |
HTTP_CONNECTION_CLOSE | If defined, the connection must be closed after responding or receiving the response to a request. |
HTTP_CONNECTION_UPGRADE | The connection should be (for requests) or had been (for responses) upgraded. Normally that will be used to establish websocket connections. |
Remarks
The way how to handle HTTP_CONNECTION_KEEP_ALIVE and HTTP_CONNECTION_CLOSE depends on the used HTTP protocol version. For HTTP 1.0, close will be the default. So if HTTP_CONNECTION_KEEP_ALIVE is
not set, the connection must close after responding or receiving the response of a request. For HTTP 1.1, keep alive is the default. So the connection should only be closed after receiving the
response or responding a request, if HTTP_CONNECTION_CLOSE is set.
http_upgrade_t
typedef enum {
HTTP_UPGRADE_NONE,
HTTP_UPGRADE_WEBSOCKET
} http_upgrade_t;
These are the upgrades the a request can define.
HTTP_UPGRADE_NONE | No upgrade, just a normal connection. This is the default. |
HTTP_UPGRADE_WEBSOCKET | The connection had been or should be upgraded to be a websocket connection. |
http_encoding_t
typedef enum {
HTTP_ENCODING_NONE, // Default
HTTP_ENCODING_GZIP
} http_encoding_t;
These are the encoding types for a HTTP connection (the moment, only gzip is supported as special encoding).
HTTP_ENCODING_NONE | No encoding. This is the default. |
HTTP_ENCODING_GZIP | The data received or sent is in gzip compressed format. |
http_result_t
typedef struct {
int code;
const char * description;
size_t descLen;
} http_result_t;
This structure holds an HTTP result code in three parts: an integer value (easier to use for checking inside the code), a descroption string (actualle the one
tha must be put to the header) and the length of the description. The HTTPLib defines constants for most of the result codes (see below). That constans
can be used to pass to functions loke HTTPHeader::SetHTTPResult(). For more information about the resturn codes see RFC 7231, Page 41.
Values
int code | The HTTP code as interger value. |
const char * description | The description of the code. |
size_t descLen | The length of the description. |
Remarks
HTTPLib has most of the HTTP return codes predefined in HTTP_* constants. The interger code of them is the same a the code of the result. So HTTP 100 has the integer number 100.
The Description is a combination of the code number and the description defined by RFC (e. G. "200 OK"). The following result codes are offered by HTTPLib:
HTTP_100 | Continue |
HTTP_101 | Switching Protocols |
HTTP_200 | OK |
HTTP_201 | Created |
HTTP_202 | Accepted |
HTTP_203 | Non-Authoritative Information |
HTTP_204 | No Content |
HTTP_205 | Reset Content |
HTTP_206 | Partial Content |
HTTP_207 | Multi-Status |
HTTP_300 | Multiple Choices |
HTTP_301 | Moved Permanently |
HTTP_302 | Found |
HTTP_303 | See Other |
HTTP_304 | Not Modified |
HTTP_305 | Use Proxy |
HTTP_307 | Temporary Redirect |
HTTP_400 | Bad Request |
HTTP_401 | Unauthorized |
HTTP_402 | Payment Required |
HTTP_403 | Forbidden |
HTTP_404 | Not Found |
HTTP_405 | Method Not Allowed |
HTTP_406 | Not Acceptable |
HTTP_407 | Proxy Authentication Required |
HTTP_408 | Request Timeout |
HTTP_409 | Conflict |
HTTP_410 | Gone |
HTTP_411 | Length Required |
HTTP_412 | Precondition Failed |
HTTP_413 | Payload Too Large |
HTTP_414 | URI Too Long |
HTTP_415 | Unsupported Media Type |
HTTP_416 | Range Not Satisfiable |
HTTP_417 | Expectation Failed |
HTTP_423 | Locked |
HTTP_426 | Upgrade Required |
HTTP_500 | Internal Server Error |
HTTP_501 | Not Implemented |
HTTP_502 | Bad Gateway |
HTTP_503 | Service Unavailable |
HTTP_504 | Gateway Timeout |
HTTP_505 | HTTP Version Not Supported |
HTTP_507 | Insufficient Storage |
Code examples
// This example shows how to use the HTTP library to response to an incomming HTTP request.
// The class itself is a subclass of USocket (for receiving data) and UHTTPParser
// (to check the request and only accept GET requests).
void MyApp::SocketRecvResult(ISocket * const socket, void * buf, size_t len) {
// parser is an HTTPParser instance of the object the function here is part of.
this->parser->ParseData(buf, len, this);
switch (this->parser->GetParseResult())
{
case HTTPParser::HTTP_PARSE_NEED_DATA:
// More data need for the header. Note that we must make a partial Recv() call here.
socket->Recv(buffer, 8192, true);
break;
case HTTPParser::HTTP_PARSE_ERROR:
debug->printf("Error while parsing the HTTP header.");
socket->Shutdown();
break;
case HTTPParser::HTTP_PARSE_CANCEL: // Unsupported request - see below
debug->printf("Parsing canceled.");
socket->Shutdown();
break;
case HTTPParser::HTTP_PARSE_DONE:
this->ResponseGETRequest(socket);
break;
}
}
// The callback function from UHTTPParser. We only accept GET requests...
bool MyApp::HTTPParserValidateRequest(class HTTPParser * httpParser)
{
if (httpParser->GetRequestMethod()) == HTTPLib::HTTP_GET) return true;
debug->printf("Only GET requests are supported.");
return false;
}
void MyApp::ResponseGETRequest(ISocket * socket)
{
const char * responseData = "<!DOCTYPE html><html xmlns=\"http://www.w3.org/1999/xhtml\"><body>Hello World</body></html>"
size_t responseLen = strlen(responseData);
// Use the object on the stack - no need to delete it later.
HTTPHeader header;
header.SetHTTPVersion(this->parser->GetMajorVersion(), this->parser->GetMinorVersion());
header.SetHTTPResult(&HTTPLib::HTTP_200);
header.SetContentSize(responseLen);
// Finally - send everything.
socket->Send(header.GetHeaderData(), header.GetHeaderSize());
socket->Send(responseData, responseLen);
}