luat_http.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "luat_base.h"
  2. // #define LUAT_HTTP_METHOD_GET 1
  3. // #define LUAT_HTTP_METHOD_POST 2
  4. // #define LUAT_HTTP_METHOD_PUT 3
  5. // #define LUAT_HTTP_METHOD_DELETE 4
  6. #define LUAT_HTTP_MAX_REQ_HEADER_COUNT (8)
  7. #define LUAT_HTTP_MAX_RESP_HEADER_COUNT (16)
  8. enum LUAT_HTTP_METHOD {
  9. GET,
  10. POST,
  11. PUT,
  12. DELETE
  13. };
  14. // typedef struct luat_http_header
  15. // {
  16. // uint16_t name_len;
  17. // uint16_t value_len;
  18. // char buff[4];
  19. // }luat_http_header_t;
  20. typedef struct luat_http_ctx {
  21. char* host;
  22. uint16_t port;
  23. int method;
  24. uint8_t ssl;
  25. char* uri;
  26. char* server_ca;
  27. char* client_ca;
  28. char* req_body;
  29. int req_body_size;
  30. uint8_t req_body_type; // 0 - None, 1 - Binary , 3 - RAW_FILE, 4 - MULITE-FILE
  31. void* userdata;
  32. size_t req_headers_size;
  33. char* req_headers[LUAT_HTTP_MAX_REQ_HEADER_COUNT];
  34. void* network;
  35. // 响应类
  36. int status_code;
  37. int total_body_size;
  38. char* resp_body;
  39. char* resp_body_size;
  40. uint8_t resp_body_end;
  41. size_t resp_headers_size;
  42. char* resp_headers[LUAT_HTTP_MAX_RESP_HEADER_COUNT];
  43. } luat_http_ctx_t;
  44. typedef void (*http_cb)(luat_http_ctx_t *ctx);
  45. int luat_http_init(luat_http_ctx_t *ctx);
  46. int luat_http_set_url(luat_http_ctx_t *ctx, char* url, int method);
  47. int luat_http_set_ca(luat_http_ctx_t *ctx, char* server_ca, char* client_ca);
  48. int luat_http_add_header(luat_http_ctx_t *ctx, char* name, char* value);
  49. int luat_http_set_header(luat_http_ctx_t *ctx, char* name, char* value);
  50. int luat_http_set_body(luat_http_ctx_t *ctx, uint8_t body_type, char* body, int bodylen);
  51. int luat_http_send(luat_http_ctx_t *ctx, http_cb cb);
  52. int luat_http_uninit(luat_http_ctx_t *ctx);