-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.cc
177 lines (145 loc) · 3.75 KB
/
handler.cc
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "handler.h"
#include <civetweb.h>
#include <exception>
#include <lace/ropebuf.h>
#include <iostream>
#include <streambuf>
namespace {
class consumer : public __gnu_cxx::_Rope_char_consumer<char> {
public:
consumer(mg_connection *c) : conn(c), bytes(0) { }
bool
operator()(const char* buf, size_t len) {
int b = mg_write(conn, buf, len);
if (b > 0)
bytes += b;
return b > 0;
}
int
apply(lace::ropebuf &buf) {
buf.rope().apply_to_pieces(0, buf.rope().length(), *this);
return bytes;
}
private:
struct mg_connection *conn;
int bytes;
};
class buffer : public std::basic_streambuf<char, std::char_traits<char> > {
public:
typedef std::basic_streambuf<char, std::char_traits<char> > streambuf_type;
typedef typename streambuf_type::char_type char_type;
typedef typename streambuf_type::traits_type traits_type;
typedef typename streambuf_type::int_type int_type;
typedef typename streambuf_type::pos_type pos_type;
typedef typename streambuf_type::off_type off_type;
buffer(mg_connection * c) : _(c), x(traits_type::eof()), len(0) {
if (const mg_request_info *req = mg_get_request_info(_))
len = req->content_length;
}
std::streamsize showmanyc() {
std::streamsize s = len + (traits_type::eof() != x);
return s > 0 ? s : traits_type::eof();
}
std::streamsize
xsgetn(char_type *s, std::streamsize n) {
std::streamsize ret = 0;
if (traits_type::eof() != x) {
*s++ = x;
--n;
x = traits_type::eof();
ret += 1;
len -= 1;
}
if (n > 0) {
int r = mg_read(_, s, n);
if (r >= 0) {
ret += r;
len -= r;
} else {
len = 0;
}
}
return ret;
}
int_type
underflow() {
if (traits_type::eof() != x)
return x;
char c;
if (1 == mg_read(_, &c, 1)) {
x = c;
--len;
} else {
len = 0;
}
return x;
}
int_type
uflow() {
if (traits_type::eof() != x) {
char c = x;
x = traits_type::eof();
return c;
}
char c;
if (0 < mg_read(_, &c, 1)) {
--len;
return c;
} else {
len = 0;
}
return traits_type::eof();
}
int_type
pbackfail(int_type c = traits_type::eof()) {
if (traits_type::eof() == x)
return x = c;
return traits_type::eof();
}
private:
mg_connection *_;
std::streamsize len;
int_type x;
};
}
void
handler::install(mg_context * mg, const std::string & path) {
mg_set_request_handler(mg, (prefix + path).c_str(), &handler::wrapper, this);
}
int
handler::wrapper(mg_connection *conn, void *p) try {
lace::ropebuf obuf;
auto & h = *reinterpret_cast<handler*>(p);
int rv = 0;
{{
buffer ibuf(conn);
std::ostream os(&obuf);
std::istream is(&ibuf);
rv = h(conn, os, is);
}}
switch (rv) {
case 0:
break;
case 200:
mg_send_http_ok(conn, "text/plain; charset=utf-8", obuf.rope().length());
consumer(conn).apply(obuf);
break;
case 301:
case 302:
case 303:
case 307:
case 308:
mg_send_http_redirect(conn, obuf.rope().replace_with_c_str(), rv);
break;
case 404:
mg_send_http_error(conn, rv, "%s", "Not found");
break;
default:
throw std::runtime_error("error");
}
return rv;
} catch(const std::exception &e) {
mg_send_http_error(conn, 500, "%s\n", e.what());
return 500;
}
//