|
|
@@ -0,0 +1,146 @@
|
|
|
+#ifndef SCUFENG_HTTP_H_
|
|
|
+#define SCUFENG_HTTP_H_
|
|
|
+
|
|
|
+#include <netinet/in.h>
|
|
|
+#include <stdint.h>
|
|
|
+
|
|
|
+#include "scufcont/DArray.h"
|
|
|
+#include "scufcont/DString.h"
|
|
|
+
|
|
|
+#include "scufeng/engine.h"
|
|
|
+
|
|
|
+#define SE_HTTP_METHODS_X(X) \
|
|
|
+ X(SE_METHOD_GET) \
|
|
|
+ X(SE_METHOD_HEAD) \
|
|
|
+ X(SE_METHOD_POST) \
|
|
|
+ X(SE_METHOD_PUT) \
|
|
|
+ X(SE_METHOD_DELETE) \
|
|
|
+ X(SE_METHOD_CONNECT) \
|
|
|
+ X(SE_METHOD_OPTIONS) \
|
|
|
+ X(SE_METHOD_TRACE) \
|
|
|
+ X(SE_METHOD_PATCH) \
|
|
|
+ X(SE_METHOD_QUERY)
|
|
|
+
|
|
|
+#define X(method) method,
|
|
|
+
|
|
|
+typedef enum : uint8_t {
|
|
|
+ SE_HTTP_METHODS_X(X)
|
|
|
+} SEHttpMethod;
|
|
|
+
|
|
|
+#undef X
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ char *key;
|
|
|
+ char *value;
|
|
|
+} SEHttpHeader;
|
|
|
+
|
|
|
+SCDARRAY_HEADER(SEHttpHeader, SEHttpHeader)
|
|
|
+
|
|
|
+typedef struct {
|
|
|
+ in_port_t port;
|
|
|
+ SEHttpMethod method;
|
|
|
+ SCDString uri;
|
|
|
+ SCDArray_SEHttpHeader headers;
|
|
|
+ uint8_t *body;
|
|
|
+ size_t body_len;
|
|
|
+} SEHttpRequest;
|
|
|
+
|
|
|
+// typedef struct {
|
|
|
+//
|
|
|
+// } SEHttpResponse;
|
|
|
+
|
|
|
+const char *SEHttpMethod_to_str(const SEHttpMethod method);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+ @return SE_SUCCESS
|
|
|
+*/
|
|
|
+SEResult SEHttpHeader_alloc(
|
|
|
+ SEHttpHeader *const header,
|
|
|
+ const char *key,
|
|
|
+ const size_t key_len,
|
|
|
+ const char *value,
|
|
|
+ const size_t value_len
|
|
|
+);
|
|
|
+
|
|
|
+void SEHttpHeader_free(SEHttpHeader *const header);
|
|
|
+void SEHttpHeader_print(const SEHttpHeader *const header, FILE *o);
|
|
|
+size_t SEHttpHeader_data_size(const SEHttpHeader *const header);
|
|
|
+size_t SEHttpHeader_data(const SEHttpHeader *const header, uint8_t *const buf);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+ @return SE_SUCCESS
|
|
|
+*/
|
|
|
+SEResult SEHttpRequest_header_add(SEHttpRequest *const req, const SEHttpHeader *const header);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+ @return SE_SUCCESS
|
|
|
+*/
|
|
|
+SEResult SEHttpRequest_header_add_c_str(
|
|
|
+ SEHttpRequest *const req,
|
|
|
+ const char *key,
|
|
|
+ const size_t key_len,
|
|
|
+ const char *value,
|
|
|
+ const size_t value_len
|
|
|
+);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+ @return SE_SUCCESS
|
|
|
+*/
|
|
|
+SEResult SEHttpRequest_alloc(
|
|
|
+ SEHttpRequest *const req,
|
|
|
+ const char *host,
|
|
|
+ const size_t host_len,
|
|
|
+ const in_port_t port,
|
|
|
+ const SEHttpMethod method,
|
|
|
+ const char *uri,
|
|
|
+ const size_t uri_len,
|
|
|
+ const uint8_t *body,
|
|
|
+ const size_t body_len
|
|
|
+);
|
|
|
+
|
|
|
+void SEHttpRequest_free(SEHttpRequest *const req);
|
|
|
+void SEHttpRequest_print(const SEHttpRequest *const req, FILE *o);
|
|
|
+size_t SEHttpRequest_headers_data_size(const SEHttpRequest *const req);
|
|
|
+size_t SEHttpRequest_data_size(const SEHttpRequest *const req);
|
|
|
+size_t SEHttpRequest_data(
|
|
|
+ const SEHttpRequest *const req,
|
|
|
+ uint8_t *const buf
|
|
|
+);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_SUCCESS
|
|
|
+ @return SE_ERRNO
|
|
|
+ @return SE_GAI
|
|
|
+ @return SE_COULD_NOT_CONNECT_TO_HOST
|
|
|
+ @return SE_COULD_NOT_FIND_ADDRESS
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+*/
|
|
|
+SEResult SEHttpRequest_send(const SEHttpRequest *const req, int *const gai_err);
|
|
|
+
|
|
|
+/**
|
|
|
+ @return SE_SUCCESS
|
|
|
+ @return SE_ERRNO
|
|
|
+ @return SE_GAI
|
|
|
+ @return SE_COULD_NOT_CONNECT_TO_HOST
|
|
|
+ @return SE_COULD_NOT_FIND_ADDRESS
|
|
|
+ @return SE_OUT_OF_MEMORY
|
|
|
+*/
|
|
|
+SEResult se_http_request_send(
|
|
|
+ const char *const host,
|
|
|
+ const size_t host_len,
|
|
|
+ const in_port_t port,
|
|
|
+ const SEHttpMethod method,
|
|
|
+ const char *const uri,
|
|
|
+ const size_t uri_len,
|
|
|
+ const char *const *const headers,
|
|
|
+ const size_t headers_len,
|
|
|
+ const uint8_t *const body,
|
|
|
+ const size_t body_len,
|
|
|
+ int *const gai_err
|
|
|
+);
|
|
|
+
|
|
|
+#endif // SCUFENG_HTTP_H_
|