nitrogen / simple_bridge

A simple, standardized interface library to Erlang HTTP Servers.
MIT License
112 stars 76 forks source link

Add `simple_bridge:host/1` callback #80

Closed dmsnell closed 1 year ago

dmsnell commented 1 year ago

Status

I know nothing about what I'm doing here and I didn't test any of this. What I've put together represents a draft idea for supporting a new interface to get the host data out of simple_bridge. I've only used cowboy and wouldn't say I'm an expert on how it operates or handles the host value (though I hope to experiment and test to find out).

Would like some review of the general idea and approach. See #79 for a motivating example: nitrogen_core crashes when calling wf:url() on a cowboy TLS server because it doesn't provide a host HTTP header. A cursory glance around the web turns up that Golang's standard library lifts the host header out of the headers and into a request property there as well. some part of Python's requests might omit the Host HTTP header in some cases but I didn't look deep enough to confirm.

Description

The host value for a given request may depend on multiple values:

When applications rely on getting this value diretly through the HTTP headers they may run into issues with this being undefined or wrong. Possible scenarios may include:

In this patch we're adding a new host/1 callback so that the underlying implementation can provide a proper value for the request's host, whether or not it's set as an HTTP header, and whether or not a proxy might be providing more accurate information.

choptastic commented 1 year ago

This is great stuff. This and your other PR were finally merged. I made a few small changes, but nothing major, and I added some tests.

One thing I didn't add a test for was the TLS or the X-Forwarded-For header, but I really like your suggestion for it.