|
CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
|
A parsed or generated HTTP response. More...
#include <NetworkProtocols.h>
Public Member Functions | |
| HTTPReply (uint64 source=0) | |
| Construct an empty reply. | |
| HTTPReply (HTTPReply *reply) | |
| Deep-copy constructor. | |
| virtual | ~HTTPReply () |
| bool | isWebsocketUpgrade () |
| bool | processHeader (const char *buffer, uint32 size, bool &isInvalid) |
| Parse the response header block from raw bytes. | |
| bool | processContent (const char *buffer, uint32 size) |
| Append body bytes after the header has been parsed. | |
| const char * | getHeaderEntry (const char *entry) |
| Look up a header field (case-insensitive). | |
| const char * | getProtocol () |
| const char * | getContent (uint32 &size) |
| Get the (decoded) response body. | |
| const char * | getRawContent (uint32 &size) |
| Get the raw, untransformed body bytes. | |
| bool | createPage (uint8 status, uint64 time, const char *serverName, uint64 lastMod, bool keepAlive, bool cache, const char *contentType, const char *content, uint32 contentSize=0, const char *additionalHeaderEntries=NULL) |
| Build a complete response with headers and content. | |
| bool | createFromFile (uint64 time, const char *serverName, uint64 ifLastMod, bool keepAlive, bool cache, const char *filename) |
| Build a response by reading a file from disk (MIME type from extension). | |
| bool | createOptionsResponse (uint8 status, uint64 time, const char *serverName, bool keepAlive, const char *origin, const char *operations) |
| Build a CORS-preflight (OPTIONS) response. | |
| bool | createErrorPage (uint8 status, uint64 time, const char *serverName, bool keepAlive) |
| Build a canned error page for the given status. | |
| bool | createAuthorizationReply (const char *realm) |
| Build a 401 Basic-auth challenge. | |
| bool | createWebsocketHTTPReply (const char *key, const char *version) |
| Build the 101 WebSocket handshake completion in-place. | |
| uint32 | getSize () |
Static Public Member Functions | |
| static HTTPReply * | CreateErrorReply (uint8 type) |
| Build a canned error reply. | |
| static HTTPReply * | CreateAuthorizationReply (const char *realm) |
| Build a 401 reply requesting Basic authentication. | |
| static HTTPReply * | CreateWebsocketHTTPReply (const char *key, const char *version) |
| Build the "101 Switching Protocols" reply for a WebSocket handshake. | |
Public Attributes | |
| uint8 | type |
| HTTP_* status id of this reply. | |
| uint64 | time |
| Timestamp of creation/receipt (ms epoch). | |
| uint64 | source |
| Packed uint64 endpoint of the sender (0 if local). | |
| uint32 | headerLength |
| Header block length within ::data, in bytes. | |
| uint32 | contentLength |
| Body length in bytes. | |
| bool | keepAlive |
| Whether the connection stays open after this reply. | |
| char * | data |
| Owned raw reply bytes (header followed by body). | |
| std::map< std::string, std::string > | entries |
| Parsed header fields (name to value). | |
| std::map< std::string, std::string > | params |
| Auxiliary parsed parameters. | |
A parsed or generated HTTP response.
Counterpart to HTTPRequest: on the client side it is filled from socket bytes (processHeader()/processContent()); on the server side it is built with createPage(), createFromFile(), createErrorPage() and friends, then serialised from the owned ::data buffer. Also carries the "101 Switching Protocols" reply that completes a WebSocket handshake.
Definition at line 680 of file NetworkProtocols.h.
| cmlabs::HTTPReply::HTTPReply | ( | uint64 | source = 0 | ) |
Construct an empty reply.
| source | Packed uint64 endpoint of the sender. |
Definition at line 1422 of file NetworkProtocols.cpp.
References contentLength, data, cmlabs::GetTimeNow(), headerLength, keepAlive, source, time, and type.
Referenced by CreateAuthorizationReply(), CreateErrorReply(), CreateWebsocketHTTPReply(), and HTTPReply().
| cmlabs::HTTPReply::HTTPReply | ( | HTTPReply * | reply | ) |
Deep-copy constructor.
| reply | Reply to copy (not modified). |
Definition at line 1431 of file NetworkProtocols.cpp.
References contentLength, data, entries, headerLength, HTTPReply(), keepAlive, params, source, time, and type.
|
virtual |
Definition at line 1444 of file NetworkProtocols.cpp.
References data.
|
static |
Build a 401 reply requesting Basic authentication.
| realm | Realm name shown by the browser. |
Definition at line 1409 of file NetworkProtocols.cpp.
References createAuthorizationReply(), and HTTPReply().
Referenced by cmlabs::RequestGateway::callInternalAPI().
| bool cmlabs::HTTPReply::createAuthorizationReply | ( | const char * | realm | ) |
Build a 401 Basic-auth challenge.
| realm | Realm name. |
Definition at line 1519 of file NetworkProtocols.cpp.
References contentLength, data, cmlabs::GetHTTPTime(), headerLength, cmlabs::HTTP_Status, HTTP_UNAUTHORIZED, and time.
Referenced by CreateAuthorizationReply().
| bool cmlabs::HTTPReply::createErrorPage | ( | uint8 | status, |
| uint64 | time, | ||
| const char * | serverName, | ||
| bool | keepAlive ) |
Build a canned error page for the given status.
| status | HTTP_* status id. |
| time | Current time. |
| serverName | Server header. |
| keepAlive | Keep the connection open. |
Definition at line 1498 of file NetworkProtocols.cpp.
References contentLength, data, cmlabs::GetHTTPTime(), headerLength, cmlabs::HTTP_Status, keepAlive, and time.
Referenced by createFromFile(), and cmlabs::RequestGateway::replyToClient().
|
static |
Build a canned error reply.
| type | One of the HTTP_* status ids. |
Definition at line 1403 of file NetworkProtocols.cpp.
References HTTPReply(), and type.
Referenced by cmlabs::NetworkChannel::HTTPServerRun(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::NetworkManager::makeHTTPRequest(), cmlabs::HTTPProtocol::ReceiveHTTPReply(), and cmlabs::NetworkChannel::sendReceiveHTTPRequest().
| bool cmlabs::HTTPReply::createFromFile | ( | uint64 | time, |
| const char * | serverName, | ||
| uint64 | ifLastMod, | ||
| bool | keepAlive, | ||
| bool | cache, | ||
| const char * | filename ) |
Build a response by reading a file from disk (MIME type from extension).
Replies 304 Use-Local-Copy when the file is unchanged since ifLastMod.
| time | Current time (ms epoch). |
| serverName | Server header value. |
| ifLastMod | Client's If-Modified-Since timestamp (0 = none). |
| keepAlive | Keep the connection open. |
| cache | Allow client caching. |
| filename | Path of the file to serve. |
Definition at line 1607 of file NetworkProtocols.cpp.
References createErrorPage(), createPage(), cmlabs::utils::FileDetails::doesExist, cmlabs::utils::GetFileDetails(), cmlabs::html::GetMimeType(), cmlabs::GetTimeNow(), HTTP_FILE_NOT_FOUND, HTTP_OK, HTTP_USE_LOCAL_COPY, cmlabs::utils::FileDetails::isDirectory, keepAlive, cmlabs::utils::FileDetails::lastModifyTime, cmlabs::utils::ReadAFile(), cmlabs::utils::ReadAFileString(), cmlabs::utils::StringFormat(), and time.
Referenced by cmlabs::RequestGateway::callInternalAPI(), and cmlabs::RequestGateway::receiveHTTPRequest().
| bool cmlabs::HTTPReply::createOptionsResponse | ( | uint8 | status, |
| uint64 | time, | ||
| const char * | serverName, | ||
| bool | keepAlive, | ||
| const char * | origin, | ||
| const char * | operations ) |
Build a CORS-preflight (OPTIONS) response.
| status | HTTP_* status id. |
| time | Current time. |
| serverName | Server header. |
| keepAlive | Keep the connection open. |
| origin | Access-Control-Allow-Origin value. |
| operations | Access-Control-Allow-Methods value. |
Definition at line 1452 of file NetworkProtocols.cpp.
References contentLength, data, cmlabs::GetHTTPTime(), headerLength, keepAlive, and time.
Referenced by cmlabs::RequestGateway::replyToClient().
| bool cmlabs::HTTPReply::createPage | ( | uint8 | status, |
| uint64 | time, | ||
| const char * | serverName, | ||
| uint64 | lastMod, | ||
| bool | keepAlive, | ||
| bool | cache, | ||
| const char * | contentType, | ||
| const char * | content, | ||
| uint32 | contentSize = 0, | ||
| const char * | additionalHeaderEntries = NULL ) |
Build a complete response with headers and content.
| status | HTTP_* status id. |
| time | Current time (ms epoch) for the Date header. |
| serverName | Server header value. |
| lastMod | Last-Modified timestamp (0 = omit). |
| keepAlive | Keep the connection open after sending. |
| cache | Allow client caching (otherwise Cache-Control: No-Cache). |
| contentType | MIME type of content. |
| content | Body bytes (copied). |
| contentSize | Body size (strlen when 0 and text). |
| additionalHeaderEntries | Optional extra raw header lines. |
Definition at line 1566 of file NetworkProtocols.cpp.
References contentLength, data, cmlabs::GetHTTPTime(), headerLength, cmlabs::HTTP_Status, keepAlive, and time.
Referenced by cmlabs::RequestGateway::callInternalAPI(), createFromFile(), cmlabs::HTTPTestServer::receiveHTTPRequest(), and cmlabs::RequestGateway::replyToClient().
|
static |
Build the "101 Switching Protocols" reply for a WebSocket handshake.
| key | Client's Sec-WebSocket-Key (the SHA1-based accept token is derived from it). |
| version | Client's Sec-WebSocket-Version. |
Definition at line 1415 of file NetworkProtocols.cpp.
References createWebsocketHTTPReply(), and HTTPReply().
Referenced by cmlabs::NetworkChannel::HTTPServerRun().
| bool cmlabs::HTTPReply::createWebsocketHTTPReply | ( | const char * | key, |
| const char * | version ) |
Build the 101 WebSocket handshake completion in-place.
| key | Client's Sec-WebSocket-Key. |
| version | Sec-WebSocket-Version. |
Definition at line 1537 of file NetworkProtocols.cpp.
References hash::SHA1::add(), base64_encode(), contentLength, data, hash::SHA1::getHashRaw(), headerLength, cmlabs::HTTP_Status, HTTP_SWITCH_PROTOCOL, hash::SHA1::reset(), and cmlabs::utils::StringFormat().
Referenced by CreateWebsocketHTTPReply().
| const char * cmlabs::HTTPReply::getContent | ( | uint32 & | size | ) |
Get the (decoded) response body.
| size | Out: body size. |
Definition at line 1791 of file NetworkProtocols.cpp.
References contentLength, data, and headerLength.
| const char * cmlabs::HTTPReply::getHeaderEntry | ( | const char * | entry | ) |
Look up a header field (case-insensitive).
| entry | Header name. |
Definition at line 1801 of file NetworkProtocols.cpp.
References entries.
Referenced by getProtocol(), and isWebsocketUpgrade().
| const char * cmlabs::HTTPReply::getProtocol | ( | ) |
Definition at line 1809 of file NetworkProtocols.cpp.
References getHeaderEntry().
| const char * cmlabs::HTTPReply::getRawContent | ( | uint32 & | size | ) |
Get the raw, untransformed body bytes.
| size | Out: body size. |
Definition at line 1796 of file NetworkProtocols.cpp.
References contentLength, data, and headerLength.
| uint32 cmlabs::HTTPReply::getSize | ( | ) |
Definition at line 1813 of file NetworkProtocols.cpp.
References contentLength, data, and headerLength.
Referenced by cmlabs::RequestGateway::callInternalAPI(), cmlabs::RequestGateway::receiveHTTPRequest(), and cmlabs::HTTPProtocol::SendHTTPReply().
| bool cmlabs::HTTPReply::isWebsocketUpgrade | ( | ) |
Definition at line 1657 of file NetworkProtocols.cpp.
References getHeaderEntry(), HTTP_SWITCH_PROTOCOL, stricmp, and type.
Referenced by cmlabs::NetworkChannel::HTTPClientRun(), and cmlabs::NetworkManager::WebsocketTest().
| bool cmlabs::HTTPReply::processContent | ( | const char * | buffer, |
| uint32 | size ) |
Append body bytes after the header has been parsed.
| buffer | Body bytes. |
| size | Byte count. |
Definition at line 1783 of file NetworkProtocols.cpp.
References contentLength, data, and headerLength.
Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply().
| bool cmlabs::HTTPReply::processHeader | ( | const char * | buffer, |
| uint32 | size, | ||
| bool & | isInvalid ) |
Parse the response header block from raw bytes.
| buffer | Raw bytes. |
| size | Byte count. |
| isInvalid | Out: set true when the bytes are not valid HTTP. |
Definition at line 1674 of file NetworkProtocols.cpp.
References contentLength, data, entries, cmlabs::utils::GetNextLineEnd(), cmlabs::GetTimeFromString(), headerLength, HTTP_ACCESS_DENIED, HTTP_FILE_NOT_FOUND, HTTP_INTERNAL_ERROR, HTTP_MOVED_PERMANENTLY, HTTP_OK, HTTP_SWITCH_PROTOCOL, HTTP_USE_LOCAL_COPY, keepAlive, stricmp, time, and type.
Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply().
| uint32 cmlabs::HTTPReply::contentLength |
Body length in bytes.
Definition at line 773 of file NetworkProtocols.h.
Referenced by createAuthorizationReply(), createErrorPage(), createOptionsResponse(), createPage(), createWebsocketHTTPReply(), getContent(), getRawContent(), getSize(), HTTPReply(), HTTPReply(), processContent(), processHeader(), and cmlabs::HTTPProtocol::ReceiveHTTPReply().
| char* cmlabs::HTTPReply::data |
Owned raw reply bytes (header followed by body).
Definition at line 775 of file NetworkProtocols.h.
Referenced by createAuthorizationReply(), createErrorPage(), createOptionsResponse(), createPage(), createWebsocketHTTPReply(), getContent(), getRawContent(), getSize(), HTTPReply(), HTTPReply(), processContent(), processHeader(), cmlabs::HTTPProtocol::SendHTTPReply(), and ~HTTPReply().
| std::map<std::string, std::string> cmlabs::HTTPReply::entries |
Parsed header fields (name to value).
Definition at line 777 of file NetworkProtocols.h.
Referenced by getHeaderEntry(), HTTPReply(), and processHeader().
| uint32 cmlabs::HTTPReply::headerLength |
Header block length within ::data, in bytes.
Definition at line 772 of file NetworkProtocols.h.
Referenced by createAuthorizationReply(), createErrorPage(), createOptionsResponse(), createPage(), createWebsocketHTTPReply(), getContent(), getRawContent(), getSize(), HTTPReply(), HTTPReply(), processContent(), processHeader(), and cmlabs::HTTPProtocol::ReceiveHTTPReply().
| bool cmlabs::HTTPReply::keepAlive |
Whether the connection stays open after this reply.
Definition at line 774 of file NetworkProtocols.h.
Referenced by createErrorPage(), createFromFile(), createOptionsResponse(), createPage(), HTTPReply(), HTTPReply(), and processHeader().
| std::map<std::string, std::string> cmlabs::HTTPReply::params |
Auxiliary parsed parameters.
Definition at line 778 of file NetworkProtocols.h.
Referenced by HTTPReply().
| uint64 cmlabs::HTTPReply::source |
Packed uint64 endpoint of the sender (0 if local).
Definition at line 771 of file NetworkProtocols.h.
Referenced by HTTPReply(), and HTTPReply().
| uint64 cmlabs::HTTPReply::time |
Timestamp of creation/receipt (ms epoch).
Definition at line 770 of file NetworkProtocols.h.
Referenced by createAuthorizationReply(), createErrorPage(), createFromFile(), createOptionsResponse(), createPage(), HTTPReply(), HTTPReply(), and processHeader().
| uint8 cmlabs::HTTPReply::type |
HTTP_* status id of this reply.
Definition at line 769 of file NetworkProtocols.h.
Referenced by CreateErrorReply(), cmlabs::NetworkChannel::createWebsocketConnection(), HTTPReply(), HTTPReply(), cmlabs::HTTPServerTest(), isWebsocketUpgrade(), processHeader(), and cmlabs::TestWebRequestClient::run().