+/*
+ * Copyright (C) 2007 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+/**
+ * @file
+ *
+ * Hyper Text Transfer Protocol (HTTP)
+ *
+ */
+
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <vsprintf.h>
#include <assert.h>
#include <gpxe/async.h>
+#include <gpxe/buffer.h>
#include <gpxe/http.h>
-/** @file
- *
- * The Hyper Text Transfer Protocol (HTTP)
- *
- * This file implements the TCP-based HTTP protocol. It connects to the
- * server specified in http_request::tcp and transmit an HTTP GET request
- * for the file specified in http_request::filename. It then decoded the
- * HTTP header, determining file status and file size. Then sends the file
- * to the callback function at http_request::callback().
- * **NOTE**: still working on correcting the closing of the tcp connection
- *
- * To use this code, do something like:
+static inline struct http_request *
+tcp_to_http ( struct tcp_application *app ) {
+ return container_of ( app, struct http_request, tcp );
+}
+
+/**
+ * Mark HTTP request as complete
*
- * @code
+ * @v http HTTP request
+ * @v rc Return status code
*
- * static void my_callback ( struct http_request *http, char *data, size_t len ) {
- * ... process data ...
- * }
+ */
+static void http_done ( struct http_request *http, int rc ) {
+
+ /* Close TCP connection */
+ tcp_close ( &http->tcp );
+
+ /* Prevent further processing of any current packet */
+ http->rx_state = HTTP_RX_DEAD;
+
+ /* Free up any dynamically allocated storage */
+ empty_line_buffer ( &http->linebuf );
+
+ /* If we had a Content-Length, and the received content length
+ * isn't correct, flag an error
+ */
+ if ( http->content_length &&
+ ( http->content_length != http->buffer->fill ) ) {
+ DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
+ http, http->buffer->fill, http->content_length );
+ rc = -EIO;
+ }
+
+ /* Mark async operation as complete */
+ async_done ( &http->aop, rc );
+}
+
+/**
+ * Convert HTTP response code to return status code
*
- * struct http_request http = {
- * .filename = "path/to/file",
- * .callback = my_callback,
- * };
+ * @v response HTTP response code
+ * @ret rc Return status code
+ */
+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;
+ }
+}
+
+/**
+ * Handle HTTP response
*
- * ... assign http.tcp.server ...
+ * @v http HTTP request
+ * @v response HTTP response
+ */
+static void http_rx_response ( struct http_request *http, char *response ) {
+ char *spc;
+ int rc = -EIO;
+
+ DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
+
+ /* Check response starts with "HTTP/" */
+ if ( strncmp ( response, "HTTP/", 5 ) != 0 )
+ goto err;
+
+ /* Locate and check response code */
+ spc = strchr ( response, ' ' );
+ if ( ! spc )
+ goto err;
+ http->response = strtoul ( spc, NULL, 10 );
+ if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
+ goto err;
+
+ /* Move to received headers */
+ http->rx_state = HTTP_RX_HEADER;
+ return;
+
+ err:
+ DBGC ( http, "HTTP %p bad response\n", http );
+ http_done ( http, rc );
+ return;
+}
+
+/**
+ * Handle HTTP Content-Length header
*
- * rc = async_wait ( get_http ( &http ) );
+ * @v http HTTP request
+ * @v value HTTP header value
+ * @ret rc Return status code
+ */
+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;
+ }
+
+ return 0;
+}
+
+/**
+ * An HTTP header handler
*
- * @endcode
+ */
+struct http_header_handler {
+ /** Name (e.g. "Content-Length") */
+ const char *header;
+ /** Handle received header
+ *
+ * @v http HTTP request
+ * @v value HTTP header value
+ * @ret rc Return status code
+ */
+ int ( * rx ) ( struct http_request *http, const char *value );
+};
+
+/** List of HTTP header handlers */
+struct http_header_handler http_header_handlers[] = {
+ {
+ .header = "Content-Length",
+ .rx = http_rx_content_length,
+ },
+ { NULL, NULL }
+};
+
+/**
+ * Handle HTTP header
*
+ * @v http HTTP request
+ * @v header HTTP header
*/
+static void http_rx_header ( struct http_request *http, char *header ) {
+ struct http_header_handler *handler;
+ char *separator;
+ char *value;
+ int rc = -EIO;
-static inline struct http_request *
-tcp_to_http ( struct tcp_application *app ) {
- return container_of ( app, struct http_request, tcp );
+ /* An empty header line marks the transition to the data phase */
+ if ( ! header[0] ) {
+ DBGC ( http, "HTTP %p start of data\n", http );
+ empty_line_buffer ( &http->linebuf );
+ http->rx_state = HTTP_RX_DATA;
+ return;
+ }
+
+ DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
+
+ /* Split header at the ": " */
+ separator = strstr ( header, ": " );
+ if ( ! separator )
+ goto err;
+ *separator = '\0';
+ value = ( separator + 2 );
+
+ /* Hand off to header handler, if one exists */
+ for ( handler = http_header_handlers ; handler->header ; handler++ ) {
+ if ( strcasecmp ( header, handler->header ) == 0 ) {
+ if ( ( rc = handler->rx ( http, value ) ) != 0 )
+ goto err;
+ break;
+ }
+ }
+ return;
+
+ err:
+ DBGC ( http, "HTTP %p bad header\n", http );
+ http_done ( http, rc );
+ return;
}
/**
- * Close an HTTP connection
+ * Handle new data arriving via HTTP connection in the data phase
*
- * @v app a TCP Application
- * @v status connection status at close
+ * @v http HTTP request
+ * @v data New data
+ * @v len Length of new data
*/
-static void http_closed ( struct tcp_application *app, int status ) {
- struct http_request *http = tcp_to_http ( app );
- async_done ( &http->aop, status );
+static void http_rx_data ( struct http_request *http,
+ const char *data, size_t len ) {
+ int rc;
+
+ /* Fill data buffer */
+ if ( ( rc = fill_buffer ( http->buffer, data,
+ http->buffer->fill, len ) ) != 0 ) {
+ DBGC ( http, "HTTP %p failed to fill data buffer: %s\n",
+ http, strerror ( rc ) );
+ http_done ( http, rc );
+ return;
+ }
+
+ /* If we have reached the content-length, stop now */
+ if ( http->content_length &&
+ ( http->buffer->fill >= http->content_length ) ) {
+ http_done ( http, 0 );
+ }
}
/**
- * Callback after a TCP connection is established
+ * Handle new data arriving via HTTP connection
*
- * @v app a TCP Application
+ * @v http HTTP request
+ * @v data New data
+ * @v len Length of new data
*/
-static void http_connected ( struct tcp_application *app ) {
+static void http_newdata ( struct tcp_application *app,
+ void *data, size_t len ) {
struct http_request *http = tcp_to_http ( app );
+ const char *buf = data;
+ char *line;
+ int rc;
- http->state = HTTP_REQUEST_FILE;
+ while ( len ) {
+ if ( http->rx_state == HTTP_RX_DEAD ) {
+ /* Do no further processing */
+ return;
+ } else if ( http->rx_state == HTTP_RX_DATA ) {
+ /* Once we're into the data phase, just fill
+ * the data buffer
+ */
+ http_rx_data ( http, buf, len );
+ return;
+ } else {
+ /* In the other phases, buffer and process a
+ * line at a time
+ */
+ if ( ( rc = line_buffer ( &http->linebuf, &buf,
+ &len ) ) != 0 ) {
+ DBGC ( http, "HTTP %p could not buffer line: "
+ "%s\n", http, strerror ( rc ) );
+ http_done ( http, rc );
+ return;
+ }
+ if ( ( line = buffered_line ( &http->linebuf ) ) ) {
+ switch ( http->rx_state ) {
+ case HTTP_RX_RESPONSE:
+ http_rx_response ( http, line );
+ break;
+ case HTTP_RX_HEADER:
+ http_rx_header ( http, line );
+ break;
+ default:
+ assert ( 0 );
+ break;
+ }
+ }
+ }
+ }
}
/**
- * Callback for when TCP data is acknowledged
+ * Send HTTP data
*
- * @v app a TCP Application
- * @v len the length of data acked
+ * @v app TCP application
+ * @v buf Temporary data buffer
+ * @v len Length of temporary data buffer
*/
-static void http_acked ( struct tcp_application *app, size_t len __attribute__ ((unused)) ) {
+static void http_senddata ( struct tcp_application *app,
+ void *buf, size_t len ) {
struct http_request *http = tcp_to_http ( app );
- // assume that the whole GET request was sent in on epacket
-
- switch ( http->state ) {
- case HTTP_REQUEST_FILE:
- http->state = HTTP_PARSE_HEADER;
- break;
- case HTTP_PARSE_HEADER:
- case HTTP_RECV_FILE:
- break;
- case HTTP_DONE:
- //tcp_close(app);
- break;
- default:
- break;
- }
- //printf("acked\n");
+ len = snprintf ( buf, len,
+ "GET /%s HTTP/1.1\r\n"
+ "User-Agent: gPXE/" VERSION "\r\n"
+ "Host: %s\r\n"
+ "\r\n", http->filename, http->hostname );
+ tcp_send ( app, ( buf + http->tx_offset ), ( len - http->tx_offset ) );
}
/**
- * Callback when new TCP data is recieved
+ * HTTP data acknowledged
*
- * @v app a TCP Application
- * @v data a pointer to the data recieved
- * @v len length of data buffer
+ * @v app TCP application
+ * @v len Length of acknowledged data
*/
-static void http_newdata ( struct tcp_application *app, void *data,
- size_t len ) {
+static void http_acked ( struct tcp_application *app, size_t len ) {
struct http_request *http = tcp_to_http ( app );
- char *content_length;
- char *start = data;
- char *rcp; int rc;
-
- switch ( http->state ) {
- case HTTP_PARSE_HEADER:
- if(strncmp("HTTP/",data,5) != 0){
- // no http header
- printf("Error: no HTTP Header\n");
- }
- // if rc is not 200, then handle problem
- // either redirect or not there
- rcp = strstr(data,"HTTP");
- if(rcp == NULL){ printf("Could not find header status line.\n"); }
- rcp += 9;
- rc = strtoul(rcp,NULL,10);
- printf("RC=%d\n",rc);
- content_length = strstr(data,"Content-Length: ");
- if(content_length != NULL){
- content_length += 16;
- http->file_size = strtoul(content_length,NULL,10);
- http->file_recv = 0;
- printf("http->file_size = %d\n", http->file_size);
- }
- start = strstr(data,"\r\n\r\n");
- if(start == NULL){ printf("No end of header\n"); }
- else{
- start += 4;
- len -= ((void *)start - data);
- http->state = HTTP_RECV_FILE;
- }
- if ( http->state != HTTP_RECV_FILE )
- break;
- case HTTP_RECV_FILE:
- http->callback(http,start,len);
- //http->file_size -= len;
- //printf("File recv is %d\n", http->file_recv);
- if ( http->file_recv == http->file_size ){
- http->state = HTTP_DONE;
- tcp_close(app);
- }
- break;
- case HTTP_REQUEST_FILE:
- case HTTP_DONE:
- default:
- break;
- }
+ http->tx_offset += len;
}
/**
- * Callback for sending TCP data
+ * HTTP connection closed by network stack
*
- * @v app a TCP Application
+ * @v app TCP application
*/
-static void http_senddata ( struct tcp_application *app, void *buf, size_t len ) {
+static void http_closed ( struct tcp_application *app, int rc ) {
struct http_request *http = tcp_to_http ( app );
- switch ( http->state ){
- case HTTP_REQUEST_FILE:
- len = snprintf(buf,len,"GET %s HTTP/1.0\r\n\r\n",http->filename);
- printf("%s\n",(char *)buf);
- // string is: GET <file> HTTP/1.0\r\n\r\n
-
- tcp_send ( app, buf, len);
- break;
- case HTTP_PARSE_HEADER:
- case HTTP_RECV_FILE:
- break;
- case HTTP_DONE:
- //tcp_close(app)
- break;
- default:
- break;
- }
+ DBGC ( http, "HTTP %p connection closed: %s\n",
+ http, strerror ( rc ) );
+
+ http_done ( http, rc );
}
+/** HTTP TCP operations */
static struct tcp_operations http_tcp_operations = {
.closed = http_closed,
- .connected = http_connected,
.acked = http_acked,
.newdata = http_newdata,
.senddata = http_senddata,
*
* @v http a HTTP request
*/
-struct async_operation * get_http ( struct http_request *http ) {
+struct async_operation * http_get ( struct http_request *http ) {
int rc;
http->tcp.tcp_op = &http_tcp_operations;
- http->state = HTTP_REQUEST_FILE;
if ( ( rc = tcp_connect ( &http->tcp, &http->server, 0 ) ) != 0 )
async_done ( &http->aop, rc );