summaryrefslogtreecommitdiffstats
path: root/qemu/roms/ipxe/src/include/ipxe/http.h
blob: a0dff7d007548a94c2ee1ab77d23b8f54b6f96fe (plain)
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
#ifndef _IPXE_HTTP_H
#define _IPXE_HTTP_H

/** @file
 *
 * Hyper Text Transport Protocol
 *
 */

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );

#include <stdint.h>
#include <ipxe/refcnt.h>
#include <ipxe/interface.h>
#include <ipxe/iobuf.h>
#include <ipxe/process.h>
#include <ipxe/retry.h>
#include <ipxe/linebuf.h>
#include <ipxe/pool.h>
#include <ipxe/tables.h>

struct http_transaction;

/******************************************************************************
 *
 * HTTP URI schemes
 *
 ******************************************************************************
 */

/** HTTP default port */
#define HTTP_PORT 80

/** HTTPS default port */
#define HTTPS_PORT 443

/** An HTTP URI scheme */
struct http_scheme {
	/** Scheme name (e.g. "http" or "https") */
	const char *name;
	/** Default port */
	unsigned int port;
	/** Transport-layer filter (if any)
	 *
	 * @v xfer		Data transfer interface
	 * @v name		Host name
	 * @v next		Next interface
	 * @ret rc		Return status code
	 */
	int ( * filter ) ( struct interface *xfer, const char *name,
			   struct interface **next );
};

/** HTTP scheme table */
#define HTTP_SCHEMES __table ( struct http_scheme, "http_schemes" )

/** Declare an HTTP scheme */
#define __http_scheme __table_entry ( HTTP_SCHEMES, 01 )

/******************************************************************************
 *
 * Connections
 *
 ******************************************************************************
 */

/** An HTTP connection
 *
 * This represents a potentially reusable connection to an HTTP
 * server.
 */
struct http_connection {
	/** Reference count */
	struct refcnt refcnt;
	/** Connection URI
	 *
	 * This encapsulates the server (and protocol) used for the
	 * connection.  This may be the origin server or a proxy
	 * server.
	 */
	struct uri *uri;
	/** HTTP scheme */
	struct http_scheme *scheme;
	/** Transport layer interface */
	struct interface socket;
	/** Data transfer interface */
	struct interface xfer;
	/** Pooled connection */
	struct pooled_connection pool;
};

/******************************************************************************
 *
 * HTTP methods
 *
 ******************************************************************************
 */

/** An HTTP method */
struct http_method {
	/** Method name (e.g. "GET" or "POST") */
	const char *name;
};

extern struct http_method http_head;
extern struct http_method http_get;
extern struct http_method http_post;

/******************************************************************************
 *
 * Requests
 *
 ******************************************************************************
 */

/** HTTP Digest authentication client nonce count
 *
 * We choose to generate a new client nonce each time.
 */
#define HTTP_DIGEST_NC "00000001"

/** HTTP Digest authentication client nonce length
 *
 * We choose to use a 32-bit hex client nonce.
 */
#define HTTP_DIGEST_CNONCE_LEN 8

/** HTTP Digest authentication response length
 *
 * The Digest authentication response is a Base16-encoded 16-byte MD5
 * checksum.
 */
#define HTTP_DIGEST_RESPONSE_LEN 32

/** HTTP request range descriptor */
struct http_request_range {
	/** Range start */
	size_t start;
	/** Range length, or zero for no range request */
	size_t len;
};

/** HTTP request content descriptor */
struct http_request_content {
	/** Content type (if any) */
	const char *type;
	/** Content data (if any) */
	const void *data;
	/** Content length */
	size_t len;
};

/** HTTP request authentication descriptor */
struct http_request_auth {
	/** Authentication scheme (if any) */
	struct http_authentication *auth;
	/** Username */
	const char *username;
	/** Password */
	const char *password;
	/** Quality of protection */
	const char *qop;
	/** Algorithm */
	const char *algorithm;
	/** Client nonce */
	char cnonce[ HTTP_DIGEST_CNONCE_LEN + 1 /* NUL */ ];
	/** Response */
	char response[ HTTP_DIGEST_RESPONSE_LEN + 1 /* NUL */ ];
};

/** An HTTP request
 *
 * This represents a single request to be sent to a server, including
 * the values required to construct all headers.
 *
 * Pointers within this structure must point to storage which is
 * guaranteed to remain valid for the lifetime of the containing HTTP
 * transaction.
 */
