Barracuda Application Server C/C++ Reference
Native APIs, integration guides, and platform interfaces
HttpClient.h
Go to the documentation of this file.
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: HttpClient.h 6056 2026-09-20 05:09:33Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2009-2026
17 *
18 * This software is copyrighted by and is the sole property of Real
19 * Time Logic LLC. All rights, title, ownership, or other interests in
20 * the software remain the property of Real Time Logic LLC. This
21 * software may only be used in accordance with the terms and
22 * conditions stipulated in the corresponding license agreement under
23 * which the software has been supplied. Any unauthorized use,
24 * duplication, transmission, distribution, or disclosure of this
25 * software is expressly forbidden.
26 *
27 * This Copyright notice may not be removed or modified without prior
28 * written consent of Real Time Logic LLC.
29 *
30 * Real Time Logic LLC. reserves the right to modify this software
31 * without notice.
32 *
33 * http://www.realtimelogic.com
34 ****************************************************************************
35 */
38#ifndef __HttpClient_h
39#define __HttpClient_h
40
41#include <DynBuffer.h>
42#ifndef NO_SHARKSSL
43#include <SharkSslEx.h>
44#endif
45
46struct HttpClient;
47
48
89typedef struct HttpClientKeyVal
90{
92 const char* key;
94 const char* val;
96
97
98
101typedef struct
102{
103#ifdef __cplusplus
107 const char* getKey(HttpClient* c);
111 const char* getVal(HttpClient* c);
112#endif
113 U16 key;
114 U16 val;
116
119#define HttpClientHeader_key(c,o) ((c)->db.super.buf + (o)->key)
122#define HttpClientHeader_val(c,o) ((c)->db.super.buf + (o)->val)
123
124
125#define HTTP_CLIENT_MAX_HEADERS 64
126
127#define HttpClient_SocksProxy 1
128#define HttpClient_ProxyConnect 2
129#define HttpClient_Persistent 4
130#define HttpClient_IPv6 8
131/* Reserved for future use */
132#define HttpClient_NoHostHeader 16
133#define HttpClient_NotUsed2 32
134/* May be set by code using the HttpClient lib */
135#define HttpClient_UserDef1 64
136#define HttpClient_UserDef2 128
137
138
184typedef struct HttpClient
185#ifdef __cplusplus
186: public SoDispCon
187{
198 HttpClient(SoDisp* disp, U8 mode=HttpClient_Persistent);
199
203 ~HttpClient();
204
212 void setSSL(SharkSsl* ssl);
213
221 void setReadTmo(BaTime timeout);
222
228 static int isURL(const char* url);
229
236 SharkSslConTrust trusted(void);
237
244 void setAcceptTrusted(bool acceptTrusted);
245
272 int request(HttpMethod methodType,
273 const char* url,
274 const char* userPass=0,
275 const HttpClientKeyVal* query=0,
276 const HttpClientKeyVal* headers=0,
277 BaFileSize size=0);
278
289 int sendData(const void* data, int len);
290
296 int getBufSize();
297
316 int readData(void* buf, int bufSize);
317
325 const char* getHeaderValue(const char* name);
326
335 HttpClientHeader* getHeaders(int* hlen);
336
341 void close();
342
349 int getStatus();
350
356 int getError();
357
362 SharkSslCon* getSharkSslCon();
363
369#else
370{
371 SoDispCon super;
372#endif
373 DynBuffer db;
374 HttpClientHeader headers[HTTP_CLIENT_MAX_HEADERS];
375
376 struct SharkSsl* sharkSslClient; /* optional SSL client */
377
378 /* Variable proxyPortNo and the following 3 variables must be
379 * set and managed by the user of this class.
380 */
382 const char* proxy; /* Proxy name/IP addr, if any */
384 const char* proxyUserPass; /* Format: "user:password" */
386 const char* intfName; /* If 0: bind to any intf, or bind to intfName */
387
388 char* data; /* Pointer to start of payload data */
389 char* host; /* Server host name */
390 BaFileSize size; /* Send or receive data size */
391 BaTime readTmo; /* Default read timeout is 20 seconds */
392 int chunkSize;
393 int lastError;
394 int portNo; /* host port number */
396 U16 proxyPortNo; /* host port number */
397 U16 headerLen;
398 S16 httpStatus;
399 BaBool chunkEncoding;
400 BaBool respManaged; /* Func HttpClient_manageEndOfRequest */
401 BaBool closeCon;
402 BaBool acceptTrusted; /* Accept only trusted connections */
403 /*
404 HttpClient_SocksProxy
405 HttpClient_Persistent
406 HttpClient_IPv6
407 */
408 U8 mode;
409 U8 methodType;
411
412#ifdef __cplusplus
413extern "C" {
414#endif
415
423#define HttpClient_setSSL(o, ssl) (o)->sharkSslClient=ssl
427#define HttpClient_setReadTmo(o, timeout) (o)->readTmo=timeout
428#ifndef NO_SHARKSSL
433#endif
434
441int HttpClient_isURL(const char* url);
453 HttpMethod methodType,
454 const char* url,
455 const char* userPass,
456 const HttpClientKeyVal* params,
457 const HttpClientKeyVal* headers,
458 BaFileSize size);
459
463int HttpClient_sendData(HttpClient* o, const void* data, int len);
468int HttpClient_readDataBuffered(HttpClient* o, void* buf, int bufSize);
472int HttpClient_readData(HttpClient* o, void* buf, int bufSize);
476const char* HttpClient_getHeaderValue(HttpClient* o, const char* name);
492#define HttpClient_getError(o) (o)->lastError
496#define HttpClient_getSoDispCon(o) ((SoDispCon*)(o))
497#ifndef NO_SHARKSSL
501SharkSslConTrust HttpClient_trusted(HttpClient* o);
502#endif
507#define HttpClient_setAcceptTrusted(o, t) (o)->acceptTrusted=t
508
509#ifdef __cplusplus
510}
511
512inline HttpClient::HttpClient(SoDisp* disp, U8 mode) {
513 HttpClient_constructor(this,disp, mode);
514}
515
518}
519
520inline void HttpClient::setSSL(SharkSsl* ssl) {
521 HttpClient_setSSL(this,ssl);
522}
523
524inline void HttpClient::setReadTmo(BaTime timeout) {
525 HttpClient_setReadTmo(this,timeout);
526}
527
528inline int HttpClient::isURL(const char* url) {
529 return HttpClient_isURL(url);
530}
531
532
533inline SharkSslConTrust HttpClient::trusted(void){
534 return HttpClient_trusted(this);
535}
536
537inline void HttpClient::setAcceptTrusted(bool t) {
538 HttpClient_setAcceptTrusted(this, t?TRUE:FALSE);
539}
540
541
542inline SharkSslCon* HttpClient::getSharkSslCon() {
543 return HttpClient_getSharkSslCon(this);
544}
545
546
547inline int HttpClient::request(HttpMethod methodType,
548 const char* url,
549 const char* userPass,
550 const HttpClientKeyVal* params,
551 const HttpClientKeyVal* headers,
552 BaFileSize size)
553{
554 return HttpClient_request(this,methodType,url,userPass,params,headers,size);
555}
556
557inline int HttpClient::sendData(const void* data, int len) {
558 return HttpClient_sendData(this, data, len);
559}
560
562 return HttpClient_getBufSize(this);
563}
564
565inline int HttpClient::readData(void* buf, int bufSize) {
566 return HttpClient_readData(this, buf, bufSize);
567}
568
569inline const char* HttpClient::getHeaderValue(const char* name) {
570 return HttpClient_getHeaderValue(this, name);
571}
572
574 return HttpClient_getHeaders(this, hlen);
575}
576
577inline void HttpClient::close() {
578 HttpClient_close(this);
579}
580
582 return HttpClient_getStatus(this);
583}
584
585
587 return HttpClient_getError(this);
588}
589
591 return HttpClient_getSoDispCon(this);
592}
593
594inline const char* HttpClientHeader::getKey(HttpClient* c) {
595 return c->db.buf + key;
596}
597inline const char* HttpClientHeader::getVal(HttpClient* c) {
598 return c->db.buf + val;
599}
600#endif
601 /* End group HttpClient */
603
604
605#endif
static int isURL(const char *url)
Classify a URL scheme; this does not validate the complete URL.
Definition: HttpClient.h:528
int readData(void *buf, int bufSize)
Read response body bytes, removing chunk framing when applicable.
Definition: HttpClient.h:565
void setSSL(SharkSsl *ssl)
Select the TLS context for future HTTPS connections.
Definition: HttpClient.h:520
SoDispCon * getSoDispCon()
Access the embedded transport object.
Definition: HttpClient.h:590
#define HttpClient_setReadTmo(o, timeout)
Set the timeout applied to individual blocking reads.
Definition: HttpClient.h:427
~HttpClient()
Close the connection and release client-owned storage.
Definition: HttpClient.h:516
int HttpClient_sendData(HttpClient *o, const void *data, int len)
Send part or all of the request body after request().
void close()
Shut down the connection and discard the current response state.
Definition: HttpClient.h:577
HttpClientHeader * getHeaders(int *hlen)
Obtain parsed response headers; may finish an upload and block for input.
Definition: HttpClient.h:573
int sendData(const void *data, int len)
Send part or all of the request body after request().
Definition: HttpClient.h:557
#define HttpClient_getSoDispCon(o)
Access the embedded transport object.
Definition: HttpClient.h:496
int getBufSize()
Query bytes already buffered beyond the response headers.
Definition: HttpClient.h:561
#define HttpClient_setSSL(o, ssl)
Select the TLS context for future HTTPS connections.
Definition: HttpClient.h:423
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.
int 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.
Definition: HttpClient.h:547
const char * getHeaderValue(const char *name)
Look up the first response header with this name, ignoring case.
Definition: HttpClient.h:569
void HttpClient_destructor(HttpClient *o)
Close the connection and release client-owned storage.
int HttpClient_getBufSize(HttpClient *o)
Query bytes already buffered beyond the response headers.
SharkSslCon * getSharkSslCon()
Access the current SharkSSL connection.
Definition: HttpClient.h:542
int getError()
Query the stored client error without performing I/O.
Definition: HttpClient.h:586
const char * getVal(HttpClient *c)
Definition: HttpClient.h:597
HttpClientHeader * HttpClient_getHeaders(HttpClient *o, int *hlen)
Obtain parsed response headers; may finish an upload and block for input.
void HttpClient_close(HttpClient *o)
Shut down the connection and discard the current response state.
int HttpClient_getStatus(HttpClient *o)
Obtain the HTTP status, reading response headers if no status is cached.
SharkSslCon * HttpClient_getSharkSslCon(HttpClient *o)
Access the current SharkSSL connection.
int getStatus()
Obtain the HTTP status, reading response headers if no status is cached.
Definition: HttpClient.h:581
SharkSslConTrust HttpClient_trusted(HttpClient *o)
Query the current connection's certificate trust result.
#define HttpClient_getError(o)
Query the stored client error without performing I/O.
Definition: HttpClient.h:492
int HttpClient_isURL(const char *url)
Classify a URL scheme; this does not validate the complete URL.
const char * HttpClient_getHeaderValue(HttpClient *o, const char *name)
Look up the first response header with this name, ignoring case.
void setReadTmo(BaTime timeout)
Set the timeout applied to individual blocking reads.
Definition: HttpClient.h:524
const char * getKey(HttpClient *c)
Definition: HttpClient.h:594
int HttpClient_readData(HttpClient *o, void *buf, int bufSize)
Read response body bytes, removing chunk framing when applicable.
SharkSslConTrust trusted(void)
Query the current connection's certificate trust result.
Definition: HttpClient.h:533
void HttpClient_constructor(HttpClient *o, SoDisp *disp, U8 mode)
Construct an idle client; no connection is opened.
struct HttpClientKeyVal HttpClientKeyVal
A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters.
#define HttpClient_setAcceptTrusted(o, t)
C form of HttpClient::setAcceptTrusted.
Definition: HttpClient.h:507
void setAcceptTrusted(bool acceptTrusted)
Choose whether new connections must pass the TLS trust check.
Definition: HttpClient.h:537
HttpClient(SoDisp *disp, U8 mode=HttpClient_Persistent)
Construct an idle client; no connection is opened.
Definition: HttpClient.h:512
HttpMethod
HTTP method types.
Definition: HttpServer.h:910
uint16_t U16
Unsigned 16-bit integer.
Definition: GenPrimT.h:91
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:00 UTC,...
Definition: GenPrimT.h:103
int16_t S16
Signed 16-bit integer.
Definition: GenPrimT.h:83
uint8_t U8
Unsigned 8-bit integer.
Definition: GenPrimT.h:89
U8 BaBool
Boolean stored in an unsigned byte; FALSE is zero and TRUE is one.
Definition: GenPrimT.h:118
U32 BaFileSize
Unsigned file size or position in bytes; 32 bits without BA_FILESIZE64.
Definition: GenPrimT.h:136
A dynamic buffer.
Definition: DynBuffer.h:68
The response HTTP headers returned by HttpClient::getHeaders.
Definition: HttpClient.h:102
A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters.
Definition: HttpClient.h:90
const char * val
Required NUL-terminated value for a non-NULL key.
Definition: HttpClient.h:94
const char * key
NUL-terminated name; NULL terminates an input array.
Definition: HttpClient.h:92
Blocking HTTP/1.1 client with optional SharkSSL HTTPS support.
Definition: HttpClient.h:187
U16 proxyPortNo
Proxy TCP port, 1 through 65535 when proxy is configured.
Definition: HttpClient.h:396
const char * intfName
Borrowed local interface name/address, or NULL for the default binding.
Definition: HttpClient.h:386
const char * proxyUserPass
Borrowed proxy credentials "user:password", or NULL.
Definition: HttpClient.h:384
const char * proxy
Borrowed proxy host string, or NULL for a direct connection.
Definition: HttpClient.h:382
Contains information about the physical socket connection.
Definition: SoDispCon.h:120
The SoDisp dispatches any socket connection that contains data by calling the SoDispCon::execute memb...
Definition: SoDisp.h:91