http_client.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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-2015 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. #include "io/stream_peer.h"
  32. #include "io/stream_peer_tcp.h"
  33. #include "io/ip.h"
  34. #include "reference.h"
  35. class HTTPClient : public Reference {
  36. OBJ_TYPE(HTTPClient,Reference);
  37. public:
  38. enum RespondeCode {
  39. // 1xx informational
  40. RESPONSE_CONTINUE = 100,
  41. RESPONSE_SWITCHING_PROTOCOLS = 101,
  42. RESPONSE_PROCESSING = 102,
  43. // 2xx successful
  44. RESPONSE_OK = 200,
  45. RESPONSE_CREATED = 201,
  46. RESPONSE_ACCEPTED = 202,
  47. RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203,
  48. RESPONSE_NO_CONTENT = 204,
  49. RESPONSE_RESET_CONTENT = 205,
  50. RESPONSE_PARTIAL_CONTENT = 206,
  51. RESPONSE_MULTI_STATUS = 207,
  52. RESPONSE_IM_USED = 226,
  53. // 3xx redirection
  54. RESPONSE_MULTIPLE_CHOICES = 300,
  55. RESPONSE_MOVED_PERMANENTLY = 301,
  56. RESPONSE_FOUND = 302,
  57. RESPONSE_SEE_OTHER = 303,
  58. RESPONSE_NOT_MODIFIED = 304,
  59. RESPONSE_USE_PROXY = 305,
  60. RESPONSE_TEMPORARY_REDIRECT = 307,
  61. // 4xx client error
  62. RESPONSE_BAD_REQUEST = 400,
  63. RESPONSE_UNAUTHORIZED = 401,
  64. RESPONSE_PAYMENT_REQUIRED = 402,
  65. RESPONSE_FORBIDDEN = 403,
  66. RESPONSE_NOT_FOUND = 404,
  67. RESPONSE_METHOD_NOT_ALLOWED = 405,
  68. RESPONSE_NOT_ACCEPTABLE = 406,
  69. RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407,
  70. RESPONSE_REQUEST_TIMEOUT = 408,
  71. RESPONSE_CONFLICT = 409,
  72. RESPONSE_GONE = 410,
  73. RESPONSE_LENGTH_REQUIRED = 411,
  74. RESPONSE_PRECONDITION_FAILED = 412,
  75. RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413,
  76. RESPONSE_REQUEST_URI_TOO_LONG = 414,
  77. RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415,
  78. RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416,
  79. RESPONSE_EXPECTATION_FAILED = 417,
  80. RESPONSE_UNPROCESSABLE_ENTITY = 422,
  81. RESPONSE_LOCKED = 423,
  82. RESPONSE_FAILED_DEPENDENCY = 424,
  83. RESPONSE_UPGRADE_REQUIRED = 426,
  84. // 5xx server error
  85. RESPONSE_INTERNAL_SERVER_ERROR = 500,
  86. RESPONSE_NOT_IMPLEMENTED = 501,
  87. RESPONSE_BAD_GATEWAY = 502,
  88. RESPONSE_SERVICE_UNAVAILABLE = 503,
  89. RESPONSE_GATEWAY_TIMEOUT = 504,
  90. RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505,
  91. RESPONSE_INSUFFICIENT_STORAGE = 507,
  92. RESPONSE_NOT_EXTENDED = 510,
  93. };
  94. enum Method {
  95. METHOD_GET,
  96. METHOD_HEAD,
  97. METHOD_POST,
  98. METHOD_PUT,
  99. METHOD_DELETE,
  100. METHOD_OPTIONS,
  101. METHOD_TRACE,
  102. METHOD_CONNECT,
  103. METHOD_MAX
  104. };
  105. enum Status {
  106. STATUS_DISCONNECTED,
  107. STATUS_RESOLVING, //resolving hostname (if passed a hostname)
  108. STATUS_CANT_RESOLVE,
  109. STATUS_CONNECTING, //connecting to ip
  110. STATUS_CANT_CONNECT,
  111. STATUS_CONNECTED, //connected, requests only accepted here
  112. STATUS_REQUESTING, // request in progress
  113. STATUS_BODY, // request resulted in body, which must be read
  114. STATUS_CONNECTION_ERROR,
  115. STATUS_SSL_HANDSHAKE_ERROR,
  116. };
  117. private:
  118. Status status;
  119. IP::ResolverID resolving;
  120. int conn_port;
  121. String conn_host;
  122. bool ssl;
  123. bool ssl_verify_host;
  124. bool blocking;
  125. Vector<uint8_t> response_str;
  126. bool chunked;
  127. Vector<uint8_t> chunk;
  128. int chunk_left;
  129. int body_size;
  130. int body_left;
  131. Ref<StreamPeerTCP> tcp_connection;
  132. Ref<StreamPeer> connection;
  133. int response_num;
  134. Vector<String> response_headers;
  135. static void _bind_methods();
  136. StringArray _get_response_headers();
  137. Dictionary _get_response_headers_as_dictionary();
  138. int read_chunk_size;
  139. Error _get_http_data(uint8_t* p_buffer, int p_bytes,int &r_received);
  140. public:
  141. Error connect_url(const String& p_url); //connects to a full url and perform request
  142. Error connect(const String &p_host,int p_port,bool p_ssl=false,bool p_verify_host=true);
  143. void set_connection(const Ref<StreamPeer>& p_connection);
  144. Error request( Method p_method, const String& p_url, const Vector<String>& p_headers,const String& p_body=String());
  145. Error send_body_text(const String& p_body);
  146. Error send_body_data(const ByteArray& p_body);
  147. void close();
  148. Status get_status() const;
  149. bool has_response() const;
  150. bool is_response_chunked() const;
  151. int get_response_code() const;
  152. Error get_response_headers(List<String> *r_response);
  153. int get_response_body_length() const;
  154. ByteArray read_response_body_chunk(); // can't get body as partial text because of most encodings UTF8, gzip, etc.
  155. void set_blocking_mode(bool p_enable); //useful mostly if running in a thread
  156. bool is_blocking_mode_enabled() const;
  157. void set_read_chunk_size(int p_size);
  158. Error poll();
  159. HTTPClient();
  160. ~HTTPClient();
  161. };
  162. VARIANT_ENUM_CAST(HTTPClient::Method);
  163. #endif // HTTP_CLIENT_H