Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check http expect header #56

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ http {

check interval=3000 rise=2 fall=5 timeout=5000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
check_http_expect_alive 200-400;
check_http_expect_header Host;
check_http_expect_header Connection ~ alive;
}
}

Expand Down Expand Up @@ -205,7 +207,19 @@ stream {
}
```

healthcheck
check_http_expect_header
-----------
类似于nginxplus中的
[match中header](http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#match)


check_http_expect_alive
-----------
类似于nginxplus中的
[match中status](http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#match)


healthcheck_status
-----------

`语法`: healthcheck_status [html|csv|json]
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ http {

check interval=3000 rise=2 fall=5 timeout=5000 type=http;
check_http_send "GET / HTTP/1.0\r\n\r\n";
check_http_expect_alive http_2xx http_3xx;
check_http_expect_alive 200-400;
check_http_expect_header Host;
check_http_expect_header Connection ~ alive;
}
}

Expand Down Expand Up @@ -250,7 +252,19 @@ stream {
}
```

healthcheck
check_http_expect_header
-----------
the same as nginx plus ->
[match header](http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#match)


check_http_expect_alive
-----------
the same as nginx plus ->
[match status](http://nginx.org/en/docs/http/ngx_http_upstream_hc_module.html#match)


healthcheck_status
-----------

`Syntax`: healthcheck_status [html|csv|json|prometheus]
Expand Down
200 changes: 184 additions & 16 deletions ngx_healthcheck_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ rc.err.len = NGX_MAX_CONF_ERRSTR;

#if (NGX_HAVE_CASELESS_FILESYSTEM)
rc.options = NGX_REGEX_CASELESS;
#else
#else
rc.options = caseless ? NGX_REGEX_CASELESS : 0;
#endif

Expand All @@ -47,11 +47,58 @@ rc.err.len = NGX_MAX_CONF_ERRSTR;
#endif
}

#define NGX_PCRE 1

ngx_int_t
ngx_upstream_check_http_header_regex(ngx_conf_t *cf, ngx_upstream_check_header_conf_t *uchcf,
ngx_str_t *regex, ngx_uint_t is_caseless)
{
#if (NGX_PCRE)
ngx_regex_compile_t rc;
u_char errstr[NGX_MAX_CONF_ERRSTR];

ngx_memzero(&rc, sizeof(ngx_regex_compile_t));

rc.pattern = *regex;

rc.err.len = NGX_MAX_CONF_ERRSTR;
rc.err.data = errstr;
rc.pool = cf->pool;

#if (NGX_HAVE_CASELESS_FILESYSTEM)
rc.options = NGX_REGEX_CASELESS;
#else
rc.options = is_caseless ? 1 : 0;
#endif

if (ngx_regex_compile(&rc) != NGX_OK) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"ngx_regex_compile: %V",
&rc.err);
return NGX_ERROR;
}

uchcf->arg_regex = rc.regex;

return NGX_OK;

#else

ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"using regex \"%V\" requires PCRE library",
regex);
return NGX_ERROR;

#endif
}

ngx_int_t
ngx_upstream_check_http_parse_status_line(
ngx_buf_t *b, ngx_uint_t *pstate, ngx_http_status_t *status)
ngx_buf_t *b, ngx_uint_t *pstate, ngx_http_status_t *status,ngx_list_t *header)
{
u_char ch, *p;
ngx_table_elt_t *elt;
size_t count;
enum {
sw_start = 0,
sw_H,
Expand All @@ -65,10 +112,24 @@ ngx_upstream_check_http_parse_status_line(
sw_status,
sw_space_after_status,
sw_status_text,
sw_lf,
sw_cr,
sw_almost_done,
receive_body

sw_header_cr,
sw_header_lf,
sw_header_end,
//special
receive_body,

sw_status_line_end,

sw_header_start_key,
sw_header_key,

sw_header_start_value,
sw_header_value,
// sw_lf,
// sw_cr,
// sw_almost_done,
// receive_body
} state;

state = *pstate;
Expand Down Expand Up @@ -196,8 +257,9 @@ ngx_upstream_check_http_parse_status_line(
state = sw_status_text;
break;
case CR:
state = sw_almost_done;
state = sw_status_line_end;
break;
// why not return error
case LF:
goto done;
default:
Expand All @@ -209,47 +271,153 @@ ngx_upstream_check_http_parse_status_line(
case sw_status_text:
switch (ch) {
case CR:
state = sw_lf;
state = sw_status_line_end;
break;
// why not return error
case LF:
goto done;
}
break;

/* LF */
case sw_lf:
case sw_status_line_end:
switch (ch) {
case LF:
state = sw_cr;
state = sw_header_start_key;
break;
default:
return NGX_ERROR;
}
break;

/* CR */
case sw_header_start_key:
switch (ch) {
case CR: /* body but unsafe*/
state = sw_header_end;
break;

default: /* response header */
//add key
count = 1;
elt = ngx_list_push(header);

elt->key.data = p;
state = sw_header_key;
break;
}
break;

case sw_header_key:
switch (ch) {
case ':': /* second CR */
elt->key.len = count;
//ngx_log_error(NGX_LOG_ERR,ngx_cycle->log,0,"find key ->%V, len-> %z",&(elt->key),&(elt->key.len));
count = 1;
state = sw_header_start_value;
break;
default: /* response header */
//add key
count++;
break;
}
break;

case sw_header_start_value:
switch (ch) {
case ' ':
break;
case LF:
return NGX_ERROR;
case CR:
return NGX_ERROR;
// other
default:
count = 1;
elt->value.data = p;
state = sw_header_value;
//add value
break;
}
break;

case sw_header_value:
switch (ch) {
case CR:
elt->value.len = count;
//ngx_log_error(NGX_LOG_ERR,ngx_cycle->log,0,"find value ->%V,len -> %z",&(elt->value),&(elt->value.len));
count = 1;
state = sw_header_cr;
break;
default:
//add value
count++;
break;
}
break;

case sw_header_cr:
switch (ch) {
case LF:
state = sw_header_lf;
break;
default:
return NGX_ERROR;
}
break;

case sw_header_lf:
switch (ch) {
case CR:
state = sw_header_end;
break;
default:
//add key
elt = ngx_list_push(header);
count = 1;
elt->key.data = p;
state = sw_header_key;
break;
}
break;

/* body */
case sw_header_end:
switch (ch) {
case LF:
/* all header_end */
status->end = p - 1;
goto done;
default:
return NGX_ERROR;
}
// how to use
case receive_body:
return NGX_OK;

/* *//* CR *//*
case sw_cr:
switch (ch) {
case CR: /* second CR */
case CR: *//* second CR *//*
state = sw_almost_done;
break;
default: /* response header */
default: *//* response header *//*
state = sw_status_text;
break;
}
break;

/* LF */
*//* LF *//*
case sw_almost_done:
switch (ch) {
case LF:
/* all header_end */
*//* all header_end *//*
status->end = p - 1;
goto done;
default:
return NGX_ERROR;
}
case receive_body:
return NGX_OK;
return NGX_OK;*/
}
}

Expand Down
Loading