Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing HTTP status message using Sinatra

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.

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.

So, if a set the Sinatra response

get '/' do
   response.status = 453
end

I get the error code right:

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

But what I want to have is:

< HTTP/1.1 453 CLIENT_ERROR

The same way I have

< HTTP/1.1 200 OK

When everything goes according to the plan.

Is there anyway to do this using Sinatra/Rack?

like image 432
RobisonSantos Avatar asked Feb 19 '23 12:02

RobisonSantos


1 Answers

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.

With Thin:

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

and the output:

$ 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

and with WEBrick:

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

which gives the output:

$ 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
like image 181
matt Avatar answered Feb 26 '23 21:02

matt