iPXE
http.h
Go to the documentation of this file.
1 #ifndef _IPXE_HTTP_H
2 #define _IPXE_HTTP_H
3 
4 /** @file
5  *
6  * Hyper Text Transport Protocol
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 FILE_SECBOOT ( PERMITTED );
12 
13 #include <stdint.h>
14 #include <ipxe/refcnt.h>
15 #include <ipxe/interface.h>
16 #include <ipxe/iobuf.h>
17 #include <ipxe/process.h>
18 #include <ipxe/retry.h>
19 #include <ipxe/linebuf.h>
20 #include <ipxe/pool.h>
21 #include <ipxe/tables.h>
22 #include <ipxe/ntlm.h>
23 
24 struct http_transaction;
25 struct http_connection;
26 
27 /******************************************************************************
28  *
29  * HTTP URI schemes
30  *
31  ******************************************************************************
32  */
33 
34 /** HTTP default port */
35 #define HTTP_PORT 80
36 
37 /** HTTPS default port */
38 #define HTTPS_PORT 443
39 
40 /** An HTTP URI scheme */
41 struct http_scheme {
42  /** Scheme name (e.g. "http" or "https") */
43  const char *name;
44  /** Default port */
45  unsigned int port;
46  /** Transport-layer filter (if any)
47  *
48  * @v conn HTTP connection
49  * @ret rc Return status code
50  */
51  int ( * filter ) ( struct http_connection *conn );
52 };
53 
54 /** HTTP scheme table */
55 #define HTTP_SCHEMES __table ( struct http_scheme, "http_schemes" )
56 
57 /** Declare an HTTP scheme */
58 #define __http_scheme __table_entry ( HTTP_SCHEMES, 01 )
59 
60 /******************************************************************************
61  *
62  * Connections
63  *
64  ******************************************************************************
65  */
66 
67 /** An HTTP connection
68  *
69  * This represents a potentially reusable connection to an HTTP
70  * server.
71  */
73  /** Reference count */
74  struct refcnt refcnt;
75  /** Connection URI
76  *
77  * This encapsulates the server (and protocol) used for the
78  * connection. This may be the origin server or a proxy
79  * server.
80  */
81  struct uri *uri;
82  /** HTTP scheme */
84  /** Transport layer interface */
85  struct interface socket;
86  /** Data transfer interface */
87  struct interface xfer;
88  /** Pooled connection */
90 };
91 
92 /******************************************************************************
93  *
94  * HTTP methods
95  *
96  ******************************************************************************
97  */
98 
99 /** An HTTP method */
100 struct http_method {
101  /** Method name (e.g. "GET" or "POST") */
102  const char *name;
103 };
104 
105 extern struct http_method http_head;
106 extern struct http_method http_get;
107 extern struct http_method http_post;
108 
109 /******************************************************************************
110  *
111  * Requests
112  *
113  ******************************************************************************
114  */
115 
116 /** HTTP Digest authentication client nonce count
117  *
118  * We choose to generate a new client nonce each time.
119  */
120 #define HTTP_DIGEST_NC "00000001"
121 
122 /** HTTP Digest authentication client nonce length
123  *
124  * We choose to use a 32-bit hex client nonce.
125  */
126 #define HTTP_DIGEST_CNONCE_LEN 8
127 
128 /** HTTP Digest authentication response length
129  *
130  * The Digest authentication response is a Base16-encoded 16-byte MD5
131  * checksum.
132  */
133 #define HTTP_DIGEST_RESPONSE_LEN 32
134 
135 /** HTTP request range descriptor */
137  /** Range start */
138  size_t start;
139  /** Range length, or zero for no range request */
140  size_t len;
141 };
142 
143 /** HTTP request content descriptor */
145  /** Content type (if any) */
146  const char *type;
147  /** Content data (if any) */
148  const void *data;
149  /** Content length */
150  size_t len;
151 };
152 
153 /** HTTP request Basic authentication descriptor */
155  /** Username */
156  const char *username;
157  /** Password */
158  const char *password;
159 };
160 
161 /** HTTP request Digest authentication descriptor */
163  /** Username */
164  const char *username;
165  /** Quality of protection */
166  const char *qop;
167  /** Algorithm */
168  const char *algorithm;
169  /** Client nonce */
170  char cnonce[ HTTP_DIGEST_CNONCE_LEN + 1 /* NUL */ ];
171  /** Response */
172  char response[ HTTP_DIGEST_RESPONSE_LEN + 1 /* NUL */ ];
173 };
174 
175 /** HTTP request NTLM authentication descriptor */
177  /** Username */
178  const char *username;
179  /** LAN Manager response */
181  /** NT response */
183  /** Authenticate message length */
184  size_t len;
185 };
186 
187 /** HTTP request authentication descriptor */
189  /** Authentication scheme (if any) */
191  /** Per-scheme information */
192  union {
193  /** Basic authentication descriptor */
195  /** Digest authentication descriptor */
197  /** NTLM authentication descriptor */
199  };
200 };
201 
202 /** An HTTP request
203  *
204  * This represents a single request to be sent to a server, including
205  * the values required to construct all headers.
206  *
207  * Pointers within this structure must point to storage which is
208  * guaranteed to remain valid for the lifetime of the containing HTTP
209  * transaction.
210  */
211 struct http_request {
212  /** Method */
214  /** Request URI string */
215  const char *uri;
216  /** Server host name */
217  const char *host;
218  /** Range descriptor */
220  /** Content descriptor */
222  /** Authentication descriptor */
224 };
225 
226 /** An HTTP request header */
228  /** Header name (e.g. "User-Agent") */
229  const char *name;
230  /** Construct remaining header line
231  *
232  * @v http HTTP transaction
233  * @v buf Buffer
234  * @v len Length of buffer
235  * @ret len Header length if present, or negative error
236  */
237  int ( * format ) ( struct http_transaction *http, char *buf,
238  size_t len );
239 };
240 
241 /** HTTP request header table */
242 #define HTTP_REQUEST_HEADERS \
243  __table ( struct http_request_header, "http_request_headers" )
244 
245 /** Declare an HTTP request header */
246 #define __http_request_header __table_entry ( HTTP_REQUEST_HEADERS, 01 )
247 
248 /******************************************************************************
249  *
250  * Responses
251  *
252  ******************************************************************************
253  */
254 
255 /** HTTP response transfer descriptor */
257  /** Transfer encoding */
259 };
260 
261 /** HTTP response content descriptor */
263  /** Content length (may be zero) */
264  size_t len;
265  /** Content encoding */
267 };
268 
269 /** HTTP response Basic authorization descriptor */
271 };
272 
273 /** HTTP response Digest authorization descriptor */
275  /** Realm */
276  const char *realm;
277  /** Quality of protection */
278  const char *qop;
279  /** Algorithm */
280  const char *algorithm;
281  /** Nonce */
282  const char *nonce;
283  /** Opaque */
284  const char *opaque;
285 };
286 
287 /** HTTP response NTLM authorization descriptor */
289  /** Challenge message */
291  /** Challenge information */
293 };
294 
295 /** HTTP response authorization descriptor */
297  /** Authentication scheme (if any) */
299  /** Per-scheme information */
300  union {
301  /** Basic authorization descriptor */
303  /** Digest authorization descriptor */
305  /** NTLM authorization descriptor */
307  };
308 };
309 
310 /** An HTTP response
311  *
312  * This represents a single response received from the server,
313  * including all values parsed from headers.
314  *
315  * Pointers within this structure may point into the raw response
316  * buffer, and so should be invalidated when the response buffer is
317  * modified or discarded.
318  */
320  /** Raw response header lines
321  *
322  * This is the raw response data received from the server, up
323  * to and including the terminating empty line. String
324  * pointers within the response may point into this data
325  * buffer; NUL terminators will be added (overwriting the
326  * original terminating characters) as needed.
327  */
329  /** Status code
330  *
331  * This is the raw HTTP numeric status code (e.g. 404).
332  */
333  unsigned int status;
334  /** Return status code
335  *
336  * This is the iPXE return status code corresponding to the
337  * HTTP status code (e.g. -ENOENT).
338  */
339  int rc;
340  /** Redirection location */
341  const char *location;
342  /** Transfer descriptor */
344  /** Content descriptor */
346  /** Authorization descriptor */
348  /** Retry delay (in seconds) */
349  unsigned int retry_after;
350  /** Flags */
351  unsigned int flags;
352 };
353 
354 /** HTTP response flags */
356  /** Keep connection alive after close */
358  /** Content length specified */
360  /** Transaction may be retried on failure */
362 };
363 
364 /** An HTTP response header */
366  /** Header name (e.g. "Transfer-Encoding") */
367  const char *name;
368  /** Parse header line
369  *
370  * @v http HTTP transaction
371  * @v line Remaining header line
372  * @ret rc Return status code
373  */
374  int ( * parse ) ( struct http_transaction *http, char *line );
375 };
376 
377 /** HTTP response header table */
378 #define HTTP_RESPONSE_HEADERS \
379  __table ( struct http_response_header, "http_response_headers" )
380 
381 /** Declare an HTTP response header */
382 #define __http_response_header __table_entry ( HTTP_RESPONSE_HEADERS, 01 )
383 
384 /******************************************************************************
385  *
386  * Transactions
387  *
388  ******************************************************************************
389  */
390 
391 /** HTTP transaction state */
392 struct http_state {
393  /** Transmit data
394  *
395  * @v http HTTP transaction
396  * @ret rc Return status code
397  */
398  int ( * tx ) ( struct http_transaction *http );
399  /** Receive data
400  *
401  * @v http HTTP transaction
402  * @v iobuf I/O buffer (may be claimed)
403  * @ret rc Return status code
404  */
405  int ( * rx ) ( struct http_transaction *http,
406  struct io_buffer **iobuf );
407  /** Server connection closed
408  *
409  * @v http HTTP transaction
410  * @v rc Reason for close
411  */
412  void ( * close ) ( struct http_transaction *http, int rc );
413 };
414 
415 /** An HTTP transaction */
417  /** Reference count */
418  struct refcnt refcnt;
419  /** Data transfer interface */
420  struct interface xfer;
421  /** Content-decoded interface */
423  /** Transfer-decoded interface */
425  /** Server connection */
426  struct interface conn;
427  /** Transmit process */
428  struct process process;
429  /** Reconnection timer */
431  /** Idle connection watchdog timer */
433 
434  /** Request URI */
435  struct uri *uri;
436  /** Request */
438  /** Response */
440  /** Temporary line buffer */
442 
443  /** Transaction state */
444  struct http_state *state;
445  /** Accumulated transfer-decoded length */
446  size_t len;
447  /** Chunk length remaining */
448  size_t remaining;
449 };
450 
451 /******************************************************************************
452  *
453  * Transfer encoding
454  *
455  ******************************************************************************
456  */
457 
458 /** An HTTP transfer encoding */
460  /** Name */
461  const char *name;
462  /** Initialise transfer encoding
463  *
464  * @v http HTTP transaction
465  * @ret rc Return status code
466  */
467  int ( * init ) ( struct http_transaction *http );
468  /** Receive data state */
470 };
471 
472 /** HTTP transfer encoding table */
473 #define HTTP_TRANSFER_ENCODINGS \
474  __table ( struct http_transfer_encoding, "http_transfer_encodings" )
475 
476 /** Declare an HTTP transfer encoding */
477 #define __http_transfer_encoding __table_entry ( HTTP_TRANSFER_ENCODINGS, 01 )
478 
479 /******************************************************************************
480  *
481  * Content encoding
482  *
483  ******************************************************************************
484  */
485 
486 /** An HTTP content encoding */
488  /** Name */
489  const char *name;
490  /** Check if content encoding is supported for this request
491  *
492  * @v http HTTP transaction
493  * @ret supported Content encoding is supported for this request
494  */
495  int ( * supported ) ( struct http_transaction *http );
496  /** Initialise content encoding
497  *
498  * @v http HTTP transaction
499  * @ret rc Return status code
500  */
501  int ( * init ) ( struct http_transaction *http );
502 };
503 
504 /** HTTP content encoding table */
505 #define HTTP_CONTENT_ENCODINGS \
506  __table ( struct http_content_encoding, "http_content_encodings" )
507 
508 /** Declare an HTTP content encoding */
509 #define __http_content_encoding __table_entry ( HTTP_CONTENT_ENCODINGS, 01 )
510 
511 /******************************************************************************
512  *
513  * Authentication
514  *
515  ******************************************************************************
516  */
517 
518 /** An HTTP authentication scheme */
520  /** Name (e.g. "Digest") */
521  const char *name;
522  /** Parse remaining "WWW-Authenticate" header line
523  *
524  * @v http HTTP transaction
525  * @v line Remaining header line
526  * @ret rc Return status code
527  */
528  int ( * parse ) ( struct http_transaction *http, char *line );
529  /** Perform authentication
530  *
531  * @v http HTTP transaction
532  * @ret rc Return status code
533  */
534  int ( * authenticate ) ( struct http_transaction *http );
535  /** Construct remaining "Authorization" header line
536  *
537  * @v http HTTP transaction
538  * @v buf Buffer
539  * @v len Length of buffer
540  * @ret len Header length if present, or negative error
541  */
542  int ( * format ) ( struct http_transaction *http, char *buf,
543  size_t len );
544 };
545 
546 /** HTTP authentication scheme table */
547 #define HTTP_AUTHENTICATIONS \
548  __table ( struct http_authentication, "http_authentications" )
549 
550 /** Declare an HTTP authentication scheme */
551 #define __http_authentication __table_entry ( HTTP_AUTHENTICATIONS, 01 )
552 
553 /******************************************************************************
554  *
555  * General
556  *
557  ******************************************************************************
558  */
559 
560 extern char * http_token ( char **line, char **value );
561 extern int http_connect ( struct interface *xfer, struct uri *uri );
562 extern int http_open ( struct interface *xfer, struct http_method *method,
563  struct uri *uri, struct http_request_range *range,
564  struct http_request_content *content );
565 extern int http_open_uri ( struct interface *xfer, struct uri *uri );
566 
567 #endif /* _IPXE_HTTP_H */
struct interface xfer
Data transfer interface.
Definition: http.h:420
A process.
Definition: process.h:18
const char * qop
Quality of protection.
Definition: http.h:166
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * username
Username.
Definition: http.h:164
struct http_response_auth_digest digest
Digest authorization descriptor.
Definition: http.h:304
Content length specified.
Definition: http.h:359
struct pci_range range
PCI bus:dev.fn address range.
Definition: pcicloud.c:40
struct http_method http_get
HTTP GET method.
Definition: httpcore.c:144
unsigned int flags
Flags.
Definition: http.h:351
struct http_response_auth_basic basic
Basic authorization descriptor.
Definition: http.h:302
void(* close)(struct http_transaction *http, int rc)
Server connection closed.
Definition: http.h:412
size_t start
Range start.
Definition: http.h:138
int(* format)(struct http_transaction *http, char *buf, size_t len)
Construct remaining "Authorization" header line.
Definition: http.h:542
const char * host
Server host name.
Definition: http.h:217
struct ntlm_challenge * challenge
Challenge message.
Definition: http.h:290
HTTP request range descriptor.
Definition: http.h:136
Pooled connections.
int(* format)(struct http_transaction *http, char *buf, size_t len)
Construct remaining header line.
Definition: http.h:237
struct interface conn
Server connection.
Definition: http.h:426
unsigned int port
Default port.
Definition: http.h:45
I/O buffers.
int(* filter)(struct http_connection *conn)
Transport-layer filter (if any)
Definition: http.h:51
const char * name
Header name (e.g.
Definition: http.h:229
Retry timers.
HTTP transaction state.
Definition: http.h:392
struct line_buffer linebuf
Temporary line buffer.
Definition: http.h:441
int(* tx)(struct http_transaction *http)
Transmit data.
Definition: http.h:398
HTTP response NTLM authorization descriptor.
Definition: http.h:288
int(* parse)(struct http_transaction *http, char *line)
Parse header line.
Definition: http.h:374
struct uri * uri
Request URI.
Definition: http.h:435
A retry timer.
Definition: retry.h:22
const char * location
Redirection location.
Definition: http.h:341
An HTTP method.
Definition: http.h:100
struct http_state * state
Transaction state.
Definition: http.h:444
An HTTP authentication scheme.
Definition: http.h:519
#define HTTP_DIGEST_RESPONSE_LEN
HTTP Digest authentication response length.
Definition: http.h:133
A pooled connection.
Definition: pool.h:18
uint8_t method
Definition: ib_mad.h:15
An HTTP request header.
Definition: http.h:227
struct interface socket
Transport layer interface.
Definition: http.h:85
An HTTP content encoding.
Definition: http.h:487
struct http_response_auth auth
Authorization descriptor.
Definition: http.h:347
struct http_request_range range
Range descriptor.
Definition: http.h:219
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
int(* rx)(struct http_transaction *http, struct io_buffer **iobuf)
Receive data.
Definition: http.h:405
struct ntlm_nt_response nt
NT response.
Definition: http.h:182
struct interface xfer
Data transfer interface.
Definition: http.h:87
A reference counter.
Definition: refcnt.h:27
struct http_request request
Request.
Definition: http.h:437
int(* authenticate)(struct http_transaction *http)
Perform authentication.
Definition: http.h:534
struct http_transfer_encoding * encoding
Transfer encoding.
Definition: http.h:258
Keep connection alive after close.
Definition: http.h:357
struct http_method http_post
HTTP POST method.
Definition: httpcore.c:149
size_t len
Authenticate message length.
Definition: http.h:184
struct http_response response
Response.
Definition: http.h:439
An HTTP transaction.
Definition: http.h:416
char * http_token(char **line, char **value)
Get HTTP response token.
Definition: httpcore.c:196
const char * username
Username.
Definition: http.h:156
HTTP response transfer descriptor.
Definition: http.h:256
const char * uri
Request URI string.
Definition: http.h:215
An object interface.
Definition: interface.h:125
const char * algorithm
Algorithm.
Definition: http.h:280
const char * algorithm
Algorithm.
Definition: http.h:168
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
size_t len
Accumulated transfer-decoded length.
Definition: http.h:446
Object interfaces.
struct http_request_auth auth
Authentication descriptor.
Definition: http.h:223
struct http_response_content content
Content descriptor.
Definition: http.h:345
ring len
Length.
Definition: dwmac.h:231
HTTP response Basic authorization descriptor.
Definition: http.h:270
HTTP request Digest authentication descriptor.
Definition: http.h:162
const char * name
Name.
Definition: http.h:489
int http_open_uri(struct interface *xfer, struct uri *uri)
Open HTTP transaction for simple URI.
Definition: httpcore.c:1984
#define HTTP_DIGEST_CNONCE_LEN
HTTP Digest authentication client nonce length.
Definition: http.h:126
struct http_request_content content
Content descriptor.
Definition: http.h:221
const char * name
Scheme name (e.g.
Definition: http.h:43
const char * name
Method name (e.g.
Definition: http.h:102
struct http_method http_head
HTTP HEAD method.
Definition: httpcore.c:139
HTTP request content descriptor.
Definition: http.h:144
struct ntlm_challenge_info info
Challenge information.
Definition: http.h:292
struct http_content_encoding * encoding
Content encoding.
Definition: http.h:266
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:190
const char * name
Header name (e.g.
Definition: http.h:367
size_t len
Content length (may be zero)
Definition: http.h:264
struct interface content
Content-decoded interface.
Definition: http.h:422
int rc
Return status code.
Definition: http.h:339
unsigned int status
Status code.
Definition: http.h:333
struct interface transfer
Transfer-decoded interface.
Definition: http.h:424
const char * type
Content type (if any)
Definition: http.h:146
Processes.
int http_connect(struct interface *xfer, struct uri *uri)
Connect to an HTTP server.
Definition: httpconn.c:237
struct line_buffer headers
Raw response header lines.
Definition: http.h:328
struct http_response_auth_ntlm ntlm
NTLM authorization descriptor.
Definition: http.h:306
const char * nonce
Nonce.
Definition: http.h:282
const void * data
Content data (if any)
Definition: http.h:148
HTTP request NTLM authentication descriptor.
Definition: http.h:176
const char * realm
Realm.
Definition: http.h:276
struct http_scheme * scheme
HTTP scheme.
Definition: http.h:83
size_t len
Range length, or zero for no range request.
Definition: http.h:140
size_t len
Content length.
Definition: http.h:150
struct http_response_transfer transfer
Transfer descriptor.
Definition: http.h:343
An HTTP URI scheme.
Definition: http.h:41
A Challenge message.
Definition: ntlm.h:101
struct http_authentication * auth
Authentication scheme (if any)
Definition: http.h:298
const char * username
Username.
Definition: http.h:178
struct http_request_auth_ntlm ntlm
NTLM authentication descriptor.
Definition: http.h:198
A line buffer.
Definition: linebuf.h:17
const char * password
Password.
Definition: http.h:158
struct uri * uri
Connection URI.
Definition: http.h:81
struct http_method * method
Method.
Definition: http.h:213
Reference counting.
HTTP response Digest authorization descriptor.
Definition: http.h:274
const char * name
Name (e.g.
Definition: http.h:521
Linker tables.
char cnonce[HTTP_DIGEST_CNONCE_LEN+1]
Client nonce.
Definition: http.h:170
size_t remaining
Chunk length remaining.
Definition: http.h:448
A Uniform Resource Identifier.
Definition: uri.h:65
Transaction may be retried on failure.
Definition: http.h:361
An HTTP request.
Definition: http.h:211
NT LAN Manager (NTLM) authentication.
FILE_SECBOOT(PERMITTED)
int http_open(struct interface *xfer, struct http_method *method, struct uri *uri, struct http_request_range *range, struct http_request_content *content)
Open HTTP transaction.
Definition: httpcore.c:642
int(* parse)(struct http_transaction *http, char *line)
Parse remaining "WWW-Authenticate" header line.
Definition: http.h:528
HTTP request authentication descriptor.
Definition: http.h:188
NTLM challenge information.
Definition: ntlm.h:166
HTTP request Basic authentication descriptor.
Definition: http.h:154
int(* init)(struct http_transaction *http)
Initialise transfer encoding.
Definition: http.h:467
Line buffering.
int(* supported)(struct http_transaction *http)
Check if content encoding is supported for this request.
Definition: http.h:495
const char * name
Name.
Definition: http.h:461
struct http_state state
Receive data state.
Definition: http.h:469
An NT response.
Definition: ntlm.h:145
struct ntlm_lm_response lm
LAN Manager response.
Definition: http.h:180
An HTTP transfer encoding.
Definition: http.h:459
http_response_flags
HTTP response flags.
Definition: http.h:355
struct pooled_connection pool
Pooled connection.
Definition: http.h:89
A LAN Manager response.
Definition: ntlm.h:137
An HTTP response.
Definition: http.h:319
struct retry_timer retry
Reconnection timer.
Definition: http.h:430
An HTTP connection.
Definition: http.h:72
HTTP response authorization descriptor.
Definition: http.h:296
int(* init)(struct http_transaction *http)
Initialise content encoding.
Definition: http.h:501
char response[HTTP_DIGEST_RESPONSE_LEN+1]
Response.
Definition: http.h:172
const char * opaque
Opaque.
Definition: http.h:284
unsigned int retry_after
Retry delay (in seconds)
Definition: http.h:349
struct http_request_auth_basic basic
Basic authentication descriptor.
Definition: http.h:194
An HTTP response header.
Definition: http.h:365
struct http_request_auth_digest digest
Digest authentication descriptor.
Definition: http.h:196
const char * qop
Quality of protection.
Definition: http.h:278
HTTP response content descriptor.
Definition: http.h:262
A persistent I/O buffer.
Definition: iobuf.h:38
struct retry_timer watchdog
Idle connection watchdog timer.
Definition: http.h:432