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/)
#include <httplib.h>
using namespace httplib;
int main(void) {
HTTP_SERVER("localhost", 1234) {
GET("/hi", {
res.set_content("Hello World!");
});
}
int main(void)
{
Server svr("localhost", 1234);
svr.get("/hi", [](Connection& c) {
c.response.set_content("Hello World!");
});
svr.run();
}
Copyright (c) 2012 Yuji Hirose. All rights reserved.

View file

@ -6,14 +6,17 @@
//
#include <httplib.h>
using namespace httplib;
int main(void)
{
HTTP_SERVER("localhost", 1234) /* svr_ */ {
GET("/hi", /* req_, res_ */ {
res_.set_content("Hello World!");
});
}
Server svr("localhost", 1234);
svr.get("/hi", [](Connection& c) {
c.response.set_content("Hello World!");
});
svr.run();
}
// 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)
{
using namespace httplib;
const char* hi = "/hi";
HTTP_SERVER("localhost", 1234) /* svr_ */ {
Server svr("localhost", 1234);
GET("/", {
res_.set_redirect(hi);
});
svr.get("/", [=](Connection& c) {
c.response.set_redirect(hi);
});
GET("/hi", {
res_.set_content("Hello World!");
});
svr.get("/hi", [](Connection& c) {
c.response.set_content("Hello World!");
});
GET("/dump", {
res_.set_content(dump_request(cxt));
});
svr.get("/dump", [](Connection& c) {
c.response.set_content(dump_request(c));
});
signal(SIGINT, [&](){
svr_->stop();
});
}
signal(SIGINT, [&]() { svr.stop(); });
svr.run();
}
// 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");
};
struct Context {
struct Connection {
Request request;
Response response;
};
@ -77,7 +77,7 @@ struct Context {
// HTTP server
class Server {
public:
typedef std::function<void (Context& context)> Handler;
typedef std::function<void (Connection& c)> Handler;
Server(const char* ipaddr_or_hostname, int port);
~Server();
@ -176,9 +176,9 @@ inline int close_server_socket(socket_t sock)
#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;
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)
{
Context cxt;
Connection c;
// 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);
return;
}
// 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
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) {
const auto& pattern = it->first;
const auto& handler = it->second;
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++) {
cxt.request.params.push_back(m[i]);
c.request.params.push_back(m[i]);
}
handler(cxt);
handler(c);
break;
}
}
} else if (cxt.request.method == "POST") {
} else if (c.request.method == "POST") {
// TODO: parse body
} else {
cxt.response.status = 400;
c.response.status = 400;
}
if (200 <= cxt.response.status && cxt.response.status < 400) {
write_response(fp_write, cxt.response);
if (200 <= c.response.status && c.response.status < 400) {
write_response(fp_write, c.response);
} 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
#endif

View file

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