6 * Hyper Text Transport Protocol
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
13 #include <ipxe/refcnt.h>
14 #include <ipxe/interface.h>
15 #include <ipxe/iobuf.h>
16 #include <ipxe/process.h>
17 #include <ipxe/retry.h>
18 #include <ipxe/linebuf.h>
19 #include <ipxe/pool.h>
20 #include <ipxe/tables.h>
22 struct http_transaction;
24 /******************************************************************************
28 ******************************************************************************
31 /** HTTP default port */
34 /** HTTPS default port */
35 #define HTTPS_PORT 443
37 /** An HTTP URI scheme */
39 /** Scheme name (e.g. "http" or "https") */
43 /** Transport-layer filter (if any)
45 * @v xfer Data transfer interface
47 * @v next Next interface
48 * @ret rc Return status code
50 int ( * filter ) ( struct interface *xfer, const char *name,
51 struct interface **next );
54 /** HTTP scheme table */
55 #define HTTP_SCHEMES __table ( struct http_scheme, "http_schemes" )
57 /** Declare an HTTP scheme */
58 #define __http_scheme __table_entry ( HTTP_SCHEMES, 01 )
60 /******************************************************************************
64 ******************************************************************************
67 /** An HTTP connection
69 * This represents a potentially reusable connection to an HTTP
72 struct http_connection {
73 /** Reference count */
77 * This encapsulates the server (and protocol) used for the
78 * connection. This may be the origin server or a proxy
83 struct http_scheme *scheme;
84 /** Transport layer interface */
85 struct interface socket;
86 /** Data transfer interface */
87 struct interface xfer;
88 /** Pooled connection */
89 struct pooled_connection pool;
92 /******************************************************************************
96 ******************************************************************************
101 /** Method name (e.g. "GET" or "POST") */
105 extern struct http_method http_head;
106 extern struct http_method http_get;
107 extern struct http_method http_post;
109 /******************************************************************************
113 ******************************************************************************
116 /** HTTP Digest authentication client nonce count
118 * We choose to generate a new client nonce each time.
120 #define HTTP_DIGEST_NC "00000001"
122 /** HTTP Digest authentication client nonce length
124 * We choose to use a 32-bit hex client nonce.
126 #define HTTP_DIGEST_CNONCE_LEN 8
128 /** HTTP Digest authentication response length
130 * The Digest authentication response is a Base16-encoded 16-byte MD5
133 #define HTTP_DIGEST_RESPONSE_LEN 32
135 /** HTTP request range descriptor */
136 struct http_request_range {
139 /** Range length, or zero for no range request */
143 /** HTTP request content descriptor */
144 struct http_request_content {
145 /** Content type (if any) */
147 /** Content data (if any) */
149 /** Content length */
153 /** HTTP request authentication descriptor */
154 struct http_request_auth {
155 /** Authentication scheme (if any) */
156 struct http_authentication *auth;
158 const char *username;
160 const char *password;
161 /** Quality of protection */
164 const char *algorithm;
166 char cnonce[ HTTP_DIGEST_CNONCE_LEN + 1 /* NUL */ ];
168 char response[ HTTP_DIGEST_RESPONSE_LEN + 1 /* NUL */ ];
173 * This represents a single request to be sent to a server, including
174 * the values required to construct all headers.
176 * Pointers within this structure must point to storage which is
177 * guaranteed to remain valid for the lifetime of the containing HTTP
180 struct http_request {
182 struct http_method *method;
183 /** Request URI string */
185 /** Server host name */
187 /** Range descriptor */
188 struct http_request_range range;
189 /** Content descriptor */
190 struct http_request_content content;
191 /** Authentication descriptor */
192 struct http_request_auth auth;
195 /** An HTTP request header */
196 struct http_request_header {
197 /** Header name (e.g. "User-Agent") */
199 /** Construct remaining header line
201 * @v http HTTP transaction
203 * @v len Length of buffer
204 * @ret len Header length if present, or negative error
206 int ( * format ) ( struct http_transaction *http, char *buf,
210 /** HTTP request header table */
211 #define HTTP_REQUEST_HEADERS \
212 __table ( struct http_request_header, "http_request_headers" )
214 /** Declare an HTTP request header */
215 #define __http_request_header __table_entry ( HTTP_REQUEST_HEADERS, 01 )
217 /******************************************************************************
221 ******************************************************************************
224 /** HTTP response transfer descriptor */
225 struct http_response_transfer {
226 /** Transfer encoding */
227 struct http_transfer_encoding *encoding;
230 /** HTTP response content descriptor */
231 struct http_response_content {
232 /** Content length (may be zero) */
234 /** Content encoding */
235 struct http_content_encoding *encoding;
238 /** HTTP response authorization descriptor */
239 struct http_response_auth {
240 /** Authentication scheme (if any) */
241 struct http_authentication *auth;
244 /** Quality of protection */
247 const char *algorithm;
256 * This represents a single response received from the server,
257 * including all values parsed from headers.
259 * Pointers within this structure may point into the raw response
260 * buffer, and so should be invalidated when the response buffer is
261 * modified or discarded.
263 struct http_response {
264 /** Raw response header lines
266 * This is the raw response data received from the server, up
267 * to and including the terminating empty line. String
268 * pointers within the response may point into this data
269 * buffer; NUL terminators will be added (overwriting the
270 * original terminating characters) as needed.
272 struct line_buffer headers;
275 * This is the raw HTTP numeric status code (e.g. 404).
278 /** Return status code
280 * This is the iPXE return status code corresponding to the
281 * HTTP status code (e.g. -ENOENT).
284 /** Redirection location */
285 const char *location;
286 /** Transfer descriptor */
287 struct http_response_transfer transfer;
288 /** Content descriptor */
289 struct http_response_content content;
290 /** Authorization descriptor */
291 struct http_response_auth auth;
292 /** Retry delay (in seconds) */
293 unsigned int retry_after;
298 /** HTTP response flags */
299 enum http_response_flags {
300 /** Keep connection alive after close */
301 HTTP_RESPONSE_KEEPALIVE = 0x0001,
302 /** Content length specified */
303 HTTP_RESPONSE_CONTENT_LEN = 0x0002,
304 /** Transaction may be retried on failure */
305 HTTP_RESPONSE_RETRY = 0x0004,
308 /** An HTTP response header */
309 struct http_response_header {
310 /** Header name (e.g. "Transfer-Encoding") */
312 /** Parse header line
314 * @v http HTTP transaction
315 * @v line Remaining header line
316 * @ret rc Return status code
318 int ( * parse ) ( struct http_transaction *http, char *line );
321 /** HTTP response header table */
322 #define HTTP_RESPONSE_HEADERS \
323 __table ( struct http_response_header, "http_response_headers" )
325 /** Declare an HTTP response header */
326 #define __http_response_header __table_entry ( HTTP_RESPONSE_HEADERS, 01 )
328 /******************************************************************************
332 ******************************************************************************
335 /** HTTP transaction state */
339 * @v http HTTP transaction
340 * @ret rc Return status code
342 int ( * tx ) ( struct http_transaction *http );
345 * @v http HTTP transaction
346 * @v iobuf I/O buffer (may be claimed)
347 * @ret rc Return status code
349 int ( * rx ) ( struct http_transaction *http,
350 struct io_buffer **iobuf );
351 /** Server connection closed
353 * @v http HTTP transaction
354 * @v rc Reason for close
356 void ( * close ) ( struct http_transaction *http, int rc );
359 /** An HTTP transaction */
360 struct http_transaction {
361 /** Reference count */
362 struct refcnt refcnt;
363 /** Data transfer interface */
364 struct interface xfer;
365 /** Content-decoded interface */
366 struct interface content;
367 /** Transfer-decoded interface */
368 struct interface transfer;
369 /** Server connection */
370 struct interface conn;
371 /** Transmit process */
372 struct process process;
373 /** Reconnection timer */
374 struct retry_timer timer;
379 struct http_request request;
381 struct http_response response;
382 /** Temporary line buffer */
383 struct line_buffer linebuf;
385 /** Transaction state */
386 struct http_state *state;
387 /** Accumulated transfer-decoded length */
389 /** Chunk length remaining */
393 /******************************************************************************
397 ******************************************************************************
400 /** An HTTP transfer encoding */
401 struct http_transfer_encoding {
404 /** Initialise transfer encoding
406 * @v http HTTP transaction
407 * @ret rc Return status code
409 int ( * init ) ( struct http_transaction *http );
410 /** Receive data state */
411 struct http_state state;
414 /** HTTP transfer encoding table */
415 #define HTTP_TRANSFER_ENCODINGS \
416 __table ( struct http_transfer_encoding, "http_transfer_encodings" )
418 /** Declare an HTTP transfer encoding */
419 #define __http_transfer_encoding __table_entry ( HTTP_TRANSFER_ENCODINGS, 01 )
421 /******************************************************************************
425 ******************************************************************************
428 /** An HTTP content encoding */
429 struct http_content_encoding {
432 /** Check if content encoding is supported for this request
434 * @v http HTTP transaction
435 * @ret supported Content encoding is supported for this request
437 int ( * supported ) ( struct http_transaction *http );
438 /** Initialise content encoding
440 * @v http HTTP transaction
441 * @ret rc Return status code
443 int ( * init ) ( struct http_transaction *http );
446 /** HTTP content encoding table */
447 #define HTTP_CONTENT_ENCODINGS \
448 __table ( struct http_content_encoding, "http_content_encodings" )
450 /** Declare an HTTP content encoding */
451 #define __http_content_encoding __table_entry ( HTTP_CONTENT_ENCODINGS, 01 )
453 /******************************************************************************
457 ******************************************************************************
460 /** An HTTP authentication scheme */
461 struct http_authentication {
462 /** Name (e.g. "Digest") */
464 /** Perform authentication
466 * @v http HTTP transaction
467 * @ret rc Return status code
469 int ( * authenticate ) ( struct http_transaction *http );
470 /** Construct remaining "Authorization" header line
472 * @v http HTTP transaction
474 * @v len Length of buffer
475 * @ret len Header length if present, or negative error
477 int ( * format ) ( struct http_transaction *http, char *buf,
481 /** HTTP authentication scheme table */
482 #define HTTP_AUTHENTICATIONS \
483 __table ( struct http_authentication, "http_authentications" )
485 /** Declare an HTTP authentication scheme */
486 #define __http_authentication __table_entry ( HTTP_AUTHENTICATIONS, 01 )
488 /******************************************************************************
492 ******************************************************************************
495 extern char * http_token ( char **line, char **value );
496 extern int http_connect ( struct interface *xfer, struct uri *uri );
497 extern int http_open ( struct interface *xfer, struct http_method *method,
498 struct uri *uri, struct http_request_range *range,
499 struct http_request_content *content );
500 extern int http_open_uri ( struct interface *xfer, struct uri *uri );
502 #endif /* _IPXE_HTTP_H */