1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966 |
- /*
- * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of the
- * License, or any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301, USA.
- *
- * You can also choose to distribute this program under the terms of
- * the Unmodified Binary Distribution Licence (as given in the file
- * COPYING.UBDL), provided that you have satisfied its requirements.
- */
-
- FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
-
- /**
- * @file
- *
- * Hyper Text Transfer Protocol (HTTP) core functionality
- *
- */
-
- #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>
-
- /* Disambiguate the various error causes */
- #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" )
-
- /** Retry delay used when we cannot understand the Retry-After header */
- #define HTTP_RETRY_SECONDS 5
-
- /** Receive profiler */
- static struct profiler http_rx_profiler __profiler = { .name = "http.rx" };
-
- /** Data transfer profiler */
- static struct profiler http_xfer_profiler __profiler = { .name = "http.xfer" };
-
- /** Human-readable error messages */
- struct errortab http_errors[] __errortab = {
- __einfo_errortab ( EINFO_EIO_4XX ),
- __einfo_errortab ( EINFO_EIO_5XX ),
- };
-
- static struct http_state http_request;
- static struct http_state http_headers;
- static struct http_state http_trailers;
- static struct http_transfer_encoding http_transfer_identity;
-
- /******************************************************************************
- *
- * Methods
- *
- ******************************************************************************
- */
-
- /** HTTP HEAD method */
- struct http_method http_head = {
- .name = "HEAD",
- };
-
- /** HTTP GET method */
- struct http_method http_get = {
- .name = "GET",
- };
-
- /** HTTP POST method */
- struct http_method http_post = {
- .name = "POST",
- };
-
- /******************************************************************************
- *
- * Utility functions
- *
- ******************************************************************************
- */
-
- /**
- * Handle received HTTP line-buffered data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer
- * @v linebuf Line buffer
- * @ret rc Return status code
- */
- static int http_rx_linebuf ( struct http_transaction *http,
- struct io_buffer *iobuf,
- struct line_buffer *linebuf ) {
- 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;
- }
-
- /**
- * Get HTTP response token
- *
- * @v line Line position
- * @v value Token value to fill in (if any)
- * @ret token Token, or NULL
- */
- char * http_token ( char **line, char **value ) {
- 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;
- }
-
- /******************************************************************************
- *
- * Transactions
- *
- ******************************************************************************
- */
-
- /**
- * Free HTTP transaction
- *
- * @v refcnt Reference count
- */
- static void http_free ( struct refcnt *refcnt ) {
- 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 );
- }
-
- /**
- * Close HTTP transaction
- *
- * @v http HTTP transaction
- * @v rc Reason for close
- */
- static void http_close ( struct http_transaction *http, int rc ) {
-
- /* 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 );
- }
-
- /**
- * Close HTTP transaction with error (even if none specified)
- *
- * @v http HTTP transaction
- * @v rc Reason for close
- */
- static void http_close_error ( struct http_transaction *http, int rc ) {
-
- /* Treat any close as an error */
- http_close ( http, ( rc ? rc : -EPIPE ) );
- }
-
- /**
- * Reopen stale HTTP connection
- *
- * @v http HTTP transaction
- */
- static void http_reopen ( struct http_transaction *http ) {
- 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 );
- }
-
- /**
- * Handle retry timer expiry
- *
- * @v timer Retry timer
- * @v over Failure indicator
- */
- static void http_expired ( struct retry_timer *timer, int over __unused ) {
- struct http_transaction *http =
- container_of ( timer, struct http_transaction, timer );
-
- /* Reopen connection */
- http_reopen ( http );
- }
-
- /**
- * HTTP transmit process
- *
- * @v http HTTP transaction
- */
- static void http_step ( struct http_transaction *http ) {
- 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 );
- }
-
- /**
- * Handle received HTTP data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer
- * @v meta Transfer metadata
- * @ret rc Return status code
- *
- * This function takes ownership of the I/O buffer.
- */
- static int http_conn_deliver ( struct http_transaction *http,
- struct io_buffer *iobuf,
- struct xfer_metadata *meta __unused ) {
- 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;
- }
-
- /**
- * Handle server connection close
- *
- * @v http HTTP transaction
- * @v rc Reason for close
- */
- static void http_conn_close ( struct http_transaction *http, int rc ) {
-
- /* 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 );
- }
-
- /**
- * Handle received content-decoded data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer
- * @v meta Data transfer metadata
- */
- static int http_content_deliver ( struct http_transaction *http,
- struct io_buffer *iobuf,
- struct xfer_metadata *meta ) {
- 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;
- }
-
- /**
- * Get underlying data transfer buffer
- *
- * @v http HTTP transaction
- * @ret xferbuf Data transfer buffer, or NULL on error
- */
- static struct xfer_buffer *
- http_content_buffer ( struct http_transaction *http ) {
-
- /* 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 );
- }
-
- /**
- * Read from block device (when HTTP block device support is not present)
- *
- * @v http HTTP transaction
- * @v data Data interface
- * @v lba Starting logical block address
- * @v count Number of logical blocks
- * @v buffer Data buffer
- * @v len Length of data buffer
- * @ret rc Return status code
- */
- __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 ) {
-
- return -ENOTSUP;
- }
-
- /**
- * Read block device capacity (when HTTP block device support is not present)
- *
- * @v control Control interface
- * @v data Data interface
- * @ret rc Return status code
- */
- __weak int http_block_read_capacity ( struct http_transaction *http __unused,
- struct interface *data __unused ) {
-
- return -ENOTSUP;
- }
-
- /** HTTP data transfer interface operations */
- static struct interface_operation http_xfer_operations[] = {
- INTF_OP ( block_read, struct http_transaction *, http_block_read ),
- INTF_OP ( block_read_capacity, struct http_transaction *,
- http_block_read_capacity ),
- INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
- INTF_OP ( intf_close, struct http_transaction *, http_close ),
- };
-
- /** HTTP data transfer interface descriptor */
- static struct interface_descriptor http_xfer_desc =
- INTF_DESC_PASSTHRU ( struct http_transaction, xfer,
- http_xfer_operations, content );
-
- /** HTTP content-decoded interface operations */
- static struct interface_operation http_content_operations[] = {
- INTF_OP ( xfer_deliver, struct http_transaction *,
- http_content_deliver ),
- INTF_OP ( xfer_buffer, struct http_transaction *, http_content_buffer ),
- INTF_OP ( intf_close, struct http_transaction *, http_close ),
- };
-
- /** HTTP content-decoded interface descriptor */
- static struct interface_descriptor http_content_desc =
- INTF_DESC_PASSTHRU ( struct http_transaction, content,
- http_content_operations, xfer );
-
- /** HTTP transfer-decoded interface operations */
- static struct interface_operation http_transfer_operations[] = {
- INTF_OP ( intf_close, struct http_transaction *, http_close ),
- };
-
- /** HTTP transfer-decoded interface descriptor */
- static struct interface_descriptor http_transfer_desc =
- INTF_DESC_PASSTHRU ( struct http_transaction, transfer,
- http_transfer_operations, conn );
-
- /** HTTP server connection interface operations */
- static struct interface_operation http_conn_operations[] = {
- INTF_OP ( xfer_deliver, struct http_transaction *, http_conn_deliver ),
- INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
- INTF_OP ( pool_reopen, struct http_transaction *, http_reopen ),
- INTF_OP ( intf_close, struct http_transaction *, http_conn_close ),
- };
-
- /** HTTP server connection interface descriptor */
- static struct interface_descriptor http_conn_desc =
- INTF_DESC_PASSTHRU ( struct http_transaction, conn,
- http_conn_operations, transfer );
-
- /** HTTP process descriptor */
- static struct process_descriptor http_process_desc =
- PROC_DESC_ONCE ( struct http_transaction, process, http_step );
-
- /**
- * Open HTTP transaction
- *
- * @v xfer Data transfer interface
- * @v method Request method
- * @v uri Request URI
- * @v range Content range (if any)
- * @v content Request content (if any)
- * @ret rc Return status code
- */
- int http_open ( struct interface *xfer, struct http_method *method,
- struct uri *uri, struct http_request_range *range,
- struct http_request_content *content ) {
- 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;
- }
-
- /**
- * Redirect HTTP transaction
- *
- * @v http HTTP transaction
- * @v location New location
- * @ret rc Return status code
- */
- static int http_redirect ( struct http_transaction *http,
- const char *location ) {
- 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;
- }
-
- /**
- * Handle successful transfer completion
- *
- * @v http HTTP transaction
- * @ret rc Return status code
- */
- static int http_transfer_complete ( struct http_transaction *http ) {
- 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;
- }
-
- /******************************************************************************
- *
- * Requests
- *
- ******************************************************************************
- */
-
- /**
- * Construct HTTP request headers
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length, or negative error
- */
- static int http_format_headers ( struct http_transaction *http, char *buf,
- size_t len ) {
- 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;
- }
-
- /**
- * Construct HTTP "Host" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_host ( struct http_transaction *http, char *buf,
- size_t len ) {
-
- /* Construct host URI */
- return snprintf ( buf, len, "%s", http->request.host );
- }
-
- /** HTTP "Host" header "*/
- struct http_request_header http_request_host __http_request_header = {
- .name = "Host",
- .format = http_format_host,
- };
-
- /**
- * Construct HTTP "User-Agent" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_user_agent ( struct http_transaction *http __unused,
- char *buf, size_t len ) {
-
- /* Construct user agent */
- return snprintf ( buf, len, "iPXE/%s", product_version );
- }
-
- /** HTTP "User-Agent" header */
- struct http_request_header http_request_user_agent __http_request_header = {
- .name = "User-Agent",
- .format = http_format_user_agent,
- };
-
- /**
- * Construct HTTP "Connection" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_connection ( struct http_transaction *http __unused,
- char *buf, size_t len ) {
-
- /* Always request keep-alive */
- return snprintf ( buf, len, "keep-alive" );
- }
-
- /** HTTP "Connection" header */
- struct http_request_header http_request_connection __http_request_header = {
- .name = "Connection",
- .format = http_format_connection,
- };
-
- /**
- * Construct HTTP "Range" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_range ( struct http_transaction *http,
- char *buf, size_t len ) {
-
- /* 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;
- }
- }
-
- /** HTTP "Range" header */
- struct http_request_header http_request_range __http_request_header = {
- .name = "Range",
- .format = http_format_range,
- };
-
- /**
- * Construct HTTP "Content-Type" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_content_type ( struct http_transaction *http,
- char *buf, size_t len ) {
-
- /* Construct content type, if applicable */
- if ( http->request.content.type ) {
- return snprintf ( buf, len, "%s", http->request.content.type );
- } else {
- return 0;
- }
- }
-
- /** HTTP "Content-Type" header */
- struct http_request_header http_request_content_type __http_request_header = {
- .name = "Content-Type",
- .format = http_format_content_type,
- };
-
- /**
- * Construct HTTP "Content-Length" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_content_length ( struct http_transaction *http,
- char *buf, size_t len ) {
-
- /* Construct content length, if applicable */
- if ( http->request.content.len ) {
- return snprintf ( buf, len, "%zd", http->request.content.len );
- } else {
- return 0;
- }
- }
-
- /** HTTP "Content-Length" header */
- struct http_request_header http_request_content_length __http_request_header = {
- .name = "Content-Length",
- .format = http_format_content_length,
- };
-
- /**
- * Construct HTTP "Accept-Encoding" header
- *
- * @v http HTTP transaction
- * @v buf Buffer
- * @v len Length of buffer
- * @ret len Length of header value, or negative error
- */
- static int http_format_accept_encoding ( struct http_transaction *http,
- char *buf, size_t len ) {
- 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;
- }
-
- /** HTTP "Accept-Encoding" header */
- struct http_request_header http_request_accept_encoding __http_request_header ={
- .name = "Accept-Encoding",
- .format = http_format_accept_encoding,
- };
-
- /**
- * Transmit request
- *
- * @v http HTTP transaction
- * @ret rc Return status code
- */
- static int http_tx_request ( struct http_transaction *http ) {
- 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;
- }
-
- /** HTTP request state */
- static struct http_state http_request = {
- .tx = http_tx_request,
- .close = http_close_error,
- };
-
- /******************************************************************************
- *
- * Response headers
- *
- ******************************************************************************
- */
-
- /**
- * Parse HTTP status line
- *
- * @v http HTTP transaction
- * @v line Status line
- * @ret rc Return status code
- */
- static int http_parse_status ( struct http_transaction *http, char *line ) {
- 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;
- }
-
- /**
- * Parse HTTP header
- *
- * @v http HTTP transaction
- * @v line Header line
- * @ret rc Return status code
- */
- static int http_parse_header ( struct http_transaction *http, char *line ) {
- 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;
- }
-
- /**
- * Parse HTTP response headers
- *
- * @v http HTTP transaction
- * @ret rc Return status code
- */
- static int http_parse_headers ( struct http_transaction *http ) {
- 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;
- }
- }
-
- /**
- * Parse HTTP "Location" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_location ( struct http_transaction *http, char *line ) {
-
- /* Store location */
- http->response.location = line;
- return 0;
- }
-
- /** HTTP "Location" header */
- struct http_response_header http_response_location __http_response_header = {
- .name = "Location",
- .parse = http_parse_location,
- };
-
- /**
- * Parse HTTP "Transfer-Encoding" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_transfer_encoding ( struct http_transaction *http,
- char *line ) {
- 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;
- }
-
- /** HTTP "Transfer-Encoding" header */
- struct http_response_header
- http_response_transfer_encoding __http_response_header = {
- .name = "Transfer-Encoding",
- .parse = http_parse_transfer_encoding,
- };
-
- /**
- * Parse HTTP "Connection" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_connection ( struct http_transaction *http, char *line ) {
- 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;
- }
-
- /** HTTP "Connection" header */
- struct http_response_header http_response_connection __http_response_header = {
- .name = "Connection",
- .parse = http_parse_connection,
- };
-
- /**
- * Parse HTTP "Content-Length" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_content_length ( struct http_transaction *http,
- char *line ) {
- 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;
- }
-
- /** HTTP "Content-Length" header */
- struct http_response_header
- http_response_content_length __http_response_header = {
- .name = "Content-Length",
- .parse = http_parse_content_length,
- };
-
- /**
- * Parse HTTP "Content-Encoding" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_content_encoding ( struct http_transaction *http,
- char *line ) {
- 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;
- }
-
- /** HTTP "Content-Encoding" header */
- struct http_response_header
- http_response_content_encoding __http_response_header = {
- .name = "Content-Encoding",
- .parse = http_parse_content_encoding,
- };
-
- /**
- * Parse HTTP "Retry-After" header
- *
- * @v http HTTP transaction
- * @v line Remaining header line
- * @ret rc Return status code
- */
- static int http_parse_retry_after ( struct http_transaction *http,
- char *line ) {
- 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;
- }
-
- /** HTTP "Retry-After" header */
- struct http_response_header http_response_retry_after __http_response_header = {
- .name = "Retry-After",
- .parse = http_parse_retry_after,
- };
-
- /**
- * Handle received HTTP headers
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_headers ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
- 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;
- }
-
- /** HTTP response headers state */
- static struct http_state http_headers = {
- .rx = http_rx_headers,
- .close = http_close_error,
- };
-
- /******************************************************************************
- *
- * Identity transfer encoding
- *
- ******************************************************************************
- */
-
- /**
- * Initialise transfer encoding
- *
- * @v http HTTP transaction
- * @ret rc Return status code
- */
- static int http_init_transfer_identity ( struct http_transaction *http ) {
- 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;
- }
-
- /**
- * Handle received data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_transfer_identity ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
- 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;
- }
-
- /**
- * Handle server connection close
- *
- * @v http HTTP transaction
- * @v rc Reason for close
- */
- static void http_close_transfer_identity ( struct http_transaction *http,
- int rc ) {
-
- /* 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 );
- }
-
- /** Identity transfer encoding */
- static struct http_transfer_encoding http_transfer_identity = {
- .name = "identity",
- .init = http_init_transfer_identity,
- .state = {
- .rx = http_rx_transfer_identity,
- .close = http_close_transfer_identity,
- },
- };
-
- /******************************************************************************
- *
- * Chunked transfer encoding
- *
- ******************************************************************************
- */
-
- /**
- * Initialise transfer encoding
- *
- * @v http HTTP transaction
- * @ret rc Return status code
- */
- static int http_init_transfer_chunked ( struct http_transaction *http ) {
-
- /* Sanity checks */
- assert ( http->remaining == 0 );
- assert ( http->linebuf.len == 0 );
-
- return 0;
- }
-
- /**
- * Handle received chunk length
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_chunk_len ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
- 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;
- }
-
- /**
- * Handle received chunk data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_chunk_data ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
- 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;
- }
-
- /**
- * Handle received chunked data
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_transfer_chunked ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
-
- /* 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 );
- }
- }
-
- /** Chunked transfer encoding */
- struct http_transfer_encoding http_transfer_chunked __http_transfer_encoding = {
- .name = "chunked",
- .init = http_init_transfer_chunked,
- .state = {
- .rx = http_rx_transfer_chunked,
- .close = http_close_error,
- },
- };
-
- /******************************************************************************
- *
- * Response trailers
- *
- ******************************************************************************
- */
-
- /**
- * Handle received HTTP trailer
- *
- * @v http HTTP transaction
- * @v iobuf I/O buffer (may be claimed)
- * @ret rc Return status code
- */
- static int http_rx_trailers ( struct http_transaction *http,
- struct io_buffer **iobuf ) {
- 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;
- }
-
- /** HTTP response trailers state */
- static struct http_state http_trailers = {
- .rx = http_rx_trailers,
- .close = http_close_error,
- };
-
- /******************************************************************************
- *
- * Simple URI openers
- *
- ******************************************************************************
- */
-
- /**
- * Construct HTTP parameter list
- *
- * @v params Parameter list
- * @v buf Buffer to contain HTTP POST parameters
- * @v len Length of buffer
- * @ret len Length of parameter list (excluding terminating NUL)
- */
- static size_t http_params ( struct parameters *params, char *buf, size_t len ) {
- 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;
- }
-
- /**
- * Open HTTP transaction for simple GET URI
- *
- * @v xfer Data transfer interface
- * @v uri Request URI
- * @ret rc Return status code
- */
- static int http_open_get_uri ( struct interface *xfer, struct uri *uri ) {
-
- return http_open ( xfer, &http_get, uri, NULL, NULL );
- }
-
- /**
- * Open HTTP transaction for simple POST URI
- *
- * @v xfer Data transfer interface
- * @v uri Request URI
- * @ret rc Return status code
- */
- static int http_open_post_uri ( struct interface *xfer, struct uri *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;
- }
-
- /**
- * Open HTTP transaction for simple URI
- *
- * @v xfer Data transfer interface
- * @v uri Request URI
- * @ret rc Return status code
- */
- int http_open_uri ( struct interface *xfer, struct uri *uri ) {
-
- /* Open GET/POST URI as applicable */
- if ( uri->params ) {
- return http_open_post_uri ( xfer, uri );
- } else {
- return http_open_get_uri ( xfer, uri );
- }
- }
-
- /* Drag in HTTP extensions */
- REQUIRING_SYMBOL ( http_open );
- REQUIRE_OBJECT ( config_http );
|