如何从 Sinatra 中转储 HTTP 请求?

发布于 2024-09-28 18:46:22 字数 67 浏览 0 评论 0原文

有没有一种方法可以按照应用程序接收数据的方式将所有传入请求转储到 Sinatra 应用程序?也许某种 Rack 中间件?

Is there a way to dump all incoming requests to a Sinatra application in the exact way the application receives the data? Maybe some sort of Rack middleware?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

地狱即天堂 2024-10-05 18:46:22

当我想调试“事物”时,我使用 -D-V 标志运行瘦:

$ thin start -p 3000 -R config.ru -D -V

-D, --debug                      Set debbuging on
-V, --trace                      Set tracing on (log raw request/response)

如果您尝试从请求获取原始输出,请使用该请求方法如下:

  # app running on http://example.com/example
  get '/foo' do
    request.body              # request body sent by the client (see below)
    request.scheme            # "http"
    request.script_name       # "/example"
    request.path_info         # "/foo"
    request.port              # 80
    request.request_method    # "GET"
    request.query_string      # ""
    request.content_length    # length of request.body
    request.media_type        # media type of request.body
    request.host              # "example.com"
    request.get?              # true (similar methods for other verbs)
    request.form_data?        # false
    request["SOME_HEADER"]    # value of SOME_HEADER header
    request.referer           # the referer of the client or '/'
    request.user_agent        # user agent (used by :agent condition)
    request.cookies           # hash of browser cookies
    request.xhr?              # is this an ajax request?
    request.url               # "http://example.com/example/foo"
    request.path              # "/example/foo"
    request.ip                # client IP address
    request.secure?           # false
    request.env               # raw env hash handed in by Rack
  end

请参阅“入门”了解更多信息。

I run thin with the -D and -V flags when I want to debug 'things':

$ thin start -p 3000 -R config.ru -D -V

-D, --debug                      Set debbuging on
-V, --trace                      Set tracing on (log raw request/response)

If you are trying to get the raw output from a request, use the request method like:

  # app running on http://example.com/example
  get '/foo' do
    request.body              # request body sent by the client (see below)
    request.scheme            # "http"
    request.script_name       # "/example"
    request.path_info         # "/foo"
    request.port              # 80
    request.request_method    # "GET"
    request.query_string      # ""
    request.content_length    # length of request.body
    request.media_type        # media type of request.body
    request.host              # "example.com"
    request.get?              # true (similar methods for other verbs)
    request.form_data?        # false
    request["SOME_HEADER"]    # value of SOME_HEADER header
    request.referer           # the referer of the client or '/'
    request.user_agent        # user agent (used by :agent condition)
    request.cookies           # hash of browser cookies
    request.xhr?              # is this an ajax request?
    request.url               # "http://example.com/example/foo"
    request.path              # "/example/foo"
    request.ip                # client IP address
    request.secure?           # false
    request.env               # raw env hash handed in by Rack
  end

See "GETTING STARTED" for more information.

掐死时间 2024-10-05 18:46:22

也许这不是您要问的,但我来到这里寻找一种方法来查看 Sinatra 中的所有 HTTP 请求标头(实际上不必枚举它们,以调试代理请求)。我发现这非常有用:

get "/my_route" do
  puts "#{ request.env }"
end

或者,以清晰的 json 格式在响应中返回该 blob:

require 'json'
get "/my_route" do
  content_type :text
  return JSON.pretty_generate(request.env)
end

瞧,所有请求详细信息:

