mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2024-12-27 21:35:41 +00:00
Use the new SplitMime in the HttpHeader.
Something like the old m_param string is still needed because Location responses need to send an URL. The type of the response now determines if m_mime or m_redirect_location is used.
This commit is contained in:
parent
5640cb5769
commit
24baf67a65
3 changed files with 45 additions and 16 deletions
|
@ -21,6 +21,7 @@
|
||||||
#include "sprout/array/array.hpp"
|
#include "sprout/array/array.hpp"
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <spdlog/spdlog.h>
|
#include <spdlog/spdlog.h>
|
||||||
|
#include <ciso646>
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -46,19 +47,26 @@ namespace tawashi {
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
HttpHeader::HttpHeader() :
|
HttpHeader::HttpHeader() :
|
||||||
m_param("text/html"),
|
m_mime {"text", "html", {}},
|
||||||
m_status_code(HttpStatusCodes::CodeNone),
|
m_status_code(HttpStatusCodes::CodeNone),
|
||||||
m_header_type(ContentType)
|
m_header_type(ContentType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHeader::HttpHeader (Types parType, HttpStatusCodes parCode, std::string&& parParam) :
|
HttpHeader::HttpHeader (Types parType, HttpStatusCodes parCode, SplitMime&& parMime) :
|
||||||
m_param(std::move(parParam)),
|
m_mime(std::move(parMime)),
|
||||||
m_status_code(parCode),
|
m_status_code(parCode),
|
||||||
m_header_type(parType)
|
m_header_type(parType)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
HttpHeader::HttpHeader (HttpStatusCodes parCode, std::string&& parRedirectLocation) :
|
||||||
|
m_redirect_location(std::move(parRedirectLocation)),
|
||||||
|
m_status_code(parCode),
|
||||||
|
m_header_type(Location)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void HttpHeader::set_status (HttpStatusCodes parCode) {
|
void HttpHeader::set_status (HttpStatusCodes parCode) {
|
||||||
m_status_code = parCode;
|
m_status_code = parCode;
|
||||||
}
|
}
|
||||||
|
@ -67,9 +75,9 @@ namespace tawashi {
|
||||||
m_status_code = HttpStatusCodes::CodeNone;
|
m_status_code = HttpStatusCodes::CodeNone;
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpHeader::set_type (Types parType, std::string&& parParameter) {
|
void HttpHeader::set_type (Types parType, SplitMime&& parParameter) {
|
||||||
m_header_type = parType;
|
m_header_type = parType;
|
||||||
m_param = std::move(parParameter);
|
m_mime = std::move(parParameter);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HttpHeader::body_required() const {
|
bool HttpHeader::body_required() const {
|
||||||
|
@ -90,11 +98,11 @@ namespace tawashi {
|
||||||
switch (parHeader.type()) {
|
switch (parHeader.type()) {
|
||||||
case HttpHeader::ContentType:
|
case HttpHeader::ContentType:
|
||||||
SPDLOG_TRACE(spdlog::get("statuslog"), "Response is a Content-type (data)");
|
SPDLOG_TRACE(spdlog::get("statuslog"), "Response is a Content-type (data)");
|
||||||
parStream << "Content-type: " << parHeader.parameter() << '\n';
|
parStream << "Content-type: " << mime_to_string(parHeader) << '\n';
|
||||||
break;
|
break;
|
||||||
case HttpHeader::Location:
|
case HttpHeader::Location:
|
||||||
SPDLOG_TRACE(spdlog::get("statuslog"), "Response is a Location (redirect)");
|
SPDLOG_TRACE(spdlog::get("statuslog"), "Response is a Location (redirect)");
|
||||||
parStream << "Location: " << parHeader.parameter() << '\n';
|
parStream << "Location: " << parHeader.redirect_location() << '\n';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
parStream << '\n';
|
parStream << '\n';
|
||||||
|
@ -102,10 +110,28 @@ namespace tawashi {
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHeader make_header_type_html() {
|
HttpHeader make_header_type_html() {
|
||||||
return HttpHeader(HttpHeader::ContentType, HttpStatusCodes::CodeNone, "text/html");
|
return HttpHeader(
|
||||||
|
HttpHeader::ContentType,
|
||||||
|
HttpStatusCodes::CodeNone,
|
||||||
|
SplitMime { "text", "html", {}}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHeader make_header_type_text_utf8() {
|
HttpHeader make_header_type_text_utf8() {
|
||||||
return HttpHeader(HttpHeader::ContentType, HttpStatusCodes::CodeNone, "text/plain; charset=utf-8");
|
return HttpHeader(
|
||||||
|
HttpHeader::ContentType,
|
||||||
|
HttpStatusCodes::CodeNone,
|
||||||
|
SplitMime {"text", "plain", MimeParametersMapType {{"charset", "utf-8"}}}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string mime_to_string (const HttpHeader& parHeader) {
|
||||||
|
bool write_ok;
|
||||||
|
std::string retval = mime_to_string(parHeader.mime(), write_ok);
|
||||||
|
if (not write_ok) {
|
||||||
|
assert(false);
|
||||||
|
return std::string();
|
||||||
|
}
|
||||||
|
return retval;
|
||||||
}
|
}
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -18,6 +18,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "enum.h"
|
#include "enum.h"
|
||||||
|
#include "mime_split.hpp"
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
@ -49,20 +50,23 @@ namespace tawashi {
|
||||||
HttpHeader();
|
HttpHeader();
|
||||||
HttpHeader (const HttpHeader&) = default;
|
HttpHeader (const HttpHeader&) = default;
|
||||||
HttpHeader (HttpHeader&&) = default;
|
HttpHeader (HttpHeader&&) = default;
|
||||||
HttpHeader (Types parType, HttpStatusCodes parCode, std::string&& parParam);
|
HttpHeader (Types parType, HttpStatusCodes parCode, SplitMime&& parMime);
|
||||||
|
HttpHeader (HttpStatusCodes parCode, std::string&& parRedirectLocation);
|
||||||
~HttpHeader() noexcept = default;
|
~HttpHeader() noexcept = default;
|
||||||
|
|
||||||
Types type() const { return m_header_type; }
|
Types type() const { return m_header_type; }
|
||||||
HttpStatusCodes status_code() const { return m_status_code; }
|
HttpStatusCodes status_code() const { return m_status_code; }
|
||||||
const std::string& parameter() const { return m_param; }
|
const std::string& redirect_location() const { return m_redirect_location; }
|
||||||
|
const SplitMime& mime() const { return m_mime; }
|
||||||
bool body_required() const;
|
bool body_required() const;
|
||||||
|
|
||||||
void set_status (HttpStatusCodes parCode);
|
void set_status (HttpStatusCodes parCode);
|
||||||
void unset_status();
|
void unset_status();
|
||||||
void set_type (Types parType, std::string&& parParameter);
|
void set_type (Types parType, SplitMime&& parParameter);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string m_param;
|
SplitMime m_mime;
|
||||||
|
std::string m_redirect_location;
|
||||||
HttpStatusCodes m_status_code;
|
HttpStatusCodes m_status_code;
|
||||||
Types m_header_type;
|
Types m_header_type;
|
||||||
};
|
};
|
||||||
|
@ -70,4 +74,5 @@ namespace tawashi {
|
||||||
std::ostream& operator<< (std::ostream& parStream, const HttpHeader& parHeader);
|
std::ostream& operator<< (std::ostream& parStream, const HttpHeader& parHeader);
|
||||||
HttpHeader make_header_type_html();
|
HttpHeader make_header_type_html();
|
||||||
HttpHeader make_header_type_text_utf8();
|
HttpHeader make_header_type_text_utf8();
|
||||||
|
[[gnu::pure]] std::string mime_to_string (const HttpHeader& parHeader);
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -36,8 +36,6 @@
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
namespace {
|
namespace {
|
||||||
const char g_def_response_type[] = "text/html";
|
|
||||||
|
|
||||||
//boost::string_ref fetch_page_basename (const cgi::Env& parEnv) {
|
//boost::string_ref fetch_page_basename (const cgi::Env& parEnv) {
|
||||||
// const boost::string_ref& path = parEnv.path_info();
|
// const boost::string_ref& path = parEnv.path_info();
|
||||||
|
|
||||||
|
@ -267,7 +265,7 @@ namespace tawashi {
|
||||||
HttpHeader Response::make_redirect (HttpStatusCodes parCode, const std::string& parLocation) {
|
HttpHeader Response::make_redirect (HttpStatusCodes parCode, const std::string& parLocation) {
|
||||||
std::ostringstream oss;
|
std::ostringstream oss;
|
||||||
oss << base_uri() << '/' << parLocation;
|
oss << base_uri() << '/' << parLocation;
|
||||||
return HttpHeader(HttpHeader::Location, parCode, oss.str());
|
return HttpHeader(parCode, oss.str());
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpHeader Response::make_error_redirect (ErrorReasons parReason) {
|
HttpHeader Response::make_error_redirect (ErrorReasons parReason) {
|
||||||
|
|
Loading…
Reference in a new issue