http_client.h 6.6 KB

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