Skip to content

Commit

Permalink
Merge pull request #65 from qicosmos/branch_add
Browse files Browse the repository at this point in the history
Branch add
  • Loading branch information
qicosmos authored Jul 31, 2018
2 parents 6db8738 + faee5ed commit 26689e3
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ namespace cinatra {
do_read_head();
}
else {
if (req_.get_method() == "GET"&&http_cache::need_cache()&&!http_cache::not_cache(req_.get_url())) {
if (req_.get_method() == "GET"&&http_cache::need_cache(req_.get_url())&&!http_cache::not_cache(req_.get_url())) {
auto raw_url = req_.raw_url();
if (!http_cache::empty()) {
auto resp_vec = http_cache::get(std::string(raw_url.data(), raw_url.length()));
Expand Down Expand Up @@ -332,7 +332,7 @@ namespace cinatra {
}

//cache
if (req_.get_method() == "GET"&&http_cache::need_cache() && !http_cache::not_cache(req_.get_url())) {
if (req_.get_method() == "GET"&&http_cache::need_cache(req_.get_url()) && !http_cache::not_cache(req_.get_url())) {
auto raw_url = req_.raw_url();
http_cache::add(std::string(raw_url.data(), raw_url.length()), res_.raw_content());
}
Expand Down
15 changes: 13 additions & 2 deletions http_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,21 @@ namespace cinatra {
skip_cache_.emplace(key);
}

static void add_single_cache(std::string_view key)
{
need_single_cache_.emplace(key);
}

static void enable_cache(bool b) {
need_cache_ = b;
}

static bool need_cache() {
return need_cache_;
static bool need_cache(std::string_view key) {
if(need_cache_){
return need_cache_;
}else{
return need_single_cache_.find(key)!= need_single_cache_.end();
}
}

static bool not_cache(std::string_view key) {
Expand All @@ -80,6 +89,7 @@ namespace cinatra {
static std::unordered_map<std::string, std::vector<std::string>> cache_;
static std::unordered_map<std::string, std::vector<std::string>>::iterator cur_it_;
static std::unordered_set<std::string_view> skip_cache_;
static std::unordered_set<std::string_view> need_single_cache_;
static std::time_t max_cache_age_;
static std::unordered_map<std::string, std::time_t > cache_time_;
};
Expand All @@ -91,4 +101,5 @@ namespace cinatra {
std::unordered_set<std::string_view> http_cache::skip_cache_;
std::time_t http_cache::max_cache_age_ = 0;
std::unordered_map<std::string, std::time_t > http_cache::cache_time_;
std::unordered_set<std::string_view> http_cache::need_single_cache_;
}
5 changes: 4 additions & 1 deletion http_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ namespace cinatra {
((b&&(b = need_cache(std::forward<AP>(ap))), false),...);
if (!b) {
http_cache::add_skip(name);
}else{
http_cache::add_single_cache(name);
}
auto tp = filter<enable_cache<bool>>(std::forward<AP>(ap)...);
auto lm = [this, name, f = std::move(f)](auto... ap) {
Expand Down Expand Up @@ -268,7 +270,7 @@ namespace cinatra {
}
break;
}
});
},enable_cache{false});
}

bool is_small_file(std::ifstream* in,request& req) const {
Expand Down Expand Up @@ -341,6 +343,7 @@ namespace cinatra {
set_static_res_handler();
http_handler_ = [this](request& req, response& res) {
res.set_base_path(this->base_path_[0],this->base_path_[1]);
res.set_url(req.get_url());
bool success = http_router_.route(req.get_method(), req.get_url(), req, res);
if (!success) {
res.set_status_and_content(status_type::bad_request, "the url is not right");
Expand Down
55 changes: 15 additions & 40 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ struct log_t

struct check {
bool before(request& req, response& res) {
/*std::cout << "before check" << std::endl;
std::cout << "before check" << std::endl;
if (req.get_header_value("name").empty()) {
res.set_status_and_content(status_type::bad_request);
res.render_404();
return false;
}*/
}

return true;
}
Expand Down Expand Up @@ -50,16 +50,16 @@ int main() {
}

server.set_base_path("base_path","/feather");
server.enable_http_cache(true);//set global cache
server.enable_http_cache(false);//set global cache
server.set_res_cache_max_age(86400);
server.set_cache_max_age(5);
server.set_http_handler<GET, POST>("/", [](request& req, response& res) {
res.set_status_and_content(status_type::ok, "hello world");
res.set_status_and_content(status_type::ok,"hello world");
},enable_cache{false});

server.set_http_handler<GET, POST>("/string", [](request& req, response& res) {
res.render_string("OK");
},enable_cache{false});
res.render_string(std::to_string(std::time(nullptr)));
},enable_cache{true});

server.set_http_handler<GET, POST>("/404", [](request& req, response& res) {
res.render_404();
Expand Down Expand Up @@ -94,29 +94,8 @@ int main() {
// json["test_text"] = "hello,world";
// json["header_text"] = "你好 cinatra";
res.render_view("./www/test.html");
/*
* ---------------------test.html---------------------------
<html>
<head>
<meta charset="utf-8">
</head>
<body>
{% include "./header/header.html" %}
<h1>{{test_text}}</h1>
</body>
</html>
----------------------------------header.html---------------------
<div>{{header_text}}</div>
*/
});

// server.set_http_handler<GET,POST>("/test_remove",[](request& req, response& res){
// fs::remove(fs::path("./abc.txt"));
// res.set_status_and_content(status_type::ok, "OK",res_content_type::string);
// });


server.set_http_handler<GET, POST>("/json", [](request& req, response& res) {
inja::json json;
json["abc"] = "abc";
Expand All @@ -133,7 +112,7 @@ int main() {

server.set_http_handler<GET, POST>("/pathinfo/*", [](request& req, response& res) {
auto s = req.get_query_value(0);
res.set_status_and_content(status_type::ok, std::string(s.data(), s.length()),res_content_type::string);
res.render_string(std::string(s.data(), s.length()));
});

server.set_http_handler<GET, POST>("/restype", [](request& req, response& res) {
Expand All @@ -154,36 +133,34 @@ int main() {

server.set_http_handler<GET, POST>("/getzh", [](request& req, response& res) {
auto zh = req.get_query_value("zh");
res.set_status_and_content(status_type::ok, std::string(zh.data(),zh.size()), res_content_type::string);
res.render_string(std::string(zh.data(),zh.size()));
});

server.set_http_handler<GET, POST>("/gzip", [](request& req, response& res) {
auto body = req.body();
std::cout << body.data() << std::endl;

res.set_status_and_content(status_type::ok, "hello world", res_content_type::none, content_encoding::gzip);
});


server.set_http_handler<GET, POST>("/test", [](request& req, response& res) {
auto name = req.get_header_value("name");
if (name.empty()) {
res.set_status_and_content(status_type::bad_request, "no name");
res.render_string("no name");
return;
}

auto id = req.get_query_value("id");
if (id.empty()) {
res.set_status_and_content(status_type::bad_request);
res.render_404();
return;
}

res.set_status_and_content(status_type::ok, "hello world");
res.render_string("hello world");
});

//aspect
server.set_http_handler<GET, POST>("/aspect", [](request& req, response& res) {
res.set_status_and_content(status_type::ok, "hello world");
res.render_string("hello world");
}, check{}, log_t{});

//web socket
Expand Down Expand Up @@ -220,8 +197,7 @@ int main() {
for (auto& file : files) {
std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
}

res.set_status_and_content(status_type::ok, "multipart finished");
res.render_string("multipart finished");
});

//http upload(octet-stream)
Expand All @@ -231,8 +207,7 @@ int main() {
for (auto& file : files) {
std::cout << file.get_file_path() << " " << file.get_file_size() << std::endl;
}

res.set_status_and_content(status_type::ok, "octet-stream finished");
res.render_string("octet-stream finished");
});

//chunked download
Expand Down
13 changes: 12 additions & 1 deletion response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ namespace cinatra {
buffers.emplace_back(boost::asio::buffer(content_.data(), content_.size()));
}

if (http_cache::need_cache()) {
if (http_cache::need_cache(raw_url_)) {
cache_data.clear();
for (auto& buf : buffers) {
cache_data.push_back(std::string(boost::asio::buffer_cast<const char*>(buf),boost::asio::buffer_size(buf)));
Expand Down Expand Up @@ -213,6 +213,16 @@ namespace cinatra {
return path_;
}

void set_url(std::string_view url)
{
raw_url_ = url;
}

std::string_view get_url(std::string_view url)
{
return raw_url_;
}

void handle_render_view(const std::string& tpl_file_path,const nlohmann::json& tmp_data,status_type server_type =status_type::ok )
{
inja::Environment env = inja::Environment("./");
Expand Down Expand Up @@ -304,6 +314,7 @@ namespace cinatra {
private:

//std::map<std::string, std::string, ci_less> headers_;
std::string_view raw_url_;
std::vector<std::pair<std::string, std::string>> headers_;
std::vector<std::string> cache_data;
std::string content_;
Expand Down

0 comments on commit 26689e3

Please sign in to comment.