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
39 #include <ipxe/refcnt.h>
40 #include <ipxe/iobuf.h>
41 #include <ipxe/xfer.h>
42 #include <ipxe/open.h>
43 #include <ipxe/socket.h>
44 #include <ipxe/tcpip.h>
45 #include <ipxe/process.h>
46 #include <ipxe/retry.h>
47 #include <ipxe/timer.h>
48 #include <ipxe/linebuf.h>
49 #include <ipxe/base64.h>
50 #include <ipxe/base16.h>
52 #include <ipxe/blockdev.h>
53 #include <ipxe/acpi.h>
54 #include <ipxe/version.h>
55 #include <ipxe/params.h>
56 #include <ipxe/profile.h>
57 #include <ipxe/http.h>
59 /* Disambiguate the various error causes */
60 #define EACCES_401 __einfo_error ( EINFO_EACCES_401 )
61 #define EINFO_EACCES_401 \
62 __einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )
63 #define EIO_OTHER __einfo_error ( EINFO_EIO_OTHER )
64 #define EINFO_EIO_OTHER \
65 __einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )
66 #define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
67 #define EINFO_EIO_CONTENT_LENGTH \
68 __einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
69 #define EINVAL_RESPONSE __einfo_error ( EINFO_EINVAL_RESPONSE )
70 #define EINFO_EINVAL_RESPONSE \
71 __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid content length" )
72 #define EINVAL_HEADER __einfo_error ( EINFO_EINVAL_HEADER )
73 #define EINFO_EINVAL_HEADER \
74 __einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid header" )
75 #define EINVAL_CONTENT_LENGTH __einfo_error ( EINFO_EINVAL_CONTENT_LENGTH )
76 #define EINFO_EINVAL_CONTENT_LENGTH \
77 __einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid content length" )
78 #define EINVAL_CHUNK_LENGTH __einfo_error ( EINFO_EINVAL_CHUNK_LENGTH )
79 #define EINFO_EINVAL_CHUNK_LENGTH \
80 __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid chunk length" )
81 #define ENOENT_404 __einfo_error ( EINFO_ENOENT_404 )
82 #define EINFO_ENOENT_404 \
83 __einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )
84 #define EPERM_403 __einfo_error ( EINFO_EPERM_403 )
85 #define EINFO_EPERM_403 \
86 __einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )
87 #define EPROTO_UNSOLICITED __einfo_error ( EINFO_EPROTO_UNSOLICITED )
88 #define EINFO_EPROTO_UNSOLICITED \
89 __einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )
91 /** Block size used for HTTP block device request */
92 #define HTTP_BLKSIZE 512
94 /** Retry delay used when we cannot understand the Retry-After header */
95 #define HTTP_RETRY_SECONDS 5
97 /** Receive profiler */
98 static struct profiler http_rx_profiler __profiler = { .name = "http.rx" };
100 /** Data transfer profiler */
101 static struct profiler http_xfer_profiler __profiler = { .name = "http.xfer" };
105 /** Request is waiting to be transmitted */
106 HTTP_TX_PENDING = 0x0001,
107 /** Fetch header only */
108 HTTP_HEAD_ONLY = 0x0002,
109 /** Client would like to keep connection alive */
110 HTTP_CLIENT_KEEPALIVE = 0x0004,
111 /** Server will keep connection alive */
112 HTTP_SERVER_KEEPALIVE = 0x0008,
113 /** Discard the current request and try again */
114 HTTP_TRY_AGAIN = 0x0010,
115 /** Provide Basic authentication details */
116 HTTP_BASIC_AUTH = 0x0020,
117 /** Provide Digest authentication details */
118 HTTP_DIGEST_AUTH = 0x0040,
119 /** Socket must be reopened */
120 HTTP_REOPEN_SOCKET = 0x0080,
123 /** HTTP receive state */
125 HTTP_RX_RESPONSE = 0,
128 /* In HTTP_RX_DATA, it is acceptable for the server to close
129 * the connection (unless we are in the middle of a chunked
133 /* In the following states, it is acceptable for the server to
134 * close the connection.
145 struct http_request {
146 /** Reference count */
147 struct refcnt refcnt;
148 /** Data transfer interface */
149 struct interface xfer;
150 /** Partial transfer interface */
151 struct interface partial;
153 /** URI being fetched */
156 unsigned int default_port;
157 /** Filter (if any) */
158 int ( * filter ) ( struct interface *xfer,
160 struct interface **next );
161 /** Transport layer interface */
162 struct interface socket;
166 /** Starting offset of partial transfer (if applicable) */
167 size_t partial_start;
168 /** Length of partial transfer (if applicable) */
172 struct process process;
175 enum http_rx_state rx_state;
178 /** Received length */
180 /** Length remaining (or 0 if unknown) */
182 /** HTTP is using Transfer-Encoding: chunked */
184 /** Current chunk length remaining (if applicable) */
185 size_t chunk_remaining;
186 /** Line buffer for received header lines */
187 struct line_buffer linebuf;
188 /** Receive data buffer (if applicable) */
191 /** Authentication realm (if any) */
193 /** Authentication nonce (if any) */
195 /** Authentication opaque string (if any) */
198 /** Request retry timer */
199 struct retry_timer timer;
200 /** Retry delay (in timer ticks) */
201 unsigned long retry_delay;
207 * @v refcnt Reference counter
209 static void http_free ( struct refcnt *refcnt ) {
210 struct http_request *http =
211 container_of ( refcnt, struct http_request, refcnt );
213 uri_put ( http->uri );
214 empty_line_buffer ( &http->linebuf );
215 free ( http->auth_realm );
216 free ( http->auth_nonce );
217 free ( http->auth_opaque );
224 * @v http HTTP request
225 * @v rc Return status code
227 static void http_close ( struct http_request *http, int rc ) {
229 /* Prevent further processing of any current packet */
230 http->rx_state = HTTP_RX_DEAD;
232 /* Prevent reconnection */
233 http->flags &= ~HTTP_CLIENT_KEEPALIVE;
236 process_del ( &http->process );
238 /* Close all data transfer interfaces */
239 intf_shutdown ( &http->socket, rc );
240 intf_shutdown ( &http->partial, rc );
241 intf_shutdown ( &http->xfer, rc );
247 * @v http HTTP request
248 * @ret rc Return status code
250 static int http_socket_open ( struct http_request *http ) {
251 struct uri *uri = http->uri;
252 struct sockaddr_tcpip server;
253 struct interface *socket;
257 memset ( &server, 0, sizeof ( server ) );
258 server.st_port = htons ( uri_port ( uri, http->default_port ) );
259 socket = &http->socket;
260 if ( http->filter ) {
261 if ( ( rc = http->filter ( socket, uri->host, &socket ) ) != 0 )
264 if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
265 ( struct sockaddr * ) &server,
266 uri->host, NULL ) ) != 0 )
275 * @v timer Retry timer
276 * @v fail Failure indicator
278 static void http_retry ( struct retry_timer *timer, int fail __unused ) {
279 struct http_request *http =
280 container_of ( timer, struct http_request, timer );
283 /* Reopen socket if required */
284 if ( http->flags & HTTP_REOPEN_SOCKET ) {
285 http->flags &= ~HTTP_REOPEN_SOCKET;
286 DBGC ( http, "HTTP %p reopening connection\n", http );
287 if ( ( rc = http_socket_open ( http ) ) != 0 ) {
288 http_close ( http, rc );
293 /* Retry the request if applicable */
294 if ( http->flags & HTTP_TRY_AGAIN ) {
295 http->flags &= ~HTTP_TRY_AGAIN;
296 DBGC ( http, "HTTP %p retrying request\n", http );
297 http->flags |= HTTP_TX_PENDING;
298 http->rx_state = HTTP_RX_RESPONSE;
299 process_add ( &http->process );
304 * Mark HTTP request as completed successfully
306 * @v http HTTP request
308 static void http_done ( struct http_request *http ) {
310 /* If we are not at an appropriate stage of the protocol
311 * (including being in the middle of a chunked transfer),
314 if ( ( http->rx_state < HTTP_RX_DATA ) || ( http->chunked != 0 ) ) {
315 DBGC ( http, "HTTP %p connection closed unexpectedly in state "
316 "%d\n", http, http->rx_state );
317 http_close ( http, -ECONNRESET );
321 /* If we had a Content-Length, and the received content length
322 * isn't correct, force an error
324 if ( http->remaining != 0 ) {
325 DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
326 http, http->rx_len, ( http->rx_len + http->remaining ) );
327 http_close ( http, -EIO_CONTENT_LENGTH );
331 /* Enter idle state */
332 http->rx_state = HTTP_RX_IDLE;
334 assert ( http->remaining == 0 );
335 assert ( http->chunked == 0 );
336 assert ( http->chunk_remaining == 0 );
338 /* Close partial transfer interface */
339 if ( ! ( http->flags & HTTP_TRY_AGAIN ) )
340 intf_restart ( &http->partial, 0 );
342 /* Close everything unless we want to keep the connection alive */
343 if ( ! ( http->flags & ( HTTP_CLIENT_KEEPALIVE | HTTP_TRY_AGAIN ) ) ) {
344 http_close ( http, 0 );
348 /* If the server is not intending to keep the connection
349 * alive, then close the socket and mark it as requiring
352 if ( ! ( http->flags & HTTP_SERVER_KEEPALIVE ) ) {
353 intf_restart ( &http->socket, 0 );
354 http->flags &= ~HTTP_SERVER_KEEPALIVE;
355 http->flags |= HTTP_REOPEN_SOCKET;
358 /* Start request retry timer */
359 start_timer_fixed ( &http->timer, http->retry_delay );
360 http->retry_delay = 0;
364 * Convert HTTP response code to return status code
366 * @v response HTTP response code
367 * @ret rc Return status code
369 static int http_response_to_rc ( unsigned int response ) {
370 switch ( response ) {
389 * Handle HTTP response
391 * @v http HTTP request
392 * @v response HTTP response
393 * @ret rc Return status code
395 static int http_rx_response ( struct http_request *http, char *response ) {
398 DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
400 /* Check response starts with "HTTP/" */
401 if ( strncmp ( response, "HTTP/", 5 ) != 0 )
402 return -EINVAL_RESPONSE;
404 /* Locate and store response code */
405 spc = strchr ( response, ' ' );
407 return -EINVAL_RESPONSE;
408 http->code = strtoul ( spc, NULL, 10 );
410 /* Move to receive headers */
411 http->rx_state = ( ( http->flags & HTTP_HEAD_ONLY ) ?
412 HTTP_RX_TRAILER : HTTP_RX_HEADER );
417 * Handle HTTP Location header
419 * @v http HTTP request
420 * @v value HTTP header value
421 * @ret rc Return status code
423 static int http_rx_location ( struct http_request *http, char *value ) {
426 /* Redirect to new location */
427 DBGC ( http, "HTTP %p redirecting to %s\n", http, value );
428 if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI_STRING,
430 DBGC ( http, "HTTP %p could not redirect: %s\n",
431 http, strerror ( rc ) );
439 * Handle HTTP Content-Length header
441 * @v http HTTP request
442 * @v value HTTP header value
443 * @ret rc Return status code
445 static int http_rx_content_length ( struct http_request *http, char *value ) {
446 struct block_device_capacity capacity;
450 /* Parse content length */
451 content_len = strtoul ( value, &endp, 10 );
452 if ( ! ( ( *endp == '\0' ) || isspace ( *endp ) ) ) {
453 DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
455 return -EINVAL_CONTENT_LENGTH;
458 /* If we already have an expected content length, and this
459 * isn't it, then complain
461 if ( http->remaining && ( http->remaining != content_len ) ) {
462 DBGC ( http, "HTTP %p incorrect Content-Length %zd (expected "
463 "%zd)\n", http, content_len, http->remaining );
464 return -EIO_CONTENT_LENGTH;
466 if ( ! ( http->flags & HTTP_HEAD_ONLY ) )
467 http->remaining = content_len;
469 /* Do nothing more if we are retrying the request */
470 if ( http->flags & HTTP_TRY_AGAIN )
473 /* Use seek() to notify recipient of filesize */
474 xfer_seek ( &http->xfer, http->remaining );
475 xfer_seek ( &http->xfer, 0 );
477 /* Report block device capacity if applicable */
478 if ( http->flags & HTTP_HEAD_ONLY ) {
479 capacity.blocks = ( content_len / HTTP_BLKSIZE );
480 capacity.blksize = HTTP_BLKSIZE;
481 capacity.max_count = -1U;
482 block_capacity ( &http->partial, &capacity );
488 * Handle HTTP Transfer-Encoding header
490 * @v http HTTP request
491 * @v value HTTP header value
492 * @ret rc Return status code
494 static int http_rx_transfer_encoding ( struct http_request *http, char *value ){
496 if ( strcasecmp ( value, "chunked" ) == 0 ) {
497 /* Mark connection as using chunked transfer encoding */
505 * Handle HTTP Connection header
507 * @v http HTTP request
508 * @v value HTTP header value
509 * @ret rc Return status code
511 static int http_rx_connection ( struct http_request *http, char *value ) {
513 if ( strcasecmp ( value, "keep-alive" ) == 0 ) {
514 /* Mark connection as being kept alive by the server */
515 http->flags |= HTTP_SERVER_KEEPALIVE;
522 * Handle WWW-Authenticate Basic header
524 * @v http HTTP request
525 * @v params Parameters
526 * @ret rc Return status code
528 static int http_rx_basic_auth ( struct http_request *http, char *params ) {
530 DBGC ( http, "HTTP %p Basic authentication required (%s)\n",
533 /* If we received a 401 Unauthorized response, then retry
534 * using Basic authentication
536 if ( ( http->code == 401 ) &&
537 ( ! ( http->flags & HTTP_BASIC_AUTH ) ) &&
538 ( http->uri->user != NULL ) ) {
539 http->flags |= ( HTTP_TRY_AGAIN | HTTP_BASIC_AUTH );
546 * Parse Digest authentication parameter
548 * @v params Parameters
549 * @v name Parameter name (including trailing "=\"")
550 * @ret value Parameter value, or NULL
552 static char * http_digest_param ( char *params, const char *name ) {
557 /* Locate parameter */
558 key = strstr ( params, name );
563 value = ( key + strlen ( name ) );
564 terminator = strchr ( value, '"' );
567 return strndup ( value, ( terminator - value ) );
571 * Handle WWW-Authenticate Digest header
573 * @v http HTTP request
574 * @v params Parameters
575 * @ret rc Return status code
577 static int http_rx_digest_auth ( struct http_request *http, char *params ) {
579 DBGC ( http, "HTTP %p Digest authentication required (%s)\n",
582 /* If we received a 401 Unauthorized response, then retry
583 * using Digest authentication
585 if ( ( http->code == 401 ) &&
586 ( ! ( http->flags & HTTP_DIGEST_AUTH ) ) &&
587 ( http->uri->user != NULL ) ) {
590 free ( http->auth_realm );
591 http->auth_realm = http_digest_param ( params, "realm=\"" );
592 if ( ! http->auth_realm ) {
593 DBGC ( http, "HTTP %p Digest prompt missing realm\n",
595 return -EINVAL_HEADER;
599 free ( http->auth_nonce );
600 http->auth_nonce = http_digest_param ( params, "nonce=\"" );
601 if ( ! http->auth_nonce ) {
602 DBGC ( http, "HTTP %p Digest prompt missing nonce\n",
604 return -EINVAL_HEADER;
608 free ( http->auth_opaque );
609 http->auth_opaque = http_digest_param ( params, "opaque=\"" );
610 if ( ! http->auth_opaque ) {
611 /* Not an error; "opaque" is optional */
614 http->flags |= ( HTTP_TRY_AGAIN | HTTP_DIGEST_AUTH );
620 /** An HTTP WWW-Authenticate header handler */
621 struct http_auth_header_handler {
622 /** Scheme (e.g. "Basic") */
624 /** Handle received parameters
626 * @v http HTTP request
627 * @v params Parameters
628 * @ret rc Return status code
630 int ( * rx ) ( struct http_request *http, char *params );
633 /** List of HTTP WWW-Authenticate header handlers */
634 static struct http_auth_header_handler http_auth_header_handlers[] = {
637 .rx = http_rx_basic_auth,
641 .rx = http_rx_digest_auth,
647 * Handle HTTP WWW-Authenticate header
649 * @v http HTTP request
650 * @v value HTTP header value
651 * @ret rc Return status code
653 static int http_rx_www_authenticate ( struct http_request *http, char *value ) {
654 struct http_auth_header_handler *handler;
661 separator = strchr ( value, ' ' );
663 DBGC ( http, "HTTP %p malformed WWW-Authenticate header\n",
665 return -EINVAL_HEADER;
669 params = ( separator + 1 );
671 /* Hand off to header handler, if one exists */
672 for ( handler = http_auth_header_handlers; handler->scheme; handler++ ){
673 if ( strcasecmp ( scheme, handler->scheme ) == 0 ) {
674 if ( ( rc = handler->rx ( http, params ) ) != 0 )
683 * Handle HTTP Retry-After header
685 * @v http HTTP request
686 * @v value HTTP header value
687 * @ret rc Return status code
689 static int http_rx_retry_after ( struct http_request *http, char *value ) {
690 unsigned long seconds;
693 DBGC ( http, "HTTP %p retry requested (%s)\n", http, value );
695 /* If we received a 503 Service Unavailable response, then
696 * retry after the specified number of seconds. If the value
697 * is not a simple number of seconds (e.g. a full HTTP date),
698 * then retry after a fixed delay, since we don't have code
699 * able to parse full HTTP dates.
701 if ( http->code == 503 ) {
702 seconds = strtoul ( value, &endp, 10 );
703 if ( *endp != '\0' ) {
704 seconds = HTTP_RETRY_SECONDS;
705 DBGC ( http, "HTTP %p cannot understand \"%s\"; "
706 "using %ld seconds\n", http, value, seconds );
708 http->flags |= HTTP_TRY_AGAIN;
709 http->retry_delay = ( seconds * TICKS_PER_SEC );
715 /** An HTTP header handler */
716 struct http_header_handler {
717 /** Name (e.g. "Content-Length") */
719 /** Handle received header
721 * @v http HTTP request
722 * @v value HTTP header value
723 * @ret rc Return status code
725 * If an error is returned, the download will be aborted.
727 int ( * rx ) ( struct http_request *http, char *value );
730 /** List of HTTP header handlers */
731 static struct http_header_handler http_header_handlers[] = {
733 .header = "Location",
734 .rx = http_rx_location,
737 .header = "Content-Length",
738 .rx = http_rx_content_length,
741 .header = "Transfer-Encoding",
742 .rx = http_rx_transfer_encoding,
745 .header = "Connection",
746 .rx = http_rx_connection,
749 .header = "WWW-Authenticate",
750 .rx = http_rx_www_authenticate,
753 .header = "Retry-After",
754 .rx = http_rx_retry_after,
762 * @v http HTTP request
763 * @v header HTTP header
764 * @ret rc Return status code
766 static int http_rx_header ( struct http_request *http, char *header ) {
767 struct http_header_handler *handler;
772 /* An empty header line marks the end of this phase */
774 empty_line_buffer ( &http->linebuf );
776 /* Handle response code */
777 if ( ! ( http->flags & HTTP_TRY_AGAIN ) ) {
778 if ( ( rc = http_response_to_rc ( http->code ) ) != 0 )
782 /* Move to next state */
783 if ( http->rx_state == HTTP_RX_HEADER ) {
784 DBGC ( http, "HTTP %p start of data\n", http );
785 http->rx_state = ( http->chunked ?
786 HTTP_RX_CHUNK_LEN : HTTP_RX_DATA );
787 if ( ( http->partial_len != 0 ) &&
788 ( ! ( http->flags & HTTP_TRY_AGAIN ) ) ) {
789 http->remaining = http->partial_len;
793 DBGC ( http, "HTTP %p end of trailer\n", http );
799 DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
801 /* Split header at the ": " */
802 separator = strstr ( header, ": " );
804 DBGC ( http, "HTTP %p malformed header\n", http );
805 return -EINVAL_HEADER;
808 value = ( separator + 2 );
810 /* Hand off to header handler, if one exists */
811 for ( handler = http_header_handlers ; handler->header ; handler++ ) {
812 if ( strcasecmp ( header, handler->header ) == 0 ) {
813 if ( ( rc = handler->rx ( http, value ) ) != 0 )
822 * Handle HTTP chunk length
824 * @v http HTTP request
825 * @v length HTTP chunk length
826 * @ret rc Return status code
828 static int http_rx_chunk_len ( struct http_request *http, char *length ) {
831 /* Skip blank lines between chunks */
832 if ( length[0] == '\0' )
835 /* Parse chunk length */
836 http->chunk_remaining = strtoul ( length, &endp, 16 );
837 if ( *endp != '\0' ) {
838 DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
840 return -EINVAL_CHUNK_LENGTH;
843 /* Terminate chunked encoding if applicable */
844 if ( http->chunk_remaining == 0 ) {
845 DBGC ( http, "HTTP %p end of chunks\n", http );
847 http->rx_state = HTTP_RX_TRAILER;
851 /* Use seek() to notify recipient of new filesize */
852 DBGC ( http, "HTTP %p start of chunk of length %zd\n",
853 http, http->chunk_remaining );
854 if ( ! ( http->flags & HTTP_TRY_AGAIN ) ) {
855 xfer_seek ( &http->xfer,
856 ( http->rx_len + http->chunk_remaining ) );
857 xfer_seek ( &http->xfer, http->rx_len );
860 /* Start receiving data */
861 http->rx_state = HTTP_RX_DATA;
866 /** An HTTP line-based data handler */
867 struct http_line_handler {
870 * @v http HTTP request
871 * @v line Line to handle
872 * @ret rc Return status code
874 int ( * rx ) ( struct http_request *http, char *line );
877 /** List of HTTP line-based data handlers */
878 static struct http_line_handler http_line_handlers[] = {
879 [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
880 [HTTP_RX_HEADER] = { .rx = http_rx_header },
881 [HTTP_RX_CHUNK_LEN] = { .rx = http_rx_chunk_len },
882 [HTTP_RX_TRAILER] = { .rx = http_rx_header },
886 * Handle new data arriving via HTTP connection
888 * @v http HTTP request
889 * @v iobuf I/O buffer
890 * @v meta Data transfer metadata
891 * @ret rc Return status code
893 static int http_socket_deliver ( struct http_request *http,
894 struct io_buffer *iobuf,
895 struct xfer_metadata *meta __unused ) {
896 struct http_line_handler *lh;
902 profile_start ( &http_rx_profiler );
903 while ( iobuf && iob_len ( iobuf ) ) {
905 switch ( http->rx_state ) {
907 /* Receiving any data in this state is an error */
908 DBGC ( http, "HTTP %p received %zd bytes while %s\n",
909 http, iob_len ( iobuf ),
910 ( ( http->rx_state == HTTP_RX_IDLE ) ?
912 rc = -EPROTO_UNSOLICITED;
915 /* Do no further processing */
918 /* Pass received data to caller */
919 data_len = iob_len ( iobuf );
920 if ( http->chunk_remaining &&
921 ( http->chunk_remaining < data_len ) ) {
922 data_len = http->chunk_remaining;
924 if ( http->remaining &&
925 ( http->remaining < data_len ) ) {
926 data_len = http->remaining;
928 if ( http->flags & HTTP_TRY_AGAIN ) {
929 /* Discard all received data */
930 iob_pull ( iobuf, data_len );
931 } else if ( http->rx_buffer != UNULL ) {
932 /* Copy to partial transfer buffer */
933 copy_to_user ( http->rx_buffer, http->rx_len,
934 iobuf->data, data_len );
935 iob_pull ( iobuf, data_len );
936 } else if ( data_len < iob_len ( iobuf ) ) {
937 /* Deliver partial buffer as raw data */
938 profile_start ( &http_xfer_profiler );
939 rc = xfer_deliver_raw ( &http->xfer,
940 iobuf->data, data_len );
941 iob_pull ( iobuf, data_len );
944 profile_stop ( &http_xfer_profiler );
946 /* Deliver whole I/O buffer */
947 profile_start ( &http_xfer_profiler );
948 if ( ( rc = xfer_deliver_iob ( &http->xfer,
949 iob_disown ( iobuf ) ) ) != 0 )
951 profile_stop ( &http_xfer_profiler );
953 http->rx_len += data_len;
954 if ( http->chunk_remaining ) {
955 http->chunk_remaining -= data_len;
956 if ( http->chunk_remaining == 0 )
957 http->rx_state = HTTP_RX_CHUNK_LEN;
959 if ( http->remaining ) {
960 http->remaining -= data_len;
961 if ( ( http->remaining == 0 ) &&
962 ( http->rx_state == HTTP_RX_DATA ) ) {
967 case HTTP_RX_RESPONSE:
969 case HTTP_RX_CHUNK_LEN:
970 case HTTP_RX_TRAILER:
971 /* In the other phases, buffer and process a
974 line_len = line_buffer ( &http->linebuf, iobuf->data,
976 if ( line_len < 0 ) {
978 DBGC ( http, "HTTP %p could not buffer line: "
979 "%s\n", http, strerror ( rc ) );
982 iob_pull ( iobuf, line_len );
983 line = buffered_line ( &http->linebuf );
985 lh = &http_line_handlers[http->rx_state];
986 if ( ( rc = lh->rx ( http, line ) ) != 0 )
998 http_close ( http, rc );
1000 profile_stop ( &http_rx_profiler );
1005 * Check HTTP socket flow control window
1007 * @v http HTTP request
1008 * @ret len Length of window
1010 static size_t http_socket_window ( struct http_request *http __unused ) {
1012 /* Window is always open. This is to prevent TCP from
1013 * stalling if our parent window is not currently open.
1015 return ( ~( ( size_t ) 0 ) );
1021 * @v http HTTP request
1022 * @v rc Reason for close
1024 static void http_socket_close ( struct http_request *http, int rc ) {
1026 /* If we have an error, terminate */
1028 http_close ( http, rc );
1032 /* Mark HTTP request as complete */
1037 * Generate HTTP Basic authorisation string
1039 * @v http HTTP request
1040 * @ret auth Authorisation string, or NULL on error
1042 * The authorisation string is dynamically allocated, and must be
1043 * freed by the caller.
1045 static char * http_basic_auth ( struct http_request *http ) {
1046 const char *user = http->uri->user;
1047 const char *password = ( http->uri->password ?
1048 http->uri->password : "" );
1049 size_t user_pw_len =
1050 ( strlen ( user ) + 1 /* ":" */ + strlen ( password ) );
1051 char user_pw[ user_pw_len + 1 /* NUL */ ];
1052 size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
1053 char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
1058 assert ( user != NULL );
1060 /* Make "user:password" string from decoded fields */
1061 snprintf ( user_pw, sizeof ( user_pw ), "%s:%s", user, password );
1063 /* Base64-encode the "user:password" string */
1064 base64_encode ( ( void * ) user_pw, user_pw_len, user_pw_base64 );
1066 /* Generate the authorisation string */
1067 len = asprintf ( &auth, "Authorization: Basic %s\r\n",
1076 * Generate HTTP Digest authorisation string
1078 * @v http HTTP request
1079 * @v method HTTP method (e.g. "GET")
1080 * @v uri HTTP request URI (e.g. "/index.html")
1081 * @ret auth Authorisation string, or NULL on error
1083 * The authorisation string is dynamically allocated, and must be
1084 * freed by the caller.
1086 static char * http_digest_auth ( struct http_request *http,
1087 const char *method, const char *uri ) {
1088 const char *user = http->uri->user;
1089 const char *password = ( http->uri->password ?
1090 http->uri->password : "" );
1091 const char *realm = http->auth_realm;
1092 const char *nonce = http->auth_nonce;
1093 const char *opaque = http->auth_opaque;
1094 static const char colon = ':';
1095 uint8_t ctx[MD5_CTX_SIZE];
1096 uint8_t digest[MD5_DIGEST_SIZE];
1097 char ha1[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1098 char ha2[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1099 char response[ base16_encoded_len ( sizeof ( digest ) ) + 1 /* NUL */ ];
1104 assert ( user != NULL );
1105 assert ( realm != NULL );
1106 assert ( nonce != NULL );
1109 digest_init ( &md5_algorithm, ctx );
1110 digest_update ( &md5_algorithm, ctx, user, strlen ( user ) );
1111 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1112 digest_update ( &md5_algorithm, ctx, realm, strlen ( realm ) );
1113 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1114 digest_update ( &md5_algorithm, ctx, password, strlen ( password ) );
1115 digest_final ( &md5_algorithm, ctx, digest );
1116 base16_encode ( digest, sizeof ( digest ), ha1 );
1119 digest_init ( &md5_algorithm, ctx );
1120 digest_update ( &md5_algorithm, ctx, method, strlen ( method ) );
1121 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1122 digest_update ( &md5_algorithm, ctx, uri, strlen ( uri ) );
1123 digest_final ( &md5_algorithm, ctx, digest );
1124 base16_encode ( digest, sizeof ( digest ), ha2 );
1126 /* Generate response */
1127 digest_init ( &md5_algorithm, ctx );
1128 digest_update ( &md5_algorithm, ctx, ha1, strlen ( ha1 ) );
1129 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1130 digest_update ( &md5_algorithm, ctx, nonce, strlen ( nonce ) );
1131 digest_update ( &md5_algorithm, ctx, &colon, sizeof ( colon ) );
1132 digest_update ( &md5_algorithm, ctx, ha2, strlen ( ha2 ) );
1133 digest_final ( &md5_algorithm, ctx, digest );
1134 base16_encode ( digest, sizeof ( digest ), response );
1136 /* Generate the authorisation string */
1137 len = asprintf ( &auth, "Authorization: Digest username=\"%s\", "
1138 "realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
1139 "%s%s%sresponse=\"%s\"\r\n", user, realm, nonce, uri,
1140 ( opaque ? "opaque=\"" : "" ),
1141 ( opaque ? opaque : "" ),
1142 ( opaque ? "\", " : "" ), response );
1150 * Generate HTTP POST parameter list
1152 * @v http HTTP request
1153 * @v buf Buffer to contain HTTP POST parameters
1154 * @v len Length of buffer
1155 * @ret len Length of parameter list (excluding terminating NUL)
1157 static size_t http_post_params ( struct http_request *http,
1158 char *buf, size_t len ) {
1159 struct parameter *param;
1160 ssize_t remaining = len;
1163 /* Add each parameter in the form "key=value", joined with "&" */
1165 for_each_param ( param, http->uri->params ) {
1167 /* Add the "&", if applicable */
1169 if ( remaining > 0 )
1176 /* URI-encode the key */
1177 frag_len = uri_encode ( param->key, 0, buf, remaining );
1180 remaining -= frag_len;
1183 if ( remaining > 0 )
1189 /* URI-encode the value */
1190 frag_len = uri_encode ( param->value, 0, buf, remaining );
1193 remaining -= frag_len;
1196 /* Ensure string is NUL-terminated even if no parameters are present */
1197 if ( remaining > 0 )
1204 * Generate HTTP POST body
1206 * @v http HTTP request
1207 * @ret post I/O buffer containing POST body, or NULL on error
1209 static struct io_buffer * http_post ( struct http_request *http ) {
1210 struct io_buffer *post;
1214 /* Calculate length of parameter list */
1215 len = http_post_params ( http, NULL, 0 );
1217 /* Allocate parameter list */
1218 post = alloc_iob ( len + 1 /* NUL */ );
1222 /* Fill parameter list */
1223 check_len = http_post_params ( http, iob_put ( post, len ),
1224 ( len + 1 /* NUL */ ) );
1225 assert ( len == check_len );
1226 DBGC ( http, "HTTP %p POST %s\n", http, ( ( char * ) post->data ) );
1234 * @v http HTTP request
1236 static void http_step ( struct http_request *http ) {
1237 struct io_buffer *post;
1238 struct uri host_uri;
1239 struct uri path_uri;
1240 char *host_uri_string;
1241 char *path_uri_string;
1249 /* Do nothing if we have already transmitted the request */
1250 if ( ! ( http->flags & HTTP_TX_PENDING ) )
1253 /* Do nothing until socket is ready */
1254 if ( ! xfer_window ( &http->socket ) )
1257 /* Force a HEAD request if we have nowhere to send any received data */
1258 if ( ( xfer_window ( &http->xfer ) == 0 ) &&
1259 ( http->rx_buffer == UNULL ) ) {
1260 http->flags |= ( HTTP_HEAD_ONLY | HTTP_CLIENT_KEEPALIVE );
1263 /* Determine method */
1264 method = ( ( http->flags & HTTP_HEAD_ONLY ) ? "HEAD" :
1265 ( http->uri->params ? "POST" : "GET" ) );
1267 /* Construct host URI */
1268 memset ( &host_uri, 0, sizeof ( host_uri ) );
1269 host_uri.host = http->uri->host;
1270 host_uri.port = http->uri->port;
1271 host_uri_string = format_uri_alloc ( &host_uri );
1272 if ( ! host_uri_string ) {
1277 /* Construct path URI */
1278 memset ( &path_uri, 0, sizeof ( path_uri ) );
1279 path_uri.path = ( http->uri->path ? http->uri->path : "/" );
1280 path_uri.query = http->uri->query;
1281 path_uri_string = format_uri_alloc ( &path_uri );
1282 if ( ! path_uri_string ) {
1287 /* Calculate range request parameters if applicable */
1288 if ( http->partial_len ) {
1289 len = asprintf ( &range, "Range: bytes=%zd-%zd\r\n",
1290 http->partial_start,
1291 ( http->partial_start + http->partial_len
1301 /* Construct authorisation, if applicable */
1302 if ( http->flags & HTTP_BASIC_AUTH ) {
1303 auth = http_basic_auth ( http );
1308 } else if ( http->flags & HTTP_DIGEST_AUTH ) {
1309 auth = http_digest_auth ( http, method, path_uri_string );
1318 /* Construct POST content, if applicable */
1319 if ( http->uri->params ) {
1320 post = http_post ( http );
1325 len = asprintf ( &content, "Content-Type: "
1326 "application/x-www-form-urlencoded\r\n"
1327 "Content-Length: %zd\r\n", iob_len ( post ) );
1337 /* Mark request as transmitted */
1338 http->flags &= ~HTTP_TX_PENDING;
1341 if ( ( rc = xfer_printf ( &http->socket,
1342 "%s %s HTTP/1.1\r\n"
1343 "User-Agent: iPXE/%s\r\n"
1347 method, path_uri_string, product_version,
1349 ( ( http->flags & HTTP_CLIENT_KEEPALIVE ) ?
1350 "Connection: keep-alive\r\n" : "" ),
1351 ( range ? range : "" ),
1352 ( auth ? auth : "" ),
1353 ( content ? content : "" ) ) ) != 0 ) {
1357 /* Send POST content, if applicable */
1359 if ( ( rc = xfer_deliver_iob ( &http->socket,
1360 iob_disown ( post ) ) ) != 0 )
1374 free ( path_uri_string );
1376 free ( host_uri_string );
1379 http_close ( http, rc );
1383 * Check HTTP data transfer flow control window
1385 * @v http HTTP request
1386 * @ret len Length of window
1388 static size_t http_xfer_window ( struct http_request *http ) {
1390 /* New block commands may be issued only when we are idle */
1391 return ( ( http->rx_state == HTTP_RX_IDLE ) ? 1 : 0 );
1395 * Initiate HTTP partial read
1397 * @v http HTTP request
1398 * @v partial Partial transfer interface
1399 * @v offset Starting offset
1400 * @v buffer Data buffer
1402 * @ret rc Return status code
1404 static int http_partial_read ( struct http_request *http,
1405 struct interface *partial,
1406 size_t offset, userptr_t buffer, size_t len ) {
1409 if ( http_xfer_window ( http ) == 0 )
1412 /* Initialise partial transfer parameters */
1413 http->rx_buffer = buffer;
1414 http->partial_start = offset;
1415 http->partial_len = len;
1417 /* Schedule request */
1418 http->rx_state = HTTP_RX_RESPONSE;
1419 http->flags = ( HTTP_TX_PENDING | HTTP_CLIENT_KEEPALIVE );
1421 http->flags |= HTTP_HEAD_ONLY;
1422 process_add ( &http->process );
1424 /* Attach to parent interface and return */
1425 intf_plug_plug ( &http->partial, partial );
1431 * Issue HTTP block device read
1433 * @v http HTTP request
1434 * @v block Block data interface
1435 * @v lba Starting logical block address
1436 * @v count Number of blocks to transfer
1437 * @v buffer Data buffer
1438 * @v len Length of data buffer
1439 * @ret rc Return status code
1441 static int http_block_read ( struct http_request *http,
1442 struct interface *block,
1443 uint64_t lba, unsigned int count,
1444 userptr_t buffer, size_t len __unused ) {
1446 return http_partial_read ( http, block, ( lba * HTTP_BLKSIZE ),
1447 buffer, ( count * HTTP_BLKSIZE ) );
1451 * Read HTTP block device capacity
1453 * @v http HTTP request
1454 * @v block Block data interface
1455 * @ret rc Return status code
1457 static int http_block_read_capacity ( struct http_request *http,
1458 struct interface *block ) {
1460 return http_partial_read ( http, block, 0, 0, 0 );
1464 * Describe HTTP device in an ACPI table
1466 * @v http HTTP request
1467 * @v acpi ACPI table
1468 * @v len Length of ACPI table
1469 * @ret rc Return status code
1471 static int http_acpi_describe ( struct http_request *http,
1472 struct acpi_description_header *acpi,
1475 DBGC ( http, "HTTP %p cannot yet describe device in an ACPI table\n",
1482 /** HTTP socket interface operations */
1483 static struct interface_operation http_socket_operations[] = {
1484 INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
1485 INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
1486 INTF_OP ( xfer_window_changed, struct http_request *, http_step ),
1487 INTF_OP ( intf_close, struct http_request *, http_socket_close ),
1490 /** HTTP socket interface descriptor */
1491 static struct interface_descriptor http_socket_desc =
1492 INTF_DESC_PASSTHRU ( struct http_request, socket,
1493 http_socket_operations, xfer );
1495 /** HTTP partial transfer interface operations */
1496 static struct interface_operation http_partial_operations[] = {
1497 INTF_OP ( intf_close, struct http_request *, http_close ),
1500 /** HTTP partial transfer interface descriptor */
1501 static struct interface_descriptor http_partial_desc =
1502 INTF_DESC ( struct http_request, partial, http_partial_operations );
1504 /** HTTP data transfer interface operations */
1505 static struct interface_operation http_xfer_operations[] = {
1506 INTF_OP ( xfer_window, struct http_request *, http_xfer_window ),
1507 INTF_OP ( block_read, struct http_request *, http_block_read ),
1508 INTF_OP ( block_read_capacity, struct http_request *,
1509 http_block_read_capacity ),
1510 INTF_OP ( intf_close, struct http_request *, http_close ),
1511 INTF_OP ( acpi_describe, struct http_request *, http_acpi_describe ),
1514 /** HTTP data transfer interface descriptor */
1515 static struct interface_descriptor http_xfer_desc =
1516 INTF_DESC_PASSTHRU ( struct http_request, xfer,
1517 http_xfer_operations, socket );
1519 /** HTTP process descriptor */
1520 static struct process_descriptor http_process_desc =
1521 PROC_DESC_ONCE ( struct http_request, process, http_step );
1524 * Initiate an HTTP connection, with optional filter
1526 * @v xfer Data transfer interface
1527 * @v uri Uniform Resource Identifier
1528 * @v default_port Default port number
1529 * @v filter Filter to apply to socket, or NULL
1530 * @ret rc Return status code
1532 int http_open_filter ( struct interface *xfer, struct uri *uri,
1533 unsigned int default_port,
1534 int ( * filter ) ( struct interface *xfer,
1536 struct interface **next ) ) {
1537 struct http_request *http;
1544 /* Allocate and populate HTTP structure */
1545 http = zalloc ( sizeof ( *http ) );
1548 ref_init ( &http->refcnt, http_free );
1549 intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
1550 intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
1551 http->uri = uri_get ( uri );
1552 http->default_port = default_port;
1553 http->filter = filter;
1554 intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
1555 process_init ( &http->process, &http_process_desc, &http->refcnt );
1556 timer_init ( &http->timer, http_retry, &http->refcnt );
1557 http->flags = HTTP_TX_PENDING;
1560 if ( ( rc = http_socket_open ( http ) ) != 0 )
1563 /* Attach to parent interface, mortalise self, and return */
1564 intf_plug_plug ( &http->xfer, xfer );
1565 ref_put ( &http->refcnt );
1569 DBGC ( http, "HTTP %p could not create request: %s\n",
1570 http, strerror ( rc ) );
1571 http_close ( http, rc );
1572 ref_put ( &http->refcnt );