Skip to content

Commit 798cc03

Browse files
Make sure we can capture the host header in HTTP1 and HTTP2.
1 parent 139307f commit 798cc03

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ext/hyper_ruby/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ fn init(ruby: &Ruby) -> Result<(), MagnusError> {
608608
request_class.define_method("path", method!(Request::path, 0))?;
609609
request_class.define_method("query_params", method!(Request::query_params, 0))?;
610610
request_class.define_method("query_param", method!(Request::query_param, 1))?;
611+
request_class.define_method("host", method!(Request::host, 0))?;
611612
request_class.define_method("header", method!(Request::header, 1))?;
612613
request_class.define_method("headers", method!(Request::headers, 0))?;
613614
request_class.define_method("body", method!(Request::body, 0))?;

ext/hyper_ruby/src/request.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ impl Request {
105105
RString::new(self.request.uri().path())
106106
}
107107

108+
pub fn host(&self) -> Value {
109+
match self.request.uri().host() {
110+
Some(host) => RString::new(host).as_value(),
111+
// Fallback to Host header if no host in URI object
112+
None => match self.request.headers().get("Host") {
113+
Some(value) => match value.to_str() {
114+
Ok(value) => RString::new(value).as_value(),
115+
Err(_) => qnil().as_value(),
116+
},
117+
None => qnil().as_value(),
118+
}
119+
}
120+
}
121+
108122
pub fn query_params(&self) -> RHash {
109123
let params = RHash::new();
110124
if let Some(query) = self.request.uri().query() {

test/test_http.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,35 @@ def test_query_params_with_encoding
241241
end
242242
end
243243

244+
def test_host
245+
with_server(-> (request) { handler_return_host(request) }) do |client|
246+
response = client.get("/")
247+
assert_equal 200, response.status
248+
# The Host header should contain the server address
249+
host_header = JSON.parse(response.body)["message"]
250+
assert_match(/127\.0\.0\.1/, host_header)
251+
end
252+
end
253+
254+
def test_http2_host
255+
with_server(-> (request) { handler_return_host(request) }) do |client|
256+
# Configure client for HTTP/2
257+
client = client.with(
258+
debug: STDERR,
259+
debug_level: 3,
260+
fallback_protocol: "h2"
261+
)
262+
263+
response = client.get("/")
264+
assert_equal 200, response.status
265+
assert_equal "2.0", response.version
266+
267+
# The Host header should contain the server address
268+
host_header = JSON.parse(response.body)["message"]
269+
assert_match(/127\.0\.0\.1/, host_header)
270+
end
271+
end
272+
244273
private
245274

246275
def handler_simple(request)
@@ -256,6 +285,11 @@ def handler_return_header(request, header_key)
256285
HyperRuby::Response.new(200, { 'Content-Type' => 'application/json' }, { message: request.header(header_key) }.to_json)
257286
end
258287

288+
def handler_return_host(request)
289+
pp request.host()
290+
HyperRuby::Response.new(200, { 'Content-Type' => 'application/json' }, { message: request.host() }.to_json)
291+
end
292+
259293
def handler_return_all_headers(request)
260294
HyperRuby::Response.new(200, { 'Content-Type' => 'application/json' }, { headers: request.headers }.to_json)
261295
end

0 commit comments

Comments
 (0)