-
Notifications
You must be signed in to change notification settings - Fork 14k
common: introduce http.h for httplib-based client #16373
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
Merged
ggerganov
merged 2 commits into
ggml-org:master
from
angt:common-introduce-http.h-for-httplib-based-client
Oct 1, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| #pragma once | ||
|
|
||
| #include <cpp-httplib/httplib.h> | ||
|
|
||
| struct common_http_url { | ||
| std::string scheme; | ||
| std::string user; | ||
| std::string password; | ||
| std::string host; | ||
| std::string path; | ||
| }; | ||
|
|
||
| static common_http_url common_http_parse_url(const std::string & url) { | ||
| common_http_url parts; | ||
| auto scheme_end = url.find("://"); | ||
|
|
||
| if (scheme_end == std::string::npos) { | ||
| throw std::runtime_error("invalid URL: no scheme"); | ||
| } | ||
| parts.scheme = url.substr(0, scheme_end); | ||
|
|
||
| if (parts.scheme != "http" && parts.scheme != "https") { | ||
| throw std::runtime_error("unsupported URL scheme: " + parts.scheme); | ||
| } | ||
|
|
||
| auto rest = url.substr(scheme_end + 3); | ||
| auto at_pos = rest.find('@'); | ||
|
|
||
| if (at_pos != std::string::npos) { | ||
| auto auth = rest.substr(0, at_pos); | ||
| auto colon_pos = auth.find(':'); | ||
| if (colon_pos != std::string::npos) { | ||
| parts.user = auth.substr(0, colon_pos); | ||
| parts.password = auth.substr(colon_pos + 1); | ||
| } else { | ||
| parts.user = auth; | ||
| } | ||
| rest = rest.substr(at_pos + 1); | ||
| } | ||
|
|
||
| auto slash_pos = rest.find('/'); | ||
|
|
||
| if (slash_pos != std::string::npos) { | ||
| parts.host = rest.substr(0, slash_pos); | ||
| parts.path = rest.substr(slash_pos); | ||
| } else { | ||
| parts.host = rest; | ||
| parts.path = "/"; | ||
| } | ||
| return parts; | ||
| } | ||
|
|
||
| static std::pair<httplib::Client, common_http_url> common_http_client(const std::string & url) { | ||
| common_http_url parts = common_http_parse_url(url); | ||
|
|
||
| if (parts.host.empty()) { | ||
| throw std::runtime_error("error: invalid URL format"); | ||
| } | ||
|
|
||
| httplib::Client cli(parts.scheme + "://" + parts.host); | ||
|
|
||
| if (!parts.user.empty()) { | ||
| cli.set_basic_auth(parts.user, parts.password); | ||
| } | ||
|
|
||
| cli.set_follow_location(true); | ||
|
|
||
| return { std::move(cli), std::move(parts) }; | ||
| } | ||
|
|
||
| static std::string common_http_show_masked_url(const common_http_url & parts) { | ||
| return parts.scheme + "://" + (parts.user.empty() ? "" : "****:****@") + parts.host + parts.path; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like the current parser does not take into account query params. I think it can be better if we can make it rfc3986-compliant, some test cases are here: https://github.com/jholloc/simple-uri-parser/blob/main/tests.cpp
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm nevermind, it may not be necessary for now as the query param can be contained request path (ref HTTP specs), so we don't actually need to parse it