vessel/old/response.h
2025-06-13 17:58:13 +03:00

51 lines
1.9 KiB
C

#ifndef _VESSEL_RESPONSE_H
#define _VESSEL_RESPONSE_H
#include "conf.h"
#include "def.h"
#include "http.h"
#include "hmap.h"
#include "stream.h"
#ifndef RESPONSE_SENDFILE_BUFSZ
# define RESPONSE_SENDFILE_BUFSZ 32768
#endif /* RESPONSE_SENDFILE_BUFSZ */
/* NOTE: Responses are meant to be accessed in a single-threaded context. */
typedef struct {
uint16_t code;
char version[HTTP_VERSION_LENGTH + 1];
Stream *fp;
HMap header_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. */
} HTTPResponse;
/* TODO: Chunked response helper */
Bool HTTPResponse_init(HTTPResponse *r, Stream *fp);
uint64_t HTTPResponse_begin(HTTPResponse *r,
const char *version,
const uint16_t code,
const char *connection);
uint64_t HTTPResponse_header(HTTPResponse *r, const char *key, const char *value);
uint64_t HTTPResponse_headerf(HTTPResponse *r, const char *key, const char *fmt, ...)
__attribute__((format(printf, 3, 4)));
uint64_t HTTPResponse_headers_end(HTTPResponse *r);
uint64_t HTTPResponse_content_and_length(
HTTPResponse *r, const char *content); /* Automatically calls HTTPResponse_headers_end() */
uint64_t HTTPResponse_content_and_lengthf(HTTPResponse *r, const char *fmt, ...)
__attribute__((format(printf, 2, 3))); /* Automatically calls HTTPResponse_headers_end() */
uint64_t HTTPResponse_content_sendfile(
HTTPResponse *r,
const char *mimetype,
const char *path,
const char *filename); /* Automatically calls HTTPResponse_headers_end() */
Bool HTTPResponse_destroy(HTTPResponse *r);
#endif /* _VESSEL_RESPONSE_H */