-
Notifications
You must be signed in to change notification settings - Fork 2
/
modserver.h
140 lines (104 loc) · 3.49 KB
/
modserver.h
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
130
131
132
133
134
135
136
137
138
139
140
#ifndef MODSERVER_H
#define MODSERVER_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
Each API function takes an opaque pointer. Do not free this.
*/
typedef struct servlet servlet;
/*
Return the value associated with the given key of the query part of the URI, or NULL if
no such key is found.
The returned string is NULL terminated. The application must not free the value.
// Example:
// For a given URI of /?key1=value1&key2=value2
const char *val = get_arg(s, "key1");
assert(strcmp(val, "value1") == 0);
*/
const char* get_arg(servlet *s, const char *name);
/*
Return the HTTP request method.
The returned string is NULL terminated. The application must not free the value.
// Example:
const char *method = get_method(s);
assert(strcmp(method, "GET") == 0);
*/
const char* get_method(servlet *s);
/*
Return the value associated with the given HTTP request header name.
HTTP header names are case-insensitive and so is the name argument.
The returned string is NULL terminated. The application must not free the value.
// Example:
const char *val0 = get_header(s, "User-Agent");
const char *val1 = get_header(s, "user-agent");
assert(strcmp(val0, val1) == 0);
*/
const char* get_header(servlet *s, const char *name);
/*
Set the HTTP response status code.
The reason phrase is automatically set based on the integer status code.
The default status code of 200 is automatically set by the server and does not have to be
set by the application.
// Example:
// 404 Not Found
set_status(s, 404);
*/
void set_status(servlet *s, int status);
/*
Set an HTTP response header.
HTTP header names are case-insensitive. Setting the same header again replaces the previous
header.
Internal copies are made of the name and value.
// Example:
set_header(s, "Set-Cookie", "key=value");
set_header(s, "Content-Type", "text/plain; charset=UTF-8");
unsigned length = 42;
char str_length[128];
snprintf(str_length, sizeof(str_length), "%u", length);
set_header(s, "Content-Length", str_length);
*/
void set_header(servlet *s, const char *name, const char *value);
/*
Write the given buffer to the output stream.
The first write to the output stream generates the HTTP status line and headers. Further
changes to the status or headers is not possible once the headers are sent. Chunked
transfer encoding is used by default when the Content-Length header is not set.
The output stream is fully buffered and is automatically flushed when it grows too large.
One may use rflush() to manually flush the output stream to the user.
// Example:
const char reply[] = "hello world";
rwrite(s, reply, sizeof(reply) - 1);
*/
size_t rwrite(servlet *s, const char *buffer, size_t length);
/*
Write to the output stream according to the given printf format string and arguments.
See the documentation of rwrite() for the behavior of rprintf() with regard to the output
stream.
// Example:
rprintf(s, "The number is <b>%i</b>\n", 42);
const char reply[] = "hello world";
rprintf(s, "%.*s", (int)sizeof(reply) - 1, reply);
// Equivalent to
// rwrite(s, buf, sizeof(buf) - 1);
*/
int rprintf(servlet *s, const char *format, ...);
/*
Write all the buffered data in the output stream to the user.
rflush() is automatically called when the application returns.
// Example:
for (int count = 0; count < 5; ++count)
{
rprintf(s, "count: %i\n", count);
// Without the rflush() the user would have to wait 5 seconds to see any output.
rflush(s);
sleep(1);
}
rprintf(s, "done\n");
*/
void rflush(servlet *s);
#ifdef __cplusplus
}
#endif
#endif