Changing HTTP status message using Sinatra

前端 未结 2 2005
甜味超标
甜味超标 2021-01-13 12:14

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,

相关标签:
2条回答
  • 2021-01-13 12:50

    I would recommend not to use custom HTTP status codes. If you think you have something of general use, consider writing an Internet Draft and going through the IETF specification process.

    0 讨论(0)
  • 2021-01-13 12:51

    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
    
    0 讨论(0)
提交回复
热议问题