| | varnish-cache/bin/varnishd/http1/cache_http1_fsm.c |
0 |
|
/*- |
1 |
|
* Copyright (c) 2006 Verdens Gang AS |
2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
3 |
|
* All rights reserved. |
4 |
|
* |
5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
6 |
|
* |
7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
8 |
|
* |
9 |
|
* Redistribution and use in source and binary forms, with or without |
10 |
|
* modification, are permitted provided that the following conditions |
11 |
|
* are met: |
12 |
|
* 1. Redistributions of source code must retain the above copyright |
13 |
|
* notice, this list of conditions and the following disclaimer. |
14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
15 |
|
* notice, this list of conditions and the following disclaimer in the |
16 |
|
* documentation and/or other materials provided with the distribution. |
17 |
|
* |
18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
28 |
|
* SUCH DAMAGE. |
29 |
|
* |
30 |
|
* This file contains the two central state machine for pushing HTTP1 |
31 |
|
* sessions through their states. |
32 |
|
* |
33 |
|
*/ |
34 |
|
|
35 |
|
#include "config.h" |
36 |
|
|
37 |
|
#include <stdio.h> |
38 |
|
#include <stdlib.h> |
39 |
|
|
40 |
|
#include "cache/cache_varnishd.h" |
41 |
|
#include "cache/cache_objhead.h" |
42 |
|
#include "cache/cache_transport.h" |
43 |
|
#include "cache_http1.h" |
44 |
|
|
45 |
|
#include "vtcp.h" |
46 |
|
|
47 |
|
static const char H1NEWREQ[] = "HTTP1::NewReq"; |
48 |
|
static const char H1PROC[] = "HTTP1::Proc"; |
49 |
|
static const char H1CLEANUP[] = "HTTP1::Cleanup"; |
50 |
|
|
51 |
|
static void HTTP1_Session(struct worker *, struct req *); |
52 |
|
|
53 |
|
static void |
54 |
447363 |
http1_setstate(const struct sess *sp, const char *s) |
55 |
|
{ |
56 |
|
uintptr_t p; |
57 |
|
|
58 |
447363 |
p = (uintptr_t)s; |
59 |
447363 |
AZ(SES_Set_proto_priv(sp, &p)); |
60 |
447363 |
} |
61 |
|
|
62 |
|
static const char * |
63 |
533676 |
http1_getstate(const struct sess *sp) |
64 |
|
{ |
65 |
|
uintptr_t *p; |
66 |
|
|
67 |
533676 |
AZ(SES_Get_proto_priv(sp, &p)); |
68 |
533676 |
return ((const char *)*p); |
69 |
|
} |
70 |
|
|
71 |
|
/*-------------------------------------------------------------------- |
72 |
|
* Call protocol for this request |
73 |
|
*/ |
74 |
|
|
75 |
|
static void v_matchproto_(task_func_t) |
76 |
90042 |
http1_req(struct worker *wrk, void *arg) |
77 |
|
{ |
78 |
|
struct req *req; |
79 |
|
|
80 |
90042 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
81 |
90042 |
CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC); |
82 |
|
|
83 |
90042 |
THR_SetRequest(req); |
84 |
90042 |
assert(!WS_IsReserved(wrk->aws)); |
85 |
90042 |
HTTP1_Session(wrk, req); |
86 |
90042 |
AZ(wrk->v1l); |
87 |
90042 |
WS_Assert(wrk->aws); |
88 |
90042 |
THR_SetRequest(NULL); |
89 |
90042 |
} |
90 |
|
|
91 |
|
/*-------------------------------------------------------------------- |
92 |
|
* Call protocol for this session (new or from waiter) |
93 |
|
* |
94 |
|
* When sessions are rescheduled from the waiter, a struct pool_task |
95 |
|
* is put on the reserved session workspace (for reasons of memory |
96 |
|
* conservation). This reservation is released as the first thing. |
97 |
|
* The acceptor and any other code which schedules this function |
98 |
|
* must obey this calling convention with a dummy reservation. |
99 |
|
*/ |
100 |
|
|
101 |
|
static void v_matchproto_(task_func_t) |
102 |
83385 |
http1_new_session(struct worker *wrk, void *arg) |
103 |
|
{ |
104 |
|
struct sess *sp; |
105 |
|
struct req *req; |
106 |
|
uintptr_t *u; |
107 |
|
ssize_t sz; |
108 |
|
|
109 |
83385 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
110 |
83385 |
CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC); |
111 |
83385 |
sp = req->sp; |
112 |
83385 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
113 |
|
|
114 |
83385 |
HTC_RxInit(req->htc, req->ws); |
115 |
|
|
116 |
83385 |
sz = sizeof u; |
117 |
83385 |
if (SES_Get_proto_priv(sp, &u) && |
118 |
83376 |
!SES_Reserve_proto_priv(sp, &u, &sz)) { |
119 |
|
/* Out of session workspace. Free the req, close the sess, |
120 |
|
* and do not set a new task func, which will exit the |
121 |
|
* worker thread. */ |
122 |
40 |
VSL(SLT_Error, req->sp->vxid, |
123 |
|
"insufficient workspace (proto_priv)"); |
124 |
40 |
WS_Release(req->ws, 0); |
125 |
40 |
Req_Release(req); |
126 |
40 |
SES_Delete(sp, SC_RX_JUNK, NAN); |
127 |
40 |
return; |
128 |
|
} |
129 |
83345 |
assert(sz == sizeof u); |
130 |
83333 |
http1_setstate(sp, H1NEWREQ); |
131 |
83333 |
wrk->task->func = http1_req; |
132 |
83333 |
wrk->task->priv = req; |
133 |
83373 |
} |
134 |
|
|
135 |
|
static void v_matchproto_(task_func_t) |
136 |
5600 |
http1_unwait(struct worker *wrk, void *arg) |
137 |
|
{ |
138 |
|
struct sess *sp; |
139 |
|
struct req *req; |
140 |
|
|
141 |
5600 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
142 |
5600 |
CAST_OBJ_NOTNULL(sp, arg, SESS_MAGIC); |
143 |
5600 |
WS_Release(sp->ws, 0); |
144 |
5600 |
req = Req_New(sp); |
145 |
5600 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
146 |
5600 |
req->htc->rfd = &sp->fd; |
147 |
5600 |
HTC_RxInit(req->htc, req->ws); |
148 |
5600 |
http1_setstate(sp, H1NEWREQ); |
149 |
5600 |
wrk->task->func = http1_req; |
150 |
5600 |
wrk->task->priv = req; |
151 |
5600 |
} |
152 |
|
|
153 |
|
static void v_matchproto_(vtr_req_body_t) |
154 |
3400 |
http1_req_body(struct req *req) |
155 |
|
{ |
156 |
|
|
157 |
3400 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
158 |
3400 |
if (V1F_Setup_Fetch(req->vfc, req->htc) != 0) |
159 |
0 |
req->req_body_status = BS_ERROR; |
160 |
3400 |
} |
161 |
|
|
162 |
|
static void |
163 |
240 |
http1_sess_panic(struct vsb *vsb, const struct sess *sp) |
164 |
|
{ |
165 |
|
|
166 |
240 |
VSB_printf(vsb, "state = %s\n", http1_getstate(sp)); |
167 |
240 |
} |
168 |
|
|
169 |
|
static void |
170 |
200 |
http1_req_panic(struct vsb *vsb, const struct req *req) |
171 |
|
{ |
172 |
|
|
173 |
200 |
VSB_printf(vsb, "state = %s\n", http1_getstate(req->sp)); |
174 |
200 |
} |
175 |
|
|
176 |
|
static void v_matchproto_(vtr_req_fail_f) |
177 |
1120 |
http1_req_fail(struct req *req, stream_close_t reason) |
178 |
|
{ |
179 |
1120 |
assert(reason != SC_NULL); |
180 |
1120 |
assert(req->sp->fd != 0); |
181 |
1120 |
if (req->sp->fd > 0) |
182 |
1120 |
SES_Close(req->sp, reason); |
183 |
1120 |
} |
184 |
|
|
185 |
|
static int v_matchproto_(vtr_minimal_response_f) |
186 |
1920 |
http1_minimal_response(struct req *req, uint16_t status) |
187 |
|
{ |
188 |
|
ssize_t wl, l; |
189 |
|
char buf[80]; |
190 |
|
const char *reason; |
191 |
|
|
192 |
1920 |
assert(status >= 100); |
193 |
1920 |
assert(status < 1000); |
194 |
|
|
195 |
1920 |
reason = http_Status2Reason(status, NULL); |
196 |
|
|
197 |
1920 |
bprintf(buf, "HTTP/1.1 %03d %s\r\n\r\n", status, reason); |
198 |
1920 |
l = strlen(buf); |
199 |
|
|
200 |
1920 |
VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1"); |
201 |
1920 |
VSLb(req->vsl, SLT_RespStatus, "%03d", status); |
202 |
1920 |
VSLbs(req->vsl, SLT_RespReason, TOSTRAND(reason)); |
203 |
|
|
204 |
1920 |
if (status >= 400) |
205 |
1720 |
req->err_code = status; |
206 |
1920 |
wl = write(req->sp->fd, buf, l); |
207 |
|
|
208 |
1920 |
if (wl > 0) |
209 |
1920 |
req->acct.resp_hdrbytes += wl; |
210 |
1920 |
if (wl != l) { |
211 |
0 |
if (wl < 0) |
212 |
0 |
VTCP_Assert(1); |
213 |
0 |
if (req->doclose == SC_NULL) |
214 |
0 |
req->doclose = SC_REM_CLOSE; |
215 |
0 |
return (-1); |
216 |
|
} |
217 |
1920 |
return (0); |
218 |
1920 |
} |
219 |
|
|
220 |
|
struct transport HTTP1_transport = { |
221 |
|
.name = "HTTP/1", |
222 |
|
.proto_ident = "HTTP", |
223 |
|
.magic = TRANSPORT_MAGIC, |
224 |
|
.deliver = V1D_Deliver, |
225 |
|
.minimal_response = http1_minimal_response, |
226 |
|
.new_session = http1_new_session, |
227 |
|
.req_body = http1_req_body, |
228 |
|
.req_fail = http1_req_fail, |
229 |
|
.req_panic = http1_req_panic, |
230 |
|
.sess_panic = http1_sess_panic, |
231 |
|
.unwait = http1_unwait, |
232 |
|
}; |
233 |
|
|
234 |
|
/*---------------------------------------------------------------------- |
235 |
|
*/ |
236 |
|
|
237 |
|
static inline void |
238 |
1320 |
http1_abort(struct req *req, uint16_t status) |
239 |
|
{ |
240 |
1320 |
assert(req->doclose != SC_NULL); |
241 |
1320 |
assert(status >= 400); |
242 |
1320 |
(void)http1_minimal_response(req, status); |
243 |
1320 |
} |
244 |
|
|
245 |
|
static int |
246 |
124999 |
http1_dissect(struct worker *wrk, struct req *req) |
247 |
|
{ |
248 |
|
|
249 |
124999 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
250 |
124999 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
251 |
124999 |
CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC); |
252 |
|
|
253 |
|
/* Allocate a new vxid now that we know we'll need it. */ |
254 |
124999 |
assert(IS_NO_VXID(req->vsl->wid)); |
255 |
124999 |
req->vsl->wid = VXID_Get(wrk, VSL_CLIENTMARKER); |
256 |
|
|
257 |
124999 |
VSLb(req->vsl, SLT_Begin, "req %ju rxreq", VXID(req->sp->vxid)); |
258 |
124999 |
VSL(SLT_Link, req->sp->vxid, "req %ju rxreq", VXID(req->vsl->wid)); |
259 |
124999 |
AZ(isnan(req->t_first)); /* First byte timestamp set by http1_wait */ |
260 |
124999 |
AZ(isnan(req->t_req)); /* Complete req rcvd set by http1_wait */ |
261 |
124999 |
req->t_prev = req->t_first; |
262 |
124999 |
VSLb_ts_req(req, "Start", req->t_first); |
263 |
124999 |
VSLb_ts_req(req, "Req", req->t_req); |
264 |
|
|
265 |
124999 |
HTTP_Setup(req->http, req->ws, req->vsl, SLT_ReqMethod); |
266 |
124999 |
req->err_code = HTTP1_DissectRequest(req->htc, req->http); |
267 |
|
|
268 |
|
/* If we could not even parse the request, just close */ |
269 |
124999 |
if (req->err_code != 0) { |
270 |
2640 |
VSLb(req->vsl, SLT_HttpGarbage, "%.*s", |
271 |
1320 |
(int)(req->htc->rxbuf_e - req->htc->rxbuf_b), |
272 |
1320 |
req->htc->rxbuf_b); |
273 |
1320 |
wrk->stats->client_req_400++; |
274 |
|
|
275 |
1320 |
(void)Req_LogStart(wrk, req); |
276 |
|
|
277 |
1320 |
req->doclose = SC_RX_JUNK; |
278 |
1320 |
http1_abort(req, 400); |
279 |
1320 |
return (-1); |
280 |
|
} |
281 |
|
|
282 |
123679 |
AZ(req->req_body_status); |
283 |
123679 |
req->req_body_status = req->htc->body_status; |
284 |
123679 |
return (0); |
285 |
124999 |
} |
286 |
|
|
287 |
|
/*---------------------------------------------------------------------- |
288 |
|
*/ |
289 |
|
|
290 |
|
static void |
291 |
90023 |
HTTP1_Session(struct worker *wrk, struct req *req) |
292 |
|
{ |
293 |
|
enum htc_status_e hs; |
294 |
|
struct sess *sp; |
295 |
|
const char *st; |
296 |
|
int i; |
297 |
|
|
298 |
90023 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
299 |
90023 |
CHECK_OBJ_NOTNULL(req, REQ_MAGIC); |
300 |
90023 |
sp = req->sp; |
301 |
90023 |
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); |
302 |
|
|
303 |
|
/* |
304 |
|
* Whenever we come in from the acceptor or waiter, we need to set |
305 |
|
* blocking mode. It would be simpler to do this in the acceptor |
306 |
|
* or waiter, but we'd rather do the syscall in the worker thread. |
307 |
|
*/ |
308 |
90023 |
if (http1_getstate(sp) == H1NEWREQ) |
309 |
88923 |
VTCP_blocking(sp->fd); |
310 |
90023 |
req->transport = XPORT_ByNumber(sp->sattr[SA_TRANSPORT]); |
311 |
|
|
312 |
441386 |
while (1) { |
313 |
442786 |
st = http1_getstate(sp); |
314 |
442786 |
if (st == H1NEWREQ) { |
315 |
193694 |
CHECK_OBJ_NOTNULL(req->transport, TRANSPORT_MAGIC); |
316 |
193694 |
assert(isnan(req->t_prev)); |
317 |
193694 |
assert(isnan(req->t_req)); |
318 |
193694 |
AZ(req->vcl); |
319 |
193694 |
AZ(req->esi_level); |
320 |
193694 |
AN(WS_Reservation(req->htc->ws)); |
321 |
|
|
322 |
387388 |
hs = HTC_RxStuff(req->htc, HTTP1_Complete, |
323 |
193694 |
&req->t_first, &req->t_req, |
324 |
193694 |
sp->t_idle + SESS_TMO(sp, timeout_linger), |
325 |
193694 |
sp->t_idle + SESS_TMO(sp, timeout_idle), |
326 |
|
NAN, |
327 |
193694 |
cache_param->http_req_size); |
328 |
193694 |
assert(!WS_IsReserved(req->htc->ws)); |
329 |
193694 |
if (hs < HTC_S_EMPTY) { |
330 |
56079 |
req->acct.req_hdrbytes += |
331 |
56079 |
req->htc->rxbuf_e - req->htc->rxbuf_b; |
332 |
56079 |
Req_AcctLogCharge(wrk->stats, req); |
333 |
56079 |
Req_Release(req); |
334 |
56079 |
SES_DeleteHS(sp, hs, NAN); |
335 |
56079 |
return; |
336 |
|
} |
337 |
137615 |
if (hs == HTC_S_IDLE) { |
338 |
7140 |
wrk->stats->sess_herd++; |
339 |
7140 |
Req_Release(req); |
340 |
7140 |
SES_Wait(sp, &HTTP1_transport); |
341 |
7140 |
return; |
342 |
|
} |
343 |
130475 |
if (hs != HTC_S_COMPLETE) |
344 |
0 |
WRONG("htc_status (nonbad)"); |
345 |
|
|
346 |
130475 |
if (H2_prism_complete(req->htc) == HTC_S_COMPLETE) { |
347 |
5480 |
if (!FEATURE(FEATURE_HTTP2)) { |
348 |
80 |
SES_Close(req->sp, SC_REQ_HTTP20); |
349 |
80 |
assert(!WS_IsReserved(req->ws)); |
350 |
80 |
assert(!WS_IsReserved(wrk->aws)); |
351 |
80 |
http1_setstate(sp, H1CLEANUP); |
352 |
80 |
continue; |
353 |
|
} |
354 |
5400 |
http1_setstate(sp, NULL); |
355 |
5400 |
H2_PU_Sess(wrk, sp, req); |
356 |
5400 |
return; |
357 |
|
} |
358 |
|
|
359 |
124995 |
i = http1_dissect(wrk, req); |
360 |
124995 |
req->acct.req_hdrbytes += |
361 |
124995 |
req->htc->rxbuf_e - req->htc->rxbuf_b; |
362 |
124995 |
if (i) { |
363 |
1320 |
assert(req->doclose != SC_NULL); |
364 |
1320 |
SES_Close(req->sp, req->doclose); |
365 |
1320 |
assert(!WS_IsReserved(req->ws)); |
366 |
1320 |
assert(!WS_IsReserved(wrk->aws)); |
367 |
1320 |
http1_setstate(sp, H1CLEANUP); |
368 |
1320 |
continue; |
369 |
|
} |
370 |
123675 |
if (http_HdrIs(req->http, H_Upgrade, "h2c")) { |
371 |
360 |
if (!FEATURE(FEATURE_HTTP2)) { |
372 |
40 |
VSLb(req->vsl, SLT_Debug, |
373 |
|
"H2 upgrade attempt"); |
374 |
360 |
} else if (req->htc->body_status != BS_NONE) { |
375 |
40 |
VSLb(req->vsl, SLT_Debug, |
376 |
|
"H2 upgrade attempt has body"); |
377 |
40 |
} else { |
378 |
280 |
http1_setstate(sp, NULL); |
379 |
280 |
req->err_code = 2; |
380 |
280 |
H2_OU_Sess(wrk, sp, req); |
381 |
280 |
return; |
382 |
|
} |
383 |
80 |
} |
384 |
123395 |
assert(req->req_step == R_STP_TRANSPORT); |
385 |
123395 |
VCL_TaskEnter(req->privs); |
386 |
123395 |
VCL_TaskEnter(req->top->privs); |
387 |
123395 |
http1_setstate(sp, H1PROC); |
388 |
372487 |
} else if (st == H1PROC) { |
389 |
124515 |
req->task->func = http1_req; |
390 |
124515 |
req->task->priv = req; |
391 |
124515 |
CNT_Embark(wrk, req); |
392 |
124515 |
if (CNT_Request(req) == REQ_FSM_DISEMBARK) |
393 |
1343 |
return; |
394 |
123172 |
wrk->stats->client_req++; |
395 |
123172 |
AZ(req->top->vcl0); |
396 |
123172 |
req->task->func = NULL; |
397 |
123172 |
req->task->priv = NULL; |
398 |
123172 |
assert(!WS_IsReserved(req->ws)); |
399 |
123172 |
assert(!WS_IsReserved(wrk->aws)); |
400 |
123172 |
http1_setstate(sp, H1CLEANUP); |
401 |
247749 |
} else if (st == H1CLEANUP) { |
402 |
|
|
403 |
124577 |
assert(!WS_IsReserved(wrk->aws)); |
404 |
124577 |
assert(!WS_IsReserved(req->ws)); |
405 |
|
|
406 |
124577 |
if (sp->fd >= 0 && req->doclose != SC_NULL) |
407 |
16261 |
SES_Close(sp, req->doclose); |
408 |
|
|
409 |
124577 |
if (sp->fd < 0) { |
410 |
19781 |
wrk->stats->sess_closed++; |
411 |
19781 |
Req_Cleanup(sp, wrk, req); |
412 |
19781 |
Req_Release(req); |
413 |
19781 |
SES_Delete(sp, SC_NULL, NAN); |
414 |
19781 |
return; |
415 |
|
} |
416 |
|
|
417 |
104796 |
Req_Cleanup(sp, wrk, req); |
418 |
104796 |
HTC_RxInit(req->htc, req->ws); |
419 |
104796 |
if (req->htc->rxbuf_e != req->htc->rxbuf_b) |
420 |
585 |
wrk->stats->sess_readahead++; |
421 |
104796 |
if (FEATURE(FEATURE_BUSY_STATS_RATE)) |
422 |
0 |
WRK_AddStat(wrk); |
423 |
104796 |
http1_setstate(sp, H1NEWREQ); |
424 |
104796 |
} else { |
425 |
0 |
WRONG("Wrong H1 session state"); |
426 |
|
} |
427 |
|
} |
428 |
90023 |
} |