Qore Programming Language  1.7.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 username");
95  return -1;
96  }
97 
98  if (!username.empty() && password.empty()) {
99  xsink->raiseException("HTTP-CLIENT-URL-ERROR", "invalid authorization credentials: username set without password");
100  return -1;
101  }
102 
103  if (!port && !host.empty() && host[0] == '/') {
104  is_unix = true;
105  // issue #3474: set URL-encoded path for UNIX domain sockets
106  QoreString tmp_host(host);
107  QoreString tmp_path;
108  tmp_path.concatEncodeUrl(xsink, tmp_host, true);
109  if (*xsink) {
110  return -1;
111  }
112  unix_urlencoded_path = tmp_path.c_str();
113  }
114 
115  return 0;
116  }
117 
118  DLLLOCAL QoreStringNode* get_url(bool suppress_password = false) const {
119  QoreStringNode *pstr = new QoreStringNode("http");
120  if (ssl) {
121  pstr->concat("s://");
122  } else {
123  pstr->concat("://");
124  }
125  if (!username.empty()) {
126  if (suppress_password) {
127  pstr->sprintf("%s@", username.c_str());
128  } else {
129  pstr->sprintf("%s:%s@", username.c_str(), password.c_str());
130  }
131  }
132 
133  if (!port) {
134  // concat and encode "host" when using a UNIX domain socket
135  pstr->concat("socket=");
136  for (unsigned i = 0; i < host.size(); ++i) {
137  char c = host[i];
138  switch (c) {
139  case ' ': pstr->concat("%20"); break;
140  case '/': pstr->concat("%2f"); break;
141  default: pstr->concat(c); break;
142  }
143  }
144  } else {
145  pstr->concat(host.c_str());
146  }
147  if (port && port != 80) {
148  pstr->sprintf(":%d", port);
149  }
150  if (!path.empty()) {
151  if (path[0] != '/')
152  pstr->concat('/');
153  pstr->concat(path.c_str());
154  }
155  return pstr;
156  }
157 
158  DLLLOCAL void setUserPassword(const char *user, const char *pass) {
159  assert(user && pass);
160  username = user;
161  password = pass;
162  }
163 
164  DLLLOCAL void clearUserPassword() {
165  username.clear();
166  password.clear();
167  }
168 
169  DLLLOCAL void clear() {
170  port = 0;
171  username.clear();
172  password.clear();
173  host.clear();
174  path.clear();
175  unix_urlencoded_path.clear();
176  ssl = false;
177  is_unix = false;
178  }
179 };
180 
181 DLLLOCAL extern method_map_t method_map;
182 DLLLOCAL extern strcase_set_t header_ignore;
183 
184 DLLLOCAL void do_content_length_event(Queue *event_queue, int64 id, int len);
185 DLLLOCAL void do_redirect_event(Queue *event_queue, int64 id, const QoreStringNode *loc, const QoreStringNode *msg);
186 DLLLOCAL void do_event(Queue *event_queue, int64 id, int event);
187 DLLLOCAL void check_headers(const char *str, int len, bool &multipart, QoreHashNode &ans, const QoreEncoding *enc,
188  ExceptionSink *xsink);
189 
190 #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