Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ext/hyper_ruby/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ fn init(ruby: &Ruby) -> Result<(), MagnusError> {
request_class.define_method("path", method!(Request::path, 0))?;
request_class.define_method("query_params", method!(Request::query_params, 0))?;
request_class.define_method("query_param", method!(Request::query_param, 1))?;
request_class.define_method("host", method!(Request::host, 0))?;
request_class.define_method("header", method!(Request::header, 1))?;
request_class.define_method("headers", method!(Request::headers, 0))?;
request_class.define_method("body", method!(Request::body, 0))?;
Expand Down
14 changes: 14 additions & 0 deletions ext/hyper_ruby/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,20 @@ impl Request {
RString::new(self.request.uri().path())
}

pub fn host(&self) -> Value {
match self.request.uri().host() {
Some(host) => RString::new(host).as_value(),
// Fallback to Host header if no host in URI object
None => match self.request.headers().get("Host") {
Some(value) => match value.to_str() {
Ok(value) => RString::new(value).as_value(),
Err(_) => qnil().as_value(),
},
None => qnil().as_value(),
}
}
}

pub fn query_params(&self) -> RHash {
let params = RHash::new();
if let Some(query) = self.request.uri().query() {
Expand Down
33 changes: 33 additions & 0 deletions test/test_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,35 @@ def test_query_params_with_encoding
end
end

def test_host
with_server(-> (request) { handler_return_host(request) }) do |client|
response = client.get("/")
assert_equal 200, response.status
# The Host header should contain the server address
host_header = JSON.parse(response.body)["message"]
assert_match(/127\.0\.0\.1/, host_header)
end
end

def test_http2_host
with_server(-> (request) { handler_return_host(request) }) do |client|
# Configure client for HTTP/2
client = client.with(
debug: STDERR,
debug_level: 3,
fallback_protocol: "h2"
)

response = client.get("/")
assert_equal 200, response.status
assert_equal "2.0", response.version

# The Host header should contain the server address
host_header = JSON.parse(response.body)["message"]
assert_match(/127\.0\.0\.1/, host_header)
end
end

private

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

def handler_return_host(request)
HyperRuby::Response.new(200, { 'Content-Type' => 'application/json' }, { message: request.host() }.to_json)
end

def handler_return_all_headers(request)
HyperRuby::Response.new(200, { 'Content-Type' => 'application/json' }, { headers: request.headers }.to_json)
end
Expand Down
Loading