{
  "SERVER_SOFTWARE": "thin 1.6.2 codename Doc Brown",
  "SERVER_NAME": "10.0.1.3",
  "rack.input": "#<StringIO:0x00000002bf82c0>",
  "rack.version": [
    1,
    0
  ],
  "rack.errors": "#<IO:0x00000002549b90>",
  "rack.multithread": false,
  "rack.multiprocess": false,
  "rack.run_once": false,
  "REQUEST_METHOD": "GET",
  "REQUEST_PATH": "/my_route",
  "PATH_INFO": "/my_route",
  "REQUEST_URI": "/my_route",
  "HTTP_VERSION": "HTTP/1.0",
  "HTTP_X_FORWARDED_FOR": "10.0.1.3, 127.0.0.1, 127.0.0.1, 127.0.0.1",
  "HTTP_HOST": "10.0.1.3:9393",
  "HTTP_CONNECTION": "close",
  "HTTP_X_REAL_IP": "10.0.1.3",
  "HTTP_X_FE_SCHEME": "http",
  "HTTP_X_FE_HOST": "10.0.10.145",
  "HTTP_X_FE_ROUTE": "/my_route",
  "HTTP_ACCEPT": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
  "HTTP_ACCEPT_LANGUAGE": "en-US,en;q=0.8",
  "HTTP_X_VARNISH": "917254702",
  "HTTP_ACCEPT_ENCODING": "gzip",
  "GATEWAY_INTERFACE": "CGI/1.2",
  "SERVER_PORT": "9393",
  "QUERY_STRING": "",
  "SERVER_PROTOCOL": "HTTP/1.1",
  "rack.url_scheme": "http",
  "SCRIPT_NAME": "",
  "REMOTE_ADDR": "10.0.10.145",
  "async.callback": "#<Method: Thin::Connection#post_process>",
  "async.close": "#<EventMachine::DefaultDeferrable:0x00000002a60070>",
  "rack.logger": "#<Rack::NullLogger:0x00000004154ad8>",
  "rack.request.query_string": "",
  "rack.request.query_hash": {
  },
  "sinatra.route": "GET (?-mix:^\\/my_route$)"
}

Maybe this is not what you're asking, but I arrived here looking for a way to see all the HTTP request headers in Sinatra (without actually having to enumerate them, to debug a proxied request). I found this quite useful:

get "/my_route" do
  puts "#{ request.env }"
end

Or, to return that blob in the response in a legible json format:

require 'json'
get "/my_route" do
  content_type :text
  return JSON.pretty_generate(request.env)
end

And voila, all the request details:

{
  "SERVER_SOFTWARE": "thin 1.6.2 codename Doc Brown",
  "SERVER_NAME": "10.0.1.3",
  "rack.input": "#<StringIO:0x00000002bf82c0>",
  "rack.version": [
    1,
    0
  ],
  "rack.errors": "#<IO:0x00000002549b90>",
  "rack.multithread": false,
  "rack.multiprocess": false,
  "rack.run_once": false,
  "REQUEST_METHOD": "GET",
  "REQUEST_PATH": "/my_route",
  "PATH_INFO": "/my_route",
  "REQUEST_URI": "/my_route",
  "HTTP_VERSION": "HTTP/1.0",
  "HTTP_X_FORWARDED_FOR": "10.0.1.3, 127.0.0.1, 127.0.0.1, 127.0.0.1",
  "HTTP_HOST": "10.0.1.3:9393",
  "HTTP_CONNECTION": "close",
  "HTTP_X_REAL_IP": "10.0.1.3",
  "HTTP_X_FE_SCHEME": "http",
  "HTTP_X_FE_HOST": "10.0.10.145",
  "HTTP_X_FE_ROUTE": "/my_route",
  "HTTP_ACCEPT": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36",
  "HTTP_ACCEPT_LANGUAGE": "en-US,en;q=0.8",
  "HTTP_X_VARNISH": "917254702",
  "HTTP_ACCEPT_ENCODING": "gzip",
  "GATEWAY_INTERFACE": "CGI/1.2",
  "SERVER_PORT": "9393",
  "QUERY_STRING": "",
  "SERVER_PROTOCOL": "HTTP/1.1",
  "rack.url_scheme": "http",
  "SCRIPT_NAME": "",
  "REMOTE_ADDR": "10.0.10.145",
  "async.callback": "#<Method: Thin::Connection#post_process>",
  "async.close": "#<EventMachine::DefaultDeferrable:0x00000002a60070>",
  "rack.logger": "#<Rack::NullLogger:0x00000004154ad8>",
  "rack.request.query_string": "",
  "rack.request.query_hash": {
  },
  "sinatra.route": "GET (?-mix:^\\/my_route$)"
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文