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

Add native System2HTTPRequest.SetProxy #5

Merged
merged 1 commit into from
May 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion natives/HTTPRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ HTTPRequest::HTTPRequest(std::string url, std::shared_ptr<CallbackFunction_t> re

HTTPRequest::HTTPRequest(const HTTPRequest &request) :
Request(request), bodyData(request.bodyData), headers(request.headers), userAgent(request.userAgent),
username(request.username), password(request.password), followRedirects(request.followRedirects) {}
username(request.username), password(request.password), proxy(request.proxy), proxyAuth(request.proxyAuth), followRedirects(request.followRedirects) {}


HTTPRequest *HTTPRequest::Clone() const {
Expand Down
2 changes: 2 additions & 0 deletions natives/HTTPRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class HTTPRequest : public Request {
std::string userAgent;
std::string username;
std::string password;
std::string proxy;
std::string proxyAuth;
bool followRedirects;

HTTPRequest(std::string url, std::shared_ptr<CallbackFunction_t> responseCallbackFunction);
Expand Down
2 changes: 2 additions & 0 deletions natives/Natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ cell_t NativeHTTPRequest_DELETE(IPluginContext *pContext, const cell_t *params);
cell_t NativeHTTPRequest_HEAD(IPluginContext *pContext, const cell_t *params);
cell_t NativeHTTPRequest_GetFollowRedirects(IPluginContext *pContext, const cell_t *params);
cell_t NativeHTTPRequest_SetFollowRedirects(IPluginContext *pContext, const cell_t *params);
cell_t NativeHTTPRequest_SetProxy(IPluginContext *pContext, const cell_t *params);

cell_t NativeFTPRequest_FTPRequest(IPluginContext *pContext, const cell_t *params);
cell_t NativeFTPRequest_SetProgressCallback(IPluginContext *pContext, const cell_t *params);
Expand Down Expand Up @@ -148,6 +149,7 @@ const sp_nativeinfo_t system2_natives[] =
{ "System2HTTPRequest.FollowRedirects.get", NativeHTTPRequest_GetFollowRedirects },
{ "System2HTTPRequest.FollowRedirects.set", NativeHTTPRequest_SetFollowRedirects },
{ "System2HTTPRequest.Headers.get", NativeHTTPRequest_GetHeaders },
{ "System2HTTPRequest.SetProxy", NativeHTTPRequest_SetProxy },

{ "System2FTPRequest.System2FTPRequest", NativeFTPRequest_FTPRequest },
{ "System2FTPRequest.SetProgressCallback", NativeFTPRequest_SetProgressCallback },
Expand Down
17 changes: 16 additions & 1 deletion natives/RequestNatives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,22 @@ cell_t NativeHTTPRequest_SetBasicAuthentication(IPluginContext *pContext, const
return 1;
}

cell_t NativeHTTPRequest_SetProxy(IPluginContext *pContext, const cell_t *params) {
HTTPRequest *request = Request::ConvertRequest<HTTPRequest>(params[1], pContext);
if (request == NULL) {
return 0;
}

char *proxy;
char *userpswd;
pContext->LocalToString(params[2], &proxy);
pContext->LocalToString(params[3], &userpswd);

request->proxy = proxy;
request->proxyAuth = userpswd;
return 1;
}

cell_t NativeHTTPRequest_GET(IPluginContext *pContext, const cell_t *params) {
HTTPRequest *request = Request::ConvertRequest<HTTPRequest>(params[1], pContext);
if (request == NULL) {
Expand Down Expand Up @@ -415,7 +431,6 @@ cell_t NativeHTTPRequest_SetFollowRedirects(IPluginContext *pContext, const cell
return 1;
}


cell_t NativeFTPRequest_FTPRequest(IPluginContext *pContext, const cell_t *params) {
auto callback = system2Extension.CreateCallbackFunction(pContext->GetFunctionById(params[1]));
if (!callback) {
Expand Down
1 change: 1 addition & 0 deletions sourcemod/scripting/include/system2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ public Extension __ext_system2 =
MarkNativeAsOptional("System2HTTPRequest.GetHeaderName");
MarkNativeAsOptional("System2HTTPRequest.SetUserAgent");
MarkNativeAsOptional("System2HTTPRequest.SetBasicAuthentication");
MarkNativeAsOptional("System2HTTPRequest.SetProxy");
MarkNativeAsOptional("System2HTTPRequest.GET");
MarkNativeAsOptional("System2HTTPRequest.POST");
MarkNativeAsOptional("System2HTTPRequest.PUT");
Expand Down
10 changes: 10 additions & 0 deletions sourcemod/scripting/include/system2/request.inc
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,16 @@ methodmap System2HTTPRequest < System2Request {
*/
public native void SetBasicAuthentication(const char[] username, const char[] password);

/**
* Sets proxy (authentication) for the request.
*
* @param proxy proxy to use for HTTP request.
* @param userpwd authentication for proxy.
*
* @noreturn
* @error Invalid request.
*/
public native void SetProxy(const char[] proxy, const char[] userpwd);

/**
* Sends the request with the HTTP GET method.
Expand Down
11 changes: 11 additions & 0 deletions threads/HTTPRequestThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ void HTTPRequestThread::RunThread(IThreadHandle *pHandle) {
curl_easy_setopt(curl, CURLOPT_PASSWORD, this->httpRequest->password.c_str());
}

// Set the proxy
if (!this->httpRequest->proxy.empty()) {
curl_easy_setopt(curl, CURLOPT_PROXY, this->httpRequest->proxy.c_str());
//curl_easy_setopt(curl, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}

// Set the user and password of proxy
if (!this->httpRequest->proxyAuth.empty()) {
curl_easy_setopt(curl, CURLOPT_PROXYUSERPWD, this->httpRequest->proxyAuth.c_str());
}

// Set the follow redirect property
if (this->httpRequest->followRedirects) {
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
Expand Down