Browse Source

[http] Reopen connections when server does not keep connection alive

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 12 years ago
parent
commit
8a5ba6733d
1 changed files with 118 additions and 31 deletions
  1. 118
    31
      src/net/tcp/httpcore.c

+ 118
- 31
src/net/tcp/httpcore.c View File

57
 #define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
57
 #define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
58
 #define EINFO_EIO_CONTENT_LENGTH \
58
 #define EINFO_EIO_CONTENT_LENGTH \
59
 	__einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
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
 #define EINVAL_RESPONSE __einfo_error ( EINFO_EINVAL_RESPONSE )
63
 #define EINVAL_RESPONSE __einfo_error ( EINFO_EINVAL_RESPONSE )
61
 #define EINFO_EINVAL_RESPONSE \
64
 #define EINFO_EINVAL_RESPONSE \
62
 	__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid content length" )
65
 	__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid content length" )
88
 	HTTP_TX_PENDING = 0x0001,
91
 	HTTP_TX_PENDING = 0x0001,
89
 	/** Fetch header only */
92
 	/** Fetch header only */
90
 	HTTP_HEAD_ONLY = 0x0002,
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
 /** HTTP receive state */
100
 /** HTTP receive state */
117
 
122
 
118
 	/** URI being fetched */
123
 	/** URI being fetched */
119
 	struct uri *uri;
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
 	/** Transport layer interface */
131
 	/** Transport layer interface */
121
 	struct interface socket;
132
 	struct interface socket;
122
 
133
 
171
 	/* Prevent further processing of any current packet */
182
 	/* Prevent further processing of any current packet */
172
 	http->rx_state = HTTP_RX_DEAD;
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
 	/* Remove process */
188
 	/* Remove process */
185
 	process_del ( &http->process );
189
 	process_del ( &http->process );
190
 	intf_shutdown ( &http->xfer, rc );
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
  * Mark HTTP request as completed successfully
226
  * Mark HTTP request as completed successfully
195
  *
227
  *
196
  * @v http		HTTP request
228
  * @v http		HTTP request
197
  */
229
  */
198
 static void http_done ( struct http_request *http ) {
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
 	/* If we had a Content-Length, and the received content length
240
 	/* If we had a Content-Length, and the received content length
201
 	 * isn't correct, force an error
241
 	 * isn't correct, force an error
202
 	 */
242
 	 */
203
 	if ( http->remaining != 0 ) {
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
 		http_close ( http, -EIO_CONTENT_LENGTH );
246
 		http_close ( http, -EIO_CONTENT_LENGTH );
205
 		return;
247
 		return;
206
 	}
248
 	}
215
 	/* Close partial transfer interface */
257
 	/* Close partial transfer interface */
216
 	intf_restart ( &http->partial, 0 );
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
 		http_close ( http, 0 );
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
 static int http_rx_transfer_encoding ( struct http_request *http,
411
 static int http_rx_transfer_encoding ( struct http_request *http,
355
 				       const char *value ) {
412
 				       const char *value ) {
356
 
413
 
357
-	if ( strcmp ( value, "chunked" ) == 0 ) {
414
+	if ( strcasecmp ( value, "chunked" ) == 0 ) {
358
 		/* Mark connection as using chunked transfer encoding */
415
 		/* Mark connection as using chunked transfer encoding */
359
 		http->chunked = 1;
416
 		http->chunked = 1;
360
 	}
417
 	}
362
 	return 0;
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
 /** An HTTP header handler */
439
 /** An HTTP header handler */
366
 struct http_header_handler {
440
 struct http_header_handler {
367
 	/** Name (e.g. "Content-Length") */
441
 	/** Name (e.g. "Content-Length") */
391
 		.header = "Transfer-Encoding",
465
 		.header = "Transfer-Encoding",
392
 		.rx = http_rx_transfer_encoding,
466
 		.rx = http_rx_transfer_encoding,
393
 	},
467
 	},
468
+	{
469
+		.header = "Connection",
470
+		.rx = http_rx_connection,
471
+	},
394
 	{ NULL, NULL }
472
 	{ NULL, NULL }
395
 };
473
 };
396
 
474
 
630
 	return ( ~( ( size_t ) 0 ) );
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
  * HTTP process
730
  * HTTP process
635
  *
731
  *
688
 	/* Force a HEAD request if we have nowhere to send any received data */
784
 	/* Force a HEAD request if we have nowhere to send any received data */
689
 	if ( ( xfer_window ( &http->xfer ) == 0 ) &&
785
 	if ( ( xfer_window ( &http->xfer ) == 0 ) &&
690
 	     ( http->rx_buffer == UNULL ) ) {
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
 	/* Determine type of request */
790
 	/* Determine type of request */
715
 				    ":" : "" ),
811
 				    ":" : "" ),
716
 				  ( http->uri->port ?
812
 				  ( http->uri->port ?
717
 				    http->uri->port : "" ),
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
 				  ( partial ? "Range: bytes=" : "" ),
816
 				  ( partial ? "Range: bytes=" : "" ),
721
 				  ( partial ? dynamic->range : "" ),
817
 				  ( partial ? dynamic->range : "" ),
722
 				  ( partial ? "\r\n" : "" ),
818
 				  ( partial ? "\r\n" : "" ),
772
 
868
 
773
 	/* Schedule request */
869
 	/* Schedule request */
774
 	http->rx_state = HTTP_RX_RESPONSE;
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
 	if ( ! len )
872
 	if ( ! len )
777
 		http->flags |= HTTP_HEAD_ONLY;
873
 		http->flags |= HTTP_HEAD_ONLY;
778
 	process_add ( &http->process );
874
 	process_add ( &http->process );
840
 	INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
936
 	INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
841
 	INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
937
 	INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
842
 	INTF_OP ( xfer_window_changed, struct http_request *, http_step ),
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
 /** HTTP socket interface descriptor */
942
 /** HTTP socket interface descriptor */
891
 					  const char *name,
987
 					  const char *name,
892
 					  struct interface **next ) ) {
988
 					  struct interface **next ) ) {
893
 	struct http_request *http;
989
 	struct http_request *http;
894
-	struct sockaddr_tcpip server;
895
-	struct interface *socket;
896
 	int rc;
990
 	int rc;
897
 
991
 
898
 	/* Sanity checks */
992
 	/* Sanity checks */
907
 	intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
1001
 	intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
908
 	intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
1002
 	intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
909
 	http->uri = uri_get ( uri );
1003
 	http->uri = uri_get ( uri );
1004
+	http->default_port = default_port;
1005
+	http->filter = filter;
910
 	intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
1006
 	intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
911
 	process_init ( &http->process, &http_process_desc, &http->refcnt );
1007
 	process_init ( &http->process, &http_process_desc, &http->refcnt );
912
 	http->flags = HTTP_TX_PENDING;
1008
 	http->flags = HTTP_TX_PENDING;
913
 
1009
 
914
 	/* Open socket */
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
 		goto err;
1012
 		goto err;
926
 
1013
 
927
 	/* Attach to parent interface, mortalise self, and return */
1014
 	/* Attach to parent interface, mortalise self, and return */

Loading…
Cancel
Save