mirror of
https://github.com/nginx/nginx.git
synced 2025-02-25 18:55:26 -06:00
optimize the most frequent cases
This commit is contained in:
parent
e23b4849b5
commit
0359ba8cc1
@ -9,6 +9,24 @@
|
|||||||
#include <ngx_http.h>
|
#include <ngx_http.h>
|
||||||
|
|
||||||
|
|
||||||
|
static uint32_t usual[] =
|
||||||
|
{ 0xffffdbfe, /* 1111 1111 1111 1111 1101 1011 1111 1110 */
|
||||||
|
|
||||||
|
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
|
||||||
|
0x7fff37d6, /* 0111 1111 1111 1111 0011 0111 1101 0110 */
|
||||||
|
|
||||||
|
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
|
||||||
|
0xefffffff, /* 1110 1111 1111 1111 1111 1111 1111 1111 */
|
||||||
|
|
||||||
|
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
|
||||||
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
||||||
|
|
||||||
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
||||||
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
||||||
|
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
|
||||||
|
0xffffffff /* 1111 1111 1111 1111 1111 1111 1111 1111 */ };
|
||||||
|
|
||||||
|
|
||||||
/* gcc, icc, msvc and others compile these switches as an jump table */
|
/* gcc, icc, msvc and others compile these switches as an jump table */
|
||||||
|
|
||||||
ngx_int_t
|
ngx_int_t
|
||||||
@ -225,13 +243,7 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
|
|||||||
/* check "/.", "//", "%", and "\" (Win32) in URI */
|
/* check "/.", "//", "%", and "\" (Win32) in URI */
|
||||||
case sw_after_slash_in_uri:
|
case sw_after_slash_in_uri:
|
||||||
|
|
||||||
c = (u_char) (ch | 0x20);
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
if (c >= 'a' && c <= 'z') {
|
|
||||||
state = sw_check_uri;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ch >= '0' && ch <= '9') {
|
|
||||||
state = sw_check_uri;
|
state = sw_check_uri;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -291,12 +303,7 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
|
|||||||
/* check "/", "%" and "\" (Win32) in URI */
|
/* check "/", "%" and "\" (Win32) in URI */
|
||||||
case sw_check_uri:
|
case sw_check_uri:
|
||||||
|
|
||||||
c = (u_char) (ch | 0x20);
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
if (c >= 'a' && c <= 'z') {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ch >= '0' && ch <= '9') {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -350,6 +357,11 @@ ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b)
|
|||||||
|
|
||||||
/* URI */
|
/* URI */
|
||||||
case sw_uri:
|
case sw_uri:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch (ch) {
|
switch (ch) {
|
||||||
case ' ':
|
case ' ':
|
||||||
r->uri_end = p;
|
r->uri_end = p;
|
||||||
@ -793,6 +805,13 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
switch (state) {
|
switch (state) {
|
||||||
|
|
||||||
case sw_usual:
|
case sw_usual:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
*u++ = ch;
|
||||||
|
ch = *p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
case '\\':
|
case '\\':
|
||||||
@ -836,10 +855,19 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
*u++ = ch;
|
*u++ = ch;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sw_slash:
|
case sw_slash:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
state = sw_usual;
|
||||||
|
*u++ = ch;
|
||||||
|
ch = *p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
case '\\':
|
case '\\':
|
||||||
@ -866,10 +894,19 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
*u++ = ch;
|
*u++ = ch;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sw_dot:
|
case sw_dot:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
state = sw_usual;
|
||||||
|
*u++ = ch;
|
||||||
|
ch = *p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
case '\\':
|
case '\\':
|
||||||
@ -898,10 +935,19 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
*u++ = ch;
|
*u++ = ch;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case sw_dot_dot:
|
case sw_dot_dot:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
state = sw_usual;
|
||||||
|
*u++ = ch;
|
||||||
|
ch = *p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
case '\\':
|
case '\\':
|
||||||
@ -938,11 +984,20 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
*u++ = ch;
|
*u++ = ch;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
case sw_dot_dot_dot:
|
case sw_dot_dot_dot:
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
state = sw_usual;
|
||||||
|
*u++ = ch;
|
||||||
|
ch = *p++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
switch(ch) {
|
switch(ch) {
|
||||||
case '\\':
|
case '\\':
|
||||||
case '/':
|
case '/':
|
||||||
@ -977,6 +1032,7 @@ ngx_http_parse_complex_uri(ngx_http_request_t *r)
|
|||||||
*u++ = ch;
|
*u++ = ch;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
break;
|
break;
|
||||||
#endif
|
#endif
|
||||||
@ -1111,6 +1167,10 @@ ngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri,
|
|||||||
|
|
||||||
ch = *p++;
|
ch = *p++;
|
||||||
|
|
||||||
|
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (ch == '?') {
|
if (ch == '?') {
|
||||||
args->len = len - 1;
|
args->len = len - 1;
|
||||||
args->data = p;
|
args->data = p;
|
||||||
@ -1124,17 +1184,12 @@ ngx_http_parse_unsafe_uri(ngx_http_request_t *r, ngx_str_t *uri,
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch != '/'
|
if (len > 2 && (ch == '/'
|
||||||
#if (NGX_WIN32)
|
#if (NGX_WIN32)
|
||||||
&& ch != '\\'
|
|| ch == '\\'
|
||||||
#endif
|
#endif
|
||||||
)
|
))
|
||||||
{
|
{
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (len > 2) {
|
|
||||||
|
|
||||||
/* detect "/../" */
|
/* detect "/../" */
|
||||||
|
|
||||||
if (p[0] == '.' && p[1] == '.' && p[2] == '/') {
|
if (p[0] == '.' && p[1] == '.' && p[2] == '/') {
|
||||||
|
Loading…
Reference in New Issue
Block a user