2023-01-09 01:22:54 +01:00
|
|
|
#ifndef HTTP_H
|
|
|
|
#define HTTP_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
struct http_payload
|
|
|
|
{
|
|
|
|
enum http_op
|
|
|
|
{
|
|
|
|
HTTP_OP_GET,
|
|
|
|
HTTP_OP_POST
|
|
|
|
} op;
|
|
|
|
|
|
|
|
const char *resource;
|
|
|
|
|
|
|
|
struct http_cookie
|
|
|
|
{
|
|
|
|
const char *field, *value;
|
|
|
|
} cookie;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
struct http_post
|
|
|
|
{
|
|
|
|
bool expect_continue;
|
|
|
|
const void *data;
|
|
|
|
size_t n;
|
|
|
|
const char *dir;
|
|
|
|
|
|
|
|
const struct http_post_file
|
|
|
|
{
|
|
|
|
const char *tmpname, *filename;
|
|
|
|
} *files;
|
|
|
|
} post;
|
|
|
|
} u;
|
Support URL parameters
Now, http_payload includes a list of human-readable parameters that can
be read (but not modified) by users. Given the following example link:
/test?key1=value1&key2=value2
This will generate two parameters, with the following values:
{
.args =
{
[0] = {.key = "key1", .value = "value1"},
[1] = {.key = "key2", .value = "value2"}
},
.n_args = 2
}
As expected, if any URL parameters are given, struct http_payload member
"resource" is accordingly trimmed so as not to include any parameters.
Therefore, considering the example above:
{.args = {...}, .resource = "/test"}
Limitations:
- Since the definition of struct http_arg is both shared by http.h
(as a read-only pointer within struct http_payload) and http.c
(as a read/write pointer within struct ctx), its members (namely key
and value) must remain as read/write pointers, even if they must not
be modified by users of http.h.
2023-04-23 05:09:53 +02:00
|
|
|
|
|
|
|
const struct http_arg
|
|
|
|
{
|
|
|
|
char *key, *value;
|
|
|
|
} *args;
|
|
|
|
|
|
|
|
size_t n_args;
|
2023-01-09 01:22:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define HTTP_STATUSES \
|
|
|
|
X(CONTINUE, "Continue", 100) \
|
|
|
|
X(OK, "OK", 200) \
|
|
|
|
X(SEE_OTHER, "See other", 303) \
|
|
|
|
X(BAD_REQUEST, "Bad Request", 400) \
|
|
|
|
X(UNAUTHORIZED, "Unauthorized", 401) \
|
|
|
|
X(FORBIDDEN, "Forbidden", 403) \
|
|
|
|
X(NOT_FOUND, "Not found", 404) \
|
2023-03-20 03:32:00 +01:00
|
|
|
X(PAYLOAD_TOO_LARGE, "Payload too large", 413) \
|
2023-01-09 01:22:54 +01:00
|
|
|
X(INTERNAL_ERROR, "Internal Server Error", 500)
|
|
|
|
|
|
|
|
struct http_response
|
|
|
|
{
|
|
|
|
enum http_status
|
|
|
|
{
|
|
|
|
#define X(x, y, z) HTTP_STATUS_##x,
|
|
|
|
HTTP_STATUSES
|
|
|
|
#undef X
|
|
|
|
} status;
|
|
|
|
|
|
|
|
struct http_header
|
|
|
|
{
|
|
|
|
char *header, *value;
|
|
|
|
} *headers;
|
|
|
|
|
|
|
|
union
|
|
|
|
{
|
|
|
|
const void *ro;
|
|
|
|
void *rw;
|
|
|
|
} buf;
|
|
|
|
|
|
|
|
FILE *f;
|
|
|
|
unsigned long long n;
|
|
|
|
size_t n_headers;
|
|
|
|
void (*free)(void *);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct http_cfg
|
|
|
|
{
|
|
|
|
int (*read)(void *buf , size_t n, void *user);
|
|
|
|
int (*write)(const void *buf, size_t n, void *user);
|
|
|
|
int (*payload)(const struct http_payload *p, struct http_response *r,
|
|
|
|
void *user);
|
Implement user quota
This feature allows admins to set a specific quota for each user, in
MiB. This feature is particularly useful for shared instances, where
unlimited user storage might be unfeasible or even dangerous for the
server.
Also, a nice HTML5 <progress> element has been added to the site that
shows how much of the quota has been consumed.
If no quota is set, slcl falls back to the default behaviour i.e.,
assume unlimited storage.
Limitations:
- While HTTP does specify a Content-Length, which determines the length
of the whole request, it does not specify how many files are involved
or their individual sizes.
- Because of this, if multiple files are uploaded simultaneously, the
whole request would be dropped if user quota is exceeded, even if not
all files exceeded it.
- Also, Content-Length adds the length of some HTTP boilerplate
(e.g.: boundaries), but slcl must rely on this before accepting the
whole request. In other words, this means some requests might be
rejected by slcl because of the extra bytes caused by such boilerplate.
- When the quota is exceeded, slcl must close the connection so that
the rest of the transfer is cancelled. Unfortunately, this means no
HTML can be sent back to the customer to inform about the situation.
2023-03-06 05:09:56 +01:00
|
|
|
int (*length)(unsigned long long len, const struct http_cookie *c,
|
2023-03-20 03:32:00 +01:00
|
|
|
struct http_response *r, void *user);
|
2023-01-09 01:22:54 +01:00
|
|
|
const char *tmpdir;
|
|
|
|
void *user;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct http_ctx *http_alloc(const struct http_cfg *cfg);
|
|
|
|
void http_free(struct http_ctx *h);
|
|
|
|
/* Positive return value: user input error, negative: fatal error. */
|
|
|
|
int http_update(struct http_ctx *h, bool *write, bool *close);
|
|
|
|
int http_response_add_header(struct http_response *r, const char *header,
|
|
|
|
const char *value);
|
|
|
|
char *http_cookie_create(const char *key, const char *value);
|
|
|
|
char *http_encode_url(const char *url);
|
2023-04-30 23:43:10 +02:00
|
|
|
char *http_decode_url(const char *url, bool spaces);
|
2023-01-09 01:22:54 +01:00
|
|
|
|
|
|
|
#endif /* HTTP_H */
|