1 /*
2  * Copyright (C) 2015 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  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 /**
27  * @file
28  *
29  * Hyper Text Transfer Protocol (HTTP) core functionality
30  *
31  */
32 
33 #include <stdint.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <strings.h>
38 #include <byteswap.h>
39 #include <errno.h>
40 #include <ctype.h>
41 #include <assert.h>
42 #include <ipxe/uri.h>
43 #include <ipxe/refcnt.h>
44 #include <ipxe/iobuf.h>
45 #include <ipxe/xfer.h>
46 #include <ipxe/open.h>
47 #include <ipxe/process.h>
48 #include <ipxe/retry.h>
49 #include <ipxe/timer.h>
50 #include <ipxe/linebuf.h>
51 #include <ipxe/xferbuf.h>
52 #include <ipxe/blockdev.h>
53 #include <ipxe/acpi.h>
54 #include <ipxe/version.h>
55 #include <ipxe/params.h>
56 #include <ipxe/profile.h>
57 #include <ipxe/vsprintf.h>
58 #include <ipxe/http.h>
59 
60 /* Disambiguate the various error causes */
61 #define EACCES_401 __einfo_error ( EINFO_EACCES_401 )
62 #define EINFO_EACCES_401 \
63 	__einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )
64 #define EINVAL_STATUS __einfo_error ( EINFO_EINVAL_STATUS )
65 #define EINFO_EINVAL_STATUS \
66 	__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid status line" )
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 EIO_OTHER __einfo_error ( EINFO_EIO_OTHER )
77 #define EINFO_EIO_OTHER \
78 	__einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )
79 #define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
80 #define EINFO_EIO_CONTENT_LENGTH \
81 	__einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
82 #define EIO_4XX __einfo_error ( EINFO_EIO_4XX )
83 #define EINFO_EIO_4XX \
84 	__einfo_uniqify ( EINFO_EIO, 0x04, "HTTP 4xx Client Error" )
85 #define EIO_5XX __einfo_error ( EINFO_EIO_5XX )
86 #define EINFO_EIO_5XX \
87 	__einfo_uniqify ( EINFO_EIO, 0x05, "HTTP 5xx Server Error" )
88 #define ENOENT_404 __einfo_error ( EINFO_ENOENT_404 )
89 #define EINFO_ENOENT_404 \
90 	__einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )
91 #define ENOTSUP_CONNECTION __einfo_error ( EINFO_ENOTSUP_CONNECTION )
92 #define EINFO_ENOTSUP_CONNECTION \
93 	__einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported connection header" )
94 #define ENOTSUP_TRANSFER __einfo_error ( EINFO_ENOTSUP_TRANSFER )
95 #define EINFO_ENOTSUP_TRANSFER \
96 	__einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported transfer encoding" )
97 #define EPERM_403 __einfo_error ( EINFO_EPERM_403 )
98 #define EINFO_EPERM_403 \
99 	__einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )
100 #define EPROTO_UNSOLICITED __einfo_error ( EINFO_EPROTO_UNSOLICITED )
101 #define EINFO_EPROTO_UNSOLICITED \
102 	__einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )
103 
104 /** Retry delay used when we cannot understand the Retry-After header */
105 #define HTTP_RETRY_SECONDS 5
106 
107 /** Receive profiler */
108 static struct profiler http_rx_profiler __profiler = { .name = "http.rx" };
109 
110 /** Data transfer profiler */
111 static struct profiler http_xfer_profiler __profiler = { .name = "http.xfer" };
112 
113 static struct http_state http_request;
114 static struct http_state http_headers;
115 static struct http_state http_trailers;
116 static struct http_transfer_encoding http_transfer_identity;
117 
118 /******************************************************************************
119  *
120  * Methods
121  *
122  ******************************************************************************
123  */
124 
125 /** HTTP HEAD method */
126 struct http_method http_head = {
127 	.name = "HEAD",
128 };
129 
130 /** HTTP GET method */
131 struct http_method http_get = {
132 	.name = "GET",
133 };
134 
135 /** HTTP POST method */
136 struct http_method http_post = {
137 	.name = "POST",
138 };
139 
140 /******************************************************************************
141  *
142  * Utility functions
143  *
144  ******************************************************************************
145  */
146 
147 /**
148  * Handle received HTTP line-buffered data
149  *
150  * @v http		HTTP transaction
151  * @v iobuf		I/O buffer
152  * @v linebuf		Line buffer
153  * @ret rc		Return status code
154  */
http_rx_linebuf(struct http_transaction * http,struct io_buffer * iobuf,struct line_buffer * linebuf)155 static int http_rx_linebuf ( struct http_transaction *http,
156 			     struct io_buffer *iobuf,
157 			     struct line_buffer *linebuf ) {
158 	int consumed;
159 	int rc;
160 
161 	/* Buffer received line */
162 	consumed = line_buffer ( linebuf, iobuf->data, iob_len ( iobuf ) );
163 	if ( consumed < 0 ) {
164 		rc = consumed;
165 		DBGC ( http, "HTTP %p could not buffer line: %s\n",
166 		       http, strerror ( rc ) );
167 		return rc;
168 	}
169 
170 	/* Consume line */
171 	iob_pull ( iobuf, consumed );
172 
173 	return 0;
174 }
175 
176 /**
177  * Get HTTP response token
178  *
179  * @v line		Line position
180  * @v value		Token value to fill in (if any)
181  * @ret token		Token, or NULL
182  */
http_token(char ** line,char ** value)183 char * http_token ( char **line, char **value ) {
184 	char *token;
185 	char quote = '\0';
186 	char c;
187 
188 	/* Avoid returning uninitialised data */
189 	if ( value )
190 		*value = NULL;
191 
192 	/* Skip any initial whitespace or commas */
193 	while ( ( isspace ( **line ) ) || ( **line == ',' ) )
194 		(*line)++;
195 
196 	/* Check for end of line and record token position */
197 	if ( ! **line )
198 		return NULL;
199 	token = *line;
200 
201 	/* Scan for end of token */
202 	while ( ( c = **line ) ) {
203 
204 		/* Terminate if we hit an unquoted whitespace or comma */
205 		if ( ( isspace ( c ) || ( c == ',' ) ) && ! quote )
206 			break;
207 
208 		/* Terminate if we hit a closing quote */
209 		if ( c == quote )
210 			break;
211 
212 		/* Check for value separator */
213 		if ( value && ( ! *value ) && ( c == '=' ) ) {
214 
215 			/* Terminate key portion of token */
216 			*((*line)++) = '\0';
217 
218 			/* Check for quote character */
219 			c = **line;
220 			if ( ( c == '"' ) || ( c == '\'' ) ) {
221 				quote = c;
222 				(*line)++;
223 			}
224 
225 			/* Record value portion of token */
226 			*value = *line;
227 
228 		} else {
229 
230 			/* Move to next character */
231 			(*line)++;
232 		}
233 	}
234 
235 	/* Terminate token, if applicable */
236 	if ( c )
237 		*((*line)++) = '\0';
238 
239 	return token;
240 }
241 
242 /******************************************************************************
243  *
244  * Transactions
245  *
246  ******************************************************************************
247  */
248 
249 /**
250  * Free HTTP transaction
251  *
252  * @v refcnt		Reference count
253  */
http_free(struct refcnt * refcnt)254 static void http_free ( struct refcnt *refcnt ) {
255 	struct http_transaction *http =
256 		container_of ( refcnt, struct http_transaction, refcnt );
257 
258 	empty_line_buffer ( &http->response.headers );
259 	empty_line_buffer ( &http->linebuf );
260 	uri_put ( http->uri );
261 	free ( http );
262 }
263 
264 /**
265  * Close HTTP transaction
266  *
267  * @v http		HTTP transaction
268  * @v rc		Reason for close
269  */
http_close(struct http_transaction * http,int rc)270 static void http_close ( struct http_transaction *http, int rc ) {
271 
272 	/* Stop process */
273 	process_del ( &http->process );
274 
275 	/* Stop timer */
276 	stop_timer ( &http->timer );
277 
278 	/* Close all interfaces */
279 	intfs_shutdown ( rc, &http->conn, &http->transfer, &http->content,
280 			 &http->xfer, NULL );
281 }
282 
283 /**
284  * Close HTTP transaction with error (even if none specified)
285  *
286  * @v http		HTTP transaction
287  * @v rc		Reason for close
288  */
http_close_error(struct http_transaction * http,int rc)289 static void http_close_error ( struct http_transaction *http, int rc ) {
290 
291 	/* Treat any close as an error */
292 	http_close ( http, ( rc ? rc : -EPIPE ) );
293 }
294 
295 /**
296  * Reopen stale HTTP connection
297  *
298  * @v http		HTTP transaction
299  */
http_reopen(struct http_transaction * http)300 static void http_reopen ( struct http_transaction *http ) {
301 	int rc;
302 
303 	/* Close existing connection */
304 	intf_restart ( &http->conn, -ECANCELED );
305 
306 	/* Reopen connection */
307 	if ( ( rc = http_connect ( &http->conn, http->uri ) ) != 0 ) {
308 		DBGC ( http, "HTTP %p could not reconnect: %s\n",
309 		       http, strerror ( rc ) );
310 		goto err_connect;
311 	}
312 
313 	/* Reset state */
314 	http->state = &http_request;
315 
316 	/* Reschedule transmission process */
317 	process_add ( &http->process );
318 
319 	return;
320 
321  err_connect:
322 	http_close ( http, rc );
323 }
324 
325 /**
326  * Handle retry timer expiry
327  *
328  * @v timer		Retry timer
329  * @v over		Failure indicator
330  */
http_expired(struct retry_timer * timer,int over __unused)331 static void http_expired ( struct retry_timer *timer, int over __unused ) {
332 	struct http_transaction *http =
333 		container_of ( timer, struct http_transaction, timer );
334 
335 	/* Reopen connection */
336 	http_reopen ( http );
337 }
338 
339 /**
340  * HTTP transmit process
341  *
342  * @v http		HTTP transaction
343  */
http_step(struct http_transaction * http)344 static void http_step ( struct http_transaction *http ) {
345 	int rc;
346 
347 	/* Do nothing if we have nothing to transmit */
348 	if ( ! http->state->tx )
349 		return;
350 
351 	/* Do nothing until connection is ready */
352 	if ( ! xfer_window ( &http->conn ) )
353 		return;
354 
355 	/* Notify data transfer interface that window may have changed */
356 	xfer_window_changed ( &http->xfer );
357 
358 	/* Do nothing until data transfer interface is ready */
359 	if ( ! xfer_window ( &http->xfer ) )
360 		return;
361 
362 	/* Transmit data */
363 	if ( ( rc = http->state->tx ( http ) ) != 0 )
364 		goto err;
365 
366 	return;
367 
368  err:
369 	http_close ( http, rc );
370 }
371 
372 /**
373  * Handle received HTTP data
374  *
375  * @v http		HTTP transaction
376  * @v iobuf		I/O buffer
377  * @v meta		Transfer metadata
378  * @ret rc		Return status code
379  *
380  * This function takes ownership of the I/O buffer.
381  */
http_conn_deliver(struct http_transaction * http,struct io_buffer * iobuf,struct xfer_metadata * meta __unused)382 static int http_conn_deliver ( struct http_transaction *http,
383 			       struct io_buffer *iobuf,
384 			       struct xfer_metadata *meta __unused ) {
385 	int rc;
386 
387 	/* Handle received data */
388 	profile_start ( &http_rx_profiler );
389 	while ( iobuf && iob_len ( iobuf ) ) {
390 
391 		/* Sanity check */
392 		if ( ( ! http->state ) || ( ! http->state->rx ) ) {
393 			DBGC ( http, "HTTP %p unexpected data\n", http );
394 			rc = -EPROTO_UNSOLICITED;
395 			goto err;
396 		}
397 
398 		/* Receive (some) data */
399 		if ( ( rc = http->state->rx ( http, &iobuf ) ) != 0 )
400 			goto err;
401 	}
402 
403 	/* Free I/O buffer, if applicable */
404 	free_iob ( iobuf );
405 
406 	profile_stop ( &http_rx_profiler );
407 	return 0;
408 
409  err:
410 	free_iob ( iobuf );
411 	http_close ( http, rc );
412 	return rc;
413 }
414 
415 /**
416  * Handle server connection close
417  *
418  * @v http		HTTP transaction
419  * @v rc		Reason for close
420  */
http_conn_close(struct http_transaction * http,int rc)421 static void http_conn_close ( struct http_transaction *http, int rc ) {
422 
423 	/* Sanity checks */
424 	assert ( http->state != NULL );
425 	assert ( http->state->close != NULL );
426 
427 	/* Restart server connection interface */
428 	intf_restart ( &http->conn, rc );
429 
430 	/* Hand off to state-specific method */
431 	http->state->close ( http, rc );
432 }
433 
434 /**
435  * Handle received content-decoded data
436  *
437  * @v http		HTTP transaction
438  * @v iobuf		I/O buffer
439  * @v meta		Data transfer metadata
440  */
http_content_deliver(struct http_transaction * http,struct io_buffer * iobuf,struct xfer_metadata * meta)441 static int http_content_deliver ( struct http_transaction *http,
442 				  struct io_buffer *iobuf,
443 				  struct xfer_metadata *meta ) {
444 	int rc;
445 
446 	/* Ignore content if this is anything other than a successful
447 	 * transfer.
448 	 */
449 	if ( http->response.rc != 0 ) {
450 		free_iob ( iobuf );
451 		return 0;
452 	}
453 
454 	/* Deliver to data transfer interface */
455 	profile_start ( &http_xfer_profiler );
456 	if ( ( rc = xfer_deliver ( &http->xfer, iob_disown ( iobuf ),
457 				   meta ) ) != 0 )
458 		return rc;
459 	profile_stop ( &http_xfer_profiler );
460 
461 	return 0;
462 }
463 
464 /**
465  * Get underlying data transfer buffer
466  *
467  * @v http		HTTP transaction
468  * @ret xferbuf		Data transfer buffer, or NULL on error
469  */
470 static struct xfer_buffer *
http_content_buffer(struct http_transaction * http)471 http_content_buffer ( struct http_transaction *http ) {
472 
473 	/* Deny access to the data transfer buffer if this is anything
474 	 * other than a successful transfer.
475 	 */
476 	if ( http->response.rc != 0 )
477 		return NULL;
478 
479 	/* Hand off to data transfer interface */
480 	return xfer_buffer ( &http->xfer );
481 }
482 
483 /**
484  * Read from block device (when HTTP block device support is not present)
485  *
486  * @v http		HTTP transaction
487  * @v data		Data interface
488  * @v lba		Starting logical block address
489  * @v count		Number of logical blocks
490  * @v buffer		Data buffer
491  * @v len		Length of data buffer
492  * @ret rc		Return status code
493  */
http_block_read(struct http_transaction * http __unused,struct interface * data __unused,uint64_t lba __unused,unsigned int count __unused,userptr_t buffer __unused,size_t len __unused)494 __weak int http_block_read ( struct http_transaction *http __unused,
495 			     struct interface *data __unused,
496 			     uint64_t lba __unused, unsigned int count __unused,
497 			     userptr_t buffer __unused, size_t len __unused ) {
498 
499 	return -ENOTSUP;
500 }
501 
502 /**
503  * Read block device capacity (when HTTP block device support is not present)
504  *
505  * @v control		Control interface
506  * @v data		Data interface
507  * @ret rc		Return status code
508  */
http_block_read_capacity(struct http_transaction * http __unused,struct interface * data __unused)509 __weak int http_block_read_capacity ( struct http_transaction *http __unused,
510 				      struct interface *data __unused ) {
511 
512 	return -ENOTSUP;
513 }
514 
515 /** HTTP data transfer interface operations */
516 static struct interface_operation http_xfer_operations[] = {
517 	INTF_OP ( block_read, struct http_transaction *, http_block_read ),
518 	INTF_OP ( block_read_capacity, struct http_transaction *,
519 		  http_block_read_capacity ),
520 	INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
521 	INTF_OP ( intf_close, struct http_transaction *, http_close ),
522 };
523 
524 /** HTTP data transfer interface descriptor */
525 static struct interface_descriptor http_xfer_desc =
526 	INTF_DESC_PASSTHRU ( struct http_transaction, xfer,
527 			     http_xfer_operations, content );
528 
529 /** HTTP content-decoded interface operations */
530 static struct interface_operation http_content_operations[] = {
531 	INTF_OP ( xfer_deliver, struct http_transaction *,
532 		  http_content_deliver ),
533 	INTF_OP ( xfer_buffer, struct http_transaction *, http_content_buffer ),
534 	INTF_OP ( intf_close, struct http_transaction *, http_close ),
535 };
536 
537 /** HTTP content-decoded interface descriptor */
538 static struct interface_descriptor http_content_desc =
539 	INTF_DESC_PASSTHRU ( struct http_transaction, content,
540 			     http_content_operations, xfer );
541 
542 /** HTTP transfer-decoded interface operations */
543 static struct interface_operation http_transfer_operations[] = {
544 	INTF_OP ( intf_close, struct http_transaction *, http_close ),
545 };
546 
547 /** HTTP transfer-decoded interface descriptor */
548 static struct interface_descriptor http_transfer_desc =
549 	INTF_DESC_PASSTHRU ( struct http_transaction, transfer,
550 			     http_transfer_operations, conn );
551 
552 /** HTTP server connection interface operations */
553 static struct interface_operation http_conn_operations[] = {
554 	INTF_OP ( xfer_deliver, struct http_transaction *, http_conn_deliver ),
555 	INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
556 	INTF_OP ( pool_reopen, struct http_transaction *, http_reopen ),
557 	INTF_OP ( intf_close, struct http_transaction *, http_conn_close ),
558 };
559 
560 /** HTTP server connection interface descriptor */
561 static struct interface_descriptor http_conn_desc =
562 	INTF_DESC_PASSTHRU ( struct http_transaction, conn,
563 			     http_conn_operations, transfer );
564 
565 /** HTTP process descriptor */
566 static struct process_descriptor http_process_desc =
567 	PROC_DESC_ONCE ( struct http_transaction, process, http_step );
568 
569 /**
570  * Open HTTP transaction
571  *
572  * @v xfer		Data transfer interface
573  * @v method		Request method
574  * @v uri		Request URI
575  * @v range		Content range (if any)
576  * @v content		Request content (if any)
577  * @ret rc		Return status code
578  */
http_open(struct interface * xfer,struct http_method * method,struct uri * uri,struct http_request_range * range,struct http_request_content * content)579 int http_open ( struct interface *xfer, struct http_method *method,
580 		struct uri *uri, struct http_request_range *range,
581 		struct http_request_content *content ) {
582 	struct http_transaction *http;
583 	struct uri request_uri;
584 	struct uri request_host;
585 	size_t request_uri_len;
586 	size_t request_host_len;
587 	size_t content_len;
588 	char *request_uri_string;
589 	char *request_host_string;
590 	void *content_data;
591 	int rc;
592 
593 	/* Calculate request URI length */
594 	memset ( &request_uri, 0, sizeof ( request_uri ) );
595 	request_uri.path = ( uri->path ? uri->path : "/" );
596 	request_uri.query = uri->query;
597 	request_uri_len =
598 		( format_uri ( &request_uri, NULL, 0 ) + 1 /* NUL */);
599 
600 	/* Calculate host name length */
601 	memset ( &request_host, 0, sizeof ( request_host ) );
602 	request_host.host = uri->host;
603 	request_host.port = uri->port;
604 	request_host_len =
605 		( format_uri ( &request_host, NULL, 0 ) + 1 /* NUL */ );
606 
607 	/* Calculate request content length */
608 	content_len = ( content ? content->len : 0 );
609 
610 	/* Allocate and initialise structure */
611 	http = zalloc ( sizeof ( *http ) + request_uri_len + request_host_len +
612 			content_len );
613 	if ( ! http ) {
614 		rc = -ENOMEM;
615 		goto err_alloc;
616 	}
617 	request_uri_string = ( ( ( void * ) http ) + sizeof ( *http ) );
618 	request_host_string = ( request_uri_string + request_uri_len );
619 	content_data = ( request_host_string + request_host_len );
620 	format_uri ( &request_uri, request_uri_string, request_uri_len );
621 	format_uri ( &request_host, request_host_string, request_host_len );
622 	ref_init ( &http->refcnt, http_free );
623 	intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
624 	intf_init ( &http->content, &http_content_desc, &http->refcnt );
625 	intf_init ( &http->transfer, &http_transfer_desc, &http->refcnt );
626 	intf_init ( &http->conn, &http_conn_desc, &http->refcnt );
627 	intf_plug_plug ( &http->transfer, &http->content );
628 	process_init ( &http->process, &http_process_desc, &http->refcnt );
629 	timer_init ( &http->timer, http_expired, &http->refcnt );
630 	http->uri = uri_get ( uri );
631 	http->request.method = method;
632 	http->request.uri = request_uri_string;
633 	http->request.host = request_host_string;
634 	if ( range ) {
635 		memcpy ( &http->request.range, range,
636 			 sizeof ( http->request.range ) );
637 	}
638 	if ( content ) {
639 		http->request.content.type = content->type;
640 		http->request.content.data = content_data;
641 		http->request.content.len = content_len;
642 		memcpy ( content_data, content->data, content_len );
643 	}
644 	http->state = &http_request;
645 	DBGC2 ( http, "HTTP %p %s://%s%s\n", http, http->uri->scheme,
646 		http->request.host, http->request.uri );
647 
648 	/* Open connection */
649 	if ( ( rc = http_connect ( &http->conn, uri ) ) != 0 ) {
650 		DBGC ( http, "HTTP %p could not connect: %s\n",
651 		       http, strerror ( rc ) );
652 		goto err_connect;
653 	}
654 
655 	/* Attach to parent interface, mortalise self, and return */
656 	intf_plug_plug ( &http->xfer, xfer );
657 	ref_put ( &http->refcnt );
658 	return 0;
659 
660  err_connect:
661 	http_close ( http, rc );
662 	ref_put ( &http->refcnt );
663  err_alloc:
664 	return rc;
665 }
666 
667 /**
668  * Redirect HTTP transaction
669  *
670  * @v http		HTTP transaction
671  * @v location		New location
672  * @ret rc		Return status code
673  */
http_redirect(struct http_transaction * http,const char * location)674 static int http_redirect ( struct http_transaction *http,
675 			   const char *location ) {
676 	struct uri *location_uri;
677 	struct uri *resolved_uri;
678 	int rc;
679 
680 	DBGC2 ( http, "HTTP %p redirecting to \"%s\"\n", http, location );
681 
682 	/* Parse location URI */
683 	location_uri = parse_uri ( location );
684 	if ( ! location_uri ) {
685 		rc = -ENOMEM;
686 		goto err_parse_uri;
687 	}
688 
689 	/* Resolve as relative to original URI */
690 	resolved_uri = resolve_uri ( http->uri, location_uri );
691 	if ( ! resolved_uri ) {
692 		rc = -ENOMEM;
693 		goto err_resolve_uri;
694 	}
695 
696 	/* Redirect to new URI */
697 	if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI,
698 				    resolved_uri ) ) != 0 ) {
699 		DBGC ( http, "HTTP %p could not redirect: %s\n",
700 		       http, strerror ( rc ) );
701 		goto err_redirect;
702 	}
703 
704  err_redirect:
705 	uri_put ( resolved_uri );
706  err_resolve_uri:
707 	uri_put ( location_uri );
708  err_parse_uri:
709 	return rc;
710 }
711 
712 /**
713  * Handle successful transfer completion
714  *
715  * @v http		HTTP transaction
716  * @ret rc		Return status code
717  */
http_transfer_complete(struct http_transaction * http)718 static int http_transfer_complete ( struct http_transaction *http ) {
719 	struct http_authentication *auth;
720 	const char *location;
721 	int rc;
722 
723 	/* Keep connection alive if applicable */
724 	if ( http->response.flags & HTTP_RESPONSE_KEEPALIVE )
725 		pool_recycle ( &http->conn );
726 
727 	/* Restart server connection interface */
728 	intf_restart ( &http->conn, 0 );
729 
730 	/* No more data is expected */
731 	http->state = NULL;
732 
733 	/* If transaction is successful, then close the
734 	 * transfer-decoded interface.  The content encoding may
735 	 * choose whether or not to immediately terminate the
736 	 * transaction.
737 	 */
738 	if ( http->response.rc == 0 ) {
739 		intf_shutdown ( &http->transfer, 0 );
740 		return 0;
741 	}
742 
743 	/* Perform redirection, if applicable */
744 	if ( ( location = http->response.location ) ) {
745 		if ( ( rc = http_redirect ( http, location ) ) != 0 )
746 			return rc;
747 		http_close ( http, 0 );
748 		return 0;
749 	}
750 
751 	/* Fail unless a retry is permitted */
752 	if ( ! ( http->response.flags & HTTP_RESPONSE_RETRY ) )
753 		return http->response.rc;
754 
755 	/* Perform authentication, if applicable */
756 	if ( ( auth = http->response.auth.auth ) ) {
757 		http->request.auth.auth = auth;
758 		DBGC2 ( http, "HTTP %p performing %s authentication\n",
759 			http, auth->name );
760 		if ( ( rc = auth->authenticate ( http ) ) != 0 ) {
761 			DBGC ( http, "HTTP %p could not authenticate: %s\n",
762 			       http, strerror ( rc ) );
763 			return rc;
764 		}
765 	}
766 
767 	/* Restart content decoding interfaces */
768 	intfs_restart ( http->response.rc, &http->content, &http->transfer,
769 			NULL );
770 	intf_plug_plug ( &http->transfer, &http->content );
771 	http->len = 0;
772 	assert ( http->remaining == 0 );
773 
774 	/* Start timer to initiate retry */
775 	DBGC2 ( http, "HTTP %p retrying after %d seconds\n",
776 		http, http->response.retry_after );
777 	start_timer_fixed ( &http->timer,
778 			    ( http->response.retry_after * TICKS_PER_SEC ) );
779 	return 0;
780 }
781 
782 /******************************************************************************
783  *
784  * Requests
785  *
786  ******************************************************************************
787  */
788 
789 /**
790  * Construct HTTP request headers
791  *
792  * @v http		HTTP transaction
793  * @v buf		Buffer
794  * @v len		Length of buffer
795  * @ret len		Length, or negative error
796  */
http_format_headers(struct http_transaction * http,char * buf,size_t len)797 static int http_format_headers ( struct http_transaction *http, char *buf,
798 				 size_t len ) {
799 	struct http_request_header *header;
800 	size_t used;
801 	size_t remaining;
802 	char *line;
803 	int value_len;
804 	int rc;
805 
806 	/* Construct request line */
807 	used = ssnprintf ( buf, len, "%s %s HTTP/1.1",
808 			   http->request.method->name, http->request.uri );
809 	if ( used < len )
810 		DBGC2 ( http, "HTTP %p TX %s\n", http, buf );
811 	used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
812 
813 	/* Construct all headers */
814 	for_each_table_entry ( header, HTTP_REQUEST_HEADERS ) {
815 
816 		/* Determine header value length */
817 		value_len = header->format ( http, NULL, 0 );
818 		if ( value_len < 0 ) {
819 			rc = value_len;
820 			return rc;
821 		}
822 
823 		/* Skip zero-length headers */
824 		if ( ! value_len )
825 			continue;
826 
827 		/* Construct header */
828 		line = ( buf + used );
829 		used += ssnprintf ( ( buf + used ), ( len - used ), "%s: ",
830 				    header->name );
831 		remaining = ( ( used < len ) ? ( len - used ) : 0 );
832 		used += header->format ( http, ( buf + used ), remaining );
833 		if ( used < len )
834 			DBGC2 ( http, "HTTP %p TX %s\n", http, line );
835 		used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
836 	}
837 
838 	/* Construct terminating newline */
839 	used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
840 
841 	return used;
842 }
843 
844 /**
845  * Construct HTTP "Host" header
846  *
847  * @v http		HTTP transaction
848  * @v buf		Buffer
849  * @v len		Length of buffer
850  * @ret len		Length of header value, or negative error
851  */
http_format_host(struct http_transaction * http,char * buf,size_t len)852 static int http_format_host ( struct http_transaction *http, char *buf,
853 			      size_t len ) {
854 
855 	/* Construct host URI */
856 	return snprintf ( buf, len, "%s", http->request.host );
857 }
858 
859 /** HTTP "Host" header "*/
860 struct http_request_header http_request_host __http_request_header = {
861 	.name = "Host",
862 	.format = http_format_host,
863 };
864 
865 /**
866  * Construct HTTP "User-Agent" header
867  *
868  * @v http		HTTP transaction
869  * @v buf		Buffer
870  * @v len		Length of buffer
871  * @ret len		Length of header value, or negative error
872  */
http_format_user_agent(struct http_transaction * http __unused,char * buf,size_t len)873 static int http_format_user_agent ( struct http_transaction *http __unused,
874 				    char *buf, size_t len ) {
875 
876 	/* Construct user agent */
877 	return snprintf ( buf, len, "iPXE/%s", product_version );
878 }
879 
880 /** HTTP "User-Agent" header */
881 struct http_request_header http_request_user_agent __http_request_header = {
882 	.name = "User-Agent",
883 	.format = http_format_user_agent,
884 };
885 
886 /**
887  * Construct HTTP "Connection" header
888  *
889  * @v http		HTTP transaction
890  * @v buf		Buffer
891  * @v len		Length of buffer
892  * @ret len		Length of header value, or negative error
893  */
http_format_connection(struct http_transaction * http __unused,char * buf,size_t len)894 static int http_format_connection ( struct http_transaction *http __unused,
895 				    char *buf, size_t len ) {
896 
897 	/* Always request keep-alive */
898 	return snprintf ( buf, len, "keep-alive" );
899 }
900 
901 /** HTTP "Connection" header */
902 struct http_request_header http_request_connection __http_request_header = {
903 	.name = "Connection",
904 	.format = http_format_connection,
905 };
906 
907 /**
908  * Construct HTTP "Range" header
909  *
910  * @v http		HTTP transaction
911  * @v buf		Buffer
912  * @v len		Length of buffer
913  * @ret len		Length of header value, or negative error
914  */
http_format_range(struct http_transaction * http,char * buf,size_t len)915 static int http_format_range ( struct http_transaction *http,
916 			       char *buf, size_t len ) {
917 
918 	/* Construct range, if applicable */
919 	if ( http->request.range.len ) {
920 		return snprintf ( buf, len, "bytes=%zd-%zd",
921 				  http->request.range.start,
922 				  ( http->request.range.start +
923 				    http->request.range.len - 1 ) );
924 	} else {
925 		return 0;
926 	}
927 }
928 
929 /** HTTP "Range" header */
930 struct http_request_header http_request_range __http_request_header = {
931 	.name = "Range",
932 	.format = http_format_range,
933 };
934 
935 /**
936  * Construct HTTP "Content-Type" header
937  *
938  * @v http		HTTP transaction
939  * @v buf		Buffer
940  * @v len		Length of buffer
941  * @ret len		Length of header value, or negative error
942  */
http_format_content_type(struct http_transaction * http,char * buf,size_t len)943 static int http_format_content_type ( struct http_transaction *http,
944 				      char *buf, size_t len ) {
945 
946 	/* Construct content type, if applicable */
947 	if ( http->request.content.type ) {
948 		return snprintf ( buf, len, "%s", http->request.content.type );
949 	} else {
950 		return 0;
951 	}
952 }
953 
954 /** HTTP "Content-Type" header */
955 struct http_request_header http_request_content_type __http_request_header = {
956 	.name = "Content-Type",
957 	.format = http_format_content_type,
958 };
959 
960 /**
961  * Construct HTTP "Content-Length" header
962  *
963  * @v http		HTTP transaction
964  * @v buf		Buffer
965  * @v len		Length of buffer
966  * @ret len		Length of header value, or negative error
967  */
http_format_content_length(struct http_transaction * http,char * buf,size_t len)968 static int http_format_content_length ( struct http_transaction *http,
969 					char *buf, size_t len ) {
970 
971 	/* Construct content length, if applicable */
972 	if ( http->request.content.len ) {
973 		return snprintf ( buf, len, "%zd", http->request.content.len );
974 	} else {
975 		return 0;
976 	}
977 }
978 
979 /** HTTP "Content-Length" header */
980 struct http_request_header http_request_content_length __http_request_header = {
981 	.name = "Content-Length",
982 	.format = http_format_content_length,
983 };
984 
985 /**
986  * Construct HTTP "Accept-Encoding" header
987  *
988  * @v http		HTTP transaction
989  * @v buf		Buffer
990  * @v len		Length of buffer
991  * @ret len		Length of header value, or negative error
992  */
http_format_accept_encoding(struct http_transaction * http,char * buf,size_t len)993 static int http_format_accept_encoding ( struct http_transaction *http,
994 					 char *buf, size_t len ) {
995 	struct http_content_encoding *encoding;
996 	const char *sep = "";
997 	size_t used = 0;
998 
999 	/* Construct list of content encodings */
1000 	for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
1001 		if ( encoding->supported && ( ! encoding->supported ( http ) ) )
1002 			continue;
1003 		used += ssnprintf ( ( buf + used ), ( len - used ),
1004 				    "%s%s", sep, encoding->name );
1005 		sep = ", ";
1006 	}
1007 
1008 	return used;
1009 }
1010 
1011 /** HTTP "Accept-Encoding" header */
1012 struct http_request_header http_request_accept_encoding __http_request_header ={
1013 	.name = "Accept-Encoding",
1014 	.format = http_format_accept_encoding,
1015 };
1016 
1017 /**
1018  * Transmit request
1019  *
1020  * @v http		HTTP transaction
1021  * @ret rc		Return status code
1022  */
http_tx_request(struct http_transaction * http)1023 static int http_tx_request ( struct http_transaction *http ) {
1024 	struct io_buffer *iobuf;
1025 	int len;
1026 	int check_len;
1027 	int rc;
1028 
1029 	/* Calculate request length */
1030 	len = http_format_headers ( http, NULL, 0 );
1031 	if ( len < 0 ) {
1032 		rc = len;
1033 		DBGC ( http, "HTTP %p could not construct request: %s\n",
1034 		       http, strerror ( rc ) );
1035 		goto err_len;
1036 	}
1037 
1038 	/* Allocate I/O buffer */
1039 	iobuf = alloc_iob ( len + 1 /* NUL */ + http->request.content.len );
1040 	if ( ! iobuf ) {
1041 		rc = -ENOMEM;
1042 		goto err_alloc;
1043 	}
1044 
1045 	/* Construct request */
1046 	check_len = http_format_headers ( http, iob_put ( iobuf, len ),
1047 					  ( len + 1 /* NUL */ ) );
1048 	assert ( check_len == len );
1049 	memcpy ( iob_put ( iobuf, http->request.content.len ),
1050 		 http->request.content.data, http->request.content.len );
1051 
1052 	/* Deliver request */
1053 	if ( ( rc = xfer_deliver_iob ( &http->conn,
1054 				       iob_disown ( iobuf ) ) ) != 0 ) {
1055 		DBGC ( http, "HTTP %p could not deliver request: %s\n",
1056 		       http, strerror ( rc ) );
1057 		goto err_deliver;
1058 	}
1059 
1060 	/* Clear any previous response */
1061 	empty_line_buffer ( &http->response.headers );
1062 	memset ( &http->response, 0, sizeof ( http->response ) );
1063 
1064 	/* Move to response headers state */
1065 	http->state = &http_headers;
1066 
1067 	return 0;
1068 
1069  err_deliver:
1070 	free_iob ( iobuf );
1071  err_alloc:
1072  err_len:
1073 	return rc;
1074 }
1075 
1076 /** HTTP request state */
1077 static struct http_state http_request = {
1078 	.tx = http_tx_request,
1079 	.close = http_close_error,
1080 };
1081 
1082 /******************************************************************************
1083  *
1084  * Response headers
1085  *
1086  ******************************************************************************
1087  */
1088 
1089 /**
1090  * Parse HTTP status line
1091  *
1092  * @v http		HTTP transaction
1093  * @v line		Status line
1094  * @ret rc		Return status code
1095  */
http_parse_status(struct http_transaction * http,char * line)1096 static int http_parse_status ( struct http_transaction *http, char *line ) {
1097 	char *endp;
1098 	char *version;
1099 	char *vernum;
1100 	char *status;
1101 	int response_rc;
1102 
1103 	DBGC2 ( http, "HTTP %p RX %s\n", http, line );
1104 
1105 	/* Parse HTTP version */
1106 	version = http_token ( &line, NULL );
1107 	if ( ( ! version ) || ( strncmp ( version, "HTTP/", 5 ) != 0 ) ) {
1108 		DBGC ( http, "HTTP %p malformed version \"%s\"\n", http, line );
1109 		return -EINVAL_STATUS;
1110 	}
1111 
1112 	/* Keepalive is enabled by default for anything newer than HTTP/1.0 */
1113 	vernum = ( version + 5 /* "HTTP/" (presence already checked) */ );
1114 	if ( vernum[0] == '0' ) {
1115 		/* HTTP/0.x : keepalive not enabled by default */
1116 	} else if ( strncmp ( vernum, "1.0", 3 ) == 0 ) {
1117 		/* HTTP/1.0 : keepalive not enabled by default */
1118 	} else {
1119 		/* HTTP/1.1 or newer: keepalive enabled by default */
1120 		http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
1121 	}
1122 
1123 	/* Parse status code */
1124 	status = line;
1125 	http->response.status = strtoul ( status, &endp, 10 );
1126 	if ( *endp != ' ' ) {
1127 		DBGC ( http, "HTTP %p malformed status code \"%s\"\n",
1128 		       http, status );
1129 		return -EINVAL_STATUS;
1130 	}
1131 
1132 	/* Convert HTTP status code to iPXE return status code */
1133 	if ( status[0] == '2' ) {
1134 		/* 2xx Success */
1135 		response_rc = 0;
1136 	} else if ( status[0] == '3' ) {
1137 		/* 3xx Redirection */
1138 		response_rc = -EXDEV;
1139 	} else if ( http->response.status == 401 ) {
1140 		/* 401 Unauthorized */
1141 		response_rc = -EACCES_401;
1142 	} else if ( http->response.status == 403 ) {
1143 		/* 403 Forbidden */
1144 		response_rc = -EPERM_403;
1145 	} else if ( http->response.status == 404 ) {
1146 		/* 404 Not Found */
1147 		response_rc = -ENOENT_404;
1148 	} else if ( status[0] == '4' ) {
1149 		/* 4xx Client Error (not already specified) */
1150 		response_rc = -EIO_4XX;
1151 	} else if ( status[0] == '5' ) {
1152 		/* 5xx Server Error */
1153 		response_rc = -EIO_5XX;
1154 	} else {
1155 		/* Unrecognised */
1156 		response_rc = -EIO_OTHER;
1157 	}
1158 	http->response.rc = response_rc;
1159 
1160 	return 0;
1161 }
1162 
1163 /**
1164  * Parse HTTP header
1165  *
1166  * @v http		HTTP transaction
1167  * @v line		Header line
1168  * @ret rc		Return status code
1169  */
http_parse_header(struct http_transaction * http,char * line)1170 static int http_parse_header ( struct http_transaction *http, char *line ) {
1171 	struct http_response_header *header;
1172 	char *name = line;
1173 	char *sep;
1174 
1175 	DBGC2 ( http, "HTTP %p RX %s\n", http, line );
1176 
1177 	/* Extract header name */
1178 	sep = strchr ( line, ':' );
1179 	if ( ! sep ) {
1180 		DBGC ( http, "HTTP %p malformed header \"%s\"\n", http, line );
1181 		return -EINVAL_HEADER;
1182 	}
1183 	*sep = '\0';
1184 
1185 	/* Extract remainder of line */
1186 	line = ( sep + 1 );
1187 	while ( isspace ( *line ) )
1188 		line++;
1189 
1190 	/* Process header, if recognised */
1191 	for_each_table_entry ( header, HTTP_RESPONSE_HEADERS ) {
1192 		if ( strcasecmp ( name, header->name ) == 0 )
1193 			return header->parse ( http, line );
1194 	}
1195 
1196 	/* Unrecognised headers should be ignored */
1197 	return 0;
1198 }
1199 
1200 /**
1201  * Parse HTTP response headers
1202  *
1203  * @v http		HTTP transaction
1204  * @ret rc		Return status code
1205  */
http_parse_headers(struct http_transaction * http)1206 static int http_parse_headers ( struct http_transaction *http ) {
1207 	char *line;
1208 	char *next;
1209 	int rc;
1210 
1211 	/* Get status line */
1212 	line = http->response.headers.data;
1213 	assert ( line != NULL );
1214 	next = ( line + strlen ( line ) + 1 /* NUL */ );
1215 
1216 	/* Parse status line */
1217 	if ( ( rc = http_parse_status ( http, line ) ) != 0 )
1218 		return rc;
1219 
1220 	/* Process header lines */
1221 	while ( 1 ) {
1222 
1223 		/* Move to next line */
1224 		line = next;
1225 		next = ( line + strlen ( line ) + 1 /* NUL */ );
1226 
1227 		/* Stop on terminating blank line */
1228 		if ( ! line[0] )
1229 			return 0;
1230 
1231 		/* Process header line */
1232 		if ( ( rc = http_parse_header ( http, line ) ) != 0 )
1233 			return rc;
1234 	}
1235 }
1236 
1237 /**
1238  * Parse HTTP "Location" header
1239  *
1240  * @v http		HTTP transaction
1241  * @v line		Remaining header line
1242  * @ret rc		Return status code
1243  */
http_parse_location(struct http_transaction * http,char * line)1244 static int http_parse_location ( struct http_transaction *http, char *line ) {
1245 
1246 	/* Store location */
1247 	http->response.location = line;
1248 	return 0;
1249 }
1250 
1251 /** HTTP "Location" header */
1252 struct http_response_header http_response_location __http_response_header = {
1253 	.name = "Location",
1254 	.parse = http_parse_location,
1255 };
1256 
1257 /**
1258  * Parse HTTP "Transfer-Encoding" header
1259  *
1260  * @v http		HTTP transaction
1261  * @v line		Remaining header line
1262  * @ret rc		Return status code
1263  */
http_parse_transfer_encoding(struct http_transaction * http,char * line)1264 static int http_parse_transfer_encoding ( struct http_transaction *http,
1265 					  char *line ) {
1266 	struct http_transfer_encoding *encoding;
1267 
1268 	/* Check for known transfer encodings */
1269 	for_each_table_entry ( encoding, HTTP_TRANSFER_ENCODINGS ) {
1270 		if ( strcasecmp ( line, encoding->name ) == 0 ) {
1271 			http->response.transfer.encoding = encoding;
1272 			return 0;
1273 		}
1274 	}
1275 
1276 	DBGC ( http, "HTTP %p unrecognised Transfer-Encoding \"%s\"\n",
1277 	       http, line );
1278 	return -ENOTSUP_TRANSFER;
1279 }
1280 
1281 /** HTTP "Transfer-Encoding" header */
1282 struct http_response_header
1283 http_response_transfer_encoding __http_response_header = {
1284 	.name = "Transfer-Encoding",
1285 	.parse = http_parse_transfer_encoding,
1286 };
1287 
1288 /**
1289  * Parse HTTP "Connection" header
1290  *
1291  * @v http		HTTP transaction
1292  * @v line		Remaining header line
1293  * @ret rc		Return status code
1294  */
http_parse_connection(struct http_transaction * http,char * line)1295 static int http_parse_connection ( struct http_transaction *http, char *line ) {
1296 	char *token;
1297 
1298 	/* Check for known connection intentions */
1299 	while ( ( token = http_token ( &line, NULL ) ) ) {
1300 		if ( strcasecmp ( token, "keep-alive" ) == 0 )
1301 			http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
1302 		if ( strcasecmp ( token, "close" ) == 0 )
1303 			http->response.flags &= ~HTTP_RESPONSE_KEEPALIVE;
1304 	}
1305 
1306 	return 0;
1307 }
1308 
1309 /** HTTP "Connection" header */
1310 struct http_response_header http_response_connection __http_response_header = {
1311 	.name = "Connection",
1312 	.parse = http_parse_connection,
1313 };
1314 
1315 /**
1316  * Parse HTTP "Content-Length" header
1317  *
1318  * @v http		HTTP transaction
1319  * @v line		Remaining header line
1320  * @ret rc		Return status code
1321  */
http_parse_content_length(struct http_transaction * http,char * line)1322 static int http_parse_content_length ( struct http_transaction *http,
1323 				       char *line ) {
1324 	char *endp;
1325 
1326 	/* Parse length */
1327 	http->response.content.len = strtoul ( line, &endp, 10 );
1328 	if ( *endp != '\0' ) {
1329 		DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
1330 		       http, line );
1331 		return -EINVAL_CONTENT_LENGTH;
1332 	}
1333 
1334 	/* Record that we have a content length (since it may be zero) */
1335 	http->response.flags |= HTTP_RESPONSE_CONTENT_LEN;
1336 
1337 	return 0;
1338 }
1339 
1340 /** HTTP "Content-Length" header */
1341 struct http_response_header
1342 http_response_content_length __http_response_header = {
1343 	.name = "Content-Length",
1344 	.parse = http_parse_content_length,
1345 };
1346 
1347 /**
1348  * Parse HTTP "Content-Encoding" header
1349  *
1350  * @v http		HTTP transaction
1351  * @v line		Remaining header line
1352  * @ret rc		Return status code
1353  */
http_parse_content_encoding(struct http_transaction * http,char * line)1354 static int http_parse_content_encoding ( struct http_transaction *http,
1355 					 char *line ) {
1356 	struct http_content_encoding *encoding;
1357 
1358 	/* Check for known content encodings */
1359 	for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
1360 		if ( encoding->supported && ( ! encoding->supported ( http ) ) )
1361 			continue;
1362 		if ( strcasecmp ( line, encoding->name ) == 0 ) {
1363 			http->response.content.encoding = encoding;
1364 			return 0;
1365 		}
1366 	}
1367 
1368 	/* Some servers (e.g. Apache) have a habit of specifying
1369 	 * unwarranted content encodings.  For example, if Apache
1370 	 * detects (via /etc/httpd/conf/magic) that a file's contents
1371 	 * are gzip-compressed, it will set "Content-Encoding: x-gzip"
1372 	 * regardless of the client's Accept-Encoding header.  The
1373 	 * only viable way to handle such servers is to treat unknown
1374 	 * content encodings as equivalent to "identity".
1375 	 */
1376 	DBGC ( http, "HTTP %p unrecognised Content-Encoding \"%s\"\n",
1377 	       http, line );
1378 	return 0;
1379 }
1380 
1381 /** HTTP "Content-Encoding" header */
1382 struct http_response_header
1383 http_response_content_encoding __http_response_header = {
1384 	.name = "Content-Encoding",
1385 	.parse = http_parse_content_encoding,
1386 };
1387 
1388 /**
1389  * Parse HTTP "Retry-After" header
1390  *
1391  * @v http		HTTP transaction
1392  * @v line		Remaining header line
1393  * @ret rc		Return status code
1394  */
http_parse_retry_after(struct http_transaction * http,char * line)1395 static int http_parse_retry_after ( struct http_transaction *http,
1396 				    char *line ) {
1397 	char *endp;
1398 
1399 	/* Try to parse value as a simple number of seconds */
1400 	http->response.retry_after = strtoul ( line, &endp, 10 );
1401 	if ( *endp != '\0' ) {
1402 		/* For any value which is not a simple number of
1403 		 * seconds (e.g. a full HTTP date), just retry after a
1404 		 * fixed delay, since we don't have code able to parse
1405 		 * full HTTP dates.
1406 		 */
1407 		http->response.retry_after = HTTP_RETRY_SECONDS;
1408 		DBGC ( http, "HTTP %p cannot understand Retry-After \"%s\"; "
1409 		       "using %d seconds\n", http, line, HTTP_RETRY_SECONDS );
1410 	}
1411 
1412 	/* Allow HTTP request to be retried after specified delay */
1413 	http->response.flags |= HTTP_RESPONSE_RETRY;
1414 
1415 	return 0;
1416 }
1417 
1418 /** HTTP "Retry-After" header */
1419 struct http_response_header http_response_retry_after __http_response_header = {
1420 	.name = "Retry-After",
1421 	.parse = http_parse_retry_after,
1422 };
1423 
1424 /**
1425  * Handle received HTTP headers
1426  *
1427  * @v http		HTTP transaction
1428  * @v iobuf		I/O buffer (may be claimed)
1429  * @ret rc		Return status code
1430  */
http_rx_headers(struct http_transaction * http,struct io_buffer ** iobuf)1431 static int http_rx_headers ( struct http_transaction *http,
1432 			     struct io_buffer **iobuf ) {
1433 	struct http_transfer_encoding *transfer;
1434 	struct http_content_encoding *content;
1435 	char *line;
1436 	int rc;
1437 
1438 	/* Buffer header line */
1439 	if ( ( rc = http_rx_linebuf ( http, *iobuf,
1440 				      &http->response.headers ) ) != 0 )
1441 		return rc;
1442 
1443 	/* Wait until we see the empty line marking end of headers */
1444 	line = buffered_line ( &http->response.headers );
1445 	if ( ( line == NULL ) || ( line[0] != '\0' ) )
1446 		return 0;
1447 
1448 	/* Process headers */
1449 	if ( ( rc = http_parse_headers ( http ) ) != 0 )
1450 		return rc;
1451 
1452 	/* Initialise content encoding, if applicable */
1453 	if ( ( content = http->response.content.encoding ) &&
1454 	     ( ( rc = content->init ( http ) ) != 0 ) ) {
1455 		DBGC ( http, "HTTP %p could not initialise %s content "
1456 		       "encoding: %s\n", http, content->name, strerror ( rc ) );
1457 		return rc;
1458 	}
1459 
1460 	/* Presize receive buffer, if we have a content length */
1461 	if ( http->response.content.len ) {
1462 		xfer_seek ( &http->transfer, http->response.content.len );
1463 		xfer_seek ( &http->transfer, 0 );
1464 	}
1465 
1466 	/* Complete transfer if this is a HEAD request */
1467 	if ( http->request.method == &http_head ) {
1468 		if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1469 			return rc;
1470 		return 0;
1471 	}
1472 
1473 	/* Default to identity transfer encoding, if none specified */
1474 	if ( ! http->response.transfer.encoding )
1475 		http->response.transfer.encoding = &http_transfer_identity;
1476 
1477 	/* Move to transfer encoding-specific data state */
1478 	transfer = http->response.transfer.encoding;
1479 	http->state = &transfer->state;
1480 
1481 	/* Initialise transfer encoding */
1482 	if ( ( rc = transfer->init ( http ) ) != 0 ) {
1483 		DBGC ( http, "HTTP %p could not initialise %s transfer "
1484 		       "encoding: %s\n", http, transfer->name, strerror ( rc ));
1485 		return rc;
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 /** HTTP response headers state */
1492 static struct http_state http_headers = {
1493 	.rx = http_rx_headers,
1494 	.close = http_close_error,
1495 };
1496 
1497 /******************************************************************************
1498  *
1499  * Identity transfer encoding
1500  *
1501  ******************************************************************************
1502  */
1503 
1504 /**
1505  * Initialise transfer encoding
1506  *
1507  * @v http		HTTP transaction
1508  * @ret rc		Return status code
1509  */
http_init_transfer_identity(struct http_transaction * http)1510 static int http_init_transfer_identity ( struct http_transaction *http ) {
1511 	int rc;
1512 
1513 	/* Complete transfer immediately if we have a zero content length */
1514 	if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1515 	     ( http->response.content.len == 0 ) &&
1516 	     ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
1517 		return rc;
1518 
1519 	return 0;
1520 }
1521 
1522 /**
1523  * Handle received data
1524  *
1525  * @v http		HTTP transaction
1526  * @v iobuf		I/O buffer (may be claimed)
1527  * @ret rc		Return status code
1528  */
http_rx_transfer_identity(struct http_transaction * http,struct io_buffer ** iobuf)1529 static int http_rx_transfer_identity ( struct http_transaction *http,
1530 				       struct io_buffer **iobuf ) {
1531 	size_t len = iob_len ( *iobuf );
1532 	int rc;
1533 
1534 	/* Update lengths */
1535 	http->len += len;
1536 
1537 	/* Fail if this transfer would overrun the expected content
1538 	 * length (if any).
1539 	 */
1540 	if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1541 	     ( http->len > http->response.content.len ) ) {
1542 		DBGC ( http, "HTTP %p content length overrun\n", http );
1543 		return -EIO_CONTENT_LENGTH;
1544 	}
1545 
1546 	/* Hand off to content encoding */
1547 	if ( ( rc = xfer_deliver_iob ( &http->transfer,
1548 				       iob_disown ( *iobuf ) ) ) != 0 )
1549 		return rc;
1550 
1551 	/* Complete transfer if we have received the expected content
1552 	 * length (if any).
1553 	 */
1554 	if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1555 	     ( http->len == http->response.content.len ) &&
1556 	     ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
1557 		return rc;
1558 
1559 	return 0;
1560 }
1561 
1562 /**
1563  * Handle server connection close
1564  *
1565  * @v http		HTTP transaction
1566  * @v rc		Reason for close
1567  */
http_close_transfer_identity(struct http_transaction * http,int rc)1568 static void http_close_transfer_identity ( struct http_transaction *http,
1569 					   int rc ) {
1570 
1571 	/* Fail if any error occurred */
1572 	if ( rc != 0 )
1573 		goto err;
1574 
1575 	/* Fail if we have a content length (since we would have
1576 	 * already closed the connection if we had received the
1577 	 * correct content length).
1578 	 */
1579 	if ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) {
1580 		DBGC ( http, "HTTP %p content length underrun\n", http );
1581 		rc = EIO_CONTENT_LENGTH;
1582 		goto err;
1583 	}
1584 
1585 	/* Indicate that transfer is complete */
1586 	if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1587 		goto err;
1588 
1589 	return;
1590 
1591  err:
1592 	http_close ( http, rc );
1593 }
1594 
1595 /** Identity transfer encoding */
1596 static struct http_transfer_encoding http_transfer_identity = {
1597 	.name = "identity",
1598 	.init = http_init_transfer_identity,
1599 	.state = {
1600 		.rx = http_rx_transfer_identity,
1601 		.close = http_close_transfer_identity,
1602 	},
1603 };
1604 
1605 /******************************************************************************
1606  *
1607  * Chunked transfer encoding
1608  *
1609  ******************************************************************************
1610  */
1611 
1612 /**
1613  * Initialise transfer encoding
1614  *
1615  * @v http		HTTP transaction
1616  * @ret rc		Return status code
1617  */
http_init_transfer_chunked(struct http_transaction * http)1618 static int http_init_transfer_chunked ( struct http_transaction *http ) {
1619 
1620 	/* Sanity checks */
1621 	assert ( http->remaining == 0 );
1622 	assert ( http->linebuf.len == 0 );
1623 
1624 	return 0;
1625 }
1626 
1627 /**
1628  * Handle received chunk length
1629  *
1630  * @v http		HTTP transaction
1631  * @v iobuf		I/O buffer (may be claimed)
1632  * @ret rc		Return status code
1633  */
http_rx_chunk_len(struct http_transaction * http,struct io_buffer ** iobuf)1634 static int http_rx_chunk_len ( struct http_transaction *http,
1635 			       struct io_buffer **iobuf ) {
1636 	char *line;
1637 	char *endp;
1638 	size_t len;
1639 	int rc;
1640 
1641 	/* Receive into temporary line buffer */
1642 	if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
1643 		return rc;
1644 
1645 	/* Wait until we receive a non-empty line */
1646 	line = buffered_line ( &http->linebuf );
1647 	if ( ( line == NULL ) || ( line[0] == '\0' ) )
1648 		return 0;
1649 
1650 	/* Parse chunk length */
1651 	http->remaining = strtoul ( line, &endp, 16 );
1652 	if ( *endp != '\0' ) {
1653 		DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
1654 		       http, line );
1655 		return -EINVAL_CHUNK_LENGTH;
1656 	}
1657 
1658 	/* Empty line buffer */
1659 	empty_line_buffer ( &http->linebuf );
1660 
1661 	/* Update expected length */
1662 	len = ( http->len + http->remaining );
1663 	xfer_seek ( &http->transfer, len );
1664 	xfer_seek ( &http->transfer, http->len );
1665 
1666 	/* If chunk length is zero, then move to response trailers state */
1667 	if ( ! http->remaining )
1668 		http->state = &http_trailers;
1669 
1670 	return 0;
1671 }
1672 
1673 /**
1674  * Handle received chunk data
1675  *
1676  * @v http		HTTP transaction
1677  * @v iobuf		I/O buffer (may be claimed)
1678  * @ret rc		Return status code
1679  */
http_rx_chunk_data(struct http_transaction * http,struct io_buffer ** iobuf)1680 static int http_rx_chunk_data ( struct http_transaction *http,
1681 				struct io_buffer **iobuf ) {
1682 	struct io_buffer *payload;
1683 	uint8_t *crlf;
1684 	size_t len;
1685 	int rc;
1686 
1687 	/* In the common case of a final chunk in a packet which also
1688 	 * includes the terminating CRLF, strip the terminating CRLF
1689 	 * (which we would ignore anyway) and hence avoid
1690 	 * unnecessarily copying the data.
1691 	 */
1692 	if ( iob_len ( *iobuf ) == ( http->remaining + 2 /* CRLF */ ) ) {
1693 		crlf = ( (*iobuf)->data + http->remaining );
1694 		if ( ( crlf[0] == '\r' ) && ( crlf[1] == '\n' ) )
1695 			iob_unput ( (*iobuf), 2 /* CRLF */ );
1696 	}
1697 	len = iob_len ( *iobuf );
1698 
1699 	/* Use whole/partial buffer as applicable */
1700 	if ( len <= http->remaining ) {
1701 
1702 		/* Whole buffer is to be consumed: decrease remaining
1703 		 * length and use original I/O buffer as payload.
1704 		 */
1705 		payload = iob_disown ( *iobuf );
1706 		http->len += len;
1707 		http->remaining -= len;
1708 
1709 	} else {
1710 
1711 		/* Partial buffer is to be consumed: copy data to a
1712 		 * temporary I/O buffer.
1713 		 */
1714 		payload = alloc_iob ( http->remaining );
1715 		if ( ! payload ) {
1716 			rc = -ENOMEM;
1717 			goto err;
1718 		}
1719 		memcpy ( iob_put ( payload, http->remaining ), (*iobuf)->data,
1720 			 http->remaining );
1721 		iob_pull ( *iobuf, http->remaining );
1722 		http->len += http->remaining;
1723 		http->remaining = 0;
1724 	}
1725 
1726 	/* Hand off to content encoding */
1727 	if ( ( rc = xfer_deliver_iob ( &http->transfer,
1728 				       iob_disown ( payload ) ) ) != 0 )
1729 		goto err;
1730 
1731 	return 0;
1732 
1733  err:
1734 	assert ( payload == NULL );
1735 	return rc;
1736 }
1737 
1738 /**
1739  * Handle received chunked data
1740  *
1741  * @v http		HTTP transaction
1742  * @v iobuf		I/O buffer (may be claimed)
1743  * @ret rc		Return status code
1744  */
http_rx_transfer_chunked(struct http_transaction * http,struct io_buffer ** iobuf)1745 static int http_rx_transfer_chunked ( struct http_transaction *http,
1746 				      struct io_buffer **iobuf ) {
1747 
1748 	/* Handle as chunk length or chunk data as appropriate */
1749 	if ( http->remaining ) {
1750 		return http_rx_chunk_data ( http, iobuf );
1751 	} else {
1752 		return http_rx_chunk_len ( http, iobuf );
1753 	}
1754 }
1755 
1756 /** Chunked transfer encoding */
1757 struct http_transfer_encoding http_transfer_chunked __http_transfer_encoding = {
1758 	.name = "chunked",
1759 	.init = http_init_transfer_chunked,
1760 	.state = {
1761 		.rx = http_rx_transfer_chunked,
1762 		.close = http_close_error,
1763 	},
1764 };
1765 
1766 /******************************************************************************
1767  *
1768  * Response trailers
1769  *
1770  ******************************************************************************
1771  */
1772 
1773 /**
1774  * Handle received HTTP trailer
1775  *
1776  * @v http		HTTP transaction
1777  * @v iobuf		I/O buffer (may be claimed)
1778  * @ret rc		Return status code
1779  */
http_rx_trailers(struct http_transaction * http,struct io_buffer ** iobuf)1780 static int http_rx_trailers ( struct http_transaction *http,
1781 			      struct io_buffer **iobuf ) {
1782 	char *line;
1783 	int rc;
1784 
1785 	/* Buffer trailer line */
1786 	if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
1787 		return rc;
1788 
1789 	/* Wait until we see the empty line marking end of trailers */
1790 	line = buffered_line ( &http->linebuf );
1791 	if ( ( line == NULL ) || ( line[0] != '\0' ) )
1792 		return 0;
1793 
1794 	/* Empty line buffer */
1795 	empty_line_buffer ( &http->linebuf );
1796 
1797 	/* Transfer is complete */
1798 	if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1799 		return rc;
1800 
1801 	return 0;
1802 }
1803 
1804 /** HTTP response trailers state */
1805 static struct http_state http_trailers = {
1806 	.rx = http_rx_trailers,
1807 	.close = http_close_error,
1808 };
1809 
1810 /******************************************************************************
1811  *
1812  * Simple URI openers
1813  *
1814  ******************************************************************************
1815  */
1816 
1817 /**
1818  * Construct HTTP parameter list
1819  *
1820  * @v params		Parameter list
1821  * @v buf		Buffer to contain HTTP POST parameters
1822  * @v len		Length of buffer
1823  * @ret len		Length of parameter list (excluding terminating NUL)
1824  */
http_params(struct parameters * params,char * buf,size_t len)1825 static size_t http_params ( struct parameters *params, char *buf, size_t len ) {
1826 	struct parameter *param;
1827 	ssize_t remaining = len;
1828 	size_t frag_len;
1829 
1830 	/* Add each parameter in the form "key=value", joined with "&" */
1831 	len = 0;
1832 	for_each_param ( param, params ) {
1833 
1834 		/* Add the "&", if applicable */
1835 		if ( len ) {
1836 			if ( remaining > 0 )
1837 				*buf = '&';
1838 			buf++;
1839 			len++;
1840 			remaining--;
1841 		}
1842 
1843 		/* URI-encode the key */
1844 		frag_len = uri_encode_string ( 0, param->key, buf, remaining );
1845 		buf += frag_len;
1846 		len += frag_len;
1847 		remaining -= frag_len;
1848 
1849 		/* Add the "=" */
1850 		if ( remaining > 0 )
1851 			*buf = '=';
1852 		buf++;
1853 		len++;
1854 		remaining--;
1855 
1856 		/* URI-encode the value */
1857 		frag_len = uri_encode_string ( 0, param->value, buf, remaining);
1858 		buf += frag_len;
1859 		len += frag_len;
1860 		remaining -= frag_len;
1861 	}
1862 
1863 	/* Ensure string is NUL-terminated even if no parameters are present */
1864 	if ( remaining > 0 )
1865 		*buf = '\0';
1866 
1867 	return len;
1868 }
1869 
1870 /**
1871  * Open HTTP transaction for simple GET URI
1872  *
1873  * @v xfer		Data transfer interface
1874  * @v uri		Request URI
1875  * @ret rc		Return status code
1876  */
http_open_get_uri(struct interface * xfer,struct uri * uri)1877 static int http_open_get_uri ( struct interface *xfer, struct uri *uri ) {
1878 
1879 	return http_open ( xfer, &http_get, uri, NULL, NULL );
1880 }
1881 
1882 /**
1883  * Open HTTP transaction for simple POST URI
1884  *
1885  * @v xfer		Data transfer interface
1886  * @v uri		Request URI
1887  * @ret rc		Return status code
1888  */
http_open_post_uri(struct interface * xfer,struct uri * uri)1889 static int http_open_post_uri ( struct interface *xfer, struct uri *uri ) {
1890 	struct parameters *params = uri->params;
1891 	struct http_request_content content;
1892 	void *data;
1893 	size_t len;
1894 	size_t check_len;
1895 	int rc;
1896 
1897 	/* Calculate length of parameter list */
1898 	len = http_params ( params, NULL, 0 );
1899 
1900 	/* Allocate temporary parameter list */
1901 	data = zalloc ( len + 1 /* NUL */ );
1902 	if ( ! data ) {
1903 		rc = -ENOMEM;
1904 		goto err_alloc;
1905 	}
1906 
1907 	/* Construct temporary parameter list */
1908 	check_len = http_params ( params, data, ( len + 1 /* NUL */ ) );
1909 	assert ( check_len == len );
1910 
1911 	/* Construct request content */
1912 	content.type = "application/x-www-form-urlencoded";
1913 	content.data = data;
1914 	content.len = len;
1915 
1916 	/* Open HTTP transaction */
1917 	if ( ( rc = http_open ( xfer, &http_post, uri, NULL, &content ) ) != 0 )
1918 		goto err_open;
1919 
1920  err_open:
1921 	free ( data );
1922  err_alloc:
1923 	return rc;
1924 }
1925 
1926 /**
1927  * Open HTTP transaction for simple URI
1928  *
1929  * @v xfer		Data transfer interface
1930  * @v uri		Request URI
1931  * @ret rc		Return status code
1932  */
http_open_uri(struct interface * xfer,struct uri * uri)1933 int http_open_uri ( struct interface *xfer, struct uri *uri ) {
1934 
1935 	/* Open GET/POST URI as applicable */
1936 	if ( uri->params ) {
1937 		return http_open_post_uri ( xfer, uri );
1938 	} else {
1939 		return http_open_get_uri ( xfer, uri );
1940 	}
1941 }
1942 
1943 /* Drag in HTTP extensions */
1944 REQUIRING_SYMBOL ( http_open );
1945 REQUIRE_OBJECT ( config_http );
1946