Qore Programming Language 2.1.1
Loading...
Searching...
No Matches
QoreHttpClientObject.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreHttpClientObject.h
4
5 Qore Programming Language
6
7 Copyright (C) 2006 - 2024 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#define HTTPCLIENT_DEFAULT_CONNECT_TIMEOUT 60000
44
45#define HTTPCLIENT_DEFAULT_MAX_REDIRECTS 5
46
47constexpr int64 UC_TARGET = (1 << 0);
48constexpr int64 UC_USERNAME = (1 << 1);
49constexpr int64 UC_PASSWORD = (1 << 2);
50constexpr int64 UC_MASK_PASSWORD = (1 << 3);
51constexpr int64 UC_PATH = (1 << 4);
52
53constexpr int64 URL_NO_AUTH = UC_TARGET | UC_PATH;
54constexpr int64 URL_NORMAL = UC_TARGET | UC_USERNAME | UC_PASSWORD | UC_PATH;
55constexpr int64 URL_MASK_PASSWORD = URL_NORMAL | UC_MASK_PASSWORD;
56
57class Queue;
58
60
62class QoreHttpClientObject : public QoreSocketObject {
63 friend hashdecl qore_httpclient_priv;
64 friend class HttpClientConnectSendRecvPollOperation;
65
66public:
69
71 DLLEXPORT virtual ~QoreHttpClientObject();
72
74
77
79
81 DLLEXPORT QoreObject* startPollSendRecv(ExceptionSink* xsink, QoreObject* self, const QoreString* method,
82 const QoreString* path, const AbstractQoreNode* data_save, const void* data, size_t size,
83 const QoreHashNode* headers, const QoreEncoding* enc = nullptr);
84
86
107 DLLEXPORT int setOptions(const QoreHashNode* opts, ExceptionSink* xsink);
108
110
112 DLLEXPORT void setDefaultPort(int prt);
113
115
117 DLLEXPORT void setDefaultPath(const char* pth);
118
120
122 DLLEXPORT const char* getDefaultPath() const;
123
125
127 DLLEXPORT const char* getConnectionPath() const;
128
130
132 DLLEXPORT void setConnectionPath(const char* path);
133
135
137 DLLEXPORT void addProtocol(const char* prot, int port, bool ssl = false);
138
140 DLLEXPORT void setTimeout(int to);
141
143 DLLEXPORT int getTimeout() const;
144
146 DLLEXPORT void setEncoding(const QoreEncoding* qe);
147
149 DLLEXPORT const QoreEncoding* getEncoding() const;
150
152
157 DLLEXPORT int setHTTPVersion(const char* version, ExceptionSink* xsink);
158
160
163 DLLEXPORT const char* getHTTPVersion() const;
164
166
169 DLLEXPORT void setHTTP11(bool h11);
170
172 DLLEXPORT bool isHTTP11() const;
173
175
179 DLLEXPORT int setURL(const char* url, ExceptionSink* xsink);
180
182
189 DLLEXPORT QoreStringNode* getUrl(int64 code = URL_NORMAL);
190
192
196 DLLEXPORT void setUserPassword(const char* user, const char* pass);
197
199 DLLEXPORT void clearUserPassword();
200
202
206 DLLEXPORT int setProxyURL(const char* proxy, ExceptionSink* xsink);
207
209
213
215
221
223 DLLEXPORT void clearProxyURL();
224
226
231 DLLEXPORT void setProxyUserPassword(const char* user, const char* pass);
232
234 DLLEXPORT void clearProxyUserPassword();
235
237
241 DLLEXPORT QoreStringNode* getUsername() const;
242
244
248 DLLEXPORT QoreStringNode* getPassword() const;
249
251
256
258
263
265 DLLEXPORT void setSecure(bool is_secure);
266
268 DLLEXPORT bool isSecure() const;
269
271 DLLEXPORT void setProxySecure(bool is_secure);
272
274 DLLEXPORT bool isProxySecure() const;
275
277 DLLEXPORT void setMaxRedirects(int max);
278
280 DLLEXPORT int getMaxRedirects() const;
281
283
285 DLLEXPORT int connect(ExceptionSink* xsink);
286
288 DLLEXPORT void disconnect();
289
291
306 DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers, const void* data,
307 unsigned size, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
308
309 DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers,
310 const QoreStringNode& body, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
311
312 DLLEXPORT QoreHashNode* sendWithSendCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
313 const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
314 ExceptionSink* xsink);
315
316 DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
317 const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
318 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
319
320 DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
321 const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
322 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
323
324 DLLEXPORT void sendWithCallbacks(const char* meth, const char* mpath, const QoreHashNode* headers,
325 const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
326 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
327
329
331 DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
332 const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
333 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
334
336
338 DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
339 const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
340 const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
341
343
345 DLLEXPORT void sendChunked(const char* meth, const char* mpath, const QoreHashNode* headers, bool getbody,
346 QoreHashNode* info, int timeout_ms, const ResolvedCallReferenceNode* recv_callback, QoreObject* obj,
347 OutputStream *os, InputStream* is, size_t max_chunk_size,
348 const ResolvedCallReferenceNode* trailer_callback, ExceptionSink* xsink);
349
351
362 DLLEXPORT AbstractQoreNode* get(const char* path, const QoreHashNode* headers, QoreHashNode* info,
363 ExceptionSink* xsink);
364
366
375 DLLEXPORT QoreHashNode* head(const char* path, const QoreHashNode* headers, QoreHashNode* info,
376 ExceptionSink* xsink);
377
379
392 DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const void* data, unsigned size,
393 QoreHashNode* info, ExceptionSink* xsink);
394
396
408 DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const QoreStringNode& body,
409 QoreHashNode* info, ExceptionSink* xsink);
410
412
416 DLLEXPORT void setDefaultHeaderValue(const char* header, const char* val);
417
419
423 DLLEXPORT void addDefaultHeaders(const QoreHashNode* hdr);
424
426
430 DLLEXPORT QoreHashNode* getDefaultHeaders() const;
431
434
437 DLLEXPORT virtual void deref(ExceptionSink* xsink);
438
440
443 DLLEXPORT void setConnectTimeout(int ms);
444
446
449 DLLEXPORT int getConnectTimeout() const;
450
452
461 DLLEXPORT int setNoDelay(bool nodelay);
462
464 DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
465
467 DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
468
470 DLLEXPORT bool getNoDelay() const;
471
473 DLLEXPORT bool isConnected() const;
474
476 DLLEXPORT void setPersistent(ExceptionSink* xsink);
477
479
481 DLLEXPORT void clearPersistent();
482
484
486 DLLEXPORT bool isPersistent() const;
487
489
491 DLLEXPORT unsigned getPersistentCount() const;
492
493 DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
494 DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg,
495 int64 min_ms = 1000);
496 DLLEXPORT QoreHashNode* getUsageInfo() const;
497 DLLEXPORT void clearStats();
498
500
502 DLLEXPORT bool setErrorPassthru(bool set);
503
505
507 DLLEXPORT bool getErrorPassthru() const;
508
510
512 DLLEXPORT bool setRedirectPassthru(bool set);
513
515
517 DLLEXPORT bool getRedirectPassthru() const;
518
520
522 DLLEXPORT bool setEncodingPassthru(bool set);
523
525
527 DLLEXPORT bool getEncodingPassthru() const;
528
530
533
535
537 DLLEXPORT void setAssumedEncoding(const char* enc);
538
540
543
545
547 DLLEXPORT bool setPreEncodedUrls(bool set);
548
550
552 DLLEXPORT bool getPreEncodedUrls() const;
553
555
557 DLLEXPORT QoreHashNode* getConfig() const;
558
559 DLLLOCAL static void static_init();
560
561 DLLLOCAL void cleanup(ExceptionSink* xsink);
562
563protected:
564 DLLEXPORT void lock();
565 DLLEXPORT void unlock();
566
567private:
569 hashdecl qore_httpclient_priv* http_priv;
570
573
575 DLLLOCAL QoreHttpClientObject& operator=(const QoreHttpClientObject&);
576};
577
578#endif
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition AbstractPrivateData.h:65
The base class for all value and parse types in Qore expression trees.
Definition AbstractQoreNode.h:57
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition ExceptionSink.h:50
Interface for private data of input streams.
Definition InputStream.h:44
Interface for private data of output streams.
Definition OutputStream.h:44
defines string encoding functions in Qore
Definition QoreEncoding.h:83
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition QoreHashNode.h:51
provides a way to communicate with HTTP servers using Qore data structures
Definition QoreHttpClientObject.h:62
DLLEXPORT void sendWithOutputStream(const char *meth, const char *mpath, const QoreHashNode *headers, const QoreStringNode &body, 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
DLLEXPORT QoreStringNode * getProxyUsername() const
Returns the username for the proxy connection, if any.
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
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
DLLEXPORT bool setEncodingPassthru(bool set)
sets the new and returns the old encoding_passthru status
DLLEXPORT const QoreEncoding * getEncoding() const
returns the default encoding for the object
virtual DLLEXPORT void deref(ExceptionSink *xsink)
decrements the reference count and deletes the object when it reaches 0
DLLEXPORT const char * getConnectionPath() const
returns the current connection path or 0 if none is set
DLLEXPORT bool isHTTP11() const
returns true if HTTP 1.1 protocol compliance has been set
virtual DLLEXPORT ~QoreHttpClientObject()
destroys the object and frees all associated memory
DLLEXPORT void clearUserPassword()
clears the username and password for the connection
DLLEXPORT bool setPreEncodedUrls(bool set)
sets the new and returns the old pre_encoded_urls flag
DLLEXPORT bool getErrorPassthru() const
returns the current error_passthru status
DLLEXPORT int getMaxRedirects() const
returns the value of the max_redirects option
DLLEXPORT void setProxyUserPassword(const char *user, const char *pass)
sets the username and password for the proxy connection
DLLEXPORT void setConnectionPath(const char *path)
sets or clears the connection path
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...
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.
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
DLLEXPORT bool setErrorPassthru(bool set)
sets the new and returns the old error_passthru status
DLLEXPORT QoreStringNode * getAssumedEncoding() const
gets the assumed encoding
DLLEXPORT int getConnectTimeout() const
returns the connect timeout in ms, negative numbers mean no timeout
DLLEXPORT void clearProxyURL()
clears the proxy URL
DLLEXPORT void setDefaultPort(int prt)
sets the default port
DLLEXPORT void setTimeout(int to)
sets the connection and response packet timeout value in milliseconds
DLLEXPORT int getTimeout() const
returns the connection and response packet timeout value in milliseconds
DLLEXPORT int connect(ExceptionSink *xsink)
opens a connection and returns a code giving the result
DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink *xsink)
sets the event queue, must be already referenced before call
DLLEXPORT bool isSecure() const
returns the SSL connection parameter flag
DLLEXPORT void setProxySecure(bool is_secure)
sets the SSL flag for use in the next connection to the proxy
DLLEXPORT bool getNoDelay() const
returns the value of the TCP_NODELAY flag on the object
DLLEXPORT void setDefaultHeaderValue(const char *header, const char *val)
sets the value of a default header to send with every outgoing message
DLLEXPORT AbstractQoreNode * post(const char *path, const QoreHashNode *headers, const QoreStringNode &body, QoreHashNode *info, ExceptionSink *xsink)
sends an HTTP "POST" message to the remote server and returns the message body of the response
DLLEXPORT QoreStringNode * getSafeProxyURL()
returns the proxy connection parameters as a URL without any password (or 0 if there is none)
DLLEXPORT void setAssumedEncoding(const char *enc)
sets the assumed encoding
DLLEXPORT const char * getHTTPVersion() const
returns the http version as a string (either "1.0" or "1.1")
DLLEXPORT int setNoDelay(bool nodelay)
sets the TCP_NODELAY flag on the object
DLLEXPORT bool isPersistent() const
Returns the value of the persistent connection flag.
DLLEXPORT bool getEncodingPassthru() const
returns the current encoding_passthru status
DLLEXPORT void setEncoding(const QoreEncoding *qe)
sets the default encoding for the object
DLLEXPORT void addDefaultHeaders(const QoreHashNode *hdr)
Sets the value of multiple headers to send with every outgoing message.
DLLEXPORT void setEventQueue(ExceptionSink *xsink, Queue *q, QoreValue arg, bool with_data)
sets the event queue, must be already referenced before call
DLLEXPORT bool isProxySecure() const
returns the SSL proxy connection parameter flag
DLLEXPORT void setConnectTimeout(int ms)
sets the connect timeout in ms
DLLEXPORT bool getPreEncodedUrls() const
returns the current pre_encoded_urls flag
DLLEXPORT void setSecure(bool is_secure)
sets the SSL flag for use in the next connection
DLLEXPORT QoreStringNode * getProxyPassword() const
Returns the password for the proxy connection, if any.
DLLEXPORT int setHTTPVersion(const char *version, ExceptionSink *xsink)
sets the http version from a string
DLLEXPORT int setProxyURL(const char *proxy, ExceptionSink *xsink)
sets the proxy URL
DLLEXPORT void clearProxyUserPassword()
clears the username and password for the proxy connection
DLLEXPORT void setMaxRedirects(int max)
sets the max_redirects option
DLLEXPORT void setUserPassword(const char *user, const char *pass)
sets the username and password for the connection
DLLEXPORT bool isConnected() const
returns the connection status of the object
DLLEXPORT QoreObject * startPollConnect(ExceptionSink *xsink, QoreObject *self)
Starts a socket connect poll operation.
DLLEXPORT void setPersistent(ExceptionSink *xsink)
temporarily disables implicit reconnections; must be called when the server is already connected
DLLEXPORT bool getRedirectPassthru() const
returns the current redirect_passthru status
DLLEXPORT QoreStringNode * getUrl(int64 code=URL_NORMAL)
returns the connection parameters as a URL according to the parameters passed
DLLEXPORT void setDefaultPath(const char *pth)
sets the default path
DLLEXPORT void clearPersistent()
Clears the persistent connection flag and reenables implicit reconnections.
DLLEXPORT void addProtocol(const char *prot, int port, bool ssl=false)
adds a protocol
DLLEXPORT QoreHashNode * getConfig() const
Returns a configuration hash for the object.
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
DLLEXPORT const char * getDefaultPath() const
returns the default path or 0 if none is set
DLLEXPORT int setOptions(const QoreHashNode *opts, ExceptionSink *xsink)
set options with a hash, returns -1 if an exception was thrown, 0 for OK
DLLEXPORT bool setRedirectPassthru(bool set)
sets the new and returns the old redirect_passthru status
DLLEXPORT QoreHttpClientObject()
creates the QoreHttpClientObject object
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
DLLEXPORT QoreHashNode * getDefaultHeaders() const
Returns a hash of default headers to be sent with every outgoing request.
DLLEXPORT int setURL(const char *url, ExceptionSink *xsink)
sets the connection URL
DLLEXPORT QoreStringNode * getUsername() const
Returns the username for the connection, if any.
DLLEXPORT void setHTTP11(bool h11)
sets or clears HTTP 1.1 protocol compliance
DLLEXPORT QoreStringNode * getHostHeaderValue() const
returns the Host header value
DLLEXPORT void disconnect()
disconnects from the remote server
DLLEXPORT unsigned getPersistentCount() const
Returns the persistent connection count.
DLLEXPORT QoreStringNode * getProxyURL()
returns the proxy connection parameters as a URL (or 0 if there is none)
DLLEXPORT QoreStringNode * getPassword() const
Returns the password for the connection, if any.
the implementation of Qore's object data type, reference counted, dynamically-allocated only
Definition QoreObject.h:61
Qore's string type supported by the QoreEncoding class.
Definition QoreString.h:93
Qore's string value type, reference counted, dynamically-allocated only.
Definition QoreStringNode.h:50
base class for resolved call references
Definition CallReferenceNode.h:115
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:266
The main value class in Qore, designed to be passed by value.
Definition QoreValue.h:279