Rack::Request

编程入门 行业动态 更新时间:2024-10-17 00:20:37
本文介绍了Rack::Request - 如何获取所有标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

标题是不言自明的.有什么方法可以获取标头(Rack::Request.env[] 除外)?

The title is pretty self-explanatory. Is there any way to get the headers (except for Rack::Request.env[])?

推荐答案

Rack 环境中提供 HTTP 标头 传递给您的应用:

HTTP_ 变量:对应于客户端提供的 HTTP 请求标头的变量(即名称以 HTTP_ 开头的变量).这些变量的存在与否应与请求中相应 HTTP 标头的存在或不存在相对应.

HTTP_ Variables: Variables corresponding to the client-supplied HTTP request headers (i.e., variables whose names begin with HTTP_). The presence or absence of these variables should correspond with the presence or absence of the appropriate HTTP header in the request.

因此 HTTP 标头以HTTP_"为前缀并添加到哈希中.

So the HTTP headers are prefixed with "HTTP_" and added to the hash.

这是一个提取并显示它们的小程序:

Here's a little program that extracts and displays them:

require 'rack' app = Proc.new do |env| headers = env.select {|k,v| k.start_with? 'HTTP_'} .collect {|key, val| [key.sub(/^HTTP_/, ''), val]} .collect {|key, val| "#{key}: #{val}<br>"} .sort [200, {'Content-Type' => 'text/html'}, headers] end Rack::Server.start :app => app, :Port => 8080

当我运行它时,除了 Chrome 或 Firefox 显示的 HTTP 标头之外,还有一个VERSION:HTPP/1.1"(即一个带有键HTTP_VERSION"和值HTTP/1.1"的条目正在添加到 env 哈希).

When I run this, in addition to the HTTP headers as shown by Chrome or Firefox, there is a "VERSION: HTPP/1.1" (i.e. an entry with key "HTTP_VERSION" and value "HTTP/1.1" is being added to the env hash).

更多推荐

Rack::Request

本文发布于:2023-11-14 19:59:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1588382.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Rack   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!