diff --git a/README b/README index aef6e73..3586630 100644 --- a/README +++ b/README @@ -15,7 +15,7 @@ Limitations Currently only inspects the following headers: Range, If-Range, If-Unmodified-Since, If-Modified-Since, Date, Accept-Encoding, - Accept-Language + Accept-Language, Accept-Charset Report Bugs Create a ticket on the issue tracking interface of GitHub: diff --git a/ngx_http_header_inspect.c b/ngx_http_header_inspect.c index 7a118f2..10ba387 100644 --- a/ngx_http_header_inspect.c +++ b/ngx_http_header_inspect.c @@ -28,6 +28,7 @@ static ngx_int_t ngx_header_inspect_range_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_acceptencoding_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_acceptlanguage_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); +static ngx_int_t ngx_header_inspect_acceptcharset_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_ifrange_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value); static ngx_int_t ngx_header_inspect_date_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, char *header, ngx_str_t value); static ngx_int_t ngx_header_inspect_process_request(ngx_http_request_t *r); @@ -758,6 +759,61 @@ return NGX_OK; } +static ngx_int_t ngx_header_inspect_parse_charset(u_char *data, ngx_uint_t maxlen, ngx_uint_t *len) { + ngx_uint_t i; + u_char d; + ngx_uint_t alphacount = 0; + + if (maxlen < 1) { + *len = 0; + return NGX_ERROR; + } + + if (data[0] == '*') { + *len = 1; + return NGX_OK; + } + + *len = 1; + for ( i = 0; i < maxlen; i++ ) { + d = data[i]; + if ( + (d == '-') || + (d == '_') || + (d == '+') || + (d == '.') || + (d == ':') + ) { + if (alphacount == 0) { + *len = i; + return NGX_ERROR; + } + alphacount = 0; + continue; + } + if ( + ((d < '0') || (d > '9')) && + ((d < 'a') || (d > 'z')) && + ((d < 'A') || (d > 'Z')) + ) { + *len = i; + if (alphacount == 0) { + return NGX_ERROR; + } else { + return NGX_OK; + } + } + alphacount++; + } + + *len = i; + if (alphacount == 0) { + return NGX_ERROR; + } else { + return NGX_OK; + } +} + static ngx_int_t ngx_header_inspect_parse_languagerange(u_char *data, ngx_uint_t maxlen, ngx_uint_t *len) { ngx_uint_t i; u_char d; @@ -810,6 +866,72 @@ } } +static ngx_int_t ngx_header_inspect_acceptcharset_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { + ngx_int_t rc = NGX_AGAIN; + ngx_uint_t i = 0; + ngx_uint_t v; + + if ((value.len == 0) || ((value.len == 1) && (value.data[0] == '*'))) { + return NGX_OK; + } + + while ( i < value.len ) { + if (ngx_header_inspect_parse_charset(&(value.data[i]), value.len-i, &v) != NGX_OK) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: invalid charset at position %d in Accept-Charset header \"%s\"", i, value.data); + rc = NGX_ERROR; + break; + } + i += v; + if ((value.data[i] == ' ') && (i < value.len)) { + i++; + } + if (i == value.len) { + rc = NGX_OK; + break; + } + if (value.data[i] == ';') { + i++; + if (i >= value.len) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: unexpected end of Accept-Charset header \"%s\"", value.data); + rc = NGX_ERROR; + break; + } + if ((value.data[i] == ' ') && (i < value.len)) { + i++; + } + if (ngx_header_inspect_parse_qvalue(&(value.data[i]), value.len-i, &v) != NGX_OK) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: invalid qvalue at position %d in Accept-Charset header \"%s\"", i, value.data); + rc = NGX_ERROR; + break; + } + i += v; + if ((value.data[i] == ' ') && (i < value.len)) { + i++; + } + if (i == value.len) { + rc = NGX_OK; + break; + } + } + if (value.data[i] != ',') { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: illegal char at position %d in Accept-Charset header \"%s\"", i, value.data); + rc = NGX_ERROR; + break; + } + i++; + if ((value.data[i] == ' ') && (i < value.len)) { + i++; + } + } + + if (rc == NGX_AGAIN) { + ngx_log_error(NGX_LOG_ALERT, log, 0, "header_inspect: unexpected end of Accept-Charset header \"%s\"", value.data); + rc = NGX_ERROR; + } + + return rc; +} + static ngx_int_t ngx_header_inspect_acceptlanguage_header(ngx_header_inspect_loc_conf_t *conf, ngx_log_t *log, ngx_str_t value) { ngx_int_t rc = NGX_AGAIN; ngx_uint_t i = 0; @@ -1021,6 +1143,11 @@ if ((rc != NGX_OK) && conf->block) { return NGX_HTTP_BAD_REQUEST; } + } else if ((h[i].key.len == 14) && (ngx_strcmp("Accept-Charset", h[i].key.data) == 0) ) { + rc = ngx_header_inspect_acceptcharset_header(conf, r->connection->log, h[i].value); + if ((rc != NGX_OK) && conf->block) { + return NGX_HTTP_BAD_REQUEST; + } } else { /* TODO: support for other headers */ if (conf->log_uninspected) {