Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reqsock:peek example wrong packet length calculation #181

Open
mattirantakomi opened this issue Nov 29, 2019 · 4 comments
Open

reqsock:peek example wrong packet length calculation #181

mattirantakomi opened this issue Nov 29, 2019 · 4 comments

Comments

@mattirantakomi
Copy link

mattirantakomi commented Nov 29, 2019

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.

@MasonLuo918
Copy link

em, I think the number 71 is first of the body character, for example, your http request like this:
GET /abc HTTP/1.1
The ascii of G is alway 71.

@nowanti
Copy link

nowanti commented Oct 1, 2021

I also encountered this problem, the first character is not the length.

@lisfox1
Copy link

lisfox1 commented Oct 30, 2024

I know its few yers later but have anyone found a fix for that?

@zhuizhuhaomeng
Copy link
Contributor

em, I think the number 71 is first of the body character, for example, your http request like this: GET /abc HTTP/1.1 The ascii of G is alway 71.

Yes. This is the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants