Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3aa6dca

Browse files
committedOct 22, 2024·
Do not allocate new string for common HTTP methods
1 parent 34f84f9 commit 3aa6dca

File tree

1 file changed

+28
-2
lines changed

1 file changed

+28
-2
lines changed
 

Diff for: ‎src/ngx_http_modsecurity_rewrite.c

+28-2
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,41 @@ ngx_http_modsecurity_rewrite_handler(ngx_http_request_t *r)
157157
break;
158158
}
159159

160+
// Do not allocate new string for common methods
161+
const char *n_method;
162+
switch (r->method) {
163+
case NGX_HTTP_GET:
164+
n_method = "GET";
165+
break;
166+
case NGX_HTTP_HEAD:
167+
n_method = "HEAD";
168+
break;
169+
case NGX_HTTP_POST:
170+
n_method = "POST";
171+
break;
172+
case NGX_HTTP_OPTIONS:
173+
n_method = "OPTIONS";
174+
break;
175+
case NGX_HTTP_CONNECT:
176+
n_method = "CONNECT";
177+
break;
178+
default:
179+
n_method = ngx_str_to_char(r->method_name, r->pool);
180+
if (n_method == (char*)-1) {
181+
return NGX_HTTP_INTERNAL_SERVER_ERROR;
182+
}
183+
break;
184+
}
185+
160186
const char *n_uri = ngx_str_to_char(r->unparsed_uri, r->pool);
161-
const char *n_method = ngx_str_to_char(r->method_name, r->pool);
162-
if (n_uri == (char*)-1 || n_method == (char*)-1) {
187+
if (n_uri == (char*)-1) {
163188
return NGX_HTTP_INTERNAL_SERVER_ERROR;
164189
}
165190
if (n_uri == NULL) {
166191
dd("uri is of length zero");
167192
return NGX_HTTP_INTERNAL_SERVER_ERROR;
168193
}
194+
169195
old_pool = ngx_http_modsecurity_pcre_malloc_init(r->pool);
170196
msc_process_uri(ctx->modsec_transaction, n_uri, n_method, http_version);
171197
ngx_http_modsecurity_pcre_malloc_done(old_pool);

0 commit comments

Comments
 (0)
Please sign in to comment.