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., 675 Mass Ave, Cambridge, MA 02139, USA.
19 FILE_LICENCE ( GPL2_OR_LATER
);
24 * Hyper Text Transfer Protocol (HTTP)
37 #include <gpxe/refcnt.h>
38 #include <gpxe/iobuf.h>
39 #include <gpxe/xfer.h>
40 #include <gpxe/open.h>
41 #include <gpxe/socket.h>
42 #include <gpxe/tcpip.h>
43 #include <gpxe/process.h>
44 #include <gpxe/linebuf.h>
45 #include <gpxe/features.h>
46 #include <gpxe/base64.h>
47 #include <gpxe/http.h>
49 FEATURE ( FEATURE_PROTOCOL
, "HTTP", DHCP_EB_FEATURE_HTTP
, 1 );
51 /** HTTP receive state */
64 /** Reference count */
66 /** Data transfer interface */
67 struct xfer_interface xfer
;
69 /** URI being fetched */
71 /** Transport layer interface */
72 struct xfer_interface socket
;
75 struct process process
;
77 /** HTTP response code */
78 unsigned int response
;
79 /** HTTP Content-Length */
80 size_t content_length
;
81 /** Received length */
84 enum http_rx_state rx_state
;
85 /** Line buffer for received header lines */
86 struct line_buffer linebuf
;
92 * @v refcnt Reference counter
94 static void http_free ( struct refcnt
*refcnt
) {
95 struct http_request
*http
=
96 container_of ( refcnt
, struct http_request
, refcnt
);
98 uri_put ( http
->uri
);
99 empty_line_buffer ( &http
->linebuf
);
104 * Mark HTTP request as complete
106 * @v http HTTP request
107 * @v rc Return status code
109 static void http_done ( struct http_request
*http
, int rc
) {
111 /* Prevent further processing of any current packet */
112 http
->rx_state
= HTTP_RX_DEAD
;
114 /* If we had a Content-Length, and the received content length
115 * isn't correct, flag an error
117 if ( http
->content_length
&&
118 ( http
->content_length
!= http
->rx_len
) ) {
119 DBGC ( http
, "HTTP %p incorrect length %zd, should be %zd\n",
120 http
, http
->rx_len
, http
->content_length
);
125 process_del ( &http
->process
);
127 /* Close all data transfer interfaces */
128 xfer_nullify ( &http
->socket
);
129 xfer_close ( &http
->socket
, rc
);
130 xfer_nullify ( &http
->xfer
);
131 xfer_close ( &http
->xfer
, rc
);
135 * Convert HTTP response code to return status code
137 * @v response HTTP response code
138 * @ret rc Return status code
140 static int http_response_to_rc ( unsigned int response
) {
141 switch ( response
) {
158 * Handle HTTP response
160 * @v http HTTP request
161 * @v response HTTP response
162 * @ret rc Return status code
164 static int http_rx_response ( struct http_request
*http
, char *response
) {
168 DBGC ( http
, "HTTP %p response \"%s\"\n", http
, response
);
170 /* Check response starts with "HTTP/" */
171 if ( strncmp ( response
, "HTTP/", 5 ) != 0 )
174 /* Locate and check response code */
175 spc
= strchr ( response
, ' ' );
178 http
->response
= strtoul ( spc
, NULL
, 10 );
179 if ( ( rc
= http_response_to_rc ( http
->response
) ) != 0 )
182 /* Move to received headers */
183 http
->rx_state
= HTTP_RX_HEADER
;
188 * Handle HTTP Location header
190 * @v http HTTP request
191 * @v value HTTP header value
192 * @ret rc Return status code
194 static int http_rx_location ( struct http_request
*http
, const char *value
) {
197 /* Redirect to new location */
198 DBGC ( http
, "HTTP %p redirecting to %s\n", http
, value
);
199 if ( ( rc
= xfer_redirect ( &http
->xfer
, LOCATION_URI_STRING
,
201 DBGC ( http
, "HTTP %p could not redirect: %s\n",
202 http
, strerror ( rc
) );
210 * Handle HTTP Content-Length header
212 * @v http HTTP request
213 * @v value HTTP header value
214 * @ret rc Return status code
216 static int http_rx_content_length ( struct http_request
*http
,
217 const char *value
) {
220 http
->content_length
= strtoul ( value
, &endp
, 10 );
221 if ( *endp
!= '\0' ) {
222 DBGC ( http
, "HTTP %p invalid Content-Length \"%s\"\n",
227 /* Use seek() to notify recipient of filesize */
228 xfer_seek ( &http
->xfer
, http
->content_length
, SEEK_SET
);
229 xfer_seek ( &http
->xfer
, 0, SEEK_SET
);
234 /** An HTTP header handler */
235 struct http_header_handler
{
236 /** Name (e.g. "Content-Length") */
238 /** Handle received header
240 * @v http HTTP request
241 * @v value HTTP header value
242 * @ret rc Return status code
244 * If an error is returned, the download will be aborted.
246 int ( * rx
) ( struct http_request
*http
, const char *value
);
249 /** List of HTTP header handlers */
250 static struct http_header_handler http_header_handlers
[] = {
252 .header
= "Location",
253 .rx
= http_rx_location
,
256 .header
= "Content-Length",
257 .rx
= http_rx_content_length
,
265 * @v http HTTP request
266 * @v header HTTP header
267 * @ret rc Return status code
269 static int http_rx_header ( struct http_request
*http
, char *header
) {
270 struct http_header_handler
*handler
;
275 /* An empty header line marks the transition to the data phase */
277 DBGC ( http
, "HTTP %p start of data\n", http
);
278 empty_line_buffer ( &http
->linebuf
);
279 http
->rx_state
= HTTP_RX_DATA
;
283 DBGC ( http
, "HTTP %p header \"%s\"\n", http
, header
);
285 /* Split header at the ": " */
286 separator
= strstr ( header
, ": " );
288 DBGC ( http
, "HTTP %p malformed header\n", http
);
292 value
= ( separator
+ 2 );
294 /* Hand off to header handler, if one exists */
295 for ( handler
= http_header_handlers
; handler
->header
; handler
++ ) {
296 if ( strcasecmp ( header
, handler
->header
) == 0 ) {
297 if ( ( rc
= handler
->rx ( http
, value
) ) != 0 )
305 /** An HTTP line-based data handler */
306 struct http_line_handler
{
309 * @v http HTTP request
310 * @v line Line to handle
311 * @ret rc Return status code
313 int ( * rx
) ( struct http_request
*http
, char *line
);
316 /** List of HTTP line-based data handlers */
317 static struct http_line_handler http_line_handlers
[] = {
318 [HTTP_RX_RESPONSE
] = { .rx
= http_rx_response
},
319 [HTTP_RX_HEADER
] = { .rx
= http_rx_header
},
323 * Handle new data arriving via HTTP connection in the data phase
325 * @v http HTTP request
326 * @v iobuf I/O buffer
327 * @ret rc Return status code
329 static int http_rx_data ( struct http_request
*http
,
330 struct io_buffer
*iobuf
) {
333 /* Update received length */
334 http
->rx_len
+= iob_len ( iobuf
);
336 /* Hand off data buffer */
337 if ( ( rc
= xfer_deliver_iob ( &http
->xfer
, iobuf
) ) != 0 )
340 /* If we have reached the content-length, stop now */
341 if ( http
->content_length
&&
342 ( http
->rx_len
>= http
->content_length
) ) {
343 http_done ( http
, 0 );
350 * Handle new data arriving via HTTP connection
352 * @v socket Transport layer interface
353 * @v iobuf I/O buffer
354 * @v meta Data transfer metadata
355 * @ret rc Return status code
357 static int http_socket_deliver_iob ( struct xfer_interface
*socket
,
358 struct io_buffer
*iobuf
,
359 struct xfer_metadata
*meta __unused
) {
360 struct http_request
*http
=
361 container_of ( socket
, struct http_request
, socket
);
362 struct http_line_handler
*lh
;
367 while ( iob_len ( iobuf
) ) {
368 switch ( http
->rx_state
) {
370 /* Do no further processing */
373 /* Once we're into the data phase, just fill
376 rc
= http_rx_data ( http
, iob_disown ( iobuf
) );
378 case HTTP_RX_RESPONSE
:
380 /* In the other phases, buffer and process a
383 len
= line_buffer ( &http
->linebuf
, iobuf
->data
,
387 DBGC ( http
, "HTTP %p could not buffer line: "
388 "%s\n", http
, strerror ( rc
) );
391 iob_pull ( iobuf
, len
);
392 line
= buffered_line ( &http
->linebuf
);
394 lh
= &http_line_handlers
[http
->rx_state
];
395 if ( ( rc
= lh
->rx ( http
, line
) ) != 0 )
407 http_done ( http
, rc
);
417 static void http_step ( struct process
*process
) {
418 struct http_request
*http
=
419 container_of ( process
, struct http_request
, process
);
420 const char *host
= http
->uri
->host
;
421 const char *user
= http
->uri
->user
;
422 const char *password
=
423 ( http
->uri
->password
? http
->uri
->password
: "" );
424 size_t user_pw_len
= ( user
? ( strlen ( user
) + 1 /* ":" */ +
425 strlen ( password
) ) : 0 );
426 size_t user_pw_base64_len
= base64_encoded_len ( user_pw_len
);
427 char user_pw
[ user_pw_len
+ 1 /* NUL */ ];
428 char user_pw_base64
[ user_pw_base64_len
+ 1 /* NUL */ ];
430 int request_len
= unparse_uri ( NULL
, 0, http
->uri
,
431 URI_PATH_BIT
| URI_QUERY_BIT
);
433 if ( xfer_window ( &http
->socket
) ) {
434 char request
[request_len
+ 1];
436 /* Construct path?query request */
437 unparse_uri ( request
, sizeof ( request
), http
->uri
,
438 URI_PATH_BIT
| URI_QUERY_BIT
);
440 /* We want to execute only once */
441 process_del ( &http
->process
);
443 /* Construct authorisation, if applicable */
445 /* Make "user:password" string from decoded fields */
446 snprintf ( user_pw
, sizeof ( user_pw
), "%s:%s",
449 /* Base64-encode the "user:password" string */
450 base64_encode ( user_pw
, user_pw_base64
);
453 /* Send GET request */
454 if ( ( rc
= xfer_printf ( &http
->socket
,
455 "GET %s%s HTTP/1.0\r\n"
456 "User-Agent: gPXE/" VERSION
"\r\n"
460 http
->uri
->path
? "" : "/",
463 "Authorization: Basic " : "" ),
464 ( user
? user_pw_base64
: "" ),
465 ( user
? "\r\n" : "" ),
467 http_done ( http
, rc
);
473 * HTTP connection closed by network stack
475 * @v socket Transport layer interface
476 * @v rc Reason for close
478 static void http_socket_close ( struct xfer_interface
*socket
, int rc
) {
479 struct http_request
*http
=
480 container_of ( socket
, struct http_request
, socket
);
482 DBGC ( http
, "HTTP %p socket closed: %s\n",
483 http
, strerror ( rc
) );
485 http_done ( http
, rc
);
488 /** HTTP socket operations */
489 static struct xfer_interface_operations http_socket_operations
= {
490 .close
= http_socket_close
,
491 .vredirect
= xfer_vreopen
,
492 .window
= unlimited_xfer_window
,
493 .alloc_iob
= default_xfer_alloc_iob
,
494 .deliver_iob
= http_socket_deliver_iob
,
495 .deliver_raw
= xfer_deliver_as_iob
,
499 * Close HTTP data transfer interface
501 * @v xfer Data transfer interface
502 * @v rc Reason for close
504 static void http_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
505 struct http_request
*http
=
506 container_of ( xfer
, struct http_request
, xfer
);
508 DBGC ( http
, "HTTP %p interface closed: %s\n",
509 http
, strerror ( rc
) );
511 http_done ( http
, rc
);
514 /** HTTP data transfer interface operations */
515 static struct xfer_interface_operations http_xfer_operations
= {
516 .close
= http_xfer_close
,
517 .vredirect
= ignore_xfer_vredirect
,
518 .window
= unlimited_xfer_window
,
519 .alloc_iob
= default_xfer_alloc_iob
,
520 .deliver_iob
= xfer_deliver_as_raw
,
521 .deliver_raw
= ignore_xfer_deliver_raw
,
525 * Initiate an HTTP connection, with optional filter
527 * @v xfer Data transfer interface
528 * @v uri Uniform Resource Identifier
529 * @v default_port Default port number
530 * @v filter Filter to apply to socket, or NULL
531 * @ret rc Return status code
533 int http_open_filter ( struct xfer_interface
*xfer
, struct uri
*uri
,
534 unsigned int default_port
,
535 int ( * filter
) ( struct xfer_interface
*xfer
,
536 struct xfer_interface
**next
) ) {
537 struct http_request
*http
;
538 struct sockaddr_tcpip server
;
539 struct xfer_interface
*socket
;
546 /* Allocate and populate HTTP structure */
547 http
= zalloc ( sizeof ( *http
) );
550 http
->refcnt
.free
= http_free
;
551 xfer_init ( &http
->xfer
, &http_xfer_operations
, &http
->refcnt
);
552 http
->uri
= uri_get ( uri
);
553 xfer_init ( &http
->socket
, &http_socket_operations
, &http
->refcnt
);
554 process_init ( &http
->process
, http_step
, &http
->refcnt
);
557 memset ( &server
, 0, sizeof ( server
) );
558 server
.st_port
= htons ( uri_port ( http
->uri
, default_port
) );
559 socket
= &http
->socket
;
561 if ( ( rc
= filter ( socket
, &socket
) ) != 0 )
564 if ( ( rc
= xfer_open_named_socket ( socket
, SOCK_STREAM
,
565 ( struct sockaddr
* ) &server
,
566 uri
->host
, NULL
) ) != 0 )
569 /* Attach to parent interface, mortalise self, and return */
570 xfer_plug_plug ( &http
->xfer
, xfer
);
571 ref_put ( &http
->refcnt
);
575 DBGC ( http
, "HTTP %p could not create request: %s\n",
576 http
, strerror ( rc
) );
577 http_done ( http
, rc
);
578 ref_put ( &http
->refcnt
);
583 * Initiate an HTTP connection
585 * @v xfer Data transfer interface
586 * @v uri Uniform Resource Identifier
587 * @ret rc Return status code
589 static int http_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
590 return http_open_filter ( xfer
, uri
, HTTP_PORT
, NULL
);
593 /** HTTP URI opener */
594 struct uri_opener http_uri_opener __uri_opener
= {