iPXE
Defines | Functions | Variables
httpcore.c File Reference

Hyper Text Transfer Protocol (HTTP) core functionality. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <byteswap.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <ipxe/uri.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/process.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/linebuf.h>
#include <ipxe/xferbuf.h>
#include <ipxe/blockdev.h>
#include <ipxe/acpi.h>
#include <ipxe/version.h>
#include <ipxe/params.h>
#include <ipxe/profile.h>
#include <ipxe/vsprintf.h>
#include <ipxe/errortab.h>
#include <ipxe/http.h>

Go to the source code of this file.

Defines

#define EACCES_401   __einfo_error ( EINFO_EACCES_401 )
#define EINFO_EACCES_401   __einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )
#define EINVAL_STATUS   __einfo_error ( EINFO_EINVAL_STATUS )
#define EINFO_EINVAL_STATUS   __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid status line" )
#define EINVAL_HEADER   __einfo_error ( EINFO_EINVAL_HEADER )
#define EINFO_EINVAL_HEADER   __einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid header" )
#define EINVAL_CONTENT_LENGTH   __einfo_error ( EINFO_EINVAL_CONTENT_LENGTH )
#define EINFO_EINVAL_CONTENT_LENGTH   __einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid content length" )
#define EINVAL_CHUNK_LENGTH   __einfo_error ( EINFO_EINVAL_CHUNK_LENGTH )
#define EINFO_EINVAL_CHUNK_LENGTH   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid chunk length" )
#define EIO_OTHER   __einfo_error ( EINFO_EIO_OTHER )
#define EINFO_EIO_OTHER   __einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )
#define EIO_CONTENT_LENGTH   __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
#define EINFO_EIO_CONTENT_LENGTH   __einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
#define EIO_4XX   __einfo_error ( EINFO_EIO_4XX )
#define EINFO_EIO_4XX   __einfo_uniqify ( EINFO_EIO, 0x04, "HTTP 4xx Client Error" )
#define EIO_5XX   __einfo_error ( EINFO_EIO_5XX )
#define EINFO_EIO_5XX   __einfo_uniqify ( EINFO_EIO, 0x05, "HTTP 5xx Server Error" )
#define ENOENT_404   __einfo_error ( EINFO_ENOENT_404 )
#define EINFO_ENOENT_404   __einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )
#define ENOTSUP_CONNECTION   __einfo_error ( EINFO_ENOTSUP_CONNECTION )
#define EINFO_ENOTSUP_CONNECTION   __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported connection header" )
#define ENOTSUP_TRANSFER   __einfo_error ( EINFO_ENOTSUP_TRANSFER )
#define EINFO_ENOTSUP_TRANSFER   __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported transfer encoding" )
#define EPERM_403   __einfo_error ( EINFO_EPERM_403 )
#define EINFO_EPERM_403   __einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )
#define EPROTO_UNSOLICITED   __einfo_error ( EINFO_EPROTO_UNSOLICITED )
#define EINFO_EPROTO_UNSOLICITED   __einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )
#define HTTP_RETRY_SECONDS   5
 Retry delay used when we cannot understand the Retry-After header.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static int http_rx_linebuf (struct http_transaction *http, struct io_buffer *iobuf, struct line_buffer *linebuf)
 Handle received HTTP line-buffered data.
char * http_token (char **line, char **value)
 Get HTTP response token.
static void http_free (struct refcnt *refcnt)
 Free HTTP transaction.
static void http_close (struct http_transaction *http, int rc)
 Close HTTP transaction.
static void http_close_error (struct http_transaction *http, int rc)
 Close HTTP transaction with error (even if none specified)
static void http_reopen (struct http_transaction *http)
 Reopen stale HTTP connection.
static void http_expired (struct retry_timer *timer, int over __unused)
 Handle retry timer expiry.
static void http_step (struct http_transaction *http)
 HTTP transmit process.
