|
32 | 32 | #include <thread> |
33 | 33 | #include <vector> |
34 | 34 |
|
35 | | -//#define LLAMA_USE_CURL |
36 | | - |
37 | 35 | #if defined(LLAMA_USE_CURL) |
38 | 36 | #include <curl/curl.h> |
39 | 37 | #include <curl/easy.h> |
40 | 38 | #else |
41 | | -#include <cpp-httplib/httplib.h> |
| 39 | +#include "http.h" |
42 | 40 | #endif |
43 | 41 |
|
44 | 42 | #ifdef __linux__ |
@@ -596,77 +594,6 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string & |
596 | 594 |
|
597 | 595 | #else |
598 | 596 |
|
599 | | -struct common_url { |
600 | | - std::string scheme; |
601 | | - std::string user; |
602 | | - std::string password; |
603 | | - std::string host; |
604 | | - std::string path; |
605 | | -}; |
606 | | - |
607 | | -static common_url parse_url(const std::string & url) { |
608 | | - common_url parts; |
609 | | - auto scheme_end = url.find("://"); |
610 | | - |
611 | | - if (scheme_end == std::string::npos) { |
612 | | - throw std::runtime_error("invalid URL: no scheme"); |
613 | | - } |
614 | | - parts.scheme = url.substr(0, scheme_end); |
615 | | - |
616 | | - if (parts.scheme != "http" && parts.scheme != "https") { |
617 | | - throw std::runtime_error("unsupported URL scheme: " + parts.scheme); |
618 | | - } |
619 | | - |
620 | | - auto rest = url.substr(scheme_end + 3); |
621 | | - auto at_pos = rest.find('@'); |
622 | | - |
623 | | - if (at_pos != std::string::npos) { |
624 | | - auto auth = rest.substr(0, at_pos); |
625 | | - auto colon_pos = auth.find(':'); |
626 | | - if (colon_pos != std::string::npos) { |
627 | | - parts.user = auth.substr(0, colon_pos); |
628 | | - parts.password = auth.substr(colon_pos + 1); |
629 | | - } else { |
630 | | - parts.user = auth; |
631 | | - } |
632 | | - rest = rest.substr(at_pos + 1); |
633 | | - } |
634 | | - |
635 | | - auto slash_pos = rest.find('/'); |
636 | | - |
637 | | - if (slash_pos != std::string::npos) { |
638 | | - parts.host = rest.substr(0, slash_pos); |
639 | | - parts.path = rest.substr(slash_pos); |
640 | | - } else { |
641 | | - parts.host = rest; |
642 | | - parts.path = "/"; |
643 | | - } |
644 | | - return parts; |
645 | | -} |
646 | | - |
647 | | -static std::pair<httplib::Client, common_url> http_client(const std::string & url) { |
648 | | - common_url parts = parse_url(url); |
649 | | - |
650 | | - if (parts.host.empty()) { |
651 | | - throw std::runtime_error("error: invalid URL format"); |
652 | | - } |
653 | | - |
654 | | - if (!parts.user.empty()) { |
655 | | - throw std::runtime_error("error: user:password@ not supported yet"); // TODO |
656 | | - } |
657 | | - |
658 | | - httplib::Client cli(parts.scheme + "://" + parts.host); |
659 | | - cli.set_follow_location(true); |
660 | | - |
661 | | - // TODO cert |
662 | | - |
663 | | - return { std::move(cli), std::move(parts) }; |
664 | | -} |
665 | | - |
666 | | -static std::string show_masked_url(const common_url & parts) { |
667 | | - return parts.scheme + "://" + (parts.user.empty() ? "" : "****:****@") + parts.host + parts.path; |
668 | | -} |
669 | | - |
670 | 597 | static void print_progress(size_t current, size_t total) { |
671 | 598 | if (!is_output_a_tty()) { |
672 | 599 | return; |
@@ -759,7 +686,7 @@ static bool common_download_file_single_online(const std::string & url, |
759 | 686 | static const int max_attempts = 3; |
760 | 687 | static const int retry_delay_seconds = 2; |
761 | 688 |
|
762 | | - auto [cli, parts] = http_client(url); |
| 689 | + auto [cli, parts] = common_http_client(url); |
763 | 690 |
|
764 | 691 | httplib::Headers default_headers = {{"User-Agent", "llama-cpp"}}; |
765 | 692 | if (!bearer_token.empty()) { |
@@ -839,7 +766,7 @@ static bool common_download_file_single_online(const std::string & url, |
839 | 766 |
|
840 | 767 | // start the download |
841 | 768 | LOG_INF("%s: trying to download model from %s to %s (etag:%s)...\n", |
842 | | - __func__, show_masked_url(parts).c_str(), path_temporary.c_str(), etag.c_str()); |
| 769 | + __func__, common_http_show_masked_url(parts).c_str(), path_temporary.c_str(), etag.c_str()); |
843 | 770 | const bool was_pull_successful = common_pull_file(cli, parts.path, path_temporary, supports_ranges, existing_size, total_size); |
844 | 771 | if (!was_pull_successful) { |
845 | 772 | if (i + 1 < max_attempts) { |
@@ -867,7 +794,7 @@ static bool common_download_file_single_online(const std::string & url, |
867 | 794 |
|
868 | 795 | std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, |
869 | 796 | const common_remote_params & params) { |
870 | | - auto [cli, parts] = http_client(url); |
| 797 | + auto [cli, parts] = common_http_client(url); |
871 | 798 |
|
872 | 799 | httplib::Headers headers = {{"User-Agent", "llama-cpp"}}; |
873 | 800 | for (const auto & header : params.headers) { |
|
0 commit comments