httpclient.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /*
  2. * ----------------------------------------------------------------------------
  3. * "THE BEER-WARE LICENSE" (Revision 42):
  4. * Martin d'Allens <martin.dallens@gmail.com> wrote this file. As long as you retain
  5. * this notice you can do whatever you want with this stuff. If we meet some day,
  6. * and you think this stuff is worth it, you can buy me a beer in return.
  7. * ----------------------------------------------------------------------------
  8. */
  9. /*
  10. * FIXME: sprintf->snprintf everywhere.
  11. * FIXME: support null characters in responses.
  12. */
  13. #include "osapi.h"
  14. #include "../libc/c_stdio.h"
  15. #include "user_interface.h"
  16. #include "espconn.h"
  17. #include "mem.h"
  18. #include "limits.h"
  19. #include "httpclient.h"
  20. #include "stdlib.h"
  21. #include "pm/swtimer.h"
  22. #define REDIRECTION_FOLLOW_MAX 20
  23. /* Internal state. */
  24. typedef struct request_args_t {
  25. char * hostname;
  26. int port;
  27. #ifdef CLIENT_SSL_ENABLE
  28. bool secure;
  29. #endif
  30. char * method;
  31. char * path;
  32. char * headers;
  33. char * post_data;
  34. char * buffer;
  35. int buffer_size;
  36. int redirect_follow_count;
  37. int timeout;
  38. os_timer_t timeout_timer;
  39. http_callback_t callback_handle;
  40. } request_args_t;
  41. static char * ICACHE_FLASH_ATTR esp_strdup( const char * str )
  42. {
  43. if ( str == NULL )
  44. {
  45. return(NULL);
  46. }
  47. char * new_str = (char *) os_malloc( os_strlen( str ) + 1 ); /* 1 for null character */
  48. if ( new_str == NULL )
  49. {
  50. HTTPCLIENT_DEBUG( "esp_strdup: malloc error" );
  51. return(NULL);
  52. }
  53. os_strcpy( new_str, str );
  54. return(new_str);
  55. }
  56. static int ICACHE_FLASH_ATTR
  57. esp_isupper( char c )
  58. {
  59. return(c >= 'A' && c <= 'Z');
  60. }
  61. static int ICACHE_FLASH_ATTR
  62. esp_isalpha( char c )
  63. {
  64. return( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
  65. }
  66. static int ICACHE_FLASH_ATTR
  67. esp_isspace( char c )
  68. {
  69. return(c == ' ' || c == '\t' || c == '\n' || c == '\12');
  70. }
  71. static int ICACHE_FLASH_ATTR
  72. esp_isdigit( char c )
  73. {
  74. return(c >= '0' && c <= '9');
  75. }
  76. static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * decode )
  77. {
  78. int i = 0, j = 0;
  79. int decode_size = 0;
  80. char *str = (char *) chunked;
  81. do
  82. {
  83. char * endstr;
  84. /* [chunk-size] */
  85. i = strtoul( str + j, NULL, 16 );
  86. HTTPCLIENT_DEBUG( "Chunk Size:%d", i );
  87. if ( i <= 0 )
  88. break;
  89. /* [chunk-size-end-ptr] */
  90. endstr = (char *) os_strstr( str + j, "\r\n" );
  91. /* [chunk-ext] */
  92. j += endstr - (str + j);
  93. /* [CRLF] */
  94. j += 2;
  95. /* [chunk-data] */
  96. decode_size += i;
  97. os_memcpy( (char *) &decode[decode_size - i], (char *) str + j, i );
  98. j += i;
  99. /* [CRLF] */
  100. j += 2;
  101. }
  102. while ( true );
  103. /*
  104. *
  105. * footer CRLF
  106. *
  107. */
  108. return(j);
  109. }
  110. static void ICACHE_FLASH_ATTR http_receive_callback( void * arg, char * buf, unsigned short len )
  111. {
  112. struct espconn * conn = (struct espconn *) arg;
  113. request_args_t * req = (request_args_t *) conn->reverse;
  114. if ( req->buffer == NULL )
  115. {
  116. return;
  117. }
  118. /* Let's do the equivalent of a realloc(). */
  119. const int new_size = req->buffer_size + len;
  120. char * new_buffer;
  121. if ( new_size > BUFFER_SIZE_MAX || NULL == (new_buffer = (char *) os_malloc( new_size ) ) )
  122. {
  123. HTTPCLIENT_ERR( "Response too long (%d)", new_size );
  124. req->buffer[0] = '\0'; /* Discard the buffer to avoid using an incomplete response. */
  125. #ifdef CLIENT_SSL_ENABLE
  126. if ( req->secure )
  127. espconn_secure_disconnect( conn );
  128. else
  129. #endif
  130. espconn_disconnect( conn );
  131. return; /* The disconnect callback will be called. */
  132. }
  133. os_memcpy( new_buffer, req->buffer, req->buffer_size );
  134. os_memcpy( new_buffer + req->buffer_size - 1 /*overwrite the null character*/, buf, len ); /* Append new data. */
  135. new_buffer[new_size - 1] = '\0'; /* Make sure there is an end of string. */
  136. os_free( req->buffer );
  137. req->buffer = new_buffer;
  138. req->buffer_size = new_size;
  139. }
  140. static void ICACHE_FLASH_ATTR http_send_callback( void * arg )
  141. {
  142. struct espconn * conn = (struct espconn *) arg;
  143. request_args_t * req = (request_args_t *) conn->reverse;
  144. if ( req->post_data == NULL )
  145. {
  146. HTTPCLIENT_DEBUG( "All sent" );
  147. }
  148. else
  149. {
  150. /* The headers were sent, now send the contents. */
  151. HTTPCLIENT_DEBUG( "Sending request body" );
  152. #ifdef CLIENT_SSL_ENABLE
  153. if ( req->secure )
  154. espconn_secure_send( conn, (uint8_t *) req->post_data, strlen( req->post_data ) );
  155. else
  156. #endif
  157. espconn_send( conn, (uint8_t *) req->post_data, strlen( req->post_data ) );
  158. os_free( req->post_data );
  159. req->post_data = NULL;
  160. }
  161. }
  162. static void ICACHE_FLASH_ATTR http_connect_callback( void * arg )
  163. {
  164. HTTPCLIENT_DEBUG( "Connected" );
  165. struct espconn * conn = (struct espconn *) arg;
  166. request_args_t * req = (request_args_t *) conn->reverse;
  167. espconn_regist_recvcb( conn, http_receive_callback );
  168. espconn_regist_sentcb( conn, http_send_callback );
  169. char post_headers[32] = "";
  170. if ( req->post_data != NULL ) /* If there is data then add Content-Length header. */
  171. {
  172. os_sprintf( post_headers, "Content-Length: %d\r\n", strlen( req->post_data ) );
  173. }
  174. if(req->headers == NULL) /* Avoid NULL pointer, it may cause exception */
  175. {
  176. req->headers = (char *)os_malloc(sizeof(char));
  177. req->headers[0] = '\0';
  178. }
  179. char ua_header[32] = "";
  180. int ua_len = 0;
  181. if (strcasestr( req->headers, "User-Agent:" ) == NULL )
  182. {
  183. os_sprintf( ua_header, "User-Agent: %s\r\n", "ESP8266" );
  184. ua_len = strlen(ua_header);
  185. }
  186. char * host_header = "";
  187. int host_len = 0;
  188. if ( strcasestr( req->headers, "Host:" ) == NULL )
  189. {
  190. int max_header_len = 9 + strlen(req->hostname); // 9 is fixed size of "Host:[space][cr][lf]\0"
  191. if ((req->port == 80)
  192. #ifdef CLIENT_SSL_ENABLE
  193. || ((req->port == 443) && ( req->secure ))
  194. #endif
  195. )
  196. {
  197. host_header = alloca(max_header_len);
  198. os_sprintf( host_header, "Host: %s\r\n", req->hostname );
  199. }
  200. else
  201. {
  202. host_header = alloca(max_header_len + 6); // 6 is worst case of ":port" where port is maximum 5 digits
  203. os_sprintf( host_header, "Host: %s:%d\r\n", req->hostname, req->port );
  204. }
  205. host_len = strlen(host_header);
  206. }
  207. char buf[69 + strlen( req->method ) + strlen( req->path ) + host_len +
  208. strlen( req->headers ) + ua_len + strlen( post_headers )];
  209. int len = os_sprintf( buf,
  210. "%s %s HTTP/1.1\r\n"
  211. "%s" // Host (if not provided in the headers from Lua)
  212. "Connection: close\r\n"
  213. "%s" // Headers from Lua (optional)
  214. "%s" // User-Agent (if not provided in the headers from Lua)
  215. "%s" // Content-Length
  216. "\r\n",
  217. req->method, req->path, host_header, req->headers, ua_header, post_headers );
  218. #ifdef CLIENT_SSL_ENABLE
  219. if (req->secure)
  220. {
  221. espconn_secure_send( conn, (uint8_t *) buf, len );
  222. }
  223. else
  224. #endif
  225. {
  226. espconn_send( conn, (uint8_t *) buf, len );
  227. }
  228. if (req->headers != NULL)
  229. {
  230. os_free( req->headers );
  231. }
  232. req->headers = NULL;
  233. HTTPCLIENT_DEBUG( "Sending request header" );
  234. }
  235. static void http_free_req( request_args_t * req)
  236. {
  237. if (req->buffer) {
  238. os_free( req->buffer );
  239. }
  240. if (req->post_data) {
  241. os_free( req->post_data );
  242. }
  243. if (req->headers) {
  244. os_free( req->headers );
  245. }
  246. os_free( req->hostname );
  247. os_free( req->method );
  248. os_free( req->path );
  249. os_free( req );
  250. }
  251. static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
  252. {
  253. HTTPCLIENT_DEBUG( "Disconnected" );
  254. struct espconn *conn = (struct espconn *) arg;
  255. if ( conn == NULL )
  256. {
  257. return;
  258. }
  259. if ( conn->proto.tcp != NULL )
  260. {
  261. os_free( conn->proto.tcp );
  262. }
  263. if ( conn->reverse != NULL )
  264. {
  265. request_args_t * req = (request_args_t *) conn->reverse;
  266. int http_status = -1;
  267. char * body = "";
  268. // Turn off timeout timer
  269. os_timer_disarm( &(req->timeout_timer) );
  270. if ( req->buffer == NULL )
  271. {
  272. HTTPCLIENT_DEBUG( "Buffer probably shouldn't be NULL" );
  273. }
  274. else if ( req->buffer[0] != '\0' )
  275. {
  276. /* FIXME: make sure this is not a partial response, using the Content-Length header. */
  277. const char * version_1_0 = "HTTP/1.0 ";
  278. const char * version_1_1 = "HTTP/1.1 ";
  279. if (( os_strncmp( req->buffer, version_1_0, strlen( version_1_0 ) ) != 0 ) &&
  280. ( os_strncmp( req->buffer, version_1_1, strlen( version_1_1 ) ) != 0 ))
  281. {
  282. HTTPCLIENT_ERR( "Invalid version in %s", req->buffer );
  283. }
  284. else
  285. {
  286. http_status = atoi( req->buffer + strlen( version_1_0 ) );
  287. char *locationOffset = (char *) strcasestr( req->buffer, "Location:" );
  288. if ( locationOffset != NULL && http_status >= 300 && http_status <= 308 ) {
  289. if (req->redirect_follow_count < REDIRECTION_FOLLOW_MAX) {
  290. locationOffset += strlen("location:");
  291. while (*locationOffset == ' ') { // skip url leading white-space
  292. locationOffset++;
  293. }
  294. char *locationOffsetEnd = (char *) os_strstr(locationOffset, "\r\n");
  295. if ( locationOffsetEnd == NULL ) {
  296. HTTPCLIENT_ERR( "Found Location header but was incomplete" );
  297. http_status = -1;
  298. } else {
  299. *locationOffsetEnd = '\0';
  300. req->redirect_follow_count++;
  301. // Check if url is absolute
  302. bool url_has_protocol =
  303. os_strncmp( locationOffset, "http://", strlen( "http://" ) ) == 0 ||
  304. os_strncmp( locationOffset, "https://", strlen( "https://" ) ) == 0;
  305. if ( url_has_protocol ) {
  306. http_request( locationOffset, req->method, req->headers,
  307. req->post_data, req->callback_handle, req->redirect_follow_count );
  308. } else {
  309. if ( os_strncmp( locationOffset, "/", 1 ) == 0) { // relative and full path
  310. http_raw_request( req->hostname, req->port,
  311. #ifdef CLIENT_SSL_ENABLE
  312. req->secure,
  313. #else
  314. 0,
  315. #endif
  316. req->method, locationOffset, req->headers, req->post_data, req->callback_handle, req->redirect_follow_count );
  317. } else { // relative and relative path
  318. // find last /
  319. const char *pathFolderEnd = strrchr(req->path, '/');
  320. int pathFolderLength = pathFolderEnd - req->path;
  321. pathFolderLength++; // use the '/'
  322. int locationLength = strlen(locationOffset);
  323. locationLength++; // use the '\0'
  324. // append pathFolder with given relative path
  325. char *completeRelativePath = (char *) os_malloc(pathFolderLength + locationLength);
  326. os_memcpy( completeRelativePath, req->path, pathFolderLength );
  327. os_memcpy( completeRelativePath + pathFolderLength, locationOffset, locationLength);
  328. http_raw_request( req->hostname, req->port,
  329. #ifdef CLIENT_SSL_ENABLE
  330. req->secure,
  331. #else
  332. 0,
  333. #endif
  334. req->method, completeRelativePath, req->headers, req->post_data, req->callback_handle, req->redirect_follow_count );
  335. os_free( completeRelativePath );
  336. }
  337. }
  338. http_free_req( req );
  339. espconn_delete( conn );
  340. os_free( conn );
  341. return;
  342. }
  343. } else {
  344. HTTPCLIENT_ERR("Too many redirections");
  345. http_status = -1;
  346. }
  347. } else {
  348. body = (char *) os_strstr(req->buffer, "\r\n\r\n");
  349. if (NULL == body) {
  350. /* Find missing body */
  351. HTTPCLIENT_ERR("Body shouldn't be NULL");
  352. /* To avoid NULL body */
  353. body = "";
  354. } else {
  355. /* Skip CR & LF */
  356. body = body + 4;
  357. }
  358. if ( strcasestr( req->buffer, "Transfer-Encoding: chunked" ) )
  359. {
  360. int body_size = req->buffer_size - (body - req->buffer);
  361. char chunked_decode_buffer[body_size];
  362. os_memset( chunked_decode_buffer, 0, body_size );
  363. /* Chuncked data */
  364. http_chunked_decode( body, chunked_decode_buffer );
  365. os_memcpy( body, chunked_decode_buffer, body_size );
  366. }
  367. }
  368. }
  369. }
  370. if ( req->callback_handle != NULL ) /* Callback is optional. */
  371. {
  372. char *req_buffer = req->buffer;
  373. req->buffer = NULL;
  374. http_callback_t req_callback;
  375. req_callback = req->callback_handle;
  376. http_free_req( req );
  377. req_callback( body, http_status, &req_buffer );
  378. if (req_buffer) {
  379. os_free(req_buffer);
  380. }
  381. } else {
  382. http_free_req( req );
  383. }
  384. }
  385. /* Fix memory leak. */
  386. espconn_delete( conn );
  387. os_free( conn );
  388. }
  389. static void ICACHE_FLASH_ATTR http_timeout_callback( void *arg )
  390. {
  391. HTTPCLIENT_ERR( "Connection timeout" );
  392. struct espconn * conn = (struct espconn *) arg;
  393. if ( conn == NULL )
  394. {
  395. HTTPCLIENT_DEBUG( "Connection is NULL" );
  396. return;
  397. }
  398. if ( conn->reverse == NULL )
  399. {
  400. HTTPCLIENT_DEBUG( "Connection request data (reverse) is NULL" );
  401. return;
  402. }
  403. request_args_t * req = (request_args_t *) conn->reverse;
  404. HTTPCLIENT_DEBUG( "Calling disconnect" );
  405. /* Call disconnect */
  406. sint8 result;
  407. #ifdef CLIENT_SSL_ENABLE
  408. if ( req->secure )
  409. result = espconn_secure_disconnect( conn );
  410. else
  411. #endif
  412. result = espconn_disconnect( conn );
  413. if (result == ESPCONN_OK || result == ESPCONN_INPROGRESS)
  414. return;
  415. else
  416. {
  417. /* not connected; execute the callback ourselves. */
  418. HTTPCLIENT_DEBUG( "manually Calling disconnect callback due to error %d", result );
  419. http_disconnect_callback( arg );
  420. }
  421. }
  422. static void ICACHE_FLASH_ATTR http_error_callback( void *arg, sint8 errType )
  423. {
  424. HTTPCLIENT_ERR( "Disconnected with error: %d", errType );
  425. http_timeout_callback( arg );
  426. }
  427. static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_t * addr, void * arg )
  428. {
  429. request_args_t * req = (request_args_t *) arg;
  430. if ( addr == NULL )
  431. {
  432. HTTPCLIENT_ERR( "DNS failed for %s", hostname );
  433. if ( req->callback_handle != NULL )
  434. {
  435. req->callback_handle( "", -1, NULL );
  436. }
  437. http_free_req( req );
  438. }
  439. else
  440. {
  441. HTTPCLIENT_DEBUG( "DNS found %s " IPSTR, hostname, IP2STR( addr ) );
  442. struct espconn * conn = (struct espconn *) os_zalloc( sizeof(struct espconn) );
  443. conn->type = ESPCONN_TCP;
  444. conn->state = ESPCONN_NONE;
  445. conn->proto.tcp = (esp_tcp *) os_zalloc( sizeof(esp_tcp) );
  446. conn->proto.tcp->local_port = espconn_port();
  447. conn->proto.tcp->remote_port = req->port;
  448. conn->reverse = req;
  449. os_memcpy( conn->proto.tcp->remote_ip, addr, 4 );
  450. espconn_regist_connectcb( conn, http_connect_callback );
  451. espconn_regist_disconcb( conn, http_disconnect_callback );
  452. espconn_regist_reconcb( conn, http_error_callback );
  453. /* Set connection timeout timer */
  454. os_timer_disarm( &(req->timeout_timer) );
  455. os_timer_setfn( &(req->timeout_timer), (os_timer_func_t *) http_timeout_callback, conn );
  456. SWTIMER_REG_CB(http_timeout_callback, SWTIMER_IMMEDIATE);
  457. //http_timeout_callback frees memory used by this function and timer cannot be dropped
  458. os_timer_arm( &(req->timeout_timer), req->timeout, false );
  459. #ifdef CLIENT_SSL_ENABLE
  460. if ( req->secure )
  461. {
  462. espconn_secure_connect( conn );
  463. }
  464. else
  465. #endif
  466. {
  467. espconn_connect( conn );
  468. }
  469. }
  470. }
  471. void ICACHE_FLASH_ATTR http_raw_request( const char * hostname, int port, bool secure, const char * method, const char * path, const char * headers, const char * post_data, http_callback_t callback_handle, int redirect_follow_count )
  472. {
  473. HTTPCLIENT_DEBUG( "DNS request" );
  474. request_args_t * req = (request_args_t *) os_zalloc( sizeof(request_args_t) );
  475. req->hostname = esp_strdup( hostname );
  476. req->port = port;
  477. #ifdef CLIENT_SSL_ENABLE
  478. req->secure = secure;
  479. #endif
  480. req->method = esp_strdup( method );
  481. req->path = esp_strdup( path );
  482. req->headers = esp_strdup( headers );
  483. req->post_data = esp_strdup( post_data );
  484. req->buffer_size = 1;
  485. req->buffer = (char *) os_malloc( 1 );
  486. req->buffer[0] = '\0'; /* Empty string. */
  487. req->callback_handle = callback_handle;
  488. req->timeout = HTTP_REQUEST_TIMEOUT_MS;
  489. req->redirect_follow_count = redirect_follow_count;
  490. ip_addr_t addr;
  491. err_t error = espconn_gethostbyname( (struct espconn *) req, /* It seems we don't need a real espconn pointer here. */
  492. hostname, &addr, http_dns_callback );
  493. if ( error == ESPCONN_INPROGRESS )
  494. {
  495. HTTPCLIENT_DEBUG( "DNS pending" );
  496. }
  497. else if ( error == ESPCONN_OK )
  498. {
  499. /* Already in the local names table (or hostname was an IP address), execute the callback ourselves. */
  500. http_dns_callback( hostname, &addr, req );
  501. }
  502. else
  503. {
  504. if ( error == ESPCONN_ARG )
  505. {
  506. HTTPCLIENT_ERR( "DNS arg error %s", hostname );
  507. }else {
  508. HTTPCLIENT_ERR( "DNS error code %d", error );
  509. }
  510. http_dns_callback( hostname, NULL, req ); /* Handle all DNS errors the same way. */
  511. }
  512. }
  513. /*
  514. * Parse an URL of the form http://host:port/path
  515. * <host> can be a hostname or an IP address
  516. * <port> is optional
  517. */
  518. void ICACHE_FLASH_ATTR http_request( const char * url, const char * method, const char * headers, const char * post_data, http_callback_t callback_handle, int redirect_follow_count )
  519. {
  520. /*
  521. * FIXME: handle HTTP auth with http://user:pass@host/
  522. * FIXME: get rid of the #anchor part if present.
  523. */
  524. char hostname[128] = "";
  525. int port = 80;
  526. bool secure = false;
  527. bool is_http = os_strncmp( url, "http://", strlen( "http://" ) ) == 0;
  528. bool is_https = os_strncmp( url, "https://", strlen( "https://" ) ) == 0;
  529. if ( is_http )
  530. url += strlen( "http://" ); /* Get rid of the protocol. */
  531. else if ( is_https )
  532. {
  533. port = 443;
  534. secure = true;
  535. url += strlen( "https://" ); /* Get rid of the protocol. */
  536. }
  537. else
  538. {
  539. HTTPCLIENT_ERR( "URL is not HTTP or HTTPS %s", url );
  540. return;
  541. }
  542. char * path = os_strchr( url, '/' );
  543. if ( path == NULL )
  544. {
  545. path = os_strchr( url, '\0' ); /* Pointer to end of string. */
  546. }
  547. char * colon = os_strchr( url, ':' );
  548. if ( colon > path )
  549. {
  550. colon = NULL; /* Limit the search to characters before the path. */
  551. }
  552. if (path - url >= sizeof(hostname)) {
  553. HTTPCLIENT_ERR( "hostname is too long %s", url );
  554. return;
  555. }
  556. if ( colon == NULL ) /* The port is not present. */
  557. {
  558. os_memcpy( hostname, url, path - url );
  559. hostname[path - url] = '\0';
  560. }
  561. else
  562. {
  563. port = atoi( colon + 1 );
  564. if ( port == 0 )
  565. {
  566. HTTPCLIENT_ERR( "Port error %s", url );
  567. return;
  568. }
  569. os_memcpy( hostname, url, colon - url );
  570. hostname[colon - url] = '\0';
  571. }
  572. if ( path[0] == '\0' ) /* Empty path is not allowed. */
  573. {
  574. path = "/";
  575. }
  576. HTTPCLIENT_DEBUG( "hostname=%s", hostname );
  577. HTTPCLIENT_DEBUG( "port=%d", port );
  578. HTTPCLIENT_DEBUG( "method=%s", method );
  579. HTTPCLIENT_DEBUG( "path=%s", path );
  580. http_raw_request( hostname, port, secure, method, path, headers, post_data, callback_handle, redirect_follow_count);
  581. }
  582. /*
  583. * Parse an URL of the form http://host:port/path
  584. * <host> can be a hostname or an IP address
  585. * <port> is optional
  586. */
  587. void ICACHE_FLASH_ATTR http_post( const char * url, const char * headers, const char * post_data, http_callback_t callback_handle )
  588. {
  589. http_request( url, "POST", headers, post_data, callback_handle, 0 );
  590. }
  591. void ICACHE_FLASH_ATTR http_get( const char * url, const char * headers, http_callback_t callback_handle )
  592. {
  593. http_request( url, "GET", headers, NULL, callback_handle, 0 );
  594. }
  595. void ICACHE_FLASH_ATTR http_delete( const char * url, const char * headers, const char * post_data, http_callback_t callback_handle )
  596. {
  597. http_request( url, "DELETE", headers, post_data, callback_handle, 0 );
  598. }
  599. void ICACHE_FLASH_ATTR http_put( const char * url, const char * headers, const char * post_data, http_callback_t callback_handle )
  600. {
  601. http_request( url, "PUT", headers, post_data, callback_handle, 0 );
  602. }
  603. void ICACHE_FLASH_ATTR http_callback_example( char * response, int http_status, char * full_response )
  604. {
  605. dbg_printf( "http_status=%d\n", http_status );
  606. if ( http_status != HTTP_STATUS_GENERIC_ERROR )
  607. {
  608. dbg_printf( "strlen(full_response)=%d\n", strlen( full_response ) );
  609. dbg_printf( "response=%s<EOF>\n", response );
  610. }
  611. }