|
@@ -57,6 +57,9 @@ FILE_LICENCE ( GPL2_OR_LATER );
|
57
|
57
|
#define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
|
58
|
58
|
#define EINFO_EIO_CONTENT_LENGTH \
|
59
|
59
|
__einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
|
|
60
|
+#define EIO_CHUNK __einfo_error ( EINFO_EIO_CHUNK )
|
|
61
|
+#define EINFO_EIO_CHUNK \
|
|
62
|
+ __einfo_uniqify ( EINFO_EIO, 0x03, "Terminated mid-chunk" )
|
60
|
63
|
#define EINVAL_RESPONSE __einfo_error ( EINFO_EINVAL_RESPONSE )
|
61
|
64
|
#define EINFO_EINVAL_RESPONSE \
|
62
|
65
|
__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid content length" )
|
|
@@ -88,8 +91,10 @@ enum http_flags {
|
88
|
91
|
HTTP_TX_PENDING = 0x0001,
|
89
|
92
|
/** Fetch header only */
|
90
|
93
|
HTTP_HEAD_ONLY = 0x0002,
|
91
|
|
- /** Keep connection alive */
|
92
|
|
- HTTP_KEEPALIVE = 0x0004,
|
|
94
|
+ /** Client would like to keep connection alive */
|
|
95
|
+ HTTP_CLIENT_KEEPALIVE = 0x0004,
|
|
96
|
+ /** Server will keep connection alive */
|
|
97
|
+ HTTP_SERVER_KEEPALIVE = 0x0008,
|
93
|
98
|
};
|
94
|
99
|
|
95
|
100
|
/** HTTP receive state */
|
|
@@ -117,6 +122,12 @@ struct http_request {
|
117
|
122
|
|
118
|
123
|
/** URI being fetched */
|
119
|
124
|
struct uri *uri;
|
|
125
|
+ /** Default port */
|
|
126
|
+ unsigned int default_port;
|
|
127
|
+ /** Filter (if any) */
|
|
128
|
+ int ( * filter ) ( struct interface *xfer,
|
|
129
|
+ const char *name,
|
|
130
|
+ struct interface **next );
|
120
|
131
|
/** Transport layer interface */
|
121
|
132
|
struct interface socket;
|
122
|
133
|
|
|
@@ -171,15 +182,8 @@ static void http_close ( struct http_request *http, int rc ) {
|
171
|
182
|
/* Prevent further processing of any current packet */
|
172
|
183
|
http->rx_state = HTTP_RX_DEAD;
|
173
|
184
|
|
174
|
|
- /* If we had a Content-Length, and the received content length
|
175
|
|
- * isn't correct, flag an error
|
176
|
|
- */
|
177
|
|
- if ( http->remaining != 0 ) {
|
178
|
|
- DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
|
179
|
|
- http, http->rx_len, ( http->rx_len + http->remaining ) );
|
180
|
|
- if ( rc == 0 )
|
181
|
|
- rc = -EIO_CONTENT_LENGTH;
|
182
|
|
- }
|
|
185
|
+ /* Prevent reconnection */
|
|
186
|
+ http->flags &= ~HTTP_CLIENT_KEEPALIVE;
|
183
|
187
|
|
184
|
188
|
/* Remove process */
|
185
|
189
|
process_del ( &http->process );
|
|
@@ -190,17 +194,55 @@ static void http_close ( struct http_request *http, int rc ) {
|
190
|
194
|
intf_shutdown ( &http->xfer, rc );
|
191
|
195
|
}
|
192
|
196
|
|
|
197
|
+/**
|
|
198
|
+ * Open HTTP socket
|
|
199
|
+ *
|
|
200
|
+ * @v http HTTP request
|
|
201
|
+ * @ret rc Return status code
|
|
202
|
+ */
|
|
203
|
+static int http_socket_open ( struct http_request *http ) {
|
|
204
|
+ struct uri *uri = http->uri;
|
|
205
|
+ struct sockaddr_tcpip server;
|
|
206
|
+ struct interface *socket;
|
|
207
|
+ int rc;
|
|
208
|
+
|
|
209
|
+ /* Open socket */
|
|
210
|
+ memset ( &server, 0, sizeof ( server ) );
|
|
211
|
+ server.st_port = htons ( uri_port ( uri, http->default_port ) );
|
|
212
|
+ socket = &http->socket;
|
|
213
|
+ if ( http->filter ) {
|
|
214
|
+ if ( ( rc = http->filter ( socket, uri->host, &socket ) ) != 0 )
|
|
215
|
+ return rc;
|
|
216
|
+ }
|
|
217
|
+ if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
|
|
218
|
+ ( struct sockaddr * ) &server,
|
|
219
|
+ uri->host, NULL ) ) != 0 )
|
|
220
|
+ return rc;
|
|
221
|
+
|
|
222
|
+ return 0;
|
|
223
|
+}
|
|
224
|
+
|
193
|
225
|
/**
|
194
|
226
|
* Mark HTTP request as completed successfully
|
195
|
227
|
*
|
196
|
228
|
* @v http HTTP request
|
197
|
229
|
*/
|
198
|
230
|
static void http_done ( struct http_request *http ) {
|
|
231
|
+ int rc;
|
|
232
|
+
|
|
233
|
+ /* If we are in the middle of a chunked transfer, force an error */
|
|
234
|
+ if ( http->chunked ) {
|
|
235
|
+ DBGC ( http, "HTTP %p terminated mid-chunk\n", http );
|
|
236
|
+ http_close ( http, -EIO_CHUNK );
|
|
237
|
+ return;
|
|
238
|
+ }
|
199
|
239
|
|
200
|
240
|
/* If we had a Content-Length, and the received content length
|
201
|
241
|
* isn't correct, force an error
|
202
|
242
|
*/
|
203
|
243
|
if ( http->remaining != 0 ) {
|
|
244
|
+ DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
|
|
245
|
+ http, http->rx_len, ( http->rx_len + http->remaining ) );
|
204
|
246
|
http_close ( http, -EIO_CONTENT_LENGTH );
|
205
|
247
|
return;
|
206
|
248
|
}
|
|
@@ -215,9 +257,24 @@ static void http_done ( struct http_request *http ) {
|
215
|
257
|
/* Close partial transfer interface */
|
216
|
258
|
intf_restart ( &http->partial, 0 );
|
217
|
259
|
|
218
|
|
- /* Close everything unless we are keeping the connection alive */
|
219
|
|
- if ( ! ( http->flags & HTTP_KEEPALIVE ) )
|
|
260
|
+ /* Close everything unless we want to keep the connection alive */
|
|
261
|
+ if ( ! ( http->flags & HTTP_CLIENT_KEEPALIVE ) ) {
|
220
|
262
|
http_close ( http, 0 );
|
|
263
|
+ return;
|
|
264
|
+ }
|
|
265
|
+
|
|
266
|
+ /* If the server is not intending to keep the connection
|
|
267
|
+ * alive, then reopen the socket.
|
|
268
|
+ */
|
|
269
|
+ if ( ! ( http->flags & HTTP_SERVER_KEEPALIVE ) ) {
|
|
270
|
+ DBGC ( http, "HTTP %p reopening connection\n", http );
|
|
271
|
+ intf_restart ( &http->socket, 0 );
|
|
272
|
+ if ( ( rc = http_socket_open ( http ) ) != 0 ) {
|
|
273
|
+ http_close ( http, rc );
|
|
274
|
+ return;
|
|
275
|
+ }
|
|
276
|
+ }
|
|
277
|
+ http->flags &= ~HTTP_SERVER_KEEPALIVE;
|
221
|
278
|
}
|
222
|
279
|
|
223
|
280
|
/**
|
|
@@ -354,7 +411,7 @@ static int http_rx_content_length ( struct http_request *http,
|
354
|
411
|
static int http_rx_transfer_encoding ( struct http_request *http,
|
355
|
412
|
const char *value ) {
|
356
|
413
|
|
357
|
|
- if ( strcmp ( value, "chunked" ) == 0 ) {
|
|
414
|
+ if ( strcasecmp ( value, "chunked" ) == 0 ) {
|
358
|
415
|
/* Mark connection as using chunked transfer encoding */
|
359
|
416
|
http->chunked = 1;
|
360
|
417
|
}
|
|
@@ -362,6 +419,23 @@ static int http_rx_transfer_encoding ( struct http_request *http,
|
362
|
419
|
return 0;
|
363
|
420
|
}
|
364
|
421
|
|
|
422
|
+/**
|
|
423
|
+ * Handle HTTP Connection header
|
|
424
|
+ *
|
|
425
|
+ * @v http HTTP request
|
|
426
|
+ * @v value HTTP header value
|
|
427
|
+ * @ret rc Return status code
|
|
428
|
+ */
|
|
429
|
+static int http_rx_connection ( struct http_request *http, const char *value ) {
|
|
430
|
+
|
|
431
|
+ if ( strcasecmp ( value, "keep-alive" ) == 0 ) {
|
|
432
|
+ /* Mark connection as being kept alive by the server */
|
|
433
|
+ http->flags |= HTTP_SERVER_KEEPALIVE;
|
|
434
|
+ }
|
|
435
|
+
|
|
436
|
+ return 0;
|
|
437
|
+}
|
|
438
|
+
|
365
|
439
|
/** An HTTP header handler */
|
366
|
440
|
struct http_header_handler {
|
367
|
441
|
/** Name (e.g. "Content-Length") */
|
|
@@ -391,6 +465,10 @@ static struct http_header_handler http_header_handlers[] = {
|
391
|
465
|
.header = "Transfer-Encoding",
|
392
|
466
|
.rx = http_rx_transfer_encoding,
|
393
|
467
|
},
|
|
468
|
+ {
|
|
469
|
+ .header = "Connection",
|
|
470
|
+ .rx = http_rx_connection,
|
|
471
|
+ },
|
394
|
472
|
{ NULL, NULL }
|
395
|
473
|
};
|
396
|
474
|
|
|
@@ -630,6 +708,24 @@ static size_t http_socket_window ( struct http_request *http __unused ) {
|
630
|
708
|
return ( ~( ( size_t ) 0 ) );
|
631
|
709
|
}
|
632
|
710
|
|
|
711
|
+/**
|
|
712
|
+ * Close HTTP socket
|
|
713
|
+ *
|
|
714
|
+ * @v http HTTP request
|
|
715
|
+ * @v rc Reason for close
|
|
716
|
+ */
|
|
717
|
+static void http_socket_close ( struct http_request *http, int rc ) {
|
|
718
|
+
|
|
719
|
+ /* If we have an error, terminate */
|
|
720
|
+ if ( rc != 0 ) {
|
|
721
|
+ http_close ( http, rc );
|
|
722
|
+ return;
|
|
723
|
+ }
|
|
724
|
+
|
|
725
|
+ /* Mark HTTP request as complete */
|
|
726
|
+ http_done ( http );
|
|
727
|
+}
|
|
728
|
+
|
633
|
729
|
/**
|
634
|
730
|
* HTTP process
|
635
|
731
|
*
|
|
@@ -688,7 +784,7 @@ static void http_step ( struct http_request *http ) {
|
688
|
784
|
/* Force a HEAD request if we have nowhere to send any received data */
|
689
|
785
|
if ( ( xfer_window ( &http->xfer ) == 0 ) &&
|
690
|
786
|
( http->rx_buffer == UNULL ) ) {
|
691
|
|
- http->flags |= ( HTTP_HEAD_ONLY | HTTP_KEEPALIVE );
|
|
787
|
+ http->flags |= ( HTTP_HEAD_ONLY | HTTP_CLIENT_KEEPALIVE );
|
692
|
788
|
}
|
693
|
789
|
|
694
|
790
|
/* Determine type of request */
|
|
@@ -715,8 +811,8 @@ static void http_step ( struct http_request *http ) {
|
715
|
811
|
":" : "" ),
|
716
|
812
|
( http->uri->port ?
|
717
|
813
|
http->uri->port : "" ),
|
718
|
|
- ( ( http->flags & HTTP_KEEPALIVE ) ?
|
719
|
|
- "Connection: Keep-Alive\r\n" : "" ),
|
|
814
|
+ ( ( http->flags & HTTP_CLIENT_KEEPALIVE ) ?
|
|
815
|
+ "Connection: keep-alive\r\n" : "" ),
|
720
|
816
|
( partial ? "Range: bytes=" : "" ),
|
721
|
817
|
( partial ? dynamic->range : "" ),
|
722
|
818
|
( partial ? "\r\n" : "" ),
|
|
@@ -772,7 +868,7 @@ static int http_partial_read ( struct http_request *http,
|
772
|
868
|
|
773
|
869
|
/* Schedule request */
|
774
|
870
|
http->rx_state = HTTP_RX_RESPONSE;
|
775
|
|
- http->flags = ( HTTP_TX_PENDING | HTTP_KEEPALIVE );
|
|
871
|
+ http->flags = ( HTTP_TX_PENDING | HTTP_CLIENT_KEEPALIVE );
|
776
|
872
|
if ( ! len )
|
777
|
873
|
http->flags |= HTTP_HEAD_ONLY;
|
778
|
874
|
process_add ( &http->process );
|
|
@@ -840,7 +936,7 @@ static struct interface_operation http_socket_operations[] = {
|
840
|
936
|
INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
|
841
|
937
|
INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
|
842
|
938
|
INTF_OP ( xfer_window_changed, struct http_request *, http_step ),
|
843
|
|
- INTF_OP ( intf_close, struct http_request *, http_close ),
|
|
939
|
+ INTF_OP ( intf_close, struct http_request *, http_socket_close ),
|
844
|
940
|
};
|
845
|
941
|
|
846
|
942
|
/** HTTP socket interface descriptor */
|
|
@@ -891,8 +987,6 @@ int http_open_filter ( struct interface *xfer, struct uri *uri,
|
891
|
987
|
const char *name,
|
892
|
988
|
struct interface **next ) ) {
|
893
|
989
|
struct http_request *http;
|
894
|
|
- struct sockaddr_tcpip server;
|
895
|
|
- struct interface *socket;
|
896
|
990
|
int rc;
|
897
|
991
|
|
898
|
992
|
/* Sanity checks */
|
|
@@ -907,21 +1001,14 @@ int http_open_filter ( struct interface *xfer, struct uri *uri,
|
907
|
1001
|
intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
|
908
|
1002
|
intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
|
909
|
1003
|
http->uri = uri_get ( uri );
|
|
1004
|
+ http->default_port = default_port;
|
|
1005
|
+ http->filter = filter;
|
910
|
1006
|
intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
|
911
|
1007
|
process_init ( &http->process, &http_process_desc, &http->refcnt );
|
912
|
1008
|
http->flags = HTTP_TX_PENDING;
|
913
|
1009
|
|
914
|
1010
|
/* Open socket */
|
915
|
|
- memset ( &server, 0, sizeof ( server ) );
|
916
|
|
- server.st_port = htons ( uri_port ( http->uri, default_port ) );
|
917
|
|
- socket = &http->socket;
|
918
|
|
- if ( filter ) {
|
919
|
|
- if ( ( rc = filter ( socket, uri->host, &socket ) ) != 0 )
|
920
|
|
- goto err;
|
921
|
|
- }
|
922
|
|
- if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
|
923
|
|
- ( struct sockaddr * ) &server,
|
924
|
|
- uri->host, NULL ) ) != 0 )
|
|
1011
|
+ if ( ( rc = http_socket_open ( http ) ) != 0 )
|
925
|
1012
|
goto err;
|
926
|
1013
|
|
927
|
1014
|
/* Attach to parent interface, mortalise self, and return */
|