1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-08-07 12:59:45 +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 "index_response.hpp"
#include <boost/algorithm/string/replace.hpp>
namespace tawashi { namespace tawashi {
IndexResponse::IndexResponse() : IndexResponse::IndexResponse (const boost::string_ref& parBaseURI) :
Response(Response::ContentType, "text/html") Response(Response::ContentType, "text/html", parBaseURI)
{ {
} }
void IndexResponse::on_send (std::ostream& parStream) { void IndexResponse::on_send (std::ostream& parStream) {
parStream << std::string html(R"(
R"(
<form action="http://127.0.0.1:8080/paste.cgi" method="POST" accept-charset="UTF-8"> <form action="http://127.0.0.1:8080/paste.cgi" method="POST" accept-charset="UTF-8">
<textarea name="pastie" cols="80" rows="24"></textarea> <textarea name="pastie" cols="80" rows="24"></textarea>
<br> <br>
<button type="submit">tawashi</button> <button type="submit">tawashi</button>
</br> </br>
</form> </form>
)"; )");
boost::replace_all(html, "{base_uri}", base_uri());
parStream << html;
} }
} //namespace tawashi } //namespace tawashi

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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