struct http_request {
	/** Method */
	struct http_method *method;
	/** Request URI string */
	const char *uri;
	/** Server host name */
	const char *host;
	/** Range descriptor */
	struct http_request_range range;
	/** Content descriptor */
	struct http_request_content content;
	/** Authentication descriptor */
	struct http_request_auth auth;
};

/** An HTTP request header */
struct http_request_header {
	/** Header name (e.g. "User-Agent") */
	const char *name;
	/** Construct remaining header line
	 *
	 * @v http		HTTP transaction
	 * @v buf		Buffer
	 * @v len		Length of buffer
	 * @ret len		Header length if present, or negative error
	 */
	int ( * format ) ( struct http_transaction *http, char *buf,
			   size_t len );
};

/** HTTP request header table */
#define HTTP_REQUEST_HEADERS \
	__table ( struct http_request_header, "http_request_headers" )

/** Declare an HTTP request header */
#define __http_request_header __table_entry ( HTTP_REQUEST_HEADERS, 01 )

/******************************************************************************
 *
 * Responses
 *
 ******************************************************************************
 */

/** HTTP response transfer descriptor */
struct http_response_transfer {
	/** Transfer encoding */
	struct http_transfer_encoding *encoding;
};

/** HTTP response content descriptor */
struct http_response_content {
	/** Content length (may be zero) */
	size_t len;
	/** Content encoding */
	struct http_content_encoding *encoding;
};

/** HTTP response authorization descriptor */
struct http_response_auth {
	/** Authentication scheme (if any) */
	struct http_authentication *auth;
	/** Realm */
	const char *realm;
	/** Quality of protection */
	const char *qop;
	/** Algorithm */
	const char *algorithm;
	/** Nonce */
	const char *nonce;
	/** Opaque */
	const char *opaque;
};

/** An HTTP response
 *
 * This represents a single response received from the server,
 * including all values parsed from headers.
 *
 * Pointers within this structure may point into the raw response
 * buffer, and so should be invalidated when the response buffer is
 * modified or discarded.
 */
struct http_response {
	/** Raw response header lines
	 *
	 * This is the raw response data received from the server, up
	 * to and including the terminating empty line.  String
	 * pointers within the response may point into this data
	 * buffer; NUL terminators will be added (overwriting the
	 * original terminating characters) as needed.
	 */
	struct line_buffer headers;
	/** Status code
	 *
	 * This is the raw HTTP numeric status code (e.g. 404).
	 */
	unsigned int status;
	/** Return status code
	 *
	 * This is the iPXE return status code corresponding to the
	 * HTTP status code (e.g. -ENOENT).
	 */
	int rc;
	/** Redirection location */
	const char *location;
	/** Transfer descriptor */
	struct http_response_transfer transfer;
	/** Content descriptor */
	struct http_response_content content;
	/** Authorization descriptor */
	struct http_response_auth auth;
	/** Retry delay (in seconds) */
	unsigned int retry_after;
	/** Flags */
	unsigned int flags;
};

/** HTTP response flags */
enum http_response_flags {
	/** Keep connection alive after close */
	HTTP_RESPONSE_KEEPALIVE = 0x0001,
	/** Content length specified */
	HTTP_RESPONSE_CONTENT_LEN = 0x0002,
	/** Transaction may be retried on failure */
	HTTP_RESPONSE_RETRY = 0x0004,
};

/** An HTTP response header */
struct http_response_header {
	/** Header name (e.g. "Transfer-Encoding") */
	const char *name;
	/** Parse header line
	 *
	 * @v http		HTTP transaction
	 * @v line		Remaining header line
	 * @ret rc		Return status code
	 */
	int ( * parse ) ( struct http_transaction *http, char *line );
};

/** HTTP response header table */
#define HTTP_RESPONSE_HEADERS \
	__table ( struct http_response_header, "http_response_headers" )

/** Declare an HTTP response header */
#define __http_response_header __table_entry ( HTTP_RESPONSE_HEADERS, 01 )

/******************************************************************************
 *
 * Transactions
 *
 ******************************************************************************
 */

