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

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