http_client.h
1 /*************************************************************************/
2 /* http_client.h */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* http://www.godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
9 /* */
10 /* Permission is hereby granted, free of charge, to any person obtaining */
11 /* a copy of this software and associated documentation files (the */
12 /* "Software"), to deal in the Software without restriction, including */
13 /* without limitation the rights to use, copy, modify, merge, publish, */
14 /* distribute, sublicense, and/or sell copies of the Software, and to */
15 /* permit persons to whom the Software is furnished to do so, subject to */
16 /* the following conditions: */
17 /* */
18 /* The above copyright notice and this permission notice shall be */
19 /* included in all copies or substantial portions of the Software. */
20 /* */
21 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
22 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
23 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
24 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
25 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
26 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
27 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
28 /*************************************************************************/
29 #ifndef HTTP_CLIENT_H
30 #define HTTP_CLIENT_H
31 
32 #include "io/stream_peer.h"
33 #include "io/stream_peer_tcp.h"
34 #include "io/ip.h"
35 #include "reference.h"
36 
37 
38 class HTTPClient : public Reference {
39 
40  OBJ_TYPE(HTTPClient,Reference);
41 public:
42 
43  enum ResponseCode {
44 
45  // 1xx informational
46  RESPONSE_CONTINUE = 100,
47  RESPONSE_SWITCHING_PROTOCOLS = 101,
48  RESPONSE_PROCESSING = 102,
49 
50  // 2xx successful
51  RESPONSE_OK = 200,
52  RESPONSE_CREATED = 201,
53  RESPONSE_ACCEPTED = 202,
54  RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203,
55  RESPONSE_NO_CONTENT = 204,
56  RESPONSE_RESET_CONTENT = 205,
57  RESPONSE_PARTIAL_CONTENT = 206,
58  RESPONSE_MULTI_STATUS = 207,
59  RESPONSE_IM_USED = 226,
60 
61  // 3xx redirection
62  RESPONSE_MULTIPLE_CHOICES = 300,
63  RESPONSE_MOVED_PERMANENTLY = 301,
64  RESPONSE_FOUND = 302,
65  RESPONSE_SEE_OTHER = 303,
66  RESPONSE_NOT_MODIFIED = 304,
67  RESPONSE_USE_PROXY = 305,
68  RESPONSE_TEMPORARY_REDIRECT = 307,
69 
70  // 4xx client error
71  RESPONSE_BAD_REQUEST = 400,
72  RESPONSE_UNAUTHORIZED = 401,
73  RESPONSE_PAYMENT_REQUIRED = 402,
74  RESPONSE_FORBIDDEN = 403,
75  RESPONSE_NOT_FOUND = 404,
76  RESPONSE_METHOD_NOT_ALLOWED = 405,
77  RESPONSE_NOT_ACCEPTABLE = 406,
78  RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407,
79  RESPONSE_REQUEST_TIMEOUT = 408,
80  RESPONSE_CONFLICT = 409,
81  RESPONSE_GONE = 410,
82  RESPONSE_LENGTH_REQUIRED = 411,
83  RESPONSE_PRECONDITION_FAILED = 412,
84  RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413,
85  RESPONSE_REQUEST_URI_TOO_LONG = 414,
86  RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415,
87  RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
88  RESPONSE_EXPECTATION_FAILED = 417,
89  RESPONSE_UNPROCESSABLE_ENTITY = 422,
90  RESPONSE_LOCKED = 423,
91  RESPONSE_FAILED_DEPENDENCY = 424,
92  RESPONSE_UPGRADE_REQUIRED = 426,
93 
94  // 5xx server error
95  RESPONSE_INTERNAL_SERVER_ERROR = 500,
96  RESPONSE_NOT_IMPLEMENTED = 501,
97  RESPONSE_BAD_GATEWAY = 502,
98  RESPONSE_SERVICE_UNAVAILABLE = 503,
99  RESPONSE_GATEWAY_TIMEOUT = 504,
100  RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505,
101  RESPONSE_INSUFFICIENT_STORAGE = 507,
102  RESPONSE_NOT_EXTENDED = 510,
103 
104  };
105 
106  enum Method {
107 
108  METHOD_GET,
109  METHOD_HEAD,
110  METHOD_POST,
111  METHOD_PUT,
112  METHOD_DELETE,
113  METHOD_OPTIONS,
114  METHOD_TRACE,
115  METHOD_CONNECT,
116  METHOD_MAX
117  };
118 
119  enum Status {
120  STATUS_DISCONNECTED,
121  STATUS_RESOLVING, //resolving hostname (if passed a hostname)
122  STATUS_CANT_RESOLVE,
123  STATUS_CONNECTING, //connecting to ip
124  STATUS_CANT_CONNECT,
125  STATUS_CONNECTED, //connected, requests only accepted here
126  STATUS_REQUESTING, // request in progress
127  STATUS_BODY, // request resulted in body, which must be read
128  STATUS_CONNECTION_ERROR,
129  STATUS_SSL_HANDSHAKE_ERROR,
130 
131  };
132 
133 private:
134 
135  Status status;
136  IP::ResolverID resolving;
137  int conn_port;
138  String conn_host;
139  bool ssl;
140  bool ssl_verify_host;
141  bool blocking;
142 
143  Vector<uint8_t> response_str;
144 
145  bool chunked;
146  Vector<uint8_t> chunk;
147  int chunk_left;
148  int body_size;
149  int body_left;
150 
151  Ref<StreamPeerTCP> tcp_connection;
152  Ref<StreamPeer> connection;
153 
154  int response_num;
155  Vector<String> response_headers;
156 
157  static void _bind_methods();
158  StringArray _get_response_headers();
159  Dictionary _get_response_headers_as_dictionary();
160  int read_chunk_size;
161 
162  Error _get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received);
163 
164 public:
165 
166 
167  Error connect_url(const String& p_url); //connects to a full url and perform request
168  Error connect(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
169 
170  void set_connection(const Ref<StreamPeer>& p_connection);
171 
172  Error request( Method p_method, const String& p_url, const Vector<String>& p_headers,const String& p_body=String());
173  Error send_body_text(const String& p_body);
174  Error send_body_data(const ByteArray& p_body);
175 
176  void close();
177 
178  Status get_status() const;
179 
180  bool has_response() const;
181  bool is_response_chunked() const;
182  int get_response_code() const;
183  Error get_response_headers(List<String> *r_response);
184  int get_response_body_length() const;
185 
186  ByteArray read_response_body_chunk(); // can't get body as partial text because of most encodings UTF8, gzip, etc.
187 
188  void set_blocking_mode(bool p_enable); //useful mostly if running in a thread
189  bool is_blocking_mode_enabled() const;
190 
191  void set_read_chunk_size(int p_size);
192 
193  Error poll();
194 
195  String query_string_from_dict(const Dictionary& p_dict);
196 
197  HTTPClient();
198  ~HTTPClient();
199 };
200 
201 VARIANT_ENUM_CAST(HTTPClient::Method);
202 
203 #endif // HTTP_CLIENT_H
Definition: reference.h:40
Definition: dictionary.h:42
Definition: http_client.h:38
Definition: dvector.h:43
Definition: ustring.h:64