Skip to content

Commit

Permalink
Add proxy setting interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
winsoft666 committed Dec 20, 2021
1 parent 7203725 commit 3084d48
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions include/teemo/teemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ class TEEMO_API Teemo {
Result setHttpHeaders(const HttpHeaders& headers) noexcept;
HttpHeaders httpHeaders() const noexcept;

// set proxy string, such as http://127.0.0.1:8888
//
Result setProxy(const utf8string& proxy) noexcept;
utf8string proxy() const noexcept;

// Start to download and state change to DOWNLOADING.
// Supported url protocol is the same as libcurl.
//
Expand Down
2 changes: 2 additions & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ typedef struct _Options {

HttpHeaders http_headers;

utf8string proxy;

_Options() : internal_stop_event(true) {
redirected_url_check_enabled = true;
content_md5_enabled = false;
Expand Down
24 changes: 16 additions & 8 deletions src/slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ static size_t __SliceWriteBodyCallback(char* buffer,
}

Result Slice::start(void* multi, int64_t disk_cache_size, int32_t max_speed) {
if (!slice_manager_)
return UNKNOWN_ERROR;

if (!slice_manager_->options())
return UNKNOWN_ERROR;

status_ = DOWNLOADING;

disk_cache_size_ = disk_cache_size;
Expand Down Expand Up @@ -136,6 +142,10 @@ Result Slice::start(void* multi, int64_t disk_cache_size, int32_t max_speed) {
curl_, CURLOPT_URL,
(redirect_url.length() > 0 ? redirect_url.c_str() : url.c_str()));

if (slice_manager_->options()->proxy.length() > 0) {
curl_easy_setopt(curl_, CURLOPT_PROXY, slice_manager_->options()->proxy.c_str());
}

curl_easy_setopt(curl_, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl_, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl_, CURLOPT_SSL_VERIFYHOST, 0L);
Expand Down Expand Up @@ -163,15 +173,13 @@ Result Slice::start(void* multi, int64_t disk_cache_size, int32_t max_speed) {
curl_easy_setopt(curl_, CURLOPT_WRITEFUNCTION, __SliceWriteBodyCallback);
curl_easy_setopt(curl_, CURLOPT_WRITEDATA, this);

if (slice_manager_->options()) {
const HttpHeaders& headers = slice_manager_->options()->http_headers;
if (headers.size() > 0) {
for (const auto& it : headers) {
utf8string headerStr = it.first + u8": " + it.second;
header_chunk_ = curl_slist_append(header_chunk_, headerStr.c_str());
}
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_chunk_);
const HttpHeaders& headers = slice_manager_->options()->http_headers;
if (headers.size() > 0) {
for (const auto& it : headers) {
utf8string headerStr = it.first + u8": " + it.second;
header_chunk_ = curl_slist_append(header_chunk_, headerStr.c_str());
}
curl_easy_setopt(curl_, CURLOPT_HTTPHEADER, header_chunk_);
}

if (end_ != -1) {
Expand Down
12 changes: 12 additions & 0 deletions src/teemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,18 @@ HttpHeaders Teemo::httpHeaders() const noexcept {
return impl_->options_.http_headers;
}

Result Teemo::setProxy(const utf8string& proxy) noexcept {
assert(impl_);
impl_->options_.proxy = proxy;

return SUCCESSED;
}

utf8string Teemo::proxy() const noexcept {
assert(impl_);
return impl_->options_.proxy;
}

std::shared_future<Result> Teemo::start(
const utf8string& url,
const utf8string& target_file_path,
Expand Down

0 comments on commit 3084d48

Please sign in to comment.