|
Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
|
The HttpClient library is the C side implementation for the Lua httpc library.
The library is also used internally by the NetIo network file system. See the Lua HTTP client for an introduction to this library.
This documentation is for the C++ API defined in the headers. See the introduction to object oriented code in C for an introduction to the C API's.
The HTTP client library can also be compiled into a standalone library. The HTTP client source code requires a few runtime files from the Barracuda Server such as the SoDisp, BufPrint, DynBuffer, HttpConnection, HttpServCon, and HttpSocket.
The HTTP client library can also be used standalone together with the SharkSSL SSL/TLS stack when secure HTTP (HTTPS) is required.
Classes | |
| struct | HttpClientKeyVal |
| A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters. More... | |
| struct | HttpClientHeader |
| The response HTTP headers returned by HttpClient::getHeaders. More... | |
| struct | HttpClient |
| Blocking HTTP/1.1 client with optional SharkSSL HTTPS support. More... | |
Macros | |
| #define | HttpClientHeader_key(c, o) ((c)->db.super.buf + (o)->key) |
| C accessor: c is the required owning client and o its header entry. More... | |
| #define | HttpClientHeader_val(c, o) ((c)->db.super.buf + (o)->val) |
| C accessor: c is the required owning client and o its header entry. More... | |
| #define | HttpClient_setSSL(o, ssl) (o)->sharkSslClient=ssl |
| Select the TLS context for future HTTPS connections. More... | |
| #define | HttpClient_setReadTmo(o, timeout) (o)->readTmo=timeout |
| Set the timeout applied to individual blocking reads. More... | |
| #define | HttpClient_getError(o) (o)->lastError |
| Query the stored client error without performing I/O. More... | |
| #define | HttpClient_getSoDispCon(o) ((SoDispCon*)(o)) |
| Access the embedded transport object. More... | |
| #define | HttpClient_setAcceptTrusted(o, t) (o)->acceptTrusted=t |
| C form of HttpClient::setAcceptTrusted. More... | |
Typedefs | |
| typedef struct HttpClientKeyVal | HttpClientKeyVal |
| A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters. More... | |
| typedef HttpClient | HttpClient |
| Blocking HTTP/1.1 client with optional SharkSSL HTTPS support. More... | |
Functions | |
| void | HttpClient_constructor (HttpClient *o, SoDisp *disp, U8 mode) |
| Construct an idle client; no connection is opened. More... | |
| SharkSslCon * | HttpClient_getSharkSslCon (HttpClient *o) |
| Access the current SharkSSL connection. More... | |
| void | HttpClient_destructor (HttpClient *o) |
| Close the connection and release client-owned storage. More... | |
| int | HttpClient_isURL (const char *url) |
| Classify a URL scheme; this does not validate the complete URL. More... | |
| int | HttpClient_request (HttpClient *o, HttpMethod methodType, const char *url, const char *userPass, const HttpClientKeyVal *params, const HttpClientKeyVal *headers, BaFileSize size) |
| C form of HttpClient::request, with the same return values and lifecycle. More... | |
| int | HttpClient_sendData (HttpClient *o, const void *data, int len) |
| Send part or all of the request body after request(). More... | |
| int | HttpClient_getBufSize (HttpClient *o) |
| Query bytes already buffered beyond the response headers. More... | |
| int | HttpClient_readData (HttpClient *o, void *buf, int bufSize) |
| Read response body bytes, removing chunk framing when applicable. More... | |
| const char * | HttpClient_getHeaderValue (HttpClient *o, const char *name) |
| Look up the first response header with this name, ignoring case. More... | |
| HttpClientHeader * | HttpClient_getHeaders (HttpClient *o, int *hlen) |
| Obtain parsed response headers; may finish an upload and block for input. More... | |
| void | HttpClient_close (HttpClient *o) |
| Shut down the connection and discard the current response state. More... | |
| int | HttpClient_getStatus (HttpClient *o) |
| Obtain the HTTP status, reading response headers if no status is cached. More... | |
| SharkSslConTrust | HttpClient_trusted (HttpClient *o) |
| Query the current connection's certificate trust result. More... | |
| HttpClient::HttpClient (SoDisp *disp, U8 mode=HttpClient_Persistent) | |
| Construct an idle client; no connection is opened. More... | |
| HttpClient::~HttpClient () | |
| Close the connection and release client-owned storage. More... | |
| void | HttpClient::setSSL (SharkSsl *ssl) |
| Select the TLS context for future HTTPS connections. More... | |
| void | HttpClient::setReadTmo (BaTime timeout) |
| Set the timeout applied to individual blocking reads. More... | |
| static int | HttpClient::isURL (const char *url) |
| Classify a URL scheme; this does not validate the complete URL. More... | |
| SharkSslConTrust | HttpClient::trusted (void) |
| Query the current connection's certificate trust result. More... | |
| void | HttpClient::setAcceptTrusted (bool acceptTrusted) |
| Choose whether new connections must pass the TLS trust check. More... | |
| SharkSslCon * | HttpClient::getSharkSslCon () |
| Access the current SharkSSL connection. More... | |
| int | HttpClient::request (HttpMethod methodType, const char *url, const char *userPass=0, const HttpClientKeyVal *query=0, const HttpClientKeyVal *headers=0, BaFileSize size=0) |
| Send request headers, opening or reusing a connection. More... | |
| int | HttpClient::sendData (const void *data, int len) |
| Send part or all of the request body after request(). More... | |
| int | HttpClient::getBufSize () |
| Query bytes already buffered beyond the response headers. More... | |
| int | HttpClient::readData (void *buf, int bufSize) |
| Read response body bytes, removing chunk framing when applicable. More... | |
| const char * | HttpClient::getHeaderValue (const char *name) |
| Look up the first response header with this name, ignoring case. More... | |
| HttpClientHeader * | HttpClient::getHeaders (int *hlen) |
| Obtain parsed response headers; may finish an upload and block for input. More... | |
| void | HttpClient::close () |
| Shut down the connection and discard the current response state. More... | |
| int | HttpClient::getStatus () |
| Obtain the HTTP status, reading response headers if no status is cached. More... | |
| int | HttpClient::getError () |
| Query the stored client error without performing I/O. More... | |
| SoDispCon * | HttpClient::getSoDispCon () |
| Access the embedded transport object. More... | |
| const char * | HttpClientHeader::getKey (HttpClient *c) |
| const char * | HttpClientHeader::getVal (HttpClient *c) |
| #define HttpClient_getError | ( | o | ) | (o)->lastError |
Query the stored client error without performing I/O.
| o | Required initialized client. |
| #define HttpClient_getSoDispCon | ( | o | ) | ((SoDispCon*)(o)) |
Access the embedded transport object.
| o | Required initialized client. |
| #define HttpClient_setAcceptTrusted | ( | o, | |
| t | |||
| ) | (o)->acceptTrusted=t |
C form of HttpClient::setAcceptTrusted.
| o | Required initialized client. |
| t | TRUE to require trusted new connections, FALSE to disable the check. |
| #define HttpClient_setReadTmo | ( | o, | |
| timeout | |||
| ) | (o)->readTmo=timeout |
Set the timeout applied to individual blocking reads.
| timeout | Milliseconds, default 20000. Use values representable by SoDispCon::setReadTmo (50 through 3276750, in 50 ms steps for exact timing). Values are rounded down to that transport's 50 ms resolution. |
| o | Required initialized client. |
| #define HttpClient_setSSL | ( | o, | |
| ssl | |||
| ) | (o)->sharkSslClient=ssl |
Select the TLS context for future HTTPS connections.
| ssl | Borrowed, configured SharkSSL client context, or NULL to disable new HTTPS connections. Keep it alive until all connections using it close. Close an existing connection before replacing its TLS configuration. This setter does not configure certificates or enable trust enforcement; see setAcceptTrusted(). |
| o | Required initialized client. |
| #define HttpClientHeader_key | ( | c, | |
| o | |||
| ) | ((c)->db.super.buf + (o)->key) |
C accessor: c is the required owning client and o its header entry.
| #define HttpClientHeader_val | ( | c, | |
| o | |||
| ) | ((c)->db.super.buf + (o)->val) |
C accessor: c is the required owning client and o its header entry.
| typedef HttpClient HttpClient |
Blocking HTTP/1.1 client with optional SharkSSL HTTPS support.
Send a request, send its body if applicable, obtain response headers/status, and read the response body. Serialize access to each instance. When the dispatcher has a mutex, hold it when entering the API; blocking transport operations release and reacquire it internally.
This example streams a GET response without relying on a prior HEAD request or allocating storage for the complete response. The destination writer is owned by the caller and is not flushed here. An error can leave partial output.
| typedef struct HttpClientKeyVal HttpClientKeyVal |
A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters.
HttpClientKeyVal can be statically declared at compile time or be dynamically created during runtime. A dynamically created HttpClientKeyVal can be released as soon as HttpClient::request returns.
Example code:
| void HttpClient::close | ( | ) |
Shut down the connection and discard the current response state.
The object can be reused by request(). This does not free the dispatcher or TLS context, report a close error, or reset cached status/error values.
| int HttpClient::getBufSize | ( | ) |
Query bytes already buffered beyond the response headers.
| int HttpClient::getError | ( | ) |
Query the stored client error without performing I/O.
| HttpClientHeader * HttpClient::getHeaders | ( | int * | hlen | ) |
Obtain parsed response headers; may finish an upload and block for input.
| hlen | Required output pointer receiving the number of array entries; receives zero on failure. |
| const char * HttpClient::getHeaderValue | ( | const char * | name | ) |
Look up the first response header with this name, ignoring case.
May finish the outgoing request and block while reading response headers.
| name | Required NUL-terminated header name. |
| const char * HttpClientHeader::getKey | ( | HttpClient * | c | ) |
| c | Required client that supplied this header. |
| SharkSslCon * HttpClient::getSharkSslCon | ( | ) |
Access the current SharkSSL connection.
| SoDispCon * HttpClient::getSoDispCon | ( | ) |
Access the embedded transport object.
| int HttpClient::getStatus | ( | ) |
Obtain the HTTP status, reading response headers if no status is cached.
| const char * HttpClientHeader::getVal | ( | HttpClient * | c | ) |
| c | Required client that supplied this header. |
Construct an idle client; no connection is opened.
| disp | Required borrowed dispatcher, which must outlive this client. |
| mode | Bitwise combination of HttpClient_Persistent (the C++ default), HttpClient_SocksProxy, and HttpClient_IPv6. Zero disables these options. Persistent mode permits connection reuse; IPv6 selects IPv6 resolution where supported. SOCKS selects SOCKS instead of an HTTP CONNECT proxy. Configure proxy, proxyPortNo, proxyUserPass, and intfName before connecting. Without a dispatcher mutex, use the client from a single thread. The initial read timeout is 20000 milliseconds. Trust enforcement is off. |
| void HttpClient_close | ( | HttpClient * | o | ) |
Shut down the connection and discard the current response state.
The object can be reused by request(). This does not free the dispatcher or TLS context, report a close error, or reset cached status/error values.
| o | Required initialized client. |
| void HttpClient_constructor | ( | HttpClient * | o, |
| SoDisp * | disp, | ||
| U8 | mode | ||
| ) |
Construct an idle client; no connection is opened.
| disp | Required borrowed dispatcher, which must outlive this client. |
| mode | Bitwise combination of HttpClient_Persistent (the C++ default), HttpClient_SocksProxy, and HttpClient_IPv6. Zero disables these options. Persistent mode permits connection reuse; IPv6 selects IPv6 resolution where supported. SOCKS selects SOCKS instead of an HTTP CONNECT proxy. Configure proxy, proxyPortNo, proxyUserPass, and intfName before connecting. Without a dispatcher mutex, use the client from a single thread. The initial read timeout is 20000 milliseconds. Trust enforcement is off. |
| o | Required initialized client storage. |
| void HttpClient_destructor | ( | HttpClient * | o | ) |
Close the connection and release client-owned storage.
Borrowed dispatcher, TLS context, and configuration strings are not destroyed. Stop all users first.
| o | Required initialized client. |
| int HttpClient_getBufSize | ( | HttpClient * | o | ) |
Query bytes already buffered beyond the response headers.
| o | Required initialized client. |
| HttpClientHeader * HttpClient_getHeaders | ( | HttpClient * | o, |
| int * | hlen | ||
| ) |
Obtain parsed response headers; may finish an upload and block for input.
| hlen | Required output pointer receiving the number of array entries; receives zero on failure. |
| o | Required initialized client. |
| const char * HttpClient_getHeaderValue | ( | HttpClient * | o, |
| const char * | name | ||
| ) |
Look up the first response header with this name, ignoring case.
May finish the outgoing request and block while reading response headers.
| name | Required NUL-terminated header name. |
| o | Required initialized client. |
| SharkSslCon * HttpClient_getSharkSslCon | ( | HttpClient * | o | ) |
Access the current SharkSSL connection.
| o | Required initialized client. |
| int HttpClient_getStatus | ( | HttpClient * | o | ) |
Obtain the HTTP status, reading response headers if no status is cached.
| o | Required initialized client. |
| int HttpClient_isURL | ( | const char * | url | ) |
| int HttpClient_readData | ( | HttpClient * | o, |
| void * | buf, | ||
| int | bufSize | ||
| ) |
Read response body bytes, removing chunk framing when applicable.
Completes the outgoing request and reads response headers if needed.
| buf | Writable buffer; returned data is binary and not NUL-terminated. NULL only processes the response headers and returns zero without consuming the body, so that use cannot be interpreted as end-of-body. |
| bufSize | Positive buffer capacity in bytes when buf is non-NULL. |
| o | Required initialized client. |
| int HttpClient_request | ( | HttpClient * | o, |
| HttpMethod | methodType, | ||
| const char * | url, | ||
| const char * | userPass, | ||
| const HttpClientKeyVal * | params, | ||
| const HttpClientKeyVal * | headers, | ||
| BaFileSize | size | ||
| ) |
C form of HttpClient::request, with the same return values and lifecycle.
| o | Required initialized client. |
| methodType | Supported HttpMethod value; see HttpClient::request. |
| url | Required absolute URL string. |
| userPass | Optional "user:password" string, or NULL. |
| params | Optional decoded query array terminated by a NULL key. |
| headers | Optional custom header array terminated by a NULL key. |
| size | Request-body byte count; zero selects chunked output for body methods. |
| int HttpClient_sendData | ( | HttpClient * | o, |
| const void * | data, | ||
| int | len | ||
| ) |
Send part or all of the request body after request().
| data | Required readable buffer when len is positive; borrowed for this call only. Binary bytes are sent unchanged. |
| len | Nonnegative byte count. Zero is a no-op, not an end marker. For fixed-length requests, do not exceed the remaining declared length. |
| o | Required initialized client. |
| SharkSslConTrust HttpClient_trusted | ( | HttpClient * | o | ) |
Query the current connection's certificate trust result.
| o | Required initialized client. |
|
static |
| int HttpClient::readData | ( | void * | buf, |
| int | bufSize | ||
| ) |
Read response body bytes, removing chunk framing when applicable.
Completes the outgoing request and reads response headers if needed.
| buf | Writable buffer; returned data is binary and not NUL-terminated. NULL only processes the response headers and returns zero without consuming the body, so that use cannot be interpreted as end-of-body. |
| bufSize | Positive buffer capacity in bytes when buf is non-NULL. |
| int HttpClient::request | ( | HttpMethod | methodType, |
| const char * | url, | ||
| const char * | userPass = 0, |
||
| const HttpClientKeyVal * | query = 0, |
||
| const HttpClientKeyVal * | headers = 0, |
||
| BaFileSize | size = 0 |
||
| ) |
Send request headers, opening or reusing a connection.
| methodType | HttpMethod_Delete, HttpMethod_Get, HttpMethod_Head, HttpMethod_Patch, HttpMethod_Post, or HttpMethod_Put. |
| url | Required NUL-terminated absolute URL, including host and path. HTTPS requires setSSL(). The ws/wss schemes are also recognized, but this call alone does not complete a WebSocket upgrade. |
| userPass | Optional NUL-terminated "user:password" for Basic authentication; NULL omits it. |
| query | Optional array of decoded query names and values. Both are URL-encoded. Terminate the array with a NULL key; each other value must be a non-NULL NUL-terminated string. |
| headers | Optional custom header array with the same terminator. Supply valid HTTP names/values without embedded CR or LF. |
| size | Body length in bytes for POST, PUT, PATCH, or DELETE. Zero selects chunked encoding. GET and HEAD ignore this argument. All argument strings/arrays need remain valid only during this call. |
| int HttpClient::sendData | ( | const void * | data, |
| int | len | ||
| ) |
Send part or all of the request body after request().
| data | Required readable buffer when len is positive; borrowed for this call only. Binary bytes are sent unchanged. |
| len | Nonnegative byte count. Zero is a no-op, not an end marker. For fixed-length requests, do not exceed the remaining declared length. |
| void HttpClient::setAcceptTrusted | ( | bool | acceptTrusted | ) |
Choose whether new connections must pass the TLS trust check.
| acceptTrusted | True requires SharkSslConTrust_CertCnDate before sending the HTTP request; failure returns E_NOT_TRUSTED. False (default) disables this requirement. Plain HTTP is rejected when enabled. Set this before connecting. Changing it does not recheck a reused connection. |
| void HttpClient::setReadTmo | ( | BaTime | timeout | ) |
Set the timeout applied to individual blocking reads.
| timeout | Milliseconds, default 20000. Use values representable by SoDispCon::setReadTmo (50 through 3276750, in 50 ms steps for exact timing). Values are rounded down to that transport's 50 ms resolution. |
| void HttpClient::setSSL | ( | SharkSsl * | ssl | ) |
Select the TLS context for future HTTPS connections.
| ssl | Borrowed, configured SharkSSL client context, or NULL to disable new HTTPS connections. Keep it alive until all connections using it close. Close an existing connection before replacing its TLS configuration. This setter does not configure certificates or enable trust enforcement; see setAcceptTrusted(). |
| SharkSslConTrust HttpClient::trusted | ( | void | ) |
Query the current connection's certificate trust result.
| HttpClient::~HttpClient | ( | ) |
Close the connection and release client-owned storage.
Borrowed dispatcher, TLS context, and configuration strings are not destroyed. Stop all users first.