Have problems adding header 'Set-Cookie' to headers_out in my nginx sub request module

hcnhcn012 nginx-forum at forum.nginx.org
Tue Jul 24 08:29:37 UTC 2018


**nginx version: `1.10.3`**

Here's my code to add 'Set-Cookie' to headers:

    void add_headers_out(ngx_http_request_t *r, char* cookies)
    {
        ngx_table_elt_t  *h;
        ngx_str_t k = ngx_string("Set-Cookie");
        ngx_str_t v = ngx_string(cookies);
        h = ngx_list_push(&r->headers_out.headers);
        if(h == NULL)
        {
            return ;
        }
        h->hash = ngx_hash_key_lc(k.data, k.len);
        h->key.len = k.len;
        h->key.data = k.data;
        h->value.len = v.len;
        h->value.data = v.data;
    }
When I call `add_headers_out` in my parent request handler:

    static void multipost_post_handler(ngx_http_request_t *r)
    {
        ...
        ///////// fill up headers and body
        //// body
        int bodylen = body.len;
        ngx_buf_t *b = ngx_create_temp_buf(r->pool, bodylen);
        b->pos = body.data;
        b->last = b->pos + bodylen;
        b->last_buf = 1;
        ngx_chain_t out;
        out.buf = b;
        out.next = NULL;

        //// headers
        r->headers_out.content_type = myctx->content_type;
        r->headers_out.content_length_n = bodylen;
        r->headers_out.status = myctx->status_code;

        // myctx->cookie1: "PHPSESSID=1f74a78647e192496597c240de765d45;"
        add_headers_out(r, myctx->cookie1);

        // Test: checking additional headers by iterating
headers_out.headers
        get_headers_out(r);
        // returns: "Set-Cookie :
PHPSESSID=1f74a78647e192496597c240de765d45;"

        // Send response to client
        r->connection->buffered |= NGX_HTTP_WRITE_BUFFERED;
        ngx_int_t ret = ngx_http_send_header(r);
        ret = ngx_http_output_filter(r, &out);
        ngx_http_finalize_request(r, ret);
        return ;
    }
It seems no problem in my code, but when I use my nginx module as a reverse
proxy module to some sites, I find `Set-Cookie` is different. For example, I
can only see some small part of original `Set-Cookie: PHPSES(then go with
nothing)` through chrome. I do not know what cause that problem. Thanks for
helping!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,280625,280625#msg-280625



More information about the nginx mailing list