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 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 authentication descriptor */
  132. struct http_request_auth {
  133. /** Authentication scheme (if any) */
  134. struct http_authentication *auth;
  135. /** Username */
  136. const char *username;
  137. /** Password */
  138. const char *password;
  139. /** Quality of protection */
  140. const char *qop;
  141. /** Algorithm */
  142. const char *algorithm;
  143. /** Client nonce */
  144. char cnonce[ HTTP_DIGEST_CNONCE_LEN + 1 /* NUL */ ];
  145. /** Response */
  146. char response[ HTTP_DIGEST_RESPONSE_LEN + 1 /* NUL */ ];
  147. };
  148. /** An HTTP request
  149. *
  150. * This represents a single request to be sent to a server, including
  151. * the values required to construct all headers.
  152. *
  153. * Pointers within this structure must point to storage which is
  154. * guaranteed to remain valid for the lifetime of the containing HTTP
  155. * transaction.
  156. */
  157. struct http_request {
  158. /** Method */
  159. struct http_method *method;
  160. /** Request URI string */
  161. const char *uri;
  162. /** Server host name */
  163. const char *host;
  164. /** Range descriptor */
  165. struct http_request_range range;
  166. /** Content descriptor */
  167. struct http_request_content content;
  168. /** Authentication descriptor */
  169. struct http_request_auth auth;
  170. };
  171. /** An HTTP request header */
  172. struct http_request_header {
  173. /** Header name (e.g. "User-Agent") */
  174. const char *name;
  175. /** Construct remaining header line
  176. *
  177. * @v http HTTP transaction
  178. * @v buf Buffer
  179. * @v len Length of buffer
  180. * @ret len Header length if present, or negative error
  181. */
  182. int ( * format ) ( struct http_transaction *http, char *buf,
  183. size_t len );
  184. };
  185. /** HTTP request header table */
  186. #define HTTP_REQUEST_HEADERS \
  187. __table ( struct http_request_header, "http_request_headers" )
  188. /** Declare an HTTP request header */
  189. #define __http_request_header __table_entry ( HTTP_REQUEST_HEADERS, 01 )
  190. /******************************************************************************
  191. *
  192. * Responses
  193. *
  194. ******************************************************************************
  195. */
  196. /** HTTP response transfer descriptor */
  197. struct http_response_transfer {
  198. /** Transfer encoding */
  199. struct http_transfer_encoding *encoding;
  200. };
  201. /** HTTP response content descriptor */
  202. struct http_response_content {
  203. /** Content length (may be zero) */
  204. size_t len;
  205. /** Content encoding */
  206. struct http_content_encoding *encoding;
  207. };
  208. /** HTTP response authorization descriptor */
  209. struct http_response_auth {
  210. /** Authentication scheme (if any) */
  211. struct http_authentication *auth;
  212. /** Realm */
  213. const char *realm;
  214. /** Quality of protection */
  215. const char *qop;
  216. /** Algorithm */
  217. const char *algorithm;
  218. /** Nonce */
  219. const char *nonce;
  220. /** Opaque */
  221. const char *opaque;
  222. };
  223. /** An HTTP response
  224. *
  225. * This represents a single response received from the server,
  226. * including all values parsed from headers.
  227. *
  228. * Pointers within this structure may point into the raw response
  229. * buffer, and so should be invalidated when the response buffer is
  230. * modified or discarded.
  231. */
  232. struct http_response {
  233. /** Raw response header lines
  234. *
  235. * This is the raw response data received from the server, up
  236. * to and including the terminating empty line. String
  237. * pointers within the response may point into this data
  238. * buffer; NUL terminators will be added (overwriting the
  239. * original terminating characters) as needed.
  240. */
  241. struct line_buffer headers;
  242. /** Status code
  243. *
  244. * This is the raw HTTP numeric status code (e.g. 404).
  245. */
  246. unsigned int status;
  247. /** Return status code
  248. *
  249. * This is the iPXE return status code corresponding to the
  250. * HTTP status code (e.g. -ENOENT).
  251. */
  252. int rc;
  253. /** Redirection location */
  254. const char *location;
  255. /** Transfer descriptor */
  256. struct http_response_transfer transfer;
  257. /** Content descriptor */
  258. struct http_response_content content;
  259. /** Authorization descriptor */
  260. struct http_response_auth auth;
  261. /** Retry delay (in seconds) */
  262. unsigned int retry_after;
  263. /** Flags */
  264. unsigned int flags;
  265. };
  266. /** HTTP response flags */
  267. enum http_response_flags {
  268. /** Keep connection alive after close */
  269. HTTP_RESPONSE_KEEPALIVE = 0x0001,
  270. /** Content length specified */
  271. HTTP_RESPONSE_CONTENT_LEN = 0x0002,
  272. /** Transaction may be retried on failure */
  273. HTTP_RESPONSE_RETRY = 0x0004,
  274. };
  275. /** An HTTP response header */
  276. struct http_response_header {
  277. /** Header name (e.g. "Transfer-Encoding") */
  278. const char *name;
  279. /** Parse header line
  280. *
  281. * @v http HTTP transaction
  282. * @v line Remaining header line
  283. * @ret rc Return status code
  284. */
  285. int ( * parse ) ( struct http_transaction *http, char *line );
  286. };
  287. /** HTTP response header table */
  288. #define HTTP_RESPONSE_HEADERS \
  289. __table ( struct http_response_header, "http_response_headers" )
  290. /** Declare an HTTP response header */
  291. #define __http_response_header __table_entry ( HTTP_RESPONSE_HEADERS, 01 )
  292. /******************************************************************************
  293. *
  294. * Transactions
  295. *
  296. ******************************************************************************
  297. */
  298. /** HTTP transaction state */
  299. struct http_state {
  300. /** Transmit data
  301. *
  302. * @v http HTTP transaction
  303. * @ret rc Return status code
  304. */
  305. int ( * tx ) ( struct http_transaction *http );
  306. /** Receive data
  307. *
  308. * @v http HTTP transaction
  309. * @v iobuf I/O buffer (may be claimed)
  310. * @ret rc Return status code
  311. */
  312. int ( * rx ) ( struct http_transaction *http,
  313. struct io_buffer **iobuf );
  314. /** Server connection closed
  315. *
  316. * @v http HTTP transaction
  317. * @v rc Reason for close
  318. */
  319. void ( * close ) ( struct http_transaction *http, int rc );
  320. };
  321. /** An HTTP transaction */
  322. struct http_transaction {
  323. /** Reference count */
  324. struct refcnt refcnt;
  325. /** Data transfer interface */
  326. struct interface xfer;
  327. /** Content-decoded interface */
  328. struct interface content;
  329. /** Transfer-decoded interface */
  330. struct interface transfer;
  331. /** Server connection */
  332. struct interface conn;
  333. /** Transmit process */
  334. struct process process;
  335. /** Reconnection timer */
  336. struct retry_timer timer;
  337. /** Request URI */
  338. struct uri *uri;
  339. /** Request */
  340. struct http_request request;
  341. /** Response */
  342. struct http_response response;
  343. /** Temporary line buffer */
  344. struct line_buffer linebuf;
  345. /** Transaction state */
  346. struct http_state *state;
  347. /** Accumulated transfer-decoded length */
  348. size_t len;
  349. /** Chunk length remaining */
  350. size_t remaining;
  351. };
  352. /******************************************************************************
  353. *
  354. * Transfer encoding
  355. *
  356. ******************************************************************************
  357. */
  358. /** An HTTP transfer encoding */
  359. struct http_transfer_encoding {
  360. /** Name */
  361. const char *name;
  362. /** Initialise transfer encoding
  363. *
  364. * @v http HTTP transaction
  365. * @ret rc Return status code
  366. */
  367. int ( * init ) ( struct http_transaction *http );
  368. /** Receive data state */
  369. struct http_state state;
  370. };
  371. /** HTTP transfer encoding table */
  372. #define HTTP_TRANSFER_ENCODINGS \
  373. __table ( struct http_transfer_encoding, "http_transfer_encodings" )
  374. /** Declare an HTTP transfer encoding */
  375. #define __http_transfer_encoding __table_entry ( HTTP_TRANSFER_ENCODINGS, 01 )
  376. /******************************************************************************
  377. *
  378. * Content encoding
  379. *
  380. ******************************************************************************
  381. */
  382. /** An HTTP content encoding */
  383. struct http_content_encoding {
  384. /** Name */
  385. const char *name;
  386. /** Check if content encoding is supported for this request
  387. *
  388. * @v http HTTP transaction
  389. * @ret supported Content encoding is supported for this request
  390. */
  391. int ( * supported ) ( struct http_transaction *http );
  392. /** Initialise content encoding
  393. *
  394. * @v http HTTP transaction
  395. * @ret rc Return status code
  396. */
  397. int ( * init ) ( struct http_transaction *http );
  398. };
  399. /** HTTP content encoding table */
  400. #define HTTP_CONTENT_ENCODINGS \
  401. __table ( struct http_content_encoding, "http_content_encodings" )
  402. /** Declare an HTTP content encoding */
  403. #define __http_content_encoding __table_entry ( HTTP_CONTENT_ENCODINGS, 01 )
  404. /******************************************************************************
  405. *
  406. * Authentication
  407. *
  408. ******************************************************************************
  409. */
  410. /** An HTTP authentication scheme */
  411. struct http_authentication {
  412. /** Name (e.g. "Digest") */
  413. const char *name;
  414. /** Perform authentication
  415. *
  416. * @v http HTTP transaction
  417. * @ret rc Return status code
  418. */
  419. int ( * authenticate ) ( struct http_transaction *http );
  420. /** Construct remaining "Authorization" header line
  421. *
  422. * @v http HTTP transaction
  423. * @v buf Buffer
  424. * @v len Length of buffer
  425. * @ret len Header length if present, or negative error
  426. */
  427. int ( * format ) ( struct http_transaction *http, char *buf,
  428. size_t len );
  429. };
  430. /** HTTP authentication scheme table */
  431. #define HTTP_AUTHENTICATIONS \
  432. __table ( struct http_authentication, "http_authentications" )
  433. /** Declare an HTTP authentication scheme */
  434. #define __http_authentication __table_entry ( HTTP_AUTHENTICATIONS, 01 )
  435. /******************************************************************************
  436. *
  437. * General
  438. *
  439. ******************************************************************************
  440. */
  441. extern char * http_token ( char **line, char **value );
  442. extern int http_connect ( struct interface *xfer, struct uri *uri );
  443. extern int http_open ( struct interface *xfer, struct http_method *method,
  444. struct uri *uri, struct http_request_range *range,
  445. struct http_request_content *content );
  446. extern int http_open_uri ( struct interface *xfer, struct uri *uri );
  447. #endif /* _IPXE_HTTP_H */