-
Notifications
You must be signed in to change notification settings - Fork 7
/
ngx_health_detect_utils.c
386 lines (304 loc) · 9.68 KB
/
ngx_health_detect_utils.c
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
#include "ngx_health_detect_utils.h"
#include "ngx_health_detect_common.h"
ngx_int_t
ngx_http_health_detect_http_init(ngx_health_detect_peer_t *peer)
{
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
if (peer->policy->send_content.len == 0) {
ctx->send.start = ctx->send.pos =
(u_char *) peer->default_policy->default_send_content.data;
ctx->send.end = ctx->send.last =
ctx->send.start + peer->default_policy->default_send_content.len;
} else {
ctx->send.start = ctx->send.pos =
(u_char *) peer->policy->send_content.data;
ctx->send.end = ctx->send.last =
ctx->send.start + peer->policy->send_content.len;
}
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
ctx->state = 0;
ngx_memzero(&ctx->status, sizeof(ngx_http_status_t));
return NGX_OK;
}
static ngx_int_t
ngx_http_health_detect_parse_status_line(
ngx_http_check_data_ctx_t *ctx, ngx_buf_t *b, ngx_http_status_t *status)
{
u_char ch, *p;
enum {
sw_start = 0,
sw_H,
sw_HT,
sw_HTT,
sw_HTTP,
sw_first_major_digit,
sw_major_digit,
sw_first_minor_digit,
sw_minor_digit,
sw_status,
sw_space_after_status,
sw_status_text,
sw_almost_done
} state;
state = ctx->state;
for (p = b->pos; p < b->last; p++) {
ch = *p;
switch (state) {
/* "HTTP/" */
case sw_start:
if (ch != 'H') {
return NGX_ERROR;
}
state = sw_H;
break;
case sw_H:
if (ch != 'T') {
return NGX_ERROR;
}
state = sw_HT;
break;
case sw_HT:
if (ch != 'T') {
return NGX_ERROR;
}
state = sw_HTT;
break;
case sw_HTT:
if (ch != 'P') {
return NGX_ERROR;
}
state = sw_HTTP;
break;
case sw_HTTP:
if (ch != '/') {
return NGX_ERROR;
}
state = sw_first_major_digit;
break;
/* the first digit of major HTTP version */
case sw_first_major_digit:
if (ch < '1' || ch > '9') {
return NGX_ERROR;
}
state = sw_major_digit;
break;
/* the major HTTP version or dot */
case sw_major_digit:
if (ch == '.') {
state = sw_first_minor_digit;
break;
}
if (ch < '0' || ch > '9') {
return NGX_ERROR;
}
break;
/* the first digit of minor HTTP version */
case sw_first_minor_digit:
if (ch < '0' || ch > '9') {
return NGX_ERROR;
}
state = sw_minor_digit;
break;
/* the minor HTTP version or the end of the request line */
case sw_minor_digit:
if (ch == ' ') {
state = sw_status;
break;
}
if (ch < '0' || ch > '9') {
return NGX_ERROR;
}
break;
/* HTTP status code */
case sw_status:
if (ch == ' ') {
break;
}
if (ch < '0' || ch > '9') {
return NGX_ERROR;
}
status->code = status->code * 10 + ch - '0';
if (++status->count == 3) {
state = sw_space_after_status;
status->start = p - 2;
}
break;
/* space or end of line */
case sw_space_after_status:
switch (ch) {
case ' ':
state = sw_status_text;
break;
case '.': /* IIS may send 403.1, 403.2, etc */
state = sw_status_text;
break;
case CR:
state = sw_almost_done;
break;
case LF:
goto done;
default:
return NGX_ERROR;
}
break;
/* any text until end of line */
case sw_status_text:
switch (ch) {
case CR:
state = sw_almost_done;
break;
case LF:
goto done;
}
break;
/* end of status line */
case sw_almost_done:
status->end = p - 1;
if (ch == LF) {
goto done;
} else {
return NGX_ERROR;
}
}
}
b->pos = p;
ctx->state = state;
return NGX_AGAIN;
done:
b->pos = p + 1;
if (status->end == NULL) {
status->end = p;
}
ctx->state = sw_start;
return NGX_OK;
}
ngx_int_t
ngx_http_health_detect_http_parse(ngx_health_detect_peer_t *peer)
{
ngx_int_t rc;
ngx_uint_t code, code_n;
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
if ((ctx->recv.last - ctx->recv.pos) > 0) {
rc = ngx_http_health_detect_parse_status_line(
ctx, &ctx->recv, &ctx->status);
if (rc == NGX_AGAIN) {
return rc;
}
if (rc == NGX_ERROR) {
ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, 0,
"http parse status line error with peer: %V ",
&peer->policy->peer_addr.name);
return rc;
}
code = ctx->status.code;
if (code >= 200 && code < 300) {
code_n = NGX_CHECK_HTTP_2XX;
} else if (code >= 300 && code < 400) {
code_n = NGX_CHECK_HTTP_3XX;
} else if (code >= 400 && code < 500) {
peer->pc.connection->error = 1;
code_n = NGX_CHECK_HTTP_4XX;
} else if (code >= 500 && code < 600) {
peer->pc.connection->error = 1;
code_n = NGX_CHECK_HTTP_5XX;
} else {
peer->pc.connection->error = 1;
code_n = NGX_CHECK_HTTP_ERR;
}
ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0,
"http_parse: code_n: %ui, expected http status: %ui", code_n,
peer->policy->data.expect_response_status.http_status);
if (code_n & peer->policy->data.expect_response_status.http_status) {
return NGX_OK;
} else {
return NGX_ERROR;
}
} else {
return NGX_AGAIN;
}
}
void
ngx_http_health_detect_http_reinit(ngx_health_detect_peer_t *peer)
{
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
ctx->send.pos = ctx->send.start;
ctx->send.last = ctx->send.end;
ctx->recv.pos = ctx->recv.last = ctx->recv.start;
ctx->state = 0;
ngx_memzero(&ctx->status, sizeof(ngx_http_status_t));
}
ngx_int_t
ngx_http_health_detect_ssl_hello_init(ngx_health_detect_peer_t *peer)
{
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
if (peer->policy->send_content.len == 0) {
ctx->send.start = ctx->send.pos =
(u_char *) peer->default_policy->default_send_content.data;
ctx->send.end = ctx->send.last =
ctx->send.start + peer->default_policy->default_send_content.len;
} else {
ctx->send.start = ctx->send.pos =
(u_char *) peer->policy->send_content.data;
ctx->send.end = ctx->send.last =
ctx->send.start + peer->policy->send_content.len;
}
ctx->recv.start = ctx->recv.pos = NULL;
ctx->recv.end = ctx->recv.last = NULL;
return NGX_OK;
}
ngx_int_t
ngx_http_health_detect_ssl_hello_parse(ngx_health_detect_peer_t *peer)
{
size_t size;
ngx_ssl_server_hello_t *resp;
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
size = ctx->recv.last - ctx->recv.pos;
if (size < sizeof(ngx_ssl_server_hello_t)) {
return NGX_AGAIN;
}
resp = (ngx_ssl_server_hello_t *) ctx->recv.pos;
ngx_log_error(NGX_LOG_DEBUG, ngx_cycle->log, 0,
"http check ssl_parse, type: %xd, version: %xd.%xd, "
"length: %xd, handshanke_type: %xd, hello_version: %xd.%xd",
resp->msg_type, resp->version.major, resp->version.minor,
ntohs(resp->length), resp->handshake_type, resp->hello_version.major,
resp->hello_version.minor);
if (resp->msg_type != NGX_SSL_HANDSHAKE) {
return NGX_ERROR;
}
if (resp->handshake_type != NGX_SSL_SERVER_HELLO) {
return NGX_ERROR;
}
return NGX_OK;
}
void
ngx_http_health_detect_ssl_hello_reinit(ngx_health_detect_peer_t *peer)
{
ngx_http_check_data_ctx_t *ctx;
ctx = peer->check_data;
ctx->send.pos = ctx->send.start;
ctx->send.last = ctx->send.end;
ctx->recv.pos = ctx->recv.last = ctx->recv.start;
}
ngx_int_t
ngx_http_health_detect_peek_one_byte(ngx_connection_t *c)
{
char buf[1];
ngx_int_t n;
ngx_err_t err;
n = recv(c->fd, buf, 1, MSG_PEEK);
err = ngx_socket_errno;
ngx_log_error(
NGX_LOG_DEBUG, c->log, err, "http check recv(): %i, fd: %d", n, c->fd);
if (n == 1 || (n == -1 && err == NGX_EAGAIN)) {
return NGX_OK;
} else {
return NGX_ERROR;
}
}