Files
core_misc/tjp/core/internet/Internet.hpp
Timothy Prepscius fa54be052a flatten 20260225
2026-02-25 12:39:24 -05:00

112 lines
2.3 KiB
C++
Executable File

// TJP COPYRIGHT HEADER
#pragma once
#include "Internet.h"
#include <string>
#include <map>
#include <vector>
#include <tjp/core/future/Future.h>
#include <tjp/core/ptr/Ptr.hpp>
#include <tjp/core/string/String.h>
#include <tjp/core/string/StringView.h>
#include <tjp/core/containers/Tuple.h>
#include <tjp/core/containers/Vector.h>
#include <tjp/core/containers/Function.h>
#include <tjp/core/types/Types.h>
#include <tjp/core/exception/Exception.hpp>
#include <tjp/core/containers/Optional.hpp>
#include "Data.hpp"
#include "Headers.hpp"
#include <tjp/core/system/System.h>
namespace tjp::core::internet {
constexpr size_t GeneralFailure = 0xFFFF;
using Header = Tuple<String, String>;
using Headers = Vector<Header>;
using StatusCode = s64;
using HeadersCallback = Function<void(const Headers &, StatusCode code)>;
using ProgressiveCallback = Function<void(char *, size_t)>;
using CompleteCallback = Function<void(StatusCode)>;
struct HttpOptions {
Headers headers;
time_t lastModifiedTime = 0;
time_t timeout = 0;
bool throwOnFailure = true;
size_t maxRedirects = 0;
Optional<String> httpVersion;
Optional<size_t> verbose;
HeadersCallback onHeaders;
ProgressiveCallback onData;
CompleteCallback onComplete;
} ;
namespace HttpStatusCodes {
const size_t NotModified = 304;
const size_t OK = 200;
} ;
inline
HttpOptions makeEmptyOptions() {
return {};
}
struct FileContents
{
Headers headers;
DataBlock data;
StatusCode statusCode = GeneralFailure;
time_t lastModifiedTime = 0;
bool ok() const
{
return statusCode < 400;
}
} ;
struct Exception : core::Exception {
using Super = core::Exception;
Optional<FileContents> contents;
Exception (
const String &what_,
Optional<FileContents> &&contents_
) :
Super(what_),
contents(std::move(contents_))
{
}
const Optional<FileContents> &getContents() const {
return contents;
}
} ;
Future<FileContents> get(const std::string &url, const HttpOptions &o = makeEmptyOptions());
Future<FileContents> post(const std::string &url, Data &&, const HttpOptions &o = makeEmptyOptions());
Future<FileContents> put(const std::string &url, Data &&, const HttpOptions &o = makeEmptyOptions());
} // namespace
#ifdef TJP_CORE_HEADER_ONLY
#if defined(SYS_IOS)
#include "ios/Internet.hpp"
#else
#include "curl/Internet.hpp"
#endif
#endif