CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
cmlabs::HTTPReply Class Reference

A parsed or generated HTTP response. More...

#include <NetworkProtocols.h>

Collaboration diagram for cmlabs::HTTPReply:
[legend]

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.
bool setDecodedContent (const char *buffer, uint32 size)
 Replace the body with already-decoded bytes (e.g.
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 HTTPReplyCreateErrorReply (uint8 type)
 Build a canned error reply.
static HTTPReplyCreateAuthorizationReply (const char *realm)
 Build a 401 reply requesting Basic authentication.
static HTTPReplyCreateWebsocketHTTPReply (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.
bool chunked
 Body uses HTTP/1.1 chunked transfer encoding.
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.

Detailed Description

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.

Note
Not thread-safe; owned by one connection thread.

Definition at line 680 of file NetworkProtocols.h.

Constructor & Destructor Documentation

◆ HTTPReply() [1/2]

cmlabs::HTTPReply::HTTPReply ( uint64 source = 0)

Construct an empty reply.

Parameters
sourcePacked uint64 endpoint of the sender.

Definition at line 1422 of file NetworkProtocols.cpp.

References chunked, contentLength, data, cmlabs::GetTimeNow(), headerLength, keepAlive, source, time, and type.

Referenced by CreateAuthorizationReply(), CreateErrorReply(), CreateWebsocketHTTPReply(), and HTTPReply().

◆ HTTPReply() [2/2]

cmlabs::HTTPReply::HTTPReply ( HTTPReply * reply)

Deep-copy constructor.

Parameters
replyReply to copy (not modified).

Definition at line 1432 of file NetworkProtocols.cpp.

References chunked, contentLength, data, entries, headerLength, HTTPReply(), keepAlive, params, source, time, and type.

◆ ~HTTPReply()

cmlabs::HTTPReply::~HTTPReply ( )
virtual

Definition at line 1449 of file NetworkProtocols.cpp.

References data.

Member Function Documentation

◆ CreateAuthorizationReply()

HTTPReply * cmlabs::HTTPReply::CreateAuthorizationReply ( const char * realm)
static

Build a 401 reply requesting Basic authentication.

Parameters
realmRealm name shown by the browser.
Returns
New reply owned by the caller.

Definition at line 1409 of file NetworkProtocols.cpp.

References createAuthorizationReply(), and HTTPReply().

Referenced by cmlabs::RequestGateway::callInternalAPI().

◆ createAuthorizationReply()

bool cmlabs::HTTPReply::createAuthorizationReply ( const char * realm)

Build a 401 Basic-auth challenge.

Parameters
realmRealm name.
Returns
true on success.

Definition at line 1524 of file NetworkProtocols.cpp.

References contentLength, data, cmlabs::GetHTTPTime(), headerLength, cmlabs::HTTP_Status, HTTP_UNAUTHORIZED, and time.

Referenced by CreateAuthorizationReply().

◆ createErrorPage()

bool cmlabs::HTTPReply::createErrorPage ( uint8 status,
uint64 time,
const char * serverName,
bool keepAlive )

Build a canned error page for the given status.

Parameters
statusHTTP_* status id.
timeCurrent time.
serverNameServer header.
keepAliveKeep the connection open.
Returns
true on success.

Definition at line 1503 of file NetworkProtocols.cpp.

References contentLength, data, cmlabs::GetHTTPTime(), headerLength, cmlabs::HTTP_Status, keepAlive, and time.

Referenced by createFromFile(), and cmlabs::RequestGateway::replyToClient().

◆ CreateErrorReply()

HTTPReply * cmlabs::HTTPReply::CreateErrorReply ( uint8 type)
static

◆ createFromFile()

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.

Parameters
timeCurrent time (ms epoch).
serverNameServer header value.
ifLastModClient's If-Modified-Since timestamp (0 = none).
keepAliveKeep the connection open.
cacheAllow client caching.
filenamePath of the file to serve.
Returns
true when the reply was assembled (404 error page when missing).

Definition at line 1612 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().

◆ createOptionsResponse()

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.

Parameters
statusHTTP_* status id.
timeCurrent time.
serverNameServer header.
keepAliveKeep the connection open.
originAccess-Control-Allow-Origin value.
operationsAccess-Control-Allow-Methods value.
Returns
true on success.

Definition at line 1457 of file NetworkProtocols.cpp.

References contentLength, data, cmlabs::GetHTTPTime(), headerLength, keepAlive, and time.

Referenced by cmlabs::RequestGateway::replyToClient().

◆ createPage()

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.

Parameters
statusHTTP_* status id.
timeCurrent time (ms epoch) for the Date header.
serverNameServer header value.
lastModLast-Modified timestamp (0 = omit).
keepAliveKeep the connection open after sending.
cacheAllow client caching (otherwise Cache-Control: No-Cache).
contentTypeMIME type of content.
contentBody bytes (copied).
contentSizeBody size (strlen when 0 and text).
additionalHeaderEntriesOptional extra raw header lines.
Returns
true when the reply was assembled.

Definition at line 1571 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().

◆ CreateWebsocketHTTPReply()

HTTPReply * cmlabs::HTTPReply::CreateWebsocketHTTPReply ( const char * key,
const char * version )
static

Build the "101 Switching Protocols" reply for a WebSocket handshake.

Parameters
keyClient's Sec-WebSocket-Key (the SHA1-based accept token is derived from it).
versionClient's Sec-WebSocket-Version.
Returns
New reply owned by the caller.

Definition at line 1415 of file NetworkProtocols.cpp.

References createWebsocketHTTPReply(), and HTTPReply().

Referenced by cmlabs::NetworkChannel::HTTPServerRun().

◆ createWebsocketHTTPReply()

bool cmlabs::HTTPReply::createWebsocketHTTPReply ( const char * key,
const char * version )

Build the 101 WebSocket handshake completion in-place.

Parameters
keyClient's Sec-WebSocket-Key.
versionSec-WebSocket-Version.
Returns
true on success.

Definition at line 1542 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().

◆ getContent()

const char * cmlabs::HTTPReply::getContent ( uint32 & size)

Get the (decoded) response body.

Parameters
sizeOut: body size.
Returns
Body bytes owned by this object, or NULL.

Definition at line 1817 of file NetworkProtocols.cpp.

References contentLength, data, and headerLength.

Referenced by cmlabs::LLMConnection::interact().

◆ getHeaderEntry()

const char * cmlabs::HTTPReply::getHeaderEntry ( const char * entry)

Look up a header field (case-insensitive).

Parameters
entryHeader name.
Returns
The value, or NULL. Valid while this object lives.

Definition at line 1827 of file NetworkProtocols.cpp.

References entries.

Referenced by getProtocol(), and isWebsocketUpgrade().

◆ getProtocol()

const char * cmlabs::HTTPReply::getProtocol ( )
Returns
The protocol/version string from the status line (e.g. "HTTP/1.1").

Definition at line 1835 of file NetworkProtocols.cpp.

References getHeaderEntry().

◆ getRawContent()

const char * cmlabs::HTTPReply::getRawContent ( uint32 & size)

Get the raw, untransformed body bytes.

Parameters
sizeOut: body size.
Returns
Body bytes owned by this object, or NULL.

Definition at line 1822 of file NetworkProtocols.cpp.

References contentLength, data, and headerLength.

◆ getSize()

uint32 cmlabs::HTTPReply::getSize ( )
Returns
Total size of the serialised reply (header + body) in bytes.

Definition at line 1839 of file NetworkProtocols.cpp.

References contentLength, data, and headerLength.

Referenced by cmlabs::RequestGateway::callInternalAPI(), cmlabs::RequestGateway::receiveHTTPRequest(), and cmlabs::HTTPProtocol::SendHTTPReply().

◆ isWebsocketUpgrade()

bool cmlabs::HTTPReply::isWebsocketUpgrade ( )
Returns
true when this reply completes a WebSocket upgrade (status 101).

Definition at line 1662 of file NetworkProtocols.cpp.

References getHeaderEntry(), HTTP_SWITCH_PROTOCOL, stricmp, and type.

Referenced by cmlabs::NetworkChannel::HTTPClientRun(), and cmlabs::NetworkManager::WebsocketTest().

◆ processContent()

bool cmlabs::HTTPReply::processContent ( const char * buffer,
uint32 size )

Append body bytes after the header has been parsed.

Parameters
bufferBody bytes.
sizeByte count.
Returns
true when the body is complete.

Definition at line 1795 of file NetworkProtocols.cpp.

References contentLength, data, and headerLength.

Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply().

◆ processHeader()

bool cmlabs::HTTPReply::processHeader ( const char * buffer,
uint32 size,
bool & isInvalid )

Parse the response header block from raw bytes.

Parameters
bufferRaw bytes.
sizeByte count.
isInvalidOut: set true when the bytes are not valid HTTP.
Returns
true when the header was fully parsed.

Definition at line 1679 of file NetworkProtocols.cpp.

References chunked, 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().

◆ setDecodedContent()

bool cmlabs::HTTPReply::setDecodedContent ( const char * buffer,
uint32 size )

Replace the body with already-decoded bytes (e.g.

after chunked transfer decoding); the parsed header block is kept unchanged.

Parameters
bufferDecoded body bytes (copied).
sizeByte count.
Returns
true when the body was set.

Definition at line 1803 of file NetworkProtocols.cpp.

References contentLength, data, and headerLength.

Referenced by cmlabs::HTTPProtocol::ReceiveHTTPReply().

Member Data Documentation

◆ chunked

bool cmlabs::HTTPReply::chunked

Body uses HTTP/1.1 chunked transfer encoding.

Definition at line 780 of file NetworkProtocols.h.

Referenced by HTTPReply(), HTTPReply(), processHeader(), and cmlabs::HTTPProtocol::ReceiveHTTPReply().

◆ contentLength

◆ data

◆ entries

std::map<std::string, std::string> cmlabs::HTTPReply::entries

Parsed header fields (name to value).

Definition at line 783 of file NetworkProtocols.h.

Referenced by getHeaderEntry(), HTTPReply(), and processHeader().

◆ headerLength

◆ keepAlive

bool cmlabs::HTTPReply::keepAlive

Whether the connection stays open after this reply.

Definition at line 779 of file NetworkProtocols.h.

Referenced by createErrorPage(), createFromFile(), createOptionsResponse(), createPage(), HTTPReply(), HTTPReply(), and processHeader().

◆ params

std::map<std::string, std::string> cmlabs::HTTPReply::params

Auxiliary parsed parameters.

Definition at line 784 of file NetworkProtocols.h.

Referenced by HTTPReply().

◆ source

uint64 cmlabs::HTTPReply::source

Packed uint64 endpoint of the sender (0 if local).

Definition at line 776 of file NetworkProtocols.h.

Referenced by HTTPReply(), and HTTPReply().

◆ time

uint64 cmlabs::HTTPReply::time

Timestamp of creation/receipt (ms epoch).

Definition at line 775 of file NetworkProtocols.h.

Referenced by createAuthorizationReply(), createErrorPage(), createFromFile(), createOptionsResponse(), createPage(), HTTPReply(), HTTPReply(), and processHeader().

◆ type


The documentation for this class was generated from the following files: