From 5ed9fd6c99340674d19a935a769b37c2f9e8a10c Mon Sep 17 00:00:00 2001 From: Joshua Huber Date: Mon, 23 Oct 2023 12:14:34 -0400 Subject: [PATCH] Save reason phrase in Protocol::HTTP1::Response 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. --- lib/async/http/protocol/http1/response.rb | 7 +++++-- test/async/http/protocol/http11.rb | 7 +++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/async/http/protocol/http1/response.rb b/lib/async/http/protocol/http1/response.rb index 67e402d6..70c75fc3 100644 --- a/lib/async/http/protocol/http1/response.rb +++ b/lib/async/http/protocol/http1/response.rb @@ -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) @@ -30,7 +33,7 @@ def initialize(connection, version, status, reason, headers, body) def connection @connection end - + def hijack? @body.nil? end diff --git a/test/async/http/protocol/http11.rb b/test/async/http/protocol/http11.rb index af2497ff..67448c2a 100755 --- a/test/async/http/protocol/http11.rb +++ b/test/async/http/protocol/http11.rb @@ -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 @@ -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