| Qore Programming Language - C/C++ Library 1.12.2

QoreHttpClientObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3 QoreHttpClientObject.h
4
5 Qore Programming Language
6
7 Copyright (C) 2006 - 2022 Qore Technologies, s.r.o.
8
9 Permission is hereby granted, free of charge, to any person obtaining a
10 copy of this software and associated documentation files (the "Software"),
11 to deal in the Software without restriction, including without limitation
12 the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 and/or sell copies of the Software, and to permit persons to whom the
14 Software is furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26
27 Note that the Qore library is released under a choice of three open-source
28 licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29 information.
30 */
31
32 #ifndef QORE_HTTP_CLIENT_OBJECT_H_
33 #define QORE_HTTP_CLIENT_OBJECT_H_
34
35 #include <qore/common.h>
36 #include <qore/QoreSocketObject.h>
37 #include <qore/OutputStream.h>
38
39 #define HTTPCLIENT_DEFAULT_PORT 80
40 #define HTTPCLIENT_DEFAULT_HOST "localhost"
41
42 #define HTTPCLIENT_DEFAULT_TIMEOUT 300000
43
44 #define HTTPCLIENT_DEFAULT_MAX_REDIRECTS 5
45
46 class Queue;
47
49
51 class QoreHttpClientObject : public QoreSocketObject {
52 friend struct qore_httpclient_priv;
53 friend class HttpClientConnectSendRecvPollOperation;
54
55 public:
57 DLLEXPORT QoreHttpClientObject();
58
60 DLLEXPORT virtual ~QoreHttpClientObject();
61
63
65 DLLEXPORT QoreObject* startPollConnect(ExceptionSink* xsink, QoreObject* self);
66
68
70 DLLEXPORT QoreObject* startPollSendRecv(ExceptionSink* xsink, QoreObject* self, const QoreString* method,
71 const QoreString* path, const AbstractQoreNode* data_save, const void* data, size_t size,
72 const QoreHashNode* headers, const QoreEncoding* enc = nullptr);
73
75
96 DLLEXPORT int setOptions(const QoreHashNode* opts, ExceptionSink* xsink);
97
99
101 DLLEXPORT void setDefaultPort(int prt);
102
104
106 DLLEXPORT void setDefaultPath(const char* pth);
107
109
111 DLLEXPORT const char* getDefaultPath() const;
112
114
116 DLLEXPORT const char* getConnectionPath() const;
117
119
121 DLLEXPORT void setConnectionPath(const char* path);
122
124
126 DLLEXPORT void addProtocol(const char* prot, int port, bool ssl = false);
127
129 DLLEXPORT void setTimeout(int to);
130
132 DLLEXPORT int getTimeout() const;
133
135 DLLEXPORT void setEncoding(const QoreEncoding* qe);
136
138 DLLEXPORT const QoreEncoding* getEncoding() const;
139
141
146 DLLEXPORT int setHTTPVersion(const char* version, ExceptionSink* xsink);
147
149
152 DLLEXPORT const char* getHTTPVersion() const;
153
155
158 DLLEXPORT void setHTTP11(bool h11);
159
161 DLLEXPORT bool isHTTP11() const;
162
164
168 DLLEXPORT int setURL(const char* url, ExceptionSink* xsink);
169
171
174 DLLEXPORT QoreStringNode* getURL();
175
177
182 DLLEXPORT QoreStringNode* getSafeURL();
183
185
189 DLLEXPORT void setUserPassword(const char* user, const char* pass);
190
192 DLLEXPORT void clearUserPassword();
193
195
199 DLLEXPORT int setProxyURL(const char* proxy, ExceptionSink* xsink);
200
202
205 DLLEXPORT QoreStringNode* getProxyURL();
206
208
213 DLLEXPORT QoreStringNode* getSafeProxyURL();
214
216 DLLEXPORT void clearProxyURL();
217
219
224 DLLEXPORT void setProxyUserPassword(const char* user, const char* pass);
225
227 DLLEXPORT void clearProxyUserPassword();
228
230 DLLEXPORT void setSecure(bool is_secure);
231
233 DLLEXPORT bool isSecure() const;
234
236 DLLEXPORT void setProxySecure(bool is_secure);
237
239 DLLEXPORT bool isProxySecure() const;
240
242 DLLEXPORT void setMaxRedirects(int max);
243
245 DLLEXPORT int getMaxRedirects() const;
246
248
250 DLLEXPORT int connect(ExceptionSink* xsink);
251
253 DLLEXPORT void disconnect();
254
256
271 DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers, const void* data,
272 unsigned size, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
273
274 DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers,
275 const QoreStringNode& body, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
276
277 DLLEXPORT QoreHashNode* sendWithSendCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
278 const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
279 ExceptionSink* xsink);
280
281 DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
282 const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
283 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
284
285 DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
286 const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
287 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
288
289 DLLEXPORT void sendWithCallbacks(const char* meth, const char* mpath, const QoreHashNode* headers,
290 const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
291 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
292
294
296 DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
297 const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
298 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
299
301
303 DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
304 const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
305 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
306
308
310 DLLEXPORT void sendChunked(const char* meth, const char* mpath, const QoreHashNode* headers, bool getbody,
311 QoreHashNode* info, int timeout_ms, const ResolvedCallReferenceNode* recv_callback, QoreObject* obj,
312 OutputStream *os, InputStream* is, size_t max_chunk_size,
313 const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
314
316
327 DLLEXPORT AbstractQoreNode* get(const char* path, const QoreHashNode* headers, QoreHashNode* info,
328 ExceptionSink* xsink);
329
331
340 DLLEXPORT QoreHashNode* head(const char* path, const QoreHashNode* headers, QoreHashNode* info,
341 ExceptionSink* xsink);
342
344
357 DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const void* data, unsigned size,
358 QoreHashNode* info, ExceptionSink* xsink);
359
361
373 DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const QoreStringNode& body,
374 QoreHashNode* info, ExceptionSink* xsink);
375
377
381 DLLEXPORT void setDefaultHeaderValue(const char* header, const char* val);
382
384
388 DLLEXPORT void addDefaultHeaders(const QoreHashNode* hdr);
389
391
395 DLLEXPORT QoreHashNode* getDefaultHeaders() const;
396
397 using AbstractPrivateData::deref;
399
402 DLLEXPORT virtual void deref(ExceptionSink* xsink);
403
405
408 DLLEXPORT void setConnectTimeout(int ms);
409
411
414 DLLEXPORT int getConnectTimeout() const;
415
417
426 DLLEXPORT int setNoDelay(bool nodelay);
427
429 DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
430
432 DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
433
435 DLLEXPORT bool getNoDelay() const;
436
438 DLLEXPORT bool isConnected() const;
439
441 DLLEXPORT void setPersistent(ExceptionSink* xsink);
442
443 DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
444 DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg,
445 int64 min_ms = 1000);
446 DLLEXPORT QoreHashNode* getUsageInfo() const;
447 DLLEXPORT void clearStats();
448
450
452 DLLEXPORT bool setErrorPassthru(bool set);
453
455
457 DLLEXPORT bool getErrorPassthru() const;
458
460
462 DLLEXPORT bool setRedirectPassthru(bool set);
463
465
467 DLLEXPORT bool getRedirectPassthru() const;
468
470
472 DLLEXPORT bool setEncodingPassthru(bool set);
473
475
477 DLLEXPORT bool getEncodingPassthru() const;
478
480
482 DLLEXPORT QoreStringNode* getHostHeaderValue() const;
483
485
487 DLLEXPORT void setAssumedEncoding(const char* enc);
488
490
492 DLLEXPORT QoreStringNode* getAssumedEncoding() const;
493
494 DLLLOCAL static void static_init();
495
496 DLLLOCAL void cleanup(ExceptionSink* xsink);
497
498 protected:
499 DLLEXPORT void lock();
500 DLLEXPORT void unlock();
501
502 private:
504 struct qore_httpclient_priv* http_priv;
505
507 DLLLOCAL QoreHttpClientObject(const QoreHttpClientObject&);
508
510 DLLLOCAL QoreHttpClientObject& operator=(const QoreHttpClientObject&);
511 };
512
513 #endif
AbstractPrivateData::deref
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:65
AbstractQoreNode
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:57
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
InputStream
Interface for private data of input streams.
Definition: InputStream.h:44
OutputStream
Interface for private data of output streams.
Definition: OutputStream.h:44
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
QoreHttpClientObject
provides a way to communicate with HTTP servers using Qore data structures
Definition: QoreHttpClientObject.h:51
QoreHttpClientObject::post
DLLEXPORT AbstractQoreNode * post(const char *path, const QoreHashNode *headers, const void *data, unsigned size, QoreHashNode *info, ExceptionSink *xsink)
sends an HTTP "POST" message to the remote server and returns the message body of the response
QoreHttpClientObject::setEncodingPassthru
DLLEXPORT bool setEncodingPassthru(bool set)
sets the new and returns the old encoding_passthru status
QoreHttpClientObject::isHTTP11
DLLEXPORT bool isHTTP11() const
returns true if HTTP 1.1 protocol compliance has been set
QoreHttpClientObject::~QoreHttpClientObject
virtual DLLEXPORT ~QoreHttpClientObject()
destroys the object and frees all associated memory
QoreHttpClientObject::clearUserPassword
DLLEXPORT void clearUserPassword()
clears the username and password for the connection
QoreHttpClientObject::getErrorPassthru
DLLEXPORT bool getErrorPassthru() const
returns the current error_passthru status
QoreHttpClientObject::getMaxRedirects
DLLEXPORT int getMaxRedirects() const
returns the value of the max_redirects option
QoreHttpClientObject::setProxyUserPassword
DLLEXPORT void setProxyUserPassword(const char *user, const char *pass)
sets the username and password for the proxy connection
QoreHttpClientObject::setConnectionPath
DLLEXPORT void setConnectionPath(const char *path)
sets or clears the connection path
QoreHttpClientObject::send
DLLEXPORT QoreHashNode * send(const char *meth, const char *path, const QoreHashNode *headers, const void *data, unsigned size, bool getbody, QoreHashNode *info, ExceptionSink *xsink)
sends a message to the remote server and returns the entire response as a hash
QoreHttpClientObject::sendChunked
DLLEXPORT void sendChunked(const char *meth, const char *mpath, const QoreHashNode *headers, bool getbody, QoreHashNode *info, int timeout_ms, const ResolvedCallReferenceNode *recv_callback, QoreObject *obj, OutputStream *os, InputStream *is, size_t max_chunk_size, const ResolvedCallReferenceNode *trailer_callback, ExceptionSink *xsink)
send a chunked HTTP message through an InputStream and receive the response to an OutputStream
QoreHttpClientObject::setErrorPassthru
DLLEXPORT bool setErrorPassthru(bool set)
sets the new and returns the old error_passthru status
QoreHttpClientObject::getConnectTimeout
DLLEXPORT int getConnectTimeout() const
returns the connect timeout in ms, negative numbers mean no timeout
QoreHttpClientObject::getAssumedEncoding
DLLEXPORT QoreStringNode * getAssumedEncoding() const
gets the assumed encoding
QoreHttpClientObject::clearProxyURL
DLLEXPORT void clearProxyURL()
clears the proxy URL
QoreHttpClientObject::setDefaultPort
DLLEXPORT void setDefaultPort(int prt)
sets the default port
QoreHttpClientObject::setTimeout
DLLEXPORT void setTimeout(int to)
sets the connection and response packet timeout value in milliseconds
QoreHttpClientObject::getTimeout
DLLEXPORT int getTimeout() const
returns the connection and response packet timeout value in milliseconds
QoreHttpClientObject::getDefaultPath
DLLEXPORT const char * getDefaultPath() const
returns the default path or 0 if none is set
QoreHttpClientObject::getConnectionPath
DLLEXPORT const char * getConnectionPath() const
returns the current connection path or 0 if none is set
QoreHttpClientObject::startPollConnect
DLLEXPORT QoreObject * startPollConnect(ExceptionSink *xsink, QoreObject *self)
Starts a socket connect poll operation.
QoreHttpClientObject::connect
DLLEXPORT int connect(ExceptionSink *xsink)
opens a connection and returns a code giving the result
QoreHttpClientObject::getSafeURL
DLLEXPORT QoreStringNode * getSafeURL()
returns the connection parameters as a URL without any password
QoreHttpClientObject::getURL
DLLEXPORT QoreStringNode * getURL()
returns the connection parameters as a URL
QoreHttpClientObject::setEventQueue
DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink *xsink)
sets the event queue, must be already referenced before call
QoreHttpClientObject::isSecure
DLLEXPORT bool isSecure() const
returns the SSL connection parameter flag
QoreHttpClientObject::setProxySecure
DLLEXPORT void setProxySecure(bool is_secure)
sets the SSL flag for use in the next connection to the proxy
QoreHttpClientObject::startPollSendRecv
DLLEXPORT QoreObject * startPollSendRecv(ExceptionSink *xsink, QoreObject *self, const QoreString *method, const QoreString *path, const AbstractQoreNode *data_save, const void *data, size_t size, const QoreHashNode *headers, const QoreEncoding *enc=nullptr)
Starts a non-blocking, polling HTTP send/receive operation.
QoreHttpClientObject::getNoDelay
DLLEXPORT bool getNoDelay() const
returns the value of the TCP_NODELAY flag on the object
QoreHttpClientObject::setDefaultHeaderValue
DLLEXPORT void setDefaultHeaderValue(const char *header, const char *val)
sets the value of a default header to send with every outgoing message
QoreHttpClientObject::deref
virtual DLLLOCAL void deref(ExceptionSink *xsink)
decrements the reference count of the object
Definition: AbstractPrivateData.h:59
QoreHttpClientObject::setAssumedEncoding
DLLEXPORT void setAssumedEncoding(const char *enc)
sets the assumed encoding
QoreHttpClientObject::get
DLLEXPORT AbstractQoreNode * get(const char *path, const QoreHashNode *headers, QoreHashNode *info, ExceptionSink *xsink)
sends an HTTP "GET" method and returns the value of the message body returned
QoreHttpClientObject::setNoDelay
DLLEXPORT int setNoDelay(bool nodelay)
sets the TCP_NODELAY flag on the object
QoreHttpClientObject::getEncodingPassthru
DLLEXPORT bool getEncodingPassthru() const
returns the current encoding_passthru status
QoreHttpClientObject::getEncoding
DLLEXPORT const QoreEncoding * getEncoding() const
returns the default encoding for the object
QoreHttpClientObject::setEncoding
DLLEXPORT void setEncoding(const QoreEncoding *qe)
sets the default encoding for the object
QoreHttpClientObject::addDefaultHeaders
DLLEXPORT void addDefaultHeaders(const QoreHashNode *hdr)
Sets the value of multiple headers to send with every outgoing message.
QoreHttpClientObject::isProxySecure
DLLEXPORT bool isProxySecure() const
returns the SSL proxy connection parameter flag
QoreHttpClientObject::getProxyURL
DLLEXPORT QoreStringNode * getProxyURL()
returns the proxy connection parameters as a URL (or 0 if there is none)
QoreHttpClientObject::setConnectTimeout
DLLEXPORT void setConnectTimeout(int ms)
sets the connect timeout in ms
QoreHttpClientObject::setSecure
DLLEXPORT void setSecure(bool is_secure)
sets the SSL flag for use in the next connection
QoreHttpClientObject::setHTTPVersion
DLLEXPORT int setHTTPVersion(const char *version, ExceptionSink *xsink)
sets the http version from a string
QoreHttpClientObject::head
DLLEXPORT QoreHashNode * head(const char *path, const QoreHashNode *headers, QoreHashNode *info, ExceptionSink *xsink)
sends an HTTP "HEAD" method and returns the headers returned, the caller owns the QoreHashNode refere...
QoreHttpClientObject::setProxyURL
DLLEXPORT int setProxyURL(const char *proxy, ExceptionSink *xsink)
sets the proxy URL
QoreHttpClientObject::clearProxyUserPassword
DLLEXPORT void clearProxyUserPassword()
clears the username and password for the proxy connection
QoreHttpClientObject::setMaxRedirects
DLLEXPORT void setMaxRedirects(int max)
sets the max_redirects option
QoreHttpClientObject::setUserPassword
DLLEXPORT void setUserPassword(const char *user, const char *pass)
sets the username and password for the connection
QoreHttpClientObject::isConnected
DLLEXPORT bool isConnected() const
returns the connection status of the object
QoreHttpClientObject::getHostHeaderValue
DLLEXPORT QoreStringNode * getHostHeaderValue() const
returns the Host header value
QoreHttpClientObject::setPersistent
DLLEXPORT void setPersistent(ExceptionSink *xsink)
temporarily disables implicit reconnections; must be called when the server is already connected
QoreHttpClientObject::getRedirectPassthru
DLLEXPORT bool getRedirectPassthru() const
returns the current redirect_passthru status
QoreHttpClientObject::getDefaultHeaders
DLLEXPORT QoreHashNode * getDefaultHeaders() const
Returns a hash of default headers to be sent with every outgoing request.
QoreHttpClientObject::setDefaultPath
DLLEXPORT void setDefaultPath(const char *pth)
sets the default path
QoreHttpClientObject::addProtocol
DLLEXPORT void addProtocol(const char *prot, int port, bool ssl=false)
adds a protocol
QoreHttpClientObject::sendWithOutputStream
DLLEXPORT void sendWithOutputStream(const char *meth, const char *mpath, const QoreHashNode *headers, const void *data, unsigned size, bool getbody, QoreHashNode *info, int timeout_ms, const ResolvedCallReferenceNode *recv_callback, QoreObject *obj, OutputStream *os, ExceptionSink *xsink)
make an HTTP request and receive the response to an OutputStream
QoreHttpClientObject::setOptions
DLLEXPORT int setOptions(const QoreHashNode *opts, ExceptionSink *xsink)
set options with a hash, returns -1 if an exception was thrown, 0 for OK
QoreHttpClientObject::setRedirectPassthru
DLLEXPORT bool setRedirectPassthru(bool set)
sets the new and returns the old redirect_passthru status
QoreHttpClientObject::QoreHttpClientObject
DLLEXPORT QoreHttpClientObject()
creates the QoreHttpClientObject object
QoreHttpClientObject::getSafeProxyURL
DLLEXPORT QoreStringNode * getSafeProxyURL()
returns the proxy connection parameters as a URL without any password (or 0 if there is none)
QoreHttpClientObject::getHTTPVersion
DLLEXPORT const char * getHTTPVersion() const
returns the http version as a string (either "1.0" or "1.1")
QoreHttpClientObject::setURL
DLLEXPORT int setURL(const char *url, ExceptionSink *xsink)
sets the connection URL
QoreHttpClientObject::setHTTP11
DLLEXPORT void setHTTP11(bool h11)
sets or clears HTTP 1.1 protocol compliance
QoreHttpClientObject::disconnect
DLLEXPORT void disconnect()
disconnects from the remote server
QoreObject
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition: QoreObject.h:60
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
QoreStringNode
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
ResolvedCallReferenceNode
base class for resolved call references
Definition: CallReferenceNode.h:109
common.h
int64
long long int64
64bit integer type, cannot use int64_t here since it breaks the API on some 64-bit systems due to equ...
Definition: common.h:260
QoreValue
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:275