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.

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