http.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. #ifndef SCUFENG_HTTP_H_
  2. #define SCUFENG_HTTP_H_
  3. #include <netinet/in.h>
  4. #include <stdint.h>
  5. #include "scufcont/DArray.h"
  6. #include "scufcont/DString.h"
  7. #include "scufeng/engine.h"
  8. #define SE_HTTP_METHODS_X(X) \
  9. X(SE_METHOD_GET) \
  10. X(SE_METHOD_HEAD) \
  11. X(SE_METHOD_POST) \
  12. X(SE_METHOD_PUT) \
  13. X(SE_METHOD_DELETE) \
  14. X(SE_METHOD_CONNECT) \
  15. X(SE_METHOD_OPTIONS) \
  16. X(SE_METHOD_TRACE) \
  17. X(SE_METHOD_PATCH) \
  18. X(SE_METHOD_QUERY)
  19. #define X(method) method,
  20. typedef enum : uint8_t {
  21. SE_HTTP_METHODS_X(X)
  22. } SEHttpMethod;
  23. #undef X
  24. typedef struct {
  25. char *key;
  26. char *value;
  27. } SEHttpHeader;
  28. SCDARRAY_HEADER(SEHttpHeader, SEHttpHeader)
  29. typedef struct {
  30. in_port_t port;
  31. SEHttpMethod method;
  32. SCDString uri;
  33. SCDArray_SEHttpHeader headers;
  34. uint8_t *body;
  35. size_t body_len;
  36. } SEHttpRequest;
  37. // typedef struct {
  38. //
  39. // } SEHttpResponse;
  40. const char *SEHttpMethod_to_str(const SEHttpMethod method);
  41. /**
  42. @return SE_OUT_OF_MEMORY
  43. @return SE_SUCCESS
  44. */
  45. SEResult SEHttpHeader_alloc(
  46. SEHttpHeader *const header,
  47. const char *key,
  48. const size_t key_len,
  49. const char *value,
  50. const size_t value_len
  51. );
  52. void SEHttpHeader_free(SEHttpHeader *const header);
  53. void SEHttpHeader_print(const SEHttpHeader *const header, FILE *o);
  54. size_t SEHttpHeader_data_size(const SEHttpHeader *const header);
  55. size_t SEHttpHeader_data(const SEHttpHeader *const header, uint8_t *const buf);
  56. /**
  57. @return SE_OUT_OF_MEMORY
  58. @return SE_SUCCESS
  59. */
  60. SEResult SEHttpRequest_header_add(SEHttpRequest *const req, const SEHttpHeader *const header);
  61. /**
  62. @return SE_OUT_OF_MEMORY
  63. @return SE_SUCCESS
  64. */
  65. SEResult SEHttpRequest_header_add_c_str(
  66. SEHttpRequest *const req,
  67. const char *key,
  68. const size_t key_len,
  69. const char *value,
  70. const size_t value_len
  71. );
  72. /**
  73. @return SE_OUT_OF_MEMORY
  74. @return SE_SUCCESS
  75. */
  76. SEResult SEHttpRequest_alloc(
  77. SEHttpRequest *const req,
  78. const char *host,
  79. const size_t host_len,
  80. const in_port_t port,
  81. const SEHttpMethod method,
  82. const char *uri,
  83. const size_t uri_len,
  84. const uint8_t *body,
  85. const size_t body_len
  86. );
  87. void SEHttpRequest_free(SEHttpRequest *const req);
  88. void SEHttpRequest_print(const SEHttpRequest *const req, FILE *o);
  89. size_t SEHttpRequest_headers_data_size(const SEHttpRequest *const req);
  90. size_t SEHttpRequest_data_size(const SEHttpRequest *const req);
  91. size_t SEHttpRequest_data(
  92. const SEHttpRequest *const req,
  93. uint8_t *const buf
  94. );
  95. /**
  96. @return SE_SUCCESS
  97. @return SE_ERRNO
  98. @return SE_GAI
  99. @return SE_COULD_NOT_CONNECT_TO_HOST
  100. @return SE_COULD_NOT_FIND_ADDRESS
  101. @return SE_OUT_OF_MEMORY
  102. */
  103. SEResult SEHttpRequest_send(const SEHttpRequest *const req, int *const gai_err);
  104. /**
  105. @return SE_SUCCESS
  106. @return SE_ERRNO
  107. @return SE_GAI
  108. @return SE_COULD_NOT_CONNECT_TO_HOST
  109. @return SE_COULD_NOT_FIND_ADDRESS
  110. @return SE_OUT_OF_MEMORY
  111. */
  112. SEResult se_http_request_send(
  113. const char *const host,
  114. const size_t host_len,
  115. const in_port_t port,
  116. const SEHttpMethod method,
  117. const char *const uri,
  118. const size_t uri_len,
  119. const char *const *const headers,
  120. const size_t headers_len,
  121. const uint8_t *const body,
  122. const size_t body_len,
  123. int *const gai_err
  124. );
  125. #endif // SCUFENG_HTTP_H_