Qore Programming Language 1.19.1
Loading...
Searching...
No Matches
QoreHttpClientObjectIntern.h
1/* -*- mode: c++; indent-tabs-mode: nil -*- */
2/*
3 QoreHttpClientObjectIntern.h
4
5 Qore Programming Language
6
7 Copyright (C) 2006 - 2023 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_INTERN_H_
33#define QORE_HTTP_CLIENT_OBJECT_INTERN_H_
34
35#include <map>
36#include <set>
37
38// ssl-enabled protocols are stored as negative numbers, non-ssl as positive
39#define make_protocol(a, b) ((a) * ((b) ? -1 : 1))
40#define get_port(a) ((a) * (((a) < 0) ? -1 : 1))
41#define get_ssl(a) (((a) < 0) ? true : false)
42
43// protocol map class to recognize user-defined protocols (mostly useful for derived classes)
44typedef std::map<std::string, int> prot_map_t;
45typedef std::map<std::string, bool, ltstrcase> method_map_t;
46typedef std::set<std::string, ltstrcase> strcase_set_t;
47typedef std::map<std::string, std::string> header_map_t;
48
49hashdecl con_info {
50 int port;
51 std::string host,
52 path,
53 unix_urlencoded_path,
54 username,
55 password;
56 bool ssl = false,
57 is_unix = false;
58
59 DLLLOCAL con_info(int n_port = 0) : port(n_port) {
60 }
61
62 DLLLOCAL bool has_url() const {
63 return !host.empty();
64 }
65
66 DLLLOCAL int set_url(QoreURL &url, bool &port_set, ExceptionSink *xsink) {
67 port = 0;
68 if (url.getPort()) {
69 port = url.getPort();
70 port_set = true;
71 }
72
73 host = url.getHost() ? url.getHost()->getBuffer() : "";
74
75 // check if hostname is really a local port number (for a URL string like: "8080")
76 if (!url.getPort() && !host.empty()) {
77 char *aux;
78 int val = strtol(host.c_str(), &aux, 10);
79 if (aux == (host.c_str() + host.size())) {
80 host = HTTPCLIENT_DEFAULT_HOST;
81 port = val;
82 port_set = true;
83 }
84 }
85
86 const QoreString *tmp = url.getPath();
87 path = tmp ? tmp->getBuffer() : "";
88 tmp = url.getUserName();
89 username = tmp ? tmp->getBuffer() : "";
90 tmp = url.getPassword();
91 password = tmp ? tmp->getBuffer() : "";
92
93 if (username.empty() && !password.empty()) {
94 xsink->raiseException("HTTP-CLIENT-URL-ERROR", "invalid authorization credentials: password set without "
95 "username");
96 return -1;
97 }
98
99 if (!username.empty() && password.empty()) {
100 xsink->raiseException("HTTP-CLIENT-URL-ERROR", "invalid authorization credentials: username set without "
101 "password");
102 return -1;
103 }
104
105 if (!port && !host.empty() && host[0] == '/') {
106 is_unix = true;
107 // issue #3474: set URL-encoded path for UNIX domain sockets
108 QoreString tmp_host(host);
109 QoreString tmp_path;
110 tmp_path.concatEncodeUrl(xsink, tmp_host, true);
111 if (*xsink) {
112 return -1;
113 }
114 unix_urlencoded_path = tmp_path.c_str();
115 }
116
117 return 0;
118 }
119
120 DLLLOCAL QoreStringNode* get_url(bool mask_password = false) const {
121 QoreStringNode *pstr = new QoreStringNode("http");
122 if (ssl) {
123 pstr->concat("s://");
124 } else {
125 pstr->concat("://");
126 }
127 bool has_username_or_password = false;
128 if (!username.empty()) {
129 pstr->concat(username);
130 has_username_or_password = true;
131 }
132 if (!password.empty()) {
133 pstr->concat(':');
134 if (mask_password) {
135 pstr->concat("<masked>");
136 } else {
137 pstr->concat(password);
138 }
139 if (!has_username_or_password) {
140 has_username_or_password = true;
141 }
142 }
143 if (has_username_or_password) {
144 pstr->concat('@');
145 }
146
147 if (!port) {
148 // concat and encode "host" when using a UNIX domain socket
149 pstr->concat("socket=");
150 for (unsigned i = 0; i < host.size(); ++i) {
151 char c = host[i];
152 switch (c) {
153 case ' ': pstr->concat("%20"); break;
154 case '/': pstr->concat("%2f"); break;
155 default: pstr->concat(c); break;
156 }
157 }
158 } else {
159 pstr->concat(host.c_str());
160 }
161 if (port && ((!ssl && port != 80) || (ssl && port != 443))) {
162 pstr->sprintf(":%d", port);
163 }
164 if (!path.empty()) {
165 if (path[0] != '/')
166 pstr->concat('/');
167 pstr->concat(path.c_str());
168 }
169 return pstr;
170 }
171
172 DLLLOCAL void setUserPassword(const char *user, const char *pass) {
173 assert(user && pass);
174 username = user;
175 password = pass;
176 }
177
178 DLLLOCAL void clearUserPassword() {
179 username.clear();
180 password.clear();
181 }
182
183 DLLLOCAL void clear() {
184 port = 0;
185 username.clear();
186 password.clear();
187 host.clear();
188 path.clear();
189 unix_urlencoded_path.clear();
190 ssl = false;
191 is_unix = false;
192 }
193};
194
195DLLLOCAL extern method_map_t method_map;
196DLLLOCAL extern strcase_set_t header_ignore;
197
198DLLLOCAL void do_redirect_event(Queue *event_queue, int64 id, const QoreStringNode *loc, const QoreStringNode *msg);
199DLLLOCAL void do_event(Queue *event_queue, int64 id, int event);
200DLLLOCAL void check_headers(const char *str, int len, bool &multipart, QoreHashNode &ans, const QoreEncoding *enc,
201 ExceptionSink *xsink);
202
203#endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:50
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
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:50
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:93
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
DLLEXPORT void concat(const QoreString *str, ExceptionSink *xsink)
concatenates a string and converts encodings if necessary
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
DLLEXPORT int concatEncodeUrl(ExceptionSink *xsink, const QoreString &url, bool encode_all=false)
concatenates a URL-encoded version of the c-string passed
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
helps with parsing URLs and provides access to URL components through Qore data structures
Definition: QoreURL.h:44
DLLEXPORT const QoreString * getHost() const
returns the hostname of the URL
DLLEXPORT const QoreString * getPath() const
returns the path component of the URL or 0 if none given
DLLEXPORT const QoreString * getUserName() const
returns the user name in the URL or 0 if none given
DLLEXPORT int getPort() const
returns the port number given in the URL or 0 if none present
DLLEXPORT const QoreString * getPassword() const
returns the password in the URL or 0 if none given
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