uhttp.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 2048
  20. #define UHTTP_REQ_MAX 4096 /* request line + headers */
  21. #define UHTTP_MAX_REDIRECTS 8
  22. #define UHTTP_IDLE_TIMEOUT_MS 10000
  23. #define UHTTP_TOTAL_TIMEOUT_MS 60000
  24. /* uhttp_open flags. */
  25. #define UHTTP_GZIP 1 /* send Accept-Encoding: gzip (caller decodes) */
  26. /* Error codes (uhttp_open/uhttp_read return 0 on success). */
  27. #define UHTTP_ERR_URL (-1)
  28. #define UHTTP_ERR_NIC (-2)
  29. #define UHTTP_ERR_IP (-3)
  30. #define UHTTP_ERR_BUSY (-4)
  31. #define UHTTP_ERR_DNS (-5)
  32. #define UHTTP_ERR_ARP (-6)
  33. #define UHTTP_ERR_CONNECT (-7)
  34. #define UHTTP_ERR_REFUSED (-8)
  35. #define UHTTP_ERR_TLS (-9)
  36. #define UHTTP_ERR_SEND (-10)
  37. #define UHTTP_ERR_TIMEOUT (-11)
  38. #define UHTTP_ERR_RESET (-12)
  39. #define UHTTP_ERR_REDIRECT (-13)
  40. #define UHTTP_ERR_TOOMANY (-14)
  41. #define UHTTP_ERR_TOOLONG (-15)
  42. struct uhttp_url {
  43. char host[UHTTP_HOST_MAX];
  44. char path[UHTTP_PATH_MAX];
  45. unsigned int port;
  46. int secure;
  47. };
  48. struct uhttp_response {
  49. int status;
  50. int chunked;
  51. int have_length;
  52. unsigned int content_length;
  53. int gzip; /* Content-Encoding: gzip */
  54. char content_type[64];
  55. char content_encoding[16];
  56. char final_url[UHTTP_HOST_MAX + UHTTP_PATH_MAX + 16];
  57. };
  58. /* Optional callbacks; curl uses them for its existing verbose output, the
  59. * browser passes NULL. */
  60. struct uhttp_hooks {
  61. void (*connected)(void *ctx, int secure);
  62. void (*status)(void *ctx, int code, const char *line);
  63. void (*header)(void *ctx, const char *line);
  64. void (*redirect)(void *ctx, const char *location);
  65. void *ctx;
  66. };
  67. struct uhttp {
  68. struct uhttp_url url;
  69. struct uhttp_response resp;
  70. const struct uhttp_hooks *hooks;
  71. const char *user_agent;
  72. const char *method; /* NULL means GET */
  73. const char *body; /* request body for POST */
  74. unsigned int body_len;
  75. const char *content_type;
  76. /* Optional cookie jar: every Set-Cookie (redirects included) is stored
  77. * and matching cookies are sent with each request and redirect hop.
  78. * The jar outlives the connection, so reconnects keep the session. */
  79. struct uhttp_jar *jar;
  80. const char *accept; /* Accept header; NULL means star/star */
  81. const char *extra_headers; /* verbatim "Name: value\r\n" lines */
  82. unsigned char buf[UHTTP_READER_MAX];
  83. unsigned int len, pos;
  84. unsigned int total_deadline;
  85. int eof;
  86. int error; /* UHTTP_ERR_* once a read failed */
  87. int connected;
  88. int keepalive; /* reuse the TCP/TLS connection */
  89. int peer_close; /* response asked to close it */
  90. /* chunked transfer state */
  91. int chunk_state; /* 0 size line, 1 data, 2 crlf, 3 trailers */
  92. unsigned int chunk_left;
  93. unsigned int body_read;
  94. };
  95. void uhttp_init(struct uhttp *h);
  96. /* Select the request method and body. Call after uhttp_init() and before
  97. * uhttp_open(); NULL method restores GET. */
  98. void uhttp_set_method(struct uhttp *h, const char *method, const char *body,
  99. unsigned int body_len, const char *content_type);
  100. int uhttp_parse_url(const char *s, struct uhttp_url *u);
  101. int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
  102. const struct uhttp_hooks *hooks);
  103. int uhttp_read(struct uhttp *h, void *buf, unsigned int max,
  104. unsigned int timeout_ms);
  105. void uhttp_close(struct uhttp *h);
  106. const char *uhttp_error_string(int err);
  107. /* Resolve an href against a base URL (absolute, absolute-path, relative and
  108. * scheme-relative forms). 0 on success. */
  109. int uhttp_resolve(const char *base, const char *href, char *out,
  110. unsigned int cap);
  111. #endif