mirror of
https://github.com/cookiengineer/audacity
synced 2025-06-15 23:59:37 +02:00
commit
2175d0219a
@ -33,4 +33,5 @@
|
||||
| audacity_use_vorbis | STRING | local | Use vorbis library [system (if available), local, off] |
|
||||
| audacity_use_vst | BOOL | ON | Use VST2 plug-in support [on, off] |
|
||||
| audacity_use_wxwidgets | STRING | local | Use wxwidgets library [system (if available), local, off] |
|
||||
| audacity_use_zlib | STRING | system | Use zlib library [system (if available), local, off] |
|
||||
| audacity_use_zlib | STRING | local | Use zlib library [system (if available), local, off] |
|
||||
| audacity_use_curl | STRING | local | Use curl library [system (if available), local] |
|
@ -139,7 +139,7 @@ include( AudacityFunctions )
|
||||
# Allow user to globally set the library preference
|
||||
cmd_option( ${_OPT}lib_preference
|
||||
"Library preference [system (if available), local]"
|
||||
"system"
|
||||
"local"
|
||||
STRINGS "system" "local"
|
||||
)
|
||||
|
||||
@ -257,11 +257,16 @@ endif()
|
||||
# Where the final product is stored
|
||||
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin )
|
||||
|
||||
set ( _SHARED_PROXY_BASE "shared" )
|
||||
set ( _SHARED_PROXY_BASE_PATH "${CMAKE_BINARY_DIR}/${_SHARED_PROXY_BASE}")
|
||||
|
||||
# Define the non-install and executable paths
|
||||
if( CMAKE_CONFIGURATION_TYPES )
|
||||
set( _DESTDIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_CFG_INTDIR}" )
|
||||
set( _SHARED_PROXY_PATH "${_SHARED_PROXY_BASE_PATH}/${CMAKE_CFG_INTDIR}")
|
||||
else()
|
||||
set( _DESTDIR "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/${CMAKE_BUILD_TYPE}" )
|
||||
set( _SHARED_PROXY_PATH "${_SHARED_PROXY_BASE_PATH}/${CMAKE_BUILD_TYPE}")
|
||||
endif()
|
||||
|
||||
set( _DEST "${_DESTDIR}" )
|
||||
|
@ -2,7 +2,7 @@ add_conan_lib(
|
||||
ZLIB
|
||||
zlib/1.2.11
|
||||
REQUIRED
|
||||
PGK_CONFIG "zlib >= 1.2.11"
|
||||
PKG_CONFIG "zlib >= 1.2.11"
|
||||
INTERFACE_NAME ZLIB::ZLIB
|
||||
OPTION_NAME zlib
|
||||
CONAN_OPTIONS
|
||||
@ -13,7 +13,7 @@ add_conan_lib(
|
||||
expat
|
||||
expat/2.2.9
|
||||
REQUIRED
|
||||
PGK_CONFIG "expat >= 2.1.0"
|
||||
PKG_CONFIG "expat >= 2.1.0"
|
||||
CONAN_OPTIONS
|
||||
expat:shared=True
|
||||
)
|
||||
@ -70,14 +70,14 @@ add_conan_lib(
|
||||
libmp3lame/3.100
|
||||
REQUIRED
|
||||
INTERFACE_NAME libmp3lame::libmp3lame
|
||||
PGK_CONFIG "lame >= 3.100"
|
||||
PKG_CONFIG "lame >= 3.100"
|
||||
)
|
||||
|
||||
add_conan_lib(
|
||||
libid3tag
|
||||
libid3tag/0.15.2b@audacity/stable
|
||||
OPTION_NAME id3tag
|
||||
PGK_CONFIG "id3tag >= 0.15.1b"
|
||||
PKG_CONFIG "id3tag >= 0.15.1b"
|
||||
CONAN_OPTIONS
|
||||
libid3tag:zlib=${id3tag_zlib}
|
||||
)
|
||||
@ -86,7 +86,35 @@ add_conan_lib(
|
||||
libmad
|
||||
libmad/0.15.2b@audacity/stable
|
||||
OPTION_NAME mad
|
||||
PGK_CONFIG "mad >= 0.15.1b"
|
||||
PKG_CONFIG "mad >= 0.15.1b"
|
||||
)
|
||||
|
||||
if( CMAKE_SYSTEM_NAME MATCHES "Darwin" )
|
||||
set( curl_ssl "darwinssl" )
|
||||
elseif( CMAKE_SYSTEM_NAME MATCHES "Windows" )
|
||||
set( curl_ssl "schannel")
|
||||
else()
|
||||
set ( curl_ssl "openssl" )
|
||||
endif ()
|
||||
|
||||
add_conan_lib(
|
||||
ThreadPool
|
||||
threadpool/20140926
|
||||
REQUIRED
|
||||
ALWAYS_ALLOW_CONAN_FALLBACK
|
||||
)
|
||||
|
||||
add_conan_lib(
|
||||
CURL
|
||||
libcurl/7.75.0
|
||||
REQUIRED
|
||||
OPTION_NAME curl
|
||||
PKG_CONFIG "libcurl >= 7.68.0"
|
||||
INTERFACE_NAME CURL::libcurl
|
||||
FIND_PACKAGE_OPTIONS
|
||||
CONAN_OPTIONS
|
||||
libcurl:with_ssl=${curl_ssl}
|
||||
libcurl:shared=True
|
||||
)
|
||||
|
||||
set_conan_vars_to_parent()
|
||||
|
@ -22,7 +22,7 @@ set( CONAN_RESOLVE_LIST )
|
||||
# SYMBOL WXWIDGET
|
||||
# REQUIRED
|
||||
# ALWAYS_ALLOW_CONAN_FALLBACK
|
||||
# PGK_CONFIG "wxwidgets >= 3.1.3"
|
||||
# PKG_CONFIG "wxwidgets >= 3.1.3"
|
||||
# FIND_PACKAGE_OPTIONS COMPONENTS adv base core html qa xml
|
||||
# INTERFACE_NAME wxwidgets::wxwidgets
|
||||
# HAS_ONLY_DEBUG_RELEASE
|
||||
@ -57,7 +57,7 @@ function (add_conan_lib package conan_package_name )
|
||||
elseif ( opt STREQUAL "CONAN_OPTIONS" )
|
||||
set( list_mode on )
|
||||
set( current_var "conan_package_options" )
|
||||
elseif ( opt STREQUAL "PGK_CONFIG" )
|
||||
elseif ( opt STREQUAL "PKG_CONFIG" )
|
||||
set( list_mode on )
|
||||
set( current_var "pkg_config_options" )
|
||||
elseif ( opt STREQUAL "OPTION_NAME" )
|
||||
@ -199,10 +199,10 @@ function ( _conan_install build_type )
|
||||
GENERATORS cmake_find_package_multi
|
||||
BUILD_REQUIRES ${CONAN_BUILD_REQUIRES}
|
||||
${CONAN_CONFIG_OPTIONS}
|
||||
IMPORTS "bin, *.dll -> ./bin/shared/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.dll -> ./bin/shared/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.dylib -> ./lib/shared/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.so* -> ./lib/shared/${build_type} @ keep_path=False"
|
||||
IMPORTS "bin, *.dll -> ./${_SHARED_PROXY_BASE}/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.dll -> ./${_SHARED_PROXY_BASE}/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.dylib -> ./${_SHARED_PROXY_BASE}/${build_type} @ keep_path=False"
|
||||
IMPORTS "lib, *.so* -> ./${_SHARED_PROXY_BASE}/${build_type} @ keep_path=False"
|
||||
OPTIONS ${CONAN_PACKAGE_OPTIONS}
|
||||
)
|
||||
|
||||
|
@ -348,7 +348,7 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
|
||||
)
|
||||
else()
|
||||
set( ATTRIBUTES "shape=octagon" )
|
||||
set_target_property_all( ${TARGET} ${DIRECTORY_PROPERTY} "${_EXEDIR}" )
|
||||
set_target_property_all( ${TARGET} ${DIRECTORY_PROPERTY} "${_SHARED_PROXY_PATH}" )
|
||||
set_target_properties( ${TARGET}
|
||||
PROPERTIES
|
||||
PREFIX ""
|
||||
@ -356,12 +356,13 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
|
||||
)
|
||||
endif()
|
||||
|
||||
if( "wxBase" IN_LIST IMPORT_TARGETS )
|
||||
if( "wxBase" IN_LIST IMPORT_TARGETS OR "wxwidgets::base" IN_LIST IMPORT_TARGETS )
|
||||
string( APPEND ATTRIBUTES " style=filled" )
|
||||
endif()
|
||||
|
||||
export_symbol_define( export_symbol "${TARGET}" )
|
||||
import_symbol_define( import_symbol "${TARGET}" )
|
||||
|
||||
list( APPEND DEFINES
|
||||
PRIVATE "${export_symbol}"
|
||||
INTERFACE "${import_symbol}"
|
||||
@ -374,9 +375,11 @@ function( audacity_module_fn NAME SOURCES IMPORT_TARGETS
|
||||
|
||||
# compute LIBRARIES
|
||||
set( LIBRARIES )
|
||||
|
||||
foreach( IMPORT ${IMPORT_TARGETS} )
|
||||
list( APPEND LIBRARIES "${IMPORT}" )
|
||||
endforeach()
|
||||
|
||||
list( APPEND LIBRARIES ${ADDITIONAL_LIBRARIES} )
|
||||
|
||||
# list( TRANSFORM SOURCES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/" )
|
||||
|
@ -45,6 +45,8 @@ function( gather_libs src )
|
||||
list( APPEND libs ${lib} )
|
||||
|
||||
gather_libs( ${lib} )
|
||||
elseif ( EXISTS "${DST}/${line}" )
|
||||
gather_libs( "${DST}/${line}" )
|
||||
endif()
|
||||
endforeach()
|
||||
elseif( CMAKE_HOST_SYSTEM_NAME MATCHES "Darwin" )
|
||||
@ -57,7 +59,11 @@ function( gather_libs src )
|
||||
foreach( line ${output} )
|
||||
if( line MATCHES "^.*\\.dylib " )
|
||||
string( REGEX REPLACE "dylib .*" "dylib" line "${line}" )
|
||||
set( lib "${WXWIN}/${line}" )
|
||||
|
||||
get_filename_component( dylib_name "${line}" NAME)
|
||||
|
||||
message(STATUS "Checking out ${line}")
|
||||
set( lib "${WXWIN}/${dylib_name}" )
|
||||
|
||||
if( NOT lib STREQUAL "${src}" AND NOT line MATCHES "@executable" AND EXISTS "${lib}")
|
||||
message(STATUS "\tProcessing ${lib}...")
|
||||
@ -68,7 +74,7 @@ function( gather_libs src )
|
||||
|
||||
message(STATUS "\t\tAdding ${refname} to ${src}")
|
||||
|
||||
list( APPEND postcmds "sh -c 'install_name_tool -change ${refname} @executable_path/../Frameworks/${refname} ${src}'" )
|
||||
list( APPEND postcmds "sh -c 'install_name_tool -change ${line} @executable_path/../Frameworks/${refname} ${src}'" )
|
||||
|
||||
gather_libs( ${lib} )
|
||||
endif()
|
||||
@ -82,10 +88,10 @@ function( gather_libs src )
|
||||
get_filename_component( libname "${src}" NAME )
|
||||
|
||||
foreach( line ${output} )
|
||||
message (STATUS "\tChecking ${line}...")
|
||||
|
||||
string( REGEX REPLACE "(.*) => .* \\(.*$" "\\1" line "${line}" )
|
||||
|
||||
message (STATUS "\tChecking ${line}...")
|
||||
|
||||
set(line "${WXWIN}/${line}")
|
||||
|
||||
if (EXISTS "${line}")
|
||||
|
30
cmake-proxies/cmake-modules/dependencies/curl.cmake
Normal file
30
cmake-proxies/cmake-modules/dependencies/curl.cmake
Normal file
@ -0,0 +1,30 @@
|
||||
if( ${_OPT}use_curl STREQUAL "system" )
|
||||
# On macOS - CURL::libcurl is not defined by FindCURL for some reason
|
||||
|
||||
if(NOT TARGET CURL::libcurl)
|
||||
add_library(CURL::libcurl UNKNOWN IMPORTED)
|
||||
set_target_properties(CURL::libcurl PROPERTIES
|
||||
INTERFACE_INCLUDE_DIRECTORIES "${CURL_INCLUDE_DIRS}")
|
||||
|
||||
if(EXISTS "${CURL_LIBRARY}")
|
||||
set_target_properties(CURL::libcurl PROPERTIES
|
||||
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
||||
IMPORTED_LOCATION "${CURL_LIBRARY}")
|
||||
endif()
|
||||
|
||||
if(CURL_LIBRARY_RELEASE)
|
||||
set_property(TARGET CURL::libcurl APPEND PROPERTY
|
||||
IMPORTED_CONFIGURATIONS RELEASE)
|
||||
set_target_properties(CURL::libcurl PROPERTIES
|
||||
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
||||
IMPORTED_LOCATION_RELEASE "${CURL_LIBRARY_RELEASE}")
|
||||
endif()
|
||||
if(CURL_LIBRARY_DEBUG)
|
||||
set_property(TARGET CURL::libcurl APPEND PROPERTY
|
||||
IMPORTED_CONFIGURATIONS DEBUG)
|
||||
set_target_properties(CURL::libcurl PROPERTIES
|
||||
IMPORTED_LINK_INTERFACE_LANGUAGES "C"
|
||||
IMPORTED_LOCATION_DEBUG "${CURL_LIBRARY_DEBUG}")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
@ -3,10 +3,12 @@
|
||||
# The list of modules is ordered so that each library occurs after any others
|
||||
# that it depends on
|
||||
set( LIBRARIES
|
||||
"lib-string-utils"
|
||||
"lib-network-manager"
|
||||
)
|
||||
|
||||
foreach( LIBRARY ${LIBRARIES} )
|
||||
add_subdirectory("${LIBRARY}")
|
||||
add_subdirectory( "${LIBRARY}" )
|
||||
endforeach()
|
||||
|
||||
set( GRAPH_EDGES "${GRAPH_EDGES}" PARENT_SCOPE )
|
||||
|
41
libraries/lib-network-manager/CMakeLists.txt
Normal file
41
libraries/lib-network-manager/CMakeLists.txt
Normal file
@ -0,0 +1,41 @@
|
||||
set(TARGET lib-network-manager)
|
||||
set( TARGET_ROOT ${CMAKE_CURRENT_SOURCE_DIR} )
|
||||
|
||||
def_vars()
|
||||
|
||||
set( SOURCES
|
||||
IResponse.h
|
||||
IResponseFactory.h
|
||||
|
||||
HeadersList.h
|
||||
HeadersList.cpp
|
||||
|
||||
CookiesList.h
|
||||
CookiesList.cpp
|
||||
|
||||
Request.h
|
||||
Request.cpp
|
||||
|
||||
NetworkManager.h
|
||||
NetworkManager.cpp
|
||||
|
||||
curl/CurlResponse.h
|
||||
curl/CurlResponse.cpp
|
||||
curl/CurlResponseFactory.h
|
||||
curl/CurlResponseFactory.cpp
|
||||
curl/CurlStringList.h
|
||||
curl/CurlStringList.cpp
|
||||
curl/CurlHandleManager.h
|
||||
curl/CurlHandleManager.cpp
|
||||
)
|
||||
|
||||
|
||||
set ( LIBRARIES PRIVATE
|
||||
CURL::libcurl
|
||||
ThreadPool::ThreadPool
|
||||
lib-string-utils
|
||||
wxwidgets::base
|
||||
)
|
||||
|
||||
|
||||
audacity_library( ${TARGET} "${SOURCES}" "${LIBRARIES}" "" "" )
|
215
libraries/lib-network-manager/CookiesList.cpp
Normal file
215
libraries/lib-network-manager/CookiesList.cpp
Normal file
@ -0,0 +1,215 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CookiesList.cpp
|
||||
@brief Define HTTP cookies list class.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class Cookie
|
||||
@brief A struct, representing a Cookie object.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class CookiesList
|
||||
@brief A class, representing a list of HTTP cookies.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
#include "CookiesList.h"
|
||||
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
|
||||
#include "DateTimeConversions.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
Cookie Cookie::Parse (const std::string& cookieString)
|
||||
{
|
||||
const size_t equalsPosition = cookieString.find ('=');
|
||||
|
||||
if (equalsPosition == std::string::npos)
|
||||
return { cookieString, std::string () };
|
||||
|
||||
std::string name = cookieString.substr (0, equalsPosition);
|
||||
|
||||
const size_t firstValueIndex = equalsPosition + 1;
|
||||
|
||||
const size_t semicolonPosition = cookieString.find (';', firstValueIndex);
|
||||
|
||||
Cookie cookie;
|
||||
|
||||
cookie.Name = std::move (name);
|
||||
cookie.Value = semicolonPosition == std::string::npos ?
|
||||
cookieString.substr (firstValueIndex) :
|
||||
cookieString.substr (firstValueIndex, semicolonPosition - firstValueIndex);
|
||||
|
||||
size_t expiresPosition = cookieString.find("Expires=");
|
||||
|
||||
if (expiresPosition != std::string::npos)
|
||||
{
|
||||
expiresPosition += std::strlen ("Expires=");
|
||||
|
||||
const size_t trailingSemicolon = cookieString.find (';', expiresPosition);
|
||||
|
||||
std::string expiresValue =
|
||||
trailingSemicolon == std::string::npos ?
|
||||
cookieString.substr (expiresPosition) :
|
||||
cookieString.substr (expiresPosition, trailingSemicolon - expiresPosition);
|
||||
|
||||
// Hack around Yandex violating RFC
|
||||
std::replace (expiresValue.begin(), expiresValue.end(), '-', ' ');
|
||||
ParseRFC822Date (expiresValue, &cookie.Expires);
|
||||
}
|
||||
|
||||
return cookie;
|
||||
}
|
||||
|
||||
bool Cookie::isSession () const noexcept
|
||||
{
|
||||
return Expires == ExpiresTime {};
|
||||
}
|
||||
|
||||
std::string Cookie::toString (bool fullString) const
|
||||
{
|
||||
std::ostringstream stream;
|
||||
|
||||
stream << Name << "=" << Value;
|
||||
|
||||
if (fullString)
|
||||
{
|
||||
if (!isSession())
|
||||
stream << "; " << "Expires=" << SerializeRFC822Date(Expires);
|
||||
}
|
||||
|
||||
return stream.str ();
|
||||
}
|
||||
|
||||
bool Cookie::isExpired () const noexcept
|
||||
{
|
||||
return !isSession() &&
|
||||
std::chrono::system_clock::now() >= Expires;
|
||||
}
|
||||
|
||||
void CookiesList::setCookie (const Cookie& cookie)
|
||||
{
|
||||
setCookie (cookie.Name, cookie.Value);
|
||||
}
|
||||
|
||||
void CookiesList::setCookie (const std::string& cookieName, std::string cookieValue)
|
||||
{
|
||||
Cookie* item = getCookie (cookieName);
|
||||
|
||||
if (item != nullptr)
|
||||
item->Value = std::move (cookieValue);
|
||||
else
|
||||
mCookies.push_back ({ cookieName, std::move (cookieValue) });
|
||||
}
|
||||
|
||||
void CookiesList::addCookie (Cookie cookie)
|
||||
{
|
||||
addCookie (std::move (cookie.Name), std::move (cookie.Value));
|
||||
}
|
||||
|
||||
void CookiesList::addCookie (std::string cookieName, std::string cookieValue)
|
||||
{
|
||||
mCookies.push_back ({ std::move (cookieName), std::move (cookieValue) });
|
||||
}
|
||||
|
||||
bool CookiesList::hasCookie (const std::string& cookieName) const noexcept
|
||||
{
|
||||
return getCookie (cookieName) != nullptr;
|
||||
}
|
||||
|
||||
std::string CookiesList::getCookieValue (const std::string& cookieName) const
|
||||
{
|
||||
const Cookie* item = getCookie (cookieName);
|
||||
|
||||
if (item != nullptr)
|
||||
return item->Value;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
const Cookie* CookiesList::getCookie (size_t idx) const noexcept
|
||||
{
|
||||
return const_cast<CookiesList*>(this)->getCookie(idx);
|
||||
}
|
||||
|
||||
const Cookie* CookiesList::getCookie (const std::string& name) const noexcept
|
||||
{
|
||||
return const_cast<CookiesList*>(this)->getCookie (name);
|
||||
}
|
||||
|
||||
size_t CookiesList::getCookiesCount () const noexcept
|
||||
{
|
||||
return mCookies.size ();
|
||||
}
|
||||
|
||||
std::string CookiesList::getCookiesString () const
|
||||
{
|
||||
std::string result;
|
||||
|
||||
for (const Cookie& cookie : mCookies)
|
||||
{
|
||||
if (!result.empty ())
|
||||
result.push_back (';');
|
||||
|
||||
result += cookie.Name + "=" + cookie.Value;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
CookiesList::CookiesIterator CookiesList::begin () noexcept
|
||||
{
|
||||
return mCookies.begin ();
|
||||
}
|
||||
|
||||
CookiesList::CookiesIterator CookiesList::end () noexcept
|
||||
{
|
||||
return mCookies.end ();
|
||||
}
|
||||
|
||||
CookiesList::CookiesConstIterator CookiesList::begin () const noexcept
|
||||
{
|
||||
return mCookies.begin ();
|
||||
}
|
||||
|
||||
CookiesList::CookiesConstIterator CookiesList::end () const noexcept
|
||||
{
|
||||
return mCookies.end ();
|
||||
}
|
||||
|
||||
Cookie* CookiesList::getCookie (size_t idx) noexcept
|
||||
{
|
||||
if (idx < mCookies.size ())
|
||||
return &mCookies[idx];
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Cookie* CookiesList::getCookie (const std::string& cookieName) noexcept
|
||||
{
|
||||
for (Cookie& cookie : mCookies)
|
||||
{
|
||||
if (cookie.Name == cookieName)
|
||||
return &cookie;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
78
libraries/lib-network-manager/CookiesList.h
Normal file
78
libraries/lib-network-manager/CookiesList.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CookiesList.h
|
||||
@brief Define HTTP cookies list class.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
|
||||
#include "NetworkManagerApi.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
using ExpiresTime = std::chrono::system_clock::time_point;
|
||||
|
||||
struct NETWORK_MANAGER_API Cookie final
|
||||
{
|
||||
std::string Name;
|
||||
std::string Value;
|
||||
|
||||
ExpiresTime Expires {};
|
||||
|
||||
static Cookie Parse (const std::string& cookieString);
|
||||
|
||||
bool isSession () const noexcept;
|
||||
bool isExpired () const noexcept;
|
||||
|
||||
std::string toString(bool fullString) const;
|
||||
};
|
||||
|
||||
class NETWORK_MANAGER_API CookiesList final
|
||||
{
|
||||
using CookiesStorageType = std::vector<Cookie>;
|
||||
public:
|
||||
using CookiesIterator = CookiesStorageType::iterator;
|
||||
using CookiesConstIterator = CookiesStorageType::const_iterator;
|
||||
|
||||
void setCookie (const Cookie& cookie);
|
||||
void setCookie (const std::string& cookieName, std::string cookieValue);
|
||||
|
||||
void addCookie (Cookie cookie);
|
||||
void addCookie (std::string cookieName, std::string cookieValue);
|
||||
|
||||
bool hasCookie (const std::string& cookieName) const noexcept;
|
||||
|
||||
std::string getCookieValue (const std::string& cookieName) const;
|
||||
|
||||
const Cookie* getCookie (size_t idx) const noexcept;
|
||||
const Cookie* getCookie (const std::string& name) const noexcept;
|
||||
|
||||
size_t getCookiesCount () const noexcept;
|
||||
|
||||
std::string getCookiesString () const;
|
||||
|
||||
CookiesIterator begin () noexcept;
|
||||
CookiesIterator end () noexcept;
|
||||
|
||||
CookiesConstIterator begin () const noexcept;
|
||||
CookiesConstIterator end () const noexcept;
|
||||
private:
|
||||
Cookie* getCookie (size_t idx) noexcept;
|
||||
Cookie* getCookie (const std::string& cookieName) noexcept;
|
||||
|
||||
CookiesStorageType mCookies;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
157
libraries/lib-network-manager/HeadersList.cpp
Normal file
157
libraries/lib-network-manager/HeadersList.cpp
Normal file
@ -0,0 +1,157 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file HeadersList.cpp
|
||||
@brief Define HTTP headers list class.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class Header
|
||||
@brief A string pair, representing HTTP header.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class HeadersList
|
||||
@brief A class, representing a list of HTTP headers.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
#include "HeadersList.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
bool Header::hasSameName (const Header& header) const
|
||||
{
|
||||
return hasSameName (header.Name);
|
||||
}
|
||||
|
||||
bool Header::hasSameName (const std::string& name) const
|
||||
{
|
||||
return std::equal (
|
||||
name.begin (), name.end (),
|
||||
Name.begin (), Name.end (),
|
||||
[](const char leftChar, const char rightChar) {
|
||||
return std::tolower (leftChar) == std::tolower (rightChar);
|
||||
});
|
||||
}
|
||||
|
||||
Header Header::Parse (const std::string& header)
|
||||
{
|
||||
const size_t colonPosition = header.find (": ");
|
||||
|
||||
if (colonPosition == std::string::npos) // This can happen when we receieve the first line of the response
|
||||
return { header, std::string () };
|
||||
|
||||
return {
|
||||
header.substr (0, colonPosition),
|
||||
header.substr (colonPosition + 2)
|
||||
};
|
||||
}
|
||||
|
||||
void HeadersList::setHeader (const Header& header)
|
||||
{
|
||||
setHeader (header.Name, header.Value);
|
||||
}
|
||||
|
||||
void HeadersList::setHeader (const std::string& headerName, std::string headerValue)
|
||||
{
|
||||
Header* item = getHeader (headerName);
|
||||
|
||||
if (item != nullptr)
|
||||
item->Value = std::move (headerValue);
|
||||
else
|
||||
mHeaders.push_back ({ headerName, std::move (headerValue) });
|
||||
}
|
||||
|
||||
void HeadersList::addHeader (Header header)
|
||||
{
|
||||
addHeader (std::move (header.Name), std::move (header.Value));
|
||||
}
|
||||
|
||||
void HeadersList::addHeader (std::string headerName, std::string headerValue)
|
||||
{
|
||||
mHeaders.push_back ({ std::move (headerName), std::move (headerValue) });
|
||||
}
|
||||
|
||||
bool HeadersList::hasHeader (const std::string& headerName) const noexcept
|
||||
{
|
||||
return getHeader (headerName) != nullptr;
|
||||
}
|
||||
|
||||
std::string HeadersList::getHeaderValue (const std::string& headerName) const
|
||||
{
|
||||
const Header* header = getHeader (headerName);
|
||||
|
||||
if (header != nullptr)
|
||||
return header->Value;
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
const Header* HeadersList::getHeader (size_t idx) const noexcept
|
||||
{
|
||||
return const_cast<HeadersList*>(this)->getHeader (idx);
|
||||
}
|
||||
|
||||
const Header* HeadersList::getHeader (const std::string& name) const noexcept
|
||||
{
|
||||
return const_cast<HeadersList*>(this)->getHeader (name);
|
||||
}
|
||||
|
||||
size_t HeadersList::getHeadersCount () const noexcept
|
||||
{
|
||||
return mHeaders.size ();
|
||||
}
|
||||
|
||||
HeadersList::HeadersIterator HeadersList::begin () noexcept
|
||||
{
|
||||
return mHeaders.begin ();
|
||||
}
|
||||
|
||||
HeadersList::HeadersIterator HeadersList::end () noexcept
|
||||
{
|
||||
return mHeaders.end ();
|
||||
}
|
||||
|
||||
HeadersList::HeadersConstIterator HeadersList::begin () const noexcept
|
||||
{
|
||||
return mHeaders.begin ();
|
||||
}
|
||||
|
||||
HeadersList::HeadersConstIterator HeadersList::end () const noexcept
|
||||
{
|
||||
return mHeaders.end ();
|
||||
}
|
||||
|
||||
Header* HeadersList::getHeader (size_t idx) noexcept
|
||||
{
|
||||
if (idx < mHeaders.size ())
|
||||
return &mHeaders[idx];
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Header* HeadersList::getHeader (const std::string& headerName) noexcept
|
||||
{
|
||||
for (Header& header : mHeaders)
|
||||
{
|
||||
if (header.hasSameName (headerName))
|
||||
return &header;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
69
libraries/lib-network-manager/HeadersList.h
Normal file
69
libraries/lib-network-manager/HeadersList.h
Normal file
@ -0,0 +1,69 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file HeadersList.h
|
||||
@brief Declare HTTP headers list class.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "NetworkManagerApi.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
struct NETWORK_MANAGER_API Header final
|
||||
{
|
||||
std::string Name;
|
||||
std::string Value;
|
||||
|
||||
bool hasSameName (const Header& header) const;
|
||||
bool hasSameName (const std::string& name) const;
|
||||
|
||||
static Header Parse (const std::string& header);
|
||||
};
|
||||
|
||||
class NETWORK_MANAGER_API HeadersList final
|
||||
{
|
||||
using HeadersStorageType = std::vector<Header>;
|
||||
public:
|
||||
using HeadersIterator = HeadersStorageType::iterator;
|
||||
using HeadersConstIterator = HeadersStorageType::const_iterator;
|
||||
|
||||
void setHeader (const Header& header);
|
||||
void setHeader (const std::string& headerName, std::string headerValue);
|
||||
|
||||
void addHeader (Header header);
|
||||
void addHeader (std::string headerName, std::string headerValue);
|
||||
|
||||
bool hasHeader (const std::string& headerName) const noexcept;
|
||||
|
||||
std::string getHeaderValue (const std::string& headerName) const;
|
||||
|
||||
const Header* getHeader (size_t idx) const noexcept;
|
||||
const Header* getHeader (const std::string& name) const noexcept;
|
||||
|
||||
size_t getHeadersCount () const noexcept;
|
||||
|
||||
HeadersIterator begin () noexcept;
|
||||
HeadersIterator end () noexcept;
|
||||
|
||||
HeadersConstIterator begin () const noexcept;
|
||||
HeadersConstIterator end () const noexcept;
|
||||
private:
|
||||
Header* getHeader (size_t idx) noexcept;
|
||||
Header* getHeader (const std::string& headerName) noexcept;
|
||||
|
||||
HeadersStorageType mHeaders;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
109
libraries/lib-network-manager/IResponse.h
Normal file
109
libraries/lib-network-manager/IResponse.h
Normal file
@ -0,0 +1,109 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file IResponse.h
|
||||
@brief Declare an interface for HTTP response.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "NetworkManagerApi.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class Request;
|
||||
class HeadersList;
|
||||
class CookiesList;
|
||||
|
||||
enum class NetworkError
|
||||
{
|
||||
NoError,
|
||||
BadURL,
|
||||
ConnectionFailed,
|
||||
ConnectionRefused,
|
||||
RemoteHostClosed,
|
||||
HostNotFound,
|
||||
Timeout,
|
||||
OperationCancelled,
|
||||
SSLHandshakeFailed,
|
||||
TooManyRedirects,
|
||||
ProxyConnectionFailed,
|
||||
ProxyNotFound,
|
||||
UnknownError,
|
||||
HTTPError
|
||||
};
|
||||
|
||||
class NETWORK_MANAGER_API IResponse
|
||||
{
|
||||
public:
|
||||
using RequestCallback = std::function<void (IResponse*)>;
|
||||
|
||||
virtual ~IResponse () = default;
|
||||
|
||||
virtual bool isFinished () const noexcept = 0;
|
||||
|
||||
virtual unsigned getHTTPCode () const noexcept = 0;
|
||||
|
||||
virtual NetworkError getError () const noexcept = 0;
|
||||
virtual std::string getErrorString () const = 0;
|
||||
|
||||
virtual bool headersReceived () const noexcept = 0;
|
||||
|
||||
virtual bool hasHeader (const std::string& headerName) const noexcept = 0;
|
||||
virtual std::string getHeader (const std::string& headerName) const = 0;
|
||||
|
||||
virtual const HeadersList& getHeaders () const noexcept = 0;
|
||||
virtual const CookiesList& getCookies () const noexcept = 0;
|
||||
|
||||
virtual const Request& getRequest () const noexcept = 0;
|
||||
virtual std::string getURL () const = 0;
|
||||
|
||||
virtual void abort () noexcept = 0;
|
||||
|
||||
virtual void setOnDataReceivedCallback (RequestCallback callback) = 0;
|
||||
virtual void setRequestFinishedCallback (RequestCallback callback) = 0;
|
||||
|
||||
// The total bytes available to read by readData
|
||||
virtual uint64_t getBytesAvailable () const noexcept = 0;
|
||||
|
||||
// Reads at max maxBytesCount into the buffer, returns the actual count of bytes read
|
||||
virtual uint64_t readData (void* buffer, uint64_t maxBytesCount) = 0;
|
||||
|
||||
template<typename RetVal = std::vector<uint8_t>>
|
||||
RetVal readAll ()
|
||||
{
|
||||
RetVal result;
|
||||
|
||||
constexpr uint64_t bufferSize = 4 * 1024;
|
||||
uint8_t buffer[bufferSize];
|
||||
|
||||
while (uint64_t bytesRead = readData (buffer, bufferSize))
|
||||
{
|
||||
using PtrType = typename RetVal::pointer;
|
||||
|
||||
PtrType begin = reinterpret_cast<PtrType>(buffer);
|
||||
PtrType end = reinterpret_cast<PtrType>(buffer + bytesRead);
|
||||
|
||||
result.insert (result.end (), begin, end);
|
||||
|
||||
if (bytesRead < bufferSize)
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
}
|
49
libraries/lib-network-manager/IResponseFactory.h
Normal file
49
libraries/lib-network-manager/IResponseFactory.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file IResponse.h
|
||||
@brief Declare an interface for HTTP response factory.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class IResponse;
|
||||
class Request;
|
||||
|
||||
using ResponsePtr = std::shared_ptr<IResponse>;
|
||||
|
||||
enum class RequestVerb
|
||||
{
|
||||
Head,
|
||||
Get,
|
||||
Post,
|
||||
Put,
|
||||
Delete
|
||||
};
|
||||
|
||||
class IResponseFactory
|
||||
{
|
||||
public:
|
||||
virtual ~IResponseFactory () = default;
|
||||
|
||||
virtual void setProxy (const std::string& proxy) = 0;
|
||||
|
||||
virtual ResponsePtr performRequest (RequestVerb verb, const Request& request) = 0;
|
||||
virtual ResponsePtr performRequest (RequestVerb verb, const Request& request, const void* data, size_t size) = 0;
|
||||
|
||||
virtual void terminate () = 0;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
79
libraries/lib-network-manager/NetworkManager.cpp
Normal file
79
libraries/lib-network-manager/NetworkManager.cpp
Normal file
@ -0,0 +1,79 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file NetworkManager.cpp
|
||||
@brief Define a class for preforming HTTP requests.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class NetworkManager
|
||||
@brief Class for preforming HTTP requests.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
#include "NetworkManager.h"
|
||||
|
||||
#include "IResponseFactory.h"
|
||||
#include "curl/CurlResponseFactory.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
NetworkManager::NetworkManager ()
|
||||
{
|
||||
mResponseFactory = std::make_unique<CurlResponseFactory> ();
|
||||
}
|
||||
|
||||
NetworkManager::~NetworkManager ()
|
||||
{}
|
||||
|
||||
NetworkManager& NetworkManager::GetInstance ()
|
||||
{
|
||||
static NetworkManager instance;
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
void NetworkManager::Terminate ()
|
||||
{
|
||||
GetInstance ().mResponseFactory->terminate ();
|
||||
}
|
||||
|
||||
ResponsePtr NetworkManager::doGet (const Request& request)
|
||||
{
|
||||
return mResponseFactory->performRequest (RequestVerb::Get, request);
|
||||
}
|
||||
|
||||
ResponsePtr NetworkManager::doHead (const Request& request)
|
||||
{
|
||||
return mResponseFactory->performRequest (RequestVerb::Head, request);
|
||||
}
|
||||
|
||||
ResponsePtr NetworkManager::doDelete (const Request& request)
|
||||
{
|
||||
return mResponseFactory->performRequest (RequestVerb::Delete, request);
|
||||
}
|
||||
|
||||
ResponsePtr NetworkManager::doPost (const Request& request, const void* data, size_t size)
|
||||
{
|
||||
return mResponseFactory->performRequest (RequestVerb::Post, request, data, size);
|
||||
}
|
||||
|
||||
ResponsePtr NetworkManager::doPut (const Request& request, const void* data, size_t size)
|
||||
{
|
||||
return mResponseFactory->performRequest (RequestVerb::Put, request, data, size);
|
||||
}
|
||||
|
||||
void NetworkManager::setProxy (const std::string& proxy)
|
||||
{
|
||||
mResponseFactory->setProxy (proxy);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
49
libraries/lib-network-manager/NetworkManager.h
Normal file
49
libraries/lib-network-manager/NetworkManager.h
Normal file
@ -0,0 +1,49 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file NetworkManager.h
|
||||
@brief Declare a class for preforming HTTP requests.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "NetworkManagerApi.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class Request;
|
||||
class IResponse;
|
||||
class IResponseFactory;
|
||||
|
||||
using ResponsePtr = std::shared_ptr<IResponse>;
|
||||
|
||||
class NETWORK_MANAGER_API NetworkManager final
|
||||
{
|
||||
NetworkManager ();
|
||||
~NetworkManager ();
|
||||
public:
|
||||
static NetworkManager& GetInstance();
|
||||
static void Terminate ();
|
||||
|
||||
ResponsePtr doGet (const Request& request);
|
||||
ResponsePtr doHead (const Request& request);
|
||||
ResponsePtr doDelete (const Request& request);
|
||||
ResponsePtr doPost (const Request& request, const void* data, size_t size);
|
||||
ResponsePtr doPut (const Request& request, const void* data, size_t size);
|
||||
|
||||
void setProxy (const std::string& proxy);
|
||||
private:
|
||||
std::unique_ptr<IResponseFactory> mResponseFactory;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
31
libraries/lib-network-manager/NetworkManagerApi.h
Normal file
31
libraries/lib-network-manager/NetworkManagerApi.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file NetworkManagerApi.h
|
||||
@brief Declare macros for the Network Manager library DLL API
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
/* The dynamic library import and export for Microsoft Windows.
|
||||
* Supported by Visual Studio and for GCC 4+ */
|
||||
#if defined _WIN32 || (defined __CYGWIN__ && defined __GNUC__)
|
||||
# ifndef NETWORK_MANAGER_API
|
||||
# ifdef BUILDING_LIB_NETWORK_MANAGER
|
||||
# define NETWORK_MANAGER_API __declspec(dllexport)
|
||||
# else
|
||||
# ifdef _DLL
|
||||
# define NETWORK_MANAGER_API __declspec(dllimport)
|
||||
# else
|
||||
# define NETWORK_MANAGER_API
|
||||
# endif
|
||||
# endif
|
||||
# endif
|
||||
#else
|
||||
# ifndef NETWORK_MANAGER_API
|
||||
# define NETWORK_MANAGER_API __attribute__((visibility("default")))
|
||||
# endif
|
||||
#endif //_WIN32 || (__CYGWIN__ && __GNUC__)
|
115
libraries/lib-network-manager/Request.cpp
Normal file
115
libraries/lib-network-manager/Request.cpp
Normal file
@ -0,0 +1,115 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file Request.cpp
|
||||
@brief Define a class for constructing HTTP requests.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
@class Request
|
||||
@brief Class to construct the HTTP request.
|
||||
|
||||
**********************************************************************/
|
||||
|
||||
#include "Request.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <algorithm>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
|
||||
Request::Request(std::string url) noexcept
|
||||
: mUrl (std::move (url))
|
||||
{
|
||||
}
|
||||
|
||||
Request& Request::setURL(std::string url) noexcept
|
||||
{
|
||||
mUrl = std::move (url);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
const std::string& Request::getURL () const noexcept
|
||||
{
|
||||
return mUrl;
|
||||
}
|
||||
|
||||
Request& Request::setHeader (const std::string& name, std::string value)
|
||||
{
|
||||
mHeaders.setHeader (name, std::move (value));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string Request::getHeader (const std::string& name) const
|
||||
{
|
||||
return mHeaders.getHeaderValue (name);
|
||||
}
|
||||
|
||||
const HeadersList& Request::getHeaders () const noexcept
|
||||
{
|
||||
return mHeaders;
|
||||
}
|
||||
|
||||
Request& Request::setCookie (const std::string& name, std::string value)
|
||||
{
|
||||
mCookies.setCookie (name, std::move (value));
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
std::string Request::getCookie (const std::string& name) const
|
||||
{
|
||||
return mCookies.getCookieValue (name);
|
||||
}
|
||||
|
||||
const CookiesList& Request::getCookies () noexcept
|
||||
{
|
||||
return mCookies;
|
||||
}
|
||||
|
||||
Request& Request::setMaxRedirects (size_t redirects) noexcept
|
||||
{
|
||||
mMaxRedirects = redirects;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
size_t Request::getMaxRedirects () const noexcept
|
||||
{
|
||||
return mMaxRedirects;
|
||||
}
|
||||
|
||||
Request& Request::setTimeout (Timeout timeout) noexcept
|
||||
{
|
||||
mTimeout = timeout;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Request::Timeout Request::getTimeout () const noexcept
|
||||
{
|
||||
return mTimeout;
|
||||
}
|
||||
|
||||
Request& Request::appendCookies (const CookiesList& list)
|
||||
{
|
||||
for (const Cookie& cookie : list)
|
||||
{
|
||||
if (!cookie.isExpired())
|
||||
mCookies.setCookie (cookie);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
75
libraries/lib-network-manager/Request.h
Normal file
75
libraries/lib-network-manager/Request.h
Normal file
@ -0,0 +1,75 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file Request.h
|
||||
@brief Declare a class for constructing HTTP requests.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <numeric>
|
||||
|
||||
#include "NetworkManagerApi.h"
|
||||
|
||||
#include "HeadersList.h"
|
||||
#include "CookiesList.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class NETWORK_MANAGER_API Request final
|
||||
{
|
||||
public:
|
||||
using Timeout = std::chrono::milliseconds;
|
||||
|
||||
static constexpr size_t INFINITE_REDIRECTS = std::numeric_limits<size_t>::max();
|
||||
|
||||
Request() = default;
|
||||
explicit Request(std::string url) noexcept;
|
||||
|
||||
Request(const Request&) = default;
|
||||
Request(Request&&) = default;
|
||||
Request& operator = (const Request&) = default;
|
||||
Request& operator = (Request&&) = default;
|
||||
|
||||
Request& setURL(std::string url) noexcept;
|
||||
const std::string& getURL() const noexcept;
|
||||
|
||||
Request& setHeader(const std::string& name, std::string value);
|
||||
std::string getHeader(const std::string& name) const;
|
||||
|
||||
const HeadersList& getHeaders () const noexcept;
|
||||
|
||||
Request& setCookie(const std::string& name, std::string value);
|
||||
Request& appendCookies (const CookiesList& list);
|
||||
|
||||
std::string getCookie(const std::string& name) const;
|
||||
|
||||
const CookiesList& getCookies () noexcept;
|
||||
|
||||
Request& setMaxRedirects(size_t redirects) noexcept;
|
||||
size_t getMaxRedirects() const noexcept;
|
||||
|
||||
|
||||
Request& setTimeout(Timeout timeout) noexcept;
|
||||
Timeout getTimeout() const noexcept;
|
||||
private:
|
||||
std::string mUrl;
|
||||
|
||||
HeadersList mHeaders;
|
||||
CookiesList mCookies;
|
||||
|
||||
size_t mMaxRedirects { INFINITE_REDIRECTS };
|
||||
|
||||
Timeout mTimeout { std::chrono::seconds (5) };
|
||||
};
|
||||
|
||||
}
|
||||
}
|
349
libraries/lib-network-manager/curl/CurlHandleManager.cpp
Normal file
349
libraries/lib-network-manager/curl/CurlHandleManager.cpp
Normal file
@ -0,0 +1,349 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlHandleManager.cpp
|
||||
@brief Define a class responsible for reuse of CURL hanldes.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "CurlHandleManager.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
|
||||
#include <wx/platinfo.h>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
void GetOSString (std::ostringstream& output, const wxPlatformInfo& platformInfo)
|
||||
{
|
||||
const wxOperatingSystemId osID = platformInfo.GetOperatingSystemId ();
|
||||
|
||||
if (osID & wxOS_WINDOWS)
|
||||
output << "Windows ";
|
||||
else if (osID & wxOS_MAC)
|
||||
output << "MacOS ";
|
||||
else if (osID & wxOS_UNIX_LINUX)
|
||||
output << "Linux ";
|
||||
else if (osID & wxOS_UNIX_FREEBSD)
|
||||
output << "FreeBSD ";
|
||||
else if (osID & wxOS_UNIX_OPENBSD)
|
||||
output << "OpenBSD ";
|
||||
else
|
||||
output << "Other ";
|
||||
|
||||
output <<
|
||||
platformInfo.GetOSMajorVersion () <<
|
||||
"_" <<
|
||||
platformInfo.GetOSMinorVersion () <<
|
||||
"_" <<
|
||||
platformInfo.GetOSMicroVersion() <<
|
||||
"; ";
|
||||
|
||||
#if defined(__amd64__) || defined(__x86_64__) || defined(_M_X64)
|
||||
output << "x64";
|
||||
#elif defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(_X86_) || defined(__THW_INTEL)
|
||||
output << "x86";
|
||||
#elif
|
||||
output << "arm64";
|
||||
#elif defined(arm) || defined(__arm__) || defined(ARM) || defined(_ARM_)
|
||||
output << "arm";
|
||||
#else
|
||||
output << "unknown";
|
||||
#endif
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
constexpr std::chrono::milliseconds CurlHandleManager::KEEP_ALIVE_IDLE;
|
||||
constexpr std::chrono::milliseconds CurlHandleManager::KEEP_ALIVE_PROBE;
|
||||
|
||||
CurlHandleManager::Handle::Handle(CurlHandleManager* owner, CURL* handle, RequestVerb verb, std::string url) noexcept
|
||||
: mHandle (handle),
|
||||
mOwner (owner),
|
||||
mVerb (verb),
|
||||
mUrl (std::move (url)),
|
||||
mHandleFromCache (handle != nullptr)
|
||||
{
|
||||
if (mHandle == nullptr)
|
||||
mHandle = curl_easy_init ();
|
||||
|
||||
setOption (CURLOPT_URL, mUrl);
|
||||
|
||||
switch (verb)
|
||||
{
|
||||
case RequestVerb::Head:
|
||||
setOption (CURLOPT_NOBODY, 1);
|
||||
break;
|
||||
case RequestVerb::Get:
|
||||
// This is a default, no additional setup is needed
|
||||
// We cache handles by the verb, so there is no need to
|
||||
// reset the handle state
|
||||
break;
|
||||
case RequestVerb::Post:
|
||||
setOption (CURLOPT_POST, 1);
|
||||
break;
|
||||
case RequestVerb::Put:
|
||||
setOption (CURLOPT_UPLOAD, 1);
|
||||
break;
|
||||
case RequestVerb::Delete:
|
||||
setOption (CURLOPT_CUSTOMREQUEST, "DELETE");
|
||||
break;
|
||||
}
|
||||
|
||||
setOption (CURLOPT_NOSIGNAL, 1L);
|
||||
|
||||
setOption (CURLOPT_SSL_VERIFYPEER, 1L);
|
||||
setOption (CURLOPT_SSL_VERIFYHOST, 2L);
|
||||
|
||||
setOption (CURLOPT_ACCEPT_ENCODING, "");
|
||||
}
|
||||
|
||||
CurlHandleManager::Handle::Handle (Handle&& rhs) noexcept
|
||||
{
|
||||
*this = std::move (rhs);
|
||||
}
|
||||
|
||||
CurlHandleManager::Handle::~Handle () noexcept
|
||||
{
|
||||
if (mReuse)
|
||||
mOwner->cacheHandle (*this);
|
||||
else
|
||||
curl_easy_cleanup (mHandle);
|
||||
}
|
||||
|
||||
CurlHandleManager::Handle& CurlHandleManager::Handle::operator=(Handle&& rhs) noexcept
|
||||
{
|
||||
std::swap (mHandle, rhs.mHandle);
|
||||
std::swap (mOwner, rhs.mOwner);
|
||||
|
||||
std::swap (mVerb, rhs.mVerb);
|
||||
|
||||
mUrl = std::move (rhs.mUrl);
|
||||
mHeaders = std::move (rhs.mHeaders);
|
||||
|
||||
mReuse = rhs.mReuse;
|
||||
rhs.mReuse = false;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
CURLcode CurlHandleManager::Handle::setOption (CURLoption option, const std::string& value) noexcept
|
||||
{
|
||||
return setOption (option, value.c_str ());
|
||||
}
|
||||
|
||||
CURLcode CurlHandleManager::Handle::appendCookie (const Cookie& cookie) noexcept
|
||||
{
|
||||
return setOption(CURLOPT_COOKIE, "Set-Cookie: " + cookie.Name + "=" + cookie.Value);
|
||||
}
|
||||
|
||||
CURLcode CurlHandleManager::Handle::appendCookies (const CookiesList& cookies)noexcept
|
||||
{
|
||||
for (const Cookie& cookie : cookies)
|
||||
{
|
||||
const CURLcode result = appendCookie (cookie);
|
||||
|
||||
if (result != CURLE_OK)
|
||||
return result;
|
||||
}
|
||||
|
||||
return CURLE_OK;
|
||||
}
|
||||
|
||||
void CurlHandleManager::Handle::appendHeader (const Header& header)
|
||||
{
|
||||
if (header.hasSameName ("User-Agent"))
|
||||
mUserAgentSet = true;
|
||||
|
||||
mHeaders.append(header.Name + ": " + header.Value);
|
||||
}
|
||||
|
||||
void CurlHandleManager::Handle::appendHeaders (const HeadersList& headers)
|
||||
{
|
||||
for (const Header& header : headers)
|
||||
appendHeader (header);
|
||||
}
|
||||
|
||||
CurlHandleManager::Handle::Result CurlHandleManager::Handle::perform ()
|
||||
{
|
||||
if (!mUserAgentSet)
|
||||
mHeaders.append ("User-Agent: " + mOwner->getUserAgent ());
|
||||
|
||||
CURLcode result = setOption (CURLOPT_HTTPHEADER, mHeaders.getCurlList ());
|
||||
|
||||
if (result != CURLE_OK)
|
||||
return { result, std::string () };
|
||||
|
||||
char currentError[CURL_ERROR_SIZE] = {};
|
||||
setOption(CURLOPT_ERRORBUFFER, currentError);
|
||||
|
||||
result = curl_easy_perform (mHandle);
|
||||
|
||||
mReuse = mReuse && result == CURLE_OK;
|
||||
|
||||
return { result, std::string (currentError) };
|
||||
}
|
||||
|
||||
void CurlHandleManager::Handle::markKeepAlive ()
|
||||
{
|
||||
mReuse = true;
|
||||
}
|
||||
|
||||
bool CurlHandleManager::Handle::isHandleFromCache () const noexcept
|
||||
{
|
||||
return mHandleFromCache;
|
||||
}
|
||||
|
||||
unsigned CurlHandleManager::Handle::getHTTPCode () const noexcept
|
||||
{
|
||||
long code = 0;
|
||||
|
||||
if (CURLE_OK != curl_easy_getinfo(mHandle, CURLINFO_RESPONSE_CODE, &code))
|
||||
return 0;
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
void CurlHandleManager::Handle::reset () noexcept
|
||||
{
|
||||
setOption (CURLOPT_COOKIELIST, nullptr);
|
||||
setOption (CURLOPT_PROXY, nullptr);
|
||||
setOption (CURLOPT_SSL_OPTIONS, 0);
|
||||
|
||||
mUserAgentSet = false;
|
||||
}
|
||||
|
||||
CurlHandleManager::CurlHandleManager ()
|
||||
{
|
||||
std::ostringstream ss;
|
||||
|
||||
ss << "Audacity/" <<
|
||||
AUDACITY_VERSION << "." <<
|
||||
AUDACITY_RELEASE << "." <<
|
||||
AUDACITY_REVISION <<
|
||||
" (";
|
||||
|
||||
GetOSString (ss, wxPlatformInfo::Get ());
|
||||
|
||||
ss << ")";
|
||||
|
||||
mUserAgent = ss.str ();
|
||||
}
|
||||
|
||||
CurlHandleManager::~CurlHandleManager ()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHandleCacheLock);
|
||||
|
||||
for (auto& cachedHandle : mHandleCache)
|
||||
curl_easy_cleanup (cachedHandle.Handle);
|
||||
}
|
||||
|
||||
void CurlHandleManager::setProxy (std::string proxy)
|
||||
{
|
||||
mProxy = std::move (proxy);
|
||||
}
|
||||
|
||||
CurlHandleManager::Handle CurlHandleManager::getHandle (RequestVerb verb, const std::string& url)
|
||||
{
|
||||
Handle handle (this, getCurlHandleFromCache (verb, url), verb, url);
|
||||
|
||||
if (!mProxy.empty ())
|
||||
{
|
||||
handle.setOption (CURLOPT_PROXY, mProxy);
|
||||
// If we use proxy, checking the CRL will likely break the SSL proxying
|
||||
handle.setOption (CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
|
||||
}
|
||||
|
||||
handle.setOption (CURLOPT_TCP_KEEPALIVE, 1L);
|
||||
|
||||
handle.setOption (CURLOPT_TCP_KEEPIDLE,
|
||||
std::chrono::duration_cast<std::chrono::seconds> (KEEP_ALIVE_IDLE).count ()
|
||||
);
|
||||
|
||||
handle.setOption (CURLOPT_TCP_KEEPINTVL,
|
||||
std::chrono::duration_cast<std::chrono::seconds> (KEEP_ALIVE_PROBE).count ()
|
||||
);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
std::string CurlHandleManager::getUserAgent () const
|
||||
{
|
||||
return mUserAgent;
|
||||
}
|
||||
|
||||
CURL* CurlHandleManager::getCurlHandleFromCache (RequestVerb verb, const std::string& url)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHandleCacheLock);
|
||||
|
||||
cleanupHandlesCache ();
|
||||
|
||||
const std::string schemeAndDomain = GetSchemeAndDomain (url);
|
||||
|
||||
auto it = std::find_if (mHandleCache.begin (), mHandleCache.end (), [verb, schemeAndDomain](const CachedHandle& handle) {
|
||||
return handle.Verb == verb && handle.SchemeAndDomain == schemeAndDomain;
|
||||
});
|
||||
|
||||
if (it == mHandleCache.end ())
|
||||
return nullptr;
|
||||
|
||||
CURL* handle = it->Handle;
|
||||
|
||||
mHandleCache.erase (it);
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
void CurlHandleManager::cacheHandle (Handle& handle)
|
||||
{
|
||||
// Reset the state to the safe defaults
|
||||
handle.reset ();
|
||||
|
||||
std::lock_guard<std::mutex> lock (mHandleCacheLock);
|
||||
|
||||
cleanupHandlesCache ();
|
||||
|
||||
mHandleCache.push_back ({
|
||||
handle.mVerb,
|
||||
GetSchemeAndDomain (handle.mUrl),
|
||||
handle.mHandle,
|
||||
RequestClock::now ()
|
||||
});
|
||||
}
|
||||
|
||||
void CurlHandleManager::cleanupHandlesCache ()
|
||||
{
|
||||
const RequestTimePoint timePoint = RequestClock::now ();
|
||||
|
||||
mHandleCache.erase (std::remove_if (mHandleCache.begin (), mHandleCache.end (), [timePoint](const CachedHandle& cachedHandle) {
|
||||
return (timePoint - cachedHandle.RequestTime) >= KEEP_ALIVE_IDLE;
|
||||
}), mHandleCache.end ());
|
||||
}
|
||||
|
||||
std::string CurlHandleManager::GetSchemeAndDomain (const std::string& url)
|
||||
{
|
||||
const size_t schemeEndPosition = url.find ("://");
|
||||
|
||||
if (schemeEndPosition == std::string::npos) // Is url even valid?
|
||||
return url;
|
||||
|
||||
const size_t domainStartPosition = schemeEndPosition + 3;
|
||||
|
||||
const size_t slashLocation = url.find ('/', domainStartPosition);
|
||||
|
||||
if (slashLocation == std::string::npos)
|
||||
return url;
|
||||
|
||||
return url.substr (domainStartPosition, slashLocation - domainStartPosition);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
127
libraries/lib-network-manager/curl/CurlHandleManager.h
Normal file
127
libraries/lib-network-manager/curl/CurlHandleManager.h
Normal file
@ -0,0 +1,127 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlHandleManager.h
|
||||
@brief Declare a class responsible for reuse of CURL hanldes.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
#include "CurlStringList.h"
|
||||
#include "../IResponseFactory.h"
|
||||
|
||||
#include "../CookiesList.h"
|
||||
#include "../HeadersList.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class CurlHandleManager final
|
||||
{
|
||||
public:
|
||||
class Handle final
|
||||
{
|
||||
friend class CurlHandleManager;
|
||||
Handle (CurlHandleManager* owner, CURL* handle, RequestVerb verb, std::string url) noexcept;
|
||||
public:
|
||||
Handle (Handle&& rhs) noexcept;
|
||||
~Handle () noexcept;
|
||||
|
||||
Handle& operator = (Handle&& rhs) noexcept;
|
||||
|
||||
template<typename... Args>
|
||||
CURLcode setOption (CURLoption option, Args... value) noexcept
|
||||
{
|
||||
return curl_easy_setopt (mHandle, option, value...);
|
||||
}
|
||||
|
||||
CURLcode setOption (CURLoption option, const std::string& value) noexcept;
|
||||
|
||||
CURLcode appendCookie (const Cookie& cookie) noexcept;
|
||||
CURLcode appendCookies (const CookiesList& cookie) noexcept;
|
||||
|
||||
void appendHeader (const Header& header);
|
||||
void appendHeaders (const HeadersList& headers);
|
||||
|
||||
struct Result final
|
||||
{
|
||||
CURLcode Code;
|
||||
std::string Message;
|
||||
};
|
||||
|
||||
Result perform ();
|
||||
|
||||
void markKeepAlive ();
|
||||
|
||||
bool isHandleFromCache () const noexcept;
|
||||
|
||||
unsigned getHTTPCode () const noexcept;
|
||||
|
||||
void reset () noexcept;
|
||||
private:
|
||||
CURL* mHandle { nullptr };
|
||||
CurlHandleManager* mOwner { nullptr };
|
||||
|
||||
RequestVerb mVerb;
|
||||
std::string mUrl;
|
||||
|
||||
CurlStringList mHeaders;
|
||||
|
||||
bool mUserAgentSet { false };
|
||||
bool mReuse { false };
|
||||
bool mHandleFromCache { false };
|
||||
};
|
||||
|
||||
CurlHandleManager ();
|
||||
~CurlHandleManager ();
|
||||
|
||||
void setProxy (std::string proxy);
|
||||
|
||||
Handle getHandle (RequestVerb verb, const std::string& url);
|
||||
private:
|
||||
using RequestClock = std::chrono::steady_clock;
|
||||
using RequestTimePoint = RequestClock::time_point;
|
||||
|
||||
static constexpr std::chrono::milliseconds KEEP_ALIVE_IDLE { std::chrono::seconds (120) };
|
||||
static constexpr std::chrono::milliseconds KEEP_ALIVE_PROBE { std::chrono::seconds (60) };
|
||||
|
||||
struct CachedHandle final
|
||||
{
|
||||
RequestVerb Verb;
|
||||
std::string SchemeAndDomain;
|
||||
|
||||
CURL* Handle { nullptr };
|
||||
|
||||
RequestTimePoint RequestTime;
|
||||
};
|
||||
|
||||
std::string getUserAgent () const;
|
||||
|
||||
CURL* getCurlHandleFromCache (RequestVerb verb, const std::string& url);
|
||||
void cacheHandle (Handle& handle);
|
||||
|
||||
void cleanupHandlesCache ();
|
||||
|
||||
static std::string GetSchemeAndDomain (const std::string& url);
|
||||
|
||||
std::string mProxy;
|
||||
std::string mUserAgent;
|
||||
|
||||
std::mutex mHandleCacheLock;
|
||||
std::vector<CachedHandle> mHandleCache;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
381
libraries/lib-network-manager/curl/CurlResponse.cpp
Normal file
381
libraries/lib-network-manager/curl/CurlResponse.cpp
Normal file
@ -0,0 +1,381 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlResponse.cpp
|
||||
@brief Define an implementation of IResponse using libcurl.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "CurlResponse.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
namespace
|
||||
{
|
||||
|
||||
static const std::map<CURLcode, NetworkError> errorsMap = {
|
||||
{ CURLE_OK, NetworkError::NoError },
|
||||
{ CURLE_URL_MALFORMAT, NetworkError::BadURL },
|
||||
{ CURLE_COULDNT_RESOLVE_PROXY, NetworkError::ProxyNotFound },
|
||||
{ CURLE_COULDNT_RESOLVE_HOST, NetworkError::HostNotFound },
|
||||
{ CURLE_COULDNT_CONNECT, NetworkError::ConnectionRefused },
|
||||
{ CURLE_HTTP_RETURNED_ERROR, NetworkError::HTTPError },
|
||||
{ CURLE_WRITE_ERROR, NetworkError::OperationCancelled },
|
||||
{ CURLE_READ_ERROR, NetworkError::OperationCancelled },
|
||||
{ CURLE_OPERATION_TIMEDOUT, NetworkError::Timeout },
|
||||
{ CURLE_RANGE_ERROR, NetworkError::HTTPError },
|
||||
{ CURLE_HTTP_POST_ERROR, NetworkError::HTTPError },
|
||||
{ CURLE_SSL_CONNECT_ERROR, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_ABORTED_BY_CALLBACK, NetworkError::OperationCancelled },
|
||||
{ CURLE_TOO_MANY_REDIRECTS, NetworkError::OperationCancelled },
|
||||
{ CURLE_PEER_FAILED_VERIFICATION, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_GOT_NOTHING, NetworkError::RemoteHostClosed },
|
||||
{ CURLE_SSL_ENGINE_NOTFOUND, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_ENGINE_SETFAILED, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SEND_ERROR, NetworkError::RemoteHostClosed },
|
||||
{ CURLE_RECV_ERROR, NetworkError::RemoteHostClosed },
|
||||
{ CURLE_SSL_CERTPROBLEM, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_CIPHER, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_CACERT, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_USE_SSL_FAILED, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_ENGINE_INITFAILED, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_CACERT_BADFILE, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_SHUTDOWN_FAILED, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_CRL_BADFILE, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_ISSUER_ERROR, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_CHUNK_FAILED, NetworkError::HTTPError },
|
||||
{ CURLE_NO_CONNECTION_AVAILABLE, NetworkError::ConnectionFailed },
|
||||
{ CURLE_SSL_PINNEDPUBKEYNOTMATCH, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_SSL_INVALIDCERTSTATUS, NetworkError::SSLHandshakeFailed },
|
||||
{ CURLE_PARTIAL_FILE, NetworkError::RemoteHostClosed }
|
||||
};
|
||||
|
||||
struct DataStream final
|
||||
{
|
||||
const char* Buffer;
|
||||
size_t Size;
|
||||
|
||||
size_t Offset { 0 };
|
||||
};
|
||||
|
||||
size_t DataStreamRead (char* ptr, size_t size, size_t nmemb, DataStream* stream) noexcept
|
||||
{
|
||||
size = std::min (size * nmemb, stream->Size - stream->Offset);
|
||||
|
||||
const char* start = stream->Buffer + stream->Offset;
|
||||
const char* end = start + size;
|
||||
|
||||
std::copy (start, end, ptr);
|
||||
|
||||
stream->Offset += size;
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
int DataStreamSeek (DataStream* stream, curl_off_t offs, int origin) noexcept
|
||||
{
|
||||
int64_t offset = offs;
|
||||
|
||||
switch (origin)
|
||||
{
|
||||
case SEEK_CUR:
|
||||
offset += stream->Offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
offset += stream->Size;
|
||||
break;
|
||||
}
|
||||
|
||||
if (offset < 0 || offset >= stream->Size)
|
||||
return CURL_SEEKFUNC_FAIL;
|
||||
|
||||
stream->Offset = offset;
|
||||
|
||||
return CURL_SEEKFUNC_OK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
CurlResponse::CurlResponse (RequestVerb verb, const Request& request, CurlHandleManager* handleManager) noexcept
|
||||
: mVerb(verb),
|
||||
mRequest(request),
|
||||
mHandleManager (handleManager)
|
||||
{
|
||||
}
|
||||
|
||||
bool CurlResponse::isFinished () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
return mRequestFinished;
|
||||
}
|
||||
|
||||
unsigned CurlResponse::getHTTPCode () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
return mHttpCode;
|
||||
}
|
||||
|
||||
NetworkError CurlResponse::getError () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
return mNetworkError;
|
||||
}
|
||||
|
||||
std::string CurlResponse::getErrorString () const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
return mErrorString;
|
||||
}
|
||||
|
||||
bool CurlResponse::headersReceived () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
return mHeadersReceived;
|
||||
}
|
||||
|
||||
bool CurlResponse::hasHeader (const std::string& headerName) const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHeadersMutex);
|
||||
return mResponseHeaders.hasHeader (headerName);
|
||||
}
|
||||
|
||||
std::string CurlResponse::getHeader (const std::string& headerName) const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHeadersMutex);
|
||||
return mResponseHeaders.getHeaderValue (headerName);
|
||||
}
|
||||
|
||||
const HeadersList& CurlResponse::getHeaders () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHeadersMutex);
|
||||
return mResponseHeaders;
|
||||
}
|
||||
|
||||
const CookiesList& CurlResponse::getCookies () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mHeadersMutex);
|
||||
return mResponseCookies;
|
||||
}
|
||||
|
||||
const Request& CurlResponse::getRequest () const noexcept
|
||||
{
|
||||
return mRequest;
|
||||
}
|
||||
|
||||
std::string CurlResponse::getURL () const
|
||||
{
|
||||
return mRequest.getURL ();
|
||||
}
|
||||
|
||||
void CurlResponse::abort () noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
mAbortRequested = true;
|
||||
}
|
||||
|
||||
void CurlResponse::setOnDataReceivedCallback (RequestCallback callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mCallbackMutex);
|
||||
|
||||
mOnDataReceivedCallback = std::move (callback);
|
||||
|
||||
if (mOnDataReceivedCallback && getBytesAvailable () > 0)
|
||||
mOnDataReceivedCallback (this);
|
||||
}
|
||||
|
||||
void CurlResponse::setRequestFinishedCallback (RequestCallback callback)
|
||||
{
|
||||
std::lock_guard<std::mutex> callbackLock (mCallbackMutex);
|
||||
|
||||
mRequestFinishedCallback = std::move (callback);
|
||||
|
||||
std::lock_guard<std::mutex> statusLock (mStatusMutex);
|
||||
|
||||
if (mRequestFinishedCallback && mRequestFinished)
|
||||
mRequestFinishedCallback (this);
|
||||
}
|
||||
|
||||
uint64_t CurlResponse::getBytesAvailable () const noexcept
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mDataBufferMutex);
|
||||
return mDataBuffer.size ();
|
||||
}
|
||||
|
||||
uint64_t CurlResponse::readData (void* buffer, uint64_t maxBytesCount)
|
||||
{
|
||||
if (buffer == nullptr || maxBytesCount == 0)
|
||||
return 0;
|
||||
|
||||
std::lock_guard<std::mutex> lock (mDataBufferMutex);
|
||||
|
||||
if (mDataBuffer.empty ())
|
||||
return 0;
|
||||
|
||||
maxBytesCount = std::min<uint64_t> (maxBytesCount, mDataBuffer.size ());
|
||||
|
||||
const auto begin = mDataBuffer.begin ();
|
||||
const auto end = begin + maxBytesCount;
|
||||
|
||||
std::copy (begin, end, static_cast<uint8_t*> (buffer));
|
||||
|
||||
mDataBuffer.erase (begin, end);
|
||||
|
||||
return maxBytesCount;
|
||||
}
|
||||
|
||||
void CurlResponse::perform (const void* ptr, size_t size)
|
||||
{
|
||||
CurlHandleManager::Handle handle = mHandleManager->getHandle (mVerb, mRequest.getURL ());
|
||||
|
||||
handle.setOption (CURLOPT_WRITEFUNCTION, WriteCallback);
|
||||
handle.setOption (CURLOPT_WRITEDATA, this);
|
||||
|
||||
handle.setOption (CURLOPT_HEADERFUNCTION, HeaderCallback);
|
||||
handle.setOption (CURLOPT_HEADERDATA, this);
|
||||
|
||||
handle.setOption (CURLOPT_FOLLOWLOCATION, mRequest.getMaxRedirects () == 0 ? 0 : 1);
|
||||
handle.setOption (CURLOPT_MAXREDIRS, mRequest.getMaxRedirects ());
|
||||
|
||||
handle.setOption (CURLOPT_CONNECTTIMEOUT_MS,
|
||||
std::chrono::duration_cast<std::chrono::milliseconds> (mRequest.getTimeout()).count ()
|
||||
);
|
||||
|
||||
handle.appendCookies (mRequest.getCookies ());
|
||||
|
||||
DataStream ds { reinterpret_cast<const char*>(ptr), size };
|
||||
|
||||
if (ptr != nullptr && size != 0)
|
||||
{
|
||||
handle.appendHeader ({ "Transfer-Encoding", std::string () });
|
||||
handle.appendHeader ({ "Content-Length", std::to_string (size) });
|
||||
|
||||
if (mVerb == RequestVerb::Post)
|
||||
handle.setOption (CURLOPT_POSTFIELDSIZE_LARGE, size);
|
||||
else
|
||||
handle.setOption (CURLOPT_INFILESIZE_LARGE, size);
|
||||
|
||||
handle.setOption (CURLOPT_READFUNCTION, DataStreamRead);
|
||||
handle.setOption (CURLOPT_READDATA, &ds);
|
||||
|
||||
handle.setOption (CURLOPT_SEEKFUNCTION, DataStreamSeek);
|
||||
handle.setOption (CURLOPT_SEEKDATA, &ds);
|
||||
}
|
||||
|
||||
handle.appendHeaders (mRequest.getHeaders ());
|
||||
|
||||
mCurrentHandle = &handle;
|
||||
const auto result = handle.perform ();
|
||||
mCurrentHandle = nullptr;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (mStatusMutex);
|
||||
|
||||
if (result.Code != CURLE_OK)
|
||||
{
|
||||
const auto it = errorsMap.find (result.Code);
|
||||
|
||||
mNetworkError = it != errorsMap.end () ? it->second : NetworkError::UnknownError;
|
||||
mErrorString = result.Message;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (mHttpCode == 0)
|
||||
mHttpCode = handle.getHTTPCode ();
|
||||
}
|
||||
|
||||
mRequestFinished = true;
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock (mCallbackMutex);
|
||||
|
||||
if (mRequestFinishedCallback)
|
||||
mRequestFinishedCallback (this);
|
||||
|
||||
mRequestFinishedCallback = {};
|
||||
mOnDataReceivedCallback = {};
|
||||
}
|
||||
|
||||
|
||||
size_t CurlResponse::WriteCallback (const uint8_t* ptr, size_t size, size_t nmemb, CurlResponse* request) noexcept
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (request->mStatusMutex);
|
||||
|
||||
if (request->mAbortRequested)
|
||||
return 0;
|
||||
|
||||
if (!request->mHeadersReceived)
|
||||
{
|
||||
request->mHeadersReceived = true;
|
||||
|
||||
// WriteCallback is called by the handle
|
||||
assert (request->mCurrentHandle != nullptr);
|
||||
|
||||
if (request->mCurrentHandle != nullptr && request->mHttpCode == 0)
|
||||
request->mHttpCode = request->mCurrentHandle->getHTTPCode ();
|
||||
}
|
||||
}
|
||||
|
||||
size *= nmemb;
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (request->mDataBufferMutex);
|
||||
request->mDataBuffer.insert (request->mDataBuffer.end (), ptr, ptr + size);
|
||||
}
|
||||
|
||||
std::lock_guard<std::mutex> lock (request->mCallbackMutex);
|
||||
|
||||
if (request->mOnDataReceivedCallback)
|
||||
request->mOnDataReceivedCallback (request);
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
size_t CurlResponse::HeaderCallback (const char* buffer, size_t size, size_t nitems, CurlResponse* request) noexcept
|
||||
{
|
||||
{
|
||||
std::lock_guard<std::mutex> lock (request->mStatusMutex);
|
||||
|
||||
if (request->mAbortRequested)
|
||||
return 0;
|
||||
|
||||
// HeaderCallback is called by the handle
|
||||
assert (request->mCurrentHandle != nullptr);
|
||||
|
||||
if (request->mCurrentHandle != nullptr && request->mHttpCode == 0)
|
||||
request->mHttpCode = request->mCurrentHandle->getHTTPCode ();
|
||||
}
|
||||
|
||||
size = size * nitems;
|
||||
|
||||
if (size < 2)
|
||||
return 0;
|
||||
|
||||
const Header header = Header::Parse (std::string (buffer, size - 2));
|
||||
|
||||
std::lock_guard<std::mutex> lock (request->mHeadersMutex);
|
||||
|
||||
if (header.hasSameName ("Set-Cookie"))
|
||||
{
|
||||
request->mResponseCookies.addCookie (Cookie::Parse (header.Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
if (header.hasSameName ("Keep-Alive"))
|
||||
request->mCurrentHandle->markKeepAlive ();
|
||||
|
||||
request->mResponseHeaders.addHeader (header);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
98
libraries/lib-network-manager/curl/CurlResponse.h
Normal file
98
libraries/lib-network-manager/curl/CurlResponse.h
Normal file
@ -0,0 +1,98 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlResponse.h
|
||||
@brief Declare an implementation of IResponse using libcurl.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <deque>
|
||||
#include <mutex>
|
||||
#include <cstdint>
|
||||
|
||||
#include "../IResponse.h"
|
||||
|
||||
#include "../HeadersList.h"
|
||||
#include "../CookiesList.h"
|
||||
#include "../Request.h"
|
||||
|
||||
#include "CurlHandleManager.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class CurlResponse final : public IResponse
|
||||
{
|
||||
public:
|
||||
CurlResponse (RequestVerb verb, const Request& request, CurlHandleManager* handleManager) noexcept;
|
||||
|
||||
bool isFinished () const noexcept override;
|
||||
unsigned getHTTPCode () const noexcept override;
|
||||
|
||||
NetworkError getError () const noexcept override;
|
||||
std::string getErrorString () const override;
|
||||
|
||||
bool headersReceived () const noexcept override;
|
||||
|
||||
bool hasHeader (const std::string& headerName) const noexcept override;
|
||||
std::string getHeader (const std::string& headerName) const override;
|
||||
|
||||
const HeadersList& getHeaders () const noexcept override;
|
||||
const CookiesList& getCookies () const noexcept override;
|
||||
|
||||
const Request& getRequest () const noexcept override;
|
||||
|
||||
std::string getURL () const override;
|
||||
|
||||
void abort () noexcept override;
|
||||
|
||||
void setOnDataReceivedCallback (RequestCallback callback) override;
|
||||
|
||||
void setRequestFinishedCallback (RequestCallback callback) override;
|
||||
|
||||
uint64_t getBytesAvailable () const noexcept override;
|
||||
uint64_t readData (void* buffer, uint64_t maxBytesCount) override;
|
||||
|
||||
void perform (const void* ptr, size_t size);
|
||||
private:
|
||||
RequestVerb mVerb;
|
||||
|
||||
Request mRequest;
|
||||
CurlHandleManager* mHandleManager;
|
||||
|
||||
CurlHandleManager::Handle* mCurrentHandle { nullptr };
|
||||
|
||||
mutable std::mutex mCallbackMutex;
|
||||
RequestCallback mOnDataReceivedCallback;
|
||||
RequestCallback mRequestFinishedCallback;
|
||||
|
||||
mutable std::mutex mHeadersMutex;
|
||||
HeadersList mResponseHeaders;
|
||||
CookiesList mResponseCookies;
|
||||
|
||||
mutable std::mutex mDataBufferMutex;
|
||||
std::deque<uint8_t> mDataBuffer;
|
||||
|
||||
mutable std::mutex mStatusMutex;
|
||||
|
||||
NetworkError mNetworkError { NetworkError::NoError };
|
||||
std::string mErrorString;
|
||||
|
||||
unsigned mHttpCode { 0 };
|
||||
|
||||
bool mHeadersReceived { false };
|
||||
bool mRequestFinished { false };
|
||||
bool mAbortRequested { false };
|
||||
|
||||
static size_t WriteCallback (const uint8_t* ptr, size_t size, size_t nmemb, CurlResponse* userdata) noexcept;
|
||||
static size_t HeaderCallback (const char* buffer, size_t size, size_t nitems, CurlResponse* userdata) noexcept;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
80
libraries/lib-network-manager/curl/CurlResponseFactory.cpp
Normal file
80
libraries/lib-network-manager/curl/CurlResponseFactory.cpp
Normal file
@ -0,0 +1,80 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlResponseFactory.cpp
|
||||
@brief Define an implementation of IResponseFactory using libcurl.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "CurlResponseFactory.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "CurlResponse.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
constexpr decltype(std::thread::hardware_concurrency ()) MIN_CURL_THREADS = 6;
|
||||
|
||||
CurlResponseFactory::CurlResponseFactory ()
|
||||
: mThreadPool (std::make_unique<ThreadPool>(
|
||||
std::max (
|
||||
MIN_CURL_THREADS,
|
||||
std::thread::hardware_concurrency ()
|
||||
)))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void CurlResponseFactory::setProxy (const std::string& proxy)
|
||||
{
|
||||
mHandleManager->setProxy (proxy);
|
||||
}
|
||||
|
||||
ResponsePtr CurlResponseFactory::performRequest (RequestVerb verb, const Request& request)
|
||||
{
|
||||
return performRequest (verb, request, nullptr, 0);
|
||||
}
|
||||
|
||||
ResponsePtr CurlResponseFactory::performRequest (RequestVerb verb, const Request& request, const void* data, size_t size)
|
||||
{
|
||||
if (!mThreadPool)
|
||||
return {};
|
||||
|
||||
std::shared_ptr<CurlResponse> response = std::make_shared<CurlResponse> (
|
||||
verb, request, mHandleManager.get ()
|
||||
);
|
||||
|
||||
std::vector<uint8_t> buffer;
|
||||
|
||||
if (data != nullptr && size != 0)
|
||||
{
|
||||
const uint8_t* start = static_cast<const uint8_t*>(data);
|
||||
const uint8_t* end = static_cast<const uint8_t*>(data) + size;
|
||||
|
||||
buffer.insert (buffer.begin (), start, end);
|
||||
}
|
||||
|
||||
mThreadPool->enqueue ([response, dataBuffer = std::move (buffer)]() {
|
||||
if (!dataBuffer.empty())
|
||||
response->perform (dataBuffer.data (), dataBuffer.size ());
|
||||
else
|
||||
response->perform (nullptr, 0);
|
||||
});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
void CurlResponseFactory::terminate ()
|
||||
{
|
||||
mThreadPool.reset ();
|
||||
mHandleManager.reset ();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
46
libraries/lib-network-manager/curl/CurlResponseFactory.h
Normal file
46
libraries/lib-network-manager/curl/CurlResponseFactory.h
Normal file
@ -0,0 +1,46 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlResponseFactory.h
|
||||
@brief Declare an implementation of IResponseFactory using libcurl.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
|
||||
#include "../IResponseFactory.h"
|
||||
|
||||
#include "CurlHandleManager.h"
|
||||
#include "ThreadPool/ThreadPool.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class CurlResponseFactory final : public IResponseFactory
|
||||
{
|
||||
public:
|
||||
CurlResponseFactory ();
|
||||
|
||||
void setProxy (const std::string& proxy) override;
|
||||
|
||||
ResponsePtr performRequest (RequestVerb verb, const Request& request) override;
|
||||
ResponsePtr performRequest (RequestVerb verb, const Request& request, const void* data, size_t size) override;
|
||||
|
||||
void terminate () override;
|
||||
|
||||
private:
|
||||
std::unique_ptr<CurlHandleManager> mHandleManager { std::make_unique<CurlHandleManager> () };
|
||||
std::unique_ptr<ThreadPool> mThreadPool;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
54
libraries/lib-network-manager/curl/CurlStringList.cpp
Normal file
54
libraries/lib-network-manager/curl/CurlStringList.cpp
Normal file
@ -0,0 +1,54 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlStringList.cpp
|
||||
@brief Define a RAII wrapper for the curl_slist.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "CurlStringList.h"
|
||||
|
||||
#include <curl/curl.h>
|
||||
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
CurlStringList::CurlStringList (CurlStringList&& rhs) noexcept
|
||||
: mList (rhs.mList)
|
||||
{
|
||||
rhs.mList = nullptr;
|
||||
}
|
||||
|
||||
CurlStringList::~CurlStringList () noexcept
|
||||
{
|
||||
curl_slist_free_all (mList);
|
||||
}
|
||||
|
||||
CurlStringList& CurlStringList::operator= (CurlStringList&& rhs) noexcept
|
||||
{
|
||||
std::swap (mList, rhs.mList);
|
||||
return *this;
|
||||
}
|
||||
|
||||
void CurlStringList::append (const std::string& string) noexcept
|
||||
{
|
||||
mList = curl_slist_append (mList, string.c_str ());
|
||||
}
|
||||
|
||||
void CurlStringList::append (const char* string) noexcept
|
||||
{
|
||||
mList = curl_slist_append (mList, string);
|
||||
}
|
||||
|
||||
curl_slist* CurlStringList::getCurlList () const noexcept
|
||||
{
|
||||
return mList;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
42
libraries/lib-network-manager/curl/CurlStringList.h
Normal file
42
libraries/lib-network-manager/curl/CurlStringList.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CurlStringList.h
|
||||
@brief Declare a RAII wrapper for the curl_slist.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
struct curl_slist;
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
namespace network_manager
|
||||
{
|
||||
|
||||
class CurlStringList final
|
||||
{
|
||||
public:
|
||||
CurlStringList () = default;
|
||||
CurlStringList (CurlStringList&& rhs) noexcept;
|
||||
|
||||
~CurlStringList () noexcept;
|
||||
|
||||
CurlStringList& operator = (CurlStringList&& rhs) noexcept;
|
||||
|
||||
void append (const std::string& string) noexcept;
|
||||
void append (const char* string) noexcept;
|
||||
|
||||
curl_slist* getCurlList () const noexcept;
|
||||
|
||||
private:
|
||||
curl_slist* mList { nullptr };
|
||||
};
|
||||
|
||||
}
|
||||
}
|
27
libraries/lib-string-utils/CMakeLists.txt
Normal file
27
libraries/lib-string-utils/CMakeLists.txt
Normal file
@ -0,0 +1,27 @@
|
||||
set(TARGET lib-string-utils)
|
||||
set(TARGET_ROOT ${CMAKE_CURRENT_SOURCE_DIR} )
|
||||
|
||||
def_vars()
|
||||
|
||||
set(SOURCES
|
||||
HexHelpers.h
|
||||
|
||||
UrlEncode.h
|
||||
UrlEncode.cpp
|
||||
|
||||
UrlDecode.h
|
||||
UrlDecode.cpp
|
||||
|
||||
CodeConversions.h
|
||||
CodeConversions.cpp
|
||||
|
||||
DateTimeConversions.h
|
||||
DateTimeConversions.cpp
|
||||
)
|
||||
|
||||
set( LIBRARIES
|
||||
PRIVATE
|
||||
wxwidgets::base
|
||||
)
|
||||
|
||||
audacity_library( ${TARGET} "${SOURCES}" "${LIBRARIES}" "" "" )
|
59
libraries/lib-string-utils/CodeConversions.cpp
Normal file
59
libraries/lib-string-utils/CodeConversions.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CodeConversions.cpp
|
||||
@brief Define functions to preform UTF-8 to std::wstring conversions.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "CodeConversions.h"
|
||||
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
std::string ToUTF8 (const std::wstring& wstr)
|
||||
{
|
||||
return std::wstring_convert<std::codecvt_utf8<wchar_t>> ().to_bytes (wstr);
|
||||
}
|
||||
|
||||
std::string ToUTF8 (const wchar_t* wstr)
|
||||
{
|
||||
return std::wstring_convert<std::codecvt_utf8<wchar_t>> ().to_bytes (wstr);
|
||||
}
|
||||
|
||||
std::string ToUTF8 (const wxString& wstr)
|
||||
{
|
||||
return wstr.ToStdString (wxGet_wxConvUTF8 ());
|
||||
}
|
||||
|
||||
std::wstring ToWString (const std::string& str)
|
||||
{
|
||||
return std::wstring_convert<std::codecvt_utf8<wchar_t>> ().from_bytes (str);
|
||||
}
|
||||
|
||||
std::wstring ToWString (const char* str)
|
||||
{
|
||||
return std::wstring_convert<std::codecvt_utf8<wchar_t>> ().from_bytes (str);
|
||||
}
|
||||
|
||||
std::wstring ToWString (const wxString& str)
|
||||
{
|
||||
return str.ToStdWstring ();
|
||||
}
|
||||
|
||||
wxString ToWXString (const std::string& str)
|
||||
{
|
||||
return wxString::FromUTF8 (str);
|
||||
}
|
||||
|
||||
wxString ToWXString (const std::wstring& str)
|
||||
{
|
||||
return wxString (str);
|
||||
}
|
||||
|
||||
}
|
31
libraries/lib-string-utils/CodeConversions.h
Normal file
31
libraries/lib-string-utils/CodeConversions.h
Normal file
@ -0,0 +1,31 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file CodeConversions.h
|
||||
@brief Declare functions to preform UTF-8 to std::wstring conversions.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <wx/string.h>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
STRING_UTILS_API std::string ToUTF8 (const std::wstring& wstr);
|
||||
STRING_UTILS_API std::string ToUTF8 (const wchar_t* wstr);
|
||||
STRING_UTILS_API std::string ToUTF8 (const wxString& wstr);
|
||||
|
||||
// std::wstring is UTF16 on windows and UTF32 elsewhere.
|
||||
STRING_UTILS_API std::wstring ToWString (const std::string& str);
|
||||
STRING_UTILS_API std::wstring ToWString (const char* str);
|
||||
STRING_UTILS_API std::wstring ToWString (const wxString& str);
|
||||
|
||||
STRING_UTILS_API wxString ToWXString (const std::string& str);
|
||||
STRING_UTILS_API wxString ToWXString (const std::wstring& str);
|
||||
|
||||
}
|
44
libraries/lib-string-utils/DateTimeConversions.cpp
Normal file
44
libraries/lib-string-utils/DateTimeConversions.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file DateTimeConversions.cpp
|
||||
@brief Define functions to work with date and time string representations.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "DateTimeConversions.h"
|
||||
|
||||
#include <wx/datetime.h>
|
||||
|
||||
#include "CodeConversions.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
bool ParseRFC822Date (const std::string& dateString, SystemTime* time)
|
||||
{
|
||||
wxDateTime dt;
|
||||
wxString::const_iterator end;
|
||||
|
||||
if (!dt.ParseRfc822Date (dateString, &end))
|
||||
return false;
|
||||
|
||||
if (time != nullptr)
|
||||
*time = std::chrono::system_clock::from_time_t (dt.GetTicks ());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string SerializeRFC822Date (SystemTime timePoint)
|
||||
{
|
||||
const wxDateTime dt (
|
||||
time_t (std::chrono::duration_cast<std::chrono::seconds> (
|
||||
timePoint.time_since_epoch ()
|
||||
).count ()));
|
||||
|
||||
return ToUTF8 (dt.Format("%a, %d %b %Y %H:%M:%S %z"));
|
||||
}
|
||||
|
||||
}
|
24
libraries/lib-string-utils/DateTimeConversions.h
Normal file
24
libraries/lib-string-utils/DateTimeConversions.h
Normal file
@ -0,0 +1,24 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file DateTimeConversions.h
|
||||
@brief Declare functions to work with date and time string representations.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <string>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
using SystemTime = std::chrono::system_clock::time_point;
|
||||
|
||||
STRING_UTILS_API bool ParseRFC822Date (const std::string& dateString, SystemTime* time);
|
||||
STRING_UTILS_API std::string SerializeRFC822Date (SystemTime timePoint);
|
||||
|
||||
}
|
34
libraries/lib-string-utils/HexHelpers.h
Normal file
34
libraries/lib-string-utils/HexHelpers.h
Normal file
@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file HexHelpers.h
|
||||
@brief Define helper functions for hex-to-num conversion.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include <cstdint>
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
inline uint8_t HexCharToNum (char c) noexcept
|
||||
{
|
||||
assert (std::isxdigit (c) != 0);
|
||||
|
||||
if ('0' <= c && c <= '9')
|
||||
return c - '0';
|
||||
else if ('A' <= c && c <= 'F')
|
||||
return c - 'A' + 10;
|
||||
else if ('a' <= c && c <= 'f')
|
||||
return c - 'a' + 10;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
51
libraries/lib-string-utils/UrlDecode.cpp
Normal file
51
libraries/lib-string-utils/UrlDecode.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file UrlDecode.cpp
|
||||
@brief Define a function to decode an URL encode string.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "UrlDecode.h"
|
||||
|
||||
#include "HexHelpers.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
std::string UrlDecode (const std::string& url)
|
||||
{
|
||||
std::string result;
|
||||
|
||||
const size_t length = url.length ();
|
||||
|
||||
for (auto it = url.begin (), end = url.end (); it != end; ++it)
|
||||
{
|
||||
const char c = *it;
|
||||
|
||||
if (c != '%')
|
||||
{
|
||||
result.push_back (c);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (++it == url.end ())
|
||||
break; // Malformed input string
|
||||
|
||||
const char c1 = *it;
|
||||
|
||||
if (++it == url.end ())
|
||||
break; // Malformed input string
|
||||
|
||||
const char c2 = *it;
|
||||
|
||||
result.push_back (HexCharToNum (c1) << 4 | HexCharToNum (c2));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
20
libraries/lib-string-utils/UrlDecode.h
Normal file
20
libraries/lib-string-utils/UrlDecode.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file UrlDecode.h
|
||||
@brief Declare a function to decode an URL encode string.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
STRING_UTILS_API std::string UrlDecode (const std::string& url);
|
||||
|
||||
}
|
44
libraries/lib-string-utils/UrlEncode.cpp
Normal file
44
libraries/lib-string-utils/UrlEncode.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file UrlEncode.cpp
|
||||
@brief Define a function to perfom URL encoding of a string.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#include "UrlEncode.h"
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
std::string UrlEncode (const std::string& url)
|
||||
{
|
||||
std::string escaped;
|
||||
|
||||
for (char c : url)
|
||||
{
|
||||
if (('0' <= c && c <= '9') ||
|
||||
('A' <= c && c <= 'Z') ||
|
||||
('a' <= c && c <= 'z') ||
|
||||
(c == '~' || c == '-' || c == '_' || c == '.')
|
||||
)
|
||||
{
|
||||
escaped.push_back (c);
|
||||
}
|
||||
else
|
||||
{
|
||||
static const char symbolLookup[] = "0123456789ABCDEF";
|
||||
|
||||
escaped.push_back ('%');
|
||||
|
||||
escaped.push_back (symbolLookup[(c & 0xF0) >> 4]);
|
||||
escaped.push_back (symbolLookup[(c & 0x0F) >> 0]);
|
||||
}
|
||||
}
|
||||
|
||||
return escaped;
|
||||
}
|
||||
|
||||
}
|
20
libraries/lib-string-utils/UrlEncode.h
Normal file
20
libraries/lib-string-utils/UrlEncode.h
Normal file
@ -0,0 +1,20 @@
|
||||
/*!********************************************************************
|
||||
|
||||
Audacity: A Digital Audio Editor
|
||||
|
||||
@file UrlEncode.h
|
||||
@brief Declare a function to perfom URL encoding of a string.
|
||||
|
||||
Dmitry Vedenko
|
||||
**********************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace audacity
|
||||
{
|
||||
|
||||
STRING_UTILS_API std::string UrlEncode (const std::string& url);
|
||||
|
||||
}
|
@ -38,6 +38,7 @@ cmake_options=(
|
||||
-Daudacity_use_sbsms=local # We prefer using the latest version of sbsms
|
||||
-Daudacity_use_soundtouch=system
|
||||
-Daudacity_use_twolame=system
|
||||
-Daudacity_use_curl=system
|
||||
)
|
||||
|
||||
cmake "${cmake_options[@]}" ../audacity
|
||||
|
@ -112,6 +112,8 @@ It handles initialization and termination by subclassing wxApp.
|
||||
#include "widgets/FileConfig.h"
|
||||
#include "widgets/FileHistory.h"
|
||||
|
||||
#include "NetworkManager.h"
|
||||
|
||||
#ifdef EXPERIMENTAL_EASY_CHANGE_KEY_BINDINGS
|
||||
#include "prefs/KeyConfigPrefs.h"
|
||||
#endif
|
||||
@ -2200,6 +2202,8 @@ int AudacityApp::OnExit()
|
||||
// Terminate the PluginManager (must be done before deleting the locale)
|
||||
PluginManager::Get().Terminate();
|
||||
|
||||
audacity::network_manager::NetworkManager::GetInstance().Terminate();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1130,10 +1130,6 @@ if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
|
||||
|
||||
# Copy over the wxWidgets DLLs
|
||||
|
||||
# Convert the paths to native
|
||||
file( TO_NATIVE_PATH "${CMAKE_BINARY_DIR}/bin/shared" libdir )
|
||||
file( TO_NATIVE_PATH "${_EXEDIR}" exedir )
|
||||
|
||||
# Copy the required wxWidgets libs into the bundle
|
||||
add_custom_command(
|
||||
TARGET
|
||||
@ -1141,7 +1137,7 @@ if( CMAKE_SYSTEM_NAME MATCHES "Windows" )
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -D SRC="${_EXEDIR}/Audacity.exe"
|
||||
-D DST="${_EXEDIR}"
|
||||
-D WXWIN="${libdir}/$<CONFIG>/"
|
||||
-D WXWIN="${_SHARED_PROXY_BASE_PATH}/$<CONFIG>/"
|
||||
-P ${AUDACITY_MODULE_PATH}/CopyLibs.cmake
|
||||
POST_BUILD
|
||||
)
|
||||
@ -1226,7 +1222,7 @@ elseif( CMAKE_SYSTEM_NAME MATCHES "Darwin" )
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -D SRC="${_EXEDIR}/Audacity"
|
||||
-D DST="${_PKGLIB}"
|
||||
-D WXWIN="${CMAKE_BINARY_DIR}/lib/shared/$<CONFIG>"
|
||||
-D WXWIN="${_SHARED_PROXY_BASE_PATH}/$<CONFIG>"
|
||||
-P ${AUDACITY_MODULE_PATH}/CopyLibs.cmake
|
||||
POST_BUILD
|
||||
)
|
||||
@ -1292,7 +1288,7 @@ else()
|
||||
COMMAND
|
||||
${CMAKE_COMMAND} -D SRC="${_EXEDIR}/audacity"
|
||||
-D DST="${_DEST}/${_PKGLIB}"
|
||||
-D WXWIN="${CMAKE_BINARY_DIR}/lib/shared/$<CONFIG>"
|
||||
-D WXWIN="${_SHARED_PROXY_BASE_PATH}/$<CONFIG>"
|
||||
-P ${AUDACITY_MODULE_PATH}/CopyLibs.cmake
|
||||
POST_BUILD
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user