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

发送http2请求时,request close #6

Open
NICK-DUAN opened this issue Dec 6, 2020 · 2 comments
Open

发送http2请求时,request close #6

NICK-DUAN opened this issue Dec 6, 2020 · 2 comments

Comments

@NICK-DUAN
Copy link

参照文档中给出的例子,我改造了了一下,如下,并进行访问,得到了 client:request() failed: closed 的错误。

        local on_headers_reach = function(ctx, headers)
          print("received HEADERS frame:")
          for k, v in pairs(headers) do
              print(k, ": ", v)
          end
        end
        
        local on_data_reach = function(ctx, data)
            print("received DATA frame:")
            print(data)
        end
        
        local on_trailers_reach = function(ctx, data)
            print("received HEADERS frame for trailer headers:")
            for k, v in pairs(headers) do
                print(k, ": ", v)
            end
        end

        local host = "10.148.165.39"
        local port = 8081
        local sock = ngx.socket.tcp()
        print('start create socket ... ')
        local ok, err = sock:connect(host, port)
        if not ok then
          print("connect create failed: " .. err)
          os.exit(1)
        end

        print('start create header ... ')
        local headers = {
          { name = ":method", value = "GET" },
          { name = ":path", value = "/authorization" },
          { name = ":scheme", value = "http" },
          { name = "accept-encoding", value = "gzip" },
      }

        print('start create opt ... ')
        local opts = {
          ctx = sock,
          recv = sock.receive,
          send = sock.send,
          preread_size = 1024,
          max_concurrent_stream = 100,
      }

        print('start create http2 ... ')
        local client, err = http2.new(opts)
        if not client then
          print("client create failed: ", err)
          os.exit(2)
        end

        print('start request ... ')
        local ok, err = client:request(headers, nil, on_headers_reach, on_data_reach, on_trailers_reach)
        if not ok then
          print("client:request() failed: ", err)
          os.exit(3)
        end

        sock:close()

还有两个问题:

  1. 如果有 query 参数,是直接按规范写在 path 中吗?类似:
        local headers = {
          { name = ":method", value = "GET" },
          { name = ":path", value = "/authorization?a=1&b=2&c=3" },
          { name = ":scheme", value = "http" },
          { name = "accept-encoding", value = "gzip" },
      }
  1. 如何获取到返回值呢?如果靠回调函数的话,是否要设置一个全局变量去接收返回值?
@tokers
Copy link
Owner

tokers commented Dec 25, 2020

参照文档中给出的例子,我改造了了一下,如下,并进行访问,得到了 client:request() failed: closed 的错误。

        local on_headers_reach = function(ctx, headers)
          print("received HEADERS frame:")
          for k, v in pairs(headers) do
              print(k, ": ", v)
          end
        end
        
        local on_data_reach = function(ctx, data)
            print("received DATA frame:")
            print(data)
        end
        
        local on_trailers_reach = function(ctx, data)
            print("received HEADERS frame for trailer headers:")
            for k, v in pairs(headers) do
                print(k, ": ", v)
            end
        end

        local host = "10.148.165.39"
        local port = 8081
        local sock = ngx.socket.tcp()
        print('start create socket ... ')
        local ok, err = sock:connect(host, port)
        if not ok then
          print("connect create failed: " .. err)
          os.exit(1)
        end

        print('start create header ... ')
        local headers = {
          { name = ":method", value = "GET" },
          { name = ":path", value = "/authorization" },
          { name = ":scheme", value = "http" },
          { name = "accept-encoding", value = "gzip" },
      }

        print('start create opt ... ')
        local opts = {
          ctx = sock,
          recv = sock.receive,
          send = sock.send,
          preread_size = 1024,
          max_concurrent_stream = 100,
      }

        print('start create http2 ... ')
        local client, err = http2.new(opts)
        if not client then
          print("client create failed: ", err)
          os.exit(2)
        end

        print('start request ... ')
        local ok, err = client:request(headers, nil, on_headers_reach, on_data_reach, on_trailers_reach)
        if not ok then
          print("client:request() failed: ", err)
          os.exit(3)
        end

        sock:close()

还有两个问题:

  1. 如果有 query 参数,是直接按规范写在 path 中吗?类似:
        local headers = {
          { name = ":method", value = "GET" },
          { name = ":path", value = "/authorization?a=1&b=2&c=3" },
          { name = ":scheme", value = "http" },
          { name = "accept-encoding", value = "gzip" },
      }
  1. 如何获取到返回值呢?如果靠回调函数的话,是否要设置一个全局变量去接收返回值?

Try to enable debug log in your nginx.conf and see whether there are some logs in your error.log.

For questions:

  1. Yep, the querystring should be written in :path
  2. The status code is in the :status pseudo header

@lfdlxh
Copy link

lfdlxh commented Dec 12, 2023

请问这个问题解决了吗?

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

3 participants