|
34 | 34 | #include <thread> |
35 | 35 | #include <vector> |
36 | 36 |
|
37 | | -//#define LLAMA_USE_CURL |
38 | | - |
39 | 37 | #if defined(LLAMA_USE_CURL) |
40 | 38 | #include <curl/curl.h> |
41 | 39 | #include <curl/easy.h> |
42 | 40 | #elif defined(LLAMA_USE_HTTPLIB) |
43 | | -#include <cpp-httplib/httplib.h> |
| 41 | +#include "http.h" |
44 | 42 | #endif |
45 | 43 |
|
46 | 44 | #ifdef __linux__ |
|
56 | 54 | #endif |
57 | 55 | #define LLAMA_MAX_URL_LENGTH 2084 // Maximum URL Length in Chrome: 2083 |
58 | 56 |
|
| 57 | +// isatty |
| 58 | +#if defined(_WIN32) |
| 59 | +#include <io.h> |
| 60 | +#else |
| 61 | +#include <unistd.h> |
| 62 | +#endif |
| 63 | + |
59 | 64 | using json = nlohmann::ordered_json; |
60 | 65 |
|
61 | 66 | std::initializer_list<enum llama_example> mmproj_examples = { |
@@ -102,6 +107,14 @@ static void write_file(const std::string & fname, const std::string & content) { |
102 | 107 | } |
103 | 108 | } |
104 | 109 |
|
| 110 | +static bool is_output_a_tty() { |
| 111 | +#if defined(_WIN32) |
| 112 | + return _isatty(_fileno(stdout)); |
| 113 | +#else |
| 114 | + return isatty(1); |
| 115 | +#endif |
| 116 | +} |
| 117 | + |
105 | 118 | common_arg & common_arg::set_examples(std::initializer_list<enum llama_example> examples) { |
106 | 119 | this->examples = std::move(examples); |
107 | 120 | return *this; |
@@ -268,10 +281,6 @@ static std::string read_etag(const std::string & path) { |
268 | 281 |
|
269 | 282 | #ifdef LLAMA_USE_CURL |
270 | 283 |
|
271 | | -bool common_has_curl() { |
272 | | - return true; |
273 | | -} |
274 | | - |
275 | 284 | // |
276 | 285 | // CURL utils |
277 | 286 | // |
@@ -588,83 +597,11 @@ std::pair<long, std::vector<char>> common_remote_get_content(const std::string & |
588 | 597 | #else |
589 | 598 |
|
590 | 599 | #ifdef LLAMA_USE_HTTPLIB |
591 | | - |
592 | | -bool common_has_curl() { |
593 | | - return false; |
594 | | -} |
595 | | - |
596 | | -struct common_url { |
597 | | - std::string scheme; |
598 | | - std::string user; |
599 | | - std::string password; |
600 | | - std::string host; |
601 | | - std::string path; |
602 | | -}; |
603 | | - |
604 | | -static common_url parse_url(const std::string & url) { |
605 | | - common_url parts; |
606 | | - auto scheme_end = url.find("://"); |
607 | | - |
608 | | - if (scheme_end == std::string::npos) { |
609 | | - throw std::runtime_error("invalid URL: no scheme"); |
610 | | - } |
611 | | - parts.scheme = url.substr(0, scheme_end); |
612 | | - |
613 | | - if (parts.scheme != "http" && parts.scheme != "https") { |
614 | | - throw std::runtime_error("unsupported URL scheme: " + parts.scheme); |
615 | | - } |
616 | | - |
617 | | - auto rest = url.substr(scheme_end + 3); |
618 | | - auto at_pos = rest.find('@'); |
619 | | - |
620 | | - if (at_pos != std::string::npos) { |
621 | | - auto auth = rest.substr(0, at_pos); |
622 | | - auto colon_pos = auth.find(':'); |
623 | | - if (colon_pos != std::string::npos) { |
624 | | - parts.user = auth.substr(0, colon_pos); |
625 | | - parts.password = auth.substr(colon_pos + 1); |
626 | | - } else { |
627 | | - parts.user = auth; |
628 | | - } |
629 | | - rest = rest.substr(at_pos + 1); |
630 | | - } |
631 | | - |
632 | | - auto slash_pos = rest.find('/'); |
633 | | - |
634 | | - if (slash_pos != std::string::npos) { |
635 | | - parts.host = rest.substr(0, slash_pos); |
636 | | - parts.path = rest.substr(slash_pos); |
637 | | - } else { |
638 | | - parts.host = rest; |
639 | | - parts.path = "/"; |
640 | | - } |
641 | | - return parts; |
642 | | -} |
643 | | - |
644 | | -static std::pair<httplib::Client, common_url> http_client(const std::string & url) { |
645 | | - common_url parts = parse_url(url); |
646 | | - |
647 | | - if (parts.host.empty()) { |
648 | | - throw std::runtime_error("error: invalid URL format"); |
649 | | - } |
650 | | - |
651 | | - if (!parts.user.empty()) { |
652 | | - throw std::runtime_error("error: user:password@ not supported yet"); // TODO |
| 600 | +static void print_progress(size_t current, size_t total) { |
| 601 | + if (!is_output_a_tty()) { |
| 602 | + return; |
653 | 603 | } |
654 | 604 |
|
655 | | - httplib::Client cli(parts.scheme + "://" + parts.host); |
656 | | - cli.set_follow_location(true); |
657 | | - |
658 | | - // TODO cert |
659 | | - |
660 | | - return { std::move(cli), std::move(parts) }; |
661 | | -} |
662 | | - |
663 | | -static std::string show_masked_url(const common_url & parts) { |
664 | | - return parts.scheme + "://" + (parts.user.empty() ? "" : "****:****@") + parts.host + parts.path; |
665 | | -} |
666 | | - |
667 | | -static void print_progress(size_t current, size_t total) { // TODO isatty |
668 | 605 | if (!total) { |
669 | 606 | return; |
670 | 607 | } |
@@ -752,7 +689,7 @@ static bool common_download_file_single_online(const std::string & url, |
752 | 689 | static const int max_attempts = 3; |
753 | 690 | static const int retry_delay_seconds = 2; |
754 | 691 |
|
755 | | - auto [cli, parts] = http_client(url); |
| 692 | + auto [cli, parts] = common_http_client(url); |
756 | 693 |
|
757 | 694 | httplib::Headers default_headers = {{"User-Agent", "llama-cpp"}}; |
758 | 695 | if (!bearer_token.empty()) { |
@@ -832,7 +769,7 @@ static bool common_download_file_single_online(const std::string & url, |
832 | 769 |
|
833 | 770 | // start the download |
834 | 771 | LOG_INF("%s: trying to download model from %s to %s (etag:%s)...\n", |
835 | | - __func__, show_masked_url(parts).c_str(), path_temporary.c_str(), etag.c_str()); |
| 772 | + __func__, common_http_show_masked_url(parts).c_str(), path_temporary.c_str(), etag.c_str()); |
836 | 773 | const bool was_pull_successful = common_pull_file(cli, parts.path, path_temporary, supports_ranges, existing_size, total_size); |
837 | 774 | if (!was_pull_successful) { |
838 | 775 | if (i + 1 < max_attempts) { |
@@ -860,7 +797,7 @@ static bool common_download_file_single_online(const std::string & url, |
860 | 797 |
|
861 | 798 | std::pair<long, std::vector<char>> common_remote_get_content(const std::string & url, |
862 | 799 | const common_remote_params & params) { |
863 | | - auto [cli, parts] = http_client(url); |
| 800 | + auto [cli, parts] = common_http_client(url); |
864 | 801 |
|
865 | 802 | httplib::Headers headers = {{"User-Agent", "llama-cpp"}}; |
866 | 803 | for (const auto & header : params.headers) { |
|
0 commit comments