get raw header on http 2.0

434 views
Skip to first unread message

Hadi Abbasi

unread,
May 26, 2018, 6:45:43 AM5/26/18
to openresty-en
Hey friends...
I want to get the request header on http2 as string like:
GET /docs/index.html HTTP/1.1
Host: www.host.com
Accept: image/gif, image/jpeg, */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
but I know that ngx.req.raw_header doesn't support Http 2.0 so  I want to know if there is a function that I can access to raw header on HTTP 2.0 ?
thanks a lot...
Best,

tokers

unread,
May 27, 2018, 9:45:57 PM5/27/18
to openresty-en
Hello!

There is no such API that you can get these "raw headers". Actually the raw headers are really some HEADERS frame which form is binary and without the human readability.

Hadi Abbasi

unread,
May 28, 2018, 6:36:14 AM5/28/18
to openresty-en
thanks a lot tokers...but my requirement is so simple...
I just need to have request header as simple string like:
GET /uri/ HTTP/1.1
Host: xyz.com
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: PHPSESSID=r2t5uvjq435r4q7ib3vtdjq120
Pragma: no-cache
Cache-Control: no-cache

I want to access to its properties using my simple header parser.
so all I wanna get is my request header as a string like upper example.
in http 1 I can get it using  ngx.req.raw_header()
but it seems to be impossible on http 2 yet and I don't know what I have to do!
thanks...
Best,

tokers

unread,
May 28, 2018, 9:53:10 PM5/28/18
to openresty-en
Hello!

Is the order significant? Maybe you can just mimic this by using ngx.req.get_headers() and assemble the request line by method, request_uri and http version.

Hadi Abbasi

unread,
May 31, 2018, 4:58:12 PM5/31/18
to openre...@googlegroups.com
yeah it can help me, and I think I can create iteration on req.get_headers so I can get all of the properties and their values! then I can concat all of these properties in a string!
I will try it and then I will report the result here...thanks a lot...

On Tue, May 29, 2018 at 6:23 AM, tokers <zcha...@gmail.com> wrote:
Hello!

Is the order significant? Maybe you can just mimic this by using ngx.req.get_headers() and assemble the request line by method, request_uri and http version.

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openresty-en+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hadi Abbasi

unread,
Jun 12, 2018, 1:36:17 AM6/12/18
to openresty-en
hey tokers...thanks a lot... your suggestion is ok and I have solved it!

function fixedRawReqHeader()
 
local result = ""
 result
=  ngx.req.get_method() .. " " .. ngx.var.uri .. " HTTP/" ..ngx.req.http_version() .. "\n"
 
for k, v in pairs(ngx.req.get_headers()) do
 result
= result .. tostring(k) .. ":" .. tostring(v) .. "\n"
 
end
 
 
return result
end


just another simple question!
I want to get the size (length) of request header , request body , response header and response body! I need to know the main size of original data (not my generated header) how can I do that?
I think if we have req or resp body, we can get the size of req or resp body using ngx.req.get_headers()["Content-Length"] or ngx.resp.get_headers()["Content-Length"]
but I have no Idea for getting the size of headers using nginx methods!
thanks a lot...
Best,

tokers

unread,
Jun 12, 2018, 1:54:14 AM6/12/18
to openresty-en
Hello!

> I want to get the size (length) of request header , request body , response header and response body! I need to know the main size of original data (not my generated header) how can I do that?
> I think if we have req or resp body, we can get the size of req or resp body using ngx.req.get_headers()["Content-Length"] or ngx.resp.get_headers()["Content-Length"]
> but I have no Idea for getting the size of headers using nginx methods!

For the part of request headers and request body, you can use the variable $request_length from ngx.var.request_length

For the part of response headers, there is no direct way, you may estimate the size by adding the delimiter, the colon yourself. But some other nginx modules may add their headers, so whether it is exact is depending on the phase that you implement it, for example, you can calculate this in the log phase, I think it is a good phase :).
Message has been deleted

Hadi Abbasi

unread,
Jun 12, 2018, 7:40:09 AM6/12/18
to openresty-en
thank you friend... your comment was helpful...thanks...good luck...
Best,
Reply all
Reply to author
Forward
0 new messages