flatten 20260225

This commit is contained in:
Timothy Prepscius
2026-02-25 12:39:24 -05:00
commit fa54be052a
315 changed files with 49791 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
// TJP COPYRIGHT HEADER
#include <tjp/core/header_only/compile.h>
#ifdef TJP_CORE_HEADER_ONLY
#pragma once
#endif
// Internet.cpp : Defines the entry point for the DLL application.
//
#include "Internet.hpp"
#include "../../http/hirose/httplib.h"
#include <string.h>
#include <stdlib.h>
#include <string>
#include <tjp/core/string/String.hpp>
#include <tjp/core/algorithm/ExecuteOnDestruct.hpp>
#include <tjp/core/future/Future.hpp>
#include <tjp/core/log/Log.h>
#include <tjp/core/log/LogOf.h>
#include <tjp/core/string/as_vector.hpp>
#include <tjp/core/string/as_string.hpp>
//===============================================================================
namespace tjp {
namespace core {
namespace internet {
TJP_CORE_HEADER_ONLY_INLINE
Future<FileContents> post (const std::string &url, Data &&data, const HttpOptions &options)
{
Promise<FileContents> promise;
post(url, std::move(data), options, promise);
return promise.get_future();
}
TJP_CORE_HEADER_ONLY_INLINE
void perform (const std::string &url, const std::string &method, Data &&data, const HttpOptions &options, Promise<FileContents> &promise)
{
xLogDebug (logVar(url));
auto getFile = [url, options, method, data=std::move(data)]() {
sLogDebug("core::internet", "starting " << logVar(url));
FileContents contents;
auto firstSlashSlash = url.find("//");
auto firstSlash = url.find("/", firstSlashSlash+2);
httplib::Client cli(url.substr(0, firstSlash));
if (options.timeout)
cli.set_read_timeout(options.timeout);
httplib::Headers headers;
for (auto &[k,v]: options.headers)
headers.insert({k, v});
decltype(cli.Get(url.substr(firstSlash), headers)) result;
if (method == "GET")
result = cli.Get(url.substr(firstSlash), headers);
else
if (method == "POST")
result = cli.Post(url.substr(firstSlash), headers, data, "text/plain");
else
if (method == "PUT")
result = cli.Put(url.substr(firstSlash), headers, data, "application/binary");
if (options.onHeaders)
options.onHeaders(contents.headers, contents.statusCode);
if (options.onData)
options.onData(contents.data.data(), contents.data.size());
if (options.onComplete)
options.onComplete(contents.statusCode);
if (result)
{
contents.statusCode = result->status;
contents.data = as_vector(result->body);
for (auto &header: result->headers)
contents.headers.push_back(header);
contents.lastModifiedTime = 0;
}
else
{
sLogRelease("errors", "get http failed " << logVar(url) << logVar(result.error()));
throw Exception (
"HTTPConnection getFile failed " + std::to_string((int)result.error()),
std::move(contents)
);
}
if (options.throwOnFailure && !contents.ok())
{
sLogRelease("errors", "get http failed " << logVar(url) << logVar(contents.statusCode));
throw Exception (
"HTTPConnection getFile failed code " + std::to_string(contents.statusCode),
std::move(contents)
);
}
return contents;
};
promise_of(promise, getFile);
}
TJP_CORE_HEADER_ONLY_INLINE
void get (const std::string &url,const HttpOptions &options, Promise<FileContents> &promise)
{
return perform(url, "GET", Data {}, options, promise);
}
TJP_CORE_HEADER_ONLY_INLINE
void post (const std::string &url, Data &&data, const HttpOptions &options, Promise<FileContents> &promise)
{
return perform(url, "POST", std::move(data), options, promise);
}
TJP_CORE_HEADER_ONLY_INLINE
void put (const std::string &url, Data &&data, const HttpOptions &options, Promise<FileContents> &promise)
{
return perform(url, "PUT", std::move(data), options, promise);
}
TJP_CORE_HEADER_ONLY_INLINE
void head (const std::string &url, Data &&data, const HttpOptions &options, Promise<FileContents> &promise)
{
return perform(url, "HEAD", std::move(data), options, promise);
}
} // namespace
} // namespace
} // namespace

View File

@@ -0,0 +1,16 @@
// TJP COPYRIGHT HEADER
#pragma once
#include "../Internet.hpp"
namespace tjp::core::internet {
void get (const std::string &url, const HttpOptions &options, Promise<FileContents> &promise);
void post (const std::string &url, Data &&, const HttpOptions &options, Promise<FileContents> &promise);
} // namespace
#ifdef TJP_CORE_HEADER_ONLY
#include "Internet.cpp"
#endif