/** HTTP transaction state */
struct http_state {
	/** Transmit data
	 *
	 * @v http		HTTP transaction
	 * @ret rc		Return status code
	 */
	int ( * tx ) ( struct http_transaction *http );
	/** Receive data
	 *
	 * @v http		HTTP transaction
	 * @v iobuf		I/O buffer (may be claimed)
	 * @ret rc		Return status code
	 */
	int ( * rx ) ( struct http_transaction *http,
		       struct io_buffer **iobuf );
	/** Server connection closed
	 *
	 * @v http		HTTP transaction
	 * @v rc		Reason for close
	 */
	void ( * close ) ( struct http_transaction *http, int rc );
};

/** An HTTP transaction */
struct http_transaction {
	/** Reference count */
	struct refcnt refcnt;
	/** Data transfer interface */
	struct interface xfer;
	/** Content-decoded interface */
	struct interface content;
	/** Transfer-decoded interface */
	struct interface transfer;
	/** Server connection */
	struct interface conn;
	/** Transmit process */
	struct process process;
	/** Reconnection timer */
	struct retry_timer timer;

	/** Request URI */
	struct uri *uri;
	/** Request */
	struct http_request request;
	/** Response */
	struct http_response response;
	/** Temporary line buffer */
	struct line_buffer linebuf;

	/** Transaction state */
	struct http_state *state;
	/** Accumulated transfer-decoded length */
	size_t len;
	/** Chunk length remaining */
	size_t remaining;
};

/******************************************************************************
 *
 * Transfer encoding
 *
 ******************************************************************************
 */

/** An HTTP transfer encoding */
struct http_transfer_encoding {
	/** Name */
	const char *name;
	/** Initialise transfer encoding
	 *
	 * @v http		HTTP transaction
	 * @ret rc		Return status code
	 */
	int ( * init ) ( struct http_transaction *http );
	/** Receive data state */
	struct http_state state;
};

/** HTTP transfer encoding table */
#define HTTP_TRANSFER_ENCODINGS \
	__table ( struct http_transfer_encoding, "http_transfer_encodings" )

/** Declare an HTTP transfer encoding */
#define __http_transfer_encoding __table_entry ( HTTP_TRANSFER_ENCODINGS, 01 )

/******************************************************************************
 *
 * Content encoding
 *
 ******************************************************************************
 */

/** An HTTP content encoding */
struct http_content_encoding {
	/** Name */
	const char *name;
	/** Check if content encoding is supported for this request
	 *
	 * @v http		HTTP transaction
	 * @ret supported	Content encoding is supported for this request
	 */
	int ( * supported ) ( struct http_transaction *http );
	/** Initialise content encoding
	 *
	 * @v http		HTTP transaction
	 * @ret rc		Return status code
	 */
	int ( * init ) ( struct http_transaction *http );
};

/** HTTP content encoding table */
#define HTTP_CONTENT_ENCODINGS \
	__table ( struct http_content_encoding, "http_content_encodings" )

/** Declare an HTTP content encoding */
#define __http_content_encoding __table_entry ( HTTP_CONTENT_ENCODINGS, 01 )

/******************************************************************************
 *
 * Authentication
 *
 ******************************************************************************
 */

/** An HTTP authentication scheme */
struct http_authentication {
	/** Name (e.g. "Digest") */
	const char *name;
	/** Perform authentication
	 *
	 * @v http		HTTP transaction
	 * @ret rc		Return status code
	 */
	int ( * authenticate ) ( struct http_transaction *http );
	/** Construct remaining "Authorization" header line
	 *
	 * @v http		HTTP transaction
	 * @v buf		Buffer
	 * @v len		Length of buffer
	 * @ret len		Header length if present, or negative error
	 */
	int ( * format ) ( struct http_transaction *http, char *buf,
			   size_t len );
};

/** HTTP authentication scheme table */
#define HTTP_AUTHENTICATIONS \
	__table ( struct http_authentication, "http_authentications" )

/** Declare an HTTP authentication scheme */
#define __http_authentication __table_entry ( HTTP_AUTHENTICATIONS, 01 )

/******************************************************************************
 *
 * General
 *
 ******************************************************************************
 */

extern char * http_token ( char **line, char **value );
extern int http_connect ( struct interface *xfer, struct uri *uri );
extern int http_open ( struct interface *xfer, struct http_method *method,
		       struct uri *uri, struct http_request_range *range,
		       struct http_request_content *content );
extern int http_open_uri ( struct interface *xfer, struct uri *uri );

#endif /* _IPXE_HTTP_H */