1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
#include <signal.h> #include <stdlib.h> #include <stdio.h> #include <utility> #include "workflow/Workflow.h" #include "workflow/HttpMessage.h" #include "workflow/HttpUtil.h" #include "workflow/WFHttpServer.h"
#ifndef _WIN32 #include <unistd.h> #endif
struct tutorial_series_context { std::string url; WFHttpTask *proxy_task; bool is_keep_alive; };
void reply_callback(WFHttpTask *proxy_task) { SeriesWork *series = series_of(proxy_task); tutorial_series_context *context = (tutorial_series_context *)series->get_context(); auto *proxy_resp = proxy_task->get_resp(); size_t size = proxy_resp->get_output_body_size();
if (proxy_task->get_state() == WFT_STATE_SUCCESS) { fprintf(stderr, "%s: Success. Http Status: %s, BodyLength: %zu\n", context->url.c_str(), proxy_resp->get_status_code(), size); } else { fprintf(stderr, "%s: Reply failed: %s, BodyLength: %zu\n", context->url.c_str(), strerror(proxy_task->get_error()), size); } }
void http_callback(WFHttpTask *task) { int state = task->get_state(); int error = task->get_error(); auto *resp = task->get_resp(); SeriesWork *series = series_of(task); tutorial_series_context *context = (tutorial_series_context *)series->get_context(); auto *proxy_resp = context->proxy_task->get_resp();
if (state == WFT_STATE_SYS_ERROR && error == ECONNRESET) state = WFT_STATE_SUCCESS;
if (state == WFT_STATE_SUCCESS) { const void *body; size_t len;
context->proxy_task->set_callback(reply_callback);
if (resp->get_parsed_body(&body, &len)) resp->append_output_body_nocopy(body, len); *proxy_resp = std::move(*resp); if (!context->is_keep_alive) proxy_resp->set_header_pair("Connection", "close"); } else { const char *err_string; int error = task->get_error();
if (state == WFT_STATE_SYS_ERROR) err_string = strerror(error); else if (state == WFT_STATE_DNS_ERROR) err_string = gai_strerror(error); else if (state == WFT_STATE_SSL_ERROR) err_string = "SSL error"; else err_string = "URL error (Cannot be a HTTPS proxy)";
fprintf(stderr, "%s: Fetch failed. state = %d, error = %d: %s\n", context->url.c_str(), state, task->get_error(), err_string);
proxy_resp->set_status_code("404"); proxy_resp->append_output_body_nocopy( "<html>404 Not Found.</html>", 27); } }
void process(WFHttpTask *proxy_task) { auto *req = proxy_task->get_req(); SeriesWork *series = series_of(proxy_task); WFHttpTask *http_task;
tutorial_series_context *context = new tutorial_series_context; context->url = req->get_request_uri(); context->proxy_task = proxy_task;
series->set_context(context); series->set_callback([](const SeriesWork *series) { delete (tutorial_series_context *)series->get_context(); }); std::string requesturi = req->get_request_uri(); protocol::HttpHeaderCursor header_cursor(req); std::string host; if (header_cursor.find("Host", host)) { requesturi = "http://" + host + requesturi; } printf("uri: %s\r\n", requesturi.c_str());
context->is_keep_alive = req->is_keep_alive(); http_task = WFTaskFactory::create_http_task(requesturi.c_str(), 0, 0, http_callback);
const void *body; size_t len;
req->set_request_uri(http_task->get_req()->get_request_uri()); req->get_parsed_body(&body, &len); req->append_output_body_nocopy(body, len); *http_task->get_req() = std::move(*req);
http_task->get_resp()->set_size_limit(200 * 1024 * 1024);
*series << http_task; }
void sig_handler(int signo) { }
int main(int argc, char *argv[]) { unsigned short port;
if (argc != 2) { fprintf(stderr, "USAGE: %s <port>\n", argv[0]); exit(1); }
port = atoi(argv[1]); signal(SIGINT, sig_handler);
struct WFServerParams params = HTTP_SERVER_PARAMS_DEFAULT; params.request_size_limit = 8 * 1024 * 1024;
WFHttpServer server(¶ms, process);
if (server.start(port) == 0) { #ifndef _WIN32 pause(); #else getchar(); #endif server.stop(); } else { perror("Cannot start server"); exit(1); }
return 0; }
|