1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2024-12-27 21:35:41 +00:00

Use base_uri in the response

This commit is contained in:
King_DuckZ 2017-04-11 19:36:18 +01:00
parent 41e1d35c7a
commit da2484b0d4
9 changed files with 31 additions and 17 deletions

View file

@ -16,23 +16,26 @@
*/
#include "index_response.hpp"
#include <boost/algorithm/string/replace.hpp>
namespace tawashi {
IndexResponse::IndexResponse() :
Response(Response::ContentType, "text/html")
IndexResponse::IndexResponse (const boost::string_ref& parBaseURI) :
Response(Response::ContentType, "text/html", parBaseURI)
{
}
void IndexResponse::on_send (std::ostream& parStream) {
parStream <<
R"(
std::string html(R"(
<form action="http://127.0.0.1:8080/paste.cgi" method="POST" accept-charset="UTF-8">
<textarea name="pastie" cols="80" rows="24"></textarea>
<br>
<button type="submit">tawashi</button>
</br>
</form>
)";
)");
boost::replace_all(html, "{base_uri}", base_uri());
parStream << html;
}
} //namespace tawashi

View file

@ -18,11 +18,12 @@
#pragma once
#include "response.hpp"
#include <boost/utility/string_ref.hpp>
namespace tawashi {
class IndexResponse : public Response {
public:
IndexResponse();
explicit IndexResponse (const boost::string_ref& parBaseURI);
private:
virtual void on_send (std::ostream& parStream) override;

View file

@ -67,16 +67,17 @@ int main() {
incredis.connect();
tawashi::cgi::Env cgi_env;
const boost::string_ref& base_uri = settings.at("base_uri");
if (cgi_env.path_info() == "/index.cgi") {
tawashi::IndexResponse resp;
tawashi::IndexResponse resp(base_uri);
resp.send();
}
else if (cgi_env.path_info() == "/paste.cgi") {
tawashi::SubmitPasteResponse resp(incredis);
tawashi::SubmitPasteResponse resp(incredis, base_uri);
resp.send();
}
else {
tawashi::PastieResponse resp(incredis);
tawashi::PastieResponse resp(incredis, base_uri);
resp.send();
}

View file

@ -23,8 +23,8 @@
#include <sstream>
namespace tawashi {
PastieResponse::PastieResponse (redis::IncRedis& parRedis) :
Response(Response::ContentType, "text/html"),
PastieResponse::PastieResponse (redis::IncRedis& parRedis, const boost::string_ref& parBaseURI) :
Response(Response::ContentType, "text/html", parBaseURI),
m_redis(parRedis),
m_plain_text(false)
{

View file

@ -19,6 +19,7 @@
#include "response.hpp"
#include <string>
#include <boost/utility/string_ref.hpp>
namespace redis {
class IncRedis;
@ -27,7 +28,7 @@ namespace redis {
namespace tawashi {
class PastieResponse : public Response {
public:
PastieResponse (redis::IncRedis& parRedis);
PastieResponse (redis::IncRedis& parRedis, const boost::string_ref& parBaseURI);
private:
virtual void on_process() override;

View file

@ -20,8 +20,9 @@
#include <cassert>
namespace tawashi {
Response::Response (Types parRespType, std::string&& parValue) :
Response::Response (Types parRespType, std::string&& parValue, const boost::string_ref& parBaseURI) :
m_resp_value(std::move(parValue)),
m_base_uri(parBaseURI),
m_resp_type(parRespType),
m_header_sent(false)
{
@ -60,4 +61,8 @@ namespace tawashi {
m_resp_type = parRespType;
m_resp_value = std::move(parValue);
}
const boost::string_ref& Response::base_uri() const {
return m_base_uri;
}
} //namespace tawashi

View file

@ -20,6 +20,7 @@
#include "cgi_env.hpp"
#include <string>
#include <iostream>
#include <boost/utility/string_ref.hpp>
namespace tawashi {
class Response {
@ -34,9 +35,10 @@ namespace tawashi {
Location
};
Response (Types parRespType, std::string&& parValue);
Response (Types parRespType, std::string&& parValue, const boost::string_ref& parBaseURI);
const cgi::Env& cgi_env() const;
void change_type (Types parRespType, std::string&& parValue);
const boost::string_ref& base_uri() const;
private:
virtual void on_process();
@ -44,6 +46,7 @@ namespace tawashi {
cgi::Env m_cgi_env;
std::string m_resp_value;
boost::string_ref m_base_uri;
Types m_resp_type;
bool m_header_sent;
};

View file

@ -27,8 +27,8 @@ namespace tawashi {
const char g_post_key[] = "pastie";
} //unnamed namespace
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis) :
Response(Response::ContentType, "text/plain"),
SubmitPasteResponse::SubmitPasteResponse (redis::IncRedis& parRedis, const boost::string_ref& parBaseURI) :
Response(Response::ContentType, "text/plain", parBaseURI),
m_redis(parRedis)
{
}

View file

@ -28,7 +28,7 @@ namespace redis {
namespace tawashi {
class SubmitPasteResponse : public Response {
public:
explicit SubmitPasteResponse (redis::IncRedis& parRedis);
SubmitPasteResponse (redis::IncRedis& parRedis, const boost::string_ref& parBaseURI);
private:
virtual void on_process() override;