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 5392 2023-02-21 15:56:50Z wini $
15 *
16 * COPYRIGHT: Real Time Logic LLC, 2009-2020
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
293 int request(HttpMethod methodType,
294 const char* url,
295 const char* userPass=0,
296 const HttpClientKeyVal* query=0,
297 const HttpClientKeyVal* headers=0,
298 BaFileSize size=0);
299
310 int sendData(const void* data, int len);
311
312 int getBufSize();
313
318 int readData(void* buf, int bufSize);
319
324 const char* getHeaderValue(const char* name);
325
337 HttpClientHeader* getHeaders(int* hlen);
338
341 void close();
342
346 int getStatus();
347
352 int getError();
353
356 SharkSslCon* getSharkSslCon();
357
358 SoDispCon* getSoDispCon();
359#else
360{
361 SoDispCon super;
362#endif
363 DynBuffer db;
364 HttpClientHeader headers[HTTP_CLIENT_MAX_HEADERS];
365
366 struct SharkSsl* sharkSslClient; /* optional SSL client */
367
368 /* Variable proxyPortNo and the following 3 variables must be
369 * set and managed by the user of this class.
370 */
371 const char* proxy; /* Proxy name/IP addr, if any */
372 const char* proxyUserPass; /* Format: "user:password" */
373 const char* intfName; /* If 0: bind to any intf, or bind to intfName */
374
375 char* data; /* Pointer to start of payload data */
376 char* host; /* Server host name */
377 BaFileSize size; /* Send or receive data size */
378 BaTime readTmo; /* Default read timeout is 20 seconds */
379 int chunkSize;
380 int lastError;
381 int portNo; /* host port number */
382 U16 proxyPortNo; /* host port number */
383 U16 headerLen;
384 S16 httpStatus;
385 BaBool chunkEncoding;
386 BaBool respManaged; /* Func HttpClient_manageEndOfRequest */
387 BaBool closeCon;
388 BaBool acceptTrusted; /* Accept only trusted connections */
389 /*
390 HttpClient_SocksProxy
391 HttpClient_Persistent
392 HttpClient_IPv6
393 */
394 U8 mode;
395 U8 methodType;
397
398#ifdef __cplusplus
399extern "C" {
400#endif
401
402void HttpClient_constructor(HttpClient* o, SoDisp* disp, U8 mode);
403#define HttpClient_setSSL(o, ssl) (o)->sharkSslClient=ssl
404#define HttpClient_setReadTmo(o, timeout) (o)->readTmo=timeout
405#ifndef NO_SHARKSSL
406SharkSslCon* HttpClient_getSharkSslCon(HttpClient* o);
407#endif
408
409void HttpClient_destructor(HttpClient* o);
410int HttpClient_isURL(const char* url);
411int HttpClient_request(HttpClient* o,
412 HttpMethod methodType,
413 const char* url,
414 const char* userPass,
415 const HttpClientKeyVal* params,
416 const HttpClientKeyVal* headers,
417 BaFileSize size);
418
419int HttpClient_sendData(HttpClient* o, const void* data, int len);
420int HttpClient_getBufSize(HttpClient* o);
421int HttpClient_readDataBuffered(HttpClient* o, void* buf, int bufSize);
422int HttpClient_readData(HttpClient* o, void* buf, int bufSize);
423const char* HttpClient_getHeaderValue(HttpClient* o, const char* name);
424HttpClientHeader* HttpClient_getHeaders(HttpClient* o, int* hlen);
425void HttpClient_close(HttpClient* o);
426int HttpClient_getStatus(HttpClient* o);
427#define HttpClient_getError(o) (o)->lastError
428#define HttpClient_getSoDispCon(o) ((SoDispCon*)(o))
429#ifndef NO_SHARKSSL
430SharkSslConTrust HttpClient_trusted(HttpClient* o);
431#endif
432#define HttpClient_setAcceptTrusted(o, t) (o)->acceptTrusted=t
433
434#ifdef __cplusplus
435}
436
437inline HttpClient::HttpClient(SoDisp* disp, U8 mode) {
438 HttpClient_constructor(this,disp, mode);
439}
440
442 HttpClient_destructor(this);
443}
444
445inline void HttpClient::setSSL(SharkSsl* ssl) {
446 HttpClient_setSSL(this,ssl);
447}
448
449inline void HttpClient::setReadTmo(BaTime timeout) {
450 HttpClient_setReadTmo(this,timeout);
451}
452
453inline int HttpClient::isURL(const char* url) {
454 return HttpClient_isURL(url);
455}
456
457
458inline SharkSslConTrust HttpClient::trusted(void){
459 return HttpClient_trusted(this);
460}
461
462inline void HttpClient::setAcceptTrusted(bool t) {
463 HttpClient_setAcceptTrusted(this, t?TRUE:FALSE);
464}
465
466
467inline SharkSslCon* HttpClient::getSharkSslCon() {
468 return HttpClient_getSharkSslCon(this);
469}
470
471
472inline int HttpClient::request(HttpMethod methodType,
473 const char* url,
474 const char* userPass,
475 const HttpClientKeyVal* params,
476 const HttpClientKeyVal* headers,
477 BaFileSize size)
478{
479 return HttpClient_request(this,methodType,url,userPass,params,headers,size);
480}
481
482inline int HttpClient::sendData(const void* data, int len) {
483 return HttpClient_sendData(this, data, len);
484}
485
486inline int HttpClient::getBufSize() {
487 return HttpClient_getBufSize(this);
488}
489
490inline int HttpClient::readData(void* buf, int bufSize) {
491 return HttpClient_readData(this, buf, bufSize);
492}
493
494inline const char* HttpClient::getHeaderValue(const char* name) {
495 return HttpClient_getHeaderValue(this, name);
496}
497
499 return HttpClient_getHeaders(this, hlen);
500}
501
502inline void HttpClient::close() {
503 HttpClient_close(this);
504}
505
507 return HttpClient_getStatus(this);
508}
509
510
512 return HttpClient_getError(this);
513}
514
515inline SoDispCon* HttpClient::getSoDispCon() {
516 return HttpClient_getSoDispCon(this);
517}
518
519inline const char* HttpClientHeader::getKey(HttpClient* c) {
520 return c->db.buf + key;
521}
522inline const char* HttpClientHeader::getVal(HttpClient* c) {
523 return c->db.buf + val;
524}
525#endif
526 /* End group HttpClient */
528
529
530#endif
static int isURL(const char *url)
Returns true if the URL is valid.
Definition: HttpClient.h:453
int readData(void *buf, int bufSize)
Read HTTP response data.
Definition: HttpClient.h:490
void setSSL(SharkSsl *ssl)
Set the SharkSSL client object to enable https: URL's.
Definition: HttpClient.h:445
~HttpClient()
Terminate the HttpClient.
Definition: HttpClient.h:441
void close()
Close a persisten HTTP 1.1 connection.
Definition: HttpClient.h:502
HttpClientHeader * getHeaders(int *hlen)
Returns all HTTP response headers.
Definition: HttpClient.h:498
int sendData(const void *data, int len)
Send data to the server.
Definition: HttpClient.h:482
int request(HttpMethod methodType, const char *url, const char *userPass=0, const HttpClientKeyVal *query=0, const HttpClientKeyVal *headers=0, BaFileSize size=0)
Definition: HttpClient.h:472
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:494
SharkSslCon * getSharkSslCon()
Wrapper for SoDispCon:getSharkSslCon.
Definition: HttpClient.h:467
int getError()
Returns the last socket error code if any.
Definition: HttpClient.h:511
const char * getVal(HttpClient *c)
Returns the value.
Definition: HttpClient.h:522
int getStatus()
Returns the server's HTTP response code or a negative value on socket errors.
Definition: HttpClient.h:506
void setReadTmo(BaTime timeout)
Set the read timeout (in milliseconds).
Definition: HttpClient.h:449
const char * getKey(HttpClient *c)
Returns the key.
Definition: HttpClient.h:519
SharkSslConTrust trusted(void)
Returns the peer's "trust" status.
Definition: HttpClient.h:458
struct HttpClientKeyVal HttpClientKeyVal
A container for key/value pairs that is used when setting custom HTTP headers and/or when setting URL...
void setAcceptTrusted(bool acceptTrusted)
Force method HttpClient::request to accept only trusted connections when connecting to a server.
Definition: HttpClient.h:462
HttpClient(SoDisp *disp, U8 mode=HttpClient_Persistent)
Create a HttpClient instance.
Definition: HttpClient.h:437
HttpMethod
HTTP method types.
Definition: HttpServer.h:772
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 that is used when setting custom HTTP headers and/or when setting URL...
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