mirror of
https://github.com/KingDuckZ/kamokan.git
synced 2025-06-07 00:51:41 +00:00
Step up work on responses.
This commit is contained in:
parent
cc20a8ccfb
commit
98d98fc4ba
8 changed files with 77 additions and 32 deletions
|
@ -17,6 +17,7 @@ add_executable(${PROJECT_NAME}
|
||||||
num_to_token.cpp
|
num_to_token.cpp
|
||||||
cgi_post.cpp
|
cgi_post.cpp
|
||||||
curl_wrapper.cpp
|
curl_wrapper.cpp
|
||||||
|
index_response.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${PROJECT_NAME} SYSTEM
|
target_include_directories(${PROJECT_NAME} SYSTEM
|
||||||
|
|
21
src/index_response.cpp
Normal file
21
src/index_response.cpp
Normal 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
13
src/index_response.hpp
Normal 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
|
28
src/main.cpp
28
src/main.cpp
|
@ -1,7 +1,7 @@
|
||||||
#include "incredis/incredis.hpp"
|
#include "incredis/incredis.hpp"
|
||||||
#include "submit_form_response.hpp"
|
#include "submit_form_response.hpp"
|
||||||
|
#include "index_response.hpp"
|
||||||
#include "cgi_env.hpp"
|
#include "cgi_env.hpp"
|
||||||
#include "cgi_post.hpp"
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
@ -15,28 +15,16 @@ int main() {
|
||||||
|
|
||||||
redis::IncRedis incredis("127.0.0.1", 6379);
|
redis::IncRedis incredis("127.0.0.1", 6379);
|
||||||
|
|
||||||
tawashi::SubmitFormResponse resp;
|
|
||||||
resp.send();
|
|
||||||
|
|
||||||
tawashi::CGIEnv cgi_env;
|
tawashi::CGIEnv cgi_env;
|
||||||
for (auto& pair : cgi_env.query_string_split()) {
|
if (cgi_env.path_info() == "/index.cgi") {
|
||||||
std::cout << "first:\t\"" << pair.first <<
|
tawashi::IndexResponse resp;
|
||||||
"\"\tsecond:\t\"" << pair.second << "\"\n";
|
resp.send();
|
||||||
}
|
}
|
||||||
|
else if (cgi_env.path_info() == "/paste.cgi") {
|
||||||
const std::size_t in_len = cgi_env.content_length();
|
incredis.connect();
|
||||||
std::cout << "\n<br>\n";
|
tawashi::SubmitFormResponse resp(incredis);
|
||||||
std::cout << "Content length: \"" << in_len << "\"\n<br>\n";
|
resp.send();
|
||||||
|
|
||||||
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";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto ver = cgi_env.gateway_interface();
|
|
||||||
if (ver)
|
|
||||||
std::cout << ver->name << " - v" << ver->major << ',' << ver->minor << "<br>\n";
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,4 +14,8 @@ namespace tawashi {
|
||||||
this->on_send(std::cout);
|
this->on_send(std::cout);
|
||||||
std::cout.flush();
|
std::cout.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CGIEnv& Response::cgi_env() const {
|
||||||
|
return m_cgi_env;
|
||||||
|
}
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "cgi_env.hpp"
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
@ -12,10 +13,12 @@ namespace tawashi {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Response (std::string&& parType);
|
Response (std::string&& parType);
|
||||||
|
const CGIEnv& cgi_env() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void on_send (std::ostream& parStream) = 0;
|
virtual void on_send (std::ostream& parStream) = 0;
|
||||||
|
|
||||||
|
CGIEnv m_cgi_env;
|
||||||
std::string m_content_type;
|
std::string m_content_type;
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -1,20 +1,30 @@
|
||||||
#include "submit_form_response.hpp"
|
#include "submit_form_response.hpp"
|
||||||
|
#include "incredis/incredis.hpp"
|
||||||
|
#include "cgi_post.hpp"
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
SubmitFormResponse::SubmitFormResponse() :
|
namespace {
|
||||||
Response("text/html")
|
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) {
|
void SubmitFormResponse::on_send (std::ostream& parStream) {
|
||||||
parStream <<
|
auto post = cgi::read_post(cgi_env());
|
||||||
R"(
|
auto post_data_it = post.find(g_post_key);
|
||||||
<form action="http://127.0.0.1:8080" method="POST" accept-charset="UTF-8">
|
if (post.end() != post_data_it) {
|
||||||
<textarea name="tawashi" cols="80" rows="24"></textarea>
|
parStream << "can't find POST data\n";
|
||||||
<br>
|
}
|
||||||
<button type="submit">tawashi</button>
|
else if (submit_to_redis(post_data_it->second)) {
|
||||||
</br>
|
parStream << "post submitted correctly\n";
|
||||||
</form>
|
}
|
||||||
)";
|
|
||||||
}
|
}
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
|
@ -2,12 +2,17 @@
|
||||||
|
|
||||||
#include "response.hpp"
|
#include "response.hpp"
|
||||||
|
|
||||||
|
namespace redis {
|
||||||
|
class IncRedis;
|
||||||
|
} //namespace redis
|
||||||
|
|
||||||
namespace tawashi {
|
namespace tawashi {
|
||||||
class SubmitFormResponse : public Response {
|
class SubmitFormResponse : public Response {
|
||||||
public:
|
public:
|
||||||
SubmitFormResponse();
|
explicit SubmitFormResponse (redis::IncRedis& parRedis);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual void on_send (std::ostream& parStream) override;
|
virtual void on_send (std::ostream& parStream) override;
|
||||||
|
redis::IncRedis& m_redis;
|
||||||
};
|
};
|
||||||
} //namespace tawashi
|
} //namespace tawashi
|
||||||
|
|
Loading…
Add table
Reference in a new issue