Skip to content

Commit

Permalink
Save reason phrase in Protocol::HTTP1::Response
Browse files Browse the repository at this point in the history
Save away the raw reason phrase provided by the server. This is of
limited niche value, as the reason phrase seems to be _mostly_
unused/ignored.

One example of where it's needed:

The [Proxmox VE API](https://pve.proxmox.com/wiki/Proxmox_VE_API)
ships error reason strings back using the HTTP reason phrase. Without
this side-channel, error status is 100% opaque/unavailable.
  • Loading branch information
uberjay committed Oct 23, 2023
1 parent 26a52f0 commit 5ed9fd6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
7 changes: 5 additions & 2 deletions lib/async/http/protocol/http1/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ def self.read(connection, request)

UPGRADE = 'upgrade'

# @param reason [String] HTTP response line reason, ignored.
attr_reader :reason

# @param reason [String] HTTP response line reason phrase
def initialize(connection, version, status, reason, headers, body)
@connection = connection
@reason = reason

protocol = headers.delete(UPGRADE)

Expand All @@ -30,7 +33,7 @@ def initialize(connection, version, status, reason, headers, body)
def connection
@connection
end

def hijack?
@body.nil?
end
Expand Down
7 changes: 7 additions & 0 deletions test/async/http/protocol/http11.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def around
expect(response).to be(:success?)
expect(response.version).to be == "HTTP/1.1"
expect(response.body).to be(:empty?)
expect(response.reason).to be == "OK"

response.read
end
Expand Down Expand Up @@ -74,6 +75,12 @@ def around

expect(response.read).to be == "Hello World!"
end

it "has access to the http reason phrase" do
response = client.head("/")

expect(response.reason).to be == "It worked!"
end
end
end
end

0 comments on commit 5ed9fd6

Please sign in to comment.