Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

http.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*
  2. * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. /**
  19. * @file
  20. *
  21. * Hyper Text Transfer Protocol (HTTP)
  22. *
  23. */
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include <strings.h>
  29. #include <byteswap.h>
  30. #include <errno.h>
  31. #include <assert.h>
  32. #include <gpxe/uri.h>
  33. #include <gpxe/refcnt.h>
  34. #include <gpxe/iobuf.h>
  35. #include <gpxe/xfer.h>
  36. #include <gpxe/open.h>
  37. #include <gpxe/socket.h>
  38. #include <gpxe/tcpip.h>
  39. #include <gpxe/process.h>
  40. #include <gpxe/linebuf.h>
  41. #include <gpxe/http.h>
  42. /** HTTP receive state */
  43. enum http_rx_state {
  44. HTTP_RX_RESPONSE = 0,
  45. HTTP_RX_HEADER,
  46. HTTP_RX_DATA,
  47. HTTP_RX_DEAD,
  48. };
  49. /**
  50. * An HTTP request
  51. *
  52. */
  53. struct http_request {
  54. /** Reference count */
  55. struct refcnt refcnt;
  56. /** Data transfer interface */
  57. struct xfer_interface xfer;
  58. /** URI being fetched */
  59. struct uri *uri;
  60. /** Transport layer interface */
  61. struct xfer_interface socket;
  62. /** TX process */
  63. struct process process;
  64. /** HTTP response code */
  65. unsigned int response;
  66. /** HTTP Content-Length */
  67. size_t content_length;
  68. /** Received length */
  69. size_t rx_len;
  70. /** RX state */
  71. enum http_rx_state rx_state;
  72. /** Line buffer for received header lines */
  73. struct line_buffer linebuf;
  74. };
  75. /**
  76. * Free HTTP request
  77. *
  78. * @v refcnt Reference counter
  79. */
  80. static void http_free ( struct refcnt *refcnt ) {
  81. struct http_request *http =
  82. container_of ( refcnt, struct http_request, refcnt );
  83. uri_put ( http->uri );
  84. empty_line_buffer ( &http->linebuf );
  85. free ( http );
  86. };
  87. /**
  88. * Mark HTTP request as complete
  89. *
  90. * @v http HTTP request
  91. * @v rc Return status code
  92. */
  93. static void http_done ( struct http_request *http, int rc ) {
  94. /* Prevent further processing of any current packet */
  95. http->rx_state = HTTP_RX_DEAD;
  96. /* If we had a Content-Length, and the received content length
  97. * isn't correct, flag an error
  98. */
  99. if ( http->content_length &&
  100. ( http->content_length != http->rx_len ) ) {
  101. DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
  102. http, http->rx_len, http->content_length );
  103. rc = -EIO;
  104. }
  105. /* Remove process */
  106. process_del ( &http->process );
  107. /* Close all data transfer interfaces */
  108. xfer_nullify ( &http->socket );
  109. xfer_close ( &http->socket, rc );
  110. xfer_nullify ( &http->xfer );
  111. xfer_close ( &http->xfer, rc );
  112. }
  113. /**
  114. * Convert HTTP response code to return status code
  115. *
  116. * @v response HTTP response code
  117. * @ret rc Return status code
  118. */
  119. static int http_response_to_rc ( unsigned int response ) {
  120. switch ( response ) {
  121. case 200:
  122. return 0;
  123. case 404:
  124. return -ENOENT;
  125. case 403:
  126. return -EPERM;
  127. default:
  128. return -EIO;
  129. }
  130. }
  131. /**
  132. * Handle HTTP response
  133. *
  134. * @v http HTTP request
  135. * @v response HTTP response
  136. * @ret rc Return status code
  137. */
  138. static int http_rx_response ( struct http_request *http, char *response ) {
  139. char *spc;
  140. int rc;
  141. DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
  142. /* Check response starts with "HTTP/" */
  143. if ( strncmp ( response, "HTTP/", 5 ) != 0 )
  144. return -EIO;
  145. /* Locate and check response code */
  146. spc = strchr ( response, ' ' );
  147. if ( ! spc )
  148. return -EIO;
  149. http->response = strtoul ( spc, NULL, 10 );
  150. if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
  151. return rc;
  152. /* Move to received headers */
  153. http->rx_state = HTTP_RX_HEADER;
  154. return 0;
  155. }
  156. /**
  157. * Handle HTTP Content-Length header
  158. *
  159. * @v http HTTP request
  160. * @v value HTTP header value
  161. * @ret rc Return status code
  162. */
  163. static int http_rx_content_length ( struct http_request *http,
  164. const char *value ) {
  165. char *endp;
  166. http->content_length = strtoul ( value, &endp, 10 );
  167. if ( *endp != '\0' ) {
  168. DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
  169. http, value );
  170. return -EIO;
  171. }
  172. /* Use seek() to notify recipient of filesize */
  173. xfer_seek ( &http->xfer, http->content_length, SEEK_SET );
  174. xfer_seek ( &http->xfer, 0, SEEK_SET );
  175. return 0;
  176. }
  177. /** An HTTP header handler */
  178. struct http_header_handler {
  179. /** Name (e.g. "Content-Length") */
  180. const char *header;
  181. /** Handle received header
  182. *
  183. * @v http HTTP request
  184. * @v value HTTP header value
  185. * @ret rc Return status code
  186. *
  187. * If an error is returned, the download will be aborted.
  188. */
  189. int ( * rx ) ( struct http_request *http, const char *value );
  190. };
  191. /** List of HTTP header handlers */
  192. static struct http_header_handler http_header_handlers[] = {
  193. {
  194. .header = "Content-Length",
  195. .rx = http_rx_content_length,
  196. },
  197. { NULL, NULL }
  198. };
  199. /**
  200. * Handle HTTP header
  201. *
  202. * @v http HTTP request
  203. * @v header HTTP header
  204. * @ret rc Return status code
  205. */
  206. static int http_rx_header ( struct http_request *http, char *header ) {
  207. struct http_header_handler *handler;
  208. char *separator;
  209. char *value;
  210. int rc;
  211. /* An empty header line marks the transition to the data phase */
  212. if ( ! header[0] ) {
  213. DBGC ( http, "HTTP %p start of data\n", http );
  214. empty_line_buffer ( &http->linebuf );
  215. http->rx_state = HTTP_RX_DATA;
  216. return 0;
  217. }
  218. DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
  219. /* Split header at the ": " */
  220. separator = strstr ( header, ": " );
  221. if ( ! separator ) {
  222. DBGC ( http, "HTTP %p malformed header\n", http );
  223. return -EIO;
  224. }
  225. *separator = '\0';
  226. value = ( separator + 2 );
  227. /* Hand off to header handler, if one exists */
  228. for ( handler = http_header_handlers ; handler->header ; handler++ ) {
  229. if ( strcasecmp ( header, handler->header ) == 0 ) {
  230. if ( ( rc = handler->rx ( http, value ) ) != 0 )
  231. return rc;
  232. break;
  233. }
  234. }
  235. return 0;
  236. }
  237. /** An HTTP line-based data handler */
  238. struct http_line_handler {
  239. /** Handle line
  240. *
  241. * @v http HTTP request
  242. * @v line Line to handle
  243. * @ret rc Return status code
  244. */
  245. int ( * rx ) ( struct http_request *http, char *line );
  246. };
  247. /** List of HTTP line-based data handlers */
  248. static struct http_line_handler http_line_handlers[] = {
  249. [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
  250. [HTTP_RX_HEADER] = { .rx = http_rx_header },
  251. };
  252. /**
  253. * Handle new data arriving via HTTP connection in the data phase
  254. *
  255. * @v http HTTP request
  256. * @v iobuf I/O buffer
  257. * @ret rc Return status code
  258. */
  259. static int http_rx_data ( struct http_request *http,
  260. struct io_buffer *iobuf ) {
  261. int rc;
  262. /* Update received length */
  263. http->rx_len += iob_len ( iobuf );
  264. /* Hand off data buffer */
  265. if ( ( rc = xfer_deliver_iob ( &http->xfer, iobuf ) ) != 0 )
  266. return rc;
  267. /* If we have reached the content-length, stop now */
  268. if ( http->content_length &&
  269. ( http->rx_len >= http->content_length ) ) {
  270. http_done ( http, 0 );
  271. }
  272. return 0;
  273. }
  274. /**
  275. * Handle new data arriving via HTTP connection
  276. *
  277. * @v socket Transport layer interface
  278. * @v iobuf I/O buffer
  279. * @v meta Data transfer metadata, or NULL
  280. * @ret rc Return status code
  281. */
  282. static int http_socket_deliver_iob ( struct xfer_interface *socket,
  283. struct io_buffer *iobuf,
  284. struct xfer_metadata *meta __unused ) {
  285. struct http_request *http =
  286. container_of ( socket, struct http_request, socket );
  287. struct http_line_handler *lh;
  288. char *line;
  289. ssize_t len;
  290. int rc = 0;
  291. while ( iob_len ( iobuf ) ) {
  292. switch ( http->rx_state ) {
  293. case HTTP_RX_DEAD:
  294. /* Do no further processing */
  295. goto done;
  296. case HTTP_RX_DATA:
  297. /* Once we're into the data phase, just fill
  298. * the data buffer
  299. */
  300. rc = http_rx_data ( http, iobuf );
  301. iobuf = NULL;
  302. goto done;
  303. case HTTP_RX_RESPONSE:
  304. case HTTP_RX_HEADER:
  305. /* In the other phases, buffer and process a
  306. * line at a time
  307. */
  308. len = line_buffer ( &http->linebuf, iobuf->data,
  309. iob_len ( iobuf ) );
  310. if ( len < 0 ) {
  311. rc = len;
  312. DBGC ( http, "HTTP %p could not buffer line: "
  313. "%s\n", http, strerror ( rc ) );
  314. goto done;
  315. }
  316. iob_pull ( iobuf, len );
  317. line = buffered_line ( &http->linebuf );
  318. if ( line ) {
  319. lh = &http_line_handlers[http->rx_state];
  320. if ( ( rc = lh->rx ( http, line ) ) != 0 )
  321. goto done;
  322. }
  323. break;
  324. default:
  325. assert ( 0 );
  326. break;
  327. }
  328. }
  329. done:
  330. if ( rc )
  331. http_done ( http, rc );
  332. free_iob ( iobuf );
  333. return rc;
  334. }
  335. /**
  336. * HTTP process
  337. *
  338. * @v process Process
  339. */
  340. static void http_step ( struct process *process ) {
  341. struct http_request *http =
  342. container_of ( process, struct http_request, process );
  343. const char *path = http->uri->path;
  344. const char *host = http->uri->host;
  345. const char *query = http->uri->query;
  346. int rc;
  347. if ( xfer_window ( &http->socket ) ) {
  348. process_del ( &http->process );
  349. if ( ( rc = xfer_printf ( &http->socket,
  350. "GET %s%s%s HTTP/1.1\r\n"
  351. "User-Agent: gPXE/" VERSION "\r\n"
  352. "Host: %s\r\n"
  353. "\r\n",
  354. ( path ? path : "/" ),
  355. ( query ? "?" : "" ),
  356. ( query ? query : "" ),
  357. host ) ) != 0 ) {
  358. http_done ( http, rc );
  359. }
  360. }
  361. }
  362. /**
  363. * HTTP connection closed by network stack
  364. *
  365. * @v socket Transport layer interface
  366. * @v rc Reason for close
  367. */
  368. static void http_socket_close ( struct xfer_interface *socket, int rc ) {
  369. struct http_request *http =
  370. container_of ( socket, struct http_request, socket );
  371. DBGC ( http, "HTTP %p socket closed: %s\n",
  372. http, strerror ( rc ) );
  373. http_done ( http, rc );
  374. }
  375. /** HTTP socket operations */
  376. static struct xfer_interface_operations http_socket_operations = {
  377. .close = http_socket_close,
  378. .vredirect = xfer_vopen,
  379. .seek = ignore_xfer_seek,
  380. .window = unlimited_xfer_window,
  381. .alloc_iob = default_xfer_alloc_iob,
  382. .deliver_iob = http_socket_deliver_iob,
  383. .deliver_raw = xfer_deliver_as_iob,
  384. };
  385. /**
  386. * Close HTTP data transfer interface
  387. *
  388. * @v xfer Data transfer interface
  389. * @v rc Reason for close
  390. */
  391. static void http_xfer_close ( struct xfer_interface *xfer, int rc ) {
  392. struct http_request *http =
  393. container_of ( xfer, struct http_request, xfer );
  394. DBGC ( http, "HTTP %p interface closed: %s\n",
  395. http, strerror ( rc ) );
  396. http_done ( http, rc );
  397. }
  398. /** HTTP data transfer interface operations */
  399. static struct xfer_interface_operations http_xfer_operations = {
  400. .close = http_xfer_close,
  401. .vredirect = ignore_xfer_vredirect,
  402. .seek = ignore_xfer_seek,
  403. .window = unlimited_xfer_window,
  404. .alloc_iob = default_xfer_alloc_iob,
  405. .deliver_iob = xfer_deliver_as_raw,
  406. .deliver_raw = ignore_xfer_deliver_raw,
  407. };
  408. /**
  409. * Initiate an HTTP connection, with optional filter
  410. *
  411. * @v xfer Data transfer interface
  412. * @v uri Uniform Resource Identifier
  413. * @v default_port Default port number
  414. * @v filter Filter to apply to socket, or NULL
  415. * @ret rc Return status code
  416. */
  417. int http_open_filter ( struct xfer_interface *xfer, struct uri *uri,
  418. unsigned int default_port,
  419. int ( * filter ) ( struct xfer_interface *xfer,
  420. struct xfer_interface **next ) ) {
  421. struct http_request *http;
  422. struct sockaddr_tcpip server;
  423. struct xfer_interface *socket;
  424. int rc;
  425. /* Sanity checks */
  426. if ( ! uri->host )
  427. return -EINVAL;
  428. /* Allocate and populate HTTP structure */
  429. http = zalloc ( sizeof ( *http ) );
  430. if ( ! http )
  431. return -ENOMEM;
  432. http->refcnt.free = http_free;
  433. xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
  434. http->uri = uri_get ( uri );
  435. xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );
  436. process_init ( &http->process, http_step, &http->refcnt );
  437. /* Open socket */
  438. memset ( &server, 0, sizeof ( server ) );
  439. server.st_port = htons ( uri_port ( http->uri, default_port ) );
  440. socket = &http->socket;
  441. if ( filter ) {
  442. if ( ( rc = filter ( socket, &socket ) ) != 0 )
  443. goto err;
  444. }
  445. if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
  446. ( struct sockaddr * ) &server,
  447. uri->host, NULL ) ) != 0 )
  448. goto err;
  449. /* Attach to parent interface, mortalise self, and return */
  450. xfer_plug_plug ( &http->xfer, xfer );
  451. ref_put ( &http->refcnt );
  452. return 0;
  453. err:
  454. DBGC ( http, "HTTP %p could not create request: %s\n",
  455. http, strerror ( rc ) );
  456. http_done ( http, rc );
  457. ref_put ( &http->refcnt );
  458. return rc;
  459. }
  460. /**
  461. * Initiate an HTTP connection
  462. *
  463. * @v xfer Data transfer interface
  464. * @v uri Uniform Resource Identifier
  465. * @ret rc Return status code
  466. */
  467. static int http_open ( struct xfer_interface *xfer, struct uri *uri ) {
  468. return http_open_filter ( xfer, uri, HTTP_PORT, NULL );
  469. }
  470. /** HTTP URI opener */
  471. struct uri_opener http_uri_opener __uri_opener = {
  472. .scheme = "http",
  473. .open = http_open,
  474. };