<div dir="ltr"># HG changeset patch<br># User Davood Falahati <<a href="mailto:0x0davood@gmail.com">0x0davood@gmail.com</a>><br># Date 1683593026 -7200<br>#      Tue May 09 02:43:46 2023 +0200<br># Node ID 1053357966cda6a0902b748a9b4b8a214b36ccd4<br># Parent  b71e69247483631bd8fc79a47cc32b762625b1fb<br>keep response body of the subrequest inside the memory and use it if send_auth_body is set<br><br>diff -r b71e69247483 -r 1053357966cd src/http/modules/ngx_http_auth_request_module.c<br>--- a/src/http/modules/ngx_http_auth_request_module.c   Mon May 01 19:16:05 2023 +0400<br>+++ b/src/http/modules/ngx_http_auth_request_module.c   Tue May 09 02:43:46 2023 +0200<br>@@ -13,6 +13,7 @@<br> typedef struct {<br>     ngx_str_t                 uri;<br>     ngx_array_t              *vars;<br>+    ngx_flag_t                enable;<br> } ngx_http_auth_request_conf_t;<br> <br> <br>@@ -62,6 +63,12 @@<br>       NGX_HTTP_LOC_CONF_OFFSET,<br>       0,<br>       NULL },<br>+    { ngx_string("send_auth_body"),<br>+      NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,<br>+      ngx_conf_set_flag_slot,<br>+      NGX_HTTP_LOC_CONF_OFFSET,<br>+      offsetof(ngx_http_auth_request_conf_t, enable),<br>+      NULL },<br> <br>       ngx_null_command<br> };<br>@@ -106,6 +113,9 @@<br>     ngx_http_post_subrequest_t    *ps;<br>     ngx_http_auth_request_ctx_t   *ctx;<br>     ngx_http_auth_request_conf_t  *arcf;<br>+    ngx_list_t *hs;<br>+    ngx_buf_t *b;<br>+    ngx_chain_t out, *in;<br> <br>     arcf = ngx_http_get_module_loc_conf(r, ngx_http_auth_request_module);<br> <br>@@ -141,6 +151,36 @@<br>         if (ctx->status == NGX_HTTP_UNAUTHORIZED) {<br>             sr = ctx->subrequest;<br> <br>+            if (arcf->enable) {<br>+<br>+                r->headers_out.content_type = sr->headers_out.content_type;<br>+<br>+                hs = &sr->headers_out.headers;<br>+<br>+                r->headers_out.headers = *hs;<br>+<br>+                b = ngx_calloc_buf(r->pool);<br>+                if (b == NULL) {<br>+                   return NGX_ERROR;<br>+                }<br>+<br>+                r->headers_out.status = ctx->status;<br>+<br>+                b->last_buf = 1;<br>+                b->last_in_chain = 1;<br>+                b->memory = 1;<br>+<br>+                out.buf = b;<br>+                out.next = NULL;<br>+<br>+                in = sr->out;<br>+                in->next = &out;<br>+<br>+                ngx_http_send_header(r);<br>+<br>+                return ngx_http_output_filter(r, in);<br>+            }<br>+<br>             h = sr->headers_out.www_authenticate;<br> <br>             if (!h && sr->upstream) {<br>@@ -191,9 +231,12 @@<br> <br>     ps->handler = ngx_http_auth_request_done;<br>     ps->data = ctx;<br>-<br>+    /*<br>+    * response body is being kept in memory and client won't receive it<br>+    * use subrequest->out to access the chain buffer<br>+    */<br>     if (ngx_http_subrequest(r, &arcf->uri, NULL, &sr, ps,<br>-                            NGX_HTTP_SUBREQUEST_WAITED)<br>+                            NGX_HTTP_SUBREQUEST_IN_MEMORY)<br>         != NGX_OK)<br>     {<br>         return NGX_ERROR;<br>@@ -209,8 +252,6 @@<br>         return NGX_ERROR;<br>     }<br> <br>-    sr->header_only = 1;<br>-<br>     ctx->subrequest = sr;<br> <br>     ngx_http_set_ctx(r, ctx, ngx_http_auth_request_module);<br>@@ -323,6 +364,8 @@<br> <br>     conf->vars = NGX_CONF_UNSET_PTR;<br> <br>+    conf->enable = NGX_CONF_UNSET;<br>+<br>     return conf;<br> }<br> <br>@@ -335,6 +378,7 @@<br> <br>     ngx_conf_merge_str_value(conf->uri, prev->uri, "");<br>     ngx_conf_merge_ptr_value(conf->vars, prev->vars, NULL);<br>+    ngx_conf_merge_value(conf->enable, prev->enable, 0);<br> <br>     return NGX_CONF_OK;<br> }<br></div>