aboutsummaryrefslogtreecommitdiff
path: root/src/uvh.h
blob: b0bb60b70678ae6c54141b08f6488461ea5749d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef UVH_H
#define UVH_H

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <uv.h>

#define UVH_MAX_HEADERS 50

#define HTTP_STATUS_CODE_MAP(XX) \
    XX(100, CONTINUE, "Continue") \
    XX(101, SWITCHING_PROTOCOLS, "Switching Protocols") \
    XX(200, OK, "OK") \
    XX(201, CREATED, "Created") \
    XX(202, ACCEPTED, "Accepted") \
    XX(203, NON_AUTHORITATIVE_INFORMATION, "Non-Authoritative Information") \
    XX(204, NO_CONTENT, "No Content") \
    XX(205, RESET_CONTENT, "Reset Content") \
    XX(206, PARTIAL_CONTENT, "Partial Content") \
    XX(300, MULTIPLE_CHOICES, "Multiple Choices") \
    XX(301, MOVED_PERMANENTLY, "Moved Permanently") \
    XX(302, FOUND, "Found") \
    XX(303, SEE_OTHER, "See Other") \
    XX(304, NOT_MODIFIED, "Not Modified") \
    XX(305, USE_PROXY, "Use Proxy") \
    XX(307, TEMPORARY_REDIRECT, "Temporary Redirect") \
    XX(400, BAD_REQUEST, "Bad Request") \
    XX(401, UNAUTHORIZED, "Unauthorized") \
    XX(402, PAYMENT_REQUIRED, "Payment Required") \
    XX(403, FORBIDDEN, "Forbidden") \
    XX(404, NOT_FOUND, "Not Found") \
    XX(405, METHOD_NOT_ALLOWED, "Method Not Allowed") \
    XX(406, NOT_ACCEPTABLE, "Not Acceptable") \
    XX(407, PROXY_AUTHENTICATION_REQUIRED, "Proxy Authentication Required") \
    XX(408, REQUEST_TIMEOUT, "Request Timeout") \
    XX(409, CONFLICT, "Conflict") \
    XX(410, GONE, "Gone") \
    XX(411, LENGTH_REQUIRED, "Length Required") \
    XX(412, PRECONDITION_FAILED, "Precondition Failed") \
    XX(413, REQUEST_ENTITY_TOO_LARGE, "Request Entity Too Large") \
    XX(414, REQUEST_URI_TOO_LONG, "Request-URI Too Long") \
    XX(415, UNSUPPORTED_MEDIA_TYPE, "Unsupported Media Type") \
    XX(416, REQUESTED_RANGE_NOT_SATISFIABLE, "Requested Range Not Satisfiable") \
    XX(417, EXPECTATION_FAILED, "Expectation Failed") \
    XX(418, IM_A_TEAPOT, "I'm a teapot") /* ;-) */ \
    XX(500, INTERNAL_SERVER_ERROR, "Internal Server Error") \
    XX(501, NOT_IMPLEMENTED, "Not Implemented") \
    XX(502, BAD_GATEWAY, "Bad Gateway") \
    XX(503, SERVICE_UNAVAILABLE, "Service Unavailable") \
    XX(504, GATEWAY_TIMEOUT, "Gateway Timeout") \
    XX(505, HTTP_VERSION_NOT_SUPPORTED, "HTTP Version Not Supported")

enum
{
#define XX(CODE, NAME, STR) HTTP_##NAME = CODE,
    HTTP_STATUS_CODE_MAP(XX)
#undef XX
};

struct uvh_request;

typedef int (*uvh_request_handler_cb)(struct uvh_request *);

struct uvh_server
{
    void *data;
    uvh_request_handler_cb request_handler;
};

struct uvh_request
{
    struct uvh_server *server;

    struct
    {
        const char *name;
        const char *value;
    } headers[UVH_MAX_HEADERS];

    int header_count;

    const char *method;
    const char *version;

    struct
    {
        const char *full;
        const char *schema;
        const char *host;
        const char *port;
        const char *path;
        const char *query;
        const char *fragment;
        const char *userinfo;
    } url;

    const char *content;
    int content_length;
};

struct uvh_server *uvh_server_init(uv_loop_t *loop, void *data,
    uvh_request_handler_cb request_handler);

int uvh_server_listen(struct uvh_server *server, const char *address,
    short port);

void uvh_request_write(struct uvh_request *req, const char *data,
    size_t len);

#ifdef __GNUC__
void uvh_request_writef(struct uvh_request *req, const char *fmt, ...)
    __attribute__((format(printf, 2, 3)));
#else
void uvh_request_writef(struct uvh_request *req, const char *fmt, ...);
#endif

void uvh_request_write_status(struct uvh_request *req, int status);

void uvh_request_write_header(struct uvh_request *req,
    const char *name, const char *value);

const char *http_status_code_str(int code);

const char *uvh_request_get_header(struct uvh_request *req,
    const char *name);

#endif /* UVH_H */