2 * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
25 * Hyper Text Transfer Protocol (HTTP) core functionality
38 #include <ipxe/refcnt.h>
39 #include <ipxe/iobuf.h>
40 #include <ipxe/xfer.h>
41 #include <ipxe/open.h>
42 #include <ipxe/socket.h>
43 #include <ipxe/tcpip.h>
44 #include <ipxe/process.h>
45 #include <ipxe/linebuf.h>
46 #include <ipxe/base64.h>
47 #include <ipxe/base16.h>
49 #include <ipxe/blockdev.h>
50 #include <ipxe/acpi.h>
51 #include <ipxe/version.h>
52 #include <ipxe/http.h>
54 /* Disambiguate the various error causes */
55 #define EACCES_401 __einfo_error ( EINFO_EACCES_401 )
56 #define EINFO_EACCES_401 \
57 __einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )
58 #define EIO_OTHER __einfo_error ( EINFO_EIO_OTHER )
59 #define EINFO_EIO_OTHER \
60 __einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )
61 #define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
62 #define EINFO_EIO_CONTENT_LENGTH \
63 __einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
64 #define EINVAL_RESPONSE __einfo_error ( EINFO_EINVAL_RESPONSE )
65 #define EINFO_EINVAL_RESPONSE \
66 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid content length" )
67 #define EINVAL_HEADER __einfo_error ( EINFO_EINVAL_HEADER )
68 #define EINFO_EINVAL_HEADER \
69 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid header" )
70 #define EINVAL_CONTENT_LENGTH __einfo_error ( EINFO_EINVAL_CONTENT_LENGTH )
71 #define EINFO_EINVAL_CONTENT_LENGTH \
72 __einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid content length" )
73 #define EINVAL_CHUNK_LENGTH __einfo_error ( EINFO_EINVAL_CHUNK_LENGTH )
74 #define EINFO_EINVAL_CHUNK_LENGTH \
75 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid chunk length" )
76 #define ENOENT_404 __einfo_error ( EINFO_ENOENT_404 )
77 #define EINFO_ENOENT_404 \
78 __einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )
79 #define EPERM_403 __einfo_error ( EINFO_EPERM_403 )
80 #define EINFO_EPERM_403 \
81 __einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )
82 #define EPROTO_UNSOLICITED __einfo_error ( EINFO_EPROTO_UNSOLICITED )
83 #define EINFO_EPROTO_UNSOLICITED \
84 __einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )
86 /** Block size used for HTTP block device request */
87 #define HTTP_BLKSIZE 512
91 /** Request is waiting to be transmitted */
92 HTTP_TX_PENDING = 0x0001,
93 /** Fetch header only */
94 HTTP_HEAD_ONLY = 0x0002,
95 /** Client would like to keep connection alive */
96 HTTP_CLIENT_KEEPALIVE = 0x0004,
97 /** Server will keep connection alive */
98 HTTP_SERVER_KEEPALIVE = 0x0008,
99 /** Discard the current request and try again */
100 HTTP_TRY_AGAIN = 0x0010,
101 /** Provide Basic authentication details */
102 HTTP_BASIC_AUTH = 0x0020,
103 /** Provide Digest authentication details */
104 HTTP_DIGEST_AUTH = 0x0040,
107 /** HTTP receive state */
109 HTTP_RX_RESPONSE = 0,
112 /* In HTTP_RX_DATA, it is acceptable for the server to close
113 * the connection (unless we are in the middle of a chunked
117 /* In the following states, it is acceptable for the server to
118 * close the connection.
129 struct http_request {
130 /** Reference count */
131 struct refcnt refcnt;
132 /** Data transfer interface */
133 struct interface xfer;
134 /** Partial transfer interface */
135 struct interface partial;
137 /** URI being fetched */
140 unsigned int default_port;
141 /** Filter (if any) */
142 int ( * filter ) ( struct interface *xfer,
144 struct interface **next );
145 /** Transport layer interface */
146 struct interface socket;
150 /** Starting offset of partial transfer (if applicable) */
151 size_t partial_start;
152 /** Length of partial transfer (if applicable) */
156 struct process process;
159 enum http_rx_state rx_state;
162 /** Received length */
164 /** Length remaining (or 0 if unknown) */
166 /** HTTP is using Transfer-Encoding: chunked */
168 /** Current chunk length remaining (if applicable) */
169 size_t chunk_remaining;
170 /** Line buffer for received header lines */
171 struct line_buffer linebuf;
172 /** Receive data buffer (if applicable) */
175 /** Authentication realm (if any) */
177 /** Authentication nonce (if any) */
179 /** Authentication opaque string (if any) */
186 * @v refcnt Reference counter
188 static void http_free ( struct refcnt *refcnt ) {
189 struct http_request *http =
190 container_of ( refcnt, struct http_request, refcnt );
192 uri_put ( http->uri );
193 empty_line_buffer ( &http->linebuf );
194 free ( http->auth_realm );
195 free ( http->auth_nonce );
196 free ( http->auth_opaque );
203 * @v http HTTP request
204 * @v rc Return status code
206 static void http_close ( struct http_request *http, int rc ) {
208 /* Prevent further processing of any current packet */
209 http->rx_state = HTTP_RX_DEAD;
211 /* Prevent reconnection */
212 http->flags &= ~HTTP_CLIENT_KEEPALIVE;
215 process_del ( &http->process );
217 /* Close all data transfer interfaces */
218 intf_shutdown ( &http->socket, rc );
219 intf_shutdown ( &http->partial, rc );
220 intf_shutdown ( &http->xfer, rc );
226 * @v http HTTP request
227 * @ret rc Return status code
229 static int http_socket_open ( struct http_request *http ) {
230 struct uri *uri = http->uri;
231 struct sockaddr_tcpip server;
232 struct interface *socket;
236 memset ( &server, 0, sizeof ( server ) );
237 server.st_port = htons ( uri_port ( uri, http->default_port ) );
238 socket = &http->socket;
239 if ( http->filter ) {
240 if ( ( rc = http->filter ( socket, uri->host, &socket ) ) != 0 )
243 if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
244 ( struct sockaddr * ) &server,
245 uri->host, NULL ) ) != 0 )
252 * Mark HTTP request as completed successfully
254 * @v http HTTP request
256 static void http_done ( struct http_request *http ) {
259 /* If we are not at an appropriate stage of the protocol
260 * (including being in the middle of a chunked transfer),
263 if ( ( http->rx_state < HTTP_RX_DATA ) || ( http->chunked != 0 ) ) {
264 DBGC ( http, "HTTP %p connection closed unexpectedly in state "
265 "%d\n", http, http->rx_state );
266 http_close ( http, -ECONNRESET );
270 /* If we had a Content-Length, and the received content length
271 * isn't correct, force an error
273 if ( http->remaining != 0 ) {
274 DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
275 http, http->rx_len, ( http->rx_len + http->remaining ) );
276 http_close ( http, -EIO_CONTENT_LENGTH );
280 /* Enter idle state */
281 http->rx_state = HTTP_RX_IDLE;
283 assert ( http->remaining == 0 );
284 assert ( http->chunked == 0 );
285 assert ( http->chunk_remaining == 0 );
287 /* Close partial transfer interface */
288 if ( ! ( http->flags & HTTP_TRY_AGAIN ) )
289 intf_restart ( &http->partial, 0 );
291 /* Close everything unless we want to keep the connection alive */
292 if ( ! ( http->flags & ( HTTP_CLIENT_KEEPALIVE | HTTP_TRY_AGAIN ) ) ) {
293 http_close ( http, 0 );
297 /* If the server is not intending to keep the connection
298 * alive, then reopen the socket.
300 if ( ! ( http->flags & HTTP_SERVER_KEEPALIVE ) ) {
301 DBGC ( http, "HTTP %p reopening connection\n", http );
302 intf_restart ( &http->socket, 0 );
303 if ( ( rc = http_socket_open ( http ) ) != 0 ) {
304 http_close ( http, rc );
308 http->flags &= ~HTTP_SERVER_KEEPALIVE;
310 /* Retry the request if applicable */
311 if ( http->flags & HTTP_TRY_AGAIN ) {
312 http->flags &= ~HTTP_TRY_AGAIN;
313 http->flags |= HTTP_TX_PENDING;
314 http->rx_state = HTTP_RX_RESPONSE;
315 process_add ( &http->process );
320 * Convert HTTP response code to return status code
322 * @v response HTTP response code
323 * @ret rc Return status code
325 static int http_response_to_rc ( unsigned int response ) {
326 switch ( response ) {
345 * Handle HTTP response
347 * @v http HTTP request
348 * @v response HTTP response
349 * @ret rc Return status code
351 static int http_rx_response ( struct http_request *http, char *response ) {
354 DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
356 /* Check response starts with "HTTP/" */
357 if ( strncmp ( response, "HTTP/", 5 ) != 0 )
358 return -EINVAL_RESPONSE;
360 /* Locate and store response code */
361 spc = strchr ( response, ' ' );
363 return -EINVAL_RESPONSE;
364 http->code = strtoul ( spc, NULL, 10 );
366 /* Move to receive headers */
367 http->rx_state = ( ( http->flags & HTTP_HEAD_ONLY ) ?
368 HTTP_RX_TRAILER : HTTP_RX_HEADER );
373 * Handle HTTP Location header
375 * @v http HTTP request
376 * @v value HTTP header value
377 * @ret rc Return status code
379 static int http_rx_location ( struct http_request *http, char *value ) {
382 /* Redirect to new location */
383 DBGC ( http, "HTTP %p redirecting to %s\n", http, value );
384 if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI_STRING,
386 DBGC ( http, "HTTP %p could not redirect: %s\n",
387 http, strerror ( rc ) );
395 * Handle HTTP Content-Length header
397 * @v http HTTP request
398 * @v value HTTP header value
399 * @ret rc Return status code
401 static int http_rx_content_length ( struct http_request *http, char *value ) {
402 struct block_device_capacity capacity;
406 /* Parse content length */
407 content_len = strtoul ( value, &endp, 10 );
408 if ( *endp != '\0' ) {
409 DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
411 return -EINVAL_CONTENT_LENGTH;
414 /* If we already have an expected content length, and this
415 * isn't it, then complain
417 if ( http->remaining && ( http->remaining != content_len ) ) {
418 DBGC ( http, "HTTP %p incorrect Content-Length %zd (expected "
419 "%zd)\n", http, content_len, http->remaining );
420 return -EIO_CONTENT_LENGTH;
422 if ( ! ( http->flags & HTTP_HEAD_ONLY ) )
423 http->remaining = content_len;
425 /* Do nothing more if we are retrying the request */
426 if ( http->flags & HTTP_TRY_AGAIN )
429 /* Use seek() to notify recipient of filesize */
430 xfer_seek ( &http->xfer, http->remaining );
431 xfer_seek ( &http->xfer, 0 );
433 /* Report block device capacity if applicable */
434 if ( http->flags & HTTP_HEAD_ONLY ) {
435 capacity.blocks = ( content_len / HTTP_BLKSIZE );
436 capacity.blksize = HTTP_BLKSIZE;
437 capacity.max_count = -1U;
438 block_capacity ( &http->partial, &capacity );
444 * Handle HTTP Transfer-Encoding header
446 * @v http HTTP request
447 * @v value HTTP header value
448 * @ret rc Return status code
450 static int http_rx_transfer_encoding ( struct http_request *http, char *value ){
452 if ( strcasecmp ( value, "chunked" ) == 0 ) {
453 /* Mark connection as using chunked transfer encoding */
461 * Handle HTTP Connection header
463 * @v http HTTP request
464 * @v value HTTP header value
465 * @ret rc Return status code
467 static int http_rx_connection ( struct http_request *http, char *value ) {
469 if ( strcasecmp ( value, "keep-alive" ) == 0 ) {
470 /* Mark connection as being kept alive by the server */
471 http->flags |= HTTP_SERVER_KEEPALIVE;
478 * Handle WWW-Authenticate Basic header
480 * @v http HTTP request
481 * @v params Parameters
482 * @ret rc Return status code
484 static int http_rx_basic_auth ( struct http_request *http, char *params ) {
486 DBGC ( http, "HTTP %p Basic authentication required (%s)\n",
489 /* If we received a 401 Unauthorized response, then retry
490 * using Basic authentication
492 if ( ( http->code == 401 ) &&
493 ( ! ( http->flags & HTTP_BASIC_AUTH ) ) &&
494 ( http->uri->user != NULL ) ) {
495 http->flags |= ( HTTP_TRY_AGAIN | HTTP_BASIC_AUTH );
502 * Parse Digest authentication parameter
504 * @v params Parameters
505 * @v name Parameter name (including trailing "=\"")
506 * @ret value Parameter value, or NULL
508 static char * http_digest_param ( char *params, const char *name ) {
513 /* Locate parameter */
514 key = strstr ( params, name );
519 value = ( key + strlen ( name ) );
520 terminator = strchr ( value, '"' );
523 return strndup ( value, ( terminator - value ) );
527 * Handle WWW-Authenticate Digest header
529 * @v http HTTP request
530 * @v params Parameters
531 * @ret rc Return status code
533 static int http_rx_digest_auth ( struct http_request *http, char *params ) {
535 DBGC ( http, "HTTP %p Digest authentication required (%s)\n",
538 /* If we received a 401 Unauthorized response, then retry
539 * using Digest authentication
541 if ( ( http->code == 401 ) &&
542 ( ! ( http->flags & HTTP_DIGEST_AUTH ) ) &&
543 ( http->uri->user != NULL ) ) {
546 free ( http->auth_realm );
547 http->auth_realm = http_digest_param ( params, "realm=\"" );
548 if ( ! http->auth_realm ) {
549 DBGC ( http, "HTTP %p Digest prompt missing realm\n",
551 return -EINVAL_HEADER;
555 free ( http->auth_nonce );
556 http->auth_nonce = http_digest_param ( params, "nonce=\"" );
557 if ( ! http->auth_nonce ) {
558 DBGC ( http, "HTTP %p Digest prompt missing nonce\n",
560 return -EINVAL_HEADER;
564 free ( http->auth_opaque );
565 http->auth_opaque = http_digest_param ( params, "opaque=\"" );
566 if ( ! http->auth_opaque ) {
567 /* Not an error; "opaque" is optional */
570 http->flags |= ( HTTP_TRY_AGAIN | HTTP_DIGEST_AUTH );
576 /** An HTTP WWW-Authenticate header handler */
577 struct http_auth_header_handler {
578 /** Scheme (e.g. "Basic") */
580 /** Handle received parameters
582 * @v http HTTP request
583 * @v params Parameters
584 * @ret rc Return status code
586 int ( * rx ) ( struct http_request *http, char *params );
589 /** List of HTTP WWW-Authenticate header handlers */
590 static struct http_auth_header_handler http_auth_header_handlers[] = {
593 .rx = http_rx_basic_auth,
597 .rx = http_rx_digest_auth,
603 * Handle HTTP WWW-Authenticate header
605 * @v http HTTP request
606 * @v value HTTP header value
607 * @ret rc Return status code
609 static int http_rx_www_authenticate ( struct http_request *http, char *value ) {
610 struct http_auth_header_handler *handler;
617 separator = strchr ( value, ' ' );
619 DBGC ( http, "HTTP %p malformed WWW-Authenticate header\n",
621 return -EINVAL_HEADER;
625 params = ( separator + 1 );
627 /* Hand off to header handler, if one exists */
628 for ( handler = http_auth_header_handlers; handler->scheme; handler++ ){
629 if ( strcasecmp ( scheme, handler->scheme ) == 0 ) {
630 if ( ( rc = handler->rx ( http, params ) ) != 0 )
638 /** An HTTP header handler */
639 struct http_header_handler {
640 /** Name (e.g. "Content-Length") */
642 /** Handle received header
644 * @v http HTTP request
645 * @v value HTTP header value
646 * @ret rc Return status code
648 * If an error is returned, the download will be aborted.
650 int ( * rx ) ( struct http_request *http, char *value );
653 /** List of HTTP header handlers */
654 static struct http_header_handler http_header_handlers[] = {
656 .header = "Location",
657 .rx = http_rx_location,
660 .header = "Content-Length",
661 .rx = http_rx_content_length,
664 .header = "Transfer-Encoding",
665 .rx = http_rx_transfer_encoding,
668 .header = "Connection",
669 .rx = http_rx_connection,
672 .header = "WWW-Authenticate",
673 .rx = http_rx_www_authenticate,
681 * @v http HTTP request
682 * @v header HTTP header
683 * @ret rc Return status code
685 static int http_rx_header ( struct http_request *http, char *header ) {
686 struct http_header_handler *handler;
691 /* An empty header line marks the end of this phase */
693 empty_line_buffer ( &http->linebuf );
695 /* Handle response code */
696 if ( ! ( http->flags & HTTP_TRY_AGAIN ) ) {
697 if ( ( rc = http_response_to_rc ( http->code ) ) != 0 )
701 /* Move to next state */
702 if ( http->rx_state == HTTP_RX_HEADER ) {
703 DBGC ( http, "HTTP %p start of data\n", http );
704 http->rx_state = ( http->chunked ?
705 HTTP_RX_CHUNK_LEN : HTTP_RX_DATA );
706 if ( ( http->partial_len != 0 ) &&
707 ( ! ( http->flags & HTTP_TRY_AGAIN ) ) ) {
708 http->remaining = http->partial_len;
712 DBGC ( http, "HTTP %p end of trailer\n", http );
718 DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
720 /* Split header at the ": " */
721 separator = strstr ( header, ": " );
723 DBGC ( http, "HTTP %p malformed header\n", http );
724 return -EINVAL_HEADER;
727 value = ( separator + 2 );
729 /* Hand off to header handler, if one exists */
730 for ( handler = http_header_handlers ; handler->header ; handler++ ) {
731 if ( strcasecmp ( header, handler->header ) == 0 ) {
732 if ( ( rc = handler->rx ( http, value ) ) != 0 )
741 * Handle HTTP chunk length
743 * @v http HTTP request
744 * @v length HTTP chunk length
745 * @ret rc Return status code
747 static int http_rx_chunk_len ( struct http_request *http, char *length ) {
750 /* Skip blank lines between chunks */
751 if ( length[0] == '\0' )
754 /* Parse chunk length */
755 http->chunk_remaining = strtoul ( length, &endp, 16 );
756 if ( *endp != '\0' ) {
757 DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
759 return -EINVAL_CHUNK_LENGTH;
762 /* Terminate chunked encoding if applicable */
763 if ( http->chunk_remaining == 0 ) {
764 DBGC ( http, "HTTP %p end of chunks\n", http );
766 http->rx_state = HTTP_RX_TRAILER;
770 /* Use seek() to notify recipient of new filesize */
771 DBGC ( http, "HTTP %p start of chunk of length %zd\n",
772 http, http->chunk_remaining );
773 if ( ! ( http->flags & HTTP_TRY_AGAIN ) ) {
774 xfer_seek ( &http->xfer,
775 ( http->rx_len + http->chunk_remaining ) );
776 xfer_seek ( &http->xfer, http->rx_len );
779 /* Start receiving data */
780 http->rx_state = HTTP_RX_DATA;
785 /** An HTTP line-based data handler */
786 struct http_line_handler {
789 * @v http HTTP request
790 * @v line Line to handle
791 * @ret rc Return status code
793 int ( * rx ) ( struct http_request *http, char *line );
796 /** List of HTTP line-based data handlers */
797 static struct http_line_handler http_line_handlers[] = {
798 [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
799 [HTTP_RX_HEADER] = { .rx = http_rx_header },
800 [HTTP_RX_CHUNK_LEN] = { .rx = http_rx_chunk_len },
801 [HTTP_RX_TRAILER] = { .rx = http_rx_header },
805 * Handle new data arriving via HTTP connection
807 * @v http HTTP request
808 * @v iobuf I/O buffer
809 * @v meta Data transfer metadata
810 * @ret rc Return status code
812 static int http_socket_deliver ( struct http_request *http,
813 struct io_buffer *iobuf,
814 struct xfer_metadata *meta __unused ) {
815 struct http_line_handler *lh;
821 while ( iobuf && iob_len ( iobuf ) ) {
823 switch ( http->rx_state ) {
825 /* Receiving any data in this state is an error */
826 DBGC ( http, "HTTP %p received %zd bytes while %s\n",
827 http, iob_len ( iobuf ),
828 ( ( http->rx_state == HTTP_RX_IDLE ) ?
830 rc = -EPROTO_UNSOLICITED;
833 /* Do no further processing */
836 /* Pass received data to caller */
837 data_len = iob_len ( iobuf );
838 if ( http->chunk_remaining &&
839 ( http->chunk_remaining < data_len ) ) {
840 data_len = http->chunk_remaining;
842 if ( http->remaining &&
843 ( http->remaining < data_len ) ) {
844 data_len = http->remaining;
846 if ( http->flags & HTTP_TRY_AGAIN ) {
847 /* Discard all received data */
848 iob_pull ( iobuf, data_len );
849 } else if ( http->rx_buffer != UNULL ) {
850 /* Copy to partial transfer buffer */
851 copy_to_user ( http->rx_buffer, http->rx_len,
852 iobuf->data, data_len );
853 iob_pull ( iobuf, data_len );
854 } else if ( data_len < iob_len ( iobuf ) ) {
855 /* Deliver partial buffer as raw data */
856 rc = xfer_deliver_raw ( &http->xfer,
857 iobuf->data, data_len );
858 iob_pull ( iobuf, data_len );
862 /* Deliver whole I/O buffer */
863 if ( ( rc = xfer_deliver_iob ( &http->xfer,
864 iob_disown ( iobuf ) ) ) != 0 )
867 http->rx_len += data_len;
868 if ( http->chunk_remaining ) {
869 http->chunk_remaining -= data_len;
870 if ( http->chunk_remaining == 0 )
871 http->rx_state = HTTP_RX_CHUNK_LEN;
873 if ( http->remaining ) {
874 http->remaining -= data_len;
875 if ( ( http->remaining == 0 ) &&
876 ( http->rx_state == HTTP_RX_DATA ) ) {
881 case HTTP_RX_RESPONSE:
883 case HTTP_RX_CHUNK_LEN:
884 case HTTP_RX_TRAILER:
885 /* In the other phases, buffer and process a
888 line_len = line_buffer ( &http->linebuf, iobuf->data,
890 if ( line_len < 0 ) {
892 DBGC ( http, "HTTP %p could not buffer line: "
893 "%s\n", http, strerror ( rc ) );
896 iob_pull ( iobuf, line_len );
897 line = buffered_line ( &http->linebuf );
899 lh = &http_line_handlers[http->rx_state];
900 if ( ( rc = lh->rx ( http, line ) ) != 0 )
912 http_close ( http, rc );
918 * Check HTTP socket flow control window
920 * @v http HTTP request
921 * @ret len Length of window
923 static size_t http_socket_window ( struct http_request *http __unused ) {
925 /* Window is always open. This is to prevent TCP from
926 * stalling if our parent window is not currently open.
928 return ( ~( ( size_t ) 0 ) );
934 * @v http HTTP request
935 * @v rc Reason for close
937 static void http_socket_close ( struct http_request *http, int rc ) {
939 /* If we have an error, terminate */
941 http_close ( http, rc );
945 /* Mark HTTP request as complete */
950 * Generate HTTP Basic authorisation string
952 * @v http HTTP request
953 * @ret auth Authorisation string, or NULL on error
955 * The authorisation string is dynamically allocated, and must be
956 * freed by the caller.
958 static char * http_basic_auth ( struct http_request *http ) {
959 const char *user = http->uri->user;
960 const char *password =
961 ( http->uri->password ? http->uri->password : "" );
963 ( strlen ( user ) + 1 /* ":" */ + strlen ( password ) );
964 char user_pw[ user_pw_len + 1 /* NUL */ ];
965 size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
966 char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
971 assert ( user != NULL );
973 /* Make "user:password" string from decoded fields */
974 snprintf ( user_pw, sizeof ( user_pw ), "%s:%s", user, password );
976 /* Base64-encode the "user:password" string */
977 base64_encode ( ( void * ) user_pw, user_pw_len, user_pw_base64 );
979 /* Generate the authorisation string */
980 len = asprintf ( &auth, "Authorization: Basic %s\r\n",
989 * Generate HTTP Digest authorisation string
991 * @v http HTTP request
992 * @v method HTTP method (e.g. "GET")
993 * @v uri HTTP request URI (e.g. "/index.html")
994 * @ret auth Authorisation string, or NULL on error
996 * The authorisation string is dynamically allocated, and must be
997 * freed by the caller.
999 static char * http_digest_auth ( struct http_request *http,
1000 const char *method, const char *uri ) {
1001 const char *user = http->uri->user;
1002 const char *password =
1003 ( http->uri->password ? http->uri->password : "" );
1004 const char *realm = http->auth_realm;
1005 const char *nonce = http->auth_nonce;
1006 const char *opaque = http->auth_opaque;
1007 static const char colon = ':';
1008 uint8_t ctx[MD5_CTX_SIZE];
1009 uint8_t digest[MD5_DIGEST_SIZE];
1010 char ha1[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1011 char ha2[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1012 char response[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1017 assert ( user != NULL );
1018 assert ( realm != NULL );
1019 assert ( nonce != NULL );
1022 digest_init ( &md5_algorithm, ctx );
1023 digest_update ( &md5_algorithm, ctx, user, strlen ( user ) );
1024 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1025 digest_update ( &md5_algorithm, ctx, realm, strlen ( realm ) );
1026 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1027 digest_update ( &md5_algorithm, ctx, password, strlen ( password ) );
1028 digest_final ( &md5_algorithm, ctx, digest );
1029 base16_encode ( digest, sizeof ( digest ), ha1 );
1032 digest_init ( &md5_algorithm, ctx );
1033 digest_update ( &md5_algorithm, ctx, method, strlen ( method ) );
1034 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1035 digest_update ( &md5_algorithm, ctx, uri, strlen ( uri ) );
1036 digest_final ( &md5_algorithm, ctx, digest );
1037 base16_encode ( digest, sizeof ( digest ), ha2 );
1039 /* Generate response */
1040 digest_init ( &md5_algorithm, ctx );
1041 digest_update ( &md5_algorithm, ctx, ha1, strlen ( ha1 ) );
1042 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1043 digest_update ( &md5_algorithm, ctx, nonce, strlen ( nonce ) );
1044 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1045 digest_update ( &md5_algorithm, ctx, ha2, strlen ( ha2 ) );
1046 digest_final ( &md5_algorithm, ctx, digest );
1047 base16_encode ( digest, sizeof ( digest ), response );
1049 /* Generate the authorisation string */
1050 len = asprintf ( &auth, "Authorization: Digest username=\"%s\", "
1051 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
1052 "%s%s%sresponse=\"%s\"\r\n", user, realm, nonce, uri,
1053 ( opaque ? "opaque=\"" : "" ),
1054 ( opaque ? opaque : "" ),
1055 ( opaque ? "\", " : "" ), response );
1065 * @v http HTTP request
1067 static void http_step ( struct http_request *http ) {
1076 /* Do nothing if we have already transmitted the request */
1077 if ( ! ( http->flags & HTTP_TX_PENDING ) )
1080 /* Do nothing until socket is ready */
1081 if ( ! xfer_window ( &http->socket ) )
1084 /* Force a HEAD request if we have nowhere to send any received data */
1085 if ( ( xfer_window ( &http->xfer ) == 0 ) &&
1086 ( http->rx_buffer == UNULL ) ) {
1087 http->flags |= ( HTTP_HEAD_ONLY | HTTP_CLIENT_KEEPALIVE );
1090 /* Determine method */
1091 method = ( ( http->flags & HTTP_HEAD_ONLY ) ? "HEAD" : "GET" );
1093 /* Construct path?query request */
1094 uri_len = ( unparse_uri ( NULL, 0, http->uri,
1095 URI_PATH_BIT | URI_QUERY_BIT )
1096 + 1 /* possible "/" */ + 1 /* NUL */ );
1097 uri = malloc ( uri_len );
1102 unparse_uri ( uri, uri_len, http->uri, URI_PATH_BIT | URI_QUERY_BIT );
1108 /* Calculate range request parameters if applicable */
1109 if ( http->partial_len ) {
1110 len = asprintf ( &range, "Range: bytes=%zd-%zd\r\n",
1111 http->partial_start,
1112 ( http->partial_start + http->partial_len
1122 /* Construct authorisation, if applicable */
1123 if ( http->flags & HTTP_BASIC_AUTH ) {
1124 auth = http_basic_auth ( http );
1129 } else if ( http->flags & HTTP_DIGEST_AUTH ) {
1130 auth = http_digest_auth ( http, method, uri );
1139 /* Mark request as transmitted */
1140 http->flags &= ~HTTP_TX_PENDING;
1143 if ( ( rc = xfer_printf ( &http->socket,
1144 "%s %s HTTP/1.1\r\n"
1145 "User-Agent: iPXE/%s\r\n"
1149 method, uri, product_version, http->uri->host,
1153 http->uri->port : "" ),
1154 ( ( http->flags & HTTP_CLIENT_KEEPALIVE ) ?
1155 "Connection: keep-alive\r\n" : "" ),
1156 ( range ? range : "" ),
1157 ( auth ? auth : "" ) ) ) != 0 ) {
1169 http_close ( http, rc );
1173 * Check HTTP data transfer flow control window
1175 * @v http HTTP request
1176 * @ret len Length of window
1178 static size_t http_xfer_window ( struct http_request *http ) {
1180 /* New block commands may be issued only when we are idle */
1181 return ( ( http->rx_state == HTTP_RX_IDLE ) ? 1 : 0 );
1185 * Initiate HTTP partial read
1187 * @v http HTTP request
1188 * @v partial Partial transfer interface
1189 * @v offset Starting offset
1190 * @v buffer Data buffer
1192 * @ret rc Return status code
1194 static int http_partial_read ( struct http_request *http,
1195 struct interface *partial,
1196 size_t offset, userptr_t buffer, size_t len ) {
1199 if ( http_xfer_window ( http ) == 0 )
1202 /* Initialise partial transfer parameters */
1203 http->rx_buffer = buffer;
1204 http->partial_start = offset;
1205 http->partial_len = len;
1207 /* Schedule request */
1208 http->rx_state = HTTP_RX_RESPONSE;
1209 http->flags = ( HTTP_TX_PENDING | HTTP_CLIENT_KEEPALIVE );
1211 http->flags |= HTTP_HEAD_ONLY;
1212 process_add ( &http->process );
1214 /* Attach to parent interface and return */
1215 intf_plug_plug ( &http->partial, partial );
1221 * Issue HTTP block device read
1223 * @v http HTTP request
1224 * @v block Block data interface
1225 * @v lba Starting logical block address
1226 * @v count Number of blocks to transfer
1227 * @v buffer Data buffer
1228 * @v len Length of data buffer
1229 * @ret rc Return status code
1231 static int http_block_read ( struct http_request *http,
1232 struct interface *block,
1233 uint64_t lba, unsigned int count,
1234 userptr_t buffer, size_t len __unused ) {
1236 return http_partial_read ( http, block, ( lba * HTTP_BLKSIZE ),
1237 buffer, ( count * HTTP_BLKSIZE ) );
1241 * Read HTTP block device capacity
1243 * @v http HTTP request
1244 * @v block Block data interface
1245 * @ret rc Return status code
1247 static int http_block_read_capacity ( struct http_request *http,
1248 struct interface *block ) {
1250 return http_partial_read ( http, block, 0, 0, 0 );
1254 * Describe HTTP device in an ACPI table
1256 * @v http HTTP request
1257 * @v acpi ACPI table
1258 * @v len Length of ACPI table
1259 * @ret rc Return status code
1261 static int http_acpi_describe ( struct http_request *http,
1262 struct acpi_description_header *acpi,
1265 DBGC ( http, "HTTP %p cannot yet describe device in an ACPI table\n",
1272 /** HTTP socket interface operations */
1273 static struct interface_operation http_socket_operations[] = {
1274 INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
1275 INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
1276 INTF_OP ( xfer_window_changed, struct http_request *, http_step ),
1277 INTF_OP ( intf_close, struct http_request *, http_socket_close ),
1280 /** HTTP socket interface descriptor */
1281 static struct interface_descriptor http_socket_desc =
1282 INTF_DESC_PASSTHRU ( struct http_request, socket,
1283 http_socket_operations, xfer );
1285 /** HTTP partial transfer interface operations */
1286 static struct interface_operation http_partial_operations[] = {
1287 INTF_OP ( intf_close, struct http_request *, http_close ),
1290 /** HTTP partial transfer interface descriptor */
1291 static struct interface_descriptor http_partial_desc =
1292 INTF_DESC ( struct http_request, partial, http_partial_operations );
1294 /** HTTP data transfer interface operations */
1295 static struct interface_operation http_xfer_operations[] = {
1296 INTF_OP ( xfer_window, struct http_request *, http_xfer_window ),
1297 INTF_OP ( block_read, struct http_request *, http_block_read ),
1298 INTF_OP ( block_read_capacity, struct http_request *,
1299 http_block_read_capacity ),
1300 INTF_OP ( intf_close, struct http_request *, http_close ),
1301 INTF_OP ( acpi_describe, struct http_request *, http_acpi_describe ),
1304 /** HTTP data transfer interface descriptor */
1305 static struct interface_descriptor http_xfer_desc =
1306 INTF_DESC_PASSTHRU ( struct http_request, xfer,
1307 http_xfer_operations, socket );
1309 /** HTTP process descriptor */
1310 static struct process_descriptor http_process_desc =
1311 PROC_DESC_ONCE ( struct http_request, process, http_step );
1314 * Initiate an HTTP connection, with optional filter
1316 * @v xfer Data transfer interface
1317 * @v uri Uniform Resource Identifier
1318 * @v default_port Default port number
1319 * @v filter Filter to apply to socket, or NULL
1320 * @ret rc Return status code
1322 int http_open_filter ( struct interface *xfer, struct uri *uri,
1323 unsigned int default_port,
1324 int ( * filter ) ( struct interface *xfer,
1326 struct interface **next ) ) {
1327 struct http_request *http;
1334 /* Allocate and populate HTTP structure */
1335 http = zalloc ( sizeof ( *http ) );
1338 ref_init ( &http->refcnt, http_free );
1339 intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
1340 intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
1341 http->uri = uri_get ( uri );
1342 http->default_port = default_port;
1343 http->filter = filter;
1344 intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
1345 process_init ( &http->process, &http_process_desc, &http->refcnt );
1346 http->flags = HTTP_TX_PENDING;
1349 if ( ( rc = http_socket_open ( http ) ) != 0 )
1352 /* Attach to parent interface, mortalise self, and return */
1353 intf_plug_plug ( &http->xfer, xfer );
1354 ref_put ( &http->refcnt );
1358 DBGC ( http, "HTTP %p could not create request: %s\n",
1359 http, strerror ( rc ) );
1360 http_close ( http, rc );
1361 ref_put ( &http->refcnt );