123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526 |
-
-
-
-
- #include <stdint.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <strings.h>
- #include <byteswap.h>
- #include <errno.h>
- #include <assert.h>
- #include <gpxe/uri.h>
- #include <gpxe/refcnt.h>
- #include <gpxe/iobuf.h>
- #include <gpxe/xfer.h>
- #include <gpxe/open.h>
- #include <gpxe/socket.h>
- #include <gpxe/tcpip.h>
- #include <gpxe/process.h>
- #include <gpxe/linebuf.h>
- #include <gpxe/tls.h>
- #include <gpxe/http.h>
-
-
- enum http_rx_state {
- HTTP_RX_RESPONSE = 0,
- HTTP_RX_HEADER,
- HTTP_RX_DATA,
- HTTP_RX_DEAD,
- };
-
-
- struct http_request {
-
- struct refcnt refcnt;
-
- struct xfer_interface xfer;
-
-
- struct uri *uri;
-
- struct xfer_interface socket;
-
-
- struct process process;
-
-
- unsigned int response;
-
- size_t content_length;
-
- size_t rx_len;
-
- enum http_rx_state rx_state;
-
- struct line_buffer linebuf;
- };
-
-
- static void http_free ( struct refcnt *refcnt ) {
- struct http_request *http =
- container_of ( refcnt, struct http_request, refcnt );
-
- uri_put ( http->uri );
- empty_line_buffer ( &http->linebuf );
- free ( http );
- };
-
-
- static void http_done ( struct http_request *http, int rc ) {
-
-
- http->rx_state = HTTP_RX_DEAD;
-
-
-
- if ( http->content_length &&
- ( http->content_length != http->rx_len ) ) {
- DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
- http, http->rx_len, http->content_length );
- rc = -EIO;
- }
-
-
- process_del ( &http->process );
-
-
- xfer_nullify ( &http->socket );
- xfer_close ( &http->socket, rc );
- xfer_nullify ( &http->xfer );
- xfer_close ( &http->xfer, rc );
- }
-
-
- static int http_response_to_rc ( unsigned int response ) {
- switch ( response ) {
- case 200:
- return 0;
- case 404:
- return -ENOENT;
- case 403:
- return -EPERM;
- default:
- return -EIO;
- }
- }
-
-
- static int http_rx_response ( struct http_request *http, char *response ) {
- char *spc;
- int rc;
-
- DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
-
-
- if ( strncmp ( response, "HTTP/", 5 ) != 0 )
- return -EIO;
-
-
- spc = strchr ( response, ' ' );
- if ( ! spc )
- return -EIO;
- http->response = strtoul ( spc, NULL, 10 );
- if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
- return rc;
-
-
- http->rx_state = HTTP_RX_HEADER;
- return 0;
- }
-
-
- static int http_rx_content_length ( struct http_request *http,
- const char *value ) {
- char *endp;
-
- http->content_length = strtoul ( value, &endp, 10 );
- if ( *endp != '\0' ) {
- DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
- http, value );
- return -EIO;
- }
-
-
- xfer_seek ( &http->xfer, http->content_length, SEEK_SET );
- xfer_seek ( &http->xfer, 0, SEEK_SET );
-
- return 0;
- }
-
-
- struct http_header_handler {
-
- const char *header;
-
-
- int ( * rx ) ( struct http_request *http, const char *value );
- };
-
-
- struct http_header_handler http_header_handlers[] = {
- {
- .header = "Content-Length",
- .rx = http_rx_content_length,
- },
- { NULL, NULL }
- };
-
-
- static int http_rx_header ( struct http_request *http, char *header ) {
- struct http_header_handler *handler;
- char *separator;
- char *value;
- int rc;
-
-
- if ( ! header[0] ) {
- DBGC ( http, "HTTP %p start of data\n", http );
- empty_line_buffer ( &http->linebuf );
- http->rx_state = HTTP_RX_DATA;
- return 0;
- }
-
- DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
-
-
- separator = strstr ( header, ": " );
- if ( ! separator ) {
- DBGC ( http, "HTTP %p malformed header\n", http );
- return -EIO;
- }
- *separator = '\0';
- value = ( separator + 2 );
-
-
- for ( handler = http_header_handlers ; handler->header ; handler++ ) {
- if ( strcasecmp ( header, handler->header ) == 0 ) {
- if ( ( rc = handler->rx ( http, value ) ) != 0 )
- return rc;
- break;
- }
- }
- return 0;
- }
-
-
- struct http_line_handler {
-
-
- int ( * rx ) ( struct http_request *http, char *line );
- };
-
-
- struct http_line_handler http_line_handlers[] = {
- [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
- [HTTP_RX_HEADER] = { .rx = http_rx_header },
- };
-
-
- static int http_rx_data ( struct http_request *http,
- struct io_buffer *iobuf ) {
- int rc;
-
-
- http->rx_len += iob_len ( iobuf );
-
-
- if ( ( rc = xfer_deliver_iob ( &http->xfer, iobuf ) ) != 0 )
- return rc;
-
-
- if ( http->content_length &&
- ( http->rx_len >= http->content_length ) ) {
- http_done ( http, 0 );
- }
-
- return 0;
- }
-
-
- static int http_socket_deliver_iob ( struct xfer_interface *socket,
- struct io_buffer *iobuf,
- struct xfer_metadata *meta __unused ) {
- struct http_request *http =
- container_of ( socket, struct http_request, socket );
- struct http_line_handler *lh;
- char *line;
- ssize_t len;
- int rc = 0;
-
- while ( iob_len ( iobuf ) ) {
- switch ( http->rx_state ) {
- case HTTP_RX_DEAD:
-
- goto done;
- case HTTP_RX_DATA:
-
-
- rc = http_rx_data ( http, iobuf );
- iobuf = NULL;
- goto done;
- case HTTP_RX_RESPONSE:
- case HTTP_RX_HEADER:
-
-
- len = line_buffer ( &http->linebuf, iobuf->data,
- iob_len ( iobuf ) );
- if ( len < 0 ) {
- DBGC ( http, "HTTP %p could not buffer line: "
- "%s\n", http, strerror ( rc ) );
- goto done;
- }
- iob_pull ( iobuf, len );
- line = buffered_line ( &http->linebuf );
- if ( line ) {
- lh = &http_line_handlers[http->rx_state];
- if ( ( rc = lh->rx ( http, line ) ) != 0 )
- goto done;
- }
- break;
- default:
- assert ( 0 );
- break;
- }
- }
-
- done:
- if ( rc )
- http_done ( http, rc );
- free_iob ( iobuf );
- return rc;
- }
-
-
- static void http_step ( struct process *process ) {
- struct http_request *http =
- container_of ( process, struct http_request, process );
- const char *path = http->uri->path;
- const char *host = http->uri->host;
- const char *query = http->uri->query;
- int rc;
-
- if ( xfer_ready ( &http->socket ) == 0 ) {
- process_del ( &http->process );
- if ( ( rc = xfer_printf ( &http->socket,
- "GET %s%s%s HTTP/1.1\r\n"
- "User-Agent: gPXE/" VERSION "\r\n"
- "Host: %s\r\n"
- "\r\n",
- ( path ? path : "/" ),
- ( query ? "?" : "" ),
- ( query ? query : "" ),
- host ) ) != 0 ) {
- http_done ( http, rc );
- }
- }
- }
-
-
- static void http_socket_close ( struct xfer_interface *socket, int rc ) {
- struct http_request *http =
- container_of ( socket, struct http_request, socket );
-
- DBGC ( http, "HTTP %p socket closed: %s\n",
- http, strerror ( rc ) );
-
- http_done ( http, rc );
- }
-
-
- static struct xfer_interface_operations http_socket_operations = {
- .close = http_socket_close,
- .vredirect = xfer_vopen,
- .request = ignore_xfer_request,
- .seek = ignore_xfer_seek,
- .alloc_iob = default_xfer_alloc_iob,
- .deliver_iob = http_socket_deliver_iob,
- .deliver_raw = xfer_deliver_as_iob,
- };
-
-
- static void http_xfer_close ( struct xfer_interface *xfer, int rc ) {
- struct http_request *http =
- container_of ( xfer, struct http_request, xfer );
-
- DBGC ( http, "HTTP %p interface closed: %s\n",
- http, strerror ( rc ) );
-
- http_done ( http, rc );
- }
-
-
- static struct xfer_interface_operations http_xfer_operations = {
- .close = http_xfer_close,
- .vredirect = ignore_xfer_vredirect,
- .request = ignore_xfer_request,
- .seek = ignore_xfer_seek,
- .alloc_iob = default_xfer_alloc_iob,
- .deliver_iob = xfer_deliver_as_raw,
- .deliver_raw = ignore_xfer_deliver_raw,
- };
-
-
- int http_open ( struct xfer_interface *xfer, struct uri *uri ) {
- struct http_request *http;
- struct sockaddr_tcpip server;
- int rc;
-
-
- if ( ! uri->host )
- return -EINVAL;
-
-
- http = malloc ( sizeof ( *http ) );
- if ( ! http )
- return -ENOMEM;
- memset ( http, 0, sizeof ( *http ) );
- http->refcnt.free = http_free;
- xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
- http->uri = uri_get ( uri );
- xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );
- process_init ( &http->process, http_step, &http->refcnt );
-
-
- memset ( &server, 0, sizeof ( server ) );
- server.st_port = htons ( uri_port ( http->uri, HTTP_PORT ) );
- if ( ( rc = xfer_open_named_socket ( &http->socket, SOCK_STREAM,
- ( struct sockaddr * ) &server,
- uri->host, NULL ) ) != 0 )
- goto err;
-
- #if 0
- if ( strcmp ( http->uri->scheme, "https" ) == 0 ) {
- st->st_port = htons ( uri_port ( http->uri, HTTPS_PORT ) );
- if ( ( rc = add_tls ( &http->stream ) ) != 0 )
- goto err;
- }
- #endif
-
-
- xfer_plug_plug ( &http->xfer, xfer );
- ref_put ( &http->refcnt );
- return 0;
-
- err:
- DBGC ( http, "HTTP %p could not create request: %s\n",
- http, strerror ( rc ) );
- http_done ( http, rc );
- ref_put ( &http->refcnt );
- return rc;
- }
-
-
- struct uri_opener http_uri_opener __uri_opener = {
- .scheme = "http",
- .open = http_open,
- };
-
-
- struct uri_opener https_uri_opener __uri_opener = {
- .scheme = "https",
- .open = http_open,
- };
|