【问题标题】:Changing HTTP status message using Sinatra使用 Sinatra 更改 HTTP 状态消息
【发布时间】:2012-08-13 00:04:46
【问题描述】:

我正在编写一个简单的 Sinatra 应用程序,并且给定用户发布带有特定数据的请求,我想返回错误“453”(自定义错误代码)以及消息 CLIENT_ERROR 或类似内容。

问题是:查看 Sinatra 文档并进行一些测试,我找不到设置响应错误消息的方法,只有响应状态。

所以,如果设置 Sinatra 响应

get '/' do
   response.status = 453
end

我得到了正确的错误代码:

curl -v localhost:4567

* About to connect() to localhost port 4567 (#0)
*   Trying 127.0.0.1... connected
> GET / HTTP/1.1
> User-Agent: curl/7.22.0 (x86_64-pc-linux-gnu) libcurl/7.22.0 OpenSSL/1.0.1 zlib/1.2.3.4     libidn/1.23 librtmp/2.3
> Host: localhost:4567
> Accept: */*
> 
< HTTP/1.1 453 
< X-Frame-Options: sameorigin
< X-XSS-Protection: 1; mode=block
< Content-Type: text/html;charset=utf-8
< Content-Length: 0
< Connection: keep-alive
< Server: thin 1.3.1 codename Triple Espresso
< 
* Connection #0 to host localhost left intact
* Closing connection #0

但我想要的是:

< HTTP/1.1 453 CLIENT_ERROR

和我一样

< HTTP/1.1 200 OK

当一切按计划进行时。

有没有办法使用 Sinatra/Rack 来做到这一点?

【问题讨论】:

    标签: ruby http sinatra rack http-status


    【解决方案1】:

    状态消息由您使用的服务器生成,例如在 Thin 中,消息在 Thin::HTTP_STATUS_CODES 中,响应行在 Thin::Response 中生成,在 WEBrick 中,它们在 WEBrick::HHTPStatus::StatusMessage 中,响应在 WEBrick::HTTPResponse 中生成。

    如果您知道自己使用的是什么服务器,则可以将错误添加到适当的哈希中。

    薄:

    require 'thin'
    Thin::HTTP_STATUS_CODES[453] = "Client Error"
    

    和输出:

    $ curl -v localhost:4567
    * About to connect() to localhost port 4567 (#0)
    *   Trying 127.0.0.1... connected
    * Connected to localhost (127.0.0.1) port 4567 (#0)
    > GET / HTTP/1.1
    > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
    > Host: localhost:4567
    > Accept: */*
    > 
    < HTTP/1.1 453 Client Error
    < X-Frame-Options: sameorigin
    < X-XSS-Protection: 1; mode=block
    < Content-Type: text/html;charset=utf-8
    < Content-Length: 0
    < Connection: keep-alive
    < Server: thin 1.4.1 codename Chromeo
    < 
    * Connection #0 to host localhost left intact
    * Closing connection #0
    

    和 WEBrick:

    require 'webrick'
    WEBrick::HTTPStatus::StatusMessage[453] = "Client Error"
    

    给出输出:

    $ curl -v localhost:4567
    * About to connect() to localhost port 4567 (#0)
    *   Trying 127.0.0.1... connected
    * Connected to localhost (127.0.0.1) port 4567 (#0)
    > GET / HTTP/1.1
    > User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
    > Host: localhost:4567
    > Accept: */*
    > 
    localhost - - [13/Aug/2012:01:41:48 BST] "GET / HTTP/1.1" 453 0
    - -> /
    < HTTP/1.1 453 Client Error 
    < X-Frame-Options: sameorigin
    < X-Xss-Protection: 1; mode=block
    < Content-Type: text/html;charset=utf-8
    < Content-Length: 0
    < Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
    < Date: Mon, 13 Aug 2012 00:41:48 GMT
    < Connection: Keep-Alive
    < 
    * Connection #0 to host localhost left intact
    * Closing connection #0
    

    【讨论】:

    • 在 Nginx +Passenger 下运行时,这可能来自 Ruby 代码吗?
    【解决方案2】:

    我建议不要使用自定义 HTTP 状态代码。如果您认为自己有一些通用的东西,请考虑编写 Internet 草案并通过 IETF 规范流程。

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2014-05-23
      • 2014-02-20
      • 1970-01-01
      • 2020-03-19
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多