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 *path
= http
->uri
->path
;
421 const char *host
= http
->uri
->host
;
422 const char *query
= http
->uri
->query
;
423 const char *user
= http
->uri
->user
;
424 const char *password
=
425 ( http
->uri
->password
? http
->uri
->password
: "" );
426 size_t user_pw_len
= ( user
? ( strlen ( user
) + 1 /* ":" */ +
427 strlen ( password
) ) : 0 );
428 size_t user_pw_base64_len
= base64_encoded_len ( user_pw_len
);
429 char user_pw
[ user_pw_len
+ 1 /* NUL */ ];
430 char user_pw_base64
[ user_pw_base64_len
+ 1 /* NUL */ ];
433 if ( xfer_window ( &http
->socket
) ) {
435 /* We want to execute only once */
436 process_del ( &http
->process
);
438 /* Construct authorisation, if applicable */
441 ssize_t remaining
= sizeof ( user_pw
);
444 /* URI-decode the username and password */
445 len
= uri_decode ( user
, buf
, remaining
);
448 *(remaining
--, buf
++) = ':';
449 len
= uri_decode ( password
, buf
, remaining
);
452 assert ( remaining
>= 0 );
454 /* Base64-encode the "user:password" string */
455 base64_encode ( user_pw
, user_pw_base64
);
458 /* Send GET request */
459 if ( ( rc
= xfer_printf ( &http
->socket
,
460 "GET %s%s%s HTTP/1.0\r\n"
461 "User-Agent: gPXE/" VERSION
"\r\n"
465 ( path
? path
: "/" ),
466 ( query
? "?" : "" ),
467 ( query
? query
: "" ),
469 "Authorization: Basic " : "" ),
470 ( user
? user_pw_base64
: "" ),
471 ( user
? "\r\n" : "" ),
473 http_done ( http
, rc
);
479 * HTTP connection closed by network stack
481 * @v socket Transport layer interface
482 * @v rc Reason for close
484 static void http_socket_close ( struct xfer_interface
*socket
, int rc
) {
485 struct http_request
*http
=
486 container_of ( socket
, struct http_request
, socket
);
488 DBGC ( http
, "HTTP %p socket closed: %s\n",
489 http
, strerror ( rc
) );
491 http_done ( http
, rc
);
494 /** HTTP socket operations */
495 static struct xfer_interface_operations http_socket_operations
= {
496 .close
= http_socket_close
,
497 .vredirect
= xfer_vreopen
,
498 .window
= unlimited_xfer_window
,
499 .alloc_iob
= default_xfer_alloc_iob
,
500 .deliver_iob
= http_socket_deliver_iob
,
501 .deliver_raw
= xfer_deliver_as_iob
,
505 * Close HTTP data transfer interface
507 * @v xfer Data transfer interface
508 * @v rc Reason for close
510 static void http_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
511 struct http_request
*http
=
512 container_of ( xfer
, struct http_request
, xfer
);
514 DBGC ( http
, "HTTP %p interface closed: %s\n",
515 http
, strerror ( rc
) );
517 http_done ( http
, rc
);
520 /** HTTP data transfer interface operations */
521 static struct xfer_interface_operations http_xfer_operations
= {
522 .close
= http_xfer_close
,
523 .vredirect
= ignore_xfer_vredirect
,
524 .window
= unlimited_xfer_window
,
525 .alloc_iob
= default_xfer_alloc_iob
,
526 .deliver_iob
= xfer_deliver_as_raw
,
527 .deliver_raw
= ignore_xfer_deliver_raw
,
531 * Initiate an HTTP connection, with optional filter
533 * @v xfer Data transfer interface
534 * @v uri Uniform Resource Identifier
535 * @v default_port Default port number
536 * @v filter Filter to apply to socket, or NULL
537 * @ret rc Return status code
539 int http_open_filter ( struct xfer_interface
*xfer
, struct uri
*uri
,
540 unsigned int default_port
,
541 int ( * filter
) ( struct xfer_interface
*xfer
,
542 struct xfer_interface
**next
) ) {
543 struct http_request
*http
;
544 struct sockaddr_tcpip server
;
545 struct xfer_interface
*socket
;
552 /* Allocate and populate HTTP structure */
553 http
= zalloc ( sizeof ( *http
) );
556 http
->refcnt
.free
= http_free
;
557 xfer_init ( &http
->xfer
, &http_xfer_operations
, &http
->refcnt
);
558 http
->uri
= uri_get ( uri
);
559 xfer_init ( &http
->socket
, &http_socket_operations
, &http
->refcnt
);
560 process_init ( &http
->process
, http_step
, &http
->refcnt
);
563 memset ( &server
, 0, sizeof ( server
) );
564 server
.st_port
= htons ( uri_port ( http
->uri
, default_port
) );
565 socket
= &http
->socket
;
567 if ( ( rc
= filter ( socket
, &socket
) ) != 0 )
570 if ( ( rc
= xfer_open_named_socket ( socket
, SOCK_STREAM
,
571 ( struct sockaddr
* ) &server
,
572 uri
->host
, NULL
) ) != 0 )
575 /* Attach to parent interface, mortalise self, and return */
576 xfer_plug_plug ( &http
->xfer
, xfer
);
577 ref_put ( &http
->refcnt
);
581 DBGC ( http
, "HTTP %p could not create request: %s\n",
582 http
, strerror ( rc
) );
583 http_done ( http
, rc
);
584 ref_put ( &http
->refcnt
);
589 * Initiate an HTTP connection
591 * @v xfer Data transfer interface
592 * @v uri Uniform Resource Identifier
593 * @ret rc Return status code
595 static int http_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
596 return http_open_filter ( xfer
, uri
, HTTP_PORT
, NULL
);
599 /** HTTP URI opener */
600 struct uri_opener http_uri_opener __uri_opener
= {