-
Notifications
You must be signed in to change notification settings - Fork 642
/
httpcore.c
1968 lines (1685 loc) · 50.4 KB
/
httpcore.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2015 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., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*
* You can also choose to distribute this program under the terms of
* the Unmodified Binary Distribution Licence (as given in the file
* COPYING.UBDL), provided that you have satisfied its requirements.
*/
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
/**
* @file
*
* Hyper Text Transfer Protocol (HTTP) core functionality
*
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <byteswap.h>
#include <errno.h>
#include <ctype.h>
#include <assert.h>
#include <ipxe/uri.h>
#include <ipxe/refcnt.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/process.h>
#include <ipxe/retry.h>
#include <ipxe/timer.h>
#include <ipxe/linebuf.h>
#include <ipxe/xferbuf.h>
#include <ipxe/blockdev.h>
#include <ipxe/acpi.h>
#include <ipxe/version.h>
#include <ipxe/params.h>
#include <ipxe/profile.h>
#include <ipxe/vsprintf.h>
#include <ipxe/http.h>
/* Disambiguate the various error causes */
#define EACCES_401 __einfo_error ( EINFO_EACCES_401 )
#define EINFO_EACCES_401 \
__einfo_uniqify ( EINFO_EACCES, 0x01, "HTTP 401 Unauthorized" )
#define EINVAL_STATUS __einfo_error ( EINFO_EINVAL_STATUS )
#define EINFO_EINVAL_STATUS \
__einfo_uniqify ( EINFO_EINVAL, 0x01, "Invalid status line" )
#define EINVAL_HEADER __einfo_error ( EINFO_EINVAL_HEADER )
#define EINFO_EINVAL_HEADER \
__einfo_uniqify ( EINFO_EINVAL, 0x02, "Invalid header" )
#define EINVAL_CONTENT_LENGTH __einfo_error ( EINFO_EINVAL_CONTENT_LENGTH )
#define EINFO_EINVAL_CONTENT_LENGTH \
__einfo_uniqify ( EINFO_EINVAL, 0x03, "Invalid content length" )
#define EINVAL_CHUNK_LENGTH __einfo_error ( EINFO_EINVAL_CHUNK_LENGTH )
#define EINFO_EINVAL_CHUNK_LENGTH \
__einfo_uniqify ( EINFO_EINVAL, 0x04, "Invalid chunk length" )
#define EIO_OTHER __einfo_error ( EINFO_EIO_OTHER )
#define EINFO_EIO_OTHER \
__einfo_uniqify ( EINFO_EIO, 0x01, "Unrecognised HTTP response code" )
#define EIO_CONTENT_LENGTH __einfo_error ( EINFO_EIO_CONTENT_LENGTH )
#define EINFO_EIO_CONTENT_LENGTH \
__einfo_uniqify ( EINFO_EIO, 0x02, "Content length mismatch" )
#define EIO_4XX __einfo_error ( EINFO_EIO_4XX )
#define EINFO_EIO_4XX \
__einfo_uniqify ( EINFO_EIO, 0x04, "HTTP 4xx Client Error" )
#define EIO_5XX __einfo_error ( EINFO_EIO_5XX )
#define EINFO_EIO_5XX \
__einfo_uniqify ( EINFO_EIO, 0x05, "HTTP 5xx Server Error" )
#define ENOENT_404 __einfo_error ( EINFO_ENOENT_404 )
#define EINFO_ENOENT_404 \
__einfo_uniqify ( EINFO_ENOENT, 0x01, "HTTP 404 Not Found" )
#define ENOTSUP_CONNECTION __einfo_error ( EINFO_ENOTSUP_CONNECTION )
#define EINFO_ENOTSUP_CONNECTION \
__einfo_uniqify ( EINFO_ENOTSUP, 0x01, "Unsupported connection header" )
#define ENOTSUP_TRANSFER __einfo_error ( EINFO_ENOTSUP_TRANSFER )
#define EINFO_ENOTSUP_TRANSFER \
__einfo_uniqify ( EINFO_ENOTSUP, 0x02, "Unsupported transfer encoding" )
#define EPERM_403 __einfo_error ( EINFO_EPERM_403 )
#define EINFO_EPERM_403 \
__einfo_uniqify ( EINFO_EPERM, 0x01, "HTTP 403 Forbidden" )
#define EPROTO_UNSOLICITED __einfo_error ( EINFO_EPROTO_UNSOLICITED )
#define EINFO_EPROTO_UNSOLICITED \
__einfo_uniqify ( EINFO_EPROTO, 0x01, "Unsolicited data" )
/** Retry delay used when we cannot understand the Retry-After header */
#define HTTP_RETRY_SECONDS 5
/** Receive profiler */
static struct profiler http_rx_profiler __profiler = { .name = "http.rx" };
/** Data transfer profiler */
static struct profiler http_xfer_profiler __profiler = { .name = "http.xfer" };
static struct http_state http_request;
static struct http_state http_headers;
static struct http_state http_trailers;
static struct http_transfer_encoding http_transfer_identity;
/******************************************************************************
*
* Methods
*
******************************************************************************
*/
/** HTTP HEAD method */
struct http_method http_head = {
.name = "HEAD",
};
/** HTTP GET method */
struct http_method http_get = {
.name = "GET",
};
/** HTTP POST method */
struct http_method http_post = {
.name = "POST",
};
/******************************************************************************
*
* Utility functions
*
******************************************************************************
*/
/**
* Handle received HTTP line-buffered data
*
* @v http HTTP transaction
* @v iobuf I/O buffer
* @v linebuf Line buffer
* @ret rc Return status code
*/
static int http_rx_linebuf ( struct http_transaction *http,
struct io_buffer *iobuf,
struct line_buffer *linebuf ) {
int consumed;
int rc;
/* Buffer received line */
consumed = line_buffer ( linebuf, iobuf->data, iob_len ( iobuf ) );
if ( consumed < 0 ) {
rc = consumed;
DBGC ( http, "HTTP %p could not buffer line: %s\n",
http, strerror ( rc ) );
return rc;
}
/* Consume line */
iob_pull ( iobuf, consumed );
return 0;
}
/**
* Get HTTP response token
*
* @v line Line position
* @v value Token value to fill in (if any)
* @ret token Token, or NULL
*/
char * http_token ( char **line, char **value ) {
char *token;
char quote = '\0';
char c;
/* Avoid returning uninitialised data */
if ( value )
*value = NULL;
/* Skip any initial whitespace or commas */
while ( ( isspace ( **line ) ) || ( **line == ',' ) )
(*line)++;
/* Check for end of line and record token position */
if ( ! **line )
return NULL;
token = *line;
/* Scan for end of token */
while ( ( c = **line ) ) {
/* Terminate if we hit an unquoted whitespace or comma */
if ( ( isspace ( c ) || ( c == ',' ) ) && ! quote )
break;
/* Terminate if we hit a closing quote */
if ( c == quote )
break;
/* Check for value separator */
if ( value && ( ! *value ) && ( c == '=' ) ) {
/* Terminate key portion of token */
*((*line)++) = '\0';
/* Check for quote character */
c = **line;
if ( ( c == '"' ) || ( c == '\'' ) ) {
quote = c;
(*line)++;
}
/* Record value portion of token */
*value = *line;
} else {
/* Move to next character */
(*line)++;
}
}
/* Terminate token, if applicable */
if ( c )
*((*line)++) = '\0';
return token;
}
/******************************************************************************
*
* Transactions
*
******************************************************************************
*/
/**
* Free HTTP transaction
*
* @v refcnt Reference count
*/
static void http_free ( struct refcnt *refcnt ) {
struct http_transaction *http =
container_of ( refcnt, struct http_transaction, refcnt );
empty_line_buffer ( &http->response.headers );
empty_line_buffer ( &http->linebuf );
uri_put ( http->uri );
free ( http );
}
/**
* Close HTTP transaction
*
* @v http HTTP transaction
* @v rc Reason for close
*/
static void http_close ( struct http_transaction *http, int rc ) {
/* Stop process */
process_del ( &http->process );
/* Stop timer */
stop_timer ( &http->timer );
/* Close all interfaces, allowing for the fact that the
* content-decoded and transfer-decoded interfaces may be
* connected to the same object.
*/
intf_shutdown ( &http->conn, rc );
intf_nullify ( &http->transfer );
intf_shutdown ( &http->content, rc );
intf_shutdown ( &http->transfer, rc );
intf_shutdown ( &http->xfer, rc );
}
/**
* Close HTTP transaction with error (even if none specified)
*
* @v http HTTP transaction
* @v rc Reason for close
*/
static void http_close_error ( struct http_transaction *http, int rc ) {
/* Treat any close as an error */
http_close ( http, ( rc ? rc : -EPIPE ) );
}
/**
* Reopen stale HTTP connection
*
* @v http HTTP transaction
*/
static void http_reopen ( struct http_transaction *http ) {
int rc;
/* Close existing connection */
intf_restart ( &http->conn, -ECANCELED );
/* Reopen connection */
if ( ( rc = http_connect ( &http->conn, http->uri ) ) != 0 ) {
DBGC ( http, "HTTP %p could not reconnect: %s\n",
http, strerror ( rc ) );
goto err_connect;
}
/* Reset state */
http->state = &http_request;
/* Reschedule transmission process */
process_add ( &http->process );
return;
err_connect:
http_close ( http, rc );
}
/**
* Handle retry timer expiry
*
* @v timer Retry timer
* @v over Failure indicator
*/
static void http_expired ( struct retry_timer *timer, int over __unused ) {
struct http_transaction *http =
container_of ( timer, struct http_transaction, timer );
/* Reopen connection */
http_reopen ( http );
}
/**
* HTTP transmit process
*
* @v http HTTP transaction
*/
static void http_step ( struct http_transaction *http ) {
int rc;
/* Do nothing if we have nothing to transmit */
if ( ! http->state->tx )
return;
/* Do nothing until connection is ready */
if ( ! xfer_window ( &http->conn ) )
return;
/* Do nothing until data transfer interface is ready */
if ( ! xfer_window ( &http->xfer ) )
return;
/* Transmit data */
if ( ( rc = http->state->tx ( http ) ) != 0 )
goto err;
return;
err:
http_close ( http, rc );
}
/**
* Handle received HTTP data
*
* @v http HTTP transaction
* @v iobuf I/O buffer
* @v meta Transfer metadata
* @ret rc Return status code
*
* This function takes ownership of the I/O buffer.
*/
static int http_conn_deliver ( struct http_transaction *http,
struct io_buffer *iobuf,
struct xfer_metadata *meta __unused ) {
int rc;
/* Handle received data */
profile_start ( &http_rx_profiler );
while ( iobuf && iob_len ( iobuf ) ) {
/* Sanity check */
if ( ( ! http->state ) || ( ! http->state->rx ) ) {
DBGC ( http, "HTTP %p unexpected data\n", http );
rc = -EPROTO_UNSOLICITED;
goto err;
}
/* Receive (some) data */
if ( ( rc = http->state->rx ( http, &iobuf ) ) != 0 )
goto err;
}
/* Free I/O buffer, if applicable */
free_iob ( iobuf );
profile_stop ( &http_rx_profiler );
return 0;
err:
free_iob ( iobuf );
http_close ( http, rc );
return rc;
}
/**
* Handle server connection close
*
* @v http HTTP transaction
* @v rc Reason for close
*/
static void http_conn_close ( struct http_transaction *http, int rc ) {
/* Sanity checks */
assert ( http->state != NULL );
assert ( http->state->close != NULL );
/* Restart server connection interface */
intf_restart ( &http->conn, rc );
/* Hand off to state-specific method */
http->state->close ( http, rc );
}
/**
* Handle received content-decoded data
*
* @v http HTTP transaction
* @v iobuf I/O buffer
* @v meta Data transfer metadata
*/
static int http_content_deliver ( struct http_transaction *http,
struct io_buffer *iobuf,
struct xfer_metadata *meta ) {
int rc;
/* Ignore content if this is anything other than a successful
* transfer.
*/
if ( http->response.rc != 0 ) {
free_iob ( iobuf );
return 0;
}
/* Deliver to data transfer interface */
profile_start ( &http_xfer_profiler );
if ( ( rc = xfer_deliver ( &http->xfer, iob_disown ( iobuf ),
meta ) ) != 0 )
return rc;
profile_stop ( &http_xfer_profiler );
return 0;
}
/**
* Get underlying data transfer buffer
*
* @v http HTTP transaction
* @ret xferbuf Data transfer buffer, or NULL on error
*/
static struct xfer_buffer *
http_content_buffer ( struct http_transaction *http ) {
/* Deny access to the data transfer buffer if this is anything
* other than a successful transfer.
*/
if ( http->response.rc != 0 )
return NULL;
/* Hand off to data transfer interface */
return xfer_buffer ( &http->xfer );
}
/**
* Read from block device (when HTTP block device support is not present)
*
* @v http HTTP transaction
* @v data Data interface
* @v lba Starting logical block address
* @v count Number of logical blocks
* @v buffer Data buffer
* @v len Length of data buffer
* @ret rc Return status code
*/
__weak int http_block_read ( struct http_transaction *http __unused,
struct interface *data __unused,
uint64_t lba __unused, unsigned int count __unused,
userptr_t buffer __unused, size_t len __unused ) {
return -ENOTSUP;
}
/**
* Read block device capacity (when HTTP block device support is not present)
*
* @v control Control interface
* @v data Data interface
* @ret rc Return status code
*/
__weak int http_block_read_capacity ( struct http_transaction *http __unused,
struct interface *data __unused ) {
return -ENOTSUP;
}
/**
* Describe device in ACPI table (when HTTP block device support is not present)
*
* @v http HTTP transaction
* @v acpi ACPI table
* @v len Length of ACPI table
* @ret rc Return status code
*/
__weak int http_acpi_describe ( struct http_transaction *http __unused,
struct acpi_description_header *acpi __unused,
size_t len __unused ) {
return -ENOTSUP;
}
/** HTTP data transfer interface operations */
static struct interface_operation http_xfer_operations[] = {
INTF_OP ( block_read, struct http_transaction *, http_block_read ),
INTF_OP ( block_read_capacity, struct http_transaction *,
http_block_read_capacity ),
INTF_OP ( acpi_describe, struct http_transaction *,
http_acpi_describe ),
INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
INTF_OP ( intf_close, struct http_transaction *, http_close ),
};
/** HTTP data transfer interface descriptor */
static struct interface_descriptor http_xfer_desc =
INTF_DESC_PASSTHRU ( struct http_transaction, xfer,
http_xfer_operations, content );
/** HTTP content-decoded interface operations */
static struct interface_operation http_content_operations[] = {
INTF_OP ( xfer_deliver, struct http_transaction *,
http_content_deliver ),
INTF_OP ( xfer_buffer, struct http_transaction *, http_content_buffer ),
INTF_OP ( intf_close, struct http_transaction *, http_close ),
};
/** HTTP content-decoded interface descriptor */
static struct interface_descriptor http_content_desc =
INTF_DESC_PASSTHRU ( struct http_transaction, content,
http_content_operations, xfer );
/** HTTP transfer-decoded interface operations */
static struct interface_operation http_transfer_operations[] = {
INTF_OP ( intf_close, struct http_transaction *, http_close ),
};
/** HTTP transfer-decoded interface descriptor */
static struct interface_descriptor http_transfer_desc =
INTF_DESC_PASSTHRU ( struct http_transaction, transfer,
http_transfer_operations, conn );
/** HTTP server connection interface operations */
static struct interface_operation http_conn_operations[] = {
INTF_OP ( xfer_deliver, struct http_transaction *, http_conn_deliver ),
INTF_OP ( xfer_window_changed, struct http_transaction *, http_step ),
INTF_OP ( pool_reopen, struct http_transaction *, http_reopen ),
INTF_OP ( intf_close, struct http_transaction *, http_conn_close ),
};
/** HTTP server connection interface descriptor */
static struct interface_descriptor http_conn_desc =
INTF_DESC_PASSTHRU ( struct http_transaction, conn,
http_conn_operations, transfer );
/** HTTP process descriptor */
static struct process_descriptor http_process_desc =
PROC_DESC_ONCE ( struct http_transaction, process, http_step );
/**
* Open HTTP transaction
*
* @v xfer Data transfer interface
* @v method Request method
* @v uri Request URI
* @v range Content range (if any)
* @v content Request content (if any)
* @ret rc Return status code
*/
int http_open ( struct interface *xfer, struct http_method *method,
struct uri *uri, struct http_request_range *range,
struct http_request_content *content ) {
struct http_transaction *http;
struct uri request_uri;
struct uri request_host;
size_t request_uri_len;
size_t request_host_len;
size_t content_len;
char *request_uri_string;
char *request_host_string;
void *content_data;
int rc;
/* Calculate request URI length */
memset ( &request_uri, 0, sizeof ( request_uri ) );
request_uri.path = ( uri->path ? uri->path : "/" );
request_uri.query = uri->query;
request_uri_len =
( format_uri ( &request_uri, NULL, 0 ) + 1 /* NUL */);
/* Calculate host name length */
memset ( &request_host, 0, sizeof ( request_host ) );
request_host.host = uri->host;
request_host.port = uri->port;
request_host_len =
( format_uri ( &request_host, NULL, 0 ) + 1 /* NUL */ );
/* Calculate request content length */
content_len = ( content ? content->len : 0 );
/* Allocate and initialise structure */
http = zalloc ( sizeof ( *http ) + request_uri_len + request_host_len +
content_len );
if ( ! http ) {
rc = -ENOMEM;
goto err_alloc;
}
request_uri_string = ( ( ( void * ) http ) + sizeof ( *http ) );
request_host_string = ( request_uri_string + request_uri_len );
content_data = ( request_host_string + request_host_len );
format_uri ( &request_uri, request_uri_string, request_uri_len );
format_uri ( &request_host, request_host_string, request_host_len );
ref_init ( &http->refcnt, http_free );
intf_init ( &http->xfer, &http_xfer_desc, &http->refcnt );
intf_init ( &http->content, &http_content_desc, &http->refcnt );
intf_init ( &http->transfer, &http_transfer_desc, &http->refcnt );
intf_init ( &http->conn, &http_conn_desc, &http->refcnt );
intf_plug_plug ( &http->transfer, &http->content );
process_init ( &http->process, &http_process_desc, &http->refcnt );
timer_init ( &http->timer, http_expired, &http->refcnt );
http->uri = uri_get ( uri );
http->request.method = method;
http->request.uri = request_uri_string;
http->request.host = request_host_string;
if ( range ) {
memcpy ( &http->request.range, range,
sizeof ( http->request.range ) );
}
if ( content ) {
http->request.content.type = content->type;
http->request.content.data = content_data;
http->request.content.len = content_len;
memcpy ( content_data, content->data, content_len );
}
http->state = &http_request;
DBGC2 ( http, "HTTP %p %s://%s%s\n", http, http->uri->scheme,
http->request.host, http->request.uri );
/* Open connection */
if ( ( rc = http_connect ( &http->conn, uri ) ) != 0 ) {
DBGC ( http, "HTTP %p could not connect: %s\n",
http, strerror ( rc ) );
goto err_connect;
}
/* Attach to parent interface, mortalise self, and return */
intf_plug_plug ( &http->xfer, xfer );
ref_put ( &http->refcnt );
return 0;
err_connect:
http_close ( http, rc );
ref_put ( &http->refcnt );
err_alloc:
return rc;
}
/**
* Redirect HTTP transaction
*
* @v http HTTP transaction
* @v location New location
* @ret rc Return status code
*/
static int http_redirect ( struct http_transaction *http,
const char *location ) {
struct uri *location_uri;
struct uri *resolved_uri;
int rc;
DBGC2 ( http, "HTTP %p redirecting to \"%s\"\n", http, location );
/* Parse location URI */
location_uri = parse_uri ( location );
if ( ! location_uri ) {
rc = -ENOMEM;
goto err_parse_uri;
}
/* Resolve as relative to original URI */
resolved_uri = resolve_uri ( http->uri, location_uri );
if ( ! resolved_uri ) {
rc = -ENOMEM;
goto err_resolve_uri;
}
/* Redirect to new URI */
if ( ( rc = xfer_redirect ( &http->xfer, LOCATION_URI,
resolved_uri ) ) != 0 ) {
DBGC ( http, "HTTP %p could not redirect: %s\n",
http, strerror ( rc ) );
goto err_redirect;
}
err_redirect:
uri_put ( resolved_uri );
err_resolve_uri:
uri_put ( location_uri );
err_parse_uri:
return rc;
}
/**
* Handle successful transfer completion
*
* @v http HTTP transaction
* @ret rc Return status code
*/
static int http_transfer_complete ( struct http_transaction *http ) {
struct http_authentication *auth;
const char *location;
int rc;
/* Keep connection alive if applicable */
if ( http->response.flags & HTTP_RESPONSE_KEEPALIVE )
pool_recycle ( &http->conn );
/* Restart server connection interface */
intf_restart ( &http->conn, 0 );
/* No more data is expected */
http->state = NULL;
/* If transaction is successful, then close the
* transfer-decoded interface. The content encoding may
* choose whether or not to immediately terminate the
* transaction.
*/
if ( http->response.rc == 0 ) {
intf_shutdown ( &http->transfer, 0 );
return 0;
}
/* Perform redirection, if applicable */
if ( ( location = http->response.location ) ) {
if ( ( rc = http_redirect ( http, location ) ) != 0 )
return rc;
http_close ( http, 0 );
return 0;
}
/* Fail unless a retry is permitted */
if ( ! ( http->response.flags & HTTP_RESPONSE_RETRY ) )
return http->response.rc;
/* Perform authentication, if applicable */
if ( ( auth = http->response.auth.auth ) ) {
http->request.auth.auth = auth;
DBGC2 ( http, "HTTP %p performing %s authentication\n",
http, auth->name );
if ( ( rc = auth->authenticate ( http ) ) != 0 ) {
DBGC ( http, "HTTP %p could not authenticate: %s\n",
http, strerror ( rc ) );
return rc;
}
}
/* Restart content decoding interfaces (which may be attached
* to the same object).
*/
intf_nullify ( &http->transfer ); /* avoid potential loops */
intf_restart ( &http->content, http->response.rc );
intf_restart ( &http->transfer, http->response.rc );
intf_plug_plug ( &http->transfer, &http->content );
http->len = 0;
assert ( http->remaining == 0 );
/* Start timer to initiate retry */
DBGC2 ( http, "HTTP %p retrying after %d seconds\n",
http, http->response.retry_after );
start_timer_fixed ( &http->timer,
( http->response.retry_after * TICKS_PER_SEC ) );
return 0;
}
/******************************************************************************
*
* Requests
*
******************************************************************************
*/
/**
* Construct HTTP request headers
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length, or negative error
*/
static int http_format_headers ( struct http_transaction *http, char *buf,
size_t len ) {
struct http_request_header *header;
size_t used;
size_t remaining;
char *line;
int value_len;
int rc;
/* Construct request line */
used = ssnprintf ( buf, len, "%s %s HTTP/1.1",
http->request.method->name, http->request.uri );
if ( used < len )
DBGC2 ( http, "HTTP %p TX %s\n", http, buf );
used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
/* Construct all headers */
for_each_table_entry ( header, HTTP_REQUEST_HEADERS ) {
/* Determine header value length */
value_len = header->format ( http, NULL, 0 );
if ( value_len < 0 ) {
rc = value_len;
return rc;
}
/* Skip zero-length headers */
if ( ! value_len )
continue;
/* Construct header */
line = ( buf + used );
used += ssnprintf ( ( buf + used ), ( len - used ), "%s: ",
header->name );
remaining = ( ( used < len ) ? ( len - used ) : 0 );
used += header->format ( http, ( buf + used ), remaining );
if ( used < len )
DBGC2 ( http, "HTTP %p TX %s\n", http, line );
used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
}
/* Construct terminating newline */
used += ssnprintf ( ( buf + used ), ( len - used ), "\r\n" );
return used;
}
/**
* Construct HTTP "Host" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_host ( struct http_transaction *http, char *buf,
size_t len ) {
/* Construct host URI */
return snprintf ( buf, len, "%s", http->request.host );
}
/** HTTP "Host" header "*/
struct http_request_header http_request_host __http_request_header = {
.name = "Host",
.format = http_format_host,
};
/**
* Construct HTTP "User-Agent" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_user_agent ( struct http_transaction *http __unused,
char *buf, size_t len ) {
/* Construct user agent */
return snprintf ( buf, len, "iPXE/%s", product_version );
}
/** HTTP "User-Agent" header */
struct http_request_header http_request_user_agent __http_request_header = {
.name = "User-Agent",
.format = http_format_user_agent,
};
/**
* Construct HTTP "Connection" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_connection ( struct http_transaction *http __unused,
char *buf, size_t len ) {
/* Always request keep-alive */
return snprintf ( buf, len, "keep-alive" );
}
/** HTTP "Connection" header */
struct http_request_header http_request_connection __http_request_header = {
.name = "Connection",
.format = http_format_connection,
};
/**
* Construct HTTP "Range" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_range ( struct http_transaction *http,
char *buf, size_t len ) {
/* Construct range, if applicable */
if ( http->request.range.len ) {
return snprintf ( buf, len, "bytes=%zd-%zd",
http->request.range.start,
( http->request.range.start +
http->request.range.len - 1 ) );
} else {
return 0;
}
}
/** HTTP "Range" header */
struct http_request_header http_request_range __http_request_header = {
.name = "Range",
.format = http_format_range,
};
/**
* Construct HTTP "Content-Type" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_content_type ( struct http_transaction *http,
char *buf, size_t len ) {
/* Construct content type, if applicable */
if ( http->request.content.type ) {
return snprintf ( buf, len, "%s", http->request.content.type );
} else {
return 0;
}
}
/** HTTP "Content-Type" header */
struct http_request_header http_request_content_type __http_request_header = {
.name = "Content-Type",
.format = http_format_content_type,
};
/**
* Construct HTTP "Content-Length" header
*
* @v http HTTP transaction
* @v buf Buffer
* @v len Length of buffer
* @ret len Length of header value, or negative error
*/
static int http_format_content_length ( struct http_transaction *http,
char *buf, size_t len ) {
/* Construct content length, if applicable */
if ( http->request.content.len ) {
return snprintf ( buf, len, "%zd", http->request.content.len );
} else {
return 0;
}
}