-
Notifications
You must be signed in to change notification settings - Fork 29
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
CURL support via scripting. #160
Open
ohlidalp
wants to merge
8
commits into
RigsOfRods:master
Choose a base branch
from
ohlidalp:servercurl
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
40c5c0a
:construction: `server.fetchUrlAsStringAsync()` using CURL
ohlidalp 9482ee5
Build fix - deleted deadcode without `return`.
ohlidalp d88ec66
Fixed build with Angelscript but without CURL
ohlidalp 62d134f
FIXUP of CMakeLists/RORSERVER_WITH_CURL
ohlidalp d2ae748
CURL final touches
ohlidalp 152c1e0
example script: say "CURL test" in chat!
ohlidalp 6e78924
example script: fixed SUCCESS/FAILURE CURL logs.
ohlidalp 18013d6
example script: typo in docs - enum name
ohlidalp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
This source file is part of Rigs of Rods | ||
Copyright 2005-2012 Pierre-Michel Ricordel | ||
Copyright 2007-2012 Thomas Fischer | ||
Copyright 2013-2023 Petr Ohlidal | ||
|
||
For more information, see http://www.rigsofrods.org/ | ||
|
||
Rigs of Rods is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License version 3, as | ||
published by the Free Software Foundation. | ||
|
||
Rigs of Rods is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifdef WITH_CURL | ||
|
||
#include "CurlHelpers.h" | ||
#include "ScriptEngine.h" | ||
|
||
#include <curl/curl.h> | ||
#include <curl/easy.h> | ||
|
||
#include <string> | ||
|
||
static size_t CurlStringWriteFunc(void *ptr, size_t size, size_t nmemb, std::string* data) | ||
{ | ||
data->append((char*)ptr, size * nmemb); | ||
return size * nmemb; | ||
} | ||
|
||
static size_t CurlXferInfoFunc(void* ptr, curl_off_t filesize_B, curl_off_t downloaded_B, curl_off_t uploadsize_B, curl_off_t uploaded_B) | ||
{ | ||
CurlTaskContext* context = (CurlTaskContext*)ptr; | ||
|
||
context->ctc_script_engine->curlStatus( | ||
CURL_STATUS_PROGRESS, (int)downloaded_B, (int)filesize_B, context->ctc_displayname, ""); | ||
|
||
// If you don't return 0, the transfer will be aborted - see the documentation | ||
return 0; | ||
} | ||
|
||
bool GetUrlAsString(const std::string& url, CURLcode& curl_result, long& response_code, std::string& response_payload) | ||
{ | ||
std::string response_header; | ||
std::string user_agent = "Rigs of Rods Server"; | ||
|
||
CURL *curl = curl_easy_init(); | ||
curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); | ||
#ifdef _WIN32 | ||
curl_easy_setopt(curl, CURLOPT_SSL_OPTIONS, CURLSSLOPT_NATIVE_CA); | ||
#endif // _WIN32 | ||
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "gzip"); | ||
curl_easy_setopt(curl, CURLOPT_USERAGENT, user_agent.c_str()); | ||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlStringWriteFunc); | ||
curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, CurlXferInfoFunc); | ||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &response_payload); | ||
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &response_header); | ||
|
||
curl_result = curl_easy_perform(curl); | ||
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code); | ||
|
||
curl_easy_cleanup(curl); | ||
curl = nullptr; | ||
|
||
if (curl_result != CURLE_OK || response_code != 200) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool CurlRequestThreadFunc(CurlTaskContext context) | ||
{ | ||
context.ctc_script_engine->curlStatus(CURL_STATUS_START, 0, 0, context.ctc_displayname, ""); | ||
std::string data; | ||
CURLcode curl_result = CURLE_OK; | ||
long http_response = 0; | ||
bool result = GetUrlAsString(context.ctc_url, /*out:*/curl_result, /*out:*/http_response, /*out:*/data); | ||
CurlStatusType type = result ? CURL_STATUS_SUCCESS : CURL_STATUS_FAILURE; | ||
context.ctc_script_engine->curlStatus(type, (int)curl_result, (int)http_response, context.ctc_displayname, curl_easy_strerror(curl_result)); | ||
return result; | ||
} | ||
|
||
#endif // WITH_CURL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
This file is part of "Rigs of Rods Server" (Relay mode) | ||
|
||
Copyright 2007 Pierre-Michel Ricordel | ||
Copyright 2014+ Rigs of Rods Community | ||
|
||
"Rigs of Rods Server" is free software: you can redistribute it | ||
and/or modify it under the terms of the GNU General Public License | ||
as published by the Free Software Foundation, either version 3 | ||
of the License, or (at your option) any later version. | ||
|
||
"Rigs of Rods Server" is distributed in the hope that it will | ||
be useful, but WITHOUT ANY WARRANTY; without even the implied | ||
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
See the GNU General Public License for more details. | ||
|
||
You should have received a copy of the GNU General Public License | ||
along with Foobar. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
|
||
#pragma once | ||
|
||
enum CurlStatusType | ||
{ | ||
CURL_STATUS_INVALID, //!< Should never be reported. | ||
CURL_STATUS_START, //!< New CURL request started, n1/n2 both 0. | ||
CURL_STATUS_PROGRESS, //!< Download in progress, n1 = bytes downloaded, n2 = total bytes. | ||
CURL_STATUS_SUCCESS, //!< CURL request finished, n1 = CURL return code, n2 = HTTP result code, message = received payload. | ||
CURL_STATUS_FAILURE, //!< CURL request finished, n1 = CURL return code, n2 = HTTP result code, message = CURL error string. | ||
}; | ||
|
||
#ifdef WITH_CURL | ||
|
||
class ScriptEngine; | ||
|
||
#include <curl/curl.h> | ||
#include <curl/easy.h> | ||
|
||
#include <string> | ||
|
||
|
||
struct CurlTaskContext | ||
{ | ||
std::string ctc_displayname; | ||
std::string ctc_url; | ||
ScriptEngine* ctc_script_engine; | ||
// Status is reported via new server callback `curlStatus()` | ||
}; | ||
|
||
bool GetUrlAsString(const std::string& url, CURLcode& curl_result, long& response_code, std::string& response_payload); | ||
|
||
bool CurlRequestThreadFunc(CurlTaskContext task); | ||
|
||
|
||
|
||
#endif // WITH_CURL |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the cause of CURL not being found