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.
22 * Hyper Text Transfer Protocol (HTTP)
35 #include <gpxe/refcnt.h>
36 #include <gpxe/iobuf.h>
37 #include <gpxe/xfer.h>
38 #include <gpxe/open.h>
39 #include <gpxe/socket.h>
40 #include <gpxe/tcpip.h>
41 #include <gpxe/process.h>
42 #include <gpxe/linebuf.h>
43 #include <gpxe/features.h>
44 #include <gpxe/http.h>
46 FEATURE ( FEATURE_PROTOCOL
, "HTTP", DHCP_EB_FEATURE_HTTP
, 1 );
48 /** HTTP receive state */
61 /** Reference count */
63 /** Data transfer interface */
64 struct xfer_interface xfer
;
66 /** URI being fetched */
68 /** Transport layer interface */
69 struct xfer_interface socket
;
72 struct process process
;
74 /** HTTP response code */
75 unsigned int response
;
76 /** HTTP Content-Length */
77 size_t content_length
;
78 /** Received length */
81 enum http_rx_state rx_state
;
82 /** Line buffer for received header lines */
83 struct line_buffer linebuf
;
89 * @v refcnt Reference counter
91 static void http_free ( struct refcnt
*refcnt
) {
92 struct http_request
*http
=
93 container_of ( refcnt
, struct http_request
, refcnt
);
95 uri_put ( http
->uri
);
96 empty_line_buffer ( &http
->linebuf
);
101 * Mark HTTP request as complete
103 * @v http HTTP request
104 * @v rc Return status code
106 static void http_done ( struct http_request
*http
, int rc
) {
108 /* Prevent further processing of any current packet */
109 http
->rx_state
= HTTP_RX_DEAD
;
111 /* If we had a Content-Length, and the received content length
112 * isn't correct, flag an error
114 if ( http
->content_length
&&
115 ( http
->content_length
!= http
->rx_len
) ) {
116 DBGC ( http
, "HTTP %p incorrect length %zd, should be %zd\n",
117 http
, http
->rx_len
, http
->content_length
);
122 process_del ( &http
->process
);
124 /* Close all data transfer interfaces */
125 xfer_nullify ( &http
->socket
);
126 xfer_close ( &http
->socket
, rc
);
127 xfer_nullify ( &http
->xfer
);
128 xfer_close ( &http
->xfer
, rc
);
132 * Convert HTTP response code to return status code
134 * @v response HTTP response code
135 * @ret rc Return status code
137 static int http_response_to_rc ( unsigned int response
) {
138 switch ( response
) {
151 * Handle HTTP response
153 * @v http HTTP request
154 * @v response HTTP response
155 * @ret rc Return status code
157 static int http_rx_response ( struct http_request
*http
, char *response
) {
161 DBGC ( http
, "HTTP %p response \"%s\"\n", http
, response
);
163 /* Check response starts with "HTTP/" */
164 if ( strncmp ( response
, "HTTP/", 5 ) != 0 )
167 /* Locate and check response code */
168 spc
= strchr ( response
, ' ' );
171 http
->response
= strtoul ( spc
, NULL
, 10 );
172 if ( ( rc
= http_response_to_rc ( http
->response
) ) != 0 )
175 /* Move to received headers */
176 http
->rx_state
= HTTP_RX_HEADER
;
181 * Handle HTTP Content-Length header
183 * @v http HTTP request
184 * @v value HTTP header value
185 * @ret rc Return status code
187 static int http_rx_content_length ( struct http_request
*http
,
188 const char *value
) {
191 http
->content_length
= strtoul ( value
, &endp
, 10 );
192 if ( *endp
!= '\0' ) {
193 DBGC ( http
, "HTTP %p invalid Content-Length \"%s\"\n",
198 /* Use seek() to notify recipient of filesize */
199 xfer_seek ( &http
->xfer
, http
->content_length
, SEEK_SET
);
200 xfer_seek ( &http
->xfer
, 0, SEEK_SET
);
205 /** An HTTP header handler */
206 struct http_header_handler
{
207 /** Name (e.g. "Content-Length") */
209 /** Handle received header
211 * @v http HTTP request
212 * @v value HTTP header value
213 * @ret rc Return status code
215 * If an error is returned, the download will be aborted.
217 int ( * rx
) ( struct http_request
*http
, const char *value
);
220 /** List of HTTP header handlers */
221 static struct http_header_handler http_header_handlers
[] = {
223 .header
= "Content-Length",
224 .rx
= http_rx_content_length
,
232 * @v http HTTP request
233 * @v header HTTP header
234 * @ret rc Return status code
236 static int http_rx_header ( struct http_request
*http
, char *header
) {
237 struct http_header_handler
*handler
;
242 /* An empty header line marks the transition to the data phase */
244 DBGC ( http
, "HTTP %p start of data\n", http
);
245 empty_line_buffer ( &http
->linebuf
);
246 http
->rx_state
= HTTP_RX_DATA
;
250 DBGC ( http
, "HTTP %p header \"%s\"\n", http
, header
);
252 /* Split header at the ": " */
253 separator
= strstr ( header
, ": " );
255 DBGC ( http
, "HTTP %p malformed header\n", http
);
259 value
= ( separator
+ 2 );
261 /* Hand off to header handler, if one exists */
262 for ( handler
= http_header_handlers
; handler
->header
; handler
++ ) {
263 if ( strcasecmp ( header
, handler
->header
) == 0 ) {
264 if ( ( rc
= handler
->rx ( http
, value
) ) != 0 )
272 /** An HTTP line-based data handler */
273 struct http_line_handler
{
276 * @v http HTTP request
277 * @v line Line to handle
278 * @ret rc Return status code
280 int ( * rx
) ( struct http_request
*http
, char *line
);
283 /** List of HTTP line-based data handlers */
284 static struct http_line_handler http_line_handlers
[] = {
285 [HTTP_RX_RESPONSE
] = { .rx
= http_rx_response
},
286 [HTTP_RX_HEADER
] = { .rx
= http_rx_header
},
290 * Handle new data arriving via HTTP connection in the data phase
292 * @v http HTTP request
293 * @v iobuf I/O buffer
294 * @ret rc Return status code
296 static int http_rx_data ( struct http_request
*http
,
297 struct io_buffer
*iobuf
) {
300 /* Update received length */
301 http
->rx_len
+= iob_len ( iobuf
);
303 /* Hand off data buffer */
304 if ( ( rc
= xfer_deliver_iob ( &http
->xfer
, iobuf
) ) != 0 )
307 /* If we have reached the content-length, stop now */
308 if ( http
->content_length
&&
309 ( http
->rx_len
>= http
->content_length
) ) {
310 http_done ( http
, 0 );
317 * Handle new data arriving via HTTP connection
319 * @v socket Transport layer interface
320 * @v iobuf I/O buffer
321 * @v meta Data transfer metadata, or NULL
322 * @ret rc Return status code
324 static int http_socket_deliver_iob ( struct xfer_interface
*socket
,
325 struct io_buffer
*iobuf
,
326 struct xfer_metadata
*meta __unused
) {
327 struct http_request
*http
=
328 container_of ( socket
, struct http_request
, socket
);
329 struct http_line_handler
*lh
;
334 while ( iob_len ( iobuf
) ) {
335 switch ( http
->rx_state
) {
337 /* Do no further processing */
340 /* Once we're into the data phase, just fill
343 rc
= http_rx_data ( http
, iobuf
);
346 case HTTP_RX_RESPONSE
:
348 /* In the other phases, buffer and process a
351 len
= line_buffer ( &http
->linebuf
, iobuf
->data
,
355 DBGC ( http
, "HTTP %p could not buffer line: "
356 "%s\n", http
, strerror ( rc
) );
359 iob_pull ( iobuf
, len
);
360 line
= buffered_line ( &http
->linebuf
);
362 lh
= &http_line_handlers
[http
->rx_state
];
363 if ( ( rc
= lh
->rx ( http
, line
) ) != 0 )
375 http_done ( http
, rc
);
385 static void http_step ( struct process
*process
) {
386 struct http_request
*http
=
387 container_of ( process
, struct http_request
, process
);
388 const char *path
= http
->uri
->path
;
389 const char *host
= http
->uri
->host
;
390 const char *query
= http
->uri
->query
;
393 if ( xfer_window ( &http
->socket
) ) {
394 process_del ( &http
->process
);
395 if ( ( rc
= xfer_printf ( &http
->socket
,
396 "GET %s%s%s HTTP/1.0\r\n"
397 "User-Agent: gPXE/" VERSION
"\r\n"
400 ( path
? path
: "/" ),
401 ( query
? "?" : "" ),
402 ( query
? query
: "" ),
404 http_done ( http
, rc
);
410 * HTTP connection closed by network stack
412 * @v socket Transport layer interface
413 * @v rc Reason for close
415 static void http_socket_close ( struct xfer_interface
*socket
, int rc
) {
416 struct http_request
*http
=
417 container_of ( socket
, struct http_request
, socket
);
419 DBGC ( http
, "HTTP %p socket closed: %s\n",
420 http
, strerror ( rc
) );
422 http_done ( http
, rc
);
425 /** HTTP socket operations */
426 static struct xfer_interface_operations http_socket_operations
= {
427 .close
= http_socket_close
,
428 .vredirect
= xfer_vopen
,
429 .window
= unlimited_xfer_window
,
430 .alloc_iob
= default_xfer_alloc_iob
,
431 .deliver_iob
= http_socket_deliver_iob
,
432 .deliver_raw
= xfer_deliver_as_iob
,
436 * Close HTTP data transfer interface
438 * @v xfer Data transfer interface
439 * @v rc Reason for close
441 static void http_xfer_close ( struct xfer_interface
*xfer
, int rc
) {
442 struct http_request
*http
=
443 container_of ( xfer
, struct http_request
, xfer
);
445 DBGC ( http
, "HTTP %p interface closed: %s\n",
446 http
, strerror ( rc
) );
448 http_done ( http
, rc
);
451 /** HTTP data transfer interface operations */
452 static struct xfer_interface_operations http_xfer_operations
= {
453 .close
= http_xfer_close
,
454 .vredirect
= ignore_xfer_vredirect
,
455 .window
= unlimited_xfer_window
,
456 .alloc_iob
= default_xfer_alloc_iob
,
457 .deliver_iob
= xfer_deliver_as_raw
,
458 .deliver_raw
= ignore_xfer_deliver_raw
,
462 * Initiate an HTTP connection, with optional filter
464 * @v xfer Data transfer interface
465 * @v uri Uniform Resource Identifier
466 * @v default_port Default port number
467 * @v filter Filter to apply to socket, or NULL
468 * @ret rc Return status code
470 int http_open_filter ( struct xfer_interface
*xfer
, struct uri
*uri
,
471 unsigned int default_port
,
472 int ( * filter
) ( struct xfer_interface
*xfer
,
473 struct xfer_interface
**next
) ) {
474 struct http_request
*http
;
475 struct sockaddr_tcpip server
;
476 struct xfer_interface
*socket
;
483 /* Allocate and populate HTTP structure */
484 http
= zalloc ( sizeof ( *http
) );
487 http
->refcnt
.free
= http_free
;
488 xfer_init ( &http
->xfer
, &http_xfer_operations
, &http
->refcnt
);
489 http
->uri
= uri_get ( uri
);
490 xfer_init ( &http
->socket
, &http_socket_operations
, &http
->refcnt
);
491 process_init ( &http
->process
, http_step
, &http
->refcnt
);
494 memset ( &server
, 0, sizeof ( server
) );
495 server
.st_port
= htons ( uri_port ( http
->uri
, default_port
) );
496 socket
= &http
->socket
;
498 if ( ( rc
= filter ( socket
, &socket
) ) != 0 )
501 if ( ( rc
= xfer_open_named_socket ( socket
, SOCK_STREAM
,
502 ( struct sockaddr
* ) &server
,
503 uri
->host
, NULL
) ) != 0 )
506 /* Attach to parent interface, mortalise self, and return */
507 xfer_plug_plug ( &http
->xfer
, xfer
);
508 ref_put ( &http
->refcnt
);
512 DBGC ( http
, "HTTP %p could not create request: %s\n",
513 http
, strerror ( rc
) );
514 http_done ( http
, rc
);
515 ref_put ( &http
->refcnt
);
520 * Initiate an HTTP connection
522 * @v xfer Data transfer interface
523 * @v uri Uniform Resource Identifier
524 * @ret rc Return status code
526 static int http_open ( struct xfer_interface
*xfer
, struct uri
*uri
) {
527 return http_open_filter ( xfer
, uri
, HTTP_PORT
, NULL
);
530 /** HTTP URI opener */
531 struct uri_opener http_uri_opener __uri_opener
= {