http_request.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #include "http_request.h"
  2. void HTTPRequest::_redirect_request(const String& p_new_url) {
  3. }
  4. Error HTTPRequest::_request() {
  5. print_line("Requesting:\n\tURL: "+url+"\n\tString: "+request_string+"\n\tPort: "+itos(port)+"\n\tSSL: "+itos(use_ssl)+"\n\tValidate SSL: "+itos(validate_ssl));
  6. return client->connect(url,port,use_ssl,validate_ssl);
  7. }
  8. Error HTTPRequest::_parse_url(const String& p_url) {
  9. url=p_url;
  10. use_ssl=false;
  11. request_string="";
  12. port=80;
  13. request_sent=false;
  14. got_response=false;
  15. body_len=-1;
  16. body.resize(0);
  17. downloaded=0;
  18. redirections=0;
  19. print_line("1 url: "+url);
  20. if (url.begins_with("http://")) {
  21. url=url.substr(7,url.length()-7);
  22. print_line("no SSL");
  23. } else if (url.begins_with("https://")) {
  24. url=url.substr(8,url.length()-8);
  25. use_ssl=true;
  26. port=443;
  27. print_line("yes SSL");
  28. } else {
  29. ERR_EXPLAIN("Malformed URL");
  30. ERR_FAIL_V(ERR_INVALID_PARAMETER);
  31. }
  32. print_line("2 url: "+url);
  33. int slash_pos = url.find("/");
  34. if (slash_pos!=-1) {
  35. request_string=url.substr(slash_pos,url.length());
  36. url=url.substr(0,slash_pos);
  37. print_line("request string: "+request_string);
  38. } else {
  39. request_string="/";
  40. print_line("no request");
  41. }
  42. print_line("3 url: "+url);
  43. int colon_pos = url.find(":");
  44. if (colon_pos!=-1) {
  45. port=url.substr(colon_pos+1,url.length()).to_int();
  46. url=url.substr(0,colon_pos);
  47. ERR_FAIL_COND_V(port<1 || port > 65535,ERR_INVALID_PARAMETER);
  48. }
  49. print_line("4 url: "+url);
  50. return OK;
  51. }
  52. Error HTTPRequest::request(const String& p_url, const Vector<String>& p_custom_headers, bool p_ssl_validate_domain) {
  53. ERR_FAIL_COND_V(!is_inside_tree(),ERR_UNCONFIGURED);
  54. if ( requesting ) {
  55. ERR_EXPLAIN("HTTPRequest is processing a request. Wait for completion or cancel it before attempting a new one.");
  56. ERR_FAIL_V(ERR_BUSY);
  57. }
  58. Error err = _parse_url(p_url);
  59. validate_ssl=p_ssl_validate_domain;
  60. bool has_user_agent=false;
  61. bool has_accept=false;
  62. headers=p_custom_headers;
  63. for(int i=0;i<headers.size();i++) {
  64. if (headers[i].findn("user-agent:")==0)
  65. has_user_agent=true;
  66. if (headers[i].findn("Accept:")==0)
  67. has_accept=true;
  68. }
  69. if (!has_user_agent) {
  70. headers.push_back("User-Agent: GodotEngine/"+String(VERSION_MKSTRING)+" ("+OS::get_singleton()->get_name()+")");
  71. }
  72. if (!has_accept) {
  73. headers.push_back("Accept: */*");
  74. }
  75. err = _request();
  76. if (err==OK) {
  77. set_process(true);
  78. requesting=true;
  79. }
  80. return err;
  81. }
  82. void HTTPRequest::cancel_request() {
  83. if (!requesting)
  84. return;
  85. if (!use_threads) {
  86. set_process(false);
  87. }
  88. if (file) {
  89. memdelete(file);
  90. file=NULL;
  91. }
  92. client->close();
  93. body.resize(0);
  94. //downloaded=0;
  95. got_response=false;
  96. response_code=-1;
  97. //body_len=-1;
  98. request_sent=false;
  99. requesting=false;
  100. }
  101. bool HTTPRequest::_handle_response(bool *ret_value) {
  102. if (!client->has_response()) {
  103. call_deferred("emit_signal","request_completed",RESULT_NO_RESPONSE,0,StringArray(),ByteArray());
  104. *ret_value=true;
  105. return true;
  106. }
  107. got_response=true;
  108. response_code=client->get_response_code();
  109. List<String> rheaders;
  110. client->get_response_headers(&rheaders);
  111. response_headers.resize(0);
  112. downloaded=0;
  113. for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
  114. print_line("HEADER: "+E->get());
  115. response_headers.push_back(E->get());
  116. }
  117. if (response_code==301 || response_code==302) {
  118. //redirect
  119. if (max_redirects>=0 && redirections>=max_redirects) {
  120. call_deferred("emit_signal","request_completed",RESULT_REDIRECT_LIMIT_REACHED,response_code,response_headers,ByteArray());
  121. *ret_value=true;
  122. return true;
  123. }
  124. String new_request;
  125. for (List<String>::Element *E=rheaders.front();E;E=E->next()) {
  126. if (E->get().findn("Location: ")!=-1) {
  127. new_request=E->get().substr(9,E->get().length()).strip_edges();
  128. }
  129. }
  130. print_line("NEW LOCATION: "+new_request);
  131. if (new_request!="") {
  132. //process redirect
  133. client->close();
  134. int new_redirs=redirections+1; //because _request() will clear it
  135. Error err;
  136. if (new_request.begins_with("http")) {
  137. //new url, request all again
  138. err=_parse_url(new_request);
  139. } else {
  140. request_string=new_request;
  141. }
  142. err = _request();
  143. print_line("new connection: "+itos(err));
  144. if (err==OK) {
  145. request_sent=false;
  146. got_response=false;
  147. body_len=-1;
  148. body.resize(0);
  149. downloaded=0;
  150. redirections=new_redirs;
  151. *ret_value=false;
  152. return true;
  153. }
  154. }
  155. }
  156. return false;
  157. }
  158. bool HTTPRequest::_update_connection() {
  159. switch( client->get_status() ) {
  160. case HTTPClient::STATUS_DISCONNECTED: {
  161. return true; //end it, since it's doing something
  162. } break;
  163. case HTTPClient::STATUS_RESOLVING: {
  164. client->poll();
  165. //must wait
  166. return false;
  167. } break;
  168. case HTTPClient::STATUS_CANT_RESOLVE: {
  169. call_deferred("emit_signal","request_completed",RESULT_CANT_RESOLVE,0,StringArray(),ByteArray());
  170. return true;
  171. } break;
  172. case HTTPClient::STATUS_CONNECTING: {
  173. client->poll();
  174. //must wait
  175. return false;
  176. } break; //connecting to ip
  177. case HTTPClient::STATUS_CANT_CONNECT: {
  178. call_deferred("emit_signal","request_completed",RESULT_CANT_CONNECT,0,StringArray(),ByteArray());
  179. return true;
  180. } break;
  181. case HTTPClient::STATUS_CONNECTED: {
  182. if (request_sent) {
  183. if (!got_response) {
  184. //no body
  185. bool ret_value;
  186. if (_handle_response(&ret_value))
  187. return ret_value;
  188. call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray());
  189. return true;
  190. }
  191. if (got_response && body_len<0) {
  192. //chunked transfer is done
  193. call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body);
  194. return true;
  195. }
  196. call_deferred("emit_signal","request_completed",RESULT_CHUNKED_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
  197. return true;
  198. //request migh have been done
  199. } else {
  200. //did not request yet, do request
  201. Error err = client->request(HTTPClient::METHOD_GET,request_string,headers);
  202. if (err!=OK) {
  203. call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
  204. return true;
  205. }
  206. request_sent=true;
  207. return false;
  208. }
  209. } break; //connected: { } break requests only accepted here
  210. case HTTPClient::STATUS_REQUESTING: {
  211. //must wait, it's requesting
  212. client->poll();
  213. return false;
  214. } break; // request in progress
  215. case HTTPClient::STATUS_BODY: {
  216. if (!got_response) {
  217. bool ret_value;
  218. if (_handle_response(&ret_value))
  219. return ret_value;
  220. if (!client->is_response_chunked() && client->get_response_body_length()==0) {
  221. call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,ByteArray());
  222. return true;
  223. }
  224. if (client->is_response_chunked()) {
  225. body_len=-1; //no body len because chunked, change your webserver configuration if you want body len
  226. } else {
  227. body_len=client->get_response_body_length();
  228. if (body_size_limit>=0 && body_len>body_size_limit) {
  229. call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
  230. return true;
  231. }
  232. }
  233. if (download_to_file!=String()) {
  234. file=FileAccess::open(download_to_file,FileAccess::WRITE);
  235. if (!file) {
  236. call_deferred("emit_signal","request_completed",RESULT_DOWNLOAD_FILE_CANT_OPEN,response_code,response_headers,ByteArray());
  237. }
  238. }
  239. }
  240. //print_line("BODY: "+itos(body.size()));
  241. client->poll();
  242. ByteArray chunk = client->read_response_body_chunk();
  243. downloaded+=chunk.size();
  244. if (file) {
  245. ByteArray::Read r=chunk.read();
  246. file->store_buffer(r.ptr(),chunk.size());
  247. if (file->get_error()!=OK) {
  248. call_deferred("emit_signal","request_completed",RESULT_DOWNLOAD_FILE_WRITE_ERROR,response_code,response_headers,ByteArray());
  249. return true;
  250. }
  251. } else {
  252. body.append_array(chunk);
  253. }
  254. if (body_size_limit>=0 && downloaded>body_size_limit) {
  255. call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_LIMIT_EXCEEDED,response_code,response_headers,ByteArray());
  256. return true;
  257. }
  258. if (body_len>=0) {
  259. if (downloaded==body_len) {
  260. call_deferred("emit_signal","request_completed",RESULT_SUCCESS,response_code,response_headers,body);
  261. return true;
  262. }
  263. /*if (body.size()>=body_len) {
  264. call_deferred("emit_signal","request_completed",RESULT_BODY_SIZE_MISMATCH,response_code,response_headers,ByteArray());
  265. return true;
  266. }*/
  267. }
  268. return false;
  269. } break; // request resulted in body: { } break which must be read
  270. case HTTPClient::STATUS_CONNECTION_ERROR: {
  271. call_deferred("emit_signal","request_completed",RESULT_CONNECTION_ERROR,0,StringArray(),ByteArray());
  272. return true;
  273. } break;
  274. case HTTPClient::STATUS_SSL_HANDSHAKE_ERROR: {
  275. call_deferred("emit_signal","request_completed",RESULT_SSL_HANDSHAKE_ERROR,0,StringArray(),ByteArray());
  276. return true;
  277. } break;
  278. }
  279. ERR_FAIL_V(false);
  280. }
  281. void HTTPRequest::_notification(int p_what) {
  282. if (p_what==NOTIFICATION_PROCESS) {
  283. bool done = _update_connection();
  284. if (done) {
  285. set_process(false);
  286. cancel_request();
  287. }
  288. }
  289. }
  290. void HTTPRequest::set_use_threads(bool p_use) {
  291. ERR_FAIL_COND( status!=HTTPClient::STATUS_DISCONNECTED );
  292. use_threads=p_use;
  293. }
  294. bool HTTPRequest::is_using_threads() const {
  295. return use_threads;
  296. }
  297. void HTTPRequest::set_body_size_limit(int p_bytes) {
  298. ERR_FAIL_COND( status!=HTTPClient::STATUS_DISCONNECTED );
  299. body_size_limit=p_bytes;
  300. }
  301. int HTTPRequest::get_body_size_limit() const {
  302. return body_size_limit;
  303. }
  304. void HTTPRequest::set_download_file(const String& p_file) {
  305. ERR_FAIL_COND( status!=HTTPClient::STATUS_DISCONNECTED );
  306. download_to_file=p_file;
  307. }
  308. String HTTPRequest::get_download_file() const {
  309. return download_to_file;
  310. }
  311. HTTPClient::Status HTTPRequest::get_http_client_status() const {
  312. return client->get_status();
  313. }
  314. void HTTPRequest::set_max_redirects(int p_max) {
  315. max_redirects=p_max;
  316. }
  317. int HTTPRequest::get_max_redirects() const{
  318. return max_redirects;
  319. }
  320. int HTTPRequest::get_downloaded_bytes() const {
  321. return downloaded;
  322. }
  323. int HTTPRequest::get_body_size() const{
  324. return body_len;
  325. }
  326. void HTTPRequest::_bind_methods() {
  327. ObjectTypeDB::bind_method(_MD("request","url","custom_headers","ssl_validate_domain"),&HTTPRequest::request,DEFVAL(StringArray()),DEFVAL(true));
  328. ObjectTypeDB::bind_method(_MD("cancel_request"),&HTTPRequest::cancel_request);
  329. ObjectTypeDB::bind_method(_MD("get_http_client_status"),&HTTPRequest::get_http_client_status);
  330. ObjectTypeDB::bind_method(_MD("set_use_threads","enable"),&HTTPRequest::set_use_threads);
  331. ObjectTypeDB::bind_method(_MD("is_using_threads"),&HTTPRequest::is_using_threads);
  332. ObjectTypeDB::bind_method(_MD("set_body_size_limit","bytes"),&HTTPRequest::set_body_size_limit);
  333. ObjectTypeDB::bind_method(_MD("get_body_size_limit"),&HTTPRequest::get_body_size_limit);
  334. ObjectTypeDB::bind_method(_MD("set_max_redirects","amount"),&HTTPRequest::set_max_redirects);
  335. ObjectTypeDB::bind_method(_MD("get_max_redirects"),&HTTPRequest::get_max_redirects);
  336. ObjectTypeDB::bind_method(_MD("set_download_file","path"),&HTTPRequest::set_download_file);
  337. ObjectTypeDB::bind_method(_MD("get_download_file"),&HTTPRequest::get_download_file);
  338. ObjectTypeDB::bind_method(_MD("get_downloaded_bytes"),&HTTPRequest::get_downloaded_bytes);
  339. ObjectTypeDB::bind_method(_MD("get_body_size"),&HTTPRequest::get_body_size);
  340. ObjectTypeDB::bind_method(_MD("_redirect_request"),&HTTPRequest::_redirect_request);
  341. ADD_PROPERTY(PropertyInfo(Variant::BOOL,"use_threads"),_SCS("set_use_threads"),_SCS("is_using_threads"));
  342. ADD_PROPERTY(PropertyInfo(Variant::INT,"body_size_limit",PROPERTY_HINT_RANGE,"-1,2000000000"),_SCS("set_body_size_limit"),_SCS("get_body_size_limit"));
  343. ADD_PROPERTY(PropertyInfo(Variant::INT,"max_redirects",PROPERTY_HINT_RANGE,"-1,1024"),_SCS("set_max_redirects"),_SCS("get_max_redirects"));
  344. ADD_SIGNAL(MethodInfo("request_completed",PropertyInfo(Variant::INT,"result"),PropertyInfo(Variant::INT,"response_code"),PropertyInfo(Variant::STRING_ARRAY,"headers"),PropertyInfo(Variant::RAW_ARRAY,"body")));
  345. BIND_CONSTANT( RESULT_SUCCESS );
  346. //BIND_CONSTANT( RESULT_NO_BODY );
  347. BIND_CONSTANT( RESULT_CHUNKED_BODY_SIZE_MISMATCH );
  348. BIND_CONSTANT( RESULT_CANT_CONNECT );
  349. BIND_CONSTANT( RESULT_CANT_RESOLVE );
  350. BIND_CONSTANT( RESULT_CONNECTION_ERROR );
  351. BIND_CONSTANT( RESULT_SSL_HANDSHAKE_ERROR );
  352. BIND_CONSTANT( RESULT_NO_RESPONSE );
  353. BIND_CONSTANT( RESULT_BODY_SIZE_LIMIT_EXCEEDED );
  354. BIND_CONSTANT( RESULT_REQUEST_FAILED );
  355. BIND_CONSTANT( RESULT_REDIRECT_LIMIT_REACHED );
  356. BIND_CONSTANT( RESULT_DOWNLOAD_FILE_WRITE_ERROR );
  357. }
  358. HTTPRequest::HTTPRequest()
  359. {
  360. port=80;
  361. redirections=0;
  362. max_redirects=8;
  363. body_len=-1;
  364. got_response=false;
  365. validate_ssl=false;
  366. use_ssl=false;
  367. response_code=0;
  368. request_sent=false;
  369. client.instance();
  370. use_threads=false;
  371. body_size_limit=-1;
  372. file=NULL;
  373. status=HTTPClient::STATUS_DISCONNECTED;
  374. }
  375. HTTPRequest::~HTTPRequest() {
  376. if (file)
  377. memdelete(file);
  378. }