使用Sinatra更改HTTP状态消息 [英] Changing HTTP status message using Sinatra

查看:182
本文介绍了使用Sinatra更改HTTP状态消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a simple Sinatra app, and given a user posts a request with an specific data, I want to return an error '453' (custom error code) with a message CLIENT_ERROR, or something similar.

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

The problem is: looking into the Sinatra documentation and doing some testing I couldn't find a way to setup the response error message, only the response status.

所以,如果一套Sinatra回复

So, if a set the Sinatra response

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进行此操作?

Is there anyway to do this using Sinatra/Rack?

推荐答案

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

The status message is generated by the server you are using, e.g. in Thin the messages are in Thin::HTTP_STATUS_CODES and the reponse line is generated in Thin::Response, and in WEBrick they are in WEBrick::HHTPStatus::StatusMessage and the response is generated in WEBrick::HTTPResponse.

如果您知道正在使用的服务器,可以将错误添加到相应的哈希值。

If you know what server you are using, you could add your error to the appropriate hash.

使用Thin:

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

这篇关于使用Sinatra更改HTTP状态消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