|
@@ -0,0 +1,886 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
14
|
+ * You should have received a copy of the GNU General Public License
|
|
15
|
+ * along with this program; if not, write to the Free Software
|
|
16
|
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/**
|
|
22
|
+ * @file
|
|
23
|
+ *
|
|
24
|
+ * Hyper Text Transfer Protocol (HTTP) core functionality
|
|
25
|
+ *
|
|
26
|
+ */
|
|
27
|
+
|
|
28
|
+#include <stdint.h>
|
|
29
|
+#include <stdlib.h>
|
|
30
|
+#include <stdio.h>
|
|
31
|
+#include <string.h>
|
|
32
|
+#include <strings.h>
|
|
33
|
+#include <byteswap.h>
|
|
34
|
+#include <errno.h>
|
|
35
|
+#include <assert.h>
|
|
36
|
+#include <ipxe/uri.h>
|
|
37
|
+#include <ipxe/refcnt.h>
|
|
38
|
+#include <ipxe/iobuf.h>
|
|
39
|
+#include <ipxe/xfer.h>
|
|
40
|
+#include <ipxe/open.h>
|
|
41
|
+#include <ipxe/socket.h>
|
|
42
|
+#include <ipxe/tcpip.h>
|
|
43
|
+#include <ipxe/process.h>
|
|
44
|
+#include <ipxe/linebuf.h>
|
|
45
|
+#include <ipxe/base64.h>
|
|
46
|
+#include <ipxe/blockdev.h>
|
|
47
|
+#include <ipxe/acpi.h>
|
|
48
|
+#include <ipxe/http.h>
|
|
49
|
+
|
|
50
|
+/** Block size used for HTTP block device request */
|
|
51
|
+#define HTTP_BLKSIZE 512
|
|
52
|
+
|
|
53
|
+/** HTTP flags */
|
|
54
|
+enum http_flags {
|
|
55
|
+ /** Request is waiting to be transmitted */
|
|
56
|
+ HTTP_TX_PENDING = 0x0001,
|
|
57
|
+ /** Fetch header only */
|
|
58
|
+ HTTP_HEAD_ONLY = 0x0002,
|
|
59
|
+ /** Keep connection alive */
|
|
60
|
+ HTTP_KEEPALIVE = 0x0004,
|
|
61
|
+};
|
|
62
|
+
|
|
63
|
+/** HTTP receive state */
|
|
64
|
+enum http_rx_state {
|
|
65
|
+ HTTP_RX_RESPONSE = 0,
|
|
66
|
+ HTTP_RX_HEADER,
|
|
67
|
+ HTTP_RX_CHUNK_LEN,
|
|
68
|
+ HTTP_RX_DATA,
|
|
69
|
+ HTTP_RX_TRAILER,
|
|
70
|
+ HTTP_RX_IDLE,
|
|
71
|
+ HTTP_RX_DEAD,
|
|
72
|
+};
|
|
73
|
+
|
|
74
|
+/**
|
|
75
|
+ * An HTTP request
|
|
76
|
+ *
|
|
77
|
+ */
|
|
78
|
+struct http_request {
|
|
79
|
+ /** Reference count */
|
|
80
|
+ struct refcnt refcnt;
|
|
81
|
+ /** Data transfer interface */
|
|
82
|
+ struct interface xfer;
|
|
83
|
+ /** Partial transfer interface */
|
|
84
|
+ struct interface partial;
|
|
85
|
+
|
|
86
|
+ /** URI being fetched */
|
|
87
|
+ struct uri *uri;
|
|
88
|
+ /** Transport layer interface */
|
|
89
|
+ struct interface socket;
|
|
90
|
+
|
|
91
|
+ /** Flags */
|
|
92
|
+ unsigned int flags;
|
|
93
|
+ /** Starting offset of partial transfer (if applicable) */
|
|
94
|
+ size_t partial_start;
|
|
95
|
+ /** Length of partial transfer (if applicable) */
|
|
96
|
+ size_t partial_len;
|
|
97
|
+
|
|
98
|
+ /** TX process */
|
|
99
|
+ struct process process;
|
|
100
|
+
|
|
101
|
+ /** RX state */
|
|
102
|
+ enum http_rx_state rx_state;
|
|
103
|
+ /** Received length */
|
|
104
|
+ size_t rx_len;
|
|
105
|
+ /** Length remaining (or 0 if unknown) */
|
|
106
|
+ size_t remaining;
|
|
107
|
+ /** HTTP is using Transfer-Encoding: chunked */
|
|
108
|
+ int chunked;
|
|
109
|
+ /** Current chunk length remaining (if applicable) */
|
|
110
|
+ size_t chunk_remaining;
|
|
111
|
+ /** Line buffer for received header lines */
|
|
112
|
+ struct line_buffer linebuf;
|
|
113
|
+ /** Receive data buffer (if applicable) */
|
|
114
|
+ userptr_t rx_buffer;
|
|
115
|
+};
|
|
116
|
+
|
|
117
|
+/**
|
|
118
|
+ * Free HTTP request
|
|
119
|
+ *
|
|
120
|
+ * @v refcnt Reference counter
|
|
121
|
+ */
|
|
122
|
+static void http_free ( struct refcnt *refcnt ) {
|
|
123
|
+ struct http_request *http =
|
|
124
|
+ container_of ( refcnt, struct http_request, refcnt );
|
|
125
|
+
|
|
126
|
+ uri_put ( http->uri );
|
|
127
|
+ empty_line_buffer ( &http->linebuf );
|
|
128
|
+ free ( http );
|
|
129
|
+};
|
|
130
|
+
|
|
131
|
+/**
|
|
132
|
+ * Close HTTP request
|
|
133
|
+ *
|
|
134
|
+ * @v http HTTP request
|
|
135
|
+ * @v rc Return status code
|
|
136
|
+ */
|
|
137
|
+static void http_close ( struct http_request *http, int rc ) {
|
|
138
|
+
|
|
139
|
+ /* Prevent further processing of any current packet */
|
|
140
|
+ http->rx_state = HTTP_RX_DEAD;
|
|
141
|
+
|
|
142
|
+ /* If we had a Content-Length, and the received content length
|
|
143
|
+ * isn't correct, flag an error
|
|
144
|
+ */
|
|
145
|
+ if ( http->remaining != 0 ) {
|
|
146
|
+ DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
|
|
147
|
+ http, http->rx_len, ( http->rx_len + http->remaining ) );
|
|
148
|
+ if ( rc == 0 )
|
|
149
|
+ rc = -EIO;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ /* Remove process */
|
|
153
|
+ process_del ( &http->process );
|
|
154
|
+
|
|
155
|
+ /* Close all data transfer interfaces */
|
|
156
|
+ intf_shutdown ( &http->socket, rc );
|
|
157
|
+ intf_shutdown ( &http->partial, rc );
|
|
158
|
+ intf_shutdown ( &http->xfer, rc );
|
|
159
|
+}
|
|
160
|
+
|
|
161
|
+/**
|
|
162
|
+ * Mark HTTP request as completed successfully
|
|
163
|
+ *
|
|
164
|
+ * @v http HTTP request
|
|
165
|
+ */
|
|
166
|
+static void http_done ( struct http_request *http ) {
|
|
167
|
+
|
|
168
|
+ /* If we had a Content-Length, and the received content length
|
|
169
|
+ * isn't correct, force an error
|
|
170
|
+ */
|
|
171
|
+ if ( http->remaining != 0 ) {
|
|
172
|
+ http_close ( http, -EIO );
|
|
173
|
+ return;
|
|
174
|
+ }
|
|
175
|
+
|
|
176
|
+ /* Enter idle state */
|
|
177
|
+ http->rx_state = HTTP_RX_IDLE;
|
|
178
|
+ http->rx_len = 0;
|
|
179
|
+ assert ( http->remaining == 0 );
|
|
180
|
+ assert ( http->chunked == 0 );
|
|
181
|
+ assert ( http->chunk_remaining == 0 );
|
|
182
|
+
|
|
183
|
+ /* Close partial transfer interface */
|
|
184
|
+ intf_restart ( &http->partial, 0 );
|
|
185
|
+
|
|
186
|
+ /* Close everything unless we are keeping the connection alive */
|
|
187
|
+ if ( ! ( http->flags & HTTP_KEEPALIVE ) )
|
|
188
|
+ http_close ( http, 0 );
|
|
189
|
+}
|
|
190
|
+
|
|
191
|
+/**
|
|
192
|
+ * Convert HTTP response code to return status code
|
|
193
|
+ *
|
|
194
|
+ * @v response HTTP response code
|
|
195
|
+ * @ret rc Return status code
|
|
196
|
+ */
|
|
197
|
+static int http_response_to_rc ( unsigned int response ) {
|
|
198
|
+ switch ( response ) {
|
|
199
|
+ case 200:
|
|
200
|
+ case 206:
|
|
201
|
+ case 301:
|
|
202
|
+ case 302:
|
|
203
|
+ return 0;
|
|
204
|
+ case 404:
|
|
205
|
+ return -ENOENT;
|
|
206
|
+ case 403:
|
|
207
|
+ return -EPERM;
|
|
208
|
+ case 401:
|
|
209
|
+ return -EACCES;
|
|
210
|
+ default:
|
|
211
|
+ return -EIO;
|
|
212
|
+ }
|
|
213
|
+}
|
|
214
|
+
|
|
215
|
+/**
|
|
216
|
+ * Handle HTTP response
|
|
217
|
+ *
|
|
218
|
+ * @v http HTTP request
|
|
219
|
+ * @v response HTTP response
|
|
220
|
+ * @ret rc Return status code
|
|
221
|
+ */
|
|
222
|
+static int http_rx_response ( struct http_request *http, char *response ) {
|
|
223
|
+ char *spc;
|
|
224
|
+ unsigned int code;
|
|
225
|
+ int rc;
|
|
226
|
+
|
|
227
|
+ DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
|
|
228
|
+
|
|
229
|
+ /* Check response starts with "HTTP/" */
|
|
230
|
+ if ( strncmp ( response, "HTTP/", 5 ) != 0 )
|
|
231
|
+ return -EIO;
|
|
232
|
+
|
|
233
|
+ /* Locate and check response code */
|
|
234
|
+ spc = strchr ( response, ' ' );
|
|
235
|
+ if ( ! spc )
|
|
236
|
+ return -EIO;
|
|
237
|
+ code = strtoul ( spc, NULL, 10 );
|
|
238
|
+ if ( ( rc = http_response_to_rc ( code ) ) != 0 )
|
|
239
|
+ return rc;
|
|
240
|
+
|
|
241
|
+ /* Move to received headers */
|
|
242
|
+ http->rx_state = HTTP_RX_HEADER;
|
|
243
|
+ return 0;
|
|
244
|
+}
|
|
245
|
+
|
|
246
|
+/**
|
|
247
|
+ * Handle HTTP Location header
|
|
248
|
+ *
|
|
249
|
+ * @v http HTTP request
|
|
250
|
+ * @v value HTTP header value
|
|
251
|
+ * @ret rc Return status code
|
|
252
|
+ */
|
|
253
|
+static int http_rx_location ( struct http_request *http, const char *value ) {
|
|
254
|
+ int rc;
|
|
255
|
+
|
|
256
|
+ /* Redirect to new location */
|
|
257
|
+ DBGC ( http, "HTTP %p redirecting to %s\n", http, value );
|
|
258
|
+ if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI_STRING,
|
|
259
|
+ value ) ) != 0 ) {
|
|
260
|
+ DBGC ( http, "HTTP %p could not redirect: %s\n",
|
|
261
|
+ http, strerror ( rc ) );
|
|
262
|
+ return rc;
|
|
263
|
+ }
|
|
264
|
+
|
|
265
|
+ return 0;
|
|
266
|
+}
|
|
267
|
+
|
|
268
|
+/**
|
|
269
|
+ * Handle HTTP Content-Length header
|
|
270
|
+ *
|
|
271
|
+ * @v http HTTP request
|
|
272
|
+ * @v value HTTP header value
|
|
273
|
+ * @ret rc Return status code
|
|
274
|
+ */
|
|
275
|
+static int http_rx_content_length ( struct http_request *http,
|
|
276
|
+ const char *value ) {
|
|
277
|
+ struct block_device_capacity capacity;
|
|
278
|
+ size_t content_len;
|
|
279
|
+ char *endp;
|
|
280
|
+
|
|
281
|
+ /* Parse content length */
|
|
282
|
+ content_len = strtoul ( value, &endp, 10 );
|
|
283
|
+ if ( *endp != '\0' ) {
|
|
284
|
+ DBGC ( http, "HTTP %p invalid Content-Length \"%s\"\n",
|
|
285
|
+ http, value );
|
|
286
|
+ return -EIO;
|
|
287
|
+ }
|
|
288
|
+
|
|
289
|
+ /* If we already have an expected content length, and this
|
|
290
|
+ * isn't it, then complain
|
|
291
|
+ */
|
|
292
|
+ if ( http->remaining && ( http->remaining != content_len ) ) {
|
|
293
|
+ DBGC ( http, "HTTP %p incorrect Content-Length %zd (expected "
|
|
294
|
+ "%zd)\n", http, content_len, http->remaining );
|
|
295
|
+ return -EIO;
|
|
296
|
+ }
|
|
297
|
+ if ( ! ( http->flags & HTTP_HEAD_ONLY ) )
|
|
298
|
+ http->remaining = content_len;
|
|
299
|
+
|
|
300
|
+ /* Use seek() to notify recipient of filesize */
|
|
301
|
+ xfer_seek ( &http->xfer, http->remaining );
|
|
302
|
+ xfer_seek ( &http->xfer, 0 );
|
|
303
|
+
|
|
304
|
+ /* Report block device capacity if applicable */
|
|
305
|
+ if ( http->flags & HTTP_HEAD_ONLY ) {
|
|
306
|
+ capacity.blocks = ( content_len / HTTP_BLKSIZE );
|
|
307
|
+ capacity.blksize = HTTP_BLKSIZE;
|
|
308
|
+ capacity.max_count = -1U;
|
|
309
|
+ block_capacity ( &http->partial, &capacity );
|
|
310
|
+ }
|
|
311
|
+ return 0;
|
|
312
|
+}
|
|
313
|
+
|
|
314
|
+/**
|
|
315
|
+ * Handle HTTP Transfer-Encoding header
|
|
316
|
+ *
|
|
317
|
+ * @v http HTTP request
|
|
318
|
+ * @v value HTTP header value
|
|
319
|
+ * @ret rc Return status code
|
|
320
|
+ */
|
|
321
|
+static int http_rx_transfer_encoding ( struct http_request *http,
|
|
322
|
+ const char *value ) {
|
|
323
|
+
|
|
324
|
+ if ( strcmp ( value, "chunked" ) == 0 ) {
|
|
325
|
+ /* Mark connection as using chunked transfer encoding */
|
|
326
|
+ http->chunked = 1;
|
|
327
|
+ }
|
|
328
|
+
|
|
329
|
+ return 0;
|
|
330
|
+}
|
|
331
|
+
|
|
332
|
+/** An HTTP header handler */
|
|
333
|
+struct http_header_handler {
|
|
334
|
+ /** Name (e.g. "Content-Length") */
|
|
335
|
+ const char *header;
|
|
336
|
+ /** Handle received header
|
|
337
|
+ *
|
|
338
|
+ * @v http HTTP request
|
|
339
|
+ * @v value HTTP header value
|
|
340
|
+ * @ret rc Return status code
|
|
341
|
+ *
|
|
342
|
+ * If an error is returned, the download will be aborted.
|
|
343
|
+ */
|
|
344
|
+ int ( * rx ) ( struct http_request *http, const char *value );
|
|
345
|
+};
|
|
346
|
+
|
|
347
|
+/** List of HTTP header handlers */
|
|
348
|
+static struct http_header_handler http_header_handlers[] = {
|
|
349
|
+ {
|
|
350
|
+ .header = "Location",
|
|
351
|
+ .rx = http_rx_location,
|
|
352
|
+ },
|
|
353
|
+ {
|
|
354
|
+ .header = "Content-Length",
|
|
355
|
+ .rx = http_rx_content_length,
|
|
356
|
+ },
|
|
357
|
+ {
|
|
358
|
+ .header = "Transfer-Encoding",
|
|
359
|
+ .rx = http_rx_transfer_encoding,
|
|
360
|
+ },
|
|
361
|
+ { NULL, NULL }
|
|
362
|
+};
|
|
363
|
+
|
|
364
|
+/**
|
|
365
|
+ * Handle HTTP header
|
|
366
|
+ *
|
|
367
|
+ * @v http HTTP request
|
|
368
|
+ * @v header HTTP header
|
|
369
|
+ * @ret rc Return status code
|
|
370
|
+ */
|
|
371
|
+static int http_rx_header ( struct http_request *http, char *header ) {
|
|
372
|
+ struct http_header_handler *handler;
|
|
373
|
+ char *separator;
|
|
374
|
+ char *value;
|
|
375
|
+ int rc;
|
|
376
|
+
|
|
377
|
+ /* An empty header line marks the end of this phase */
|
|
378
|
+ if ( ! header[0] ) {
|
|
379
|
+ empty_line_buffer ( &http->linebuf );
|
|
380
|
+ if ( ( http->rx_state == HTTP_RX_HEADER ) &&
|
|
381
|
+ ( ! ( http->flags & HTTP_HEAD_ONLY ) ) ) {
|
|
382
|
+ DBGC ( http, "HTTP %p start of data\n", http );
|
|
383
|
+ http->rx_state = ( http->chunked ?
|
|
384
|
+ HTTP_RX_CHUNK_LEN : HTTP_RX_DATA );
|
|
385
|
+ return 0;
|
|
386
|
+ } else {
|
|
387
|
+ DBGC ( http, "HTTP %p end of trailer\n", http );
|
|
388
|
+ http_done ( http );
|
|
389
|
+ return 0;
|
|
390
|
+ }
|
|
391
|
+ }
|
|
392
|
+
|
|
393
|
+ DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
|
|
394
|
+
|
|
395
|
+ /* Split header at the ": " */
|
|
396
|
+ separator = strstr ( header, ": " );
|
|
397
|
+ if ( ! separator ) {
|
|
398
|
+ DBGC ( http, "HTTP %p malformed header\n", http );
|
|
399
|
+ return -EIO;
|
|
400
|
+ }
|
|
401
|
+ *separator = '\0';
|
|
402
|
+ value = ( separator + 2 );
|
|
403
|
+
|
|
404
|
+ /* Hand off to header handler, if one exists */
|
|
405
|
+ for ( handler = http_header_handlers ; handler->header ; handler++ ) {
|
|
406
|
+ if ( strcasecmp ( header, handler->header ) == 0 ) {
|
|
407
|
+ if ( ( rc = handler->rx ( http, value ) ) != 0 )
|
|
408
|
+ return rc;
|
|
409
|
+ break;
|
|
410
|
+ }
|
|
411
|
+ }
|
|
412
|
+ return 0;
|
|
413
|
+}
|
|
414
|
+
|
|
415
|
+/**
|
|
416
|
+ * Handle HTTP chunk length
|
|
417
|
+ *
|
|
418
|
+ * @v http HTTP request
|
|
419
|
+ * @v length HTTP chunk length
|
|
420
|
+ * @ret rc Return status code
|
|
421
|
+ */
|
|
422
|
+static int http_rx_chunk_len ( struct http_request *http, char *length ) {
|
|
423
|
+ char *endp;
|
|
424
|
+
|
|
425
|
+ /* Skip blank lines between chunks */
|
|
426
|
+ if ( length[0] == '\0' )
|
|
427
|
+ return 0;
|
|
428
|
+
|
|
429
|
+ /* Parse chunk length */
|
|
430
|
+ http->chunk_remaining = strtoul ( length, &endp, 16 );
|
|
431
|
+ if ( *endp != '\0' ) {
|
|
432
|
+ DBGC ( http, "HTTP %p invalid chunk length \"%s\"\n",
|
|
433
|
+ http, length );
|
|
434
|
+ return -EIO;
|
|
435
|
+ }
|
|
436
|
+
|
|
437
|
+ /* Terminate chunked encoding if applicable */
|
|
438
|
+ if ( http->chunk_remaining == 0 ) {
|
|
439
|
+ DBGC ( http, "HTTP %p end of chunks\n", http );
|
|
440
|
+ http->chunked = 0;
|
|
441
|
+ http->rx_state = HTTP_RX_TRAILER;
|
|
442
|
+ return 0;
|
|
443
|
+ }
|
|
444
|
+
|
|
445
|
+ /* Use seek() to notify recipient of new filesize */
|
|
446
|
+ DBGC ( http, "HTTP %p start of chunk of length %zd\n",
|
|
447
|
+ http, http->chunk_remaining );
|
|
448
|
+ xfer_seek ( &http->xfer, ( http->rx_len + http->chunk_remaining ) );
|
|
449
|
+ xfer_seek ( &http->xfer, http->rx_len );
|
|
450
|
+
|
|
451
|
+ /* Start receiving data */
|
|
452
|
+ http->rx_state = HTTP_RX_DATA;
|
|
453
|
+
|
|
454
|
+ return 0;
|
|
455
|
+}
|
|
456
|
+
|
|
457
|
+/** An HTTP line-based data handler */
|
|
458
|
+struct http_line_handler {
|
|
459
|
+ /** Handle line
|
|
460
|
+ *
|
|
461
|
+ * @v http HTTP request
|
|
462
|
+ * @v line Line to handle
|
|
463
|
+ * @ret rc Return status code
|
|
464
|
+ */
|
|
465
|
+ int ( * rx ) ( struct http_request *http, char *line );
|
|
466
|
+};
|
|
467
|
+
|
|
468
|
+/** List of HTTP line-based data handlers */
|
|
469
|
+static struct http_line_handler http_line_handlers[] = {
|
|
470
|
+ [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
|
|
471
|
+ [HTTP_RX_HEADER] = { .rx = http_rx_header },
|
|
472
|
+ [HTTP_RX_CHUNK_LEN] = { .rx = http_rx_chunk_len },
|
|
473
|
+ [HTTP_RX_TRAILER] = { .rx = http_rx_header },
|
|
474
|
+};
|
|
475
|
+
|
|
476
|
+/**
|
|
477
|
+ * Handle new data arriving via HTTP connection
|
|
478
|
+ *
|
|
479
|
+ * @v http HTTP request
|
|
480
|
+ * @v iobuf I/O buffer
|
|
481
|
+ * @v meta Data transfer metadata
|
|
482
|
+ * @ret rc Return status code
|
|
483
|
+ */
|
|
484
|
+static int http_socket_deliver ( struct http_request *http,
|
|
485
|
+ struct io_buffer *iobuf,
|
|
486
|
+ struct xfer_metadata *meta __unused ) {
|
|
487
|
+ struct http_line_handler *lh;
|
|
488
|
+ char *line;
|
|
489
|
+ size_t data_len;
|
|
490
|
+ ssize_t line_len;
|
|
491
|
+ int rc = 0;
|
|
492
|
+
|
|
493
|
+ while ( iobuf && iob_len ( iobuf ) ) {
|
|
494
|
+
|
|
495
|
+ switch ( http->rx_state ) {
|
|
496
|
+ case HTTP_RX_IDLE:
|
|
497
|
+ /* Receiving any data in this state is an error */
|
|
498
|
+ DBGC ( http, "HTTP %p received %zd bytes while %s\n",
|
|
499
|
+ http, iob_len ( iobuf ),
|
|
500
|
+ ( ( http->rx_state == HTTP_RX_IDLE ) ?
|
|
501
|
+ "idle" : "dead" ) );
|
|
502
|
+ rc = -EPROTO;
|
|
503
|
+ goto done;
|
|
504
|
+ case HTTP_RX_DEAD:
|
|
505
|
+ /* Do no further processing */
|
|
506
|
+ goto done;
|
|
507
|
+ case HTTP_RX_DATA:
|
|
508
|
+ /* Pass received data to caller */
|
|
509
|
+ data_len = iob_len ( iobuf );
|
|
510
|
+ if ( http->chunk_remaining &&
|
|
511
|
+ ( http->chunk_remaining < data_len ) ) {
|
|
512
|
+ data_len = http->chunk_remaining;
|
|
513
|
+ }
|
|
514
|
+ if ( http->remaining &&
|
|
515
|
+ ( http->remaining < data_len ) ) {
|
|
516
|
+ data_len = http->remaining;
|
|
517
|
+ }
|
|
518
|
+ if ( http->rx_buffer != UNULL ) {
|
|
519
|
+ /* Copy to partial transfer buffer */
|
|
520
|
+ copy_to_user ( http->rx_buffer, http->rx_len,
|
|
521
|
+ iobuf->data, data_len );
|
|
522
|
+ iob_pull ( iobuf, data_len );
|
|
523
|
+ } else if ( data_len < iob_len ( iobuf ) ) {
|
|
524
|
+ /* Deliver partial buffer as raw data */
|
|
525
|
+ rc = xfer_deliver_raw ( &http->xfer,
|
|
526
|
+ iobuf->data, data_len );
|
|
527
|
+ iob_pull ( iobuf, data_len );
|
|
528
|
+ if ( rc != 0 )
|
|
529
|
+ goto done;
|
|
530
|
+ } else {
|
|
531
|
+ /* Deliver whole I/O buffer */
|
|
532
|
+ if ( ( rc = xfer_deliver_iob ( &http->xfer,
|
|
533
|
+ iob_disown ( iobuf ) ) ) != 0 )
|
|
534
|
+ goto done;
|
|
535
|
+ }
|
|
536
|
+ http->rx_len += data_len;
|
|
537
|
+ if ( http->chunk_remaining ) {
|
|
538
|
+ http->chunk_remaining -= data_len;
|
|
539
|
+ if ( http->chunk_remaining == 0 )
|
|
540
|
+ http->rx_state = HTTP_RX_CHUNK_LEN;
|
|
541
|
+ }
|
|
542
|
+ if ( http->remaining ) {
|
|
543
|
+ http->remaining -= data_len;
|
|
544
|
+ if ( ( http->remaining == 0 ) &&
|
|
545
|
+ ( http->rx_state == HTTP_RX_DATA ) ) {
|
|
546
|
+ http_done ( http );
|
|
547
|
+ }
|
|
548
|
+ }
|
|
549
|
+ break;
|
|
550
|
+ case HTTP_RX_RESPONSE:
|
|
551
|
+ case HTTP_RX_HEADER:
|
|
552
|
+ case HTTP_RX_CHUNK_LEN:
|
|
553
|
+ case HTTP_RX_TRAILER:
|
|
554
|
+ /* In the other phases, buffer and process a
|
|
555
|
+ * line at a time
|
|
556
|
+ */
|
|
557
|
+ line_len = line_buffer ( &http->linebuf, iobuf->data,
|
|
558
|
+ iob_len ( iobuf ) );
|
|
559
|
+ if ( line_len < 0 ) {
|
|
560
|
+ rc = line_len;
|
|
561
|
+ DBGC ( http, "HTTP %p could not buffer line: "
|
|
562
|
+ "%s\n", http, strerror ( rc ) );
|
|
563
|
+ goto done;
|
|
564
|
+ }
|
|
565
|
+ iob_pull ( iobuf, line_len );
|
|
566
|
+ line = buffered_line ( &http->linebuf );
|
|
567
|
+ if ( line ) {
|
|
568
|
+ lh = &http_line_handlers[http->rx_state];
|
|
569
|
+ if ( ( rc = lh->rx ( http, line ) ) != 0 )
|
|
570
|
+ goto done;
|
|
571
|
+ }
|
|
572
|
+ break;
|
|
573
|
+ default:
|
|
574
|
+ assert ( 0 );
|
|
575
|
+ break;
|
|
576
|
+ }
|
|
577
|
+ }
|
|
578
|
+
|
|
579
|
+ done:
|
|
580
|
+ if ( rc )
|
|
581
|
+ http_close ( http, rc );
|
|
582
|
+ free_iob ( iobuf );
|
|
583
|
+ return rc;
|
|
584
|
+}
|
|
585
|
+
|
|
586
|
+/**
|
|
587
|
+ * Check HTTP socket flow control window
|
|
588
|
+ *
|
|
589
|
+ * @v http HTTP request
|
|
590
|
+ * @ret len Length of window
|
|
591
|
+ */
|
|
592
|
+static size_t http_socket_window ( struct http_request *http __unused ) {
|
|
593
|
+
|
|
594
|
+ /* Window is always open. This is to prevent TCP from
|
|
595
|
+ * stalling if our parent window is not currently open.
|
|
596
|
+ */
|
|
597
|
+ return ( ~( ( size_t ) 0 ) );
|
|
598
|
+}
|
|
599
|
+
|
|
600
|
+/**
|
|
601
|
+ * HTTP process
|
|
602
|
+ *
|
|
603
|
+ * @v http HTTP request
|
|
604
|
+ */
|
|
605
|
+static void http_step ( struct http_request *http ) {
|
|
606
|
+ const char *host = http->uri->host;
|
|
607
|
+ const char *user = http->uri->user;
|
|
608
|
+ const char *password =
|
|
609
|
+ ( http->uri->password ? http->uri->password : "" );
|
|
610
|
+ size_t user_pw_len = ( user ? ( strlen ( user ) + 1 /* ":" */ +
|
|
611
|
+ strlen ( password ) ) : 0 );
|
|
612
|
+ size_t user_pw_base64_len = base64_encoded_len ( user_pw_len );
|
|
613
|
+ uint8_t user_pw[ user_pw_len + 1 /* NUL */ ];
|
|
614
|
+ char user_pw_base64[ user_pw_base64_len + 1 /* NUL */ ];
|
|
615
|
+ int rc;
|
|
616
|
+ int request_len = unparse_uri ( NULL, 0, http->uri,
|
|
617
|
+ URI_PATH_BIT | URI_QUERY_BIT );
|
|
618
|
+ char request[ request_len + 1 /* NUL */ ];
|
|
619
|
+ char range[48]; /* Enough for two 64-bit integers in decimal */
|
|
620
|
+ int partial;
|
|
621
|
+
|
|
622
|
+ /* Do nothing if we have already transmitted the request */
|
|
623
|
+ if ( ! ( http->flags & HTTP_TX_PENDING ) )
|
|
624
|
+ return;
|
|
625
|
+
|
|
626
|
+ /* Do nothing until socket is ready */
|
|
627
|
+ if ( ! xfer_window ( &http->socket ) )
|
|
628
|
+ return;
|
|
629
|
+
|
|
630
|
+ /* Construct path?query request */
|
|
631
|
+ unparse_uri ( request, sizeof ( request ), http->uri,
|
|
632
|
+ URI_PATH_BIT | URI_QUERY_BIT );
|
|
633
|
+
|
|
634
|
+ /* Construct authorisation, if applicable */
|
|
635
|
+ if ( user ) {
|
|
636
|
+ /* Make "user:password" string from decoded fields */
|
|
637
|
+ snprintf ( ( ( char * ) user_pw ), sizeof ( user_pw ),
|
|
638
|
+ "%s:%s", user, password );
|
|
639
|
+
|
|
640
|
+ /* Base64-encode the "user:password" string */
|
|
641
|
+ base64_encode ( user_pw, user_pw_len, user_pw_base64 );
|
|
642
|
+ }
|
|
643
|
+
|
|
644
|
+ /* Force a HEAD request if we have nowhere to send any received data */
|
|
645
|
+ if ( ( xfer_window ( &http->xfer ) == 0 ) &&
|
|
646
|
+ ( http->rx_buffer == UNULL ) ) {
|
|
647
|
+ http->flags |= ( HTTP_HEAD_ONLY | HTTP_KEEPALIVE );
|
|
648
|
+ }
|
|
649
|
+
|
|
650
|
+ /* Determine type of request */
|
|
651
|
+ partial = ( http->partial_len != 0 );
|
|
652
|
+ snprintf ( range, sizeof ( range ), "%zd-%zd", http->partial_start,
|
|
653
|
+ ( http->partial_start + http->partial_len - 1 ) );
|
|
654
|
+
|
|
655
|
+ /* Mark request as transmitted */
|
|
656
|
+ http->flags &= ~HTTP_TX_PENDING;
|
|
657
|
+
|
|
658
|
+ /* Send GET request */
|
|
659
|
+ if ( ( rc = xfer_printf ( &http->socket,
|
|
660
|
+ "%s %s%s HTTP/1.1\r\n"
|
|
661
|
+ "User-Agent: iPXE/" VERSION "\r\n"
|
|
662
|
+ "Host: %s%s%s\r\n"
|
|
663
|
+ "%s%s%s%s%s%s%s"
|
|
664
|
+ "\r\n",
|
|
665
|
+ ( ( http->flags & HTTP_HEAD_ONLY ) ?
|
|
666
|
+ "HEAD" : "GET" ),
|
|
667
|
+ ( http->uri->path ? "" : "/" ),
|
|
668
|
+ request, host,
|
|
669
|
+ ( http->uri->port ?
|
|
670
|
+ ":" : "" ),
|
|
671
|
+ ( http->uri->port ?
|
|
672
|
+ http->uri->port : "" ),
|
|
673
|
+ ( ( http->flags & HTTP_KEEPALIVE ) ?
|
|
674
|
+ "Connection: Keep-Alive\r\n" : "" ),
|
|
675
|
+ ( partial ? "Range: bytes=" : "" ),
|
|
676
|
+ ( partial ? range : "" ),
|
|
677
|
+ ( partial ? "\r\n" : "" ),
|
|
678
|
+ ( user ?
|
|
679
|
+ "Authorization: Basic " : "" ),
|
|
680
|
+ ( user ? user_pw_base64 : "" ),
|
|
681
|
+ ( user ? "\r\n" : "" ) ) ) != 0 ) {
|
|
682
|
+ http_close ( http, rc );
|
|
683
|
+ }
|
|
684
|
+}
|
|
685
|
+
|
|
686
|
+/**
|
|
687
|
+ * Check HTTP data transfer flow control window
|
|
688
|
+ *
|
|
689
|
+ * @v http HTTP request
|
|
690
|
+ * @ret len Length of window
|
|
691
|
+ */
|
|
692
|
+static size_t http_xfer_window ( struct http_request *http ) {
|
|
693
|
+
|
|
694
|
+ /* New block commands may be issued only when we are idle */
|
|
695
|
+ return ( ( http->rx_state == HTTP_RX_IDLE ) ? 1 : 0 );
|
|
696
|
+}
|
|
697
|
+
|
|
698
|
+/**
|
|
699
|
+ * Initiate HTTP partial read
|
|
700
|
+ *
|
|
701
|
+ * @v http HTTP request
|
|
702
|
+ * @v partial Partial transfer interface
|
|
703
|
+ * @v offset Starting offset
|
|
704
|
+ * @v buffer Data buffer
|
|
705
|
+ * @v len Length
|
|
706
|
+ * @ret rc Return status code
|
|
707
|
+ */
|
|
708
|
+static int http_partial_read ( struct http_request *http,
|
|
709
|
+ struct interface *partial,
|
|
710
|
+ size_t offset, userptr_t buffer, size_t len ) {
|
|
711
|
+
|
|
712
|
+ /* Sanity check */
|
|
713
|
+ if ( http_xfer_window ( http ) == 0 )
|
|
714
|
+ return -EBUSY;
|
|
715
|
+
|
|
716
|
+ /* Initialise partial transfer parameters */
|
|
717
|
+ http->rx_buffer = buffer;
|
|
718
|
+ http->partial_start = offset;
|
|
719
|
+ http->partial_len = len;
|
|
720
|
+ http->remaining = len;
|
|
721
|
+
|
|
722
|
+ /* Schedule request */
|
|
723
|
+ http->rx_state = HTTP_RX_RESPONSE;
|
|
724
|
+ http->flags = ( HTTP_TX_PENDING | HTTP_KEEPALIVE );
|
|
725
|
+ if ( ! len )
|
|
726
|
+ http->flags |= HTTP_HEAD_ONLY;
|
|
727
|
+ process_add ( &http->process );
|
|
728
|
+
|
|
729
|
+ /* Attach to parent interface and return */
|
|
730
|
+ intf_plug_plug ( &http->partial, partial );
|
|
731
|
+
|
|
732
|
+ return 0;
|
|
733
|
+}
|
|
734
|
+
|
|
735
|
+/**
|
|
736
|
+ * Issue HTTP block device read
|
|
737
|
+ *
|
|
738
|
+ * @v http HTTP request
|
|
739
|
+ * @v block Block data interface
|
|
740
|
+ * @v lba Starting logical block address
|
|
741
|
+ * @v count Number of blocks to transfer
|
|
742
|
+ * @v buffer Data buffer
|
|
743
|
+ * @v len Length of data buffer
|
|
744
|
+ * @ret rc Return status code
|
|
745
|
+ */
|
|
746
|
+static int http_block_read ( struct http_request *http,
|
|
747
|
+ struct interface *block,
|
|
748
|
+ uint64_t lba, unsigned int count,
|
|
749
|
+ userptr_t buffer, size_t len __unused ) {
|
|
750
|
+
|
|
751
|
+ return http_partial_read ( http, block, ( lba * HTTP_BLKSIZE ),
|
|
752
|
+ buffer, ( count * HTTP_BLKSIZE ) );
|
|
753
|
+}
|
|
754
|
+
|
|
755
|
+/**
|
|
756
|
+ * Read HTTP block device capacity
|
|
757
|
+ *
|
|
758
|
+ * @v http HTTP request
|
|
759
|
+ * @v block Block data interface
|
|
760
|
+ * @ret rc Return status code
|
|
761
|
+ */
|
|
762
|
+static int http_block_read_capacity ( struct http_request *http,
|
|
763
|
+ struct interface *block ) {
|
|
764
|
+
|
|
765
|
+ return http_partial_read ( http, block, 0, 0, 0 );
|
|
766
|
+}
|
|
767
|
+
|
|
768
|
+/**
|
|
769
|
+ * Describe HTTP device in an ACPI table
|
|
770
|
+ *
|
|
771
|
+ * @v http HTTP request
|
|
772
|
+ * @v acpi ACPI table
|
|
773
|
+ * @v len Length of ACPI table
|
|
774
|
+ * @ret rc Return status code
|
|
775
|
+ */
|
|
776
|
+static int http_acpi_describe ( struct http_request *http,
|
|
777
|
+ struct acpi_description_header *acpi,
|
|
778
|
+ size_t len ) {
|
|
779
|
+
|
|
780
|
+ DBGC ( http, "HTTP %p cannot yet describe device in an ACPI table\n",
|
|
781
|
+ http );
|
|
782
|
+ ( void ) acpi;
|
|
783
|
+ ( void ) len;
|
|
784
|
+ return 0;
|
|
785
|
+}
|
|
786
|
+
|
|
787
|
+/** HTTP socket interface operations */
|
|
788
|
+static struct interface_operation http_socket_operations[] = {
|
|
789
|
+ INTF_OP ( xfer_window, struct http_request *, http_socket_window ),
|
|
790
|
+ INTF_OP ( xfer_deliver, struct http_request *, http_socket_deliver ),
|
|
791
|
+ INTF_OP ( xfer_window_changed, struct http_request *, http_step ),
|
|
792
|
+ INTF_OP ( intf_close, struct http_request *, http_close ),
|
|
793
|
+};
|
|
794
|
+
|
|
795
|
+/** HTTP socket interface descriptor */
|
|
796
|
+static struct interface_descriptor http_socket_desc =
|
|
797
|
+ INTF_DESC_PASSTHRU ( struct http_request, socket,
|
|
798
|
+ http_socket_operations, xfer );
|
|
799
|
+
|
|
800
|
+/** HTTP partial transfer interface operations */
|
|
801
|
+static struct interface_operation http_partial_operations[] = {
|
|
802
|
+ INTF_OP ( intf_close, struct http_request *, http_close ),
|
|
803
|
+};
|
|
804
|
+
|
|
805
|
+/** HTTP partial transfer interface descriptor */
|
|
806
|
+static struct interface_descriptor http_partial_desc =
|
|
807
|
+ INTF_DESC ( struct http_request, partial, http_partial_operations );
|
|
808
|
+
|
|
809
|
+/** HTTP data transfer interface operations */
|
|
810
|
+static struct interface_operation http_xfer_operations[] = {
|
|
811
|
+ INTF_OP ( xfer_window, struct http_request *, http_xfer_window ),
|
|
812
|
+ INTF_OP ( block_read, struct http_request *, http_block_read ),
|
|
813
|
+ INTF_OP ( block_read_capacity, struct http_request *,
|
|
814
|
+ http_block_read_capacity ),
|
|
815
|
+ INTF_OP ( intf_close, struct http_request *, http_close ),
|
|
816
|
+ INTF_OP ( acpi_describe, struct http_request *, http_acpi_describe ),
|
|
817
|
+};
|
|
818
|
+
|
|
819
|
+/** HTTP data transfer interface descriptor */
|
|
820
|
+static struct interface_descriptor http_xfer_desc =
|
|
821
|
+ INTF_DESC_PASSTHRU ( struct http_request, xfer,
|
|
822
|
+ http_xfer_operations, socket );
|
|
823
|
+
|
|
824
|
+/** HTTP process descriptor */
|
|
825
|
+static struct process_descriptor http_process_desc =
|
|
826
|
+ PROC_DESC_ONCE ( struct http_request, process, http_step );
|
|
827
|
+
|
|
828
|
+/**
|
|
829
|
+ * Initiate an HTTP connection, with optional filter
|
|
830
|
+ *
|
|
831
|
+ * @v xfer Data transfer interface
|
|
832
|
+ * @v uri Uniform Resource Identifier
|
|
833
|
+ * @v default_port Default port number
|
|
834
|
+ * @v filter Filter to apply to socket, or NULL
|
|
835
|
+ * @ret rc Return status code
|
|
836
|
+ */
|
|
837
|
+int http_open_filter ( struct interface *xfer, struct uri *uri,
|
|
838
|
+ unsigned int default_port,
|
|
839
|
+ int ( * filter ) ( struct interface *xfer,
|
|
840
|
+ struct interface **next ) ) {
|
|
841
|
+ struct http_request *http;
|
|
842
|
+ struct sockaddr_tcpip server;
|
|
843
|
+ struct interface *socket;
|
|
844
|
+ int rc;
|
|
845
|
+
|
|
846
|
+ /* Sanity checks */
|
|
847
|
+ if ( ! uri->host )
|
|
848
|
+ return -EINVAL;
|
|
849
|
+
|
|
850
|
+ /* Allocate and populate HTTP structure */
|
|
851
|
+ http = zalloc ( sizeof ( *http ) );
|
|
852
|
+ if ( ! http )
|
|
853
|
+ return -ENOMEM;
|
|
854
|
+ ref_init ( &http->refcnt, http_free );
|
|
855
|
+ intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
|
|
856
|
+ intf_init ( &http->partial, &http_partial_desc, &http->refcnt );
|
|
857
|
+ http->uri = uri_get ( uri );
|
|
858
|
+ intf_init ( &http->socket, &http_socket_desc, &http->refcnt );
|
|
859
|
+ process_init ( &http->process, &http_process_desc, &http->refcnt );
|
|
860
|
+ http->flags = HTTP_TX_PENDING;
|
|
861
|
+
|
|
862
|
+ /* Open socket */
|
|
863
|
+ memset ( &server, 0, sizeof ( server ) );
|
|
864
|
+ server.st_port = htons ( uri_port ( http->uri, default_port ) );
|
|
865
|
+ socket = &http->socket;
|
|
866
|
+ if ( filter ) {
|
|
867
|
+ if ( ( rc = filter ( socket, &socket ) ) != 0 )
|
|
868
|
+ goto err;
|
|
869
|
+ }
|
|
870
|
+ if ( ( rc = xfer_open_named_socket ( socket, SOCK_STREAM,
|
|
871
|
+ ( struct sockaddr * ) &server,
|
|
872
|
+ uri->host, NULL ) ) != 0 )
|
|
873
|
+ goto err;
|
|
874
|
+
|
|
875
|
+ /* Attach to parent interface, mortalise self, and return */
|
|
876
|
+ intf_plug_plug ( &http->xfer, xfer );
|
|
877
|
+ ref_put ( &http->refcnt );
|
|
878
|
+ return 0;
|
|
879
|
+
|
|
880
|
+ err:
|
|
881
|
+ DBGC ( http, "HTTP %p could not create request: %s\n",
|
|
882
|
+ http, strerror ( rc ) );
|
|
883
|
+ http_close ( http, rc );
|
|
884
|
+ ref_put ( &http->refcnt );
|
|
885
|
+ return rc;
|
|
886
|
+}
|