Open initprism opened 2 years ago
Thanks for working on this! Just a few suggestions on the shape of the API.
I think we need to make this a bit more flexible to support the more advanced patterns that curl also supports, for example directing to a specific port, or "matching" any port.
More generally, it seems like curl allows you to override the host, or the port, or both. For each override, the rule can either match on everything, host, port, or both. I'd like to avoid curl's "stringly-typed" approach of omitting a parameter though of just an empty string. So at risk of being more complicated, I'm imagining something that might be used like this?
let client = HttpClient::builder()
.rewrite_rules(RewriteRules::builder()
// Match example.com, any port, send to example.org with same port
.add(Matcher::host("example.com"), Rewrite::host("example.org"))
// Match anything with port 8080, send to same host on port 8081
.add(Matcher::port(8080), Rewrite::port(8081))
// Change specific host and port to different host and port
.add(Matcher::host_and_port("example.com", 9000), Rewrite::host_and_port("example.org", 9001))
// Match everything, send to example.org with original port
.add(Matcher::any(), Rewrite::host("example.org"))
.build())
// Or equivalently, using IntoIterator:
.rewrite_rules([
(Matcher::host("example.com"), Rewrite::host("example.org")),
(Matcher::port(8080), Rewrite::port(8081)),
(Matcher::host_and_port("example.com", 9000), Rewrite::host_and_port("example.org", 9001)),
(Matcher::any(), Rewrite::host("example.org")),
])
.build()?;
An API like this prevents you from creating empty rules; you must have a rewrite action, but while both destination host and port are optional at least one or the other must be present. What do you think?
I'm not sure about everything yet, one thing that might be nice to allow is for individual requests to override or add their own rules without overwriting the entire list of rules that might be set on the client itself. Maybe that means that we store the rules as a map and the rule matching terms implement Eq
and Hash
so that specific rules can be replaced. Though it is also worth noting that curl evaluates the rules from top to bottom, so order is important so maybe we can't do that.
Codecov Report
74.12% <ø> (ø)
60.00% <50.00%> (-2.50%)
54.54% <54.54%> (ø)
79.55% <100.00%> (+0.23%)
Continue to review full report at Codecov.