1
0
Fork 0
mirror of https://github.com/KingDuckZ/kamokan.git synced 2025-02-17 09:35:49 +00:00

Step up work on responses.

This commit is contained in:
King_DuckZ 2017-04-06 21:27:38 +01:00
parent cc20a8ccfb
commit 98d98fc4ba
8 changed files with 77 additions and 32 deletions

View file

@ -17,6 +17,7 @@ add_executable(${PROJECT_NAME}
num_to_token.cpp
cgi_post.cpp
curl_wrapper.cpp
index_response.cpp
)
target_include_directories(${PROJECT_NAME} SYSTEM

21
src/index_response.cpp Normal file
View file

@ -0,0 +1,21 @@
#include "index_response.hpp"
namespace tawashi {
IndexResponse::IndexResponse() :
Response("text/html")
{
}
void IndexResponse::on_send (std::ostream& parStream) {
parStream <<
R"(
<form action="http://127.0.0.1:8080" method="POST" accept-charset="UTF-8">
<textarea name="tawashi" cols="80" rows="24"></textarea>
<br>
<button type="submit">tawashi</button>
</br>
</form>
)";
}
} //namespace tawashi

13
src/index_response.hpp Normal file
View file

@ -0,0 +1,13 @@
#pragma once
#include "response.hpp"
namespace tawashi {
class IndexResponse : public Response {
public:
IndexResponse();
private:
virtual void on_send (std::ostream& parStream) override;
};
} //namespace tawashi

View file

@ -1,7 +1,7 @@
#include "incredis/incredis.hpp"
#include "submit_form_response.hpp"
#include "index_response.hpp"
#include "cgi_env.hpp"
#include "cgi_post.hpp"
#include <iostream>
#include <string>
@ -15,28 +15,16 @@ int main() {
redis::IncRedis incredis("127.0.0.1", 6379);
tawashi::SubmitFormResponse resp;
resp.send();
tawashi::CGIEnv cgi_env;
for (auto& pair : cgi_env.query_string_split()) {
std::cout << "first:\t\"" << pair.first <<
"\"\tsecond:\t\"" << pair.second << "\"\n";
if (cgi_env.path_info() == "/index.cgi") {
tawashi::IndexResponse resp;
resp.send();
}
const std::size_t in_len = cgi_env.content_length();
std::cout << "\n<br>\n";
std::cout << "Content length: \"" << in_len << "\"\n<br>\n";
cgi_env.print_all(std::cout, "<br>\n");
for (auto& itm : tawashi::cgi::read_post(cgi_env)) {
std::cout << "Key: \"" << itm.first << "\"<br>\nValue: \"" <<
itm.second << "\"<br>\n";
else if (cgi_env.path_info() == "/paste.cgi") {
incredis.connect();
tawashi::SubmitFormResponse resp(incredis);
resp.send();
}
auto ver = cgi_env.gateway_interface();
if (ver)
std::cout << ver->name << " - v" << ver->major << ',' << ver->minor << "<br>\n";
return 0;
}

View file

@ -14,4 +14,8 @@ namespace tawashi {
this->on_send(std::cout);
std::cout.flush();
}
const CGIEnv& Response::cgi_env() const {
return m_cgi_env;
}
} //namespace tawashi

View file

@ -1,5 +1,6 @@
#pragma once
#include "cgi_env.hpp"
#include <string>
#include <iostream>
@ -12,10 +13,12 @@ namespace tawashi {
protected:
Response (std::string&& parType);
const CGIEnv& cgi_env() const;
private:
virtual void on_send (std::ostream& parStream) = 0;
CGIEnv m_cgi_env;
std::string m_content_type;
};
} //namespace tawashi

View file

@ -1,20 +1,30 @@
#include "submit_form_response.hpp"
#include "incredis/incredis.hpp"
#include "cgi_post.hpp"
namespace tawashi {
SubmitFormResponse::SubmitFormResponse() :
Response("text/html")
namespace {
const char g_post_key[] = "tawashi";
bool submit_to_redis (const std::string& parText) {
return true;
}
} //unnamed namespace
SubmitFormResponse::SubmitFormResponse (redis::IncRedis& parRedis) :
Response("text/html"),
m_redis(parRedis)
{
}
void SubmitFormResponse::on_send (std::ostream& parStream) {
parStream <<
R"(
<form action="http://127.0.0.1:8080" method="POST" accept-charset="UTF-8">
<textarea name="tawashi" cols="80" rows="24"></textarea>
<br>
<button type="submit">tawashi</button>
</br>
</form>
)";
auto post = cgi::read_post(cgi_env());
auto post_data_it = post.find(g_post_key);
if (post.end() != post_data_it) {
parStream << "can't find POST data\n";
}
else if (submit_to_redis(post_data_it->second)) {
parStream << "post submitted correctly\n";
}
}
} //namespace tawashi

View file

@ -2,12 +2,17 @@
#include "response.hpp"
namespace redis {
class IncRedis;
} //namespace redis
namespace tawashi {
class SubmitFormResponse : public Response {
public:
SubmitFormResponse();
explicit SubmitFormResponse (redis::IncRedis& parRedis);
private:
virtual void on_send (std::ostream& parStream) override;
redis::IncRedis& m_redis;
};
} //namespace tawashi