uhttp.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* uhttp: small ring-3 HTTP/1.1 GET client shared by curl and the browser.
  2. *
  3. * The kernel owns the NIC and the TCP stack; this module drives them through
  4. * the SYS_NET_* / SYS_TCP_* calls, optionally wraps the connection in TLS
  5. * (tls.c), follows redirects and de-chunks the response. The caller supplies
  6. * the connection object, so there is no allocation.
  7. *
  8. * https uses the phase-A tls.c client: encrypted but WITHOUT certificate
  9. * verification. The caller can show a warning (curl does).
  10. */
  11. #ifndef UHTTP_H
  12. #define UHTTP_H
  13. #include "ucmd_lib.h"
  14. #include "uhttp_cookie.h"
  15. #define UHTTP_HOST_MAX 128
  16. #define UHTTP_PATH_MAX 1024 /* path + query (OIDC URLs are long) */
  17. #define UHTTP_LOC_MAX 1280
  18. #define UHTTP_LINE_MAX 1536 /* Location / Set-Cookie header lines */
  19. #define UHTTP_READER_MAX 16384
  20. #define UHTTP_REQ_MAX 4096 /* request line + headers */
  21. #define UHTTP_MAX_REDIRECTS 8
  22. #define UHTTP_IDLE_TIMEOUT_MS 10000
  23. /* Backstop only: the idle timeout above catches a stalled peer, so slow but
  24. * steady links (a 1.5 MB news page on emulated hardware, a big image over a
  25. * poor connection) must not be cut off after a minute. */
  26. #define UHTTP_TOTAL_TIMEOUT_MS 300000
  27. /* uhttp_open flags. */
  28. #define UHTTP_GZIP 1 /* send Accept-Encoding: gzip (caller decodes) */
  29. /* Error codes (uhttp_open/uhttp_read return 0 on success). */
  30. #define UHTTP_ERR_URL (-1)
  31. #define UHTTP_ERR_NIC (-2)
  32. #define UHTTP_ERR_IP (-3)
  33. #define UHTTP_ERR_BUSY (-4)
  34. #define UHTTP_ERR_DNS (-5)
  35. #define UHTTP_ERR_ARP (-6)
  36. #define UHTTP_ERR_CONNECT (-7)
  37. #define UHTTP_ERR_REFUSED (-8)
  38. #define UHTTP_ERR_TLS (-9)
  39. #define UHTTP_ERR_SEND (-10)
  40. #define UHTTP_ERR_TIMEOUT (-11)
  41. #define UHTTP_ERR_RESET (-12)
  42. #define UHTTP_ERR_REDIRECT (-13)
  43. #define UHTTP_ERR_TOOMANY (-14)
  44. #define UHTTP_ERR_TOOLONG (-15)
  45. struct uhttp_url {
  46. char host[UHTTP_HOST_MAX];
  47. char path[UHTTP_PATH_MAX];
  48. unsigned int port;
  49. int secure;
  50. };
  51. struct uhttp_response {
  52. int status;
  53. int chunked;
  54. int have_length;
  55. unsigned int content_length;
  56. int gzip; /* Content-Encoding: gzip */
  57. char content_type[64];
  58. char content_encoding[16];
  59. char final_url[UHTTP_HOST_MAX + UHTTP_PATH_MAX + 16];
  60. };
  61. /* Optional callbacks; curl uses them for its existing verbose output, the
  62. * browser passes NULL. */
  63. struct uhttp_hooks {
  64. void (*connected)(void *ctx, int secure);
  65. void (*status)(void *ctx, int code, const char *line);
  66. void (*header)(void *ctx, const char *line);
  67. void (*redirect)(void *ctx, const char *location);
  68. void *ctx;
  69. };
  70. struct uhttp {
  71. struct uhttp_url url;
  72. struct uhttp_response resp;
  73. const struct uhttp_hooks *hooks;
  74. const char *user_agent;
  75. const char *method; /* NULL means GET */
  76. const char *body; /* request body for POST */
  77. unsigned int body_len;
  78. const char *content_type;
  79. /* Optional cookie jar: every Set-Cookie (redirects included) is stored
  80. * and matching cookies are sent with each request and redirect hop.
  81. * The jar outlives the connection, so reconnects keep the session. */
  82. struct uhttp_jar *jar;
  83. const char *accept; /* Accept header; NULL means star/star */
  84. const char *extra_headers; /* verbatim "Name: value\r\n" lines */
  85. unsigned char buf[UHTTP_READER_MAX];
  86. unsigned int len, pos;
  87. unsigned int total_deadline;
  88. int eof;
  89. int error; /* UHTTP_ERR_* once a read failed */
  90. int connected;
  91. int keepalive; /* reuse the TCP/TLS connection */
  92. int peer_close; /* response asked to close it */
  93. /* chunked transfer state */
  94. int chunk_state; /* 0 size line, 1 data, 2 crlf, 3 trailers */
  95. unsigned int chunk_left;
  96. unsigned int body_read;
  97. };
  98. void uhttp_init(struct uhttp *h);
  99. /* Select the request method and body. Call after uhttp_init() and before
  100. * uhttp_open(); NULL method restores GET. */
  101. void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
  102. unsigned int body_len, const char *content_type);
  103. int uhttp_parse_url(const char *s, struct uhttp_url *u);
  104. int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
  105. const struct uhttp_hooks *hooks);
  106. int uhttp_read(struct uhttp *h, void *buf, unsigned int max,
  107. unsigned int timeout_ms);
  108. void uhttp_close(struct uhttp *h);
  109. const char *uhttp_error_string(int err);
  110. /* Resolve an href against a base URL (absolute, absolute-path, relative and
  111. * scheme-relative forms). 0 on success. */
  112. int uhttp_resolve(const char *base, const char *href, char *out,
  113. unsigned int cap);
  114. #endif