Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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