284 lines
5.8 KiB
C++
284 lines
5.8 KiB
C++
#include "HTTPObjects.h"
|
|
#include "HTTPObjectsBridge.h"
|
|
|
|
namespace uns
|
|
{
|
|
UrlQuery::UrlQuery() : pimpl(std::make_unique<Impl>(webcc::UrlQuery{ "" }))
|
|
{
|
|
}
|
|
|
|
UrlQuery::UrlQuery(std::unique_ptr<Impl> impl) : pimpl(std::move(impl))
|
|
{
|
|
}
|
|
|
|
UrlQuery::~UrlQuery() = default;
|
|
|
|
UrlQuery::UrlQuery(const UrlQuery& other)
|
|
: pimpl(std::make_unique<Impl>(other.pimpl->webcc_query))
|
|
{
|
|
}
|
|
|
|
UrlQuery& UrlQuery::operator=(const UrlQuery& other)
|
|
{
|
|
if (this != &other)
|
|
{
|
|
pimpl = std::make_unique<Impl>(other.pimpl->webcc_query);
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
UrlQuery::UrlQuery(UrlQuery&& other) noexcept = default;
|
|
UrlQuery& UrlQuery::operator=(UrlQuery&& other) noexcept = default;
|
|
|
|
bool UrlQuery::Empty() const
|
|
{
|
|
return pimpl->webcc_query.Empty();
|
|
}
|
|
|
|
std::size_t UrlQuery::Size() const
|
|
{
|
|
return pimpl->webcc_query.Size();
|
|
}
|
|
|
|
bool UrlQuery::Has(const std::string& key) const
|
|
{
|
|
return pimpl->webcc_query.Has(key);
|
|
}
|
|
|
|
const std::string& UrlQuery::Get(const std::string& key) const
|
|
{
|
|
return pimpl->webcc_query.Get(key);
|
|
}
|
|
|
|
std::pair<std::string, std::string> UrlQuery::Get(std::size_t index) const
|
|
{
|
|
const auto& raw_param = pimpl->webcc_query.Get(index);
|
|
return { raw_param.first, raw_param.second };
|
|
}
|
|
|
|
std::string UrlQuery::ToString(bool encode) const
|
|
{
|
|
return pimpl->webcc_query.ToString(encode);
|
|
}
|
|
}
|
|
|
|
uns::FormPart::FormPart(std::unique_ptr<Impl> impl) : pimpl(std::move(impl))
|
|
{
|
|
}
|
|
|
|
uns::FormPart::~FormPart() = default;
|
|
|
|
std::string uns::FormPart::GetNameS() const
|
|
{
|
|
return pimpl->webcc_form->name();
|
|
}
|
|
|
|
std::string_view uns::FormPart::GetName() const
|
|
{
|
|
return pimpl->webcc_form->name();
|
|
}
|
|
|
|
std::string uns::FormPart::GetFileNameS() const
|
|
{
|
|
return pimpl->webcc_form->file_name();
|
|
}
|
|
|
|
std::string_view uns::FormPart::GetFileName() const
|
|
{
|
|
return pimpl->webcc_form->file_name();
|
|
}
|
|
|
|
std::string uns::FormPart::GetMediaTypeS() const
|
|
{
|
|
return pimpl->webcc_form->media_type();
|
|
}
|
|
|
|
std::string_view uns::FormPart::GetMediaType() const
|
|
{
|
|
return pimpl->webcc_form->media_type();
|
|
}
|
|
|
|
const std::string& uns::FormPart::GetData() const
|
|
{
|
|
return pimpl->webcc_form->data();
|
|
}
|
|
|
|
std::size_t uns::FormPart::GetSize() const
|
|
{
|
|
// 注意:虽然 webcc 原生的 GetSize() 没有写 const 修饰符,
|
|
// 但由于 pimpl 内部存的是智能指针,这里依然可以在 const 函数里安全调用它
|
|
return pimpl->webcc_form->GetSize();
|
|
}
|
|
|
|
std::size_t uns::FormPart::GetDataSize() const
|
|
{
|
|
return pimpl->webcc_form->GetDataSize();
|
|
}
|
|
|
|
uns::Request::Request(std::unique_ptr<Impl> impl) : pimpl(std::move(impl))
|
|
{
|
|
}
|
|
|
|
uns::Request::~Request() = default;
|
|
|
|
namespace uns
|
|
{
|
|
std::string Request::GetMethodS() const
|
|
{
|
|
return pimpl->webcc_req->method();
|
|
}
|
|
|
|
std::string_view Request::GetMethod() const
|
|
{
|
|
return pimpl->webcc_req->method();
|
|
}
|
|
|
|
std::string Request::GetAddressS() const
|
|
{
|
|
return pimpl->webcc_req->address();
|
|
}
|
|
|
|
std::string_view Request::GetAddress() const
|
|
{
|
|
return pimpl->webcc_req->address();
|
|
}
|
|
|
|
size_t Request::GetContentLength() const
|
|
{
|
|
return pimpl->webcc_req->content_length();
|
|
}
|
|
|
|
std::string Request::GetDataS() const
|
|
{
|
|
return pimpl->webcc_req->data();
|
|
}
|
|
|
|
std::string_view Request::GetData() const
|
|
{
|
|
return pimpl->webcc_req->data();
|
|
}
|
|
|
|
std::string Request::GetSchemeS() const
|
|
{
|
|
return pimpl->webcc_req->url().scheme();
|
|
}
|
|
|
|
std::string_view Request::GetScheme() const
|
|
{
|
|
return pimpl->webcc_req->url().scheme();
|
|
}
|
|
|
|
std::string Request::GetHostS() const
|
|
{
|
|
return pimpl->webcc_req->host();
|
|
}
|
|
|
|
std::string_view Request::GetHost() const
|
|
{
|
|
return pimpl->webcc_req->host();
|
|
}
|
|
|
|
int Request::GetPortI() const
|
|
{
|
|
try
|
|
{
|
|
return std::stoi(pimpl->webcc_req->port(), nullptr, 10);
|
|
}
|
|
catch(const std::exception&)
|
|
{
|
|
return -1;
|
|
}
|
|
return -2;
|
|
}
|
|
|
|
std::string Request::GetPortS() const
|
|
{
|
|
return pimpl->webcc_req->port();
|
|
}
|
|
|
|
std::string_view Request::GetPort() const
|
|
{
|
|
return pimpl->webcc_req->port();
|
|
}
|
|
|
|
std::string Request::GetPathS() const
|
|
{
|
|
return pimpl->webcc_req->url().path();
|
|
}
|
|
|
|
std::string_view Request::GetPath() const
|
|
{
|
|
return pimpl->webcc_req->url().path();
|
|
}
|
|
|
|
std::string Request::GetQueryStringS() const
|
|
{
|
|
return pimpl->webcc_req->url().query();
|
|
}
|
|
|
|
std::string_view Request::GetQueryString() const
|
|
{
|
|
return pimpl->webcc_req->url().query();
|
|
}
|
|
|
|
UrlQuery Request::GetQuery() const
|
|
{
|
|
auto query_impl = std::make_unique<UrlQuery::Impl>(pimpl->webcc_req->query());
|
|
return UrlQuery(std::move(query_impl));
|
|
}
|
|
|
|
const std::vector<std::string>& Request::GetArgs() const
|
|
{
|
|
return pimpl->webcc_req->args();
|
|
}
|
|
|
|
bool Request::IsForm() const
|
|
{
|
|
return pimpl->webcc_req->IsForm();
|
|
}
|
|
|
|
const std::vector<FormPartPtr>& Request::GetFormParts() const
|
|
{
|
|
// 懒加载影子转换:仅在第一次访问且确属 Form 请求时构建镜像包装
|
|
if (!pimpl->form_parts_cached)
|
|
{
|
|
if (pimpl->webcc_req->IsForm())
|
|
{
|
|
const auto& raw_parts = pimpl->webcc_req->form_parts();
|
|
pimpl->cached_form_parts.reserve(raw_parts.size());
|
|
|
|
for (const auto& raw_part : raw_parts)
|
|
{
|
|
auto part_impl = std::make_unique<FormPart::Impl>(raw_part);
|
|
// 由于 Request 是 FormPart 的友元,这里可以直接通过 new 访问其私有构造函数
|
|
auto uns_part = std::shared_ptr<FormPart>(new FormPart(std::move(part_impl)));
|
|
pimpl->cached_form_parts.push_back(uns_part);
|
|
}
|
|
}
|
|
pimpl->form_parts_cached = true;
|
|
}
|
|
return pimpl->cached_form_parts;
|
|
}
|
|
|
|
bool Request::HasHeader(std::string_view header) const
|
|
{
|
|
return pimpl->webcc_req->HasHeader(header);
|
|
}
|
|
|
|
std::string Request::GetHeader(std::string_view header, bool* existed) const
|
|
{
|
|
return pimpl->webcc_req->GetHeader(header, existed);
|
|
}
|
|
|
|
std::vector<std::pair<std::string, std::string>> Request::GetAllHeaders() const
|
|
{
|
|
return pimpl->webcc_req->GetAllHeaders().data();
|
|
}
|
|
}
|
|
|
|
uns::Response::Response(std::unique_ptr<Impl> impl) : pimpl(std::move(impl))
|
|
{
|
|
}
|
|
|
|
uns::Response::~Response() = default;
|