openresty / openresty/stream-lua-nginx-module
reqsock:peek example wrong packet length calculation
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 749
- Forks
- 210
- Avg merge
- 4h 31m
- Merged PRs (30d)
- 3
Description
Example taken from https://github.com/openresty/stream-lua-nginx-module#reqsockpeek
local sock = assert(ngx.req.socket())
local data = assert(sock:peek(1)) -- peek the first 1 byte that contains the length
data = string.byte(data)
data = assert(sock:peek(data + 1)) -- peek the length + the size byte
local payload = data:sub(2) -- trim the length byte to get actual payload
ngx.log(ngx.INFO, "payload is: ", payload)
I'm using Ubuntu 18.04 LTS with Openresty 1.15.8.2-1~bionic1 and I have tried to use the above example with the following configuration:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
stream {
resolver 1.1.1.1 ipv6=off valid=1s;
server {
listen 443;
proxy_pass $ssl_preread_server_name.backend.server.local:443;
ssl_preread on;
}
server {
listen 80;
# $the_variable_that_i_want_to_extract = HTTP Host Header taken from request headers
proxy_pass $the_variable_that_i_want_to_extract.backend.server.local:80;
preread_by_lua_block {
local sock = assert(ngx.req.socket())
local data = assert(sock:peek(1))
data = string.byte(data)
ngx.log(ngx.CRIT, "payload is: ", data)
}
}
}
The payload size returned is always 71 even if I make a HTTP request with a very long header "Host: this-is-a-very-long-hostname-which-is-surely-over-71-bytes-long-just-for.example.com". So it seems that your example code will truncate sock:peek length to 71 which is usually not enough to grab all the header data.
What I'm trying to do is to extract HTTP request header data to be able to put HTTP Host Header in to a variable to be able to use it as a part of proxy_pass hostname in a similar way that I can do with HTTPS SSL SNI connections with ngx_stream_ssl_preread_module (http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html) $ssl_preread_server_name variable.
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the reqsock:peek example linked in the issue and compare its length calculation with the reported stream configuration. Check the documented behavior of sock:peek and the example’s assumptions about packet data. Done means the example accurately describes the returned length and its limits, with any required correction documented.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- documentation
- Issue type
- Documentation
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100