Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

impl host_rule (CURLOPT_CONNECT_TO) #363

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

initprism
Copy link

No description provided.

@sagebind sagebind linked an issue Nov 9, 2021 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Nov 9, 2021

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%) ⬇️
src/config/host_rule.rs 54.54% <54.54%> (ø)
src/client.rs 79.55% <100.00%> (+0.23%) ⬆️

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.

@sagebind
Copy link
Owner

sagebind commented Nov 9, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

support fo connect_to
2 participants