Barracuda Application Server C/C++ Reference
NO
HttpClient.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * Barracuda Application Server
10 *
11 ****************************************************************************
12 * HEADER
13 *
14 * $Id: HttpClient.h 5971 2026-09-11 10:01:39Z 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 */
36#ifndef __HttpClient_h
37#define __HttpClient_h
38
39#include <DynBuffer.h>
40#ifndef NO_SHARKSSL
41#include <SharkSslEx.h>
42#endif
43
44struct HttpClient;
45
46
87typedef struct HttpClientKeyVal
88{
89 const char* key;
90 const char* val;
92
93
94
97typedef struct
98{
99#ifdef __cplusplus
102 const char* getKey(HttpClient* c);
105 const char* getVal(HttpClient* c);
106#endif
107 U16 key;
108 U16 val;
110
111#define HttpClientHeader_key(c,o) ((c)->db.super.buf + (o)->key)
112#define HttpClientHeader_val(c,o) ((c)->db.super.buf + (o)->val)
113
114
115#define HTTP_CLIENT_MAX_HEADERS 64
116
117#define HttpClient_SocksProxy 1
118#define HttpClient_ProxyConnect 2
119#define HttpClient_Persistent 4
120#define HttpClient_IPv6 8
121/* Reserved for future use */
122#define HttpClient_NoHostHeader 16
123#define HttpClient_NotUsed2 32
124/* May be set by code using the HttpClient lib */
125#define HttpClient_UserDef1 64
126#define HttpClient_UserDef2 128
127
128
187typedef struct HttpClient
188#ifdef __cplusplus
189: public SoDispCon
190{
223 HttpClient(SoDisp* disp, U8 mode=HttpClient_Persistent);
224
226 ~HttpClient();
227
239 void setSSL(SharkSsl* ssl);
240
244 void setReadTmo(BaTime timeout);
245
247 static int isURL(const char* url);
248
254 SharkSslConTrust trusted(void);
255
264 void setAcceptTrusted(bool acceptTrusted);
265
294 int request(HttpMethod methodType,
295 const char* url,
296 const char* userPass=0,
297 const HttpClientKeyVal* query=0,
298 const HttpClientKeyVal* headers=0,
299 BaFileSize size=0);
300
311 int sendData(const void* data, int len);
312
314 int getBufSize();
315
320 int readData(void* buf, int bufSize);
321
326 const char* getHeaderValue(const char* name);
327
339 HttpClientHeader* getHeaders(int* hlen);
340
343 void close();
344
348 int getStatus();
349
354 int getError();
355
358 SharkSslCon* getSharkSslCon();
359
362#else
363{
364 SoDispCon super;
365#endif
366 DynBuffer db;
367 HttpClientHeader headers[HTTP_CLIENT_MAX_HEADERS];
368
369 struct SharkSsl* sharkSslClient; /* optional SSL client */
370
371 /* Variable proxyPortNo and the following 3 variables must be
372 * set and managed by the user of this class.
373 */
374 const char* proxy; /* Proxy name/IP addr, if any */
375 const char* proxyUserPass; /* Format: "user:password" */
376 const char* intfName; /* If 0: bind to any intf, or bind to intfName */
377
378 char* data; /* Pointer to start of payload data */
379 char* host; /* Server host name */
380 BaFileSize size; /* Send or receive data size */
381 BaTime readTmo; /* Default read timeout is 20 seconds */
382 int chunkSize;
383 int lastError;
384 int portNo; /* host port number */
385 U16 proxyPortNo; /* host port number */
386 U16 headerLen;
387 S16 httpStatus;
388 BaBool chunkEncoding;
389 BaBool respManaged; /* Func HttpClient_manageEndOfRequest */
390 BaBool closeCon;
391 BaBool acceptTrusted; /* Accept only trusted connections */
392 /*
393 HttpClient_SocksProxy
394 HttpClient_Persistent
395 HttpClient_IPv6
396 */
397 U8 mode;
398 U8 methodType;
400
401#ifdef __cplusplus
402extern "C" {
403#endif
404
405void HttpClient_constructor(HttpClient* o, SoDisp* disp, U8 mode);
406#define HttpClient_setSSL(o, ssl) (o)->sharkSslClient=ssl
407#define HttpClient_setReadTmo(o, timeout) (o)->readTmo=timeout
408#ifndef NO_SHARKSSL
409SharkSslCon* HttpClient_getSharkSslCon(HttpClient* o);
410#endif
411
412void HttpClient_destructor(HttpClient* o);
413int HttpClient_isURL(const char* url);
414int HttpClient_request(HttpClient* o,
415 HttpMethod methodType,
416 const char* url,
417 const char* userPass,
418 const HttpClientKeyVal* params,
419 const HttpClientKeyVal* headers,
420 BaFileSize size);
421
422int HttpClient_sendData(HttpClient* o, const void* data, int len);
423int HttpClient_getBufSize(HttpClient* o);
424int HttpClient_readDataBuffered(HttpClient* o, void* buf, int bufSize);
425int HttpClient_readData(HttpClient* o, void* buf, int bufSize);
426const char* HttpClient_getHeaderValue(HttpClient* o, const char* name);
427HttpClientHeader* HttpClient_getHeaders(HttpClient* o, int* hlen);
428void HttpClient_close(HttpClient* o);
429int HttpClient_getStatus(HttpClient* o);
430#define HttpClient_getError(o) (o)->lastError
431#define HttpClient_getSoDispCon(o) ((SoDispCon*)(o))
432#ifndef NO_SHARKSSL
433SharkSslConTrust HttpClient_trusted(HttpClient* o);
434#endif
435#define HttpClient_setAcceptTrusted(o, t) (o)->acceptTrusted=t
436
437#ifdef __cplusplus
438}
439
440inline HttpClient::HttpClient(SoDisp* disp, U8 mode) {
441 HttpClient_constructor(this,disp, mode);
442}
443
445 HttpClient_destructor(this);
446}
447
448inline void HttpClient::setSSL(SharkSsl* ssl) {
449 HttpClient_setSSL(this,ssl);
450}
451
452inline void HttpClient::setReadTmo(BaTime timeout) {
453 HttpClient_setReadTmo(this,timeout);
454}
455
456inline int HttpClient::isURL(const char* url) {
457 return HttpClient_isURL(url);
458}
459
460
461inline SharkSslConTrust HttpClient::trusted(void){
462 return HttpClient_trusted(this);
463}
464
465inline void HttpClient::setAcceptTrusted(bool t) {
466 HttpClient_setAcceptTrusted(this, t?TRUE:FALSE);
467}
468
469
470inline SharkSslCon* HttpClient::getSharkSslCon() {
471 return HttpClient_getSharkSslCon(this);
472}
473
474
475inline int HttpClient::request(HttpMethod methodType,
476 const char* url,
477 const char* userPass,
478 const HttpClientKeyVal* params,
479 const HttpClientKeyVal* headers,
480 BaFileSize size)
481{
482 return HttpClient_request(this,methodType,url,userPass,params,headers,size);
483}
484
485inline int HttpClient::sendData(const void* data, int len) {
486 return HttpClient_sendData(this, data, len);
487}
488
490 return HttpClient_getBufSize(this);
491}
492
493inline int HttpClient::readData(void* buf, int bufSize) {
494 return HttpClient_readData(this, buf, bufSize);
495}
496
497inline const char* HttpClient::getHeaderValue(const char* name) {
498 return HttpClient_getHeaderValue(this, name);
499}
500
502 return HttpClient_getHeaders(this, hlen);
503}
504
505inline void HttpClient::close() {
506 HttpClient_close(this);
507}
508
510 return HttpClient_getStatus(this);
511}
512
513
515 return HttpClient_getError(this);
516}
517
519 return HttpClient_getSoDispCon(this);
520}
521
522inline const char* HttpClientHeader::getKey(HttpClient* c) {
523 return c->db.buf + key;
524}
525inline const char* HttpClientHeader::getVal(HttpClient* c) {
526 return c->db.buf + val;
527}
528#endif
529 /* End group HttpClient */
531
532
533#endif
static int isURL(const char *url)
Returns true if the URL is valid.
Definition: HttpClient.h:456
int readData(void *buf, int bufSize)
Read HTTP response data.
Definition: HttpClient.h:493
void setSSL(SharkSsl *ssl)
Set the SharkSSL client object to enable https: URL's.
Definition: HttpClient.h:448
SoDispCon * getSoDispCon()
Returns the underlying dispatcher connection object.
Definition: HttpClient.h:518
~HttpClient()
Terminate the HttpClient.
Definition: HttpClient.h:444
void close()
Close a persistent HTTP 1.1 connection.
Definition: HttpClient.h:505
HttpClientHeader * getHeaders(int *hlen)
Returns all HTTP response headers.
Definition: HttpClient.h:501
int sendData(const void *data, int len)
Send data to the server.
Definition: HttpClient.h:485
int getBufSize()
Returns the size of the internal response buffer.
Definition: HttpClient.h:489
int request(HttpMethod methodType, const char *url, const char *userPass=0, const HttpClientKeyVal *query=0, const HttpClientKeyVal *headers=0, BaFileSize size=0)
Send an HTTP request.
Definition: HttpClient.h:475
const char * getHeaderValue(const char *name)
Returns the value for the header 'name' or NULL if the header is not in the response.
Definition: HttpClient.h:497
SharkSslCon * getSharkSslCon()
Wrapper for SoDispCon::getSharkSslCon.
Definition: HttpClient.h:470
int getError()
Returns the last socket error code if any.
Definition: HttpClient.h:514
const char * getVal(HttpClient *c)
Returns the value.
Definition: HttpClient.h:525
int getStatus()
Returns the server's HTTP response code or a negative value on socket errors.
Definition: HttpClient.h:509
void setReadTmo(BaTime timeout)
Set the read timeout (in milliseconds).
Definition: HttpClient.h:452
const char * getKey(HttpClient *c)
Returns the key.
Definition: HttpClient.h:522
SharkSslConTrust trusted(void)
Returns the peer's "trust" status.
Definition: HttpClient.h:461
struct HttpClientKeyVal HttpClientKeyVal
A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters.
void setAcceptTrusted(bool acceptTrusted)
Force method HttpClient::request to accept only trusted connections when connecting to a server.
Definition: HttpClient.h:465
HttpClient(SoDisp *disp, U8 mode=HttpClient_Persistent)
Create a HttpClient instance.
Definition: HttpClient.h:440
HttpMethod
HTTP method types.
Definition: HttpServer.h:788
S64 BaTime
An arithmetic type representing calendar time with epoch of 1970-01-01 00:00:10 GMT – i....
Definition: GenPrimT.h:93
A dynamic buffer.
Definition: DynBuffer.h:60
The response HTTP headers returned by HttpClient::getHeaders.
Definition: HttpClient.h:98
A container for key/value pairs used when setting custom HTTP headers or URL encoded HTTP parameters.
Definition: HttpClient.h:88
The HTTP(S) "C" client library implementation conforms to the HTTP/1.1 standard, RFC 2616.
Definition: HttpClient.h:190
Contains information about the physical socket connection.
Definition: SoDispCon.h:112
The SoDisp dispatches any socket connection that contains data by calling the SoDispCon::execute memb...
Definition: SoDisp.h:86