static int http_conn_deliver (struct http_transaction *http, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
 Handle received HTTP data.
static void http_conn_close (struct http_transaction *http, int rc)
 Handle server connection close.
static int http_content_deliver (struct http_transaction *http, struct io_buffer *iobuf, struct xfer_metadata *meta)
 Handle received content-decoded data.
static struct xfer_bufferhttp_content_buffer (struct http_transaction *http)
 Get underlying data transfer buffer.
__weak int http_block_read (struct http_transaction *http __unused, struct interface *data __unused, uint64_t lba __unused, unsigned int count __unused, userptr_t buffer __unused, size_t len __unused)
 Read from block device (when HTTP block device support is not present)
__weak int http_block_read_capacity (struct http_transaction *http __unused, struct interface *data __unused)
 Read block device capacity (when HTTP block device support is not present)
int http_open (struct interface *xfer, struct http_method *method, struct uri *uri, struct http_request_range *range, struct http_request_content *content)
 Open HTTP transaction.
static int http_redirect (struct http_transaction *http, const char *location)
 Redirect HTTP transaction.
static int http_transfer_complete (struct http_transaction *http)
 Handle successful transfer completion.
static int http_format_headers (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP request headers.
static int http_format_host (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Host" header.
static int http_format_user_agent (struct http_transaction *http __unused, char *buf, size_t len)
 Construct HTTP "User-Agent" header.
static int http_format_connection (struct http_transaction *http __unused, char *buf, size_t len)
 Construct HTTP "Connection" header.
static int http_format_range (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Range" header.
static int http_format_content_type (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Content-Type" header.
static int http_format_content_length (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Content-Length" header.
static int http_format_accept_encoding (struct http_transaction *http, char *buf, size_t len)
 Construct HTTP "Accept-Encoding" header.
static int http_tx_request (struct http_transaction *http)
 Transmit request.
static int http_parse_status (struct http_transaction *http, char *line)
 Parse HTTP status line.
static int http_parse_header (struct http_transaction *http, char *line)
 Parse HTTP header.
static int http_parse_headers (struct http_transaction *http)
 Parse HTTP response headers.
static int http_parse_location (struct http_transaction *http, char *line)
 Parse HTTP "Location" header.
static int http_parse_transfer_encoding (struct http_transaction *http, char *line)
 Parse HTTP "Transfer-Encoding" header.
static int http_parse_connection (struct http_transaction *http, char *line)
 Parse HTTP "Connection" header.
static int http_parse_content_length (struct http_transaction *http, char *line)
 Parse HTTP "Content-Length" header.
static int http_parse_content_encoding (struct http_transaction *http, char *line)
 Parse HTTP "Content-Encoding" header.
static int http_parse_retry_after (struct http_transaction *http, char *line)
 Parse HTTP "Retry-After" header.
static int http_rx_headers (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received HTTP headers.
static int http_init_transfer_identity (struct http_transaction *http)
 Initialise transfer encoding.
static int http_rx_transfer_identity (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received data.
static void http_close_transfer_identity (struct http_transaction *http, int rc)
 Handle server connection close.
static int http_init_transfer_chunked (struct http_transaction *http)
 Initialise transfer encoding.
static int http_rx_chunk_len (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received chunk length.
static int http_rx_chunk_data (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received chunk data.
static int http_rx_transfer_chunked (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received chunked data.
static int http_rx_trailers (struct http_transaction *http, struct io_buffer **iobuf)
 Handle received HTTP trailer.
static size_t http_params (struct parameters *params, char *buf, size_t len)
 Construct HTTP parameter list.
static int http_open_get_uri (struct interface *xfer, struct uri *uri)
 Open HTTP transaction for simple GET URI.
static int http_open_post_uri (struct interface *xfer, struct uri *uri)
 Open HTTP transaction for simple POST URI.
int http_open_uri (struct interface *xfer, struct uri *uri)
 Open HTTP transaction for simple URI.
 REQUIRING_SYMBOL (http_open)
 REQUIRE_OBJECT (config_http)

Variables

static struct profiler
http_rx_profiler 
__profiler = { .name = "http.rx" }
 Receive profiler.
struct errortab http_errors[] __errortab
 Human-readable error messages.
static struct http_state http_request
 HTTP request state.
static struct http_state http_headers
 HTTP response headers state.
static struct http_state http_trailers
 HTTP response trailers state.
static struct
http_transfer_encoding 
http_transfer_identity
 Identity transfer encoding.
struct http_method http_head
 HTTP HEAD method.
struct http_method http_get
 HTTP GET method.
struct http_method http_post
 HTTP POST method.
static struct interface_operation http_xfer_operations []
 HTTP data transfer interface operations.
static struct interface_descriptor http_xfer_desc
 HTTP data transfer interface descriptor.
static struct interface_operation http_content_operations []
 HTTP content-decoded interface operations.
static struct interface_descriptor http_content_desc
 HTTP content-decoded interface descriptor.
static struct interface_operation http_transfer_operations []
 HTTP transfer-decoded interface operations.
static struct interface_descriptor http_transfer_desc
 HTTP transfer-decoded interface descriptor.
static struct interface_operation http_conn_operations []
 HTTP server connection interface operations.
static struct interface_descriptor http_conn_desc
 HTTP server connection interface descriptor.
static struct process_descriptor http_process_desc
 HTTP process descriptor.
struct http_request_header
http_request_host 
__http_request_header
 HTTP "Host" header ".
struct http_response_header
http_response_location 
__http_response_header
 HTTP "Location" header.
struct http_transfer_encoding
http_transfer_chunked 
__http_transfer_encoding
 Chunked transfer encoding.

Detailed Description

Hyper Text Transfer Protocol (HTTP) core functionality.

Definition in file httpcore.c.


Define Documentation

Definition at line 62 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EACCES_401   __einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )

Definition at line 63 of file httpcore.c.

Definition at line 65 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EINVAL_STATUS   __einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid status line" )

Definition at line 66 of file httpcore.c.

Definition at line 68 of file httpcore.c.

Referenced by http_parse_header().

#define EINFO_EINVAL_HEADER   __einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid header" )

Definition at line 69 of file httpcore.c.

Definition at line 71 of file httpcore.c.

Referenced by http_parse_content_length().

#define EINFO_EINVAL_CONTENT_LENGTH   __einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid content length" )

Definition at line 72 of file httpcore.c.

Definition at line 74 of file httpcore.c.

Referenced by http_rx_chunk_len().

#define EINFO_EINVAL_CHUNK_LENGTH   __einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid chunk length" )

Definition at line 75 of file httpcore.c.

Definition at line 77 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EIO_OTHER   __einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )

Definition at line 78 of file httpcore.c.

Definition at line 80 of file httpcore.c.

Referenced by http_close_transfer_identity(), and http_rx_transfer_identity().

#define EINFO_EIO_CONTENT_LENGTH   __einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )

Definition at line 81 of file httpcore.c.

Definition at line 83 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EIO_4XX   __einfo_uniqify ( EINFO_EIO, 0x04, "HTTP 4xx Client Error" )

Definition at line 84 of file httpcore.c.

Definition at line 86 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EIO_5XX   __einfo_uniqify ( EINFO_EIO, 0x05, "HTTP 5xx Server Error" )

Definition at line 87 of file httpcore.c.

Definition at line 89 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_ENOENT_404   __einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )

Definition at line 90 of file httpcore.c.

Definition at line 92 of file httpcore.c.

#define EINFO_ENOTSUP_CONNECTION   __einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported connection header" )

Definition at line 93 of file httpcore.c.

Definition at line 95 of file httpcore.c.

Referenced by http_parse_transfer_encoding().

#define EINFO_ENOTSUP_TRANSFER   __einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported transfer encoding" )

Definition at line 96 of file httpcore.c.

Definition at line 98 of file httpcore.c.

Referenced by http_parse_status().

#define EINFO_EPERM_403   __einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )

Definition at line 99 of file httpcore.c.

Definition at line 101 of file httpcore.c.

Referenced by http_conn_deliver().

#define EINFO_EPROTO_UNSOLICITED   __einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )

Definition at line 102 of file httpcore.c.

#define HTTP_RETRY_SECONDS   5

Retry delay used when we cannot understand the Retry-After header.

Definition at line 106 of file httpcore.c.

Referenced by http_parse_retry_after().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
static int http_rx_linebuf ( struct http_transaction http,
struct io_buffer iobuf,
struct line_buffer linebuf 
) [static]

Handle received HTTP line-buffered data.

Parameters:
httpHTTP transaction
iobufI/O buffer
linebufLine buffer
Return values:
rcReturn status code

Definition at line 162 of file httpcore.c.

References io_buffer::data, DBGC, iob_len(), iob_pull, line_buffer(), rc, and strerror().

Referenced by http_rx_chunk_len(), http_rx_headers(), and http_rx_trailers().

                                                           {
        int consumed;
        int rc;

        /* Buffer received line */
        consumed = line_buffer ( linebuf, iobuf->data, iob_len ( iobuf ) );
        if ( consumed < 0 ) {
                rc = consumed;
                DBGC ( http, "HTTP %p could not buffer line: %s\n",
                       http, strerror ( rc ) );
                return rc;
        }

        /* Consume line */
        iob_pull ( iobuf, consumed );

        return 0;
}
char* http_token ( char **  line,
char **  value 
)

Get HTTP response token.

Parameters:
lineLine position
valueToken value to fill in (if any)
Return values:
tokenToken, or NULL

Definition at line 190 of file httpcore.c.

References isspace(), NULL, and token.

Referenced by http_parse_connection(), http_parse_digest_auth(), http_parse_status(), and http_parse_www_authenticate().

                                                {
        char *token;
        char quote = '\0';
        char c;

        /* Avoid returning uninitialised data */
        if ( value )
                *value = NULL;

        /* Skip any initial whitespace or commas */
        while ( ( isspace ( **line ) ) || ( **line == ',' ) )
                (*line)++;

        /* Check for end of line and record token position */
        if ( ! **line )
                return NULL;
        token = *line;

        /* Scan for end of token */
        while ( ( c = **line ) ) {

                /* Terminate if we hit an unquoted whitespace or comma */
                if ( ( isspace ( c ) || ( c == ',' ) ) && ! quote )
                        break;

                /* Terminate if we hit a closing quote */
                if ( c == quote )
                        break;

                /* Check for value separator */
                if ( value && ( ! *value ) && ( c == '=' ) ) {

                        /* Terminate key portion of token */
                        *((*line)++) = '\0';

                        /* Check for quote character */
                        c = **line;
                        if ( ( c == '"' ) || ( c == '\'' ) ) {
                                quote = c;
                                (*line)++;
                        }

                        /* Record value portion of token */
                        *value = *line;

                } else {

                        /* Move to next character */
                        (*line)++;
                }
        }

        /* Terminate token, if applicable */
        if ( c )
                *((*line)++) = '\0';

        return token;
}
static void http_free ( struct refcnt refcnt) [static]

Free HTTP transaction.

Parameters:
refcntReference count

Definition at line 261 of file httpcore.c.

References container_of, empty_line_buffer(), free, http_response::headers, http_transaction::linebuf, http_transaction::response, http_transaction::uri, and uri_put().

Referenced by http_open().

                                                {
        struct http_transaction *http =
                container_of ( refcnt, struct http_transaction, refcnt );

        empty_line_buffer ( &http->response.headers );
        empty_line_buffer ( &http->linebuf );
        uri_put ( http->uri );
        free ( http );
}
static void http_close ( struct http_transaction http,
int  rc 
) [static]

Close HTTP transaction.

Parameters:
httpHTTP transaction
rcReason for close

Definition at line 277 of file httpcore.c.

References http_transaction::conn, http_transaction::content, intfs_shutdown(), NULL, http_transaction::process, process_del(), stop_timer(), http_transaction::timer, http_transaction::transfer, and http_transaction::xfer.

Referenced by http_close_error(), http_close_transfer_identity(), http_conn_deliver(), http_open(), http_reopen(), http_step(), and http_transfer_complete().

                                                                 {

        /* Stop process */
        process_del ( &http->process );

        /* Stop timer */
        stop_timer ( &http->timer );

        /* Close all interfaces */
        intfs_shutdown ( rc, &http->conn, &http->transfer, &http->content,
                         &http->xfer, NULL );
}
static void http_close_error ( struct http_transaction http,
int  rc 
) [static]

Close HTTP transaction with error (even if none specified)

Parameters:
httpHTTP transaction
rcReason for close

Definition at line 296 of file httpcore.c.

References EPIPE, and http_close().

                                                                       {

        /* Treat any close as an error */
        http_close ( http, ( rc ? rc : -EPIPE ) );
}
static void http_reopen ( struct http_transaction http) [static]

Reopen stale HTTP connection.

Parameters:
httpHTTP transaction

Definition at line 307 of file httpcore.c.

References http_transaction::conn, DBGC, ECANCELED, http_close(), http_connect(), http_request, intf_restart(), http_transaction::process, process_add(), rc, http_transaction::state, strerror(), and http_transaction::uri.

Referenced by http_expired(), and http_transfer_complete().

                                                          {
        int rc;

        /* Close existing connection */
        intf_restart ( &http->conn, -ECANCELED );

        /* Reopen connection */
        if ( ( rc = http_connect ( &http->conn, http->uri ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not reconnect: %s\n",
                       http, strerror ( rc ) );
                goto err_connect;
        }

        /* Reset state */
        http->state = &http_request;

        /* Reschedule transmission process */
        process_add ( &http->process );

        return;

 err_connect:
        http_close ( http, rc );
}
static void http_expired ( struct retry_timer timer,
int over  __unused 
) [static]

Handle retry timer expiry.

Parameters:
timerRetry timer
overFailure indicator

Definition at line 338 of file httpcore.c.

References container_of, and http_reopen().

Referenced by http_open().

                                                                          {
        struct http_transaction *http =
                container_of ( timer, struct http_transaction, timer );

        /* Reopen connection */
        http_reopen ( http );
}
static void http_step ( struct http_transaction http) [static]

HTTP transmit process.

Parameters:
httpHTTP transaction

Definition at line 351 of file httpcore.c.

References http_transaction::conn, http_close(), rc, http_transaction::state, http_state::tx, http_transaction::xfer, xfer_window(), and xfer_window_changed().

                                                        {
        int rc;

        /* Do nothing if we have nothing to transmit */
        if ( ! http->state->tx )
                return;

        /* Do nothing until connection is ready */
        if ( ! xfer_window ( &http->conn ) )
                return;

        /* Notify data transfer interface that window may have changed */
        xfer_window_changed ( &http->xfer );

        /* Do nothing until data transfer interface is ready */
        if ( ! xfer_window ( &http->xfer ) )
                return;

        /* Transmit data */
        if ( ( rc = http->state->tx ( http ) ) != 0 )
                goto err;

        return;

 err:
        http_close ( http, rc );
}
static int http_conn_deliver ( struct http_transaction http,
struct io_buffer iobuf,
struct xfer_metadata *meta  __unused 
) [static]

Handle received HTTP data.

Parameters:
httpHTTP transaction
iobufI/O buffer
metaTransfer metadata
Return values:
rcReturn status code

This function takes ownership of the I/O buffer.

Definition at line 389 of file httpcore.c.

References DBGC, EPROTO_UNSOLICITED, free_iob(), http_close(), iob_len(), profile_start(), profile_stop(), rc, http_state::rx, and http_transaction::state.

                                                                     {
        int rc;

        /* Handle received data */
        profile_start ( &http_rx_profiler );
        while ( iobuf && iob_len ( iobuf ) ) {

                /* Sanity check */
                if ( ( ! http->state ) || ( ! http->state->rx ) ) {
                        DBGC ( http, "HTTP %p unexpected data\n", http );
                        rc = -EPROTO_UNSOLICITED;
                        goto err;
                }

                /* Receive (some) data */
                if ( ( rc = http->state->rx ( http, &iobuf ) ) != 0 )
                        goto err;
        }

        /* Free I/O buffer, if applicable */
        free_iob ( iobuf );

        profile_stop ( &http_rx_profiler );
        return 0;

 err:
        free_iob ( iobuf );
        http_close ( http, rc );
        return rc;
}
static void http_conn_close ( struct http_transaction http,
int  rc 
) [static]

Handle server connection close.

Parameters:
httpHTTP transaction
rcReason for close

Definition at line 428 of file httpcore.c.

References assert, http_state::close, http_transaction::conn, intf_restart(), NULL, and http_transaction::state.

                                                                      {

        /* Sanity checks */
        assert ( http->state != NULL );
        assert ( http->state->close != NULL );

        /* Restart server connection interface */
        intf_restart ( &http->conn, rc );

        /* Hand off to state-specific method */
        http->state->close ( http, rc );
}
static int http_content_deliver ( struct http_transaction http,
struct io_buffer iobuf,
struct xfer_metadata meta 
) [static]

Handle received content-decoded data.

Parameters:
httpHTTP transaction
iobufI/O buffer
metaData transfer metadata

Definition at line 448 of file httpcore.c.

References free_iob(), iob_disown, profile_start(), profile_stop(), http_response::rc, rc, http_transaction::response, http_transaction::xfer, and xfer_deliver().

                                                               {
        int rc;

        /* Ignore content if this is anything other than a successful
         * transfer.
         */
        if ( http->response.rc != 0 ) {
                free_iob ( iobuf );
                return 0;
        }

        /* Deliver to data transfer interface */
        profile_start ( &http_xfer_profiler );
        if ( ( rc = xfer_deliver ( &http->xfer, iob_disown ( iobuf ),
                                   meta ) ) != 0 )
                return rc;
        profile_stop ( &http_xfer_profiler );

        return 0;
}
static struct xfer_buffer* http_content_buffer ( struct http_transaction http) [static, read]

Get underlying data transfer buffer.

Parameters:
httpHTTP transaction
Return values:
xferbufData transfer buffer, or NULL on error

Definition at line 478 of file httpcore.c.

References NULL, http_response::rc, http_transaction::response, http_transaction::xfer, and xfer_buffer().

                                                      {

        /* Deny access to the data transfer buffer if this is anything
         * other than a successful transfer.
         */
        if ( http->response.rc != 0 )
                return NULL;

        /* Hand off to data transfer interface */
        return xfer_buffer ( &http->xfer );
}
__weak int http_block_read ( struct http_transaction *http  __unused,
struct interface *data  __unused,
uint64_t lba  __unused,
unsigned int count  __unused,
userptr_t buffer  __unused,
size_t len  __unused 
)

Read from block device (when HTTP block device support is not present)

Parameters:
httpHTTP transaction
dataData interface
lbaStarting logical block address
countNumber of logical blocks
bufferData buffer
lenLength of data buffer
Return values:
rcReturn status code

Definition at line 501 of file httpcore.c.

References ENOTSUP.

                                                                              {

        return -ENOTSUP;
}
__weak int http_block_read_capacity ( struct http_transaction *http  __unused,
struct interface *data  __unused 
)

Read block device capacity (when HTTP block device support is not present)

Parameters:
controlControl interface
dataData interface
Return values:
rcReturn status code

Definition at line 516 of file httpcore.c.

References ENOTSUP.

                                                                        {

        return -ENOTSUP;
}
int http_open ( struct interface xfer,
struct http_method method,
struct uri uri,
struct http_request_range range,
struct http_request_content content 
)

Open HTTP transaction.

Parameters:
xferData transfer interface
methodRequest method
uriRequest URI
rangeContent range (if any)
contentRequest content (if any)
Return values:
rcReturn status code

Definition at line 586 of file httpcore.c.

References http_transaction::conn, http_request::content, http_transaction::content, http_request_content::data, DBGC, DBGC2, ENOMEM, format_uri(), uri::host, http_request::host, http_close(), http_connect(), http_expired(), http_free(), http_request, intf_init(), intf_plug_plug(), http_request_content::len, memcpy(), memset(), http_request::method, method, NULL, uri::path, uri::port, http_transaction::process, process_init(), uri::query, http_request::range, rc, ref_init, ref_put, http_transaction::refcnt, http_transaction::request, uri::scheme, http_transaction::state, strerror(), http_transaction::timer, http_transaction::transfer, http_request_content::type, http_request::uri, http_transaction::uri, uri_get(), http_transaction::xfer, and zalloc().

Referenced by http_block_read(), http_block_read_capacity(), http_open_get_uri(), http_open_post_uri(), peerblk_raw_open(), and peerblk_retrieval_open().

                                                       {
        struct http_transaction *http;
        struct uri request_uri;
        struct uri request_host;
        size_t request_uri_len;
        size_t request_host_len;
        size_t content_len;
        char *request_uri_string;
        char *request_host_string;
        void *content_data;
        int rc;

        /* Calculate request URI length */
        memset ( &request_uri, 0, sizeof ( request_uri ) );
        request_uri.path = ( uri->path ? uri->path : "/" );
        request_uri.query = uri->query;
        request_uri_len =
                ( format_uri ( &request_uri, NULL, 0 ) + 1 /* NUL */);

        /* Calculate host name length */
        memset ( &request_host, 0, sizeof ( request_host ) );
        request_host.host = uri->host;
        request_host.port = uri->port;
        request_host_len =
                ( format_uri ( &request_host, NULL, 0 ) + 1 /* NUL */ );

        /* Calculate request content length */
        content_len = ( content ? content->len : 0 );

        /* Allocate and initialise structure */
        http = zalloc ( sizeof ( *http ) + request_uri_len + request_host_len +
                        content_len );
        if ( ! http ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        request_uri_string = ( ( ( void * ) http ) + sizeof ( *http ) );
        request_host_string = ( request_uri_string + request_uri_len );
        content_data = ( request_host_string + request_host_len );
        format_uri ( &request_uri, request_uri_string, request_uri_len );
        format_uri ( &request_host, request_host_string, request_host_len );
        ref_init ( &http->refcnt, http_free );
        intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
        intf_init ( &http->content, &http_content_desc, &http->refcnt );
        intf_init ( &http->transfer, &http_transfer_desc, &http->refcnt );
        intf_init ( &http->conn, &http_conn_desc, &http->refcnt );
        intf_plug_plug ( &http->transfer, &http->content );
        process_init ( &http->process, &http_process_desc, &http->refcnt );
        timer_init ( &http->timer, http_expired, &http->refcnt );
        http->uri = uri_get ( uri );
        http->request.method = method;
        http->request.uri = request_uri_string;
        http->request.host = request_host_string;
        if ( range ) {
                memcpy ( &http->request.range, range,
                         sizeof ( http->request.range ) );
        }
        if ( content ) {
                http->request.content.type = content->type;
                http->request.content.data = content_data;
                http->request.content.len = content_len;
                memcpy ( content_data, content->data, content_len );
        }
        http->state = &http_request;
        DBGC2 ( http, "HTTP %p %s://%s%s\n", http, http->uri->scheme,
                http->request.host, http->request.uri );

        /* Open connection */
        if ( ( rc = http_connect ( &http->conn, uri ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not connect: %s\n",
                       http, strerror ( rc ) );
                goto err_connect;
        }

        /* Attach to parent interface, mortalise self, and return */
        intf_plug_plug ( &http->xfer, xfer );
        ref_put ( &http->refcnt );
        return 0;

 err_connect:
        http_close ( http, rc );
        ref_put ( &http->refcnt );
 err_alloc:
        return rc;
}
static int http_redirect ( struct http_transaction http,
const char *  location 
) [static]

Redirect HTTP transaction.

Parameters:
httpHTTP transaction
locationNew location
Return values:
rcReturn status code

Definition at line 681 of file httpcore.c.

References DBGC, DBGC2, ENOMEM, LOCATION_URI, parse_uri(), rc, resolve_uri(), strerror(), http_transaction::uri, uri_put(), http_transaction::xfer, and xfer_redirect().

Referenced by http_transfer_complete().

                                                  {
        struct uri *location_uri;
        struct uri *resolved_uri;
        int rc;

        DBGC2 ( http, "HTTP %p redirecting to \"%s\"\n", http, location );

        /* Parse location URI */
        location_uri = parse_uri ( location );
        if ( ! location_uri ) {
                rc = -ENOMEM;
                goto err_parse_uri;
        }

        /* Resolve as relative to original URI */
        resolved_uri = resolve_uri ( http->uri, location_uri );
        if ( ! resolved_uri ) {
                rc = -ENOMEM;
                goto err_resolve_uri;
        }

        /* Redirect to new URI */
        if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI,
                                    resolved_uri ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not redirect: %s\n",
                       http, strerror ( rc ) );
                goto err_redirect;
        }

 err_redirect:
        uri_put ( resolved_uri );
 err_resolve_uri:
        uri_put ( location_uri );
 err_parse_uri:
        return rc;
}
static int http_transfer_complete ( struct http_transaction http) [static]

Handle successful transfer completion.

Parameters:
httpHTTP transaction
Return values:
rcReturn status code

Definition at line 725 of file httpcore.c.

References assert, http_request_auth::auth, http_request::auth, http_response_auth::auth, http_response::auth, http_authentication::authenticate, http_transaction::conn, http_transaction::content, DBGC, DBGC2, http_response::flags, http_close(), http_redirect(), http_reopen(), HTTP_RESPONSE_KEEPALIVE, HTTP_RESPONSE_RETRY, intf_plug_plug(), intf_restart(), intf_shutdown(), intfs_restart(), http_transaction::len, http_response::location, http_authentication::name, NULL, pool_recycle(), http_response::rc, rc, http_transaction::remaining, http_transaction::request, http_transaction::response, http_response::retry_after, start_timer_fixed(), http_transaction::state, strerror(), TICKS_PER_SEC, http_transaction::timer, and http_transaction::transfer.

Referenced by http_close_transfer_identity(), http_init_transfer_identity(), http_rx_headers(), http_rx_trailers(), and http_rx_transfer_identity().

                                                                    {
        struct http_authentication *auth;
        const char *location;
        int rc;

        /* Keep connection alive if applicable */
        if ( http->response.flags & HTTP_RESPONSE_KEEPALIVE )
                pool_recycle ( &http->conn );

        /* Restart server connection interface */
        intf_restart ( &http->conn, 0 );

        /* No more data is expected */
        http->state = NULL;

        /* If transaction is successful, then close the
         * transfer-decoded interface.  The content encoding may
         * choose whether or not to immediately terminate the
         * transaction.
         */
        if ( http->response.rc == 0 ) {
                intf_shutdown ( &http->transfer, 0 );
                return 0;
        }

        /* Perform redirection, if applicable */
        if ( ( location = http->response.location ) ) {
                if ( ( rc = http_redirect ( http, location ) ) != 0 )
                        return rc;
                http_close ( http, 0 );
                return 0;
        }

        /* Fail unless a retry is permitted */
        if ( ! ( http->response.flags & HTTP_RESPONSE_RETRY ) )
                return http->response.rc;

        /* Perform authentication, if applicable */
        if ( ( auth = http->response.auth.auth ) ) {
                http->request.auth.auth = auth;
                DBGC2 ( http, "HTTP %p performing %s authentication\n",
                        http, auth->name );
                if ( ( rc = auth->authenticate ( http ) ) != 0 ) {
                        DBGC ( http, "HTTP %p could not authenticate: %s\n",
                               http, strerror ( rc ) );
                        return rc;
                }
        }

        /* Restart content decoding interfaces */
        intfs_restart ( http->response.rc, &http->content, &http->transfer,
                        NULL );
        intf_plug_plug ( &http->transfer, &http->content );
        http->len = 0;
        assert ( http->remaining == 0 );

        /* Retry immediately if applicable.  We cannot rely on an
         * immediate timer expiry, since certain Microsoft-designed
         * HTTP extensions such as NTLM break the fundamentally
         * stateless nature of HTTP and rely on the same connection
         * being reused for authentication.  See RFC7230 section 2.3
         * for further details.
         */
        if ( ! http->response.retry_after ) {
                http_reopen ( http );
                return 0;
        }

        /* Start timer to initiate retry */
        DBGC2 ( http, "HTTP %p retrying after %d seconds\n",
                http, http->response.retry_after );
        start_timer_fixed ( &http->timer,
                            ( http->response.retry_after * TICKS_PER_SEC ) );
        return 0;
}
static int http_format_headers ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP request headers.

Parameters:
httpHTTP transaction
bufBuffer
lenLength of buffer
Return values:
lenLength, or negative error

Definition at line 816 of file httpcore.c.

References DBGC2, for_each_table_entry, http_request_header::format, header, HTTP_REQUEST_HEADERS, len, http_request::method, http_method::name, http_request_header::name, NULL, rc, http_transaction::request, ssnprintf(), and http_request::uri.

Referenced by http_tx_request().

                                              {
        struct http_request_header *header;
        size_t used;
        size_t remaining;
        char *line;
        int value_len;
        int rc;

        /* Construct request line */
        used = ssnprintf ( buf, len, "%s %s HTTP/1.1",
                           http->request.method->name, http->request.uri );
        if ( used < len )
                DBGC2 ( http, "HTTP %p TX %s\n", http, buf );
        used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );

        /* Construct all headers */
        for_each_table_entry ( header, HTTP_REQUEST_HEADERS ) {

                /* Determine header value length */
                value_len = header->format ( http, NULL, 0 );
                if ( value_len < 0 ) {
                        rc = value_len;
                        return rc;
                }

                /* Skip zero-length headers */
                if ( ! value_len )
                        continue;

                /* Construct header */
                line = ( buf + used );
                used += ssnprintf ( ( buf + used ), ( len - used ), "%s: ",
                                    header->name );
                remaining = ( ( used < len ) ? ( len - used ) : 0 );
                used += header->format ( http, ( buf + used ), remaining );
                if ( used < len )
                        DBGC2 ( http, "HTTP %p TX %s\n", http, line );
                used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
        }

        /* Construct terminating newline */
        used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );

        return used;
}
static int http_format_host ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Host" header.

Parameters:
httpHTTP transaction
bufBuffer
lenLength of buffer
Return values:
lenLength of header value, or negative error

Definition at line 871 of file httpcore.c.

References http_request::host, http_transaction::request, and snprintf().

                                           {

        /* Construct host URI */
        return snprintf ( buf, len, "%s", http->request.host );
}
static int http_format_user_agent ( struct http_transaction *http  __unused,
char *  buf,
size_t  len 
) [static]

Construct HTTP "User-Agent" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 892 of file httpcore.c.

References product_version, and snprintf().

                                                            {

        /* Construct user agent */
        return snprintf ( buf, len, "iPXE/%s", product_version );
}
static int http_format_connection ( struct http_transaction *http  __unused,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Connection" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 913 of file httpcore.c.

References snprintf().

                                                            {

        /* Always request keep-alive */
        return snprintf ( buf, len, "keep-alive" );
}
static int http_format_range ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Range" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 934 of file httpcore.c.

References http_request_range::len, http_request::range, http_transaction::request, snprintf(), and http_request_range::start.

                                                       {

        /* Construct range, if applicable */
        if ( http->request.range.len ) {
                return snprintf ( buf, len, "bytes=%zd-%zd",
                                  http->request.range.start,
                                  ( http->request.range.start +
                                    http->request.range.len - 1 ) );
        } else {
                return 0;
        }
}
static int http_format_content_type ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Content-Type" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 962 of file httpcore.c.

References http_request::content, http_transaction::request, snprintf(), and http_request_content::type.

                                                              {

        /* Construct content type, if applicable */
        if ( http->request.content.type ) {
                return snprintf ( buf, len, "%s", http->request.content.type );
        } else {
                return 0;
        }
}
static int http_format_content_length ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Content-Length" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 987 of file httpcore.c.

References http_request::content, http_request_content::len, http_transaction::request, and snprintf().

                                                                {

        /* Construct content length, if applicable */
        if ( http->request.content.len ) {
                return snprintf ( buf, len, "%zd", http->request.content.len );
        } else {
                return 0;
        }
}
static int http_format_accept_encoding ( struct http_transaction http,
char *  buf,
size_t  len 
) [static]

Construct HTTP "Accept-Encoding" header.

http HTTP transaction buf Buffer len Length of buffer len Length of header value, or negative error

Definition at line 1012 of file httpcore.c.

References for_each_table_entry, HTTP_CONTENT_ENCODINGS, http_content_encoding::name, ssnprintf(), and http_content_encoding::supported.

                                                                 {
        struct http_content_encoding *encoding;
        const char *sep = "";
        size_t used = 0;

        /* Construct list of content encodings */
        for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
                if ( encoding->supported && ( ! encoding->supported ( http ) ) )
                        continue;
                used += ssnprintf ( ( buf + used ), ( len - used ),
                                    "%s%s", sep, encoding->name );
                sep = ", ";
        }

        return used;
}
static int http_tx_request ( struct http_transaction http) [static]

Transmit request.

http HTTP transaction rc Return status code

Definition at line 1042 of file httpcore.c.

References alloc_iob(), assert, http_transaction::conn, http_request::content, http_request_content::data, DBGC, empty_line_buffer(), ENOMEM, free_iob(), http_response::headers, http_format_headers(), http_headers, iob_disown, iob_put, http_request_content::len, len, memcpy(), memset(), NULL, rc, http_transaction::request, http_transaction::response, http_transaction::state, strerror(), and xfer_deliver_iob().

                                                             {
        struct io_buffer *iobuf;
        int len;
        int check_len;
        int rc;

        /* Calculate request length */
        len = http_format_headers ( http, NULL, 0 );
        if ( len < 0 ) {
                rc = len;
                DBGC ( http, "HTTP %p could not construct request: %s\n",
                       http, strerror ( rc ) );
                goto err_len;
        }

        /* Allocate I/O buffer */
        iobuf = alloc_iob ( len + 1 /* NUL */ + http->request.content.len );
        if ( ! iobuf ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* Construct request */
        check_len = http_format_headers ( http, iob_put ( iobuf, len ),
                                          ( len + 1 /* NUL */ ) );
        assert ( check_len == len );
        memcpy ( iob_put ( iobuf, http->request.content.len ),
                 http->request.content.data, http->request.content.len );

        /* Deliver request */
        if ( ( rc = xfer_deliver_iob ( &http->conn,
                                       iob_disown ( iobuf ) ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not deliver request: %s\n",
                       http, strerror ( rc ) );
                goto err_deliver;
        }

        /* Clear any previous response */
        empty_line_buffer ( &http->response.headers );
        memset ( &http->response, 0, sizeof ( http->response ) );

        /* Move to response headers state */
        http->state = &http_headers;

        return 0;

 err_deliver:
        free_iob ( iobuf );
 err_alloc:
 err_len:
        return rc;
}
static int http_parse_status ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP status line.

http HTTP transaction line Status line rc Return status code

Definition at line 1115 of file httpcore.c.

References DBGC, DBGC2, EACCES_401, EINVAL_STATUS, EIO_4XX, EIO_5XX, EIO_OTHER, ENOENT_404, EPERM_403, EXDEV, http_response::flags, HTTP_RESPONSE_KEEPALIVE, http_token(), NULL, http_response::rc, http_transaction::response, status, http_response::status, strncmp(), strtoul(), and version.

Referenced by http_parse_headers().

                                                                           {
        char *endp;
        char *version;
        char *vernum;
        char *status;
        int response_rc;

        DBGC2 ( http, "HTTP %p RX %s\n", http, line );

        /* Parse HTTP version */
        version = http_token ( &line, NULL );
        if ( ( ! version ) || ( strncmp ( version, "HTTP/", 5 ) != 0 ) ) {
                DBGC ( http, "HTTP %p malformed version \"%s\"\n", http, line );
                return -EINVAL_STATUS;
        }

        /* Keepalive is enabled by default for anything newer than HTTP/1.0 */
        vernum = ( version + 5 /* "HTTP/" (presence already checked) */ );
        if ( vernum[0] == '0' ) {
                /* HTTP/0.x : keepalive not enabled by default */
        } else if ( strncmp ( vernum, "1.0", 3 ) == 0 ) {
                /* HTTP/1.0 : keepalive not enabled by default */
        } else {
                /* HTTP/1.1 or newer: keepalive enabled by default */
                http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
        }

        /* Parse status code */
        status = line;
        http->response.status = strtoul ( status, &endp, 10 );
        if ( *endp != ' ' ) {
                DBGC ( http, "HTTP %p malformed status code \"%s\"\n",
                       http, status );
                return -EINVAL_STATUS;
        }

        /* Convert HTTP status code to iPXE return status code */
        if ( status[0] == '2' ) {
                /* 2xx Success */
                response_rc = 0;
        } else if ( status[0] == '3' ) {
                /* 3xx Redirection */
                response_rc = -EXDEV;
        } else if ( http->response.status == 401 ) {
                /* 401 Unauthorized */
                response_rc = -EACCES_401;
        } else if ( http->response.status == 403 ) {
                /* 403 Forbidden */
                response_rc = -EPERM_403;
        } else if ( http->response.status == 404 ) {
                /* 404 Not Found */
                response_rc = -ENOENT_404;
        } else if ( status[0] == '4' ) {
                /* 4xx Client Error (not already specified) */
                response_rc = -EIO_4XX;
        } else if ( status[0] == '5' ) {
                /* 5xx Server Error */
                response_rc = -EIO_5XX;
        } else {
                /* Unrecognised */
                response_rc = -EIO_OTHER;
        }
        http->response.rc = response_rc;
        if ( response_rc )
                DBGC ( http, "HTTP %p status %s\n", http, status );

        return 0;
}
static int http_parse_header ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP header.

Parameters:
httpHTTP transaction
lineHeader line
Return values:
rcReturn status code

Definition at line 1191 of file httpcore.c.

References DBGC, DBGC2, EINVAL_HEADER, for_each_table_entry, header, HTTP_RESPONSE_HEADERS, isspace(), http_response_header::name, name, http_response_header::parse, strcasecmp(), and strchr().

Referenced by http_parse_headers().

                                                                           {
        struct http_response_header *header;
        char *name = line;
        char *sep;

        DBGC2 ( http, "HTTP %p RX %s\n", http, line );

        /* Extract header name */
        sep = strchr ( line, ':' );
        if ( ! sep ) {
                DBGC ( http, "HTTP %p malformed header \"%s\"\n", http, line );
                return -EINVAL_HEADER;
        }
        *sep = '\0';

        /* Extract remainder of line */
        line = ( sep + 1 );
        while ( isspace ( *line ) )
                line++;

        /* Process header, if recognised */
        for_each_table_entry ( header, HTTP_RESPONSE_HEADERS ) {
                if ( strcasecmp ( name, header->name ) == 0 )
                        return header->parse ( http, line );
        }

        /* Unrecognised headers should be ignored */
        return 0;
}
static int http_parse_headers ( struct http_transaction http) [static]

Parse HTTP response headers.

Parameters:
httpHTTP transaction
Return values:
rcReturn status code

Definition at line 1227 of file httpcore.c.

References assert, line_buffer::data, http_response::headers, http_parse_header(), http_parse_status(), next, NULL, rc, http_transaction::response, and strlen().

Referenced by http_rx_headers().

                                                                {
        char *line;
        char *next;
        int rc;

        /* Get status line */
        line = http->response.headers.data;
        assert ( line != NULL );
        next = ( line + strlen ( line ) + 1 /* NUL */ );

        /* Parse status line */
        if ( ( rc = http_parse_status ( http, line ) ) != 0 )
                return rc;

        /* Process header lines */
        while ( 1 ) {

                /* Move to next line */
                line = next;
                next = ( line + strlen ( line ) + 1 /* NUL */ );

                /* Stop on terminating blank line */
                if ( ! line[0] )
                        return 0;

                /* Process header line */
                if ( ( rc = http_parse_header ( http, line ) ) != 0 )
                        return rc;
        }
}
static int http_parse_location ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Location" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1265 of file httpcore.c.

References http_response::location, and http_transaction::response.

                                                                             {

        /* Store location */
        http->response.location = line;
        return 0;
}
static int http_parse_transfer_encoding ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Transfer-Encoding" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1285 of file httpcore.c.

References DBGC, http_response_transfer::encoding, ENOTSUP_TRANSFER, for_each_table_entry, HTTP_TRANSFER_ENCODINGS, http_transfer_encoding::name, http_transaction::response, strcasecmp(), and http_response::transfer.

                                                       {
        struct http_transfer_encoding *encoding;

        /* Check for known transfer encodings */
        for_each_table_entry ( encoding, HTTP_TRANSFER_ENCODINGS ) {
                if ( strcasecmp ( line, encoding->name ) == 0 ) {
                        http->response.transfer.encoding = encoding;
                        return 0;
                }
        }

        DBGC ( http, "HTTP %p unrecognised Transfer-Encoding \"%s\"\n",
               http, line );
        return -ENOTSUP_TRANSFER;
}
static int http_parse_connection ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Connection" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1316 of file httpcore.c.

References http_response::flags, HTTP_RESPONSE_KEEPALIVE, http_token(), NULL, http_transaction::response, strcasecmp(), and token.

                                                                               {
        char *token;

        /* Check for known connection intentions */
        while ( ( token = http_token ( &line, NULL ) ) ) {
                if ( strcasecmp ( token, "keep-alive" ) == 0 )
                        http->response.flags |= HTTP_RESPONSE_KEEPALIVE;
                if ( strcasecmp ( token, "close" ) == 0 )
                        http->response.flags &= ~HTTP_RESPONSE_KEEPALIVE;
        }

        return 0;
}
static int http_parse_content_length ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Content-Length" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1343 of file httpcore.c.

References http_response::content, DBGC, EINVAL_CONTENT_LENGTH, http_response::flags, HTTP_RESPONSE_CONTENT_LEN, http_response_content::len, http_transaction::response, and strtoul().

                                                    {
        char *endp;

        /* Parse length */
        http->response.content.len = strtoul ( line, &endp, 10 );
        if ( *endp != '\0' ) {
                DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
                       http, line );
                return -EINVAL_CONTENT_LENGTH;
        }

        /* Record that we have a content length (since it may be zero) */
        http->response.flags |= HTTP_RESPONSE_CONTENT_LEN;

        return 0;
}
static int http_parse_content_encoding ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Content-Encoding" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1375 of file httpcore.c.

References http_response::content, DBGC, http_response_content::encoding, for_each_table_entry, HTTP_CONTENT_ENCODINGS, http_content_encoding::name, http_transaction::response, strcasecmp(), and http_content_encoding::supported.

                                                      {
        struct http_content_encoding *encoding;

        /* Check for known content encodings */
        for_each_table_entry ( encoding, HTTP_CONTENT_ENCODINGS ) {
                if ( encoding->supported && ( ! encoding->supported ( http ) ) )
                        continue;
                if ( strcasecmp ( line, encoding->name ) == 0 ) {
                        http->response.content.encoding = encoding;
                        return 0;
                }
        }

        /* Some servers (e.g. Apache) have a habit of specifying
         * unwarranted content encodings.  For example, if Apache
         * detects (via /etc/httpd/conf/magic) that a file's contents
         * are gzip-compressed, it will set "Content-Encoding: x-gzip"
         * regardless of the client's Accept-Encoding header.  The
         * only viable way to handle such servers is to treat unknown
         * content encodings as equivalent to "identity".
         */
        DBGC ( http, "HTTP %p unrecognised Content-Encoding \"%s\"\n",
               http, line );
        return 0;
}
static int http_parse_retry_after ( struct http_transaction http,
char *  line 
) [static]

Parse HTTP "Retry-After" header.

Parameters:
httpHTTP transaction
lineRemaining header line
Return values:
rcReturn status code

Definition at line 1416 of file httpcore.c.

References DBGC, http_response::flags, HTTP_RESPONSE_RETRY, HTTP_RETRY_SECONDS, http_transaction::response, http_response::retry_after, and strtoul().

                                                 {
        char *endp;

        /* Try to parse value as a simple number of seconds */
        http->response.retry_after = strtoul ( line, &endp, 10 );
        if ( *endp != '\0' ) {
                /* For any value which is not a simple number of
                 * seconds (e.g. a full HTTP date), just retry after a
                 * fixed delay, since we don't have code able to parse
                 * full HTTP dates.
                 */
                http->response.retry_after = HTTP_RETRY_SECONDS;
                DBGC ( http, "HTTP %p cannot understand Retry-After \"%s\"; "
                       "using %d seconds\n", http, line, HTTP_RETRY_SECONDS );
        }

        /* Allow HTTP request to be retried after specified delay */
        http->response.flags |= HTTP_RESPONSE_RETRY;

        return 0;
}
static int http_rx_headers ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received HTTP headers.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1452 of file httpcore.c.

References buffered_line(), http_response::content, DBGC, http_response_transfer::encoding, http_response_content::encoding, http_response::headers, http_parse_headers(), http_rx_linebuf(), http_transfer_complete(), http_transfer_identity, http_transfer_encoding::init, http_content_encoding::init, http_response_content::len, http_request::method, http_transfer_encoding::name, http_content_encoding::name, NULL, rc, http_transaction::request, http_transaction::response, http_transaction::state, http_transfer_encoding::state, strerror(), http_response::transfer, http_transaction::transfer, and xfer_seek().

                                                        {
        struct http_transfer_encoding *transfer;
        struct http_content_encoding *content;
        char *line;
        int rc;

        /* Buffer header line */
        if ( ( rc = http_rx_linebuf ( http, *iobuf,
                                      &http->response.headers ) ) != 0 )
                return rc;

        /* Wait until we see the empty line marking end of headers */
        line = buffered_line ( &http->response.headers );
        if ( ( line == NULL ) || ( line[0] != '\0' ) )
                return 0;

        /* Process headers */
        if ( ( rc = http_parse_headers ( http ) ) != 0 )
                return rc;

        /* Initialise content encoding, if applicable */
        if ( ( content = http->response.content.encoding ) &&
             ( ( rc = content->init ( http ) ) != 0 ) ) {
                DBGC ( http, "HTTP %p could not initialise %s content "
                       "encoding: %s\n", http, content->name, strerror ( rc ) );
                return rc;
        }

        /* Presize receive buffer, if we have a content length */
        if ( http->response.content.len ) {
                xfer_seek ( &http->transfer, http->response.content.len );
                xfer_seek ( &http->transfer, 0 );
        }

        /* Complete transfer if this is a HEAD request */
        if ( http->request.method == &http_head ) {
                if ( ( rc = http_transfer_complete ( http ) ) != 0 )
                        return rc;
                return 0;
        }

        /* Default to identity transfer encoding, if none specified */
        if ( ! http->response.transfer.encoding )
                http->response.transfer.encoding = &http_transfer_identity;

        /* Move to transfer encoding-specific data state */
        transfer = http->response.transfer.encoding;
        http->state = &transfer->state;

        /* Initialise transfer encoding */
        if ( ( rc = transfer->init ( http ) ) != 0 ) {
                DBGC ( http, "HTTP %p could not initialise %s transfer "
                       "encoding: %s\n", http, transfer->name, strerror ( rc ));
                return rc;
        }

        return 0;
}
static int http_init_transfer_identity ( struct http_transaction http) [static]

Initialise transfer encoding.

Parameters:
httpHTTP transaction
Return values:
rcReturn status code

Definition at line 1531 of file httpcore.c.

References http_response::content, http_response::flags, HTTP_RESPONSE_CONTENT_LEN, http_transfer_complete(), http_response_content::len, rc, and http_transaction::response.

                                                                         {
        int rc;

        /* Complete transfer immediately if we have a zero content length */
        if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
             ( http->response.content.len == 0 ) &&
             ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
                return rc;

        return 0;
}
static int http_rx_transfer_identity ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received data.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1550 of file httpcore.c.

References http_response::content, DBGC, EIO_CONTENT_LENGTH, http_response::flags, HTTP_RESPONSE_CONTENT_LEN, http_transfer_complete(), iob_disown, iob_len(), http_response_content::len, len, http_transaction::len, rc, http_transaction::response, http_transaction::transfer, and xfer_deliver_iob().

                                                                  {
        size_t len = iob_len ( *iobuf );
        int rc;

        /* Update lengths */
        http->len += len;

        /* Fail if this transfer would overrun the expected content
         * length (if any).
         */
        if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
             ( http->len > http->response.content.len ) ) {
                DBGC ( http, "HTTP %p content length overrun\n", http );
                return -EIO_CONTENT_LENGTH;
        }

        /* Hand off to content encoding */
        if ( ( rc = xfer_deliver_iob ( &http->transfer,
                                       iob_disown ( *iobuf ) ) ) != 0 )
                return rc;

        /* Complete transfer if we have received the expected content
         * length (if any).
         */
        if ( ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) &&
             ( http->len == http->response.content.len ) &&
             ( ( rc = http_transfer_complete ( http ) ) != 0 ) )
                return rc;

        return 0;
}
static void http_close_transfer_identity ( struct http_transaction http,
int  rc 
) [static]

Handle server connection close.

Parameters:
httpHTTP transaction
rcReason for close

Definition at line 1589 of file httpcore.c.

References DBGC, EIO_CONTENT_LENGTH, http_response::flags, http_close(), HTTP_RESPONSE_CONTENT_LEN, http_transfer_complete(), and http_transaction::response.

                                                    {

        /* Fail if any error occurred */
        if ( rc != 0 )
                goto err;

        /* Fail if we have a content length (since we would have
         * already closed the connection if we had received the
         * correct content length).
         */
        if ( http->response.flags & HTTP_RESPONSE_CONTENT_LEN ) {
                DBGC ( http, "HTTP %p content length underrun\n", http );
                rc = EIO_CONTENT_LENGTH;
                goto err;
        }

        /* Indicate that transfer is complete */
        if ( ( rc = http_transfer_complete ( http ) ) != 0 )
                goto err;

        return;

 err:
        http_close ( http, rc );
}
static int http_init_transfer_chunked ( struct http_transaction http) [static]

Initialise transfer encoding.

Parameters:
httpHTTP transaction
Return values:
rcReturn status code

Definition at line 1639 of file httpcore.c.

References assert, line_buffer::len, http_transaction::linebuf, and http_transaction::remaining.

                                                                        {

        /* Sanity checks */
        assert ( http->remaining == 0 );
        assert ( http->linebuf.len == 0 );

        return 0;
}
static int http_rx_chunk_len ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received chunk length.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1655 of file httpcore.c.

References buffered_line(), DBGC, EINVAL_CHUNK_LENGTH, empty_line_buffer(), http_rx_linebuf(), http_trailers, len, http_transaction::len, http_transaction::linebuf, NULL, rc, http_transaction::remaining, http_transaction::state, strtoul(), http_transaction::transfer, and xfer_seek().

Referenced by http_rx_transfer_chunked().

                                                          {
        char *line;
        char *endp;
        size_t len;
        int rc;

        /* Receive into temporary line buffer */
        if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
                return rc;

        /* Wait until we receive a non-empty line */
        line = buffered_line ( &http->linebuf );
        if ( ( line == NULL ) || ( line[0] == '\0' ) )
                return 0;

        /* Parse chunk length */
        http->remaining = strtoul ( line, &endp, 16 );
        if ( *endp != '\0' ) {
                DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
                       http, line );
                return -EINVAL_CHUNK_LENGTH;
        }

        /* Empty line buffer */
        empty_line_buffer ( &http->linebuf );

        /* Update expected length */
        len = ( http->len + http->remaining );
        xfer_seek ( &http->transfer, len );
        xfer_seek ( &http->transfer, http->len );

        /* If chunk length is zero, then move to response trailers state */
        if ( ! http->remaining )
                http->state = &http_trailers;

        return 0;
}
static int http_rx_chunk_data ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received chunk data.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1701 of file httpcore.c.

References alloc_iob(), assert, ENOMEM, iob_disown, iob_len(), iob_pull, iob_put, iob_unput, len, http_transaction::len, memcpy(), NULL, rc, http_transaction::remaining, http_transaction::transfer, and xfer_deliver_iob().

Referenced by http_rx_transfer_chunked().

                                                           {
        struct io_buffer *payload;
        uint8_t *crlf;
        size_t len;
        int rc;

        /* In the common case of a final chunk in a packet which also
         * includes the terminating CRLF, strip the terminating CRLF
         * (which we would ignore anyway) and hence avoid
         * unnecessarily copying the data.
         */
        if ( iob_len ( *iobuf ) == ( http->remaining + 2 /* CRLF */ ) ) {
                crlf = ( (*iobuf)->data + http->remaining );
                if ( ( crlf[0] == '\r' ) && ( crlf[1] == '\n' ) )
                        iob_unput ( (*iobuf), 2 /* CRLF */ );
        }
        len = iob_len ( *iobuf );

        /* Use whole/partial buffer as applicable */
        if ( len <= http->remaining ) {

                /* Whole buffer is to be consumed: decrease remaining
                 * length and use original I/O buffer as payload.
                 */
                payload = iob_disown ( *iobuf );
                http->len += len;
                http->remaining -= len;

        } else {

                /* Partial buffer is to be consumed: copy data to a
                 * temporary I/O buffer.
                 */
                payload = alloc_iob ( http->remaining );
                if ( ! payload ) {
                        rc = -ENOMEM;
                        goto err;
                }
                memcpy ( iob_put ( payload, http->remaining ), (*iobuf)->data,
                         http->remaining );
                iob_pull ( *iobuf, http->remaining );
                http->len += http->remaining;
                http->remaining = 0;
        }

        /* Hand off to content encoding */
        if ( ( rc = xfer_deliver_iob ( &http->transfer,
                                       iob_disown ( payload ) ) ) != 0 )
                goto err;

        return 0;

 err:
        assert ( payload == NULL );
        return rc;
}
static int http_rx_transfer_chunked ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received chunked data.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1766 of file httpcore.c.

References http_rx_chunk_data(), http_rx_chunk_len(), and http_transaction::remaining.

                                                                 {

        /* Handle as chunk length or chunk data as appropriate */
        if ( http->remaining ) {
                return http_rx_chunk_data ( http, iobuf );
        } else {
                return http_rx_chunk_len ( http, iobuf );
        }
}
static int http_rx_trailers ( struct http_transaction http,
struct io_buffer **  iobuf 
) [static]

Handle received HTTP trailer.

Parameters:
httpHTTP transaction
iobufI/O buffer (may be claimed)
Return values:
rcReturn status code

Definition at line 1801 of file httpcore.c.

References buffered_line(), empty_line_buffer(), http_rx_linebuf(), http_transfer_complete(), http_transaction::linebuf, NULL, and rc.

                                                         {
        char *line;
        int rc;

        /* Buffer trailer line */
        if ( ( rc = http_rx_linebuf ( http, *iobuf, &http->linebuf ) ) != 0 )
                return rc;

        /* Wait until we see the empty line marking end of trailers */
        line = buffered_line ( &http->linebuf );
        if ( ( line == NULL ) || ( line[0] != '\0' ) )
                return 0;

        /* Empty line buffer */
        empty_line_buffer ( &http->linebuf );

        /* Transfer is complete */
        if ( ( rc = http_transfer_complete ( http ) ) != 0 )
                return rc;

        return 0;
}
static size_t http_params ( struct parameters params,
char *  buf,
size_t  len 
) [static]

Construct HTTP parameter list.

Parameters:
paramsParameter list
bufBuffer to contain HTTP POST parameters
lenLength of buffer
Return values:
lenLength of parameter list (excluding terminating NUL)

Definition at line 1846 of file httpcore.c.

References for_each_param, parameter::key, len, param, uri_encode_string(), and parameter::value.

Referenced by http_open_post_uri().

                                                                               {
        struct parameter *param;
        ssize_t remaining = len;
        size_t frag_len;

        /* Add each parameter in the form "key=value", joined with "&" */
        len = 0;
        for_each_param ( param, params ) {

                /* Add the "&", if applicable */
                if ( len ) {
                        if ( remaining > 0 )
                                *buf = '&';
                        buf++;
                        len++;
                        remaining--;
                }

                /* URI-encode the key */
                frag_len = uri_encode_string ( 0, param->key, buf, remaining );
                buf += frag_len;
                len += frag_len;
                remaining -= frag_len;

                /* Add the "=" */
                if ( remaining > 0 )
                        *buf = '=';
                buf++;
                len++;
                remaining--;

                /* URI-encode the value */
                frag_len = uri_encode_string ( 0, param->value, buf, remaining);
                buf += frag_len;
                len += frag_len;
                remaining -= frag_len;
        }

        /* Ensure string is NUL-terminated even if no parameters are present */
        if ( remaining > 0 )
                *buf = '\0';

        return len;
}
static int http_open_get_uri ( struct interface xfer,
struct uri uri 
) [static]

Open HTTP transaction for simple GET URI.

Parameters:
xferData transfer interface
uriRequest URI
Return values:
rcReturn status code

Definition at line 1898 of file httpcore.c.

References http_open(), and NULL.

Referenced by http_open_uri().

                                                                         {

        return http_open ( xfer, &http_get, uri, NULL, NULL );
}
static int http_open_post_uri ( struct interface xfer,
struct uri uri 
) [static]

Open HTTP transaction for simple POST URI.

Parameters:
xferData transfer interface
uriRequest URI
Return values:
rcReturn status code

Definition at line 1910 of file httpcore.c.

References assert, http_request_content::data, data, ENOMEM, free, http_open(), http_params(), http_request_content::len, len, NULL, uri::params, params, rc, http_request_content::type, and zalloc().

Referenced by http_open_uri().

                                                                          {
        struct parameters *params = uri->params;
        struct http_request_content content;
        void *data;
        size_t len;
        size_t check_len;
        int rc;

        /* Calculate length of parameter list */
        len = http_params ( params, NULL, 0 );

        /* Allocate temporary parameter list */
        data = zalloc ( len + 1 /* NUL */ );
        if ( ! data ) {
                rc = -ENOMEM;
                goto err_alloc;
        }

        /* Construct temporary parameter list */
        check_len = http_params ( params, data, ( len + 1 /* NUL */ ) );
        assert ( check_len == len );

        /* Construct request content */
        content.type = "application/x-www-form-urlencoded";
        content.data = data;
        content.len = len;

        /* Open HTTP transaction */
        if ( ( rc = http_open ( xfer, &http_post, uri, NULL, &content ) ) != 0 )
                goto err_open;

 err_open:
        free ( data );
 err_alloc:
        return rc;
}
int http_open_uri ( struct interface xfer,
struct uri uri 
)

Open HTTP transaction for simple URI.

Parameters:
xferData transfer interface
uriRequest URI
Return values:
rcReturn status code

Definition at line 1954 of file httpcore.c.

References http_open_get_uri(), http_open_post_uri(), and uri::params.

                                                              {

        /* Open GET/POST URI as applicable */
        if ( uri->params ) {
                return http_open_post_uri ( xfer, uri );
        } else {
                return http_open_get_uri ( xfer, uri );
        }
}
REQUIRE_OBJECT ( config_http  )

Variable Documentation

struct profiler http_xfer_profiler __profiler = { .name = "http.rx" } [static]

Receive profiler.

Data transfer profiler.

Definition at line 109 of file httpcore.c.

struct errortab http_errors [] __errortab
Initial value:

Human-readable error messages.

Definition at line 115 of file httpcore.c.

static struct http_state http_request [static]
Initial value:
 {
        .tx = http_tx_request,
        .close = http_close_error,
}

HTTP request state.

Definition at line 120 of file httpcore.c.

Referenced by http_open(), and http_reopen().

static struct http_state http_headers [static]
Initial value:
 {
        .rx = http_rx_headers,
        .close = http_close_error,
}

HTTP response headers state.

Definition at line 121 of file httpcore.c.

Referenced by http_tx_request().

static struct http_state http_trailers [static]
Initial value:
 {
        .rx = http_rx_trailers,
        .close = http_close_error,
}

HTTP response trailers state.

Definition at line 122 of file httpcore.c.

Referenced by http_rx_chunk_len().

Initial value:
 {
        .name = "identity",
        .init = http_init_transfer_identity,
        .state = {
                .rx = http_rx_transfer_identity,
                .close = http_close_transfer_identity,
        },
}

Identity transfer encoding.

Definition at line 123 of file httpcore.c.

Referenced by http_rx_headers().

Initial value:
 {
        .name = "HEAD",
}

HTTP HEAD method.

Definition at line 133 of file httpcore.c.

Referenced by http_block_read_capacity().

Initial value:
 {
        .name = "GET",
}

HTTP GET method.

Definition at line 138 of file httpcore.c.

Referenced by http_block_read(), and peerblk_raw_open().

Initial value:
 {
        .name = "POST",
}

HTTP POST method.

Definition at line 143 of file httpcore.c.

Referenced by peerblk_retrieval_open().

Initial value:

HTTP data transfer interface operations.

Definition at line 523 of file httpcore.c.

Initial value:

HTTP data transfer interface descriptor.

Definition at line 532 of file httpcore.c.

Initial value:

HTTP content-decoded interface operations.

Definition at line 537 of file httpcore.c.

Initial value:

HTTP content-decoded interface descriptor.

Definition at line 545 of file httpcore.c.

Initial value:

HTTP transfer-decoded interface operations.

Definition at line 550 of file httpcore.c.

Initial value:

HTTP transfer-decoded interface descriptor.

Definition at line 555 of file httpcore.c.

Initial value:

HTTP server connection interface operations.

Definition at line 560 of file httpcore.c.

Initial value:

HTTP server connection interface descriptor.

Definition at line 568 of file httpcore.c.

Initial value:

HTTP process descriptor.

Definition at line 573 of file httpcore.c.

struct http_request_header http_request_accept_encoding __http_request_header
Initial value:
 {
        .name = "Host",
        .format = http_format_host,
}

HTTP "Host" header ".

HTTP "Accept-Encoding" header.

HTTP "Content-Length" header.

HTTP "Content-Type" header.

HTTP "Range" header.

HTTP "Connection" header.

HTTP "User-Agent" header.

Definition at line 879 of file httpcore.c.

struct http_response_header http_response_retry_after __http_response_header
Initial value:
 {
        .name = "Location",
        .parse = http_parse_location,
}

HTTP "Location" header.

HTTP "Retry-After" header.

HTTP "Content-Encoding" header.

HTTP "Content-Length" header.

HTTP "Connection" header.

HTTP "Transfer-Encoding" header.

Definition at line 1273 of file httpcore.c.

struct http_transfer_encoding http_transfer_chunked __http_transfer_encoding
Initial value:
 {
        .name = "chunked",
        .init = http_init_transfer_chunked,
        .state = {
                .rx = http_rx_transfer_chunked,
                .close = http_close_error,
        },
}

Chunked transfer encoding.

Definition at line 1778 of file httpcore.c.