vessel/web/include/request.h
Arija A. 7a848b778c
refact: Update formats of header guards.
Signed-off-by: Arija A. <ari@ari.lt>
2025-06-14 16:07:35 +03:00

74 lines
2.6 KiB
C

#ifndef VESSEL_WEB_REQUEST_H_
#define VESSEL_WEB_REQUEST_H_
#include "conf.h"
#include "http.h"
#include "httpbody.h"
#include <vessel/hmap.h>
#include <vessel/stream.h>
#include <stdint.h>
typedef enum {
vw_HTTPRequestParseError_ok = 0,
vw_HTTPRequestParseError_read,
vw_HTTPRequestParseError_format,
vw_HTTPRequestParseError_format_method,
vw_HTTPRequestParseError_format_sp,
vw_HTTPRequestParseError_format_path,
vw_HTTPRequestParseError_http_version,
vw_HTTPRequestParseError_format_header,
vw_HTTPRequestParseError_input,
vw_HTTPRequestParseError_content_length,
vw_HTTPRequestParseError_content_chunkiness,
} vw_HTTPRequestParseError;
typedef struct {
char method[VW_HTTP_METHOD_MAX_LENGTH];
char path[VW_HTTP_PATH_MAX_LENGTH];
char version[VW_HTTP_VERSION_LENGTH + 1];
vs_HMap headers;
size_t header_size;
vs_Stream *stream;
/* Write state */
struct {
uint16_t code; /* Response code */
vs_HMap headers_buf; /* Buffer for headers that were inserted during when
the response was not began. Destroyed after
began=true. */
bool began; /* Set to true after a single call of HTTPResponse_begin. */
bool headered; /* Set to true after no more headers can be written. */
} res;
vw_HTTPBody body; /* Response & request body (shared) */
bool keepalive; /* Keep-alive indicator */
/* Misc. */
char _buf[VW_HTTP_BUFFER_MAX_SIZE];
} vw_HTTPRequest;
bool vw_HTTPRequest_init(vw_HTTPRequest *http, vs_Stream *stream);
vw_HTTPRequestParseError vw_HTTPRequest_read(vw_HTTPRequest *http);
const char *vw_HTTPRequestParseError_to_str(vw_HTTPRequestParseError err) __attribute__((pure));
bool vw_HTTPRequest_destroy(vw_HTTPRequest *http);
size_t vw_HTTPRequest_res_begin(vw_HTTPRequest *http, uint16_t code);
size_t vw_HTTPRequest_res_header(vw_HTTPRequest *http, const char *key, const char *value);
size_t vw_HTTPRequest_res_headerf(vw_HTTPRequest *http, const char *key, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
size_t vw_HTTPRequest_res_headers_end(vw_HTTPRequest *http);
size_t vw_HTTPRequest_write(vw_HTTPRequest *http, const void *buf, size_t count);
size_t vw_HTTPRequest_write_chunk(vw_HTTPRequest *http, const void *buf, size_t count);
size_t vw_HTTPRequest_writef(vw_HTTPRequest *http, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
size_t vw_HTTPRequest_writef_chunk(vw_HTTPRequest *http, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
#endif /* VESSEL_WEB_REQUEST_H_ */