http.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 + 1] != '/' && url[w + 1] != '\0'; w ++)
  130. port[w] = url[w + 1];
  131. /* get port ok */
  132. port[w] = '\0';
  133. url += w + 1;
  134. break;
  135. }
  136. if ((*url < '0' || *url > '9') && *url != '.')
  137. is_domain = 1;
  138. host_addr[i++] = *url;
  139. url ++;
  140. }
  141. *request = (char*)url;
  142. /* get host addr ok. */
  143. host_addr[i] = '\0';
  144. if (is_domain)
  145. {
  146. /* resolve the host name. */
  147. hptr = gethostbyname(host_addr);
  148. if(hptr == 0)
  149. {
  150. rt_kprintf("HTTP: failed to resolve domain '%s'\n", host_addr);
  151. return -1;
  152. }
  153. memcpy(&server->sin_addr, *hptr->h_addr_list, sizeof(server->sin_addr));
  154. }
  155. else
  156. {
  157. inet_aton(host_addr, (struct in_addr*)&(server->sin_addr));
  158. }
  159. /* set the port */
  160. server->sin_port = htons((int) strtol(port, NULL, 10));
  161. server->sin_family = AF_INET;
  162. return 0;
  163. }
  164. //
  165. // This is the main HTTP client connect work. Makes the connection
  166. // and handles the protocol and reads the return headers. Needs
  167. // to leave the stream at the start of the real data.
  168. //
  169. static int http_connect(struct http_session* session,
  170. struct sockaddr_in * server, char *host_addr, const char *url)
  171. {
  172. int socket_handle;
  173. int peer_handle;
  174. int rc;
  175. char mimeBuffer[100];
  176. if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
  177. {
  178. rt_kprintf( "HTTP: SOCKET FAILED\n" );
  179. return -1;
  180. }
  181. peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
  182. if ( peer_handle < 0 )
  183. {
  184. rt_kprintf( "HTTP: CONNECT FAILED %i\n", peer_handle );
  185. return -1;
  186. }
  187. {
  188. char *buf;
  189. rt_uint32_t length;
  190. buf = rt_malloc (512);
  191. if (*url)
  192. length = rt_snprintf(buf, 512, _http_get, url, host_addr, server->sin_port);
  193. else
  194. length = rt_snprintf(buf, 512, _http_get, "/", host_addr, server->sin_port);
  195. rc = send(peer_handle, buf, length, 0);
  196. rt_kprintf("HTTP request:\n%s", buf);
  197. /* release buffer */
  198. rt_free(buf);
  199. }
  200. // We now need to read the header information
  201. while ( 1 )
  202. {
  203. int i;
  204. // read a line from the header information.
  205. rc = http_read_line( peer_handle, mimeBuffer, 100 );
  206. rt_kprintf(">> %s\n", mimeBuffer);
  207. if ( rc < 0 ) return rc;
  208. // End of headers is a blank line. exit.
  209. if (rc == 0) break;
  210. if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
  211. // Convert mimeBuffer to upper case, so we can do string comps
  212. for(i = 0; i < strlen(mimeBuffer); i++)
  213. mimeBuffer[i] = toupper(mimeBuffer[i]);
  214. if(strstr(mimeBuffer, "HTTP/1.")) // First line of header, contains status code. Check for an error code
  215. {
  216. rc = http_is_error_header(mimeBuffer);
  217. if(rc)
  218. {
  219. rt_kprintf("HTTP: status code = %d!\n", rc);
  220. return -rc;
  221. }
  222. }
  223. if(strstr(mimeBuffer, "CONTENT-LENGTH:"))
  224. {
  225. session->size = http_parse_content_length(mimeBuffer);
  226. rt_kprintf("size = %d\n", session->size);
  227. }
  228. }
  229. // We've sent the request, and read the headers. SockHandle is
  230. // now at the start of the main data read for a file io read.
  231. return peer_handle;
  232. }
  233. struct http_session* http_session_open(char* url)
  234. {
  235. int peer_handle = 0;
  236. struct sockaddr_in server;
  237. char *request, host_addr[32];
  238. struct http_session* session;
  239. session = (struct http_session*) rt_malloc(sizeof(struct http_session));
  240. if(session == RT_NULL) return RT_NULL;
  241. session->size = 0;
  242. session->position = 0;
  243. /* Check valid IP address and URL */
  244. if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
  245. {
  246. rt_free(session);
  247. return RT_NULL;
  248. }
  249. // Now we connect and initiate the transfer by sending a
  250. // request header to the server, and receiving the response header
  251. if((peer_handle = http_connect(session, &server, host_addr, request)) < 0)
  252. {
  253. rt_kprintf("HTTP: failed to connect to '%s'!\n", host_addr);
  254. rt_free(session);
  255. return RT_NULL;
  256. }
  257. // http connect returns valid socket. Save in handle list.
  258. session->socket = peer_handle;
  259. /* open successfully */
  260. return session;
  261. }
  262. rt_size_t http_session_read(struct http_session* session, rt_uint8_t *buffer, rt_size_t length)
  263. {
  264. int bytesRead = 0;
  265. int totalRead = 0;
  266. int left = length;
  267. // Read until: there is an error, we've read "size" bytes or the remote
  268. // side has closed the connection.
  269. do
  270. {
  271. bytesRead = recv(session->socket, buffer + totalRead, left, 0);
  272. if(bytesRead <= 0) break;
  273. left -= bytesRead;
  274. totalRead += bytesRead;
  275. } while(left);
  276. return totalRead;
  277. }
  278. rt_off_t http_session_seek(struct http_session* session, rt_off_t offset, int mode)
  279. {
  280. switch(mode)
  281. {
  282. case SEEK_SET:
  283. session->position = offset;
  284. break;
  285. case SEEK_CUR:
  286. session->position += offset;
  287. break;
  288. case SEEK_END:
  289. session->position = session->size + offset;
  290. break;
  291. }
  292. return session->position;
  293. }
  294. int http_session_close(struct http_session* session)
  295. {
  296. lwip_close(session->socket);
  297. rt_free(session);
  298. return 0;
  299. }
  300. //
  301. // This is the main HTTP client connect work. Makes the connection
  302. // and handles the protocol and reads the return headers. Needs
  303. // to leave the stream at the start of the real data.
  304. //
  305. static int shoutcast_connect(struct shoutcast_session* session,
  306. struct sockaddr_in* server, char* host_addr, const char* url)
  307. {
  308. int socket_handle;
  309. int peer_handle;
  310. int rc;
  311. char mimeBuffer[256];
  312. if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
  313. {
  314. rt_kprintf( "ICY: SOCKET FAILED\n" );
  315. return -1;
  316. }
  317. peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
  318. if ( peer_handle < 0 )
  319. {
  320. rt_kprintf( "ICY: CONNECT FAILED %i\n", peer_handle );
  321. return -1;
  322. }
  323. {
  324. char *buf;
  325. rt_uint32_t length;
  326. buf = rt_malloc (512);
  327. if (*url)
  328. length = rt_snprintf(buf, 512, _shoutcast_get, url, host_addr, ntohs(server->sin_port));
  329. else
  330. length = rt_snprintf(buf, 512, _shoutcast_get, "/", host_addr, ntohs(server->sin_port));
  331. rc = send(peer_handle, buf, length, 0);
  332. rt_kprintf("SHOUTCAST request:\n%s", buf);
  333. /* release buffer */
  334. rt_free(buf);
  335. }
  336. /* read the header information */
  337. while ( 1 )
  338. {
  339. // read a line from the header information.
  340. rc = http_read_line(peer_handle, mimeBuffer, 100);
  341. rt_kprintf(">>%s", mimeBuffer);
  342. if ( rc < 0 ) return rc;
  343. // End of headers is a blank line. exit.
  344. if (rc == 0) break;
  345. if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
  346. if(strstr(mimeBuffer, "ICY")) // First line of header, contains status code. Check for an error code
  347. {
  348. rc = shoutcast_is_error_header(mimeBuffer);
  349. if(rc)
  350. {
  351. rt_kprintf("ICY: status code = %d!\n", rc);
  352. return -rc;
  353. }
  354. }
  355. if (strstr(mimeBuffer, "HTTP/1."))
  356. {
  357. rc = http_is_error_header(mimeBuffer);
  358. if(rc)
  359. {
  360. rt_kprintf("HTTP: status code = %d!\n", rc);
  361. return -rc;
  362. }
  363. }
  364. if (strstr(mimeBuffer, "icy-name:"))
  365. {
  366. /* get name */
  367. char* name;
  368. name = mimeBuffer + strlen("icy-name:");
  369. session->station_name = rt_strdup(name);
  370. rt_kprintf("station name: %s\n", session->station_name);
  371. }
  372. if (strstr(mimeBuffer, "icy-br:"))
  373. {
  374. /* get bitrate */
  375. session->bitrate = strtol(mimeBuffer + strlen("icy-br:"), RT_NULL, 10);
  376. rt_kprintf("bitrate: %d\n", session->bitrate);
  377. }
  378. if (strstr(mimeBuffer, "icy-metaint:"))
  379. {
  380. /* get metaint */
  381. session->metaint = strtol(mimeBuffer + strlen("icy-metaint:"), RT_NULL, 10);
  382. rt_kprintf("metaint: %d\n", session->metaint);
  383. }
  384. if (strstr(mimeBuffer, "content-type:"))
  385. {
  386. /* check content-type */
  387. if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
  388. {
  389. rt_kprintf("ICY content is not audio/mpeg.\n");
  390. return -1;
  391. }
  392. }
  393. if (strstr(mimeBuffer, "Content-Type:"))
  394. {
  395. /* check content-type */
  396. if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
  397. {
  398. rt_kprintf("ICY content is not audio/mpeg.\n");
  399. return -1;
  400. }
  401. }
  402. }
  403. // We've sent the request, and read the headers. SockHandle is
  404. // now at the start of the main data read for a file io read.
  405. return peer_handle;
  406. }
  407. struct shoutcast_session* shoutcast_session_open(char* url)
  408. {
  409. int peer_handle = 0;
  410. struct sockaddr_in server;
  411. char *request, host_addr[32];
  412. struct shoutcast_session* session;
  413. session = (struct shoutcast_session*) rt_malloc(sizeof(struct shoutcast_session));
  414. if(session == RT_NULL) return RT_NULL;
  415. session->metaint = 0;
  416. session->current_meta_chunk = 0;
  417. session->bitrate = 0;
  418. session->station_name = RT_NULL;
  419. /* Check valid IP address and URL */
  420. if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
  421. {
  422. rt_free(session);
  423. return RT_NULL;
  424. }
  425. rt_kprintf("connect to: %s...\n", host_addr);
  426. // Now we connect and initiate the transfer by sending a
  427. // request header to the server, and receiving the response header
  428. if((peer_handle = shoutcast_connect(session, &server, host_addr, request)) < 0)
  429. {
  430. rt_kprintf("SHOUTCAST: failed to connect to '%s'!\n", host_addr);
  431. if (session->station_name != RT_NULL)
  432. rt_free(session->station_name);
  433. rt_free(session);
  434. return RT_NULL;
  435. }
  436. // http connect returns valid socket. Save in handle list.
  437. session->socket = peer_handle;
  438. /* open successfully */
  439. return session;
  440. }
  441. rt_size_t shoutcast_session_read(struct shoutcast_session* session, rt_uint8_t *buffer, rt_size_t length)
  442. {
  443. int bytesRead = 0;
  444. int totalRead = 0;
  445. int left = length;
  446. static rt_uint32_t first_meta_size = 0;
  447. // Read until: there is an error, we've read "size" bytes or the remote
  448. // side has closed the connection.
  449. do
  450. {
  451. bytesRead = recv(session->socket, buffer + totalRead, left, 0);
  452. if(bytesRead <= 0)
  453. {
  454. rt_kprintf("no data on recv, len %d,err %d\n", bytesRead,
  455. lwip_get_error(session->socket));
  456. break;
  457. }
  458. left -= bytesRead;
  459. totalRead += bytesRead;
  460. } while(left);
  461. /* handle meta */
  462. if (first_meta_size > 0)
  463. {
  464. /* skip meta data */
  465. memmove(&buffer[0], &buffer[first_meta_size], totalRead - first_meta_size);
  466. // rt_kprintf("remove meta: len %d\n", first_meta_size);
  467. totalRead = totalRead - first_meta_size;
  468. first_meta_size = 0;
  469. session->current_meta_chunk = totalRead;
  470. }
  471. else
  472. {
  473. if (session->current_meta_chunk + totalRead == session->metaint)
  474. {
  475. rt_uint8_t meta_data;
  476. recv(session->socket, &meta_data, 1, 0);
  477. /* remove meta data in next packet */
  478. first_meta_size = meta_data * 16;
  479. session->current_meta_chunk = 0;
  480. // rt_kprintf("get meta: len %d\n", first_meta_size);
  481. }
  482. else if (session->current_meta_chunk + totalRead > session->metaint)
  483. {
  484. int meta_length, next_chunk_length;
  485. // rt_kprintf("c: %d, total: %d, m: %d\n", session->current_meta_chunk, totalRead, session->metaint);
  486. /* get the length of meta data */
  487. meta_length = buffer[session->metaint - session->current_meta_chunk] * 16;
  488. next_chunk_length = totalRead - (session->metaint - session->current_meta_chunk) -
  489. (meta_length + 1);
  490. // rt_kprintf("l: %d, n: %d\n", meta_length, next_chunk_length);
  491. /* skip meta data */
  492. memmove(&buffer[session->metaint - session->current_meta_chunk],
  493. &buffer[session->metaint - session->current_meta_chunk + meta_length + 1],
  494. next_chunk_length);
  495. /* set new current meta chunk */
  496. session->current_meta_chunk = next_chunk_length;
  497. totalRead = totalRead - (meta_length + 1);
  498. }
  499. else
  500. {
  501. session->current_meta_chunk += totalRead;
  502. }
  503. }
  504. return totalRead;
  505. }
  506. rt_off_t shoutcast_session_seek(struct shoutcast_session* session, rt_off_t offset, int mode)
  507. {
  508. /* not support seek yet */
  509. return 0;
  510. }
  511. int shoutcast_session_close(struct shoutcast_session* session)
  512. {
  513. lwip_close(session->socket);
  514. if (session->station_name != RT_NULL)
  515. rt_free(session->station_name);
  516. rt_free(session);
  517. return 0;
  518. }
  519. #include <finsh.h>
  520. void http_test(char* url)
  521. {
  522. struct http_session* session;
  523. char buffer[80];
  524. rt_size_t length;
  525. session = http_session_open(url);
  526. if (session == RT_NULL)
  527. {
  528. rt_kprintf("open http session failed\n");
  529. return;
  530. }
  531. do
  532. {
  533. rt_memset(buffer, 0, sizeof(buffer));
  534. length = http_session_read(session, (rt_uint8_t*)buffer, sizeof(buffer));
  535. rt_kprintf(buffer);rt_kprintf("\n");
  536. } while (length > 0);
  537. http_session_close(session);
  538. }
  539. FINSH_FUNCTION_EXPORT(http_test, http client test);
  540. void shoutcast_test(char* url)
  541. {
  542. struct shoutcast_session* session;
  543. session = shoutcast_session_open(url);
  544. if (session == RT_NULL)
  545. {
  546. rt_kprintf("open shoutcast session failed\n");
  547. return;
  548. }
  549. shoutcast_session_close(session);
  550. }
  551. FINSH_FUNCTION_EXPORT(shoutcast_test, shoutcast client test);