http.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. /*
  2. * http client for RT-Thread
  3. */
  4. #include "http.h"
  5. #include <dfs_posix.h>
  6. #include <lwip/sockets.h>
  7. #include <lwip/netdb.h>
  8. const char _http_get[] = "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: RT-Thread HTTP Agent\r\n\r\n";
  9. const char _shoutcast_get[] = "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: RT-Thread HTTP Agent\r\nIcy-MetaData: 1\r\nConnection: close\r\n\r\n";
  10. extern long int strtol(const char *nptr, char **endptr, int base);
  11. //
  12. // This function will parse the Content-Length header line and return the file size
  13. //
  14. int http_parse_content_length(char *mime_buf)
  15. {
  16. char *line;
  17. line = strstr(mime_buf, "CONTENT-LENGTH:");
  18. line += strlen("CONTENT-LENGTH:");
  19. // Advance past any whitepace characters
  20. while((*line == ' ') || (*line == '\t')) line++;
  21. return (int)strtol(line, RT_NULL, 10);
  22. }
  23. //
  24. // This function will parse the initial response header line and return 0 for a "200 OK",
  25. // or return the error code in the event of an error (such as 404 - not found)
  26. //
  27. int http_is_error_header(char *mime_buf)
  28. {
  29. char *line;
  30. int i;
  31. int code;
  32. line = strstr(mime_buf, "HTTP/1.");
  33. line += strlen("HTTP/1.");
  34. // Advance past minor protocol version number
  35. line++;
  36. // Advance past any whitespace characters
  37. while((*line == ' ') || (*line == '\t')) line++;
  38. // Terminate string after status code
  39. for(i = 0; ((line[i] != ' ') && (line[i] != '\t')); i++);
  40. line[i] = '\0';
  41. code = (int)strtol(line, RT_NULL, 10);
  42. if( code == 200 )
  43. return 0;
  44. else
  45. return code;
  46. }
  47. int shoutcast_is_error_header(char *mime_buf)
  48. {
  49. char *line;
  50. int i;
  51. int code;
  52. line = strstr(mime_buf, "ICY");
  53. line += strlen("ICY");
  54. // Advance past minor protocol version number
  55. line++;
  56. // Advance past any whitespace characters
  57. while((*line == ' ') || (*line == '\t')) line++;
  58. // Terminate string after status code
  59. for(i = 0; ((line[i] != ' ') && (line[i] != '\t')); i++);
  60. line[i] = '\0';
  61. code = (int)strtol(line, RT_NULL, 10);
  62. if( code == 200 )
  63. return 0;
  64. else
  65. return code;
  66. }
  67. //
  68. // When a request has been sent, we can expect mime headers to be
  69. // before the data. We need to read exactly to the end of the headers
  70. // and no more data. This readline reads a single char at a time.
  71. //
  72. int http_read_line( int socket, char * buffer, int size )
  73. {
  74. char * ptr = buffer;
  75. int count = 0;
  76. int rc;
  77. // Keep reading until we fill the buffer.
  78. while ( count < size )
  79. {
  80. rc = recv( socket, ptr, 1, 0 );
  81. if ( rc <= 0 ) return rc;
  82. if ((*ptr == '\n'))
  83. {
  84. ptr ++;
  85. count++;
  86. break;
  87. }
  88. // increment after check for cr. Don't want to count the cr.
  89. count++;
  90. ptr++;
  91. }
  92. // Terminate string
  93. *ptr = '\0';
  94. // return how many bytes read.
  95. return count;
  96. }
  97. /*
  98. * resolve server address
  99. * @param server the server sockaddress
  100. * @param url the input URL address, for example, http://www.rt-thread.org/index.html
  101. * @param host_addr the buffer pointer to save server host address
  102. * @param request the pointer to point the request url, for example, /index.html
  103. *
  104. * @return 0 on resolve server address OK, others failed
  105. */
  106. int http_resolve_address(struct sockaddr_in *server, const char * url, char *host_addr, char** request)
  107. {
  108. char *ptr;
  109. char port[6] = "80"; /* default port of 80(HTTP) */
  110. int i = 0, is_domain;
  111. struct hostent *hptr;
  112. /* strip http: */
  113. ptr = strchr(url, ':');
  114. if (ptr != NULL)
  115. {
  116. url = ptr + 1;
  117. }
  118. /* URL must start with double forward slashes. */
  119. if((url[0] != '/') || (url[1] != '/' )) return -1;
  120. url += 2; is_domain = 0;
  121. i = 0;
  122. /* allow specification of port in URL like http://www.server.net:8080/ */
  123. while (*url)
  124. {
  125. if (*url == '/') break;
  126. if (*url == ':')
  127. {
  128. unsigned char w;
  129. for (w = 0; w < 5 && url[w] != '/' && url[w] != '\0'; w ++)
  130. port[w] = url[w + 1];
  131. /* get port ok */
  132. port[w] = '\0';
  133. url += w + 1;
  134. break;
  135. }
  136. host_addr[i++] = *url;
  137. url ++;
  138. }
  139. *request = (char*)url;
  140. /* get host addr ok. */
  141. host_addr[i] = '\0';
  142. if (is_domain)
  143. {
  144. /* resolve the host name. */
  145. hptr = gethostbyname(host_addr);
  146. if(hptr == 0)
  147. {
  148. rt_kprintf("HTTP: failed to resolve domain '%s'\n", host_addr);
  149. return -1;
  150. }
  151. memcpy(&server->sin_addr, *hptr->h_addr_list, sizeof(server->sin_addr));
  152. }
  153. else
  154. {
  155. inet_aton(host_addr, (struct in_addr*)&(server->sin_addr));
  156. }
  157. /* set the port */
  158. server->sin_port = htons((int) strtol(port, NULL, 10));
  159. server->sin_family = AF_INET;
  160. return 0;
  161. }
  162. //
  163. // This is the main HTTP client connect work. Makes the connection
  164. // and handles the protocol and reads the return headers. Needs
  165. // to leave the stream at the start of the real data.
  166. //
  167. static int http_connect(struct http_session* session,
  168. struct sockaddr_in * server, char *host_addr, const char *url)
  169. {
  170. int socket_handle;
  171. int peer_handle;
  172. int rc;
  173. char mimeBuffer[100];
  174. if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
  175. {
  176. rt_kprintf( "HTTP: SOCKET FAILED\n" );
  177. return -1;
  178. }
  179. peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
  180. if ( peer_handle < 0 )
  181. {
  182. rt_kprintf( "HTTP: CONNECT FAILED %i\n", peer_handle );
  183. return -1;
  184. }
  185. {
  186. char *buf;
  187. rt_uint32_t length;
  188. buf = rt_malloc (512);
  189. if (*url)
  190. length = rt_snprintf(buf, 512, _http_get, url, host_addr, server->sin_port);
  191. else
  192. length = rt_snprintf(buf, 512, _http_get, "/", host_addr, server->sin_port);
  193. rc = send(peer_handle, buf, length, 0);
  194. rt_kprintf("HTTP request:\n%s", buf);
  195. /* release buffer */
  196. rt_free(buf);
  197. }
  198. // We now need to read the header information
  199. while ( 1 )
  200. {
  201. int i;
  202. // read a line from the header information.
  203. rc = http_read_line( peer_handle, mimeBuffer, 100 );
  204. rt_kprintf(">> %s\n", mimeBuffer);
  205. if ( rc < 0 ) return rc;
  206. // End of headers is a blank line. exit.
  207. if (rc == 0) break;
  208. if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
  209. // Convert mimeBuffer to upper case, so we can do string comps
  210. for(i = 0; i < strlen(mimeBuffer); i++)
  211. mimeBuffer[i] = toupper(mimeBuffer[i]);
  212. if(strstr(mimeBuffer, "HTTP/1.")) // First line of header, contains status code. Check for an error code
  213. {
  214. rc = http_is_error_header(mimeBuffer);
  215. if(rc)
  216. {
  217. rt_kprintf("HTTP: status code = %d!\n", rc);
  218. return -rc;
  219. }
  220. }
  221. if(strstr(mimeBuffer, "CONTENT-LENGTH:"))
  222. {
  223. session->size = http_parse_content_length(mimeBuffer);
  224. rt_kprintf("size = %d\n", session->size);
  225. }
  226. }
  227. // We've sent the request, and read the headers. SockHandle is
  228. // now at the start of the main data read for a file io read.
  229. return peer_handle;
  230. }
  231. struct http_session* http_session_open(char* url)
  232. {
  233. int peer_handle = 0;
  234. struct sockaddr_in server;
  235. char *request, host_addr[32];
  236. struct http_session* session;
  237. session = (struct http_session*) rt_malloc(sizeof(struct http_session));
  238. if(session == RT_NULL) return RT_NULL;
  239. session->size = 0;
  240. session->position = 0;
  241. /* Check valid IP address and URL */
  242. if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
  243. {
  244. rt_free(session);
  245. return RT_NULL;
  246. }
  247. // Now we connect and initiate the transfer by sending a
  248. // request header to the server, and receiving the response header
  249. if((peer_handle = http_connect(session, &server, host_addr, request)) < 0)
  250. {
  251. rt_kprintf("HTTP: failed to connect to '%s'!\n", host_addr);
  252. rt_free(session);
  253. return RT_NULL;
  254. }
  255. // http connect returns valid socket. Save in handle list.
  256. session->socket = peer_handle;
  257. /* open successfully */
  258. return session;
  259. }
  260. rt_size_t http_session_read(struct http_session* session, rt_uint8_t *buffer, rt_size_t length)
  261. {
  262. int bytesRead = 0;
  263. int totalRead = 0;
  264. int left = length;
  265. // Read until: there is an error, we've read "size" bytes or the remote
  266. // side has closed the connection.
  267. do
  268. {
  269. bytesRead = recv(session->socket, buffer + totalRead, left, 0);
  270. if(bytesRead <= 0) break;
  271. left -= bytesRead;
  272. totalRead += bytesRead;
  273. } while(left);
  274. return totalRead;
  275. }
  276. rt_off_t http_session_seek(struct http_session* session, rt_off_t offset, int mode)
  277. {
  278. switch(mode)
  279. {
  280. case SEEK_SET:
  281. session->position = offset;
  282. break;
  283. case SEEK_CUR:
  284. session->position += offset;
  285. break;
  286. case SEEK_END:
  287. session->position = session->size + offset;
  288. break;
  289. }
  290. return session->position;
  291. }
  292. int http_session_close(struct http_session* session)
  293. {
  294. lwip_close(session->socket);
  295. rt_free(session);
  296. return 0;
  297. }
  298. //
  299. // This is the main HTTP client connect work. Makes the connection
  300. // and handles the protocol and reads the return headers. Needs
  301. // to leave the stream at the start of the real data.
  302. //
  303. static int shoutcast_connect(struct shoutcast_session* session,
  304. struct sockaddr_in* server, char* host_addr, const char* url)
  305. {
  306. int socket_handle;
  307. int peer_handle;
  308. int rc;
  309. char mimeBuffer[256];
  310. if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
  311. {
  312. rt_kprintf( "ICY: SOCKET FAILED\n" );
  313. return -1;
  314. }
  315. peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
  316. if ( peer_handle < 0 )
  317. {
  318. rt_kprintf( "ICY: CONNECT FAILED %i\n", peer_handle );
  319. return -1;
  320. }
  321. {
  322. char *buf;
  323. rt_uint32_t length;
  324. buf = rt_malloc (512);
  325. if (*url)
  326. length = rt_snprintf(buf, 512, _shoutcast_get, url, host_addr, server->sin_port);
  327. else
  328. length = rt_snprintf(buf, 512, _shoutcast_get, "/", host_addr, server->sin_port);
  329. rc = send(peer_handle, buf, length, 0);
  330. rt_kprintf("SHOUTCAST request:\n%s", buf);
  331. /* release buffer */
  332. rt_free(buf);
  333. }
  334. /* read the header information */
  335. while ( 1 )
  336. {
  337. // read a line from the header information.
  338. rc = http_read_line(peer_handle, mimeBuffer, 100);
  339. rt_kprintf(">>%s", mimeBuffer);
  340. if ( rc < 0 ) return rc;
  341. // End of headers is a blank line. exit.
  342. if (rc == 0) break;
  343. if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
  344. if(strstr(mimeBuffer, "ICY")) // First line of header, contains status code. Check for an error code
  345. {
  346. rc = shoutcast_is_error_header(mimeBuffer);
  347. if(rc)
  348. {
  349. rt_kprintf("ICY: status code = %d!\n", rc);
  350. return -rc;
  351. }
  352. }
  353. if (strstr(mimeBuffer, "HTTP/1."))
  354. {
  355. rc = http_is_error_header(mimeBuffer);
  356. if(rc)
  357. {
  358. rt_kprintf("HTTP: status code = %d!\n", rc);
  359. return -rc;
  360. }
  361. }
  362. if (strstr(mimeBuffer, "icy-name:"))
  363. {
  364. /* get name */
  365. char* name;
  366. name = mimeBuffer + strlen("icy-name:");
  367. session->station_name = rt_strdup(name);
  368. rt_kprintf("station name: %s\n", session->station_name);
  369. }
  370. if (strstr(mimeBuffer, "icy-br:"))
  371. {
  372. /* get bitrate */
  373. session->bitrate = strtol(mimeBuffer + strlen("icy-br:"), RT_NULL, 10);
  374. rt_kprintf("bitrate: %d\n", session->bitrate);
  375. }
  376. if (strstr(mimeBuffer, "icy-metaint:"))
  377. {
  378. /* get metaint */
  379. session->metaint = strtol(mimeBuffer + strlen("icy-metaint:"), RT_NULL, 10);
  380. rt_kprintf("metaint: %d\n", session->metaint);
  381. }
  382. if (strstr(mimeBuffer, "content-type:"))
  383. {
  384. /* check content-type */
  385. if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
  386. {
  387. rt_kprintf("ICY content is not audio/mpeg.\n");
  388. return -1;
  389. }
  390. }
  391. if (strstr(mimeBuffer, "Content-Type:"))
  392. {
  393. /* check content-type */
  394. if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
  395. {
  396. rt_kprintf("ICY content is not audio/mpeg.\n");
  397. return -1;
  398. }
  399. }
  400. }
  401. // We've sent the request, and read the headers. SockHandle is
  402. // now at the start of the main data read for a file io read.
  403. return peer_handle;
  404. }
  405. struct shoutcast_session* shoutcast_session_open(char* url)
  406. {
  407. int peer_handle = 0;
  408. struct sockaddr_in server;
  409. char *request, host_addr[32];
  410. struct shoutcast_session* session;
  411. session = (struct shoutcast_session*) rt_malloc(sizeof(struct shoutcast_session));
  412. if(session == RT_NULL) return RT_NULL;
  413. session->metaint = 0;
  414. session->current_meta_chunk = 0;
  415. session->bitrate = 0;
  416. session->station_name = RT_NULL;
  417. /* Check valid IP address and URL */
  418. if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
  419. {
  420. rt_free(session);
  421. return RT_NULL;
  422. }
  423. // Now we connect and initiate the transfer by sending a
  424. // request header to the server, and receiving the response header
  425. if((peer_handle = shoutcast_connect(session, &server, host_addr, request)) < 0)
  426. {
  427. rt_kprintf("SHOUTCAST: failed to connect to '%s'!\n", host_addr);
  428. if (session->station_name != RT_NULL)
  429. rt_free(session->station_name);
  430. rt_free(session);
  431. return RT_NULL;
  432. }
  433. // http connect returns valid socket. Save in handle list.
  434. session->socket = peer_handle;
  435. /* open successfully */
  436. return session;
  437. }
  438. rt_size_t shoutcast_session_read(struct shoutcast_session* session, rt_uint8_t *buffer, rt_size_t length)
  439. {
  440. int bytesRead = 0;
  441. int totalRead = 0;
  442. int left = length;
  443. static rt_uint32_t first_meta_size = 0;
  444. // Read until: there is an error, we've read "size" bytes or the remote
  445. // side has closed the connection.
  446. do
  447. {
  448. bytesRead = recv(session->socket, buffer + totalRead, left, 0);
  449. if(bytesRead <= 0)
  450. {
  451. rt_kprintf("no data on recv, len %d,err %d\n", bytesRead,
  452. lwip_get_error(session->socket));
  453. break;
  454. }
  455. left -= bytesRead;
  456. totalRead += bytesRead;
  457. } while(left);
  458. /* handle meta */
  459. if (first_meta_size > 0)
  460. {
  461. /* skip meta data */
  462. memmove(&buffer[0], &buffer[first_meta_size], totalRead - first_meta_size);
  463. // rt_kprintf("remove meta: len %d\n", first_meta_size);
  464. totalRead = totalRead - first_meta_size;
  465. first_meta_size = 0;
  466. session->current_meta_chunk = totalRead;
  467. }
  468. else
  469. {
  470. if (session->current_meta_chunk + totalRead == session->metaint)
  471. {
  472. rt_uint8_t meta_data;
  473. recv(session->socket, &meta_data, 1, 0);
  474. /* remove meta data in next packet */
  475. first_meta_size = meta_data * 16;
  476. session->current_meta_chunk = 0;
  477. // rt_kprintf("get meta: len %d\n", first_meta_size);
  478. }
  479. else if (session->current_meta_chunk + totalRead > session->metaint)
  480. {
  481. int meta_length, next_chunk_length;
  482. // rt_kprintf("c: %d, total: %d, m: %d\n", session->current_meta_chunk, totalRead, session->metaint);
  483. /* get the length of meta data */
  484. meta_length = buffer[session->metaint - session->current_meta_chunk] * 16;
  485. next_chunk_length = totalRead - (session->metaint - session->current_meta_chunk) -
  486. (meta_length + 1);
  487. // rt_kprintf("l: %d, n: %d\n", meta_length, next_chunk_length);
  488. /* skip meta data */
  489. memmove(&buffer[session->metaint - session->current_meta_chunk],
  490. &buffer[session->metaint - session->current_meta_chunk + meta_length + 1],
  491. next_chunk_length);
  492. /* set new current meta chunk */
  493. session->current_meta_chunk = next_chunk_length;
  494. totalRead = totalRead - (meta_length + 1);
  495. }
  496. else
  497. {
  498. session->current_meta_chunk += totalRead;
  499. }
  500. }
  501. return totalRead;
  502. }
  503. rt_off_t shoutcast_session_seek(struct shoutcast_session* session, rt_off_t offset, int mode)
  504. {
  505. /* not support seek yet */
  506. return 0;
  507. }
  508. int shoutcast_session_close(struct shoutcast_session* session)
  509. {
  510. lwip_close(session->socket);
  511. if (session->station_name != RT_NULL)
  512. rt_free(session->station_name);
  513. rt_free(session);
  514. return 0;
  515. }
  516. #include <finsh.h>
  517. void http_test(char* url)
  518. {
  519. struct http_session* session;
  520. char buffer[80];
  521. rt_size_t length;
  522. session = http_session_open(url);
  523. if (session == RT_NULL)
  524. {
  525. rt_kprintf("open http session failed\n");
  526. return;
  527. }
  528. do
  529. {
  530. rt_memset(buffer, 0, sizeof(buffer));
  531. length = http_session_read(session, (rt_uint8_t*)buffer, sizeof(buffer));
  532. rt_kprintf(buffer);rt_kprintf("\n");
  533. } while (length > 0);
  534. http_session_close(session);
  535. }
  536. FINSH_FUNCTION_EXPORT(http_test, http client test);
  537. void shoutcast_test(char* url)
  538. {
  539. struct shoutcast_session* session;
  540. session = shoutcast_session_open(url);
  541. if (session == RT_NULL)
  542. {
  543. rt_kprintf("open shoutcast session failed\n");
  544. return;
  545. }
  546. shoutcast_session_close(session);
  547. }
  548. FINSH_FUNCTION_EXPORT(shoutcast_test, shoutcast client test);