even more weird
parent
b8a3c2efec
commit
7eb10d44db
6
Makefile
6
Makefile
|
@ -2,8 +2,8 @@
|
|||
|
||||
PREFIX?=$(HOME)
|
||||
|
||||
BOOST_INCLUDE?=/opt/local/include
|
||||
BOOST_LIB?=/opt/local/lib
|
||||
BOOST_INCLUDE?=$(HOME)/dev/boost_1_72_0
|
||||
BOOST_LIB?=$(HOME)/dev/boost_1_72_0/stage/lib
|
||||
|
||||
SIGNING_IDENTITY?=68AD28395D8090C2A8ACDD3A31FA6735C8DAE2F4
|
||||
|
||||
|
@ -12,7 +12,7 @@ CC?=cc
|
|||
CXX?=c++
|
||||
CFLAGS+=-std=c11
|
||||
CXXFLAGS+=-I$(BOOST_INCLUDE) -std=c++11
|
||||
LDFLAGS+=-L$(BOOST_LIB) -lboost_regex-mt
|
||||
LDFLAGS+=-L$(BOOST_LIB) -lboost_regex -lboost_filesystem
|
||||
|
||||
ifdef NDEBUG
|
||||
CFLAGS+=-O3 -DNDEBUG
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>pEp::Webserver running.</title>
|
||||
</head>
|
||||
<body>
|
||||
This is a test page.
|
||||
This is the test page of the p≡p Webserver. If you can see this then everything works.
|
||||
</body>
|
||||
</html>
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "webserver.hh"
|
||||
|
||||
int main()
|
||||
{
|
||||
pEp::Webserver web{net::ip::address::from_string("127.0.0.1"), 8080, "htdocs"};
|
||||
web.run();
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
16
webserver.cc
16
webserver.cc
|
@ -16,7 +16,7 @@ namespace pEp {
|
|||
Webserver::Webserver(net::ip::address addr, unsigned short port, std::string doc_root)
|
||||
: _ioc(1), _acceptor(_ioc, {addr, port}), _doc_root(doc_root), _running(false) { }
|
||||
|
||||
static beast::string_view mime_type(beast::string_view path)
|
||||
beast::string_view Webserver::mime_type(beast::string_view path)
|
||||
{
|
||||
using beast::iequals;
|
||||
auto const ext = [&path]
|
||||
|
@ -45,11 +45,6 @@ static beast::string_view mime_type(beast::string_view path)
|
|||
return "application/octet-stream";
|
||||
}
|
||||
|
||||
static void fail(beast::error_code ec, char const* what)
|
||||
{
|
||||
std::cerr << what << ": " << ec.message() << "\n";
|
||||
;}
|
||||
|
||||
void Webserver::add_url_handler(std::string url_regex, handler_t handler)
|
||||
{
|
||||
std::lock_guard< std::mutex > lock(_mtx);
|
||||
|
@ -81,9 +76,10 @@ void Webserver::deliver_file(tcp::socket *socket, Webserver::request req)
|
|||
static boost::regex file{"/([\\w\\d]{1,100}\\.[\\w\\d]{1,4})"};
|
||||
boost::cmatch m;
|
||||
// there's a strange bug without this string variable d
|
||||
// req.target().data() may end with '\x10' when directly being put into
|
||||
// regex_match()
|
||||
// req.target().data() may end with '\x10'
|
||||
std::string d = req.target().data();
|
||||
if (d.back() == '\x10')
|
||||
d.pop_back();
|
||||
if (boost::regex_match(d.c_str(), m, file)) {
|
||||
fs::path p{_doc_root};
|
||||
p /= m[1];
|
||||
|
@ -139,7 +135,7 @@ void Webserver::do_session(tcp::socket *socket)
|
|||
break;
|
||||
if (ec) {
|
||||
delete socket;
|
||||
return fail(ec, "reading from stream");
|
||||
throw std::ios_base::failure(ec.message());
|
||||
}
|
||||
|
||||
switch (req.method()) {
|
||||
|
@ -173,7 +169,7 @@ void Webserver::do_session(tcp::socket *socket)
|
|||
|
||||
if (ec) {
|
||||
delete socket;
|
||||
return fail(ec, "writing to stream");
|
||||
throw std::ios_base::failure(ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,8 @@ namespace pEp {
|
|||
void run();
|
||||
void shutdown();
|
||||
|
||||
static beast::string_view mime_type(beast::string_view path);
|
||||
|
||||
protected:
|
||||
void deliver_status(tcp::socket *socket, request req, http::status status);
|
||||
void deliver_file(tcp::socket *socket, request req);
|
||||
|
|
Loading…
Reference in New Issue