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

Workflow支持通过Http代理访问Http(s)服务啦 #379

Closed
kedixa opened this issue May 20, 2021 · 0 comments
Closed

Workflow支持通过Http代理访问Http(s)服务啦 #379

kedixa opened this issue May 20, 2021 · 0 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@kedixa
Copy link
Contributor

kedixa commented May 20, 2021

PR: #378

Workflow支持通过Http代理访问Http(s)服务啦,使用方式与普通Http请求基本一致

std::string url = "https://www.sogou.com/";
std::string proxy = "http://user:[email protected]:port"; // proxy url here
WFHttpTask *task = WFTaskFactory::create_http_task(url, proxy, 3, 3, http_callback);
task->start();

如上述代码所示,仅需在创建Http任务时传入http proxy的url即可创建一个使用proxy的http任务

  1. 若proxy无需使用密码,则忽略user:passwd@即可
  2. 若需要进行重定向,则任务会自动处理且使用相同的proxy发起新的请求
  3. 若Connect proxy时被proxy拒绝(例如密码认证失败),用户会得到一个task->get_state() == WFT_STATE_TASK_ERROR && task->get_error() == WFT_ERR_HTTP_PROXY_CONNECT_FAILED的错误,此时proxy返回的内容被放在task->get_resp()

一个较为完整的示例如下:

#include <iostream>
#include <string>
#include "workflow/HttpUtil.h"
#include "workflow/WFTaskFactory.h"
#include "workflow/WFFacilities.h"

using namespace std;
using namespace protocol;

WFFacilities::WaitGroup wait_group(1);

void http_callback(WFHttpTask *task) {
    int state = task->get_state();
    int error = task->get_error();
    const char *errstr = WFGlobal::get_error_string(state, error);
    HttpResponse *resp = task->get_resp();

    if (state == WFT_STATE_TASK_ERROR &&
        error == WFT_ERR_HTTP_PROXY_CONNECT_FAILED) {
        printf("Connect to proxy return non 200\n\n");
        printf("%s %s %s\n", resp->get_http_version(),
            resp->get_status_code(), resp->get_reason_phrase()
        );
    }
    else if (state != WFT_STATE_SUCCESS) {
        printf("error: %s\n", errstr);
    }
    else {
        const void *body;
        size_t size = 0;
        resp->get_parsed_body(&body, &size);
        printf("Get response from remote server\n\n");
        printf("%s %s %s\n", resp->get_http_version(),
            resp->get_status_code(), resp->get_reason_phrase()
        );

        string name, value;
        HttpHeaderCursor resp_cursor(resp);
        while (resp_cursor.next(name, value))
            printf("%s: %s\n", name.c_str(), value.c_str());
        printf("\n");
    }

    wait_group.done();
}

int main() {
    string url = "https://www.sogou.com/";
    // TODO use a real proxy here
    std::string proxy = "http://user:[email protected]:port";
    WFHttpTask *task;

    task = WFTaskFactory::create_http_task(url, proxy, 3, 3, http_callback);
    task->start();

    wait_group.wait();
    return 0;
}
@Barenboim Barenboim added documentation Improvements or additions to documentation enhancement New feature or request labels May 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants