Qore Programming Language  0.9.16
QoreHttpClientObjectIntern.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  QoreHttpClientObjectIntern.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_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() const {
119  QoreStringNode *pstr = new QoreStringNode("http");
120  if (ssl)
121  pstr->concat("s://");
122  else
123  pstr->concat("://");
124  if (!username.empty())
125  pstr->sprintf("%s:%s@", username.c_str(), password.c_str());
126 
127  if (!port) {
128  // concat and encode "host" when using a UNIX domain socket
129  pstr->concat("socket=");
130  for (unsigned i = 0; i < host.size(); ++i) {
131  char c = host[i];
132  switch (c) {
133  case ' ': pstr->concat("%20"); break;
134  case '/': pstr->concat("%2f"); break;
135  default: pstr->concat(c); break;
136  }
137  }
138  }
139  else
140  pstr->concat(host.c_str());
141  if (port && port != 80)
142  pstr->sprintf(":%d", port);
143  if (!path.empty()) {
144  if (path[0] != '/')
145  pstr->concat('/');
146  pstr->concat(path.c_str());
147  }
148  return pstr;
149  }
150 
151  DLLLOCAL void setUserPassword(const char *user, const char *pass) {
152  assert(user && pass);
153  username = user;
154  password = pass;
155  }
156 
157  DLLLOCAL void clearUserPassword() {
158  username.clear();
159  password.clear();
160  }
161 
162  DLLLOCAL void clear() {
163  port = 0;
164  username.clear();
165  password.clear();
166  host.clear();
167  path.clear();
168  unix_urlencoded_path.clear();
169  ssl = false;
170  is_unix = false;
171  }
172 };
173 
174 DLLLOCAL extern method_map_t method_map;
175 DLLLOCAL extern strcase_set_t header_ignore;
176 
177 DLLLOCAL void do_content_length_event(Queue *event_queue, int64 id, int len);
178 DLLLOCAL void do_redirect_event(Queue *event_queue, int64 id, const QoreStringNode *loc, const QoreStringNode *msg);
179 DLLLOCAL void do_event(Queue *event_queue, int64 id, int event);
180 DLLLOCAL void check_headers(const char *str, int len, bool &multipart, QoreHashNode &ans, const QoreEncoding *enc, ExceptionSink *xsink);
181 
182 #endif
QoreURL::getUserName
const DLLEXPORT QoreString * getUserName() const
returns the user name in the URL or 0 if none given
QoreURL::getPort
DLLEXPORT int getPort() const
returns the port number given in the URL or 0 if none present
QoreHashNode
This is the hash or associative list container type in Qore, dynamically allocated only,...
Definition: QoreHashNode.h:50
int64
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
QoreString::sprintf
DLLEXPORT int sprintf(const char *fmt,...)
this will concatentate a formatted string to the existing string according to the format string and t...
QoreString
Qore's string type supported by the QoreEncoding class.
Definition: QoreString.h:81
QoreString::c_str
const DLLEXPORT char * c_str() const
returns the string's buffer; this data should not be changed
ExceptionSink::raiseException
DLLEXPORT AbstractQoreNode * raiseException(const char *err, const char *fmt,...)
appends a Qore-language exception to the list
QoreString::concatEncodeUrl
DLLEXPORT int concatEncodeUrl(ExceptionSink *xsink, const QoreString &url, bool encode_all=false)
concatenates a URL-encoded version of the c-string passed
QoreString::getBuffer
const DLLEXPORT char * getBuffer() const
returns the string's buffer; this data should not be changed
QoreString::concat
DLLEXPORT void concat(const QoreString *str, ExceptionSink *xsink)
concatenates a string and converts encodings if necessary
ExceptionSink
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:48
QoreURL::getPassword
const DLLEXPORT QoreString * getPassword() const
returns the password in the URL or 0 if none given
QoreURL::getHost
const DLLEXPORT QoreString * getHost() const
returns the hostname of the URL
QoreEncoding
defines string encoding functions in Qore
Definition: QoreEncoding.h:83
QoreStringNode
Qore's string value type, reference counted, dynamically-allocated only.
Definition: QoreStringNode.h:50
QoreURL
helps with parsing URLs and provides access to URL components through Qore data structures
Definition: QoreURL.h:39
QoreURL::getPath
const DLLEXPORT QoreString * getPath() const
returns the path component of the URL or 0 if none given