simple http client

This commit is contained in:
2026-01-03 04:20:33 +02:00
parent 1897035d8e
commit 965cecc901
16 changed files with 428 additions and 521 deletions

0
appleadi/auth.cpp Normal file
View File

View File

@@ -56,10 +56,6 @@ builddir:
mkdir -p ../build/tools/fpc mkdir -p ../build/tools/fpc
install_fpc: ../build/tools/fpc install_fpc: ../build/tools/fpc
mv build/fpc_temp build/fpc
#mv build/libtier0_temp.so libtier0.so
mv build/libfpc_temp.so build/libfpc.so
#mv build/libfilesystem_std_temp.so libfilesystem_std.so
cp -r build/* ../build/tools/fpc cp -r build/* ../build/tools/fpc
install_temp: builddir install_temp: builddir

View File

@@ -16,7 +16,7 @@ DECLARE_BUILD_STAGE(xtool)
} }
CUtlString outputProject = CUtlString("fpcxtool/.build/%s/debug/libfpcxtool.a",Target_t::HostTarget().GetTriplet().GetString()); CUtlString outputProject = CUtlString("fpcxtool/.build/%s/debug/libfpcxtool.so",Target_t::HostTarget().GetTriplet().GetString());
V_printf("SWIFT %s\n", outputProject.GetString()); V_printf("SWIFT %s\n", outputProject.GetString());
CUtlVector<CUtlString> args = {"build"}; CUtlVector<CUtlString> args = {"build"};
@@ -36,14 +36,18 @@ DECLARE_BUILD_STAGE(xtool_cpp)
compileProject.m_szName = "xtool"; compileProject.m_szName = "xtool";
compileProject.files = { compileProject.files = {
"fpcxtool/sign.cpp" "fpcxtool/fpcxtool.cpp"
}; };
compileProject.includeDirectories = {"../public","../../public"}; compileProject.includeDirectories = {"../public","../../public"};
compileProject.bFPIC = true; compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject); ldProject = ccompiler->Compile(&compileProject);
ldProject.linkType = ELINK_DYNAMIC_LIBRARY; ldProject.linkType = ELINK_DYNAMIC_LIBRARY;
ldProject.libraryObjects.AppendTail(GET_PROJECT_LIBRARY(xtool, "xtool")); ldProject.objects.AppendTail({GET_PROJECT_LIBRARY(xtool, "xtool")});
ldProject.libraryObjects = {
GET_PROJECT_LIBRARY(tier1, "tier1"),
};
szOutputProject = linker->Link(&ldProject); szOutputProject = linker->Link(&ldProject);
ADD_OUTPUT_OBJECT("xtool", szOutputProject); ADD_OUTPUT_OBJECT("xtool", szOutputProject);
return 0; return 0;

View File

@@ -1,447 +0,0 @@
{
"originHash" : "1b40ee21cafb6201503d43773a417fca9894828946ab37ff5dc543cfd8b2565c",
"pins" : [
{
"identity" : "aexml",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tadija/AEXML.git",
"state" : {
"revision" : "db806756c989760b35108146381535aec231092b",
"version" : "4.7.0"
}
},
{
"identity" : "async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/async-http-client.git",
"state" : {
"revision" : "5dd84c7bb48b348751d7bbe7ba94a17bafdcef37",
"version" : "1.30.2"
}
},
{
"identity" : "bigint",
"kind" : "remoteSourceControl",
"location" : "https://github.com/attaswift/BigInt",
"state" : {
"revision" : "e07e00fa1fd435143a2dcf8b7eec9a7710b2fdfe",
"version" : "5.7.0"
}
},
{
"identity" : "combine-schedulers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/combine-schedulers",
"state" : {
"revision" : "fd16d76fd8b9a976d88bfb6cacc05ca8d19c91b6",
"version" : "1.1.0"
}
},
{
"identity" : "jsonutilities",
"kind" : "remoteSourceControl",
"location" : "https://github.com/yonaskolb/JSONUtilities.git",
"state" : {
"revision" : "128d2ffc22467f69569ef8ff971683e2393191a0",
"version" : "4.2.0"
}
},
{
"identity" : "opencombine",
"kind" : "remoteSourceControl",
"location" : "https://github.com/OpenCombine/OpenCombine.git",
"state" : {
"revision" : "8576f0d579b27020beccbccc3ea6844f3ddfc2c2",
"version" : "0.14.0"
}
},
{
"identity" : "pathkit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kylef/PathKit.git",
"state" : {
"revision" : "3bfd2737b700b9a36565a8c94f4ad2b050a5e574",
"version" : "1.0.1"
}
},
{
"identity" : "rainbow",
"kind" : "remoteSourceControl",
"location" : "https://github.com/onevcat/Rainbow.git",
"state" : {
"revision" : "cdf146ae671b2624917648b61c908d1244b98ca1",
"version" : "4.2.1"
}
},
{
"identity" : "spectre",
"kind" : "remoteSourceControl",
"location" : "https://github.com/kylef/Spectre.git",
"state" : {
"revision" : "26cc5e9ae0947092c7139ef7ba612e34646086c7",
"version" : "0.10.1"
}
},
{
"identity" : "swift-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-algorithms.git",
"state" : {
"revision" : "87e50f483c54e6efd60e885f7f5aa946cee68023",
"version" : "1.2.1"
}
},
{
"identity" : "swift-argument-parser",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-argument-parser",
"state" : {
"revision" : "c5d11a805e765f52ba34ec7284bd4fcd6ba68615",
"version" : "1.7.0"
}
},
{
"identity" : "swift-asn1",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-asn1.git",
"state" : {
"revision" : "810496cf121e525d660cd0ea89a758740476b85f",
"version" : "1.5.1"
}
},
{
"identity" : "swift-async-algorithms",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-async-algorithms.git",
"state" : {
"revision" : "6c050d5ef8e1aa6342528460db614e9770d7f804",
"version" : "1.1.1"
}
},
{
"identity" : "swift-atomics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-atomics.git",
"state" : {
"revision" : "b601256eab081c0f92f059e12818ac1d4f178ff7",
"version" : "1.3.0"
}
},
{
"identity" : "swift-certificates",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-certificates",
"state" : {
"revision" : "133a347911b6ad0fc8fe3bf46ca90c66cff97130",
"version" : "1.17.0"
}
},
{
"identity" : "swift-clocks",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-clocks",
"state" : {
"revision" : "cc46202b53476d64e824e0b6612da09d84ffde8e",
"version" : "1.0.6"
}
},
{
"identity" : "swift-collections",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-collections",
"state" : {
"revision" : "7b847a3b7008b2dc2f47ca3110d8c782fb2e5c7e",
"version" : "1.3.0"
}
},
{
"identity" : "swift-concurrency-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-concurrency-extras",
"state" : {
"revision" : "5a3825302b1a0d744183200915a47b508c828e6f",
"version" : "1.3.2"
}
},
{
"identity" : "swift-crypto",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-crypto",
"state" : {
"revision" : "95ba0316a9b733e92bb6b071255ff46263bbe7dc",
"version" : "3.15.1"
}
},
{
"identity" : "swift-dependencies",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/swift-dependencies",
"state" : {
"revision" : "a10f9feeb214bc72b5337b6ef6d5a029360db4cc",
"version" : "1.10.0"
}
},
{
"identity" : "swift-distributed-tracing",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-distributed-tracing.git",
"state" : {
"revision" : "baa932c1336f7894145cbaafcd34ce2dd0b77c97",
"version" : "1.3.1"
}
},
{
"identity" : "swift-http-structured-headers",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-structured-headers.git",
"state" : {
"revision" : "76d7627bd88b47bf5a0f8497dd244885960dde0b",
"version" : "1.6.0"
}
},
{
"identity" : "swift-http-types",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-http-types",
"state" : {
"revision" : "45eb0224913ea070ec4fba17291b9e7ecf4749ca",
"version" : "1.5.1"
}
},
{
"identity" : "swift-log",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-log.git",
"state" : {
"revision" : "bc386b95f2a16ccd0150a8235e7c69eab2b866ca",
"version" : "1.8.0"
}
},
{
"identity" : "swift-nio",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio",
"state" : {
"revision" : "a1605a3303a28e14d822dec8aaa53da8a9490461",
"version" : "2.92.0"
}
},
{
"identity" : "swift-nio-extras",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-extras.git",
"state" : {
"revision" : "1c90641b02b6ab47c6d0db2063a12198b04e83e2",
"version" : "1.31.2"
}
},
{
"identity" : "swift-nio-http2",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-http2.git",
"state" : {
"revision" : "c2ba4cfbb83f307c66f5a6df6bb43e3c88dfbf80",
"version" : "1.39.0"
}
},
{
"identity" : "swift-nio-ssl",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-ssl.git",
"state" : {
"revision" : "173cc69a058623525a58ae6710e2f5727c663793",
"version" : "2.36.0"
}
},
{
"identity" : "swift-nio-transport-services",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-nio-transport-services.git",
"state" : {
"revision" : "60c3e187154421171721c1a38e800b390680fb5d",
"version" : "1.26.0"
}
},
{
"identity" : "swift-numerics",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-numerics.git",
"state" : {
"revision" : "0c0290ff6b24942dadb83a929ffaaa1481df04a2",
"version" : "1.1.1"
}
},
{
"identity" : "swift-openapi-async-http-client",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-openapi-async-http-client",
"state" : {
"revision" : "4828c3aa33784185d8f99f5c27844fcfb1436b3d",
"version" : "1.3.0"
}
},
{
"identity" : "swift-openapi-runtime",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-openapi-runtime",
"state" : {
"revision" : "7cdf33371bf89b23b9cf4fd3ce8d3c825c28fbe8",
"version" : "1.9.0"
}
},
{
"identity" : "swift-openapi-urlsession",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-openapi-urlsession",
"state" : {
"revision" : "279aa6b77be6aa842a4bf3c45fa79fa15edf3e07",
"version" : "1.2.0"
}
},
{
"identity" : "swift-service-context",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-service-context.git",
"state" : {
"revision" : "1983448fefc717a2bc2ebde5490fe99873c5b8a6",
"version" : "1.2.1"
}
},
{
"identity" : "swift-service-lifecycle",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swift-server/swift-service-lifecycle.git",
"state" : {
"revision" : "1de37290c0ab3c5a96028e0f02911b672fd42348",
"version" : "2.9.1"
}
},
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "4799286537280063c85a32f09884cfbca301b1a1",
"version" : "602.0.0"
}
},
{
"identity" : "swift-system",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-system",
"state" : {
"revision" : "395a77f0aa927f0ff73941d7ac35f2b46d47c9db",
"version" : "1.6.3"
}
},
{
"identity" : "swiftcli",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jakeheis/SwiftCLI.git",
"state" : {
"revision" : "2e949055d9797c1a6bddcda0e58dada16cc8e970",
"version" : "6.0.3"
}
},
{
"identity" : "swiftymobiledevice",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/SwiftyMobileDevice",
"state" : {
"revision" : "280d0dc8153469944491aee2f19af29e9c7a77f0",
"version" : "1.5.0"
}
},
{
"identity" : "unxip",
"kind" : "remoteSourceControl",
"location" : "https://github.com/saagarjha/unxip",
"state" : {
"revision" : "03c32d6b4e7d8a9fb58747e5f8b83397da8bf5ba",
"version" : "3.2.0"
}
},
{
"identity" : "version",
"kind" : "remoteSourceControl",
"location" : "https://github.com/mxcl/Version",
"state" : {
"revision" : "67ce582bb9de70e1eb2ee41fd71aad3b5f86d97b",
"version" : "2.2.0"
}
},
{
"identity" : "websocket-kit",
"kind" : "remoteSourceControl",
"location" : "https://github.com/vapor/websocket-kit.git",
"state" : {
"revision" : "8666c92dbbb3c8eefc8008c9c8dcf50bfd302167",
"version" : "2.16.1"
}
},
{
"identity" : "xcodegen",
"kind" : "remoteSourceControl",
"location" : "https://github.com/yonaskolb/XcodeGen",
"state" : {
"revision" : "21ac9944b0ab546a07422dbed86f33dd2ebd76f8",
"version" : "2.44.1"
}
},
{
"identity" : "xcodeproj",
"kind" : "remoteSourceControl",
"location" : "https://github.com/tuist/XcodeProj.git",
"state" : {
"revision" : "b1caa062d4aaab3e3d2bed5fe0ac5f8ce9bf84f4",
"version" : "8.27.7"
}
},
{
"identity" : "xctest-dynamic-overlay",
"kind" : "remoteSourceControl",
"location" : "https://github.com/pointfreeco/xctest-dynamic-overlay",
"state" : {
"revision" : "31073495cae9caf243c440eac94b3ab067e3d7bc",
"version" : "1.8.0"
}
},
{
"identity" : "xtool",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/xtool",
"state" : {
"revision" : "22655f3e70b87bef9be736e9625984e21bf8c697",
"version" : "1.16.1"
}
},
{
"identity" : "xtool-core",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/xtool-core",
"state" : {
"revision" : "d412ac768d1e73d16ad7ccc6805f12fc701709f6",
"version" : "1.4.0"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams",
"state" : {
"revision" : "3d6871d5b4a5cd519adf233fbb576e0a2af71c17",
"version" : "5.4.0"
}
},
{
"identity" : "zsign",
"kind" : "remoteSourceControl",
"location" : "https://github.com/xtool-org/zsign",
"state" : {
"revision" : "6c4a9f52dd8bf374aa938ef9c17d5f261f2c333a",
"version" : "1.7.0"
}
}
],
"version" : 3
}

View File

@@ -1,30 +0,0 @@
// swift-tools-version:6.2
import PackageDescription
let package = Package(
name: "fpcxtool",
platforms: [
.macOS(.v12)
],
products: [
.library(
name: "fpcxtool",
type: .static,
targets: ["fpcxtool"]
),
],
dependencies: [
.package(url: "https://github.com/xtool-org/xtool", "1.16.1" ..< "1.17.0"),
],
targets: [
.target(
name: "fpcxtool",
dependencies: [
.product(name: "XKit", package: "xtool")
],
path: "swift"
)
]
)

View File

@@ -1,14 +0,0 @@
#include "deploy.h"
extern "C" void FPC_XToolSign( const char *szIpa );
class CXToolDevice: public IDeployDevice
{
public:
virtual const char *GetName() override;
virtual void *Install( const char *szPath ) override;
void *pDeviceHandle;
};

View File

@@ -1,19 +0,0 @@
import XKit
@_cdecl("FPCXToolAuth")
func FPCXToolAuth()
{
}
@_cdecl("FPCXToolSign")
func FPCXToolSign()
{
}
@_cdecl("FPCXToolInstall")
func FPCXToolInstall( _ appDir: UnsafePointer<CChar> )
{
}

View File

@@ -139,9 +139,14 @@ DECLARE_BUILD_STAGE(install)
CUtlString szTier0 = GET_PROJECT_LIBRARY(tier0, "tier0"); CUtlString szTier0 = GET_PROJECT_LIBRARY(tier0, "tier0");
CUtlString szTier1 = GET_PROJECT_LIBRARY(tier1, "tier1"); CUtlString szTier1 = GET_PROJECT_LIBRARY(tier1, "tier1");
CUtlString szTier2 = GET_PROJECT_LIBRARY(tier2, "tier2"); CUtlString szTier2 = GET_PROJECT_LIBRARY(tier2, "tier2");
CUtlString xtool = GET_PROJECT_LIBRARY(xtool_cpp, "xtool");
filesystem2->CopyFile("build/fpc_temp", szExe); filesystem2->CopyFile("build/fpc_temp", szExe);
filesystem2->CopyFile("build/libfpc_temp.so", szLibFpc); filesystem2->CopyFile("build/libfpc_temp.so", szLibFpc);
if (xtool)
{
filesystem2->CopyFile("build", xtool);
}
/* /*
filesystem2->CopyFile("build/libtier1.a", szTier1); filesystem2->CopyFile("build/libtier1.a", szTier1);

View File

@@ -79,9 +79,15 @@ CUtlString CAppleTool::BuildPackage( AppleManifest_t manifest, CUtlString szMani
return CUtlString("../%s.ipa", manifest.m_szPackageName.GetString()); return CUtlString("../%s.ipa", manifest.m_szPackageName.GetString());
} }
static IAppleDaemon *appledaemon;
CUtlString CAppleTool::SignPackage( const char *szIpa, const char *szPassword ) CUtlString CAppleTool::SignPackage( const char *szIpa, const char *szPassword )
{ {
CreateInterfaceFn fnFactory = Sys_GetFactory("xtool");
if (fnFactory == NULL)
Plat_FatalErrorFunc("Couldn't get xtool\n");
appledaemon = (IAppleDaemon*)fnFactory(APPLE_DAEMON_INTERFACE_VERSION, NULL);
appledaemon->SignPackage(szIpa);
return szIpa;
} }

View File

@@ -26,6 +26,15 @@ public:
virtual CUtlString SignPackage( const char *szIpa, const char *szPassword ) = 0; virtual CUtlString SignPackage( const char *szIpa, const char *szPassword ) = 0;
}; };
abstract_class IAppleDaemon
{
public:
virtual void SignPackage( const char *szIpa ) = 0;
};
#define APPLE_DAEMON_INTERFACE_VERSION "AppleDaemon001"
IAppleTool *AppleTool(); IAppleTool *AppleTool();
#endif #endif

76
http/build.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include "helper.h"
#include "c.h"
#include "ld.h"
#include "tier1/utlstring.h"
#include "tier1/commandline.h"
ADD_DEPENDENCY_BUILD_FILE(tier0, "../tier0/build.cpp")
ADD_DEPENDENCY_BUILD_FILE(tier1, "../tier1/build.cpp")
ADD_DEPENDENCY_BUILD_FILE(tier2, "../tier2/build.cpp")
DECLARE_BUILD_STAGE(funnyhttp)
{
CProject_t compileProject = {};
LinkProject_t ldProject = {};
compileProject.m_szName = "funnyhttp";
compileProject.files = {
"client.cpp"
};
compileProject.includeDirectories = {"../public"};
compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject);
ldProject.objects.AppendTail({GET_PROJECT_LIBRARY(tier1, "tier1")});
ldProject.objects.AppendTail({GET_PROJECT_LIBRARY(tier2, "tier2")});
ldProject.libraries = {
"ssl",
"crypto",
};
ldProject.linkType = ELINK_DYNAMIC_LIBRARY;
CUtlString szOutputDir = linker->Link(&ldProject);
ADD_OUTPUT_OBJECT("funnyhttp", szOutputDir)
return 0;
};
DECLARE_BUILD_STAGE(test)
{
CProject_t compileProject = {};
LinkProject_t ldProject = {};
filesystem2->MakeDirectory("build");
filesystem2->CopyFile("build",GET_PROJECT_LIBRARY(funnyhttp, "funnyhttp"));
filesystem2->CopyFile("build",GET_PROJECT_LIBRARY(tier0, "tier0"));
compileProject.m_szName = "funnyhttptest";
compileProject.files = {
"test.cpp"
};
compileProject.includeDirectories = {"../public"};
compileProject.bFPIC = true;
ldProject = ccompiler->Compile(&compileProject);
ldProject.libraryObjects = {
GET_PROJECT_LIBRARY(tier1, "tier1"),
GET_PROJECT_LIBRARY(tier2, "tier2"),
};
ldProject.libraries = {
"tier0",
};
ldProject.libraryDirectories = {
"build",
};
ldProject.linkType = ELINK_EXECUTABLE;
CUtlString szOutputDir = linker->Link(&ldProject);
filesystem2->CopyFile("build",szOutputDir);
CUtlVector<CUtlString> args = {};
runner->Run("build/funnyhttptest", args);
runner->Wait();
return 0;
};

View File

@@ -0,0 +1,240 @@
#include "http/http.h"
#include "tier1/interface.h"
#include "tier1/utlstring.h"
#include "netdb.h"
#include "unistd.h"
#include "fcntl.h"
#include "openssl/ssl.h"
#include "openssl/err.h"
abstract_class CHTTPClient: public IHTTPClient
{
public:
virtual void Post( const char *szResource, HTTPHeader_t *pHeader, uint32_t uDataSize, const char *data ) override;
virtual void Get( const char *szResource, HTTPHeader_t *pHeader ) override;
virtual HTTPResponse_t GetResponse() override;
HTTPResponse_t ParseResponse( const char *szMessage );
const char *GetVersion();
const char *m_szHostName;
bool m_bIsSecure;
SSL *m_pSSL;
SSL_CTX *m_pSSLCtx;
int m_iFileDescriptor;
};
void CHTTPClient::Post( const char *szResource, HTTPHeader_t *pHeader, uint32_t uDataSize, const char *data )
{
}
void CHTTPClient::Get( const char *szResource, HTTPHeader_t *pHeader )
{
if (pHeader == NULL)
return;
CUtlString szMessage = CUtlString(
"GET %s HTTP/%s\r\n",
szResource, GetVersion());
CUtlString szHeader = CUtlString(
"Host: %s\r\n",
m_szHostName
);
CUtlString szCombined;
int i = 0;
for ( i = 0; i < pHeader->m_nParamCount; i++ )
{
szHeader.AppendTail(CUtlString("%s: %s\r\n", pHeader->m_params[i].m_szParamName, pHeader->m_params[i].m_szValue));
}
szCombined = szMessage;
szCombined.AppendTail(szHeader);
szCombined.AppendTail("\r\n");
if (m_bIsSecure)
SSL_write(m_pSSL, szCombined.GetString(), szCombined.GetLenght());
else
write(m_iFileDescriptor, szCombined.GetString(), szCombined.GetLenght());
}
HTTPResponse_t CHTTPClient::GetResponse()
{
CUtlResizableBuffer<char> szCharBuffer(0);
char response[4096];
int n;
int nPreviousSize = 0;
if (m_bIsSecure)
{
secureRead:
n = SSL_read(m_pSSL, response, sizeof(response));
szCharBuffer.Resize(nPreviousSize+n);
V_memcpy((char*)szCharBuffer.GetMemory()+nPreviousSize, response, n);
nPreviousSize += n;
if (SSL_pending(m_pSSL)>0)
goto secureRead;
}
else {
httpRead:
n = read(m_iFileDescriptor, response, sizeof(response));
V_printf("%i\n",n);
szCharBuffer.Resize(nPreviousSize+n+1);
szCharBuffer[nPreviousSize+n] = 0;
V_memcpy((char*)szCharBuffer.GetMemory()+nPreviousSize, response, n);
nPreviousSize += n;
goto httpRead;
}
szCharBuffer.Resize(n+1);
return ParseResponse(szCharBuffer);
}
HTTPResponse_t CHTTPClient::ParseResponse( const char *szMessage )
{
char cPreviousCharacter = 0;
char cCurrentCharacter = 0;
const char *pcCurrentCharacter = szMessage;
CUtlString szBuffer = "";
bool bIsMessage = true;
HTTPResponse_t response = {};
CUtlVector<HTTPHeaderParam_t> headers = {};
CUtlBuffer<char> data = {};
// Parse header
while (*pcCurrentCharacter)
{
cCurrentCharacter = *pcCurrentCharacter;
if ( cPreviousCharacter == '\r')
{
if ( cCurrentCharacter == '\n')
{
if (bIsMessage)
{
uint32_t uResult = 0;
V_sscanf(szBuffer, "HTTP/1.1 %i", &uResult);
response.m_uCode = uResult;
}
bIsMessage = false;
if (szBuffer == "")
break;
szBuffer = "";
cPreviousCharacter = 0;
cCurrentCharacter = *pcCurrentCharacter++;
continue;
}
}
if (cPreviousCharacter != 0)
szBuffer.AppendTail(cPreviousCharacter);
pcCurrentCharacter++;
cPreviousCharacter = cCurrentCharacter;
};
pcCurrentCharacter++;
uint32_t nDataLen = V_strlen(pcCurrentCharacter);
data = CUtlBuffer<char>(nDataLen+1);
V_strcpy(data.GetMemory(), pcCurrentCharacter);
data[nDataLen] = 0;
response.m_message = data;
return response;
}
const char *CHTTPClient::GetVersion()
{
return "1.1";
}
abstract_class CHTTPClientManager: public IHTTPClientManager
{
public:
CHTTPClientManager();
virtual IHTTPClient *Connect( const char *szUrl, bool bSecure ) override;
virtual void Disconnect( IHTTPClient *szConnection ) override;
};
IHTTPClient *CHTTPClientManager::Connect( const char *szUrl, bool bSecure )
{
struct hostent *pServerHostName = NULL;
struct sockaddr_in serverAddress;
int fd = 0;
int err;
CHTTPClient *pClient;
SSL_CTX *ctx;
SSL *ssl;
pServerHostName = gethostbyname(szUrl);
if (!pServerHostName)
return NULL;
fd = socket(AF_INET, SOCK_STREAM, 0);
if ( fd < 0 )
return NULL;
V_memset(&serverAddress, 0, sizeof(serverAddress));
serverAddress.sin_family = AF_INET;
V_memcpy(&serverAddress.sin_addr.s_addr, pServerHostName->h_addr, pServerHostName->h_length);
// https
if ( bSecure )
serverAddress.sin_port = htons(443);
else
serverAddress.sin_port = htons(80);
err = connect(fd, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
if (err < 0)
return NULL;
if (bSecure)
{
ctx = SSL_CTX_new(TLS_client_method());
if (!ctx)
return NULL;
ssl =SSL_new(ctx);
if (!ssl)
return NULL;
SSL_set_fd(ssl, fd);
SSL_set_tlsext_host_name(ssl, szUrl);
int r = SSL_connect(ssl);
if (r <= 0)
{
ERR_print_errors_fp(stdout);
SSL_free(ssl);
SSL_CTX_free(ctx);
return NULL;
}
};
pClient = new CHTTPClient();
pClient->m_iFileDescriptor = fd;
pClient->m_szHostName = szUrl;
if (bSecure)
{
pClient->m_bIsSecure = bSecure;
pClient->m_pSSL = ssl;
pClient->m_pSSLCtx = ctx;
}
return pClient;
}
void CHTTPClientManager::Disconnect( IHTTPClient *pClient )
{
CHTTPClient *pC = (CHTTPClient*)pClient;
close(pC->m_iFileDescriptor);
}
CHTTPClientManager::CHTTPClientManager()
{
SSL_library_init();
SSL_load_error_strings();
OpenSSL_add_all_algorithms();
}
CHTTPClientManager s_HttpClientManager;
EXPOSE_INTERFACE_GLOBALVAR(IHTTPClientManager, CHTTPClientManager, HTTP_CLIENT_INTERFACE_VERSION, s_HttpClientManager);

36
http/test.cpp Normal file
View File

@@ -0,0 +1,36 @@
#include "http/http.h"
#include "tier1/interface.h"
IHTTPClientManager *g_pHttpClientMgr;
int main()
{
Sys_GetFactory("tier0");
CreateInterfaceFn pHttpFactory = Sys_GetFactory("funnyhttp");
g_pHttpClientMgr = (IHTTPClientManager*)pHttpFactory(HTTP_CLIENT_INTERFACE_VERSION, NULL);
if ( !g_pHttpClientMgr )
return 0;
IHTTPClient *pClient = g_pHttpClientMgr->Connect("ani.sidestore.io", true);
printf("%p\n",pClient);
if ( pClient == NULL )
return 0;
HTTPHeaderParam_t params[] = {
{"User-Agent", "Funny"},
{"Accept", "application/json"},
};
HTTPHeader_t stHeader = {
sizeof(params)/sizeof(params[0]),
params
};
pClient->Get("/", &stHeader);
HTTPResponse_t stResponse = pClient->GetResponse();
if (stResponse.m_uCode == 200)
{
V_printf("%s\n", stResponse.m_message.GetMemory());
}
g_pHttpClientMgr->Disconnect(pClient);
return 0;
}

46
public/http/http.h Normal file
View File

@@ -0,0 +1,46 @@
#ifndef HTTP_H
#define HTTP_H
#include "tier0/platform.h"
#include "tier1/utlvector.h"
struct HTTPHeaderParam_t
{
const char *m_szParamName;
const char *m_szValue;
};
struct HTTPHeader_t
{
uint32_t m_nParamCount;
HTTPHeaderParam_t *m_params;
};
struct HTTPResponse_t
{
CUtlBuffer<HTTPHeaderParam_t> m_params;
CUtlBuffer<char> m_message;
uint32_t m_uCode;
bool bIsValid;
};
abstract_class IHTTPClient
{
public:
virtual void Post( const char *szResource, HTTPHeader_t *pHeader, uint32_t uDataSize, const char *data ) = 0;
virtual void Get( const char *szResource, HTTPHeader_t *pHeader ) = 0;
virtual HTTPResponse_t GetResponse() = 0;
};
abstract_class IHTTPClientManager
{
public:
virtual IHTTPClient *Connect( const char *szUrl, bool bSecure ) = 0;
virtual void Disconnect( IHTTPClient *pClient ) = 0;
};
#define HTTP_CLIENT_INTERFACE_VERSION "FHTTPClientMgr001"
extern IHTTPClientManager *g_pHttpClientMgr;
#endif

View File

@@ -1,11 +1,8 @@
#include "tier0/network.h" #include "tier0/network.h"
#include "tier1/commandline.h" #include "tier1/commandline.h"
void Net_Init() void Net_Init()
{ {
} }
void Net_Deinit() void Net_Deinit()

View File

@@ -46,5 +46,7 @@ CreateInterfaceFn Sys_GetFactory( const char *szLibrary )
szLib = CUtlString("lib%s.so", szLib.GetFileName().GetString()); szLib = CUtlString("lib%s.so", szLib.GetFileName().GetString());
pLib = Plat_LoadLibrary(szLib); pLib = Plat_LoadLibrary(szLib);
#endif #endif
if ( !pLib )
return NULL;
return Sys_GetFactory(pLib); return Sys_GetFactory(pLib);
} }