Move WebServices to use LibreSSL + cpp-httplib (#3501)
Move WebServices to use LibreSSL + cpp-httplib Remove curl + openssl build dependencies
This commit is contained in:
parent
e2c5666883
commit
9283053701
18 changed files with 2633 additions and 150 deletions
6
.gitmodules
vendored
6
.gitmodules
vendored
|
@ -25,9 +25,9 @@
|
||||||
[submodule "enet"]
|
[submodule "enet"]
|
||||||
path = externals/enet
|
path = externals/enet
|
||||||
url = https://github.com/lsalzman/enet.git
|
url = https://github.com/lsalzman/enet.git
|
||||||
[submodule "cpr"]
|
|
||||||
path = externals/cpr
|
|
||||||
url = https://github.com/whoshuu/cpr.git
|
|
||||||
[submodule "inih"]
|
[submodule "inih"]
|
||||||
path = externals/inih/inih
|
path = externals/inih/inih
|
||||||
url = https://github.com/benhoyt/inih.git
|
url = https://github.com/benhoyt/inih.git
|
||||||
|
[submodule "libressl"]
|
||||||
|
path = externals/libressl
|
||||||
|
url = https://github.com/citra-emu/ext-libressl-portable.git
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
cd /citra
|
cd /citra
|
||||||
|
|
||||||
apt-get update
|
apt-get update
|
||||||
apt-get install -y build-essential wget git python-launchpadlib libssl-dev
|
apt-get install -y build-essential wget git python-launchpadlib
|
||||||
|
|
||||||
# Install specific versions of packages with their dependencies
|
# Install specific versions of packages with their dependencies
|
||||||
# The apt repositories remove older versions regularly, so we can't use
|
# The apt repositories remove older versions regularly, so we can't use
|
||||||
|
@ -12,7 +12,6 @@ apt-get install -y build-essential wget git python-launchpadlib libssl-dev
|
||||||
libsdl2-dev 2.0.7+dfsg1-3ubuntu1 bionic \
|
libsdl2-dev 2.0.7+dfsg1-3ubuntu1 bionic \
|
||||||
qtbase5-dev 5.9.3+dfsg-0ubuntu2 bionic \
|
qtbase5-dev 5.9.3+dfsg-0ubuntu2 bionic \
|
||||||
libqt5opengl5-dev 5.9.3+dfsg-0ubuntu2 bionic \
|
libqt5opengl5-dev 5.9.3+dfsg-0ubuntu2 bionic \
|
||||||
libcurl4-openssl-dev 7.58.0-2ubuntu1 bionic \
|
|
||||||
libicu57 57.1-6ubuntu0.2 bionic
|
libicu57 57.1-6ubuntu0.2 bionic
|
||||||
|
|
||||||
# Get a recent version of CMake
|
# Get a recent version of CMake
|
||||||
|
@ -21,7 +20,7 @@ echo y | sh cmake-3.10.1-Linux-x86_64.sh --prefix=cmake
|
||||||
export PATH=/citra/cmake/cmake-3.10.1-Linux-x86_64/bin:$PATH
|
export PATH=/citra/cmake/cmake-3.10.1-Linux-x86_64/bin:$PATH
|
||||||
|
|
||||||
mkdir build && cd build
|
mkdir build && cd build
|
||||||
cmake .. -DUSE_SYSTEM_CURL=ON -DCMAKE_BUILD_TYPE=Release -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
cmake .. -DCMAKE_BUILD_TYPE=Release -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
||||||
make -j4
|
make -j4
|
||||||
|
|
||||||
ctest -VV -C Release
|
ctest -VV -C Release
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
cd /citra
|
cd /citra
|
||||||
|
|
||||||
apt-get update
|
apt-get update
|
||||||
apt-get install -y build-essential libsdl2-dev qtbase5-dev libqt5opengl5-dev qttools5-dev qttools5-dev-tools libcurl4-openssl-dev libssl-dev wget git
|
apt-get install -y build-essential libsdl2-dev qtbase5-dev libqt5opengl5-dev qttools5-dev qttools5-dev-tools wget git
|
||||||
|
|
||||||
# Get a recent version of CMake
|
# Get a recent version of CMake
|
||||||
wget https://cmake.org/files/v3.10/cmake-3.10.1-Linux-x86_64.sh
|
wget https://cmake.org/files/v3.10/cmake-3.10.1-Linux-x86_64.sh
|
||||||
|
@ -11,7 +11,7 @@ echo y | sh cmake-3.10.1-Linux-x86_64.sh --prefix=cmake
|
||||||
export PATH=/citra/cmake/cmake-3.10.1-Linux-x86_64/bin:$PATH
|
export PATH=/citra/cmake/cmake-3.10.1-Linux-x86_64/bin:$PATH
|
||||||
|
|
||||||
mkdir build && cd build
|
mkdir build && cd build
|
||||||
cmake .. -DUSE_SYSTEM_CURL=ON -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
cmake .. -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
||||||
make -j4
|
make -j4
|
||||||
|
|
||||||
ctest -VV -C Release
|
ctest -VV -C Release
|
||||||
|
|
|
@ -6,7 +6,7 @@ export MACOSX_DEPLOYMENT_TARGET=10.12
|
||||||
export Qt5_DIR=$(brew --prefix)/opt/qt5
|
export Qt5_DIR=$(brew --prefix)/opt/qt5
|
||||||
|
|
||||||
mkdir build && cd build
|
mkdir build && cd build
|
||||||
cmake .. -DUSE_SYSTEM_CURL=ON -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h" -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
cmake .. -DCMAKE_OSX_ARCHITECTURES="x86_64;x86_64h" -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${ENABLE_COMPATIBILITY_REPORTING:-"OFF"}
|
||||||
make -j4
|
make -j4
|
||||||
|
|
||||||
ctest -VV -C Release
|
ctest -VV -C Release
|
||||||
|
|
|
@ -17,16 +17,6 @@ option(ENABLE_QT_TRANSLATION "Enable translations for the Qt frontend" OFF)
|
||||||
CMAKE_DEPENDENT_OPTION(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" ON "ENABLE_QT;MSVC" OFF)
|
CMAKE_DEPENDENT_OPTION(CITRA_USE_BUNDLED_QT "Download bundled Qt binaries" ON "ENABLE_QT;MSVC" OFF)
|
||||||
|
|
||||||
option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
|
option(ENABLE_WEB_SERVICE "Enable web services (telemetry, etc.)" ON)
|
||||||
option(CITRA_USE_BUNDLED_CURL "FOR MINGW ONLY: Download curl configured against winssl instead of openssl" OFF)
|
|
||||||
|
|
||||||
if (ENABLE_WEB_SERVICE AND CITRA_USE_BUNDLED_CURL AND WINDOWS AND MSVC)
|
|
||||||
message("Turning off use bundled curl as msvc can compile curl on cpr")
|
|
||||||
SET(CITRA_USE_BUNDLED_CURL OFF CACHE BOOL "" FORCE)
|
|
||||||
endif()
|
|
||||||
if (ENABLE_WEB_SERVICE AND NOT CITRA_USE_BUNDLED_CURL AND MINGW)
|
|
||||||
message(AUTHOR_WARNING "Turning on CITRA_USE_BUNDLED_CURL. Override it only if you know what you are doing.")
|
|
||||||
SET(CITRA_USE_BUNDLED_CURL ON CACHE BOOL "" FORCE)
|
|
||||||
endif()
|
|
||||||
|
|
||||||
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
|
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/.git/hooks/pre-commit)
|
||||||
message(STATUS "Copying pre-commit hook")
|
message(STATUS "Copying pre-commit hook")
|
||||||
|
|
11
appveyor.yml
11
appveyor.yml
|
@ -26,8 +26,7 @@ install:
|
||||||
- git submodule update --init --recursive
|
- git submodule update --init --recursive
|
||||||
- ps: |
|
- ps: |
|
||||||
if ($env:BUILD_TYPE -eq 'mingw') {
|
if ($env:BUILD_TYPE -eq 'mingw') {
|
||||||
$dependencies = "mingw64/mingw-w64-x86_64-qt5",
|
$dependencies = "mingw64/mingw-w64-x86_64-qt5"
|
||||||
"mingw64/mingw-w64-x86_64-curl"
|
|
||||||
# redirect err to null to prevent warnings from becoming errors
|
# redirect err to null to prevent warnings from becoming errors
|
||||||
# workaround to prevent pacman from failing due to cyclical dependencies
|
# workaround to prevent pacman from failing due to cyclical dependencies
|
||||||
C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw64/mingw-w64-x86_64-freetype mingw64/mingw-w64-x86_64-fontconfig" 2> $null
|
C:\msys64\usr\bin\bash -lc "pacman --noconfirm -S mingw64/mingw-w64-x86_64-freetype mingw64/mingw-w64-x86_64-fontconfig" 2> $null
|
||||||
|
@ -44,9 +43,9 @@ before_build:
|
||||||
$COMPAT = if ($env:ENABLE_COMPATIBILITY_REPORTING -eq $null) {0} else {$env:ENABLE_COMPATIBILITY_REPORTING}
|
$COMPAT = if ($env:ENABLE_COMPATIBILITY_REPORTING -eq $null) {0} else {$env:ENABLE_COMPATIBILITY_REPORTING}
|
||||||
if ($env:BUILD_TYPE -eq 'msvc') {
|
if ($env:BUILD_TYPE -eq 'msvc') {
|
||||||
# redirect stderr and change the exit code to prevent powershell from cancelling the build if cmake prints a warning
|
# redirect stderr and change the exit code to prevent powershell from cancelling the build if cmake prints a warning
|
||||||
cmd /C 'cmake -G "Visual Studio 15 2017 Win64" -DCITRA_USE_BUNDLED_QT=1 -DCITRA_USE_BUNDLED_SDL2=1 -DCMAKE_USE_OPENSSL=0 -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${COMPAT} .. 2>&1 && exit 0'
|
cmd /C 'cmake -G "Visual Studio 15 2017 Win64" -DCITRA_USE_BUNDLED_QT=1 -DCITRA_USE_BUNDLED_SDL2=1 -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${COMPAT} .. 2>&1 && exit 0'
|
||||||
} else {
|
} else {
|
||||||
C:\msys64\usr\bin\bash.exe -lc "cmake -G 'MSYS Makefiles' -DUSE_SYSTEM_CURL=1 -DCITRA_USE_BUNDLED_CURL=1 -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${COMPAT} .. 2>&1"
|
C:\msys64\usr\bin\bash.exe -lc "cmake -G 'MSYS Makefiles' -DCMAKE_BUILD_TYPE=Release -DENABLE_QT_TRANSLATION=ON -DCITRA_ENABLE_COMPATIBILITY_REPORTING=${COMPAT} .. 2>&1"
|
||||||
}
|
}
|
||||||
- cd ..
|
- cd ..
|
||||||
|
|
||||||
|
@ -114,14 +113,12 @@ after_build:
|
||||||
|
|
||||||
# copy the compiled binaries and other release files to the release folder
|
# copy the compiled binaries and other release files to the release folder
|
||||||
Get-ChildItem "$CMAKE_BINARY_DIR" -Recurse -Filter "citra*.exe" | Copy-Item -destination $RELEASE_DIST
|
Get-ChildItem "$CMAKE_BINARY_DIR" -Recurse -Filter "citra*.exe" | Copy-Item -destination $RELEASE_DIST
|
||||||
# copy the libcurl dll
|
|
||||||
Get-ChildItem "$CMAKE_BINARY_DIR" -Recurse -Filter "libcurl.dll" | Copy-Item -destination $RELEASE_DIST
|
|
||||||
Copy-Item -path "$CMAKE_SOURCE_DIR/license.txt" -destination $RELEASE_DIST
|
Copy-Item -path "$CMAKE_SOURCE_DIR/license.txt" -destination $RELEASE_DIST
|
||||||
Copy-Item -path "$CMAKE_SOURCE_DIR/README.md" -destination $RELEASE_DIST
|
Copy-Item -path "$CMAKE_SOURCE_DIR/README.md" -destination $RELEASE_DIST
|
||||||
|
|
||||||
# copy all the dll dependencies to the release folder
|
# copy all the dll dependencies to the release folder
|
||||||
. "./.appveyor/UtilityFunctions.ps1"
|
. "./.appveyor/UtilityFunctions.ps1"
|
||||||
$DLLSearchPath = "$CMAKE_BINARY_DIR\externals\curl-7_55_1\lib;C:\msys64\mingw64\bin;$env:PATH"
|
$DLLSearchPath = "C:\msys64\mingw64\bin;$env:PATH"
|
||||||
$MingwDLLs = RecursivelyGetDeps $DLLSearchPath "$RELEASE_DIST\citra.exe"
|
$MingwDLLs = RecursivelyGetDeps $DLLSearchPath "$RELEASE_DIST\citra.exe"
|
||||||
$MingwDLLs += RecursivelyGetDeps $DLLSearchPath "$RELEASE_DIST\citra-qt.exe"
|
$MingwDLLs += RecursivelyGetDeps $DLLSearchPath "$RELEASE_DIST\citra-qt.exe"
|
||||||
Write-Host "Detected the following dependencies:"
|
Write-Host "Detected the following dependencies:"
|
||||||
|
|
29
externals/CMakeLists.txt
vendored
29
externals/CMakeLists.txt
vendored
|
@ -60,23 +60,18 @@ add_subdirectory(enet)
|
||||||
target_include_directories(enet INTERFACE ./enet/include)
|
target_include_directories(enet INTERFACE ./enet/include)
|
||||||
|
|
||||||
if (ENABLE_WEB_SERVICE)
|
if (ENABLE_WEB_SERVICE)
|
||||||
# msys installed curl is configured to use openssl, but that isn't portable
|
# LibreSSL
|
||||||
# since it relies on having the bundled certs install in the home folder for SSL
|
set(LIBRESSL_SKIP_INSTALL ON CACHE BOOL "")
|
||||||
# by default on mingw, download the precompiled curl thats linked against windows native ssl
|
add_definitions(-DHAVE_INET_NTOP)
|
||||||
if (MINGW AND CITRA_USE_BUNDLED_CURL)
|
add_subdirectory(libressl)
|
||||||
download_bundled_external("curl/" "curl-7_55_1" CURL_PREFIX)
|
target_include_directories(ssl INTERFACE ./libressl/include)
|
||||||
set(CURL_PREFIX "${CMAKE_BINARY_DIR}/externals/curl-7_55_1")
|
|
||||||
set(CURL_FOUND YES)
|
# lurlparser
|
||||||
set(CURL_INCLUDE_DIR "${CURL_PREFIX}/include" CACHE PATH "Path to curl headers")
|
add_subdirectory(lurlparser)
|
||||||
set(CURL_LIBRARY "${CURL_PREFIX}/lib/libcurldll.a" CACHE PATH "Path to curl library")
|
|
||||||
set(CURL_DLL_DIR "${CURL_PREFIX}/lib/" CACHE PATH "Path to curl.dll")
|
# httplib
|
||||||
set(USE_SYSTEM_CURL ON CACHE BOOL "")
|
add_library(httplib INTERFACE)
|
||||||
endif()
|
target_include_directories(httplib INTERFACE ./httplib)
|
||||||
# CPR
|
|
||||||
set(BUILD_TESTING OFF CACHE BOOL "")
|
|
||||||
set(BUILD_CPR_TESTS OFF CACHE BOOL "")
|
|
||||||
add_subdirectory(cpr)
|
|
||||||
target_include_directories(cpr INTERFACE ./cpr/include)
|
|
||||||
|
|
||||||
# JSON
|
# JSON
|
||||||
add_library(json-headers INTERFACE)
|
add_library(json-headers INTERFACE)
|
||||||
|
|
1
externals/cpr
vendored
1
externals/cpr
vendored
|
@ -1 +0,0 @@
|
||||||
Subproject commit b5758fbc88021437f968fe5174f121b8b92f5d5c
|
|
15
externals/httplib/README.md
vendored
Normal file
15
externals/httplib/README.md
vendored
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
From https://github.com/yhirose/cpp-httplib/commit/25aa0b34c3c43ad51fc60c09e2e420c4ebda75cd
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
===
|
||||||
|
|
||||||
|
cpp-httplib
|
||||||
|
|
||||||
|
A C++11 header-only HTTP library.
|
||||||
|
|
||||||
|
It's extremely easy to setup. Just include httplib.h file in your code!
|
||||||
|
|
||||||
|
Inspired by Sinatra and express.
|
||||||
|
|
||||||
|
© 2017 Yuji Hirose
|
2041
externals/httplib/httplib.h
vendored
Normal file
2041
externals/httplib/httplib.h
vendored
Normal file
File diff suppressed because it is too large
Load diff
1
externals/libressl
vendored
Submodule
1
externals/libressl
vendored
Submodule
|
@ -0,0 +1 @@
|
||||||
|
Subproject commit 49f073a705ac0d0f5593fb2e5a6f081a08106e20
|
8
externals/lurlparser/CMakeLists.txt
vendored
Normal file
8
externals/lurlparser/CMakeLists.txt
vendored
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
add_library(lurlparser
|
||||||
|
LUrlParser.cpp
|
||||||
|
LUrlParser.h
|
||||||
|
)
|
||||||
|
|
||||||
|
create_target_directory_groups(lurlparser)
|
||||||
|
|
||||||
|
target_include_directories(lurlparser INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
|
265
externals/lurlparser/LUrlParser.cpp
vendored
Normal file
265
externals/lurlparser/LUrlParser.cpp
vendored
Normal file
|
@ -0,0 +1,265 @@
|
||||||
|
/*
|
||||||
|
* Lightweight URL & URI parser (RFC 1738, RFC 3986)
|
||||||
|
* https://github.com/corporateshark/LUrlParser
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "LUrlParser.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cstring>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// check if the scheme name is valid
|
||||||
|
static bool IsSchemeValid( const std::string& SchemeName )
|
||||||
|
{
|
||||||
|
for ( auto c : SchemeName )
|
||||||
|
{
|
||||||
|
if ( !isalpha( c ) && c != '+' && c != '-' && c != '.' ) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool LUrlParser::clParseURL::GetPort( int* OutPort ) const
|
||||||
|
{
|
||||||
|
if ( !IsValid() ) { return false; }
|
||||||
|
|
||||||
|
int Port = atoi( m_Port.c_str() );
|
||||||
|
|
||||||
|
if ( Port <= 0 || Port > 65535 ) { return false; }
|
||||||
|
|
||||||
|
if ( OutPort ) { *OutPort = Port; }
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// based on RFC 1738 and RFC 3986
|
||||||
|
LUrlParser::clParseURL LUrlParser::clParseURL::ParseURL( const std::string& URL )
|
||||||
|
{
|
||||||
|
LUrlParser::clParseURL Result;
|
||||||
|
|
||||||
|
const char* CurrentString = URL.c_str();
|
||||||
|
|
||||||
|
/*
|
||||||
|
* <scheme>:<scheme-specific-part>
|
||||||
|
* <scheme> := [a-z\+\-\.]+
|
||||||
|
* For resiliency, programs interpreting URLs should treat upper case letters as equivalent to lower case in scheme names
|
||||||
|
*/
|
||||||
|
|
||||||
|
// try to read scheme
|
||||||
|
{
|
||||||
|
const char* LocalString = strchr( CurrentString, ':' );
|
||||||
|
|
||||||
|
if ( !LocalString )
|
||||||
|
{
|
||||||
|
return clParseURL( LUrlParserError_NoUrlCharacter );
|
||||||
|
}
|
||||||
|
|
||||||
|
// save the scheme name
|
||||||
|
Result.m_Scheme = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
if ( !IsSchemeValid( Result.m_Scheme ) )
|
||||||
|
{
|
||||||
|
return clParseURL( LUrlParserError_InvalidSchemeName );
|
||||||
|
}
|
||||||
|
|
||||||
|
// scheme should be lowercase
|
||||||
|
std::transform( Result.m_Scheme.begin(), Result.m_Scheme.end(), Result.m_Scheme.begin(), ::tolower );
|
||||||
|
|
||||||
|
// skip ':'
|
||||||
|
CurrentString = LocalString+1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* //<user>:<password>@<host>:<port>/<url-path>
|
||||||
|
* any ":", "@" and "/" must be normalized
|
||||||
|
*/
|
||||||
|
|
||||||
|
// skip "//"
|
||||||
|
if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
|
||||||
|
if ( *CurrentString++ != '/' ) return clParseURL( LUrlParserError_NoDoubleSlash );
|
||||||
|
|
||||||
|
// check if the user name and password are specified
|
||||||
|
bool bHasUserName = false;
|
||||||
|
|
||||||
|
const char* LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString )
|
||||||
|
{
|
||||||
|
if ( *LocalString == '@' )
|
||||||
|
{
|
||||||
|
// user name and password are specified
|
||||||
|
bHasUserName = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if ( *LocalString == '/' )
|
||||||
|
{
|
||||||
|
// end of <host>:<port> specification
|
||||||
|
bHasUserName = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
// user name and password
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
if ( bHasUserName )
|
||||||
|
{
|
||||||
|
// read user name
|
||||||
|
while ( *LocalString && *LocalString != ':' && *LocalString != '@' ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_UserName = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
// proceed with the current pointer
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
if ( *CurrentString == ':' )
|
||||||
|
{
|
||||||
|
// skip ':'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read password
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString && *LocalString != '@' ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_Password = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip '@'
|
||||||
|
if ( *CurrentString != '@' )
|
||||||
|
{
|
||||||
|
return clParseURL( LUrlParserError_NoAtSign );
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool bHasBracket = ( *CurrentString == '[' );
|
||||||
|
|
||||||
|
// go ahead, read the host name
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString )
|
||||||
|
{
|
||||||
|
if ( bHasBracket && *LocalString == ']' )
|
||||||
|
{
|
||||||
|
// end of IPv6 address
|
||||||
|
LocalString++;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else if ( !bHasBracket && ( *LocalString == ':' || *LocalString == '/' ) )
|
||||||
|
{
|
||||||
|
// port number is specified
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalString++;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result.m_Host = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
// is port number specified?
|
||||||
|
if ( *CurrentString == ':' )
|
||||||
|
{
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read port number
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString && *LocalString != '/' ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_Port = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// end of string
|
||||||
|
if ( !*CurrentString )
|
||||||
|
{
|
||||||
|
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// skip '/'
|
||||||
|
if ( *CurrentString != '/' )
|
||||||
|
{
|
||||||
|
return clParseURL( LUrlParserError_NoSlash );
|
||||||
|
}
|
||||||
|
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// parse the path
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString && *LocalString != '#' && *LocalString != '?' ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_Path = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
|
||||||
|
// check for query
|
||||||
|
if ( *CurrentString == '?' )
|
||||||
|
{
|
||||||
|
// skip '?'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read query
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString && *LocalString != '#' ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_Query = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for fragment
|
||||||
|
if ( *CurrentString == '#' )
|
||||||
|
{
|
||||||
|
// skip '#'
|
||||||
|
CurrentString++;
|
||||||
|
|
||||||
|
// read fragment
|
||||||
|
LocalString = CurrentString;
|
||||||
|
|
||||||
|
while ( *LocalString ) LocalString++;
|
||||||
|
|
||||||
|
Result.m_Fragment = std::string( CurrentString, LocalString - CurrentString );
|
||||||
|
|
||||||
|
CurrentString = LocalString;
|
||||||
|
}
|
||||||
|
|
||||||
|
Result.m_ErrorCode = LUrlParserError_Ok;
|
||||||
|
|
||||||
|
return Result;
|
||||||
|
}
|
78
externals/lurlparser/LUrlParser.h
vendored
Normal file
78
externals/lurlparser/LUrlParser.h
vendored
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
/*
|
||||||
|
* Lightweight URL & URI parser (RFC 1738, RFC 3986)
|
||||||
|
* https://github.com/corporateshark/LUrlParser
|
||||||
|
*
|
||||||
|
* The MIT License (MIT)
|
||||||
|
*
|
||||||
|
* Copyright (C) 2015 Sergey Kosarevsky (sk@linderdaum.com)
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace LUrlParser
|
||||||
|
{
|
||||||
|
enum LUrlParserError
|
||||||
|
{
|
||||||
|
LUrlParserError_Ok = 0,
|
||||||
|
LUrlParserError_Uninitialized = 1,
|
||||||
|
LUrlParserError_NoUrlCharacter = 2,
|
||||||
|
LUrlParserError_InvalidSchemeName = 3,
|
||||||
|
LUrlParserError_NoDoubleSlash = 4,
|
||||||
|
LUrlParserError_NoAtSign = 5,
|
||||||
|
LUrlParserError_UnexpectedEndOfLine = 6,
|
||||||
|
LUrlParserError_NoSlash = 7,
|
||||||
|
};
|
||||||
|
|
||||||
|
class clParseURL
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
LUrlParserError m_ErrorCode;
|
||||||
|
std::string m_Scheme;
|
||||||
|
std::string m_Host;
|
||||||
|
std::string m_Port;
|
||||||
|
std::string m_Path;
|
||||||
|
std::string m_Query;
|
||||||
|
std::string m_Fragment;
|
||||||
|
std::string m_UserName;
|
||||||
|
std::string m_Password;
|
||||||
|
|
||||||
|
clParseURL()
|
||||||
|
: m_ErrorCode( LUrlParserError_Uninitialized )
|
||||||
|
{}
|
||||||
|
|
||||||
|
/// return 'true' if the parsing was successful
|
||||||
|
bool IsValid() const { return m_ErrorCode == LUrlParserError_Ok; }
|
||||||
|
|
||||||
|
/// helper to convert the port number to int, return 'true' if the port is valid (within the 0..65535 range)
|
||||||
|
bool GetPort( int* OutPort ) const;
|
||||||
|
|
||||||
|
/// parse the URL
|
||||||
|
static clParseURL ParseURL( const std::string& URL );
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit clParseURL( LUrlParserError ErrorCode )
|
||||||
|
: m_ErrorCode( ErrorCode )
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace LUrlParser
|
19
externals/lurlparser/README.md
vendored
Normal file
19
externals/lurlparser/README.md
vendored
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
From https://github.com/corporateshark/LUrlParser/commit/455d5e2d27e3946f11ad0328fee9ee2628e6a8e2
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
===
|
||||||
|
|
||||||
|
Lightweight URL & URI parser (RFC 1738, RFC 3986)
|
||||||
|
|
||||||
|
(C) Sergey Kosarevsky, 2015
|
||||||
|
|
||||||
|
@corporateshark sk@linderdaum.com
|
||||||
|
|
||||||
|
http://www.linderdaum.com
|
||||||
|
|
||||||
|
http://blog.linderdaum.com
|
||||||
|
|
||||||
|
=============================
|
||||||
|
|
||||||
|
A tiny and lightweight URL & URI parser (RFC 1738, RFC 3986) written in C++.
|
|
@ -17,7 +17,7 @@ struct WebResult {
|
||||||
Success,
|
Success,
|
||||||
InvalidURL,
|
InvalidURL,
|
||||||
CredentialsMissing,
|
CredentialsMissing,
|
||||||
CprError,
|
LibError,
|
||||||
HttpError,
|
HttpError,
|
||||||
WrongContent,
|
WrongContent,
|
||||||
NoWebservice,
|
NoWebservice,
|
||||||
|
|
|
@ -11,4 +11,8 @@ add_library(web_service STATIC
|
||||||
|
|
||||||
create_target_directory_groups(web_service)
|
create_target_directory_groups(web_service)
|
||||||
|
|
||||||
target_link_libraries(web_service PUBLIC common cpr json-headers)
|
get_directory_property(OPENSSL_LIBS
|
||||||
|
DIRECTORY ${CMAKE_SOURCE_DIR}/externals/libressl
|
||||||
|
DEFINITION OPENSSL_LIBS)
|
||||||
|
add_definitions(-DCPPHTTPLIB_OPENSSL_SUPPORT)
|
||||||
|
target_link_libraries(web_service PUBLIC common json-headers ${OPENSSL_LIBS} httplib lurlparser)
|
||||||
|
|
|
@ -2,13 +2,11 @@
|
||||||
// Licensed under GPLv2 or any later version
|
// Licensed under GPLv2 or any later version
|
||||||
// Refer to the license.txt file included.
|
// Refer to the license.txt file included.
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <cpr/cpr.h>
|
#include <LUrlParser.h>
|
||||||
|
#include <httplib.h>
|
||||||
#include "common/announce_multiplayer_room.h"
|
#include "common/announce_multiplayer_room.h"
|
||||||
#include "common/logging/log.h"
|
#include "common/logging/log.h"
|
||||||
#include "web_service/web_backend.h"
|
#include "web_service/web_backend.h"
|
||||||
|
@ -17,25 +15,45 @@ namespace WebService {
|
||||||
|
|
||||||
static constexpr char API_VERSION[]{"1"};
|
static constexpr char API_VERSION[]{"1"};
|
||||||
|
|
||||||
static std::unique_ptr<cpr::Session> g_session;
|
constexpr int HTTP_PORT = 80;
|
||||||
|
constexpr int HTTPS_PORT = 443;
|
||||||
|
|
||||||
void Win32WSAStartup() {
|
constexpr int TIMEOUT_SECONDS = 30;
|
||||||
#ifdef _WIN32
|
|
||||||
// On Windows, CPR/libcurl does not properly initialize Winsock. The below code is used to
|
std::unique_ptr<httplib::Client> GetClientFor(const LUrlParser::clParseURL& parsedUrl) {
|
||||||
// initialize Winsock globally, which fixes this problem. Without this, only the first CPR
|
namespace hl = httplib;
|
||||||
// session will properly be created, and subsequent ones will fail.
|
|
||||||
WSADATA wsa_data;
|
int port;
|
||||||
const int wsa_result{WSAStartup(MAKEWORD(2, 2), &wsa_data)};
|
|
||||||
if (wsa_result) {
|
std::unique_ptr<hl::Client> cli;
|
||||||
LOG_CRITICAL(WebService, "WSAStartup failed: %d", wsa_result);
|
|
||||||
|
if (parsedUrl.m_Scheme == "http") {
|
||||||
|
if (!parsedUrl.GetPort(&port)) {
|
||||||
|
port = HTTP_PORT;
|
||||||
|
}
|
||||||
|
return std::make_unique<hl::Client>(parsedUrl.m_Host.c_str(), port, TIMEOUT_SECONDS,
|
||||||
|
hl::HttpVersion::v1_1);
|
||||||
|
} else if (parsedUrl.m_Scheme == "https") {
|
||||||
|
if (!parsedUrl.GetPort(&port)) {
|
||||||
|
port = HTTPS_PORT;
|
||||||
|
}
|
||||||
|
return std::make_unique<hl::SSLClient>(parsedUrl.m_Host.c_str(), port, TIMEOUT_SECONDS,
|
||||||
|
hl::HttpVersion::v1_1);
|
||||||
|
} else {
|
||||||
|
LOG_ERROR(WebService, "Bad URL scheme %s", parsedUrl.m_Scheme.c_str());
|
||||||
|
return nullptr;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::future<Common::WebResult> PostJson(const std::string& url, const std::string& data,
|
std::future<Common::WebResult> PostJson(const std::string& url, const std::string& data,
|
||||||
bool allow_anonymous, const std::string& username,
|
bool allow_anonymous, const std::string& username,
|
||||||
const std::string& token) {
|
const std::string& token) {
|
||||||
if (url.empty()) {
|
using lup = LUrlParser::clParseURL;
|
||||||
|
namespace hl = httplib;
|
||||||
|
|
||||||
|
lup parsedUrl = lup::ParseURL(url);
|
||||||
|
|
||||||
|
if (url.empty() || !parsedUrl.IsValid()) {
|
||||||
LOG_ERROR(WebService, "URL is invalid");
|
LOG_ERROR(WebService, "URL is invalid");
|
||||||
return std::async(std::launch::deferred, []() {
|
return std::async(std::launch::deferred, []() {
|
||||||
return Common::WebResult{Common::WebResult::Code::InvalidURL, "URL is invalid"};
|
return Common::WebResult{Common::WebResult::Code::InvalidURL, "URL is invalid"};
|
||||||
|
@ -51,51 +69,71 @@ std::future<Common::WebResult> PostJson(const std::string& url, const std::strin
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Win32WSAStartup();
|
|
||||||
|
|
||||||
// Built request header
|
// Built request header
|
||||||
cpr::Header header;
|
hl::Headers params;
|
||||||
if (are_credentials_provided) {
|
if (are_credentials_provided) {
|
||||||
// Authenticated request if credentials are provided
|
// Authenticated request if credentials are provided
|
||||||
header = {{"Content-Type", "application/json"},
|
params = {{std::string("x-username"), username},
|
||||||
{"x-username", username.c_str()},
|
{std::string("x-token"), token},
|
||||||
{"x-token", token.c_str()},
|
{std::string("api-version"), std::string(API_VERSION)},
|
||||||
{"api-version", API_VERSION}};
|
{std::string("Content-Type"), std::string("application/json")}};
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, anonymous request
|
// Otherwise, anonymous request
|
||||||
header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
|
params = {{std::string("api-version"), std::string(API_VERSION)},
|
||||||
|
{std::string("Content-Type"), std::string("application/json")}};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Post JSON asynchronously
|
// Post JSON asynchronously
|
||||||
return cpr::PostCallback(
|
return std::async(std::launch::async, [url, parsedUrl, params, data] {
|
||||||
[](cpr::Response r) {
|
std::unique_ptr<hl::Client> cli = GetClientFor(parsedUrl);
|
||||||
if (r.error) {
|
|
||||||
LOG_ERROR(WebService, "POST to %s returned cpr error: %u:%s", r.url.c_str(),
|
if (cli == nullptr) {
|
||||||
static_cast<u32>(r.error.code), r.error.message.c_str());
|
return Common::WebResult{Common::WebResult::Code::InvalidURL, "URL is invalid"};
|
||||||
return Common::WebResult{Common::WebResult::Code::CprError, r.error.message};
|
|
||||||
}
|
}
|
||||||
if (r.status_code >= 400) {
|
|
||||||
LOG_ERROR(WebService, "POST to %s returned error status code: %u", r.url.c_str(),
|
hl::Request request;
|
||||||
r.status_code);
|
request.method = "POST";
|
||||||
|
request.path = "/" + parsedUrl.m_Path;
|
||||||
|
request.headers = params;
|
||||||
|
request.body = data;
|
||||||
|
|
||||||
|
hl::Response response;
|
||||||
|
|
||||||
|
if (!cli->send(request, response)) {
|
||||||
|
LOG_ERROR(WebService, "POST to %s returned null", url.c_str());
|
||||||
|
return Common::WebResult{Common::WebResult::Code::LibError, "Null response"};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.status >= 400) {
|
||||||
|
LOG_ERROR(WebService, "POST to %s returned error status code: %u", url.c_str(),
|
||||||
|
response.status);
|
||||||
return Common::WebResult{Common::WebResult::Code::HttpError,
|
return Common::WebResult{Common::WebResult::Code::HttpError,
|
||||||
std::to_string(r.status_code)};
|
std::to_string(response.status)};
|
||||||
}
|
}
|
||||||
if (r.header["content-type"].find("application/json") == std::string::npos) {
|
|
||||||
LOG_ERROR(WebService, "POST to %s returned wrong content: %s", r.url.c_str(),
|
auto content_type = response.headers.find("content-type");
|
||||||
r.header["content-type"].c_str());
|
|
||||||
return Common::WebResult{Common::WebResult::Code::WrongContent,
|
if (content_type == response.headers.end() ||
|
||||||
r.header["content-type"]};
|
content_type->second.find("application/json") == std::string::npos) {
|
||||||
|
LOG_ERROR(WebService, "POST to %s returned wrong content: %s", url.c_str(),
|
||||||
|
content_type->second.c_str());
|
||||||
|
return Common::WebResult{Common::WebResult::Code::WrongContent, content_type->second};
|
||||||
}
|
}
|
||||||
|
|
||||||
return Common::WebResult{Common::WebResult::Code::Success, ""};
|
return Common::WebResult{Common::WebResult::Code::Success, ""};
|
||||||
},
|
});
|
||||||
cpr::Url{url}, cpr::Body{data}, header);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
|
std::future<T> GetJson(std::function<T(const std::string&)> func, const std::string& url,
|
||||||
bool allow_anonymous, const std::string& username,
|
bool allow_anonymous, const std::string& username,
|
||||||
const std::string& token) {
|
const std::string& token) {
|
||||||
if (url.empty()) {
|
using lup = LUrlParser::clParseURL;
|
||||||
|
namespace hl = httplib;
|
||||||
|
|
||||||
|
lup parsedUrl = lup::ParseURL(url);
|
||||||
|
|
||||||
|
if (url.empty() || !parsedUrl.IsValid()) {
|
||||||
LOG_ERROR(WebService, "URL is invalid");
|
LOG_ERROR(WebService, "URL is invalid");
|
||||||
return std::async(std::launch::deferred, [func{std::move(func)}]() { return func(""); });
|
return std::async(std::launch::deferred, [func{std::move(func)}]() { return func(""); });
|
||||||
}
|
}
|
||||||
|
@ -106,42 +144,55 @@ std::future<T> GetJson(std::function<T(const std::string&)> func, const std::str
|
||||||
return std::async(std::launch::deferred, [func{std::move(func)}]() { return func(""); });
|
return std::async(std::launch::deferred, [func{std::move(func)}]() { return func(""); });
|
||||||
}
|
}
|
||||||
|
|
||||||
Win32WSAStartup();
|
|
||||||
|
|
||||||
// Built request header
|
// Built request header
|
||||||
cpr::Header header;
|
hl::Headers params;
|
||||||
if (are_credentials_provided) {
|
if (are_credentials_provided) {
|
||||||
// Authenticated request if credentials are provided
|
// Authenticated request if credentials are provided
|
||||||
header = {{"Content-Type", "application/json"},
|
params = {{std::string("x-username"), username},
|
||||||
{"x-username", username.c_str()},
|
{std::string("x-token"), token},
|
||||||
{"x-token", token.c_str()},
|
{std::string("api-version"), std::string(API_VERSION)}};
|
||||||
{"api-version", API_VERSION}};
|
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, anonymous request
|
// Otherwise, anonymous request
|
||||||
header = cpr::Header{{"Content-Type", "application/json"}, {"api-version", API_VERSION}};
|
params = {{std::string("api-version"), std::string(API_VERSION)}};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get JSON asynchronously
|
// Get JSON asynchronously
|
||||||
return cpr::GetCallback(
|
return std::async(std::launch::async, [func, url, parsedUrl, params] {
|
||||||
[func{std::move(func)}](cpr::Response r) {
|
std::unique_ptr<hl::Client> cli = GetClientFor(parsedUrl);
|
||||||
if (r.error) {
|
|
||||||
LOG_ERROR(WebService, "GET to %s returned cpr error: %u:%s", r.url.c_str(),
|
if (cli == nullptr) {
|
||||||
static_cast<u32>(r.error.code), r.error.message.c_str());
|
|
||||||
return func("");
|
return func("");
|
||||||
}
|
}
|
||||||
if (r.status_code >= 400) {
|
|
||||||
LOG_ERROR(WebService, "GET to %s returned error code: %u", r.url.c_str(),
|
hl::Request request;
|
||||||
r.status_code);
|
request.method = "GET";
|
||||||
|
request.path = "/" + parsedUrl.m_Path;
|
||||||
|
request.headers = params;
|
||||||
|
|
||||||
|
hl::Response response;
|
||||||
|
|
||||||
|
if (!cli->send(request, response)) {
|
||||||
|
LOG_ERROR(WebService, "GET to %s returned null", url.c_str());
|
||||||
return func("");
|
return func("");
|
||||||
}
|
}
|
||||||
if (r.header["content-type"].find("application/json") == std::string::npos) {
|
|
||||||
LOG_ERROR(WebService, "GET to %s returned wrong content: %s", r.url.c_str(),
|
if (response.status >= 400) {
|
||||||
r.header["content-type"].c_str());
|
LOG_ERROR(WebService, "GET to %s returned error status code: %u", url.c_str(),
|
||||||
|
response.status);
|
||||||
return func("");
|
return func("");
|
||||||
}
|
}
|
||||||
return func(r.text);
|
|
||||||
},
|
auto content_type = response.headers.find("content-type");
|
||||||
cpr::Url{url}, header);
|
|
||||||
|
if (content_type == response.headers.end() ||
|
||||||
|
content_type->second.find("application/json") == std::string::npos) {
|
||||||
|
LOG_ERROR(WebService, "GET to %s returned wrong content: %s", url.c_str(),
|
||||||
|
content_type->second.c_str());
|
||||||
|
return func("");
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(response.body);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
template std::future<bool> GetJson(std::function<bool(const std::string&)> func,
|
template std::future<bool> GetJson(std::function<bool(const std::string&)> func,
|
||||||
|
@ -154,45 +205,66 @@ template std::future<AnnounceMultiplayerRoom::RoomList> GetJson(
|
||||||
|
|
||||||
void DeleteJson(const std::string& url, const std::string& data, const std::string& username,
|
void DeleteJson(const std::string& url, const std::string& data, const std::string& username,
|
||||||
const std::string& token) {
|
const std::string& token) {
|
||||||
if (url.empty()) {
|
using lup = LUrlParser::clParseURL;
|
||||||
|
namespace hl = httplib;
|
||||||
|
|
||||||
|
lup parsedUrl = lup::ParseURL(url);
|
||||||
|
|
||||||
|
if (url.empty() || !parsedUrl.IsValid()) {
|
||||||
LOG_ERROR(WebService, "URL is invalid");
|
LOG_ERROR(WebService, "URL is invalid");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (token.empty() || username.empty()) {
|
const bool are_credentials_provided{!token.empty() && !username.empty()};
|
||||||
|
if (!are_credentials_provided) {
|
||||||
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
LOG_ERROR(WebService, "Credentials must be provided for authenticated requests");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Win32WSAStartup();
|
|
||||||
|
|
||||||
// Built request header
|
// Built request header
|
||||||
cpr::Header header = {{"Content-Type", "application/json"},
|
hl::Headers params = {{std::string("x-username"), username},
|
||||||
{"x-username", username.c_str()},
|
{std::string("x-token"), token},
|
||||||
{"x-token", token.c_str()},
|
{std::string("api-version"), std::string(API_VERSION)},
|
||||||
{"api-version", API_VERSION}};
|
{std::string("Content-Type"), std::string("application/json")}};
|
||||||
|
|
||||||
// Delete JSON asynchronously
|
// Delete JSON asynchronously
|
||||||
static std::future<void> future;
|
std::async(std::launch::async, [url, parsedUrl, params, data] {
|
||||||
future = cpr::DeleteCallback(
|
std::unique_ptr<hl::Client> cli = GetClientFor(parsedUrl);
|
||||||
[](cpr::Response r) {
|
|
||||||
if (r.error) {
|
if (cli == nullptr) {
|
||||||
LOG_ERROR(WebService, "Delete to %s returned cpr error: %u:%s", r.url.c_str(),
|
|
||||||
static_cast<u32>(r.error.code), r.error.message.c_str());
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (r.status_code >= 400) {
|
|
||||||
LOG_ERROR(WebService, "Delete to %s returned error status code: %u", r.url.c_str(),
|
hl::Request request;
|
||||||
r.status_code);
|
request.method = "DELETE";
|
||||||
|
request.path = "/" + parsedUrl.m_Path;
|
||||||
|
request.headers = params;
|
||||||
|
request.body = data;
|
||||||
|
|
||||||
|
hl::Response response;
|
||||||
|
|
||||||
|
if (!cli->send(request, response)) {
|
||||||
|
LOG_ERROR(WebService, "DELETE to %s returned null", url.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (r.header["content-type"].find("application/json") == std::string::npos) {
|
|
||||||
LOG_ERROR(WebService, "Delete to %s returned wrong content: %s", r.url.c_str(),
|
if (response.status >= 400) {
|
||||||
r.header["content-type"].c_str());
|
LOG_ERROR(WebService, "DELETE to %s returned error status code: %u", url.c_str(),
|
||||||
|
response.status);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
cpr::Url{url}, cpr::Body{data}, header);
|
auto content_type = response.headers.find("content-type");
|
||||||
|
|
||||||
|
if (content_type == response.headers.end() ||
|
||||||
|
content_type->second.find("application/json") == std::string::npos) {
|
||||||
|
LOG_ERROR(WebService, "DELETE to %s returned wrong content: %s", url.c_str(),
|
||||||
|
content_type->second.c_str());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace WebService
|
} // namespace WebService
|
||||||
|
|
Loading…
Reference in a new issue