You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

http.h 14KB

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