Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / src / net / tcp / http.c
blob4dc1ab73aacae62bdb3410f58b9120587e7e0fc2
1 /*
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 /**
20 * @file
22 * Hyper Text Transfer Protocol (HTTP)
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <strings.h>
31 #include <byteswap.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <gpxe/uri.h>
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 */
49 enum http_rx_state {
50 HTTP_RX_RESPONSE = 0,
51 HTTP_RX_HEADER,
52 HTTP_RX_DATA,
53 HTTP_RX_DEAD,
56 /**
57 * An HTTP request
60 struct http_request {
61 /** Reference count */
62 struct refcnt refcnt;
63 /** Data transfer interface */
64 struct xfer_interface xfer;
66 /** URI being fetched */
67 struct uri *uri;
68 /** Transport layer interface */
69 struct xfer_interface socket;
71 /** TX process */
72 struct process process;
74 /** HTTP response code */
75 unsigned int response;
76 /** HTTP Content-Length */
77 size_t content_length;
78 /** Received length */
79 size_t rx_len;
80 /** RX state */
81 enum http_rx_state rx_state;
82 /** Line buffer for received header lines */
83 struct line_buffer linebuf;
86 /**
87 * Free HTTP request
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 );
97 free ( http );
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 );
118 rc = -EIO;
121 /* Remove process */
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 ) {
139 case 200:
140 return 0;
141 case 404:
142 return -ENOENT;
143 case 403:
144 return -EPERM;
145 default:
146 return -EIO;
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 ) {
158 char *spc;
159 int rc;
161 DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
163 /* Check response starts with "HTTP/" */
164 if ( strncmp ( response, "HTTP/", 5 ) != 0 )
165 return -EIO;
167 /* Locate and check response code */
168 spc = strchr ( response, ' ' );
169 if ( ! spc )
170 return -EIO;
171 http->response = strtoul ( spc, NULL, 10 );
172 if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
173 return rc;
175 /* Move to received headers */
176 http->rx_state = HTTP_RX_HEADER;
177 return 0;
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 ) {
189 char *endp;
191 http->content_length = strtoul ( value, &endp, 10 );
192 if ( *endp != '\0' ) {
193 DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
194 http, value );
195 return -EIO;
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 );
202 return 0;
205 /** An HTTP header handler */
206 struct http_header_handler {
207 /** Name (e.g. "Content-Length") */
208 const char *header;
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,
226 { NULL, NULL }
230 * Handle HTTP header
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;
238 char *separator;
239 char *value;
240 int rc;
242 /* An empty header line marks the transition to the data phase */
243 if ( ! header[0] ) {
244 DBGC ( http, "HTTP %p start of data\n", http );
245 empty_line_buffer ( &http->linebuf );
246 http->rx_state = HTTP_RX_DATA;
247 return 0;
250 DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
252 /* Split header at the ": " */
253 separator = strstr ( header, ": " );
254 if ( ! separator ) {
255 DBGC ( http, "HTTP %p malformed header\n", http );
256 return -EIO;
258 *separator = '\0';
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 )
265 return rc;
266 break;
269 return 0;
272 /** An HTTP line-based data handler */
273 struct http_line_handler {
274 /** Handle line
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 ) {
298 int rc;
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 )
305 return rc;
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 );
313 return 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;
330 char *line;
331 ssize_t len;
332 int rc = 0;
334 while ( iob_len ( iobuf ) ) {
335 switch ( http->rx_state ) {
336 case HTTP_RX_DEAD:
337 /* Do no further processing */
338 goto done;
339 case HTTP_RX_DATA:
340 /* Once we're into the data phase, just fill
341 * the data buffer
343 rc = http_rx_data ( http, iobuf );
344 iobuf = NULL;
345 goto done;
346 case HTTP_RX_RESPONSE:
347 case HTTP_RX_HEADER:
348 /* In the other phases, buffer and process a
349 * line at a time
351 len = line_buffer ( &http->linebuf, iobuf->data,
352 iob_len ( iobuf ) );
353 if ( len < 0 ) {
354 rc = len;
355 DBGC ( http, "HTTP %p could not buffer line: "
356 "%s\n", http, strerror ( rc ) );
357 goto done;
359 iob_pull ( iobuf, len );
360 line = buffered_line ( &http->linebuf );
361 if ( line ) {
362 lh = &http_line_handlers[http->rx_state];
363 if ( ( rc = lh->rx ( http, line ) ) != 0 )
364 goto done;
366 break;
367 default:
368 assert ( 0 );
369 break;
373 done:
374 if ( rc )
375 http_done ( http, rc );
376 free_iob ( iobuf );
377 return rc;
381 * HTTP process
383 * @v process Process
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;
391 int rc;
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"
398 "Host: %s\r\n"
399 "\r\n",
400 ( path ? path : "/" ),
401 ( query ? "?" : "" ),
402 ( query ? query : "" ),
403 host ) ) != 0 ) {
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;
477 int rc;
479 /* Sanity checks */
480 if ( ! uri->host )
481 return -EINVAL;
483 /* Allocate and populate HTTP structure */
484 http = zalloc ( sizeof ( *http ) );
485 if ( ! http )
486 return -ENOMEM;
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 );
493 /* Open socket */
494 memset ( &server, 0, sizeof ( server ) );
495 server.st_port = htons ( uri_port ( http->uri, default_port ) );
496 socket = &http->socket;
497 if ( filter ) {
498 if ( ( rc = filter ( socket, &socket ) ) != 0 )
499 goto err;
501 if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
502 ( struct sockaddr * ) &server,
503 uri->host, NULL ) ) != 0 )
504 goto err;
506 /* Attach to parent interface, mortalise self, and return */
507 xfer_plug_plug ( &http->xfer, xfer );
508 ref_put ( &http->refcnt );
509 return 0;
511 err:
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 );
516 return rc;
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 = {
532 .scheme = "http",
533 .open = http_open,