isahc icon indicating copy to clipboard operation
isahc copied to clipboard

impl host_rule (CURLOPT_CONNECT_TO)

Open initprism opened this issue 2 years ago • 2 comments

initprism avatar Nov 09 '21 00:11 initprism

Codecov Report

Merging #363 (9ddf6b4) into master (46a6593) will decrease coverage by 0.08%. The diff coverage is 62.50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #363      +/-   ##
==========================================
- Coverage   79.38%   79.29%   -0.09%     
==========================================
  Files          51       52       +1     
  Lines        3061     3077      +16     
==========================================
+ Hits         2430     2440      +10     
- Misses        631      637       +6     
Impacted Files Coverage Δ
src/config/mod.rs 74.12% <ø> (ø)
src/config/client.rs 60.00% <50.00%> (-2.50%) :arrow_down:
src/config/host_rule.rs 54.54% <54.54%> (ø)
src/client.rs 79.55% <100.00%> (+0.23%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 46a6593...9ddf6b4. Read the comment docs.

codecov[bot] avatar Nov 09 '21 02:11 codecov[bot]

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.

sagebind avatar Nov 09 '21 04:11 sagebind