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. #define REDIRECTION_FOLLOW_MAX 20
  22. /* Internal state. */
  23. typedef struct request_args_t {
  24. char * hostname;
  25. int port;
  26. #ifdef CLIENT_SSL_ENABLE
  27. bool secure;
  28. #endif
  29. char * method;
  30. char * path;
  31. char * headers;
  32. char * post_data;
  33. char * buffer;
  34. int buffer_size;
  35. int redirect_follow_count;
  36. int timeout;
  37. os_timer_t timeout_timer;
  38. http_callback_t callback_handle;
  39. } request_args_t;
  40. static char * ICACHE_FLASH_ATTR esp_strdup( const char * str )
  41. {
  42. if ( str == NULL )
  43. {
  44. return(NULL);
  45. }
  46. char * new_str = (char *) os_malloc( os_strlen( str ) + 1 ); /* 1 for null character */
  47. if ( new_str == NULL )
  48. {
  49. HTTPCLIENT_DEBUG( "esp_strdup: malloc error" );
  50. return(NULL);
  51. }
  52. os_strcpy( new_str, str );
  53. return(new_str);
  54. }
  55. static int ICACHE_FLASH_ATTR
  56. esp_isupper( char c )
  57. {
  58. return(c >= 'A' && c <= 'Z');
  59. }
  60. static int ICACHE_FLASH_ATTR
  61. esp_isalpha( char c )
  62. {
  63. return( (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') );
  64. }
  65. static int ICACHE_FLASH_ATTR
  66. esp_isspace( char c )
  67. {
  68. return(c == ' ' || c == '\t' || c == '\n' || c == '\12');
  69. }
  70. static int ICACHE_FLASH_ATTR
  71. esp_isdigit( char c )
  72. {
  73. return(c >= '0' && c <= '9');
  74. }
  75. static int ICACHE_FLASH_ATTR http_chunked_decode( const char * chunked, char * decode )
  76. {
  77. int i = 0, j = 0;
  78. int decode_size = 0;
  79. char *str = (char *) chunked;
  80. do
  81. {
  82. char * endstr;
  83. /* [chunk-size] */
  84. i = strtoul( str + j, NULL, 16 );
  85. HTTPCLIENT_DEBUG( "Chunk Size:%d", i );
  86. if ( i <= 0 )
  87. break;
  88. /* [chunk-size-end-ptr] */
  89. endstr = (char *) os_strstr( str + j, "\r\n" );
  90. /* [chunk-ext] */
  91. j += endstr - (str + j);
  92. /* [CRLF] */
  93. j += 2;
  94. /* [chunk-data] */
  95. decode_size += i;
  96. os_memcpy( (char *) &decode[decode_size - i], (char *) str + j, i );
  97. j += i;
  98. /* [CRLF] */
  99. j += 2;
  100. }
  101. while ( true );
  102. /*
  103. *
  104. * footer CRLF
  105. *
  106. */
  107. return(j);
  108. }
  109. static void ICACHE_FLASH_ATTR http_receive_callback( void * arg, char * buf, unsigned short len )
  110. {
  111. struct espconn * conn = (struct espconn *) arg;
  112. request_args_t * req = (request_args_t *) conn->reverse;
  113. if ( req->buffer == NULL )
  114. {
  115. return;
  116. }
  117. /* Let's do the equivalent of a realloc(). */
  118. const int new_size = req->buffer_size + len;
  119. char * new_buffer;
  120. if ( new_size > BUFFER_SIZE_MAX || NULL == (new_buffer = (char *) os_malloc( new_size ) ) )
  121. {
  122. HTTPCLIENT_ERR( "Response too long (%d)", new_size );
  123. req->buffer[0] = '\0'; /* Discard the buffer to avoid using an incomplete response. */
  124. #ifdef CLIENT_SSL_ENABLE
  125. if ( req->secure )
  126. espconn_secure_disconnect( conn );
  127. else
  128. #endif
  129. espconn_disconnect( conn );
  130. return; /* The disconnect callback will be called. */
  131. }
  132. os_memcpy( new_buffer, req->buffer, req->buffer_size );
  133. os_memcpy( new_buffer + req->buffer_size - 1 /*overwrite the null character*/, buf, len ); /* Append new data. */
  134. new_buffer[new_size - 1] = '\0'; /* Make sure there is an end of string. */
  135. os_free( req->buffer );
  136. req->buffer = new_buffer;
  137. req->buffer_size = new_size;
  138. }
  139. static void ICACHE_FLASH_ATTR http_send_callback( void * arg )
  140. {
  141. struct espconn * conn = (struct espconn *) arg;
  142. request_args_t * req = (request_args_t *) conn->reverse;
  143. if ( req->post_data == NULL )
  144. {
  145. HTTPCLIENT_DEBUG( "All sent" );
  146. }
  147. else
  148. {
  149. /* The headers were sent, now send the contents. */
  150. HTTPCLIENT_DEBUG( "Sending request body" );
  151. #ifdef CLIENT_SSL_ENABLE
  152. if ( req->secure )
  153. espconn_secure_send( conn, (uint8_t *) req->post_data, strlen( req->post_data ) );
  154. else
  155. #endif
  156. espconn_send( conn, (uint8_t *) req->post_data, strlen( req->post_data ) );
  157. os_free( req->post_data );
  158. req->post_data = NULL;
  159. }
  160. }
  161. static void ICACHE_FLASH_ATTR http_connect_callback( void * arg )
  162. {
  163. HTTPCLIENT_DEBUG( "Connected" );
  164. struct espconn * conn = (struct espconn *) arg;
  165. request_args_t * req = (request_args_t *) conn->reverse;
  166. espconn_regist_recvcb( conn, http_receive_callback );
  167. espconn_regist_sentcb( conn, http_send_callback );
  168. char post_headers[32] = "";
  169. if ( req->post_data != NULL ) /* If there is data then add Content-Length header. */
  170. {
  171. os_sprintf( post_headers, "Content-Length: %d\r\n", strlen( req->post_data ) );
  172. }
  173. if(req->headers == NULL) /* Avoid NULL pointer, it may cause exception */
  174. {
  175. req->headers = (char *)os_malloc(sizeof(char));
  176. req->headers[0] = '\0';
  177. }
  178. char ua_header[32] = "";
  179. int ua_len = 0;
  180. if (os_strstr( req->headers, "User-Agent:" ) == NULL && os_strstr( req->headers, "user-agent:" ) == NULL)
  181. {
  182. os_sprintf( ua_header, "User-Agent: %s\r\n", "ESP8266" );
  183. ua_len = strlen(ua_header);
  184. }
  185. char * host_header = "";
  186. int host_len = 0;
  187. if ( os_strstr( req->headers, "Host:" ) == NULL && os_strstr( req->headers, "host:" ) == NULL)
  188. {
  189. int max_header_len = 9 + strlen(req->hostname); // 9 is fixed size of "Host:[space][cr][lf]\0"
  190. if ((req->port == 80)
  191. #ifdef CLIENT_SSL_ENABLE
  192. || ((req->port == 443) && ( req->secure ))
  193. #endif
  194. )
  195. {
  196. host_header = alloca(max_header_len);
  197. os_sprintf( host_header, "Host: %s\r\n", req->hostname );
  198. }
  199. else
  200. {
  201. host_header = alloca(max_header_len + 6); // 6 is worst case of ":port" where port is maximum 5 digits
  202. os_sprintf( host_header, "Host: %s:%d\r\n", req->hostname, req->port );
  203. }
  204. host_len = strlen(host_header);
  205. }
  206. char buf[69 + strlen( req->method ) + strlen( req->path ) + host_len +
  207. strlen( req->headers ) + ua_len + strlen( post_headers )];
  208. int len = os_sprintf( buf,
  209. "%s %s HTTP/1.1\r\n"
  210. "%s" // Host (if not provided in the headers from Lua)
  211. "Connection: close\r\n"
  212. "%s" // Headers from Lua (optional)
  213. "%s" // User-Agent (if not provided in the headers from Lua)
  214. "%s" // Content-Length
  215. "\r\n",
  216. req->method, req->path, host_header, req->headers, ua_header, post_headers );
  217. #ifdef CLIENT_SSL_ENABLE
  218. if (req->secure)
  219. {
  220. espconn_secure_send( conn, (uint8_t *) buf, len );
  221. }
  222. else
  223. #endif
  224. {
  225. espconn_send( conn, (uint8_t *) buf, len );
  226. }
  227. if (req->headers != NULL)
  228. {
  229. os_free( req->headers );
  230. }
  231. req->headers = NULL;
  232. HTTPCLIENT_DEBUG( "Sending request header" );
  233. }
  234. static void http_free_req( request_args_t * req)
  235. {
  236. if (req->buffer) {
  237. os_free( req->buffer );
  238. }
  239. if (req->post_data) {
  240. os_free( req->post_data );
  241. }
  242. if (req->headers) {
  243. os_free( req->headers );
  244. }
  245. os_free( req->hostname );
  246. os_free( req->method );
  247. os_free( req->path );
  248. os_free( req );
  249. }
  250. static void ICACHE_FLASH_ATTR http_disconnect_callback( void * arg )
  251. {
  252. HTTPCLIENT_DEBUG( "Disconnected" );
  253. struct espconn *conn = (struct espconn *) arg;
  254. if ( conn == NULL )
  255. {
  256. return;
  257. }
  258. if ( conn->proto.tcp != NULL )
  259. {
  260. os_free( conn->proto.tcp );
  261. }
  262. if ( conn->reverse != NULL )
  263. {
  264. request_args_t * req = (request_args_t *) conn->reverse;
  265. int http_status = -1;
  266. char * body = "";
  267. // Turn off timeout timer
  268. os_timer_disarm( &(req->timeout_timer) );
  269. if ( req->buffer == NULL )
  270. {
  271. HTTPCLIENT_DEBUG( "Buffer probably shouldn't be NULL" );
  272. }
  273. else if ( req->buffer[0] != '\0' )
  274. {
  275. /* FIXME: make sure this is not a partial response, using the Content-Length header. */
  276. const char * version_1_0 = "HTTP/1.0 ";
  277. const char * version_1_1 = "HTTP/1.1 ";
  278. if (( os_strncmp( req->buffer, version_1_0, strlen( version_1_0 ) ) != 0 ) &&
  279. ( os_strncmp( req->buffer, version_1_1, strlen( version_1_1 ) ) != 0 ))
  280. {
  281. HTTPCLIENT_ERR( "Invalid version in %s", req->buffer );
  282. }
  283. else
  284. {
  285. http_status = atoi( req->buffer + strlen( version_1_0 ) );
  286. char *locationOffset = (char *) os_strstr( req->buffer, "Location:" );
  287. if ( locationOffset == NULL ) {
  288. locationOffset = (char *) os_strstr( req->buffer, "location:" );
  289. }
  290. if ( locationOffset != NULL && http_status >= 300 && http_status <= 308 ) {
  291. if (req->redirect_follow_count < REDIRECTION_FOLLOW_MAX) {
  292. locationOffset += strlen("location:");
  293. while (*locationOffset == ' ') { // skip url leading white-space
  294. locationOffset++;
  295. }
  296. char *locationOffsetEnd = (char *) os_strstr(locationOffset, "\r\n");
  297. if ( locationOffsetEnd == NULL ) {
  298. HTTPCLIENT_ERR( "Found Location header but was incomplete" );
  299. http_status = -1;
  300. } else {
  301. *locationOffsetEnd = '\0';
  302. req->redirect_follow_count++;
  303. // Check if url is absolute
  304. bool url_has_protocol =
  305. os_strncmp( locationOffset, "http://", strlen( "http://" ) ) == 0 ||
  306. os_strncmp( locationOffset, "https://", strlen( "https://" ) ) == 0;
  307. if ( url_has_protocol ) {
  308. http_request( locationOffset, req->method, req->headers,
  309. req->post_data, req->callback_handle, req->redirect_follow_count );
  310. } else {
  311. if ( os_strncmp( locationOffset, "/", 1 ) == 0) { // relative and full path
  312. http_raw_request( req->hostname, req->port,
  313. #ifdef CLIENT_SSL_ENABLE
  314. req->secure,
  315. #else
  316. 0,
  317. #endif
  318. req->method, locationOffset, req->headers, req->post_data, req->callback_handle, req->redirect_follow_count );
  319. } else { // relative and relative path
  320. // find last /
  321. const char *pathFolderEnd = strrchr(req->path, '/');
  322. int pathFolderLength = pathFolderEnd - req->path;
  323. pathFolderLength++; // use the '/'
  324. int locationLength = strlen(locationOffset);
  325. locationLength++; // use the '\0'
  326. // append pathFolder with given relative path
  327. char *completeRelativePath = (char *) os_malloc(pathFolderLength + locationLength);
  328. os_memcpy( completeRelativePath, req->path, pathFolderLength );
  329. os_memcpy( completeRelativePath + pathFolderLength, locationOffset, locationLength);
  330. http_raw_request( req->hostname, req->port,
  331. #ifdef CLIENT_SSL_ENABLE
  332. req->secure,
  333. #else
  334. 0,
  335. #endif
  336. req->method, completeRelativePath, req->headers, req->post_data, req->callback_handle, req->redirect_follow_count );
  337. os_free( completeRelativePath );
  338. }
  339. }
  340. http_free_req( req );
  341. espconn_delete( conn );
  342. os_free( conn );
  343. return;
  344. }
  345. } else {
  346. HTTPCLIENT_ERR("Too many redirections");
  347. http_status = -1;
  348. }
  349. } else {
  350. body = (char *) os_strstr(req->buffer, "\r\n\r\n");
  351. if (NULL == body) {
  352. /* Find missing body */
  353. HTTPCLIENT_ERR("Body shouldn't be NULL");
  354. /* To avoid NULL body */
  355. body = "";
  356. } else {
  357. /* Skip CR & LF */
  358. body = body + 4;
  359. }
  360. if ( os_strstr( req->buffer, "Transfer-Encoding: chunked" ) )
  361. {
  362. int body_size = req->buffer_size - (body - req->buffer);
  363. char chunked_decode_buffer[body_size];
  364. os_memset( chunked_decode_buffer, 0, body_size );
  365. /* Chuncked data */
  366. http_chunked_decode( body, chunked_decode_buffer );
  367. os_memcpy( body, chunked_decode_buffer, body_size );
  368. }
  369. }
  370. }
  371. }
  372. if ( req->callback_handle != NULL ) /* Callback is optional. */
  373. {
  374. char *req_buffer = req->buffer;
  375. req->buffer = NULL;
  376. http_callback_t req_callback;
  377. req_callback = req->callback_handle;
  378. http_free_req( req );
  379. req_callback( body, http_status, &req_buffer );
  380. if (req_buffer) {
  381. os_free(req_buffer);
  382. }
  383. } else {
  384. http_free_req( req );
  385. }
  386. }
  387. /* Fix memory leak. */
  388. espconn_delete( conn );
  389. os_free( conn );
  390. }
  391. static void ICACHE_FLASH_ATTR http_timeout_callback( void *arg )
  392. {
  393. HTTPCLIENT_ERR( "Connection timeout" );
  394. struct espconn * conn = (struct espconn *) arg;
  395. if ( conn == NULL )
  396. {
  397. HTTPCLIENT_DEBUG( "Connection is NULL" );
  398. return;
  399. }
  400. if ( conn->reverse == NULL )
  401. {
  402. HTTPCLIENT_DEBUG( "Connection request data (reverse) is NULL" );
  403. return;
  404. }
  405. request_args_t * req = (request_args_t *) conn->reverse;
  406. HTTPCLIENT_DEBUG( "Calling disconnect" );
  407. /* Call disconnect */
  408. sint8 result;
  409. #ifdef CLIENT_SSL_ENABLE
  410. if ( req->secure )
  411. result = espconn_secure_disconnect( conn );
  412. else
  413. #endif
  414. result = espconn_disconnect( conn );
  415. if (result == ESPCONN_OK || result == ESPCONN_INPROGRESS)
  416. return;
  417. else
  418. {
  419. /* not connected; execute the callback ourselves. */
  420. HTTPCLIENT_DEBUG( "manually Calling disconnect callback due to error %d", result );
  421. http_disconnect_callback( arg );
  422. }
  423. }
  424. static void ICACHE_FLASH_ATTR http_error_callback( void *arg, sint8 errType )
  425. {
  426. HTTPCLIENT_ERR( "Disconnected with error: %d", errType );
  427. http_timeout_callback( arg );
  428. }
  429. static void ICACHE_FLASH_ATTR http_dns_callback( const char * hostname, ip_addr_t * addr, void * arg )
  430. {
  431. request_args_t * req = (request_args_t *) arg;
  432. if ( addr == NULL )
  433. {
  434. HTTPCLIENT_ERR( "DNS failed for %s", hostname );
  435. if ( req->callback_handle != NULL )
  436. {
  437. req->callback_handle( "", -1, NULL );
  438. }
  439. http_free_req( req );
  440. }
  441. else
  442. {
  443. HTTPCLIENT_DEBUG( "DNS found %s " IPSTR, hostname, IP2STR( addr ) );
  444. struct espconn * conn = (struct espconn *) os_zalloc( sizeof(struct espconn) );
  445. conn->type = ESPCONN_TCP;
  446. conn->state = ESPCONN_NONE;
  447. conn->proto.tcp = (esp_tcp *) os_zalloc( sizeof(esp_tcp) );
  448. conn->proto.tcp->local_port = espconn_port();
  449. conn->proto.tcp->remote_port = req->port;
  450. conn->reverse = req;
  451. os_memcpy( conn->proto.tcp->remote_ip, addr, 4 );
  452. espconn_regist_connectcb( conn, http_connect_callback );
  453. espconn_regist_disconcb( conn, http_disconnect_callback );
  454. espconn_regist_reconcb( conn, http_error_callback );
  455. /* Set connection timeout timer */
  456. os_timer_disarm( &(req->timeout_timer) );
  457. os_timer_setfn( &(req->timeout_timer), (os_timer_func_t *) http_timeout_callback, conn );
  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. }