Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

http.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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 *host = http->uri->host;
  374. const char *user = http->uri->user;
  375. const char *password =
  376. ( http->uri->password ? http->uri->password : "" );
  377. size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
  378. strlen ( password ) ) : 0 );
  379. size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
  380. char user_pw[ user_pw_len + 1 /* NUL */ ];
  381. char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
  382. int rc;
  383. int request_len = unparse_uri ( NULL, 0, http->uri,
  384. URI_PATH_BIT | URI_QUERY_BIT );
  385. if ( xfer_window ( &http->socket ) ) {
  386. char request[request_len + 1];
  387. /* Construct path?query request */
  388. unparse_uri ( request, sizeof ( request ), http->uri,
  389. URI_PATH_BIT | URI_QUERY_BIT );
  390. /* We want to execute only once */
  391. process_del ( &http->process );
  392. /* Construct authorisation, if applicable */
  393. if ( user ) {
  394. /* Make "user:password" string from decoded fields */
  395. snprintf ( user_pw, sizeof ( user_pw ), "%s:%s",
  396. user, password );
  397. /* Base64-encode the "user:password" string */
  398. base64_encode ( user_pw, user_pw_base64 );
  399. }
  400. /* Send GET request */
  401. if ( ( rc = xfer_printf ( &http->socket,
  402. "GET %s HTTP/1.0\r\n"
  403. "User-Agent: gPXE/" VERSION "\r\n"
  404. "%s%s%s"
  405. "Host: %s\r\n"
  406. "\r\n",
  407. request,
  408. ( user ?
  409. "Authorization: Basic " : "" ),
  410. ( user ? user_pw_base64 : "" ),
  411. ( user ? "\r\n" : "" ),
  412. host ) ) != 0 ) {
  413. http_done ( http, rc );
  414. }
  415. }
  416. }
  417. /**
  418. * HTTP connection closed by network stack
  419. *
  420. * @v socket Transport layer interface
  421. * @v rc Reason for close
  422. */
  423. static void http_socket_close ( struct xfer_interface *socket, int rc ) {
  424. struct http_request *http =
  425. container_of ( socket, struct http_request, socket );
  426. DBGC ( http, "HTTP %p socket closed: %s\n",
  427. http, strerror ( rc ) );
  428. http_done ( http, rc );
  429. }
  430. /** HTTP socket operations */
  431. static struct xfer_interface_operations http_socket_operations = {
  432. .close = http_socket_close,
  433. .vredirect = xfer_vreopen,
  434. .window = unlimited_xfer_window,
  435. .alloc_iob = default_xfer_alloc_iob,
  436. .deliver_iob = http_socket_deliver_iob,
  437. .deliver_raw = xfer_deliver_as_iob,
  438. };
  439. /**
  440. * Close HTTP data transfer interface
  441. *
  442. * @v xfer Data transfer interface
  443. * @v rc Reason for close
  444. */
  445. static void http_xfer_close ( struct xfer_interface *xfer, int rc ) {
  446. struct http_request *http =
  447. container_of ( xfer, struct http_request, xfer );
  448. DBGC ( http, "HTTP %p interface closed: %s\n",
  449. http, strerror ( rc ) );
  450. http_done ( http, rc );
  451. }
  452. /** HTTP data transfer interface operations */
  453. static struct xfer_interface_operations http_xfer_operations = {
  454. .close = http_xfer_close,
  455. .vredirect = ignore_xfer_vredirect,
  456. .window = unlimited_xfer_window,
  457. .alloc_iob = default_xfer_alloc_iob,
  458. .deliver_iob = xfer_deliver_as_raw,
  459. .deliver_raw = ignore_xfer_deliver_raw,
  460. };
  461. /**
  462. * Initiate an HTTP connection, with optional filter
  463. *
  464. * @v xfer Data transfer interface
  465. * @v uri Uniform Resource Identifier
  466. * @v default_port Default port number
  467. * @v filter Filter to apply to socket, or NULL
  468. * @ret rc Return status code
  469. */
  470. int http_open_filter ( struct xfer_interface *xfer, struct uri *uri,
  471. unsigned int default_port,
  472. int ( * filter ) ( struct xfer_interface *xfer,
  473. struct xfer_interface **next ) ) {
  474. struct http_request *http;
  475. struct sockaddr_tcpip server;
  476. struct xfer_interface *socket;
  477. int rc;
  478. /* Sanity checks */
  479. if ( ! uri->host )
  480. return -EINVAL;
  481. /* Allocate and populate HTTP structure */
  482. http = zalloc ( sizeof ( *http ) );
  483. if ( ! http )
  484. return -ENOMEM;
  485. http->refcnt.free = http_free;
  486. xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
  487. http->uri = uri_get ( uri );
  488. xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );
  489. process_init ( &http->process, http_step, &http->refcnt );
  490. /* Open socket */
  491. memset ( &server, 0, sizeof ( server ) );
  492. server.st_port = htons ( uri_port ( http->uri, default_port ) );
  493. socket = &http->socket;
  494. if ( filter ) {
  495. if ( ( rc = filter ( socket, &socket ) ) != 0 )
  496. goto err;
  497. }
  498. if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
  499. ( struct sockaddr * ) &server,
  500. uri->host, NULL ) ) != 0 )
  501. goto err;
  502. /* Attach to parent interface, mortalise self, and return */
  503. xfer_plug_plug ( &http->xfer, xfer );
  504. ref_put ( &http->refcnt );
  505. return 0;
  506. err:
  507. DBGC ( http, "HTTP %p could not create request: %s\n",
  508. http, strerror ( rc ) );
  509. http_done ( http, rc );
  510. ref_put ( &http->refcnt );
  511. return rc;
  512. }
  513. /**
  514. * Initiate an HTTP connection
  515. *
  516. * @v xfer Data transfer interface
  517. * @v uri Uniform Resource Identifier
  518. * @ret rc Return status code
  519. */
  520. static int http_open ( struct xfer_interface *xfer, struct uri *uri ) {
  521. return http_open_filter ( xfer, uri, HTTP_PORT, NULL );
  522. }
  523. /** HTTP URI opener */
  524. struct uri_opener http_uri_opener __uri_opener = {
  525. .scheme = "http",
  526. .open = http_open,
  527. };