Qore Programming Language  1.12.0
QoreHttpClientObjectIntern.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreHttpClientObjectIntern.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_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)
44 typedef std::map<std::string, int> prot_map_t;
45 typedef std::map<std::string, bool, ltstrcase> method_map_t;
46 typedef std::set<std::string, ltstrcase> strcase_set_t;
47 typedef std::map<std::string, std::string> header_map_t;
48 
49 hashdecl 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 suppress_password = false) const {
121  QoreStringNode *pstr = new QoreStringNode("http");
122  if (ssl) {
123  pstr->concat("s://");
124  } else {
125  pstr->concat("://");
126  }
127  if (!username.empty()) {
128  if (suppress_password) {
129  pstr->sprintf("%s@", username.c_str());
130  } else {
131  pstr->sprintf("%s:%s@", username.c_str(), password.c_str());
132  }
133  }
134 
135  if (!port) {
136  // concat and encode "host" when using a UNIX domain socket
137  pstr->concat("socket=");
138  for (unsigned i = 0; i < host.size(); ++i) {
139  char c = host[i];
140  switch (c) {
141  case ' ': pstr->concat("%20"); break;
142  case '/': pstr->concat("%2f"); break;
143  default: pstr->concat(c); break;
144  }
145  }
146  } else {
147  pstr->concat(host.c_str());
148  }
149  if (port && port != 80) {
150  pstr->sprintf(":%d", port);
151  }
152  if (!path.empty()) {
153  if (path[0] != '/')
154  pstr->concat('/');
155  pstr->concat(path.c_str());
156  }
157  return pstr;
158  }
159 
160  DLLLOCAL void setUserPassword(const char *user, const char *pass) {
161  assert(user && pass);
162  username = user;
163  password = pass;
164  }
165 
166  DLLLOCAL void clearUserPassword() {
167  username.clear();
168  password.clear();
169  }
170 
171  DLLLOCAL void clear() {
172  port = 0;
173  username.clear();
174  password.clear();
175  host.clear();
176  path.clear();
177  unix_urlencoded_path.clear();
178  ssl = false;
179  is_unix = false;
180  }
181 };
182 
183 DLLLOCAL extern method_map_t method_map;
184 DLLLOCAL extern strcase_set_t header_ignore;
185 
186 DLLLOCAL void do_redirect_event(Queue *event_queue, int64 id, const QoreStringNode *loc, const QoreStringNode *msg);
187 DLLLOCAL void do_event(Queue *event_queue, int64 id, int event);
188 DLLLOCAL void check_headers(const char *str, int len, bool &multipart, QoreHashNode &ans, const QoreEncoding *enc,
189  ExceptionSink *xsink);
190 
191 #endif
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
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 void concat(const QoreString *str, ExceptionSink *xsink)
concatenates a string and converts encodings if necessary
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
DLLEXPORT const char * c_str() const
returns the string's buffer; this data should not be changed
DLLEXPORT const char * getBuffer() const
returns the string's buffer; this data should not be changed
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 * getPassword() const
returns the password in the URL or 0 if none given
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
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