tizen 2.3.1 release
[external/qemu.git] / roms / ipxe / src / net / tcp / httpcore.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /**
23  * @file
24  *
25  * Hyper Text Transfer Protocol (HTTP) core functionality
26  *
27  */
28
29 #include <stdint.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <strings.h>
34 #include <byteswap.h>
35 #include <errno.h>
36 #include <assert.h>
37 #include <ipxe/uri.h>
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>
48 #include <ipxe/md5.h>
49 #include <ipxe/blockdev.h>
50 #include <ipxe/acpi.h>
51 #include <ipxe/version.h>
52 #include <ipxe/http.h>
53
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" )
85
86 /** Block size used for HTTP block device request */
87 #define HTTP_BLKSIZE 512
88
89 /** HTTP flags */
90 enum http_flags {
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,
105 };
106
107 /** HTTP receive state */
108 enum http_rx_state {
109         HTTP_RX_RESPONSE = 0,
110         HTTP_RX_HEADER,
111         HTTP_RX_CHUNK_LEN,
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
114          * transfer).
115          */
116         HTTP_RX_DATA,
117         /* In the following states, it is acceptable for the server to
118          * close the connection.
119          */
120         HTTP_RX_TRAILER,
121         HTTP_RX_IDLE,
122         HTTP_RX_DEAD,
123 };
124
125 /**
126  * An HTTP request
127  *
128  */
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;
136
137         /** URI being fetched */
138         struct uri *uri;
139         /** Default port */
140         unsigned int default_port;
141         /** Filter (if any) */
142         int ( * filter ) ( struct interface *xfer,
143                            const char *name,
144                            struct interface **next );
145         /** Transport layer interface */
146         struct interface socket;
147
148         /** Flags */
149         unsigned int flags;
150         /** Starting offset of partial transfer (if applicable) */
151         size_t partial_start;
152         /** Length of partial transfer (if applicable) */
153         size_t partial_len;
154
155         /** TX process */
156         struct process process;
157
158         /** RX state */
159         enum http_rx_state rx_state;
160         /** Response code */
161         unsigned int code;
162         /** Received length */
163         size_t rx_len;
164         /** Length remaining (or 0 if unknown) */
165         size_t remaining;
166         /** HTTP is using Transfer-Encoding: chunked */
167         int 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) */
173         userptr_t rx_buffer;
174
175         /** Authentication realm (if any) */
176         char *auth_realm;
177         /** Authentication nonce (if any) */
178         char *auth_nonce;
179         /** Authentication opaque string (if any) */
180         char *auth_opaque;
181 };
182
183 /**
184  * Free HTTP request
185  *
186  * @v refcnt            Reference counter
187  */
188 static void http_free ( struct refcnt *refcnt ) {
189         struct http_request *http =
190                 container_of ( refcnt, struct http_request, refcnt );
191
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 );
197         free ( http );
198 };
199
200 /**
201  * Close HTTP request
202  *
203  * @v http              HTTP request
204  * @v rc                Return status code
205  */
206 static void http_close ( struct http_request *http, int rc ) {
207
208         /* Prevent further processing of any current packet */
209         http->rx_state = HTTP_RX_DEAD;
210
211         /* Prevent reconnection */
212         http->flags &= ~HTTP_CLIENT_KEEPALIVE;
213
214         /* Remove process */
215         process_del ( &http->process );
216
217         /* Close all data transfer interfaces */
218         intf_shutdown ( &http->socket, rc );
219         intf_shutdown ( &http->partial, rc );
220         intf_shutdown ( &http->xfer, rc );
221 }
222
223 /**
224  * Open HTTP socket
225  *
226  * @v http              HTTP request
227  * @ret rc              Return status code
228  */
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;
233         int rc;
234
235         /* Open 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 )
241                         return rc;
242         }
243         if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
244                                              ( struct sockaddr * ) &server,
245                                              uri->host, NULL ) ) != 0 )
246                 return rc;
247
248         return 0;
249 }
250
251 /**
252  * Mark HTTP request as completed successfully
253  *
254  * @v http              HTTP request
255  */
256 static void http_done ( struct http_request *http ) {
257         int rc;
258
259         /* If we are not at an appropriate stage of the protocol
260          * (including being in the middle of a chunked transfer),
261          * force an error.
262          */
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 );
267                 return;
268         }
269
270         /* If we had a Content-Length, and the received content length
271          * isn't correct, force an error
272          */
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 );
277                 return;
278         }
279
280         /* Enter idle state */
281         http->rx_state = HTTP_RX_IDLE;
282         http->rx_len = 0;
283         assert ( http->remaining == 0 );
284         assert ( http->chunked == 0 );
285         assert ( http->chunk_remaining == 0 );
286
287         /* Close partial transfer interface */
288         if ( ! ( http->flags & HTTP_TRY_AGAIN ) )
289                 intf_restart ( &http->partial, 0 );
290
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 );
294                 return;
295         }
296
297         /* If the server is not intending to keep the connection
298          * alive, then reopen the socket.
299          */
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 );
305                         return;
306                 }
307         }
308         http->flags &= ~HTTP_SERVER_KEEPALIVE;
309
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 );
316         }
317 }
318
319 /**
320  * Convert HTTP response code to return status code
321  *
322  * @v response          HTTP response code
323  * @ret rc              Return status code
324  */
325 static int http_response_to_rc ( unsigned int response ) {
326         switch ( response ) {
327         case 200:
328         case 206:
329         case 301:
330         case 302:
331         case 303:
332                 return 0;
333         case 404:
334                 return -ENOENT_404;
335         case 403:
336                 return -EPERM_403;
337         case 401:
338                 return -EACCES_401;
339         default:
340                 return -EIO_OTHER;
341         }
342 }
343
344 /**
345  * Handle HTTP response
346  *
347  * @v http              HTTP request
348  * @v response          HTTP response
349  * @ret rc              Return status code
350  */
351 static int http_rx_response ( struct http_request *http, char *response ) {
352         char *spc;
353
354         DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
355
356         /* Check response starts with "HTTP/" */
357         if ( strncmp ( response, "HTTP/", 5 ) != 0 )
358                 return -EINVAL_RESPONSE;
359
360         /* Locate and store response code */
361         spc = strchr ( response, ' ' );
362         if ( ! spc )
363                 return -EINVAL_RESPONSE;
364         http->code = strtoul ( spc, NULL, 10 );
365
366         /* Move to receive headers */
367         http->rx_state = ( ( http->flags & HTTP_HEAD_ONLY ) ?
368                            HTTP_RX_TRAILER : HTTP_RX_HEADER );
369         return 0;
370 }
371
372 /**
373  * Handle HTTP Location header
374  *
375  * @v http              HTTP request
376  * @v value             HTTP header value
377  * @ret rc              Return status code
378  */
379 static int http_rx_location ( struct http_request *http, char *value ) {
380         int rc;
381
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,
385                                     value ) ) != 0 ) {
386                 DBGC ( http, "HTTP %p could not redirect: %s\n",
387                        http, strerror ( rc ) );
388                 return rc;
389         }
390
391         return 0;
392 }
393
394 /**
395  * Handle HTTP Content-Length header
396  *
397  * @v http              HTTP request
398  * @v value             HTTP header value
399  * @ret rc              Return status code
400  */
401 static int http_rx_content_length ( struct http_request *http, char *value ) {
402         struct block_device_capacity capacity;
403         size_t content_len;
404         char *endp;
405
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",
410                        http, value );
411                 return -EINVAL_CONTENT_LENGTH;
412         }
413
414         /* If we already have an expected content length, and this
415          * isn't it, then complain
416          */
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;
421         }
422         if ( ! ( http->flags & HTTP_HEAD_ONLY ) )
423                 http->remaining = content_len;
424
425         /* Do nothing more if we are retrying the request */
426         if ( http->flags & HTTP_TRY_AGAIN )
427                 return 0;
428
429         /* Use seek() to notify recipient of filesize */
430         xfer_seek ( &http->xfer, http->remaining );
431         xfer_seek ( &http->xfer, 0 );
432
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 );
439         }
440         return 0;
441 }
442
443 /**
444  * Handle HTTP Transfer-Encoding header
445  *
446  * @v http              HTTP request
447  * @v value             HTTP header value
448  * @ret rc              Return status code
449  */
450 static int http_rx_transfer_encoding ( struct http_request *http, char *value ){
451
452         if ( strcasecmp ( value, "chunked" ) == 0 ) {
453                 /* Mark connection as using chunked transfer encoding */
454                 http->chunked = 1;
455         }
456
457         return 0;
458 }
459
460 /**
461  * Handle HTTP Connection header
462  *
463  * @v http              HTTP request
464  * @v value             HTTP header value
465  * @ret rc              Return status code
466  */
467 static int http_rx_connection ( struct http_request *http, char *value ) {
468
469         if ( strcasecmp ( value, "keep-alive" ) == 0 ) {
470                 /* Mark connection as being kept alive by the server */
471                 http->flags |= HTTP_SERVER_KEEPALIVE;
472         }
473
474         return 0;
475 }
476
477 /**
478  * Handle WWW-Authenticate Basic header
479  *
480  * @v http              HTTP request
481  * @v params            Parameters
482  * @ret rc              Return status code
483  */
484 static int http_rx_basic_auth ( struct http_request *http, char *params ) {
485
486         DBGC ( http, "HTTP %p Basic authentication required (%s)\n",
487                http, params );
488
489         /* If we received a 401 Unauthorized response, then retry
490          * using Basic authentication
491          */
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 );
496         }
497
498         return 0;
499 }
500
501 /**
502  * Parse Digest authentication parameter
503  *
504  * @v params            Parameters
505  * @v name              Parameter name (including trailing "=\"")
506  * @ret value           Parameter value, or NULL
507  */
508 static char * http_digest_param ( char *params, const char *name ) {
509         char *key;
510         char *value;
511         char *terminator;
512
513         /* Locate parameter */
514         key = strstr ( params, name );
515         if ( ! key )
516                 return NULL;
517
518         /* Extract value */
519         value = ( key + strlen ( name ) );
520         terminator = strchr ( value, '"' );
521         if ( ! terminator )
522                 return NULL;
523         return strndup ( value, ( terminator - value ) );
524 }
525
526 /**
527  * Handle WWW-Authenticate Digest header
528  *
529  * @v http              HTTP request
530  * @v params            Parameters
531  * @ret rc              Return status code
532  */
533 static int http_rx_digest_auth ( struct http_request *http, char *params ) {
534
535         DBGC ( http, "HTTP %p Digest authentication required (%s)\n",
536                http, params );
537
538         /* If we received a 401 Unauthorized response, then retry
539          * using Digest authentication
540          */
541         if ( ( http->code == 401 ) &&
542              ( ! ( http->flags & HTTP_DIGEST_AUTH ) ) &&
543              ( http->uri->user != NULL ) ) {
544
545                 /* Extract realm */
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",
550                                http );
551                         return -EINVAL_HEADER;
552                 }
553
554                 /* Extract nonce */
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",
559                                http );
560                         return -EINVAL_HEADER;
561                 }
562
563                 /* Extract opaque */
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 */
568                 }
569
570                 http->flags |= ( HTTP_TRY_AGAIN | HTTP_DIGEST_AUTH );
571         }
572
573         return 0;
574 }
575
576 /** An HTTP WWW-Authenticate header handler */
577 struct http_auth_header_handler {
578         /** Scheme (e.g. "Basic") */
579         const char *scheme;
580         /** Handle received parameters
581          *
582          * @v http      HTTP request
583          * @v params    Parameters
584          * @ret rc      Return status code
585          */
586         int ( * rx ) ( struct http_request *http, char *params );
587 };
588
589 /** List of HTTP WWW-Authenticate header handlers */
590 static struct http_auth_header_handler http_auth_header_handlers[] = {
591         {
592                 .scheme = "Basic",
593                 .rx = http_rx_basic_auth,
594         },
595         {
596                 .scheme = "Digest",
597                 .rx = http_rx_digest_auth,
598         },
599         { NULL, NULL },
600 };
601
602 /**
603  * Handle HTTP WWW-Authenticate header
604  *
605  * @v http              HTTP request
606  * @v value             HTTP header value
607  * @ret rc              Return status code
608  */
609 static int http_rx_www_authenticate ( struct http_request *http, char *value ) {
610         struct http_auth_header_handler *handler;
611         char *separator;
612         char *scheme;
613         char *params;
614         int rc;
615
616         /* Extract scheme */
617         separator = strchr ( value, ' ' );
618         if ( ! separator ) {
619                 DBGC ( http, "HTTP %p malformed WWW-Authenticate header\n",
620                        http );
621                 return -EINVAL_HEADER;
622         }
623         *separator = '\0';
624         scheme = value;
625         params = ( separator + 1 );
626
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 )
631                                 return rc;
632                         break;
633                 }
634         }
635         return 0;
636 }
637
638 /** An HTTP header handler */
639 struct http_header_handler {
640         /** Name (e.g. "Content-Length") */
641         const char *header;
642         /** Handle received header
643          *
644          * @v http      HTTP request
645          * @v value     HTTP header value
646          * @ret rc      Return status code
647          *
648          * If an error is returned, the download will be aborted.
649          */
650         int ( * rx ) ( struct http_request *http, char *value );
651 };
652
653 /** List of HTTP header handlers */
654 static struct http_header_handler http_header_handlers[] = {
655         {
656                 .header = "Location",
657                 .rx = http_rx_location,
658         },
659         {
660                 .header = "Content-Length",
661                 .rx = http_rx_content_length,
662         },
663         {
664                 .header = "Transfer-Encoding",
665                 .rx = http_rx_transfer_encoding,
666         },
667         {
668                 .header = "Connection",
669                 .rx = http_rx_connection,
670         },
671         {
672                 .header = "WWW-Authenticate",
673                 .rx = http_rx_www_authenticate,
674         },
675         { NULL, NULL }
676 };
677
678 /**
679  * Handle HTTP header
680  *
681  * @v http              HTTP request
682  * @v header            HTTP header
683  * @ret rc              Return status code
684  */
685 static int http_rx_header ( struct http_request *http, char *header ) {
686         struct http_header_handler *handler;
687         char *separator;
688         char *value;
689         int rc;
690
691         /* An empty header line marks the end of this phase */
692         if ( ! header[0] ) {
693                 empty_line_buffer ( &http->linebuf );
694
695                 /* Handle response code */
696                 if ( ! ( http->flags & HTTP_TRY_AGAIN ) ) {
697                         if ( ( rc = http_response_to_rc ( http->code ) ) != 0 )
698                                 return rc;
699                 }
700
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;
709                         }
710                         return 0;
711                 } else {
712                         DBGC ( http, "HTTP %p end of trailer\n", http );
713                         http_done ( http );
714                         return 0;
715                 }
716         }
717
718         DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
719
720         /* Split header at the ": " */
721         separator = strstr ( header, ": " );
722         if ( ! separator ) {
723                 DBGC ( http, "HTTP %p malformed header\n", http );
724                 return -EINVAL_HEADER;
725         }
726         *separator = '\0';
727         value = ( separator + 2 );
728
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 )
733                                 return rc;
734                         break;
735                 }
736         }
737         return 0;
738 }
739
740 /**
741  * Handle HTTP chunk length
742  *
743  * @v http              HTTP request
744  * @v length            HTTP chunk length
745  * @ret rc              Return status code
746  */
747 static int http_rx_chunk_len ( struct http_request *http, char *length ) {
748         char *endp;
749
750         /* Skip blank lines between chunks */
751         if ( length[0] == '\0' )
752                 return 0;
753
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",
758                        http, length );
759                 return -EINVAL_CHUNK_LENGTH;
760         }
761
762         /* Terminate chunked encoding if applicable */
763         if ( http->chunk_remaining == 0 ) {
764                 DBGC ( http, "HTTP %p end of chunks\n", http );
765                 http->chunked = 0;
766                 http->rx_state = HTTP_RX_TRAILER;
767                 return 0;
768         }
769
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 );
777         }
778
779         /* Start receiving data */
780         http->rx_state = HTTP_RX_DATA;
781
782         return 0;
783 }
784
785 /** An HTTP line-based data handler */
786 struct http_line_handler {
787         /** Handle line
788          *
789          * @v http      HTTP request
790          * @v line      Line to handle
791          * @ret rc      Return status code
792          */
793         int ( * rx ) ( struct http_request *http, char *line );
794 };
795
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 },
802 };
803
804 /**
805  * Handle new data arriving via HTTP connection
806  *
807  * @v http              HTTP request
808  * @v iobuf             I/O buffer
809  * @v meta              Data transfer metadata
810  * @ret rc              Return status code
811  */
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;
816         char *line;
817         size_t data_len;
818         ssize_t line_len;
819         int rc = 0;
820
821         while ( iobuf && iob_len ( iobuf ) ) {
822
823                 switch ( http->rx_state ) {
824                 case HTTP_RX_IDLE:
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 ) ?
829                                  "idle" : "dead" ) );
830                         rc = -EPROTO_UNSOLICITED;
831                         goto done;
832                 case HTTP_RX_DEAD:
833                         /* Do no further processing */
834                         goto done;
835                 case HTTP_RX_DATA:
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;
841                         }
842                         if ( http->remaining &&
843                              ( http->remaining < data_len ) ) {
844                                 data_len = http->remaining;
845                         }
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 );
859                                 if ( rc != 0 )
860                                         goto done;
861                         } else {
862                                 /* Deliver whole I/O buffer */
863                                 if ( ( rc = xfer_deliver_iob ( &http->xfer,
864                                                  iob_disown ( iobuf ) ) ) != 0 )
865                                         goto done;
866                         }
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;
872                         }
873                         if ( http->remaining ) {
874                                 http->remaining -= data_len;
875                                 if ( ( http->remaining == 0 ) &&
876                                      ( http->rx_state == HTTP_RX_DATA ) ) {
877                                         http_done ( http );
878                                 }
879                         }
880                         break;
881                 case HTTP_RX_RESPONSE:
882                 case HTTP_RX_HEADER:
883                 case HTTP_RX_CHUNK_LEN:
884                 case HTTP_RX_TRAILER:
885                         /* In the other phases, buffer and process a
886                          * line at a time
887                          */
888                         line_len = line_buffer ( &http->linebuf, iobuf->data,
889                                                  iob_len ( iobuf ) );
890                         if ( line_len < 0 ) {
891                                 rc = line_len;
892                                 DBGC ( http, "HTTP %p could not buffer line: "
893                                        "%s\n", http, strerror ( rc ) );
894                                 goto done;
895                         }
896                         iob_pull ( iobuf, line_len );
897                         line = buffered_line ( &http->linebuf );
898                         if ( line ) {
899                                 lh = &http_line_handlers[http->rx_state];
900                                 if ( ( rc = lh->rx ( http, line ) ) != 0 )
901                                         goto done;
902                         }
903                         break;
904                 default:
905                         assert ( 0 );
906                         break;
907                 }
908         }
909
910  done:
911         if ( rc )
912                 http_close ( http, rc );
913         free_iob ( iobuf );
914         return rc;
915 }
916
917 /**
918  * Check HTTP socket flow control window
919  *
920  * @v http              HTTP request
921  * @ret len             Length of window
922  */
923 static size_t http_socket_window ( struct http_request *http __unused ) {
924
925         /* Window is always open.  This is to prevent TCP from
926          * stalling if our parent window is not currently open.
927          */
928         return ( ~( ( size_t ) 0 ) );
929 }
930
931 /**
932  * Close HTTP socket
933  *
934  * @v http              HTTP request
935  * @v rc                Reason for close
936  */
937 static void http_socket_close ( struct http_request *http, int rc ) {
938
939         /* If we have an error, terminate */
940         if ( rc != 0 ) {
941                 http_close ( http, rc );
942                 return;
943         }
944
945         /* Mark HTTP request as complete */
946         http_done ( http );
947 }
948
949 /**
950  * Generate HTTP Basic authorisation string
951  *
952  * @v http              HTTP request
953  * @ret auth            Authorisation string, or NULL on error
954  *
955  * The authorisation string is dynamically allocated, and must be
956  * freed by the caller.
957  */
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 : "" );
962         size_t user_pw_len =
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 */ ];
967         char *auth;
968         int len;
969
970         /* Sanity check */
971         assert ( user != NULL );
972
973         /* Make "user:password" string from decoded fields */
974         snprintf ( user_pw, sizeof ( user_pw ), "%s:%s", user, password );
975
976         /* Base64-encode the "user:password" string */
977         base64_encode ( ( void * ) user_pw, user_pw_len, user_pw_base64 );
978
979         /* Generate the authorisation string */
980         len = asprintf ( &auth, "Authorization: Basic %s\r\n",
981                          user_pw_base64 );
982         if ( len < 0 )
983                 return NULL;
984
985         return auth;
986 }
987
988 /**
989  * Generate HTTP Digest authorisation string
990  *
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
995  *
996  * The authorisation string is dynamically allocated, and must be
997  * freed by the caller.
998  */
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 */ ];
1013         char *auth;
1014         int len;
1015
1016         /* Sanity checks */
1017         assert ( user != NULL );
1018         assert ( realm != NULL );
1019         assert ( nonce != NULL );
1020
1021         /* Generate HA1 */
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 );
1030
1031         /* Generate HA2 */
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 );
1038
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 );
1048
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 );
1056         if ( len < 0 )
1057                 return NULL;
1058
1059         return auth;
1060 }
1061
1062 /**
1063  * HTTP process
1064  *
1065  * @v http              HTTP request
1066  */
1067 static void http_step ( struct http_request *http ) {
1068         size_t uri_len;
1069         char *method;
1070         char *uri;
1071         char *range;
1072         char *auth;
1073         int len;
1074         int rc;
1075
1076         /* Do nothing if we have already transmitted the request */
1077         if ( ! ( http->flags & HTTP_TX_PENDING ) )
1078                 return;
1079
1080         /* Do nothing until socket is ready */
1081         if ( ! xfer_window ( &http->socket ) )
1082                 return;
1083
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 );
1088         }
1089
1090         /* Determine method */
1091         method = ( ( http->flags & HTTP_HEAD_ONLY ) ? "HEAD" : "GET" );
1092
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 );
1098         if ( ! uri ) {
1099                 rc = -ENOMEM;
1100                 goto err_uri;
1101         }
1102         unparse_uri ( uri, uri_len, http->uri, URI_PATH_BIT | URI_QUERY_BIT );
1103         if ( ! uri[0] ) {
1104                 uri[0] = '/';
1105                 uri[1] = '\0';
1106         }
1107
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
1113                                    - 1 ) );
1114                 if ( len < 0 ) {
1115                         rc = len;
1116                         goto err_range;
1117                 }
1118         } else {
1119                 range = NULL;
1120         }
1121
1122         /* Construct authorisation, if applicable */
1123         if ( http->flags & HTTP_BASIC_AUTH ) {
1124                 auth = http_basic_auth ( http );
1125                 if ( ! auth ) {
1126                         rc = -ENOMEM;
1127                         goto err_auth;
1128                 }
1129         } else if ( http->flags & HTTP_DIGEST_AUTH ) {
1130                 auth = http_digest_auth ( http, method, uri );
1131                 if ( ! auth ) {
1132                         rc = -ENOMEM;
1133                         goto err_auth;
1134                 }
1135         } else {
1136                 auth = NULL;
1137         }
1138
1139         /* Mark request as transmitted */
1140         http->flags &= ~HTTP_TX_PENDING;
1141
1142         /* Send request */
1143         if ( ( rc = xfer_printf ( &http->socket,
1144                                   "%s %s HTTP/1.1\r\n"
1145                                   "User-Agent: iPXE/%s\r\n"
1146                                   "Host: %s%s%s\r\n"
1147                                   "%s%s%s"
1148                                   "\r\n",
1149                                   method, uri, product_version, http->uri->host,
1150                                   ( http->uri->port ?
1151                                     ":" : "" ),
1152                                   ( http->uri->port ?
1153                                     http->uri->port : "" ),
1154                                   ( ( http->flags & HTTP_CLIENT_KEEPALIVE ) ?
1155                                     "Connection: keep-alive\r\n" : "" ),
1156                                   ( range ? range : "" ),
1157                                   ( auth ? auth : "" ) ) ) != 0 ) {
1158                 goto err_xfer;
1159         }
1160
1161  err_xfer:
1162         free ( auth );
1163  err_auth:
1164         free ( range );
1165  err_range:
1166         free ( uri );
1167  err_uri:
1168         if ( rc != 0 )
1169                 http_close ( http, rc );
1170 }
1171
1172 /**
1173  * Check HTTP data transfer flow control window
1174  *
1175  * @v http              HTTP request
1176  * @ret len             Length of window
1177  */
1178 static size_t http_xfer_window ( struct http_request *http ) {
1179
1180         /* New block commands may be issued only when we are idle */
1181         return ( ( http->rx_state == HTTP_RX_IDLE ) ? 1 : 0 );
1182 }
1183
1184 /**
1185  * Initiate HTTP partial read
1186  *
1187  * @v http              HTTP request
1188  * @v partial           Partial transfer interface
1189  * @v offset            Starting offset
1190  * @v buffer            Data buffer
1191  * @v len               Length
1192  * @ret rc              Return status code
1193  */
1194 static int http_partial_read ( struct http_request *http,
1195                                struct interface *partial,
1196                                size_t offset, userptr_t buffer, size_t len ) {
1197
1198         /* Sanity check */
1199         if ( http_xfer_window ( http ) == 0 )
1200                 return -EBUSY;
1201
1202         /* Initialise partial transfer parameters */
1203         http->rx_buffer = buffer;
1204         http->partial_start = offset;
1205         http->partial_len = len;
1206
1207         /* Schedule request */
1208         http->rx_state = HTTP_RX_RESPONSE;
1209         http->flags = ( HTTP_TX_PENDING | HTTP_CLIENT_KEEPALIVE );
1210         if ( ! len )
1211                 http->flags |= HTTP_HEAD_ONLY;
1212         process_add ( &http->process );
1213
1214         /* Attach to parent interface and return */
1215         intf_plug_plug ( &http->partial, partial );
1216
1217         return 0;
1218 }
1219
1220 /**
1221  * Issue HTTP block device read
1222  *
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
1230  */
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 ) {
1235
1236         return http_partial_read ( http, block, ( lba * HTTP_BLKSIZE ),
1237                                    buffer, ( count * HTTP_BLKSIZE ) );
1238 }
1239
1240 /**
1241  * Read HTTP block device capacity
1242  *
1243  * @v http              HTTP request
1244  * @v block             Block data interface
1245  * @ret rc              Return status code
1246  */
1247 static int http_block_read_capacity ( struct http_request *http,
1248                                       struct interface *block ) {
1249
1250         return http_partial_read ( http, block, 0, 0, 0 );
1251 }
1252
1253 /**
1254  * Describe HTTP device in an ACPI table
1255  *
1256  * @v http              HTTP request
1257  * @v acpi              ACPI table
1258  * @v len               Length of ACPI table
1259  * @ret rc              Return status code
1260  */
1261 static int http_acpi_describe ( struct http_request *http,
1262                                 struct acpi_description_header *acpi,
1263                                 size_t len ) {
1264
1265         DBGC ( http, "HTTP %p cannot yet describe device in an ACPI table\n",
1266                http );
1267         ( void ) acpi;
1268         ( void ) len;
1269         return 0;
1270 }
1271
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 ),
1278 };
1279
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 );
1284
1285 /** HTTP partial transfer interface operations */
1286 static struct interface_operation http_partial_operations[] = {
1287         INTF_OP ( intf_close, struct http_request *, http_close ),
1288 };
1289
1290 /** HTTP partial transfer interface descriptor */
1291 static struct interface_descriptor http_partial_desc =
1292         INTF_DESC ( struct http_request, partial, http_partial_operations );
1293
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 ),
1302 };
1303
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 );
1308
1309 /** HTTP process descriptor */
1310 static struct process_descriptor http_process_desc =
1311         PROC_DESC_ONCE ( struct http_request, process, http_step );
1312
1313 /**
1314  * Initiate an HTTP connection, with optional filter
1315  *
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
1321  */
1322 int http_open_filter ( struct interface *xfer, struct uri *uri,
1323                        unsigned int default_port,
1324                        int ( * filter ) ( struct interface *xfer,
1325                                           const char *name,
1326                                           struct interface **next ) ) {
1327         struct http_request *http;
1328         int rc;
1329
1330         /* Sanity checks */
1331         if ( ! uri->host )
1332                 return -EINVAL;
1333
1334         /* Allocate and populate HTTP structure */
1335         http = zalloc ( sizeof ( *http ) );
1336         if ( ! http )
1337                 return -ENOMEM;
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;
1347
1348         /* Open socket */
1349         if ( ( rc = http_socket_open ( http ) ) != 0 )
1350                 goto err;
1351
1352         /* Attach to parent interface, mortalise self, and return */
1353         intf_plug_plug ( &http->xfer, xfer );
1354         ref_put ( &http->refcnt );
1355         return 0;
1356
1357  err:
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 );
1362         return rc;
1363 }