Skip to content

Commit

Permalink
Forward request User-Agent for download instead of using internal one
Browse files Browse the repository at this point in the history
Optimize performance.
  • Loading branch information
tindy2013 committed Aug 8, 2020
1 parent 2705b2a commit b705d84
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/webget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,9 @@ static int curlGet(const FetchArgument argument, FetchResult &result)
if(argument.request_headers)
{
for(auto &x : *argument.request_headers)
{
if(toLower(x.first) != "user-agent")
list = curl_slist_append(list, (x.first + ": " + x.second).data());
}
list = curl_slist_append(list, (x.first + ": " + x.second).data());
}
list = curl_slist_append(list, "SubConverter-Request: 1");
if(list)
curl_easy_setopt(curl_handle, CURLOPT_HTTPHEADER, list);

Expand Down
18 changes: 11 additions & 7 deletions src/webserver_libevent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct responseRoute
std::vector<responseRoute> responses;
string_map redirect_map;

const char *request_header_blacklist[] = {"host", "user-agent", "accept", "accept-encoding"};
const char *request_header_blacklist[] = {"host", "accept", "accept-encoding"};

static inline void buffer_cleanup(struct evbuffer *eb)
{
Expand Down Expand Up @@ -89,8 +89,8 @@ static inline int process_request(Request &request, Response &response, std::str
void OnReq(evhttp_request *req, void *args)
{
const char *req_content_type = evhttp_find_header(req->input_headers, "Content-Type"), *req_ac_method = evhttp_find_header(req->input_headers, "Access-Control-Request-Method");
const char *req_method = req_ac_method == NULL ? EVBUFFER_LENGTH(req->input_buffer) == 0 ? "GET" : "POST" : "OPTIONS", *uri = req->uri;
const char *user_agent = evhttp_find_header(req->input_headers, "User-Agent");
const char *uri = req->uri, *internal_flag = evhttp_find_header(req->input_headers, "SubConverter-Request");

char *client_ip;
u_short client_port;
evhttp_connection_get_peer(evhttp_request_get_connection(req), &client_ip, &client_port);
Expand All @@ -99,7 +99,7 @@ void OnReq(evhttp_request *req, void *args)
int retVal;
std::string postdata, content_type, return_data;

if(user_agent != NULL && user_agent_str.compare(user_agent) == 0)
if(internal_flag != NULL && strcmp(internal_flag, "1") == 0)
{
evhttp_send_error(req, 500, "Loop request detected!");
return;
Expand All @@ -118,7 +118,13 @@ void OnReq(evhttp_request *req, void *args)

Request request;
Response response;
request.method = req_method;
switch(req->type)
{
case EVHTTP_REQ_GET: request.method = "GET"; break;
case EVHTTP_REQ_POST: request.method = "POST"; break;
case EVHTTP_REQ_OPTIONS: request.method = "OPTIONS"; break;
default: break;
}
request.url = uri;

struct evkeyval* kv = req->input_headers->tqh_first;
Expand All @@ -128,8 +134,6 @@ void OnReq(evhttp_request *req, void *args)
request.headers.emplace(kv->key, kv->value);
kv = kv->next.tqe_next;
}
if(user_agent)
request.headers.emplace("X-User-Agent", user_agent);
request.headers.emplace("X-Client-IP", client_ip);

retVal = process_request(request, response, return_data);
Expand Down

0 comments on commit b705d84

Please sign in to comment.