1
0
mirror of synced 2025-04-19 00:24:02 +03:00

Renamed Context to Connection. Removed DSL macro.

This commit is contained in:
yhirose 2012-09-27 21:05:36 -04:00
parent 6897c64c74
commit ced9c38339
5 changed files with 53 additions and 57 deletions

View File

@ -11,13 +11,17 @@ Server Example
Inspired by [Sinatra](http://www.sinatrarb.com/) Inspired by [Sinatra](http://www.sinatrarb.com/)
#include <httplib.h> #include <httplib.h>
using namespace httplib;
int main(void) { int main(void)
HTTP_SERVER("localhost", 1234) { {
GET("/hi", { Server svr("localhost", 1234);
res.set_content("Hello World!");
}); svr.get("/hi", [](Connection& c) {
} c.response.set_content("Hello World!");
});
svr.run();
} }
Copyright (c) 2012 Yuji Hirose. All rights reserved. Copyright (c) 2012 Yuji Hirose. All rights reserved.

View File

@ -6,14 +6,17 @@
// //
#include <httplib.h> #include <httplib.h>
using namespace httplib;
int main(void) int main(void)
{ {
HTTP_SERVER("localhost", 1234) /* svr_ */ { Server svr("localhost", 1234);
GET("/hi", /* req_, res_ */ {
res_.set_content("Hello World!"); svr.get("/hi", [](Connection& c) {
}); c.response.set_content("Hello World!");
} });
svr.run();
} }
// vim: et ts=4 sw=4 cin cino={1s ff=unix // vim: et ts=4 sw=4 cin cino={1s ff=unix

View File

@ -21,26 +21,27 @@ template<typename Fn> void signal(int sig, Fn fn)
int main(void) int main(void)
{ {
using namespace httplib;
const char* hi = "/hi"; const char* hi = "/hi";
HTTP_SERVER("localhost", 1234) /* svr_ */ { Server svr("localhost", 1234);
GET("/", { svr.get("/", [=](Connection& c) {
res_.set_redirect(hi); c.response.set_redirect(hi);
}); });
GET("/hi", { svr.get("/hi", [](Connection& c) {
res_.set_content("Hello World!"); c.response.set_content("Hello World!");
}); });
GET("/dump", { svr.get("/dump", [](Connection& c) {
res_.set_content(dump_request(cxt)); c.response.set_content(dump_request(c));
}); });
signal(SIGINT, [&](){ signal(SIGINT, [&]() { svr.stop(); });
svr_->stop();
}); svr.run();
}
} }
// vim: et ts=4 sw=4 cin cino={1s ff=unix // vim: et ts=4 sw=4 cin cino={1s ff=unix

View File

@ -69,7 +69,7 @@ struct Response {
void set_content(const std::string& s, const char* content_type = "text/plain"); void set_content(const std::string& s, const char* content_type = "text/plain");
}; };
struct Context { struct Connection {
Request request; Request request;
Response response; Response response;
}; };
@ -77,7 +77,7 @@ struct Context {
// HTTP server // HTTP server
class Server { class Server {
public: public:
typedef std::function<void (Context& context)> Handler; typedef std::function<void (Connection& c)> Handler;
Server(const char* ipaddr_or_hostname, int port); Server(const char* ipaddr_or_hostname, int port);
~Server(); ~Server();
@ -176,9 +176,9 @@ inline int close_server_socket(socket_t sock)
#endif #endif
} }
std::string dump_request(Context& cxt) std::string dump_request(Connection& c)
{ {
const auto& req = cxt.request; const auto& req = c.request;
std::string s; std::string s;
char buf[BUFSIZ]; char buf[BUFSIZ];
@ -413,61 +413,49 @@ inline void write_error(FILE* fp, int status)
inline void Server::process_request(FILE* fp_read, FILE* fp_write) inline void Server::process_request(FILE* fp_read, FILE* fp_write)
{ {
Context cxt; Connection c;
// Read and parse request line // Read and parse request line
if (!read_request_line(fp_read, cxt.request)) { if (!read_request_line(fp_read, c.request)) {
write_error(fp_write, 400); write_error(fp_write, 400);
return; return;
} }
// Read headers // Read headers
read_headers(fp_read, cxt.request.headers); read_headers(fp_read, c.request.headers);
printf("%s", dump_request(cxt).c_str()); printf("%s", dump_request(c).c_str());
// Routing // Routing
cxt.response.status = 404; c.response.status = 404;
if (cxt.request.method == "GET") { if (c.request.method == "GET") {
for (auto it = get_handlers_.begin(); it != get_handlers_.end(); ++it) { for (auto it = get_handlers_.begin(); it != get_handlers_.end(); ++it) {
const auto& pattern = it->first; const auto& pattern = it->first;
const auto& handler = it->second; const auto& handler = it->second;
std::smatch m; std::smatch m;
if (std::regex_match(cxt.request.url, m, pattern)) { if (std::regex_match(c.request.url, m, pattern)) {
for (size_t i = 1; i < m.size(); i++) { for (size_t i = 1; i < m.size(); i++) {
cxt.request.params.push_back(m[i]); c.request.params.push_back(m[i]);
} }
handler(cxt); handler(c);
break; break;
} }
} }
} else if (cxt.request.method == "POST") { } else if (c.request.method == "POST") {
// TODO: parse body // TODO: parse body
} else { } else {
cxt.response.status = 400; c.response.status = 400;
} }
if (200 <= cxt.response.status && cxt.response.status < 400) { if (200 <= c.response.status && c.response.status < 400) {
write_response(fp_write, cxt.response); write_response(fp_write, c.response);
} else { } else {
write_error(fp_write, cxt.response.status); write_error(fp_write, c.response.status);
} }
} }
#define HTTP_SERVER(host, port) \
for (std::shared_ptr<httplib::Server> svr_ = std::make_shared<httplib::Server>(host, port); \
svr_; \
svr_->run(), svr_.reset())
#define GET(url, body) \
svr_->get(url, [&](httplib::Context& cxt) { \
const auto& req_ = cxt.request; \
auto& res_ = cxt.response; \
body \
});
} // namespace httplib } // namespace httplib
#endif #endif

View File

@ -55,8 +55,8 @@ TEST(ServerTest, GetMethod)
{ {
Server svr("localhost", 1914); Server svr("localhost", 1914);
svr.get("hi", [&](httplib::Context& cxt) { svr.get("hi", [&](httplib::Connection& c) {
cxt.response.set_content("Hello World!"); c.response.set_content("Hello World!");
}); });
svr.on_ready([&]() { svr.on_ready([&]() {