sptk2 logo
SPTK Home Page
Classes | Public Member Functions | Protected Member Functions | List of all members
sptk::HttpConnect Class Reference

HTTP socket. More...

#include <HttpConnect.h>

Classes

class  Authorization
 
class  BasicAuthorization
 
class  BearerAuthorization
 

Public Member Functions

 HttpConnect (TCPSocket &socket)
 Constructor. More...
 
HttpHeaders & requestHeaders ()
 Returns the HTTP request headers. More...
 
const HttpHeaders & responseHeaders () const
 Returns the HTTP headers. More...
 
int cmd_get (const String &pageName, const HttpParams &parameters, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
 Sends the GET command to the server. More...
 
int cmd_post (const String &pageName, const HttpParams &parameters, const Buffer &content, Buffer &output, const sptk::Strings &possibleContentEncodings, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
 Sends the POST command to the server. More...
 
int cmd_put (const String &pageName, const HttpParams &parameters, const Buffer &content, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
 Sends the PUT command to the server. More...
 
int cmd_delete (const String &pageName, const HttpParams &parameters, Buffer &output, const Authorization *authorization=nullptr, std::chrono::milliseconds timeout=std::chrono::seconds(60))
 Sends the DELETE command to the server. More...
 
String responseHeader (const String &headerName) const
 Get value of response header. More...
 
int statusCode () const
 Get the request execution status code. More...
 
String statusText () const
 Get the request execution status text. More...
 

Protected Member Functions

Strings makeHeaders (const String &httpCommand, const String &pageName, const HttpParams &requestParameters, const Authorization *authorization) const
 
void sendCommand (const String &cmd)
 Sends a single command to HTTP server. More...
 
void sendCommand (const Buffer &cmd)
 Sends a single command to HTTP server. More...
 
int getResponse (Buffer &output, std::chrono::milliseconds timeout)
 Retrieves the server response on the command. More...
 

Detailed Description

HTTP socket.

Implements the GET, POST, PUT and DELETE methods of HTTP protocol. Allows to define the host, port, submit information, and then sends the HTML request to the server.

Constructor & Destructor Documentation

◆ HttpConnect()

sptk::HttpConnect::HttpConnect ( TCPSocket socket)
explicit

Constructor.

External socket has to be active before HTTP operations. In order to use HTTPS protocol, use COpenSSLSocket.

Parameters
socketexternal socket

Member Function Documentation

◆ cmd_delete()

int sptk::HttpConnect::cmd_delete ( const String pageName,
const HttpParams parameters,
Buffer output,
const Authorization authorization = nullptr,
std::chrono::milliseconds  timeout = std::chrono::seconds(60) 
)

Sends the DELETE command to the server.

Retrieves the server response into internal read buffer.

Parameters
pageNamePage URL without the server name.
parametersHTTP request parameters
outputOutput data
authorizationOptional authorization
timeoutRequest timeout
Returns
HTTP result code

◆ cmd_get()

int sptk::HttpConnect::cmd_get ( const String pageName,
const HttpParams parameters,
Buffer output,
const Authorization authorization = nullptr,
std::chrono::milliseconds  timeout = std::chrono::seconds(60) 
)

Sends the GET command to the server.

Retrieves the server response into internal read buffer.

Parameters
pageNamePage URL without the server name.
parametersHTTP request parameters
outputOutput data
authorizationOptional authorization
timeoutResponse timeout
Returns
HTTP result code

◆ cmd_post()

int sptk::HttpConnect::cmd_post ( const String pageName,
const HttpParams parameters,
const Buffer content,
Buffer output,
const sptk::Strings possibleContentEncodings,
const Authorization authorization = nullptr,
std::chrono::milliseconds  timeout = std::chrono::seconds(60) 
)

Sends the POST command to the server.

Retrieves the server response into internal read buffer.

Parameters
pageNamePage URL without the server name.
parametersHTTP request parameters
contentThe data to post to the server
gzipContentIf true then compress buffer and set HTTP header Content-Encoding
outputOutput data
authorizationOptional authorization
timeoutResponse timeout
Returns
HTTP result code

◆ cmd_put()

int sptk::HttpConnect::cmd_put ( const String pageName,
const HttpParams parameters,
const Buffer content,
Buffer output,
const Authorization authorization = nullptr,
std::chrono::milliseconds  timeout = std::chrono::seconds(60) 
)

Sends the PUT command to the server.

Retrieves the server response into internal read buffer.

Parameters
pageNamePage URL without the server name.
parametersHTTP request parameters
contentThe data to post to the server
outputOutput data
authorizationOptional authorization
timeoutOptional response timeout
Returns
HTTP result code

◆ getResponse()

int sptk::HttpConnect::getResponse ( Buffer output,
std::chrono::milliseconds  timeout 
)
protected

Retrieves the server response on the command.

Stops when HTTP server closes the connection. The server response can then be accessed through the htmlData() method.

Parameters
timeoutResponse timeout
Returns
HTTP result code

◆ makeHeaders()

Strings sptk::HttpConnect::makeHeaders ( const String httpCommand,
const String pageName,
const HttpParams requestParameters,
const Authorization authorization 
) const
protected

Create default headers for HTTP request

◆ requestHeaders()

HttpHeaders & sptk::HttpConnect::requestHeaders ( )
inline

Returns the HTTP request headers.

The HTTP headers request should be set before sending a command to the server

Returns
internal http request headers reference

◆ responseHeader()

String sptk::HttpConnect::responseHeader ( const String headerName) const

Get value of response header.

Parameters
headerNameResponse header name
Returns
header value, or empty string if header is not a part of the response

◆ responseHeaders()

const HttpHeaders & sptk::HttpConnect::responseHeaders ( ) const
inline

Returns the HTTP headers.

The HTTP response headers make sense only after sending a command to the server (if that command calls getResponse() method internally).

Returns
internal http headers reference

◆ sendCommand() [1/2]

void sptk::HttpConnect::sendCommand ( const Buffer cmd)
protected

Sends a single command to HTTP server.

CRLF characters are automatically appended to the command.

Parameters
cmdHTTP command

◆ sendCommand() [2/2]

void sptk::HttpConnect::sendCommand ( const String cmd)
protected

Sends a single command to HTTP server.

CRLF characters are automatically appended to the command.

Parameters
cmdHTTP command

◆ statusCode()

int sptk::HttpConnect::statusCode ( ) const

Get the request execution status code.

Returns
request execution status code

◆ statusText()

String sptk::HttpConnect::statusText ( ) const

Get the request execution status text.

Returns
request execution status text

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

Fri Oct 14 2022 09:58:42: SPTK 5.4.1