This repository has been archived by the owner on Jan 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
resp.T
400 lines (339 loc) · 10.1 KB
/
resp.T
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//-*-c++-*-
/* $Id$ */
/*
*
* Copyright (C) 2002-2004 Maxwell Krohn (max@okcupid.com)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA
*
*/
#include "resp.h"
#include "httpconst.h"
#include "parseopt.h"
http_status_set_t http_status;
//
// what this OKWS is called when sending responses back to clients;
// we can get sneaky here and call ourselves apache, for instance.
//
str global_okws_server_label = okws_server_label;
http_status_set_t::http_status_set_t ()
{
// 100 series
add (100, "Continue");
add (101, "Switching Protocols");
// 200 series
add (HTTP_OK, "OK");
add (201, "Created");
add (202, "Accepted");
add (203, "Non-Authoratative Information");
add (204, "No Content");
add (205, "Resent Content");
add (HTTP_PARTIAL_CONTENT, "Partial Content");
// 300 series
add (300, "Multiple Choices");
add (HTTP_MOVEDPERM, "Moved Permanently");
add (HTTP_REDIRECT, "Found");
add (HTTP_SEEOTHER, "See Other");
add (HTTP_NOT_MODIFIED, "Not Modified");
add (HTTP_USEPROXY, "Use Proxy");
add (307, "Temporary Redirect");
add (HTTP_BAD_REQUEST, "Bad Request");
// 400 series
add (401, "Unauthorized");
add (402, "Payment Required");
add (HTTP_FORBIDDEN, "Forbidden");
add (HTTP_NOT_FOUND, "Not Found");
add (HTTP_NOT_ALLOWED, "Method Not Allowed");
add (406, "Not Acceptable");
add (407, "Proxy Authentication Required");
add (HTTP_TIMEOUT, "Request Time-out");
add (409, "Conflict");
add (HTTP_GONE, "Gone");
add (411, "Length Required");
add (412, "Precondition Failed");
add (HTTP_REQ_TOO_BIG, "Request Entity Too Large");
add (HTTP_URI_TOO_BIG, "Request-URI Too Large");
add (415, "Unsupported Media Type");
add (416, "Requested range not satisfied");
add (HTTP_UNEXPECTED_EOF, "Expectation Failed");
// 500 series
add (HTTP_SRV_ERROR, "Internal Server Error");
add (HTTP_NOT_IMPLEMENTED, "Not Implemented");
add (HTTP_BAD_GATEWAY, "Bad Gateway");
add (HTTP_UNAVAILABLE, "Service Unavailable");
add (504, "Gateway Time-out");
add (505, "HTTP Version not supported");
add (HTTP_CLIENT_EOF, "Client EOF before response sent.");
}
//-----------------------------------------------------------------------
void
http_resp_header_t::fill ()
{
add_date ();
add ("Content-Type", attributes.get_content_type ());
add_connection ();
add ("Cache-control", attributes.get_cache_control ());
str tmp;
if ((tmp = attributes.get_expires ()))
add ("Expires", tmp);
if ((tmp = attributes.get_content_disposition ()))
add ("Content-Disposition", tmp);
add_server ();
add_content_delivery_headers ();
// anything else the user might have added; takes precedence
// over anything put above
cleanme = attributes.get_others (&fields);
}
//-----------------------------------------------------------------------
void
http_resp_header_t::add_connection ()
{
add ("Connection", attributes.get_connection ());
}
//-----------------------------------------------------------------------
void
http_resp_header_redirect_t::fill (const str &s)
{
add_date ();
add_connection ();
add_server ();
add ("Location", s);
// anything else the user might have added; takes precedence
// over anything put above
cleanme = attributes.get_others (&fields);
}
//-----------------------------------------------------------------------
void
http_resp_header_t::fill_outer (ssize_t len)
{
fill ();
if (!attributes.get_content_delivery().chunked && len >= 0) {
add (http_hdr_size_t (len));
}
}
//-----------------------------------------------------------------------
void
http_resp_header_t::add_content_delivery_headers ()
{
if (attributes.get_content_delivery ().mode != GZIP_NONE) {
add ("Content-Encoding", "gzip");
}
if (attributes.get_content_delivery ().chunked) {
add ("Transfer-Encoding", "chunked");
}
}
//-----------------------------------------------------------------------
void
http_resp_header_t::add_server() {
add ("Server", global_okws_server_label);
add ("X-OKWS-Version", global_okws_server_label);
}
//-----------------------------------------------------------------------
strbuf
http_resp_header_t::to_strbuf () const
{
strbuf b;
fill_strbuf (b);
return b;
}
//-----------------------------------------------------------------------
void
http_resp_header_t::fill_strbuf (strbuf &b) const
{
vec<bool> output_me;
b << "HTTP/";
switch (attributes.get_version ()) {
case 1:
b << "1.1 ";
break;
default:
b << "1.0 ";
break;
}
u_int status = attributes.get_status ();
b << status;
if (status == HTTP_OK)
b << " OK";
b << HTTP_CRLF;
int lim = fields.size ();
// 2 of the same field in, last in wins;
// eventually, some fields might allow multiple winners;
// this would be pretty easy to set up.
if (cleanme) {
output_me.setsize (lim);
bhash<str> winner_hsh;
for (int i = lim - 1; i >= 0; i--) {
if (fields[i].can_duplicate ()) {
output_me[i] = true;
} else {
str n = mytolower (fields[i].name);
if (winner_hsh[n]) {
output_me[i] = false;
} else {
winner_hsh.insert (n);
output_me[i] = true;
}
}
}
}
for (int i = 0; i < lim; i++) {
if (!cleanme || output_me[i]) {
const str &name = fields[i].name;
const str &val = fields[i].val;
// If the header's name or value contains a newline, drop it on the floor
if (
!strchr(name.cstr(), '\r') && !strchr(name.cstr(), '\n') &&
!strchr(val.cstr(), '\r') && !strchr(val.cstr(), '\n')
) { b << name << ": " << val << HTTP_CRLF; }
}
}
b << HTTP_CRLF;
}
//-----------------------------------------------------------------------
str
http_status_set_t::get_desc (int n, str *l) const
{
http_status_t *s = tab[n];
if (!s) {
if (l) *l = "";
return "";
}
if (l) *l = s->ldesc;
return s->sdesc;
}
//-----------------------------------------------------------------------
u_int
http_response_t::send (ptr<ahttpcon> x, cbv::ptr cb)
{
const strbuf &b = to_strbuf ();
u_int ret = b.tosuio ()->resid ();
x->send (b, cb);
return ret;
}
//-----------------------------------------------------------------------
tamed void
http_response_t::send2_T (ptr<ahttpcon> x, ev_ssize_t ev)
{
tvars {
ssize_t ret;
strbuf b;
}
header.fill_strbuf (b);
b.take (body);
twait {
x->send2 (b, mkevent (ret));
}
ev->trigger (ret);
}
//-----------------------------------------------------------------------
bool
http_resp_attributes_t::get_others (vec<http_hdr_field_t> *out)
{
if (_others) {
for (u_int i = 0; i < _others->size (); i++) {
out->push_back ((*_others)[i]);
}
return true;
}
return false;
}
//-----------------------------------------------------------------------
tamed void
http_pub_t::publish (ptr<pub3::remote_publisher_t> p, str fn,
evb_t ev, ptr<pub3::dict_t> env, htpv_t v,
compressible_t::opts_t o)
{
tvars {
zbuf b;
bool rc;
}
twait { p->run (&b, fn, mkevent (rc), env); }
if (rc) {
b.to_strbuf (&body, o);
}
// Fill in the headers once we've published the body...
header.fill_outer (body.len ());
ev->trigger (rc);
}
//-----------------------------------------------------------------------
typedef event<ptr<http_pub_t> >::ref http_pub2_ev_t;
//-----------------------------------------------------------------------
tamed static void
http_pub_t::alloc2 (ptr<pub3::remote_publisher_t> p,
const http_resp_attributes_t &hra, str fn,
http_pub2_ev_t ev, ptr<pub3::dict_t> env)
{
tvars {
ptr<http_pub_t> ret (New refcounted<http_pub_t> (hra));
bool rc;
}
twait {
ret->publish (p, fn, mkevent (rc), env, hra.get_version (),
hra.get_content_delivery ());
}
ev->trigger (rc ? ret : NULL);
}
//-----------------------------------------------------------------------
http_error_t::http_error_t (int n, const str &si, const str &aux, htpv_t v)
: http_response_t (http_resp_header_t (n, v), make_body (n, si, aux))
{
// If we're making a default body (via make_body), then disable
// gzip in the reply.
header.disable_gzip ();
header.fill_outer (nbytes);
}
//-----------------------------------------------------------------------
http_error_t::http_error_t (const http_resp_attributes_t &hra,
const str &si, const str &aux)
: http_response_t (http_resp_header_t (hra),
make_body (hra.get_status (), si, aux))
{
// If we're making a default body (via make_body), then disable
// gzip in the reply.
header.disable_gzip ();
header.fill_outer (nbytes);
}
//-----------------------------------------------------------------------
gzip_mode_t
ok_gzip_get_mode (const compressible_t &b, int v, bool do_gzip)
{
bool do_gz = b.inflated_len () > 0 && v > 0 && do_gzip;
return do_gz ? ok_gzip_mode : GZIP_NONE;
}
//-----------------------------------------------------------------------
http_resp_header_ok_t::http_resp_header_ok_t (ssize_t s,
const http_resp_attributes_t &a)
: http_resp_header_t (a)
{
fill_outer (s);
}
//-----------------------------------------------------------------------
strbuf
http_response_t::to_strbuf () const
{
strbuf b = header.to_strbuf ();
if (!is_head_request ()) {
b << body;
}
return b;
}
//-----------------------------------------------------------------------
void
http_resp_header_t::disable_gzip ()
{
attributes.get_content_delivery ().mode = GZIP_NONE;
}
//-----------------------------------------------------------------------