Fix regex capture when using map directive and rewrite

Corentin REGAL corentin.regal at gmail.com
Thu Sep 17 08:39:45 UTC 2020


Hello,

I fixed a bug when using map directive and rewrite.
Captured variables in the map directive override captures of the rewrite.

*nginx.conf*
daemon off;
error_log  /var/log/nginx/error.log debug;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    include            mime.types;
    default_type       application/octet-stream;
    sendfile           on;
    keepalive_timeout  0;

    map $http_content_type $input_type {
        default  json;
        ~image/  image;
    }

    server {
        server_name localhost;
        listen 80;
        location / {
            rewrite "/(.+)" /$1_${input_type} redirect;
        }
    }
}


*before patch (/_image)*
curl -v -H'Content-Type: image/png' localhost:80/predict
...
< Location: http://localhost/_image
...

*after patch (/predict_image)*
curl -v -H'Content-Type: image/png' localhost:80/predict
...
< Location: http://localhost/predict_image
...

*patch*
# HG changeset patch
# User Corentin Regal <corentin.regal at gmail.com>
# Date 1600329166 0
#      Thu Sep 17 07:52:46 2020 +0000
# Node ID a065d5f865e90a7426d37b30a9faa72e0966756f
# Parent  052ecc68d35038b1b4adde12efe6249a92055f09
Fix ngx_http_map_find to not erase rewrite captures

diff -r 052ecc68d350 -r a065d5f865e9 src/http/ngx_http_variables.c
--- a/src/http/ngx_http_variables.c     Wed Sep 16 18:26:25 2020 +0300
+++ b/src/http/ngx_http_variables.c     Thu Sep 17 07:52:46 2020 +0000
@@ -2410,7 +2410,7 @@

         for (i = 0; i < map->nregex; i++) {

-            n = ngx_http_regex_exec(r, reg[i].regex, match);
+            n = ngx_regex_exec(reg[i].regex->regex, match, NULL, 0);

             if (n == NGX_OK) {
                 return reg[i].value;

It's my first time using Mercurial or a mailing list, I hope I did it right
:)

Regards,

Corentin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20200917/e2f5766e/attachment.htm>


More information about the nginx-devel mailing list