2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
29 * Hyper Text Transfer Protocol (HTTP) core functionality
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>
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" )
104 /** Retry delay used when we cannot understand the Retry-After header */
105 #define HTTP_RETRY_SECONDS 5
107 /** Receive profiler */
108 static struct profiler http_rx_profiler __profiler = { .name = "http.rx" };
110 /** Data transfer profiler */
111 static struct profiler http_xfer_profiler __profiler = { .name = "http.xfer" };
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;
118 /******************************************************************************
122 ******************************************************************************
125 /** HTTP HEAD method */
126 struct http_method http_head = {
130 /** HTTP GET method */
131 struct http_method http_get = {
135 /** HTTP POST method */
136 struct http_method http_post = {
140 /******************************************************************************
144 ******************************************************************************
148 * Handle received HTTP line-buffered data
150 * @v http HTTP transaction
151 * @v iobuf I/O buffer
152 * @v linebuf Line buffer
153 * @ret rc Return status code
155 static int http_rx_linebuf ( struct http_transaction *http,
156 struct io_buffer *iobuf,
157 struct line_buffer *linebuf ) {
161 /* Buffer received line */
162 consumed = line_buffer ( linebuf, iobuf->data, iob_len ( iobuf ) );
163 if ( consumed < 0 ) {
165 DBGC ( http, "HTTP %p could not buffer line: %s\n",
166 http, strerror ( rc ) );
171 iob_pull ( iobuf, consumed );
177 * Get HTTP response token
179 * @v line Line position
180 * @v value Token value to fill in (if any)
181 * @ret token Token, or NULL
183 char * http_token ( char **line, char **value ) {
188 /* Avoid returning uninitialised data */
192 /* Skip any initial whitespace */
193 while ( isspace ( **line ) )
196 /* Check for end of line and record token position */
201 /* Scan for end of token */
202 while ( ( c = **line ) ) {
204 /* Terminate if we hit an unquoted whitespace */
205 if ( isspace ( c ) && ! quote )
208 /* Terminate if we hit a closing quote */
212 /* Check for value separator */
213 if ( value && ( ! *value ) && ( c == '=' ) ) {
215 /* Terminate key portion of token */
218 /* Check for quote character */
220 if ( ( c == '"' ) || ( c == '\'' ) ) {
225 /* Record value portion of token */
230 /* Move to next character */
235 /* Terminate token, if applicable */
242 /******************************************************************************
246 ******************************************************************************
250 * Free HTTP transaction
252 * @v refcnt Reference count
254 static void http_free ( struct refcnt *refcnt ) {
255 struct http_transaction *http =
256 container_of ( refcnt, struct http_transaction, refcnt );
258 empty_line_buffer ( &http->response.headers );
259 empty_line_buffer ( &http->linebuf );
260 uri_put ( http->uri );
265 * Close HTTP transaction
267 * @v http HTTP transaction
268 * @v rc Reason for close
270 static void http_close ( struct http_transaction *http, int rc ) {
273 process_del ( &http->process );
276 stop_timer ( &http->timer );
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.
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 );
290 * Close HTTP transaction with error (even if none specified)
292 * @v http HTTP transaction
293 * @v rc Reason for close
295 static void http_close_error ( struct http_transaction *http, int rc ) {
297 /* Treat any close as an error */
298 http_close ( http, ( rc ? rc : -EPIPE ) );
302 * Reopen stale HTTP connection
304 * @v http HTTP transaction
306 static void http_reopen ( struct http_transaction *http ) {
309 /* Close existing connection */
310 intf_restart ( &http->conn, -ECANCELED );
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 ) );
320 http->state = &http_request;
322 /* Reschedule transmission process */
323 process_add ( &http->process );
328 http_close ( http, rc );
332 * Handle retry timer expiry
334 * @v timer Retry timer
335 * @v over Failure indicator
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 );
341 /* Reopen connection */
342 http_reopen ( http );
346 * HTTP transmit process
348 * @v http HTTP transaction
350 static void http_step ( struct http_transaction *http ) {
353 /* Do nothing if we have nothing to transmit */
354 if ( ! http->state->tx )
357 /* Do nothing until connection is ready */
358 if ( ! xfer_window ( &http->conn ) )
361 /* Do nothing until data transfer interface is ready */
362 if ( ! xfer_window ( &http->xfer ) )
366 if ( ( rc = http->state->tx ( http ) ) != 0 )
372 http_close ( http, rc );
376 * Handle received HTTP data
378 * @v http HTTP transaction
379 * @v iobuf I/O buffer
380 * @v meta Transfer metadata
381 * @ret rc Return status code
383 * This function takes ownership of the I/O buffer.
385 static int http_conn_deliver ( struct http_transaction *http,
386 struct io_buffer *iobuf,
387 struct xfer_metadata *meta __unused ) {
390 /* Handle received data */
391 profile_start ( &http_rx_profiler );
392 while ( iobuf && iob_len ( iobuf ) ) {
395 if ( ( ! http->state ) || ( ! http->state->rx ) ) {
396 DBGC ( http, "HTTP %p unexpected data\n", http );
397 rc = -EPROTO_UNSOLICITED;
401 /* Receive (some) data */
402 if ( ( rc = http->state->rx ( http, &iobuf ) ) != 0 )
406 /* Free I/O buffer, if applicable */
409 profile_stop ( &http_rx_profiler );
414 http_close ( http, rc );
419 * Handle server connection close
421 * @v http HTTP transaction
422 * @v rc Reason for close
424 static void http_conn_close ( struct http_transaction *http, int rc ) {
427 assert ( http->state != NULL );
428 assert ( http->state->close != NULL );
430 /* Restart server connection interface */
431 intf_restart ( &http->conn, rc );
433 /* Hand off to state-specific method */
434 http->state->close ( http, rc );
438 * Handle received content-decoded data
440 * @v http HTTP transaction
441 * @v iobuf I/O buffer
442 * @v meta Data transfer metadata
444 static int http_content_deliver ( struct http_transaction *http,
445 struct io_buffer *iobuf,
446 struct xfer_metadata *meta ) {
449 /* Ignore content if this is anything other than a successful
452 if ( http->response.rc != 0 ) {
457 /* Deliver to data transfer interface */
458 profile_start ( &http_xfer_profiler );
459 if ( ( rc = xfer_deliver ( &http->xfer, iob_disown ( iobuf ),
462 profile_stop ( &http_xfer_profiler );
468 * Get underlying data transfer buffer
470 * @v http HTTP transaction
471 * @ret xferbuf Data transfer buffer, or NULL on error
473 static struct xfer_buffer *
474 http_content_buffer ( struct http_transaction *http ) {
476 /* Deny access to the data transfer buffer if this is anything
477 * other than a successful transfer.
479 if ( http->response.rc != 0 )
482 /* Hand off to data transfer interface */
483 return xfer_buffer ( &http->xfer );
487 * Read from block device (when HTTP block device support is not present)
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
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 ) {
506 * Read block device capacity (when HTTP block device support is not present)
508 * @v control Control interface
509 * @v data Data interface
510 * @ret rc Return status code
512 __weak int http_block_read_capacity ( struct http_transaction *http __unused,
513 struct interface *data __unused ) {
519 * Describe device in ACPI table (when HTTP block device support is not present)
521 * @v http HTTP transaction
523 * @v len Length of ACPI table
524 * @ret rc Return status code
526 __weak int http_acpi_describe ( struct http_transaction *http __unused,
527 struct acpi_description_header *acpi __unused,
528 size_t len __unused ) {
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 ),
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 );
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 ),
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 );
562 /** HTTP transfer-decoded interface operations */
563 static struct interface_operation http_transfer_operations[] = {
564 INTF_OP ( intf_close, struct http_transaction *, http_close ),
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 );
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 ),
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 );
585 /** HTTP process descriptor */
586 static struct process_descriptor http_process_desc =
587 PROC_DESC_ONCE ( struct http_transaction, process, http_step );
590 * Open HTTP transaction
592 * @v xfer Data transfer interface
593 * @v method Request method
595 * @v range Content range (if any)
596 * @v content Request content (if any)
597 * @ret rc Return status code
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;
608 char *request_uri_string;
609 char *request_host_string;
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;
618 ( format_uri ( &request_uri, NULL, 0 ) + 1 /* NUL */);
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;
625 ( format_uri ( &request_host, NULL, 0 ) + 1 /* NUL */ );
627 /* Calculate request content length */
628 content_len = ( content ? content->len : 0 );
630 /* Allocate and initialise structure */
631 http = zalloc ( sizeof ( *http ) + request_uri_len + request_host_len +
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;
655 memcpy ( &http->request.range, range,
656 sizeof ( http->request.range ) );
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 );
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 );
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 ) );
675 /* Attach to parent interface, mortalise self, and return */
676 intf_plug_plug ( &http->xfer, xfer );
677 ref_put ( &http->refcnt );
681 http_close ( http, rc );
682 ref_put ( &http->refcnt );
688 * Handle successful transfer completion
690 * @v http HTTP transaction
691 * @ret rc Return status code
693 static int http_transfer_complete ( struct http_transaction *http ) {
694 struct http_authentication *auth;
695 const char *location;
698 /* Keep connection alive if applicable */
699 if ( http->response.flags & HTTP_RESPONSE_KEEPALIVE )
700 pool_recycle ( &http->conn );
702 /* Restart server connection interface */
703 intf_restart ( &http->conn, 0 );
705 /* No more data is expected */
708 /* If transaction is successful, then close the
709 * transfer-decoded interface. The content encoding may
710 * choose whether or not to immediately terminate the
713 if ( http->response.rc == 0 ) {
714 intf_shutdown ( &http->transfer, 0 );
718 /* Perform redirection, if applicable */
719 if ( ( location = http->response.location ) ) {
720 DBGC2 ( http, "HTTP %p redirecting to \"%s\"\n",
722 if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI_STRING,
723 location ) ) != 0 ) {
724 DBGC ( http, "HTTP %p could not redirect: %s\n",
725 http, strerror ( rc ) );
728 http_close ( http, 0 );
732 /* Fail unless a retry is permitted */
733 if ( ! ( http->response.flags & HTTP_RESPONSE_RETRY ) )
734 return http->response.rc;
736 /* Perform authentication, if applicable */
737 if ( ( auth = http->response.auth.auth ) ) {
738 http->request.auth.auth = auth;
739 DBGC2 ( http, "HTTP %p performing %s authentication\n",
741 if ( ( rc = auth->authenticate ( http ) ) != 0 ) {
742 DBGC ( http, "HTTP %p could not authenticate: %s\n",
743 http, strerror ( rc ) );
748 /* Restart content decoding interfaces (which may be attached
749 * to the same object).
751 intf_nullify ( &http->content );
752 intf_nullify ( &http->transfer );
753 intf_restart ( &http->content, http->response.rc );
754 intf_restart ( &http->transfer, http->response.rc );
755 http->content.desc = &http_content_desc;
756 http->transfer.desc = &http_transfer_desc;
757 intf_plug_plug ( &http->transfer, &http->content );
759 assert ( http->remaining == 0 );
761 /* Start timer to initiate retry */
762 DBGC2 ( http, "HTTP %p retrying after %d seconds\n",
763 http, http->response.retry_after );
764 start_timer_fixed ( &http->timer,
765 ( http->response.retry_after * TICKS_PER_SEC ) );
769 /******************************************************************************
773 ******************************************************************************
777 * Construct HTTP request headers
779 * @v http HTTP transaction
781 * @v len Length of buffer
782 * @ret len Length, or negative error
784 static int http_format_headers ( struct http_transaction *http, char *buf,
786 struct http_request_header *header;
793 /* Construct request line */
794 used = ssnprintf ( buf, len, "%s %s HTTP/1.1",
795 http->request.method->name, http->request.uri );
797 DBGC2 ( http, "HTTP %p TX %s\n", http, buf );
798 used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
800 /* Construct all headers */
801 for_each_table_entry ( header, HTTP_REQUEST_HEADERS ) {
803 /* Determine header value length */
804 value_len = header->format ( http, NULL, 0 );
805 if ( value_len < 0 ) {
810 /* Skip zero-length headers */
814 /* Construct header */
815 line = ( buf + used );
816 used += ssnprintf ( ( buf + used ), ( len - used ), "%s: ",
818 remaining = ( ( used < len ) ? ( len - used ) : 0 );
819 used += header->format ( http, ( buf + used ), remaining );
821 DBGC2 ( http, "HTTP %p TX %s\n", http, line );
822 used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
825 /* Construct terminating newline */
826 used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
832 * Construct HTTP "Host" header
834 * @v http HTTP transaction
836 * @v len Length of buffer
837 * @ret len Length of header value, or negative error
839 static int http_format_host ( struct http_transaction *http, char *buf,
842 /* Construct host URI */
843 return snprintf ( buf, len, "%s", http->request.host );
846 /** HTTP "Host" header "*/
847 struct http_request_header http_request_host __http_request_header = {
849 .format = http_format_host,
853 * Construct HTTP "User-Agent" header
855 * @v http HTTP transaction
857 * @v len Length of buffer
858 * @ret len Length of header value, or negative error
860 static int http_format_user_agent ( struct http_transaction *http __unused,
861 char *buf, size_t len ) {
863 /* Construct user agent */
864 return snprintf ( buf, len, "iPXE/%s", product_version );
867 /** HTTP "User-Agent" header */
868 struct http_request_header http_request_user_agent __http_request_header = {
869 .name = "User-Agent",
870 .format = http_format_user_agent,
874 * Construct HTTP "Connection" header
876 * @v http HTTP transaction
878 * @v len Length of buffer
879 * @ret len Length of header value, or negative error
881 static int http_format_connection ( struct http_transaction *http __unused,
882 char *buf, size_t len ) {
884 /* Always request keep-alive */
885 return snprintf ( buf, len, "keep-alive" );
888 /** HTTP "Connection" header */
889 struct http_request_header http_request_connection __http_request_header = {
890 .name = "Connection",
891 .format = http_format_connection,
895 * Construct HTTP "Range" header
897 * @v http HTTP transaction
899 * @v len Length of buffer
900 * @ret len Length of header value, or negative error
902 static int http_format_range ( struct http_transaction *http,
903 char *buf, size_t len ) {
905 /* Construct range, if applicable */
906 if ( http->request.range.len ) {
907 return snprintf ( buf, len, "bytes=%zd-%zd",
908 http->request.range.start,
909 ( http->request.range.start +
910 http->request.range.len - 1 ) );
916 /** HTTP "Range" header */
917 struct http_request_header http_request_range __http_request_header = {
919 .format = http_format_range,
923 * Construct HTTP "Content-Type" header
925 * @v http HTTP transaction
927 * @v len Length of buffer
928 * @ret len Length of header value, or negative error
930 static int http_format_content_type ( struct http_transaction *http,
931 char *buf, size_t len ) {
933 /* Construct content type, if applicable */
934 if ( http->request.content.type ) {
935 return snprintf ( buf, len, "%s", http->request.content.type );
941 /** HTTP "Content-Type" header */
942 struct http_request_header http_request_content_type __http_request_header = {
943 .name = "Content-Type",
944 .format = http_format_content_type,
948 * Construct HTTP "Content-Length" header
950 * @v http HTTP transaction
952 * @v len Length of buffer
953 * @ret len Length of header value, or negative error
955 static int http_format_content_length ( struct http_transaction *http,
956 char *buf, size_t len ) {
958 /* Construct content length, if applicable */
959 if ( http->request.content.len ) {
960 return snprintf ( buf, len, "%zd", http->request.content.len );
966 /** HTTP "Content-Length" header */
967 struct http_request_header http_request_content_length __http_request_header = {
968 .name = "Content-Length",
969 .format = http_format_content_length,
973 * Construct HTTP "Accept-Encoding" header
975 * @v http HTTP transaction
977 * @v len Length of buffer
978 * @ret len Length of header value, or negative error
980 static int http_format_accept_encoding ( struct http_transaction *http,
981 char *buf, size_t len ) {
982 struct http_content_encoding *encoding;
983 const char *sep = "";
986 /* Construct list of content encodings */
987 for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
988 if ( encoding->supported && ( ! encoding->supported ( http ) ) )
990 used += ssnprintf ( ( buf + used ), ( len - used ),
991 "%s%s", sep, encoding->name );
998 /** HTTP "Accept-Encoding" header */
999 struct http_request_header http_request_accept_encoding __http_request_header ={
1000 .name = "Accept-Encoding",
1001 .format = http_format_accept_encoding,
1007 * @v http HTTP transaction
1008 * @ret rc Return status code
1010 static int http_tx_request ( struct http_transaction *http ) {
1011 struct io_buffer *iobuf;
1016 /* Calculate request length */
1017 len = http_format_headers ( http, NULL, 0 );
1020 DBGC ( http, "HTTP %p could not construct request: %s\n",
1021 http, strerror ( rc ) );
1025 /* Allocate I/O buffer */
1026 iobuf = alloc_iob ( len + 1 /* NUL */ + http->request.content.len );
1032 /* Construct request */
1033 check_len = http_format_headers ( http, iob_put ( iobuf, len ),
1034 ( len + 1 /* NUL */ ) );
1035 assert ( check_len == len );
1036 memcpy ( iob_put ( iobuf, http->request.content.len ),
1037 http->request.content.data, http->request.content.len );
1039 /* Deliver request */
1040 if ( ( rc = xfer_deliver_iob ( &http->conn,
1041 iob_disown ( iobuf ) ) ) != 0 ) {
1042 DBGC ( http, "HTTP %p could not deliver request: %s\n",
1043 http, strerror ( rc ) );
1047 /* Clear any previous response */
1048 empty_line_buffer ( &http->response.headers );
1049 memset ( &http->response, 0, sizeof ( http->response ) );
1051 /* Move to response headers state */
1052 http->state = &http_headers;
1063 /** HTTP request state */
1064 static struct http_state http_request = {
1065 .tx = http_tx_request,
1066 .close = http_close_error,
1069 /******************************************************************************
1073 ******************************************************************************
1077 * Parse HTTP status line
1079 * @v http HTTP transaction
1080 * @v line Status line
1081 * @ret rc Return status code
1083 static int http_parse_status ( struct http_transaction *http, char *line ) {
1090 DBGC2 ( http, "HTTP %p RX %s\n", http, line );
1092 /* Parse HTTP version */
1093 version = http_token ( &line, NULL );
1094 if ( ( ! version ) || ( strncmp ( version, "HTTP/", 5 ) != 0 ) ) {
1095 DBGC ( http, "HTTP %p malformed version \"%s\"\n", http, line );
1096 return -EINVAL_STATUS;
1099 /* Keepalive is enabled by default for anything newer than HTTP/1.0 */
1100 vernum = ( version + 5 /* "HTTP/" (presence already checked) */ );
1101 if ( vernum[0] == '0' ) {
1102 /* HTTP/0.x : keepalive not enabled by default */
1103 } else if ( strncmp ( vernum, "1.0", 3 ) == 0 ) {
1104 /* HTTP/1.0 : keepalive not enabled by default */
1106 /* HTTP/1.1 or newer: keepalive enabled by default */
1107 http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
1110 /* Parse status code */
1112 http->response.status = strtoul ( status, &endp, 10 );
1113 if ( *endp != ' ' ) {
1114 DBGC ( http, "HTTP %p malformed status code \"%s\"\n",
1116 return -EINVAL_STATUS;
1119 /* Convert HTTP status code to iPXE return status code */
1120 if ( status[0] == '2' ) {
1123 } else if ( status[0] == '3' ) {
1124 /* 3xx Redirection */
1125 response_rc = -EXDEV;
1126 } else if ( http->response.status == 401 ) {
1127 /* 401 Unauthorized */
1128 response_rc = -EACCES_401;
1129 } else if ( http->response.status == 403 ) {
1131 response_rc = -EPERM_403;
1132 } else if ( http->response.status == 404 ) {
1134 response_rc = -ENOENT_404;
1135 } else if ( status[0] == '4' ) {
1136 /* 4xx Client Error (not already specified) */
1137 response_rc = -EIO_4XX;
1138 } else if ( status[0] == '5' ) {
1139 /* 5xx Server Error */
1140 response_rc = -EIO_5XX;
1143 response_rc = -EIO_OTHER;
1145 http->response.rc = response_rc;
1153 * @v http HTTP transaction
1154 * @v line Header line
1155 * @ret rc Return status code
1157 static int http_parse_header ( struct http_transaction *http, char *line ) {
1158 struct http_response_header *header;
1162 DBGC2 ( http, "HTTP %p RX %s\n", http, line );
1164 /* Extract header name */
1165 sep = strstr ( line, ": " );
1167 DBGC ( http, "HTTP %p malformed header \"%s\"\n", http, line );
1168 return -EINVAL_HEADER;
1171 line = ( sep + 2 /* ": " */ );
1173 /* Process header, if recognised */
1174 for_each_table_entry ( header, HTTP_RESPONSE_HEADERS ) {
1175 if ( strcasecmp ( name, header->name ) == 0 )
1176 return header->parse ( http, line );
1179 /* Unrecognised headers should be ignored */
1184 * Parse HTTP response headers
1186 * @v http HTTP transaction
1187 * @ret rc Return status code
1189 static int http_parse_headers ( struct http_transaction *http ) {
1194 /* Get status line */
1195 line = http->response.headers.data;
1196 assert ( line != NULL );
1197 next = ( line + strlen ( line ) + 1 /* NUL */ );
1199 /* Parse status line */
1200 if ( ( rc = http_parse_status ( http, line ) ) != 0 )
1203 /* Process header lines */
1206 /* Move to next line */
1208 next = ( line + strlen ( line ) + 1 /* NUL */ );
1210 /* Stop on terminating blank line */
1214 /* Process header line */
1215 if ( ( rc = http_parse_header ( http, line ) ) != 0 )
1221 * Parse HTTP "Location" header
1223 * @v http HTTP transaction
1224 * @v line Remaining header line
1225 * @ret rc Return status code
1227 static int http_parse_location ( struct http_transaction *http, char *line ) {
1229 /* Store location */
1230 http->response.location = line;
1234 /** HTTP "Location" header */
1235 struct http_response_header http_response_location __http_response_header = {
1237 .parse = http_parse_location,
1241 * Parse HTTP "Transfer-Encoding" header
1243 * @v http HTTP transaction
1244 * @v line Remaining header line
1245 * @ret rc Return status code
1247 static int http_parse_transfer_encoding ( struct http_transaction *http,
1249 struct http_transfer_encoding *encoding;
1251 /* Check for known transfer encodings */
1252 for_each_table_entry ( encoding, HTTP_TRANSFER_ENCODINGS ) {
1253 if ( strcasecmp ( line, encoding->name ) == 0 ) {
1254 http->response.transfer.encoding = encoding;
1259 DBGC ( http, "HTTP %p unrecognised Transfer-Encoding \"%s\"\n",
1261 return -ENOTSUP_TRANSFER;
1264 /** HTTP "Transfer-Encoding" header */
1265 struct http_response_header
1266 http_response_transfer_encoding __http_response_header = {
1267 .name = "Transfer-Encoding",
1268 .parse = http_parse_transfer_encoding,
1272 * Parse HTTP "Connection" header
1274 * @v http HTTP transaction
1275 * @v line Remaining header line
1276 * @ret rc Return status code
1278 static int http_parse_connection ( struct http_transaction *http, char *line ) {
1280 /* Check for known connection intentions */
1281 if ( strcasecmp ( line, "keep-alive" ) == 0 ) {
1282 http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
1285 if ( strcasecmp ( line, "close" ) == 0 ) {
1286 http->response.flags &= ~HTTP_RESPONSE_KEEPALIVE;
1290 DBGC ( http, "HTTP %p unrecognised Connection \"%s\"\n", http, line );
1291 return -ENOTSUP_CONNECTION;
1294 /** HTTP "Connection" header */
1295 struct http_response_header http_response_connection __http_response_header = {
1296 .name = "Connection",
1297 .parse = http_parse_connection,
1301 * Parse HTTP "Content-Length" header
1303 * @v http HTTP transaction
1304 * @v line Remaining header line
1305 * @ret rc Return status code
1307 static int http_parse_content_length ( struct http_transaction *http,
1312 http->response.content.len = strtoul ( line, &endp, 10 );
1313 if ( *endp != '\0' ) {
1314 DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
1316 return -EINVAL_CONTENT_LENGTH;
1319 /* Record that we have a content length (since it may be zero) */
1320 http->response.flags |= HTTP_RESPONSE_CONTENT_LEN;
1325 /** HTTP "Content-Length" header */
1326 struct http_response_header
1327 http_response_content_length __http_response_header = {
1328 .name = "Content-Length",
1329 .parse = http_parse_content_length,
1333 * Parse HTTP "Content-Encoding" header
1335 * @v http HTTP transaction
1336 * @v line Remaining header line
1337 * @ret rc Return status code
1339 static int http_parse_content_encoding ( struct http_transaction *http,
1341 struct http_content_encoding *encoding;
1343 /* Check for known content encodings */
1344 for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
1345 if ( encoding->supported && ( ! encoding->supported ( http ) ) )
1347 if ( strcasecmp ( line, encoding->name ) == 0 ) {
1348 http->response.content.encoding = encoding;
1353 /* Some servers (e.g. Apache) have a habit of specifying
1354 * unwarranted content encodings. For example, if Apache
1355 * detects (via /etc/httpd/conf/magic) that a file's contents
1356 * are gzip-compressed, it will set "Content-Encoding: x-gzip"
1357 * regardless of the client's Accept-Encoding header. The
1358 * only viable way to handle such servers is to treat unknown
1359 * content encodings as equivalent to "identity".
1361 DBGC ( http, "HTTP %p unrecognised Content-Encoding \"%s\"\n",
1366 /** HTTP "Content-Encoding" header */
1367 struct http_response_header
1368 http_response_content_encoding __http_response_header = {
1369 .name = "Content-Encoding",
1370 .parse = http_parse_content_encoding,
1374 * Parse HTTP "Retry-After" header
1376 * @v http HTTP transaction
1377 * @v line Remaining header line
1378 * @ret rc Return status code
1380 static int http_parse_retry_after ( struct http_transaction *http,
1384 /* Try to parse value as a simple number of seconds */
1385 http->response.retry_after = strtoul ( line, &endp, 10 );
1386 if ( *endp != '\0' ) {
1387 /* For any value which is not a simple number of
1388 * seconds (e.g. a full HTTP date), just retry after a
1389 * fixed delay, since we don't have code able to parse
1392 http->response.retry_after = HTTP_RETRY_SECONDS;
1393 DBGC ( http, "HTTP %p cannot understand Retry-After \"%s\"; "
1394 "using %d seconds\n", http, line, HTTP_RETRY_SECONDS );
1397 /* Allow HTTP request to be retried after specified delay */
1398 http->response.flags |= HTTP_RESPONSE_RETRY;
1403 /** HTTP "Retry-After" header */
1404 struct http_response_header http_response_retry_after __http_response_header = {
1405 .name = "Retry-After",
1406 .parse = http_parse_retry_after,
1410 * Handle received HTTP headers
1412 * @v http HTTP transaction
1413 * @v iobuf I/O buffer (may be claimed)
1414 * @ret rc Return status code
1416 static int http_rx_headers ( struct http_transaction *http,
1417 struct io_buffer **iobuf ) {
1418 struct http_transfer_encoding *transfer;
1419 struct http_content_encoding *content;
1423 /* Buffer header line */
1424 if ( ( rc = http_rx_linebuf ( http, *iobuf,
1425 &http->response.headers ) ) != 0 )
1428 /* Wait until we see the empty line marking end of headers */
1429 line = buffered_line ( &http->response.headers );
1430 if ( ( line == NULL ) || ( line[0] != '\0' ) )
1433 /* Process headers */
1434 if ( ( rc = http_parse_headers ( http ) ) != 0 )
1437 /* Initialise content encoding, if applicable */
1438 if ( ( content = http->response.content.encoding ) &&
1439 ( ( rc = content->init ( http ) ) != 0 ) ) {
1440 DBGC ( http, "HTTP %p could not initialise %s content "
1441 "encoding: %s\n", http, content->name, strerror ( rc ) );
1445 /* Presize receive buffer, if we have a content length */
1446 if ( http->response.content.len ) {
1447 xfer_seek ( &http->transfer, http->response.content.len );
1448 xfer_seek ( &http->transfer, 0 );
1451 /* Complete transfer if this is a HEAD request */
1452 if ( http->request.method == &http_head ) {
1453 if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1458 /* Default to identity transfer encoding, if none specified */
1459 if ( ! http->response.transfer.encoding )
1460 http->response.transfer.encoding = &http_transfer_identity;
1462 /* Move to transfer encoding-specific data state */
1463 transfer = http->response.transfer.encoding;
1464 http->state = &transfer->state;
1466 /* Initialise transfer encoding */
1467 if ( ( rc = transfer->init ( http ) ) != 0 ) {
1468 DBGC ( http, "HTTP %p could not initialise %s transfer "
1469 "encoding: %s\n", http, transfer->name, strerror ( rc ));
1476 /** HTTP response headers state */
1477 static struct http_state http_headers = {
1478 .rx = http_rx_headers,
1479 .close = http_close_error,
1482 /******************************************************************************
1484 * Identity transfer encoding
1486 ******************************************************************************
1490 * Initialise transfer encoding
1492 * @v http HTTP transaction
1493 * @ret rc Return status code
1495 static int http_init_transfer_identity ( struct http_transaction *http ) {
1498 /* Complete transfer immediately if we have a zero content length */
1499 if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1500 ( http->response.content.len == 0 ) &&
1501 ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
1508 * Handle received data
1510 * @v http HTTP transaction
1511 * @v iobuf I/O buffer (may be claimed)
1512 * @ret rc Return status code
1514 static int http_rx_transfer_identity ( struct http_transaction *http,
1515 struct io_buffer **iobuf ) {
1516 size_t len = iob_len ( *iobuf );
1519 /* Update lengths */
1522 /* Fail if this transfer would overrun the expected content
1525 if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1526 ( http->len > http->response.content.len ) ) {
1527 DBGC ( http, "HTTP %p content length overrun\n", http );
1528 return -EIO_CONTENT_LENGTH;
1531 /* Hand off to content encoding */
1532 if ( ( rc = xfer_deliver_iob ( &http->transfer,
1533 iob_disown ( *iobuf ) ) ) != 0 )
1536 /* Complete transfer if we have received the expected content
1539 if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
1540 ( http->len == http->response.content.len ) &&
1541 ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
1548 * Handle server connection close
1550 * @v http HTTP transaction
1551 * @v rc Reason for close
1553 static void http_close_transfer_identity ( struct http_transaction *http,
1556 /* Fail if any error occurred */
1560 /* Fail if we have a content length (since we would have
1561 * already closed the connection if we had received the
1562 * correct content length).
1564 if ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) {
1565 DBGC ( http, "HTTP %p content length underrun\n", http );
1566 rc = EIO_CONTENT_LENGTH;
1570 /* Indicate that transfer is complete */
1571 if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1577 http_close ( http, rc );
1580 /** Identity transfer encoding */
1581 static struct http_transfer_encoding http_transfer_identity = {
1583 .init = http_init_transfer_identity,
1585 .rx = http_rx_transfer_identity,
1586 .close = http_close_transfer_identity,
1590 /******************************************************************************
1592 * Chunked transfer encoding
1594 ******************************************************************************
1598 * Initialise transfer encoding
1600 * @v http HTTP transaction
1601 * @ret rc Return status code
1603 static int http_init_transfer_chunked ( struct http_transaction *http ) {
1606 assert ( http->remaining == 0 );
1607 assert ( http->linebuf.len == 0 );
1613 * Handle received chunk length
1615 * @v http HTTP transaction
1616 * @v iobuf I/O buffer (may be claimed)
1617 * @ret rc Return status code
1619 static int http_rx_chunk_len ( struct http_transaction *http,
1620 struct io_buffer **iobuf ) {
1626 /* Receive into temporary line buffer */
1627 if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
1630 /* Wait until we receive a non-empty line */
1631 line = buffered_line ( &http->linebuf );
1632 if ( ( line == NULL ) || ( line[0] == '\0' ) )
1635 /* Parse chunk length */
1636 http->remaining = strtoul ( line, &endp, 16 );
1637 if ( *endp != '\0' ) {
1638 DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
1640 return -EINVAL_CHUNK_LENGTH;
1643 /* Empty line buffer */
1644 empty_line_buffer ( &http->linebuf );
1646 /* Update expected length */
1647 len = ( http->len + http->remaining );
1648 xfer_seek ( &http->transfer, len );
1649 xfer_seek ( &http->transfer, http->len );
1651 /* If chunk length is zero, then move to response trailers state */
1652 if ( ! http->remaining )
1653 http->state = &http_trailers;
1659 * Handle received chunk data
1661 * @v http HTTP transaction
1662 * @v iobuf I/O buffer (may be claimed)
1663 * @ret rc Return status code
1665 static int http_rx_chunk_data ( struct http_transaction *http,
1666 struct io_buffer **iobuf ) {
1667 struct io_buffer *payload;
1672 /* In the common case of a final chunk in a packet which also
1673 * includes the terminating CRLF, strip the terminating CRLF
1674 * (which we would ignore anyway) and hence avoid
1675 * unnecessarily copying the data.
1677 if ( iob_len ( *iobuf ) == ( http->remaining + 2 /* CRLF */ ) ) {
1678 crlf = ( (*iobuf)->data + http->remaining );
1679 if ( ( crlf[0] == '\r' ) && ( crlf[1] == '\n' ) )
1680 iob_unput ( (*iobuf), 2 /* CRLF */ );
1682 len = iob_len ( *iobuf );
1684 /* Use whole/partial buffer as applicable */
1685 if ( len <= http->remaining ) {
1687 /* Whole buffer is to be consumed: decrease remaining
1688 * length and use original I/O buffer as payload.
1690 payload = iob_disown ( *iobuf );
1692 http->remaining -= len;
1696 /* Partial buffer is to be consumed: copy data to a
1697 * temporary I/O buffer.
1699 payload = alloc_iob ( http->remaining );
1704 memcpy ( iob_put ( payload, http->remaining ), (*iobuf)->data,
1706 iob_pull ( *iobuf, http->remaining );
1707 http->len += http->remaining;
1708 http->remaining = 0;
1711 /* Hand off to content encoding */
1712 if ( ( rc = xfer_deliver_iob ( &http->transfer,
1713 iob_disown ( payload ) ) ) != 0 )
1719 assert ( payload == NULL );
1724 * Handle received chunked data
1726 * @v http HTTP transaction
1727 * @v iobuf I/O buffer (may be claimed)
1728 * @ret rc Return status code
1730 static int http_rx_transfer_chunked ( struct http_transaction *http,
1731 struct io_buffer **iobuf ) {
1733 /* Handle as chunk length or chunk data as appropriate */
1734 if ( http->remaining ) {
1735 return http_rx_chunk_data ( http, iobuf );
1737 return http_rx_chunk_len ( http, iobuf );
1741 /** Chunked transfer encoding */
1742 struct http_transfer_encoding http_transfer_chunked __http_transfer_encoding = {
1744 .init = http_init_transfer_chunked,
1746 .rx = http_rx_transfer_chunked,
1747 .close = http_close_error,
1751 /******************************************************************************
1755 ******************************************************************************
1759 * Handle received HTTP trailer
1761 * @v http HTTP transaction
1762 * @v iobuf I/O buffer (may be claimed)
1763 * @ret rc Return status code
1765 static int http_rx_trailers ( struct http_transaction *http,
1766 struct io_buffer **iobuf ) {
1770 /* Buffer trailer line */
1771 if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
1774 /* Wait until we see the empty line marking end of trailers */
1775 line = buffered_line ( &http->linebuf );
1776 if ( ( line == NULL ) || ( line[0] != '\0' ) )
1779 /* Empty line buffer */
1780 empty_line_buffer ( &http->linebuf );
1782 /* Transfer is complete */
1783 if ( ( rc = http_transfer_complete ( http ) ) != 0 )
1789 /** HTTP response trailers state */
1790 static struct http_state http_trailers = {
1791 .rx = http_rx_trailers,
1792 .close = http_close_error,
1795 /******************************************************************************
1797 * Simple URI openers
1799 ******************************************************************************
1803 * Construct HTTP parameter list
1805 * @v params Parameter list
1806 * @v buf Buffer to contain HTTP POST parameters
1807 * @v len Length of buffer
1808 * @ret len Length of parameter list (excluding terminating NUL)
1810 static size_t http_params ( struct parameters *params, char *buf, size_t len ) {
1811 struct parameter *param;
1812 ssize_t remaining = len;
1815 /* Add each parameter in the form "key=value", joined with "&" */
1817 for_each_param ( param, params ) {
1819 /* Add the "&", if applicable */
1821 if ( remaining > 0 )
1828 /* URI-encode the key */
1829 frag_len = uri_encode ( param->key, 0, buf, remaining );
1832 remaining -= frag_len;
1835 if ( remaining > 0 )
1841 /* URI-encode the value */
1842 frag_len = uri_encode ( param->value, 0, buf, remaining );
1845 remaining -= frag_len;
1848 /* Ensure string is NUL-terminated even if no parameters are present */
1849 if ( remaining > 0 )
1856 * Open HTTP transaction for simple GET URI
1858 * @v xfer Data transfer interface
1859 * @v uri Request URI
1860 * @ret rc Return status code
1862 static int http_open_get_uri ( struct interface *xfer, struct uri *uri ) {
1864 return http_open ( xfer, &http_get, uri, NULL, NULL );
1868 * Open HTTP transaction for simple POST URI
1870 * @v xfer Data transfer interface
1871 * @v uri Request URI
1872 * @ret rc Return status code
1874 static int http_open_post_uri ( struct interface *xfer, struct uri *uri ) {
1875 struct parameters *params = uri->params;
1876 struct http_request_content content;
1882 /* Calculate length of parameter list */
1883 len = http_params ( params, NULL, 0 );
1885 /* Allocate temporary parameter list */
1886 data = zalloc ( len + 1 /* NUL */ );
1892 /* Construct temporary parameter list */
1893 check_len = http_params ( params, data, ( len + 1 /* NUL */ ) );
1894 assert ( check_len == len );
1896 /* Construct request content */
1897 content.type = "application/x-www-form-urlencoded";
1898 content.data = data;
1901 /* Open HTTP transaction */
1902 if ( ( rc = http_open ( xfer, &http_post, uri, NULL, &content ) ) != 0 )
1912 * Open HTTP transaction for simple URI
1914 * @v xfer Data transfer interface
1915 * @v uri Request URI
1916 * @ret rc Return status code
1918 int http_open_uri ( struct interface *xfer, struct uri *uri ) {
1920 /* Open GET/POST URI as applicable */
1921 if ( uri->params ) {
1922 return http_open_post_uri ( xfer, uri );
1924 return http_open_get_uri ( xfer, uri );
1928 /* Drag in HTTP extensions */
1929 REQUIRING_SYMBOL ( http_open );
1930 REQUIRE_OBJECT ( config_http );