Qore Programming Language  0.9.4.6
QoreHttpClientObject.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreHttpClientObject.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2006 - 2020 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 private:
54  hashdecl qore_httpclient_priv* http_priv;
55 
58 
60  DLLLOCAL QoreHttpClientObject& operator=(const QoreHttpClientObject&);
61 
62 protected:
63  DLLEXPORT void lock();
64  DLLEXPORT void unlock();
65 
66 public:
68  DLLEXPORT QoreHttpClientObject();
69 
71  DLLEXPORT virtual ~QoreHttpClientObject();
72 
74 
91  DLLEXPORT int setOptions(const QoreHashNode* opts, ExceptionSink* xsink);
92 
94 
96  DLLEXPORT void setDefaultPort(int prt);
97 
99 
101  DLLEXPORT void setDefaultPath(const char* pth);
102 
104 
106  DLLEXPORT const char* getDefaultPath() const;
107 
109 
111  DLLEXPORT const char* getConnectionPath() const;
112 
114 
116  DLLEXPORT void setConnectionPath(const char* path);
117 
119 
121  DLLEXPORT void addProtocol(const char* prot, int port, bool ssl = false);
122 
124  DLLEXPORT void setTimeout(int to);
125 
127  DLLEXPORT int getTimeout() const;
128 
130  DLLEXPORT void setEncoding(const QoreEncoding* qe);
131 
133  DLLEXPORT const QoreEncoding* getEncoding() const;
134 
136 
141  DLLEXPORT int setHTTPVersion(const char* version, ExceptionSink* xsink);
142 
144 
147  DLLEXPORT const char* getHTTPVersion() const;
148 
150 
153  DLLEXPORT void setHTTP11(bool h11);
154 
156  DLLEXPORT bool isHTTP11() const;
157 
159 
163  DLLEXPORT int setURL(const char* url, ExceptionSink* xsink);
164 
166 
169  DLLEXPORT QoreStringNode* getURL();
170 
172 
176  DLLEXPORT void setUserPassword(const char* user, const char* pass);
177 
179  DLLEXPORT void clearUserPassword();
180 
182 
186  DLLEXPORT int setProxyURL(const char* proxy, ExceptionSink* xsink);
187 
189 
192  DLLEXPORT QoreStringNode* getProxyURL();
193 
195  DLLEXPORT void clearProxyURL();
196 
198 
202  DLLEXPORT void setProxyUserPassword(const char* user, const char* pass);
203 
205  DLLEXPORT void clearProxyUserPassword();
206 
208  DLLEXPORT void setSecure(bool is_secure);
209 
211  DLLEXPORT bool isSecure() const;
212 
214  DLLEXPORT void setProxySecure(bool is_secure);
215 
217  DLLEXPORT bool isProxySecure() const;
218 
220  DLLEXPORT void setMaxRedirects(int max);
221 
223  DLLEXPORT int getMaxRedirects() const;
224 
226 
228  DLLEXPORT int connect(ExceptionSink* xsink);
229 
231  DLLEXPORT void disconnect();
232 
234 
245  DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers, const void* data,
246  unsigned size, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
247 
248  DLLEXPORT QoreHashNode* send(const char* meth, const char* path, const QoreHashNode* headers,
249  const QoreStringNode& body, bool getbody, QoreHashNode* info, ExceptionSink* xsink);
250 
251  DLLEXPORT QoreHashNode* sendWithSendCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
252  const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
253  ExceptionSink* xsink);
254 
255  DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
256  const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
257  const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
258 
259  DLLEXPORT void sendWithRecvCallback(const char* meth, const char* mpath, const QoreHashNode* headers,
260  const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
261  const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
262 
263  DLLEXPORT void sendWithCallbacks(const char* meth, const char* mpath, const QoreHashNode* headers,
264  const ResolvedCallReferenceNode* send_callback, bool getbody, QoreHashNode* info, int timeout_ms,
265  const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, ExceptionSink* xsink);
266 
268 
270  DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
271  const void* data, unsigned size, bool getbody, QoreHashNode* info, int timeout_ms,
272  const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
273 
275 
277  DLLEXPORT void sendWithOutputStream(const char* meth, const char* mpath, const QoreHashNode* headers,
278  const QoreStringNode& body, bool getbody, QoreHashNode* info, int timeout_ms,
279  const ResolvedCallReferenceNode* recv_callback, QoreObject* obj, OutputStream *os, ExceptionSink* xsink);
280 
282 
284  DLLEXPORT void sendChunked(const char* meth, const char* mpath, const QoreHashNode* headers, bool getbody,
285  QoreHashNode* info, int timeout_ms, const ResolvedCallReferenceNode* recv_callback, QoreObject* obj,
286  OutputStream *os, InputStream* is, size_t max_chunk_size, const ResolvedCallReferenceNode* trailer_callback,
287  ExceptionSink* xsink);
288 
290 
298  DLLEXPORT AbstractQoreNode* get(const char* path, const QoreHashNode* headers, QoreHashNode* info, ExceptionSink* xsink);
299 
301 
308  DLLEXPORT QoreHashNode* head(const char* path, const QoreHashNode* headers, QoreHashNode* info, ExceptionSink* xsink);
309 
311 
321  DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const void* data, unsigned size, QoreHashNode* info, ExceptionSink* xsink);
322 
324 
336  DLLEXPORT AbstractQoreNode* post(const char* path, const QoreHashNode* headers, const QoreStringNode& body,
337  QoreHashNode* info, ExceptionSink* xsink);
338 
340 
344  DLLEXPORT void setDefaultHeaderValue(const char* header, const char* val);
345 
348 
351  DLLEXPORT virtual void deref(ExceptionSink* xsink);
352 
354 
357  DLLEXPORT void setConnectTimeout(int ms);
358 
360 
363  DLLEXPORT int getConnectTimeout() const;
364 
366 
375  DLLEXPORT int setNoDelay(bool nodelay);
376 
378  DLLEXPORT void setEventQueue(Queue* cbq, ExceptionSink* xsink);
379 
381  DLLEXPORT void setEventQueue(ExceptionSink* xsink, Queue* q, QoreValue arg, bool with_data);
382 
384  DLLEXPORT bool getNoDelay() const;
385 
387  DLLEXPORT bool isConnected() const;
388 
390  DLLEXPORT void setPersistent(ExceptionSink* xsink);
391 
392  DLLEXPORT void clearWarningQueue(ExceptionSink* xsink);
393  DLLEXPORT void setWarningQueue(ExceptionSink* xsink, int64 warning_ms, int64 warning_bs, Queue* wq, QoreValue arg, int64 min_ms = 1000);
394  DLLEXPORT QoreHashNode* getUsageInfo() const;
395  DLLEXPORT void clearStats();
396 
398 
400  DLLEXPORT bool setErrorPassthru(bool set);
401 
403 
405  DLLEXPORT bool getErrorPassthru() const;
406 
408 
410  DLLEXPORT bool setRedirectPassthru(bool set);
411 
413 
415  DLLEXPORT bool getRedirectPassthru() const;
416 
418 
420  DLLEXPORT bool setEncodingPassthru(bool set);
421 
423 
425  DLLEXPORT bool getEncodingPassthru() const;
426 
428 
430  DLLEXPORT QoreStringNode* getHostHeaderValue() const;
431 
433 
435  DLLEXPORT void setAssumedEncoding(const char* enc);
436 
438 
440  DLLEXPORT QoreStringNode* getAssumedEncoding() const;
441 
442  DLLLOCAL static void static_init();
443 
444  DLLLOCAL void cleanup(ExceptionSink* xsink);
445 };
446 
447 #endif
DLLEXPORT bool setErrorPassthru(bool set)
sets the new and returns the old error_passthru status
DLLEXPORT void clearProxyUserPassword()
clears the username and password for the proxy connection
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 bool getEncodingPassthru() const
returns the current encoding_passthru status
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
DLLEXPORT QoreStringNode * getURL()
returns the connection parameters as a URL, caller owns the reference count returned ...
DLLEXPORT void setPersistent(ExceptionSink *xsink)
temporarily disables implicit reconnections; must be called when the server is already connected ...
DLLEXPORT int connect(ExceptionSink *xsink)
opens a connection and returns a code giving the result
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:50
DLLEXPORT bool isProxySecure() const
returns the SSL proxy connection parameter flag
DLLEXPORT int setOptions(const QoreHashNode *opts, ExceptionSink *xsink)
set options with a hash, returns -1 if an exception was thrown, 0 for OK
DLLEXPORT int getTimeout() const
returns the connection and response packet timeout value in milliseconds
DLLEXPORT int getConnectTimeout() const
returns the connect timeout in ms, negative numbers mean no timeout
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 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 QoreHttpClientObject()
creates the QoreHttpClientObject object
The base class for all value and parse types in Qore expression trees.
Definition: AbstractQoreNode.h:54
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 void setTimeout(int to)
sets the connection and response packet timeout value in milliseconds
DLLEXPORT void setDefaultPath(const char *pth)
sets the default path
DLLEXPORT bool setEncodingPassthru(bool set)
sets the new and returns the old encoding_passthru status
DLLEXPORT int setProxyURL(const char *proxy, ExceptionSink *xsink)
sets the proxy URL
DLLEXPORT const char * getConnectionPath() const
returns the current connection path or 0 if none is set
DLLEXPORT void addProtocol(const char *prot, int port, bool ssl=false)
adds a protocol
Interface for private data of output streams.
Definition: OutputStream.h:44
virtual DLLEXPORT ~QoreHttpClientObject()
destroys the object and frees all associated memory
DLLEXPORT int setURL(const char *url, ExceptionSink *xsink)
sets the connection URL
DLLEXPORT void setDefaultPort(int prt)
sets the default port
Qore&#39;s string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
DLLEXPORT bool isSecure() const
returns the SSL connection parameter flag
DLLEXPORT void setHTTP11(bool h11)
sets or clears HTTP 1.1 protocol compliance
DLLEXPORT void setConnectTimeout(int ms)
sets the connect timeout in ms
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 void setDefaultHeaderValue(const char *header, const char *val)
sets the value of a default header to send with every outgoing message
DLLEXPORT bool getNoDelay() const
returns the value of the TCP_NODELAY flag on the object
DLLEXPORT void setUserPassword(const char *user, const char *pass)
sets the username and password for the connection
virtual DLLLOCAL void deref()
decrements the reference count of the object without the possibility of throwing a Qore-language exce...
Definition: AbstractPrivateData.h:67
DLLEXPORT bool setRedirectPassthru(bool set)
sets the new and returns the old redirect_passthru status
DLLEXPORT int setHTTPVersion(const char *version, ExceptionSink *xsink)
sets the http version from a string
DLLEXPORT void setMaxRedirects(int max)
sets the max_redirects option
DLLEXPORT void setEncoding(const QoreEncoding *qe)
sets the default encoding for the object
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
DLLEXPORT const QoreEncoding * getEncoding() const
returns the default encoding for the object
DLLEXPORT void disconnect()
disconnects from the remote server
the implementation of Qore&#39;s object data type, reference counted, dynamically-allocated only ...
Definition: QoreObject.h:61
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
provides a way to communicate with HTTP servers using Qore data structures
Definition: QoreHttpClientObject.h:51
DLLEXPORT void setProxySecure(bool is_secure)
sets the SSL flag for use in the next connection to the proxy
DLLEXPORT QoreStringNode * getAssumedEncoding() const
gets the assumed encoding
Interface for private data of input streams.
Definition: InputStream.h:44
DLLEXPORT QoreStringNode * getProxyURL()
returns the proxy connection parameters as a URL (or 0 if there is none), caller owns the reference c...
DLLEXPORT int setNoDelay(bool nodelay)
sets the TCP_NODELAY flag on the object
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
DLLEXPORT bool isHTTP11() const
returns true if HTTP 1.1 protocol compliance has been set
DLLEXPORT const char * getDefaultPath() const
returns the default path or 0 if none is set
DLLEXPORT bool isConnected() const
returns the connection status of the object
DLLEXPORT bool getRedirectPassthru() const
returns the current redirect_passthru status
base class for resolved call references
Definition: CallReferenceNode.h:105
DLLEXPORT void setSecure(bool is_secure)
sets the SSL flag for use in the next connection
DLLEXPORT void clearUserPassword()
clears the username and password for the connection
DLLEXPORT void setEventQueue(Queue *cbq, ExceptionSink *xsink)
sets the event queue, must be already referenced before call
DLLEXPORT bool getErrorPassthru() const
returns the current error_passthru status
DLLEXPORT void setProxyUserPassword(const char *user, const char *pass)
sets the username and password for the proxy connection
DLLEXPORT QoreStringNode * getHostHeaderValue() const
returns the Host header value
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, caller owns the QoreH...
DLLEXPORT void setConnectionPath(const char *path)
sets or clears the connection path
DLLEXPORT void clearProxyURL()
clears the proxy URL
DLLEXPORT int getMaxRedirects() const
returns the value of the max_redirects option