You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.c 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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/features.h>
  42. #include <gpxe/http.h>
  43. FEATURE ( FEATURE_PROTOCOL, "HTTP", DHCP_EB_FEATURE_HTTP, 1 );
  44. /** HTTP receive state */
  45. enum http_rx_state {
  46. HTTP_RX_RESPONSE = 0,
  47. HTTP_RX_HEADER,
  48. HTTP_RX_DATA,
  49. HTTP_RX_DEAD,
  50. };
  51. /**
  52. * An HTTP request
  53. *
  54. */
  55. struct http_request {
  56. /** Reference count */
  57. struct refcnt refcnt;
  58. /** Data transfer interface */
  59. struct xfer_interface xfer;
  60. /** URI being fetched */
  61. struct uri *uri;
  62. /** Transport layer interface */
  63. struct xfer_interface socket;
  64. /** TX process */
  65. struct process process;
  66. /** HTTP response code */
  67. unsigned int response;
  68. /** HTTP Content-Length */
  69. size_t content_length;
  70. /** Received length */
  71. size_t rx_len;
  72. /** RX state */
  73. enum http_rx_state rx_state;
  74. /** Line buffer for received header lines */
  75. struct line_buffer linebuf;
  76. };
  77. /**
  78. * Free HTTP request
  79. *
  80. * @v refcnt Reference counter
  81. */
  82. static void http_free ( struct refcnt *refcnt ) {
  83. struct http_request *http =
  84. container_of ( refcnt, struct http_request, refcnt );
  85. uri_put ( http->uri );
  86. empty_line_buffer ( &http->linebuf );
  87. free ( http );
  88. };
  89. /**
  90. * Mark HTTP request as complete
  91. *
  92. * @v http HTTP request
  93. * @v rc Return status code
  94. */
  95. static void http_done ( struct http_request *http, int rc ) {
  96. /* Prevent further processing of any current packet */
  97. http->rx_state = HTTP_RX_DEAD;
  98. /* If we had a Content-Length, and the received content length
  99. * isn't correct, flag an error
  100. */
  101. if ( http->content_length &&
  102. ( http->content_length != http->rx_len ) ) {
  103. DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
  104. http, http->rx_len, http->content_length );
  105. rc = -EIO;
  106. }
  107. /* Remove process */
  108. process_del ( &http->process );
  109. /* Close all data transfer interfaces */
  110. xfer_nullify ( &http->socket );
  111. xfer_close ( &http->socket, rc );
  112. xfer_nullify ( &http->xfer );
  113. xfer_close ( &http->xfer, rc );
  114. }
  115. /**
  116. * Convert HTTP response code to return status code
  117. *
  118. * @v response HTTP response code
  119. * @ret rc Return status code
  120. */
  121. static int http_response_to_rc ( unsigned int response ) {
  122. switch ( response ) {
  123. case 200:
  124. return 0;
  125. case 404:
  126. return -ENOENT;
  127. case 403:
  128. return -EPERM;
  129. default:
  130. return -EIO;
  131. }
  132. }
  133. /**
  134. * Handle HTTP response
  135. *
  136. * @v http HTTP request
  137. * @v response HTTP response
  138. * @ret rc Return status code
  139. */
  140. static int http_rx_response ( struct http_request *http, char *response ) {
  141. char *spc;
  142. int rc;
  143. DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
  144. /* Check response starts with "HTTP/" */
  145. if ( strncmp ( response, "HTTP/", 5 ) != 0 )
  146. return -EIO;
  147. /* Locate and check response code */
  148. spc = strchr ( response, ' ' );
  149. if ( ! spc )
  150. return -EIO;
  151. http->response = strtoul ( spc, NULL, 10 );
  152. if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
  153. return rc;
  154. /* Move to received headers */
  155. http->rx_state = HTTP_RX_HEADER;
  156. return 0;
  157. }
  158. /**
  159. * Handle HTTP Content-Length header
  160. *
  161. * @v http HTTP request
  162. * @v value HTTP header value
  163. * @ret rc Return status code
  164. */
  165. static int http_rx_content_length ( struct http_request *http,
  166. const char *value ) {
  167. char *endp;
  168. http->content_length = strtoul ( value, &endp, 10 );
  169. if ( *endp != '\0' ) {
  170. DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
  171. http, value );
  172. return -EIO;
  173. }
  174. /* Use seek() to notify recipient of filesize */
  175. xfer_seek ( &http->xfer, http->content_length, SEEK_SET );
  176. xfer_seek ( &http->xfer, 0, SEEK_SET );
  177. return 0;
  178. }
  179. /** An HTTP header handler */
  180. struct http_header_handler {
  181. /** Name (e.g. "Content-Length") */
  182. const char *header;
  183. /** Handle received header
  184. *
  185. * @v http HTTP request
  186. * @v value HTTP header value
  187. * @ret rc Return status code
  188. *
  189. * If an error is returned, the download will be aborted.
  190. */
  191. int ( * rx ) ( struct http_request *http, const char *value );
  192. };
  193. /** List of HTTP header handlers */
  194. static struct http_header_handler http_header_handlers[] = {
  195. {
  196. .header = "Content-Length",
  197. .rx = http_rx_content_length,
  198. },
  199. { NULL, NULL }
  200. };
  201. /**
  202. * Handle HTTP header
  203. *
  204. * @v http HTTP request
  205. * @v header HTTP header
  206. * @ret rc Return status code
  207. */
  208. static int http_rx_header ( struct http_request *http, char *header ) {
  209. struct http_header_handler *handler;
  210. char *separator;
  211. char *value;
  212. int rc;
  213. /* An empty header line marks the transition to the data phase */
  214. if ( ! header[0] ) {
  215. DBGC ( http, "HTTP %p start of data\n", http );
  216. empty_line_buffer ( &http->linebuf );
  217. http->rx_state = HTTP_RX_DATA;
  218. return 0;
  219. }
  220. DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
  221. /* Split header at the ": " */
  222. separator = strstr ( header, ": " );
  223. if ( ! separator ) {
  224. DBGC ( http, "HTTP %p malformed header\n", http );
  225. return -EIO;
  226. }
  227. *separator = '\0';
  228. value = ( separator + 2 );
  229. /* Hand off to header handler, if one exists */
  230. for ( handler = http_header_handlers ; handler->header ; handler++ ) {
  231. if ( strcasecmp ( header, handler->header ) == 0 ) {
  232. if ( ( rc = handler->rx ( http, value ) ) != 0 )
  233. return rc;
  234. break;
  235. }
  236. }
  237. return 0;
  238. }
  239. /** An HTTP line-based data handler */
  240. struct http_line_handler {
  241. /** Handle line
  242. *
  243. * @v http HTTP request
  244. * @v line Line to handle
  245. * @ret rc Return status code
  246. */
  247. int ( * rx ) ( struct http_request *http, char *line );
  248. };
  249. /** List of HTTP line-based data handlers */
  250. static struct http_line_handler http_line_handlers[] = {
  251. [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
  252. [HTTP_RX_HEADER] = { .rx = http_rx_header },
  253. };
  254. /**
  255. * Handle new data arriving via HTTP connection in the data phase
  256. *
  257. * @v http HTTP request
  258. * @v iobuf I/O buffer
  259. * @ret rc Return status code
  260. */
  261. static int http_rx_data ( struct http_request *http,
  262. struct io_buffer *iobuf ) {
  263. int rc;
  264. /* Update received length */
  265. http->rx_len += iob_len ( iobuf );
  266. /* Hand off data buffer */
  267. if ( ( rc = xfer_deliver_iob ( &http->xfer, iobuf ) ) != 0 )
  268. return rc;
  269. /* If we have reached the content-length, stop now */
  270. if ( http->content_length &&
  271. ( http->rx_len >= http->content_length ) ) {
  272. http_done ( http, 0 );
  273. }
  274. return 0;
  275. }
  276. /**
  277. * Handle new data arriving via HTTP connection
  278. *
  279. * @v socket Transport layer interface
  280. * @v iobuf I/O buffer
  281. * @v meta Data transfer metadata, or NULL
  282. * @ret rc Return status code
  283. */
  284. static int http_socket_deliver_iob ( struct xfer_interface *socket,
  285. struct io_buffer *iobuf,
  286. struct xfer_metadata *meta __unused ) {
  287. struct http_request *http =
  288. container_of ( socket, struct http_request, socket );
  289. struct http_line_handler *lh;
  290. char *line;
  291. ssize_t len;
  292. int rc = 0;
  293. while ( iob_len ( iobuf ) ) {
  294. switch ( http->rx_state ) {
  295. case HTTP_RX_DEAD:
  296. /* Do no further processing */
  297. goto done;
  298. case HTTP_RX_DATA:
  299. /* Once we're into the data phase, just fill
  300. * the data buffer
  301. */
  302. rc = http_rx_data ( http, iobuf );
  303. iobuf = NULL;
  304. goto done;
  305. case HTTP_RX_RESPONSE:
  306. case HTTP_RX_HEADER:
  307. /* In the other phases, buffer and process a
  308. * line at a time
  309. */
  310. len = line_buffer ( &http->linebuf, iobuf->data,
  311. iob_len ( iobuf ) );
  312. if ( len < 0 ) {
  313. rc = len;
  314. DBGC ( http, "HTTP %p could not buffer line: "
  315. "%s\n", http, strerror ( rc ) );
  316. goto done;
  317. }
  318. iob_pull ( iobuf, len );
  319. line = buffered_line ( &http->linebuf );
  320. if ( line ) {
  321. lh = &http_line_handlers[http->rx_state];
  322. if ( ( rc = lh->rx ( http, line ) ) != 0 )
  323. goto done;
  324. }
  325. break;
  326. default:
  327. assert ( 0 );
  328. break;
  329. }
  330. }
  331. done:
  332. if ( rc )
  333. http_done ( http, rc );
  334. free_iob ( iobuf );
  335. return rc;
  336. }
  337. /**
  338. * HTTP process
  339. *
  340. * @v process Process
  341. */
  342. static void http_step ( struct process *process ) {
  343. struct http_request *http =
  344. container_of ( process, struct http_request, process );
  345. const char *path = http->uri->path;
  346. const char *host = http->uri->host;
  347. const char *query = http->uri->query;
  348. int rc;
  349. if ( xfer_window ( &http->socket ) ) {
  350. process_del ( &http->process );
  351. if ( ( rc = xfer_printf ( &http->socket,
  352. "GET %s%s%s HTTP/1.0\r\n"
  353. "User-Agent: gPXE/" VERSION "\r\n"
  354. "Host: %s\r\n"
  355. "\r\n",
  356. ( path ? path : "/" ),
  357. ( query ? "?" : "" ),
  358. ( query ? query : "" ),
  359. host ) ) != 0 ) {
  360. http_done ( http, rc );
  361. }
  362. }
  363. }
  364. /**
  365. * HTTP connection closed by network stack
  366. *
  367. * @v socket Transport layer interface
  368. * @v rc Reason for close
  369. */
  370. static void http_socket_close ( struct xfer_interface *socket, int rc ) {
  371. struct http_request *http =
  372. container_of ( socket, struct http_request, socket );
  373. DBGC ( http, "HTTP %p socket closed: %s\n",
  374. http, strerror ( rc ) );
  375. http_done ( http, rc );
  376. }
  377. /** HTTP socket operations */
  378. static struct xfer_interface_operations http_socket_operations = {
  379. .close = http_socket_close,
  380. .vredirect = xfer_vopen,
  381. .window = unlimited_xfer_window,
  382. .alloc_iob = default_xfer_alloc_iob,
  383. .deliver_iob = http_socket_deliver_iob,
  384. .deliver_raw = xfer_deliver_as_iob,
  385. };
  386. /**
  387. * Close HTTP data transfer interface
  388. *
  389. * @v xfer Data transfer interface
  390. * @v rc Reason for close
  391. */
  392. static void http_xfer_close ( struct xfer_interface *xfer, int rc ) {
  393. struct http_request *http =
  394. container_of ( xfer, struct http_request, xfer );
  395. DBGC ( http, "HTTP %p interface closed: %s\n",
  396. http, strerror ( rc ) );
  397. http_done ( http, rc );
  398. }
  399. /** HTTP data transfer interface operations */
  400. static struct xfer_interface_operations http_xfer_operations = {
  401. .close = http_xfer_close,
  402. .vredirect = ignore_xfer_vredirect,
  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. };