WPILibC++ 2024.3.2
wpi::HttpServerConnection Class Referenceabstract

#include <wpinet/HttpServerConnection.h>

Inheritance diagram for wpi::HttpServerConnection:
wpi::HttpWebSocketServerConnection< Derived >

Public Member Functions

 HttpServerConnection (std::shared_ptr< uv::Stream > stream)
 
virtual ~HttpServerConnection ()=default
 

Protected Member Functions

virtual void ProcessRequest ()=0
 Process an incoming HTTP request. More...
 
virtual void BuildCommonHeaders (raw_ostream &os)
 Build common response headers. More...
 
virtual void BuildHeader (raw_ostream &os, int code, std::string_view codeText, std::string_view contentType, uint64_t contentLength, std::string_view extra={})
 Build HTTP response header, along with other header information like mimetype. More...
 
void SendData (std::span< const uv::Buffer > bufs, bool closeAfter=false)
 Send data to client. More...
 
virtual void SendResponse (int code, std::string_view codeText, std::string_view contentType, std::string_view content, std::string_view extraHeader={})
 Send HTTP response, along with other header information like mimetype. More...
 
virtual void SendStaticResponse (int code, std::string_view codeText, std::string_view contentType, std::string_view content, bool gzipped, std::string_view extraHeader={})
 Send HTTP response from static data, along with other header information like mimetype. More...
 
virtual void SendError (int code, std::string_view message={})
 Send error header and message. More...
 

Protected Attributes

HttpParser m_request {HttpParser::kRequest}
 The HTTP request. More...
 
bool m_keepAlive = false
 Whether the connection should be kept alive. More...
 
bool m_acceptGzip = false
 If gzip is an acceptable encoding for responses. More...
 
uv::Streamm_stream
 The underlying stream for the connection. More...
 
sig::ScopedConnection m_dataConn
 The header reader connection. More...
 
sig::ScopedConnection m_endConn
 The end stream connection. More...
 
sig::Connection m_messageCompleteConn
 The message complete connection. More...
 

Constructor & Destructor Documentation

◆ HttpServerConnection()

wpi::HttpServerConnection::HttpServerConnection ( std::shared_ptr< uv::Stream stream)
explicit

◆ ~HttpServerConnection()

virtual wpi::HttpServerConnection::~HttpServerConnection ( )
virtualdefault

Member Function Documentation

◆ BuildCommonHeaders()

virtual void wpi::HttpServerConnection::BuildCommonHeaders ( raw_ostream os)
protectedvirtual

Build common response headers.

Called by SendHeader() to send headers common to every response. Each line must be terminated with "\r\n".

The default implementation sends the following: "Server: WebServer/1.0\r\n" "Cache-Control: no-store, no-cache, must-revalidate, pre-check=0, " "post-check=0, max-age=0\r\n" "Pragma: no-cache\r\n" "Expires: Mon, 3 Jan 2000 12:34:56 GMT\r\n"

These parameters should ensure the browser does not cache the response. A browser should connect for each file and not serve files from its cache.

Parameters
osresponse stream

◆ BuildHeader()

virtual void wpi::HttpServerConnection::BuildHeader ( raw_ostream os,
int  code,
std::string_view  codeText,
std::string_view  contentType,
uint64_t  contentLength,
std::string_view  extra = {} 
)
protectedvirtual

Build HTTP response header, along with other header information like mimetype.

Calls BuildCommonHeaders().

Parameters
osresponse stream
codeHTTP response code (e.g. 200)
codeTextHTTP response code text (e.g. "OK")
contentTypeMIME content type (e.g. "text/plain")
contentLengthLength of content. If 0 is provided, m_keepAlive will be set to false.
extraExtra HTTP headers to send, including final "\r\n"

◆ ProcessRequest()

virtual void wpi::HttpServerConnection::ProcessRequest ( )
protectedpure virtual

Process an incoming HTTP request.

This is called after the incoming message completes (e.g. from the HttpParser::messageComplete callback).

The implementation should read request details from m_request and call the appropriate Send() functions to send a response back to the client.

◆ SendData()

void wpi::HttpServerConnection::SendData ( std::span< const uv::Buffer bufs,
bool  closeAfter = false 
)
protected

Send data to client.

This is a convenience wrapper around m_stream.Write() to provide auto-close functionality.

Parameters
bufsBuffers to write. Deallocate() will be called on each buffer after the write completes. If different behavior is desired, call m_stream.Write() directly instead.
closeAfterclose the connection after the write completes

◆ SendError()

virtual void wpi::HttpServerConnection::SendError ( int  code,
std::string_view  message = {} 
)
protectedvirtual

Send error header and message.

This provides standard code responses for 400, 401, 403, 404, 500, and 503. Other codes will be reported as 501. For arbitrary code handling, use SendResponse() instead.

Parameters
codeHTTP error code (e.g. 404)
messageAdditional message text

◆ SendResponse()

virtual void wpi::HttpServerConnection::SendResponse ( int  code,
std::string_view  codeText,
std::string_view  contentType,
std::string_view  content,
std::string_view  extraHeader = {} 
)
protectedvirtual

Send HTTP response, along with other header information like mimetype.

Calls BuildHeader().

Parameters
codeHTTP response code (e.g. 200)
codeTextHTTP response code text (e.g. "OK")
contentTypeMIME content type (e.g. "text/plain")
contentResponse message content
extraHeaderExtra HTTP headers to send, including final "\r\n"

◆ SendStaticResponse()

virtual void wpi::HttpServerConnection::SendStaticResponse ( int  code,
std::string_view  codeText,
std::string_view  contentType,
std::string_view  content,
bool  gzipped,
std::string_view  extraHeader = {} 
)
protectedvirtual

Send HTTP response from static data, along with other header information like mimetype.

Calls BuildHeader(). Supports gzip pre-compressed data (it will decompress if the client does not accept gzip encoded data). Unlike SendResponse(), content is not copied and its contents must remain valid for an unspecified lifetime.

Parameters
codeHTTP response code (e.g. 200)
codeTextHTTP response code text (e.g. "OK")
contentTypeMIME content type (e.g. "text/plain")
contentResponse message content
gzippedTrue if content is gzip compressed
extraHeaderExtra HTTP headers to send, including final "\r\n"

Member Data Documentation

◆ m_acceptGzip

bool wpi::HttpServerConnection::m_acceptGzip = false
protected

If gzip is an acceptable encoding for responses.

◆ m_dataConn

sig::ScopedConnection wpi::HttpServerConnection::m_dataConn
protected

The header reader connection.

◆ m_endConn

sig::ScopedConnection wpi::HttpServerConnection::m_endConn
protected

The end stream connection.

◆ m_keepAlive

bool wpi::HttpServerConnection::m_keepAlive = false
protected

Whether the connection should be kept alive.

◆ m_messageCompleteConn

sig::Connection wpi::HttpServerConnection::m_messageCompleteConn
protected

The message complete connection.

◆ m_request

HttpParser wpi::HttpServerConnection::m_request {HttpParser::kRequest}
protected

The HTTP request.

◆ m_stream

uv::Stream& wpi::HttpServerConnection::m_stream
protected

The underlying stream for the connection.


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