OpenTTD Source 20241224-master-gee860a5c8e
NetworkHTTPRequest Class Reference

Single HTTP request. More...

Public Member Functions

 NetworkHTTPRequest (const std::string &uri, HTTPCallback *callback, const std::string &data)
 Create a new HTTP request.
 
 ~NetworkHTTPRequest ()
 Destructor of the HTTP request.
 
 NetworkHTTPRequest (const std::wstring &uri, HTTPCallback *callback, const std::string &data)
 Create a new HTTP request.
 
void Connect ()
 Start the HTTP request handling.
 
bool Receive ()
 Poll and process the HTTP request/response.
 
void WinHttpCallback (DWORD code, void *info, DWORD length)
 Callback from the WinHTTP library, called when-ever something changes about the HTTP request status.
 

Data Fields

const std::string uri
 URI to connect to.
 
HTTPThreadSafeCallback callback
 Callback to send data back on.
 
const std::string data
 Data to send, if any.
 

Private Attributes

const std::wstring uri
 URI to connect to.
 
HINTERNET connection = nullptr
 Current connection object.
 
HINTERNET request = nullptr
 Current request object.
 
std::atomic< bool > finished = false
 Whether we are finished with the request.
 
int depth = 0
 Current redirect depth we are in.
 

Detailed Description

Single HTTP request.

Definition at line 54 of file http_curl.cpp.

Constructor & Destructor Documentation

◆ NetworkHTTPRequest() [1/2]

NetworkHTTPRequest::NetworkHTTPRequest ( const std::string &  uri,
HTTPCallback callback,
const std::string &  data 
)
inline

Create a new HTTP request.

Parameters
urithe URI to connect to (https://.../..).
callbackthe callback to send data back on.
datathe data we want to send. When non-empty, this will be a POST request, otherwise a GET request.

Definition at line 63 of file http_curl.cpp.

References lock.

◆ ~NetworkHTTPRequest()

NetworkHTTPRequest::~NetworkHTTPRequest ( )
inline

Destructor of the HTTP request.

Makes sure all handlers are closed, and all memory is free'd.

Definition at line 72 of file http_curl.cpp.

References lock.

◆ NetworkHTTPRequest() [2/2]

NetworkHTTPRequest::NetworkHTTPRequest ( const std::wstring &  uri,
HTTPCallback callback,
const std::string &  data 
)

Create a new HTTP request.

Parameters
urithe URI to connect to (https://.../..).
callbackthe callback to send data back on.
datathe data we want to send. When non-empty, this will be a POST request, otherwise a GET request.

Definition at line 65 of file http_winhttp.cpp.

References lock.

Member Function Documentation

◆ Connect()

void NetworkHTTPRequest::Connect ( )

Start the HTTP request handling.

This is done in an async manner, so we can do other things while waiting for the HTTP request to finish. The actual receiving of the data is done in Receive().

Definition at line 213 of file http_winhttp.cpp.

References callback, connection, data, Debug, finished, HTTPThreadSafeCallback::OnFailure(), and uri.

◆ Receive()

bool NetworkHTTPRequest::Receive ( )

Poll and process the HTTP request/response.

Returns
True iff the request is done; no call to Receive() should be done after it returns true.

Definition at line 266 of file http_winhttp.cpp.

References callback, Debug, finished, HTTPThreadSafeCallback::IsQueueEmpty(), and HTTPThreadSafeCallback::OnFailure().

◆ WinHttpCallback()

void NetworkHTTPRequest::WinHttpCallback ( DWORD  code,
void *  info,
DWORD  length 
)

Callback from the WinHTTP library, called when-ever something changes about the HTTP request status.

The callback needs to call some WinHttp functions for certain states, so WinHttp continues to read the request. This also allows us to abort when things go wrong, by simply not calling those functions. Comments with "Next step:" mark where WinHttp needs a call to continue.

Parameters
codeThe code of the event.
infoThe information about the event.
lengthThe length of the information.

Definition at line 100 of file http_winhttp.cpp.

References callback, Debug, depth, finished, HTTPThreadSafeCallback::OnFailure(), HTTPThreadSafeCallback::OnReceiveData(), and request.

Field Documentation

◆ callback

HTTPThreadSafeCallback NetworkHTTPRequest::callback

Callback to send data back on.

Definition at line 79 of file http_curl.cpp.

Referenced by Connect(), Receive(), and WinHttpCallback().

◆ connection

HINTERNET NetworkHTTPRequest::connection = nullptr
private

Current connection object.

Definition at line 34 of file http_winhttp.cpp.

Referenced by Connect().

◆ data

const std::string NetworkHTTPRequest::data

Data to send, if any.

Definition at line 80 of file http_curl.cpp.

Referenced by Connect().

◆ depth

int NetworkHTTPRequest::depth = 0
private

Current redirect depth we are in.

Definition at line 37 of file http_winhttp.cpp.

Referenced by WinHttpCallback().

◆ finished

std::atomic<bool> NetworkHTTPRequest::finished = false
private

Whether we are finished with the request.

Definition at line 36 of file http_winhttp.cpp.

Referenced by Connect(), Receive(), and WinHttpCallback().

◆ request

HINTERNET NetworkHTTPRequest::request = nullptr
private

Current request object.

Definition at line 35 of file http_winhttp.cpp.

Referenced by WinHttpCallback().

◆ uri [1/2]

const std::string NetworkHTTPRequest::uri

URI to connect to.

Definition at line 78 of file http_curl.cpp.

Referenced by Connect().

◆ uri [2/2]

const std::wstring NetworkHTTPRequest::uri
private

URI to connect to.

Definition at line 30 of file http_winhttp.cpp.


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