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