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 14KB

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