uhttp.h 3.8 KB

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