19#include <sys/socket.h>
20#include <netinet/in.h>
26#include <websocketpp/config/core.hpp>
27#include <websocketpp/server.hpp>
29#define IODEBUG(...) while (0) { printerr (__VA_ARGS__); break; }
33struct WebSocketServerImpl;
38 static const size_t connection_read_buffer_size = 16384;
41using WppServer = websocketpp::server<CustomServerConfig>;
42using WppConnectionP = WppServer::connection_ptr;
43using WppConnection = WppConnectionP::element_type;
44using WppHdl = websocketpp::connection_hdl;
47operator!= (
const WppHdl &a,
const WppHdl &b)
49 return a.owner_before (b) || b.owner_before (a);
51static inline bool operator== (
const WppHdl &a,
const WppHdl &b) {
return !(a != b); }
62 Ase::LoopID source_id = Ase::LoopID::INVALID;
70 String server_url_, dir_, token_, see_other_;
79 Ase::LoopID listen_source_id_ = Ase::LoopID::INVALID;
81 void update_client_sources();
82 bool io_handler (
PollFD &pfd);
89 int listen_port ()
const override {
return listen_port_; }
90 String url ()
const override {
return server_url_; }
91 WppConnectionP wppconp (WppHdl hdl)
93 websocketpp::lib::error_code ec;
94 return wppserver_.get_con_from_hdl (hdl, ec);
97 http_dir (
const String &path)
override
107 aseignore.
open (Path::join (dir_,
".aseignore"));
109 if (!cxxline.
empty())
113 http_alias (
const String &webdir,
const String &path)
override
119 std::stable_sort (aliases_.begin(), aliases_.end(), [] (
const auto &a,
const auto &b) {
120 return a.first.size() > b.first.size();
124 map_url (
const String &urlpath)
override
136 for (
const auto &ignorepat : ignores_)
141 for (
const auto &pair : aliases_)
142 if (absurl == pair.first)
144 else if (absurl.
size() > pair.first.size() &&
145 absurl[pair.first.size()] ==
'/' &&
146 strncmp (absurl.
c_str(), pair.first.c_str(), pair.first.size()) == 0)
147 return Path::join (pair.second, absurl.
c_str() + pair.first.size());
150 return Path::join (dir_, absurl);
156 listen_fd_ = sinfo.fd;
157 listen_port_ = sinfo.port;
158 if (::listen (listen_fd_, 128) < 0)
159 fatal_error (
"failed to listen on %s:%d, fd=%d: %s", sinfo.host, sinfo.port, listen_fd_,
strerror (
errno));
160 fcntl (listen_fd_, F_SETFL, O_NONBLOCK);
162 server_url_ = fullurl;
165 listen_source_id_ = main_loop->exec_io_handler ([
this] (
PollFD &pfd) {
168 sockaddr_in client_addr;
169 socklen_t client_len = sizeof (client_addr);
170 int client_fd = accept (listen_fd_, (sockaddr*) &client_addr, &client_len);
171 if (client_fd >= 0) {
172 fcntl (client_fd, F_SETFL, O_NONBLOCK);
173 sockaddr_in local_addr;
174 socklen_t local_len = sizeof (local_addr);
175 getsockname (client_fd, (sockaddr*) &local_addr, &local_len);
176 WppConnectionP con = wppserver_.get_connection();
180 char ip_str[INET_ADDRSTRLEN];
181 inet_ntop (AF_INET, &(local_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
182 cs.local_ip = ip_str;
183 cs.local_port = ntohs (local_addr.sin_port);
184 inet_ntop (AF_INET, &(client_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
185 cs.remote_ip = ip_str;
186 cs.remote_port = ntohs (client_addr.sin_port);
188 con->set_write_handler ([this, client_fd] (WppHdl hdl, char const *data, size_t len)
190 auto it = clients_.find (client_fd);
191 if (it != clients_.end()) {
192 it->second.out_buffer.append (data, len);
195 IODEBUG (
"%s:%u:%s: fd=%d write_handler: len=%d\n", __FILE__, __LINE__, __func__, it != clients_.end() ? it->first : -1, len);
196 return websocketpp::lib::error_code();
198 clients_[client_fd] = cs;
199 IODEBUG (
"%s:%u:%s: accept(%d)\n", __FILE__, __LINE__, __func__, client_fd);
202 update_client_sources();
209 see_other (
const String &uri)
override
219 main_loop->cancel (&listen_source_id_);
220 ::close (listen_fd_);
223 void ws_opened (WebSocketConnectionP);
224 void ws_closed (WebSocketConnectionP);
225 void reset ()
override;
226 WebSocketServerImpl (
const MakeConnection &make_con,
int logflags,
const std::string &token) :
227 token_ (token), make_con_ (make_con), logflags_ (logflags)
232WebSocketServerImpl::setup ()
234 wppserver_.set_user_agent (user_agent());
235 wppserver_.set_validate_handler ([
this] (WppHdl hdl)
237 WebSocketConnectionP conp = make_connection (hdl);
238 WppConnectionP cp = this->wppconp (hdl);
240 const bool is_authenticated = conp->authenticated (token_);
241 if (is_authenticated) {
242 const int index = conp->validate();
245 if (
size_t (index) < subprotocols.size())
247 cp->select_subprotocol (subprotocols[index]);
250 else if (subprotocols.size() == 0 && index == 0)
254 cp->set_status (websocketpp::http::status_code::forbidden);
255 conp->log_status (cp->get_response_code());
258 wppserver_.set_http_handler ([
this] (WppHdl hdl)
260 WebSocketConnectionP conp = make_connection (hdl);
262 conp->http_request();
264 wppserver_.clear_access_channels (websocketpp::log::alevel::all);
265 wppserver_.clear_error_channels (websocketpp::log::elevel::all);
269WebSocketServerImpl::io_handler (PollFD &pfd)
271 const int fd = pfd.fd;
272 IODEBUG (
"%s:%u:%s: io-ready(%d): r=%d w=%d other=0x%x\n", __FILE__, __LINE__, __func__, fd,
273 pfd.revents & PollFD::IN, pfd.revents & PollFD::OUT,
274 pfd.revents & ~(PollFD::IN | PollFD::OUT));
275 auto it = clients_.find (fd);
276 if (it == clients_.end())
278 auto &client = it->second;
279 if (client.out_buffer.size()) {
280 ssize_t n = ::send (fd, client.out_buffer.data(), client.out_buffer.size(), 0);
281 IODEBUG (
"%s:%u:%s: send(%d,%d)\n", __FILE__, __LINE__, __func__, fd, n);
283 client.out_buffer.erase (0, n);
284 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
285 client.con->fatal_error();
287 if ((pfd.revents & PollFD::IN) &&
288 client.con->get_state() != websocketpp::session::state::closed) {
290 ssize_t n = ::recv (fd, buf,
sizeof (buf), 0);
291 IODEBUG (
"%s:%u:%s: recv(%d)=%d\n", __FILE__, __LINE__, __func__, fd, n);
294 client.con->read_all (buf, n);
296 client.con->fatal_error();
301 else if (n < 0 && errno != EAGAIN && errno != EWOULDBLOCK) {
302 client.con->fatal_error();
305 if (pfd.revents & (PollFD::ERR | PollFD::HUP | PollFD::NVAL))
306 client.con->fatal_error();
307 if (client.con->get_state() == websocketpp::session::state::closed)
308 IODEBUG (
"%s:%u:%s: closed fd=%d ob=%d io_errors=0x%x\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size(), pfd.revents & (PollFD::ERR | PollFD::HUP | PollFD::NVAL));
309 if (client.out_buffer.size())
310 pfd.events |= PollFD::OUT;
312 pfd.events &= ~PollFD::OUT;
313 const bool closed = client.out_buffer.empty() && client.con->get_state() == websocketpp::session::state::closed;
319WebSocketServerImpl::update_client_sources()
322 for (
auto &[fd, client] : clients_)
323 if (client.source_id ==
Ase::LoopID::INVALID)
324 client.source_id = main_loop->exec_io_handler ([this] (PollFD &pfd)
326 return this->io_handler (pfd);
327 }, fd,
"r", LoopPriority::NORMAL);
333 WppServer &wppserver;
337 WppConnectionP wppconp() {
return server->wppconp (hdl); }
342WebSocketConnection::log_status (
int intstatus,
const String &filepath)
344 const auto status = websocketpp::http::status_code::value (intstatus);
345 WppConnectionP cp = internals_.wppconp();
346 if (!cp || !(logflags_ & 16))
348 using namespace AnsiColors;
349 const bool rejected = (status >= 400 && status <= 499) || status == 303;
350 const String C1 = rejected ? color (FG_RED) :
"";
351 const String C0 = rejected ? color (RESET) :
"";
352 String resource = cp->get_resource();
353 resource = Re::sub (
"\\btoken=[^;?]+",
"token=<………>", resource);
354 log (string_format (
"%s%d %s %s%s%s",
356 cp->get_request().get_method(), resource, C0,
357 filepath.
empty() ?
"" :
" [" + filepath +
"]"));
360WebSocketConnection::WebSocketConnection (Internals &internals,
int logflags) :
361 internals_ (*new (internals_mem_) Internals (internals)), logflags_ (logflags)
363 static_assert (
sizeof (Internals) <=
sizeof (internals_mem_));
367WebSocketConnection::~WebSocketConnection()
369 internals_.~Internals();
373WebSocketConnection::is_open ()
const
375 return internals_.opened;
382 WppConnectionP cp = internals_.wppconp();
384 websocketpp::lib::error_code ec;
387 for (
const auto &[f, client] : internals_.server->clients_)
388 if (client.con == cp)
390 IODEBUG (
"%s:%u:%s: fd=%d message=%d\n", __FILE__, __LINE__, __func__, fd,
message.size());
391 internals_.wppserver.send (internals_.hdl,
message, websocketpp::frame::opcode::text, ec);
395 log (
string_format (
"Error: %s: %s", __func__, ec.message()));
396 websocketpp::lib::error_code ec2;
397 cp->close (websocketpp::close::status::going_away,
"", ec2);
406 WppConnectionP cp = internals_.wppconp();
408 websocketpp::lib::error_code ec;
409 internals_.wppserver.send (internals_.hdl, blob, websocketpp::frame::opcode::binary, ec);
413 log (
string_format (
"Error: %s: %s", __func__, ec.message()));
414 websocketpp::lib::error_code ec2;
415 cp->close (websocketpp::close::status::going_away,
"", ec2);
421 for (
size_t i = 0; i < blob.
size(); i++)
423 if (i && 0 == i % 16)
435WebSocketConnection::get_info ()
437 WppConnectionP cp = internals_.wppconp();
438 const websocketpp::http::parser::request &rq = cp->get_request();
441 info.header = [headers] (
const String &header) {
442 const auto it = headers->find (header);
443 return it == headers->end() ?
"" : it->second;
446 for (
auto it : *headers)
447 IODEBUG (
"%s: %s\n", it.first, it.second);
450 for (
const auto &[fd, client] : internals_.server->clients_)
451 if (client.con == cp) {
452 info.local = client.local_ip;
453 info.lport = client.local_port;
454 info.remote = client.remote_ip;
455 info.rport = client.remote_port;
458 info.subs = cp->get_requested_subprotocols();
463WebSocketConnection::nickname ()
465 if (internals_.nickname_.
empty())
467 Info info = get_info();
468 const String ua = info.header (
"User-Agent");
472 info.header (
"Accept-Encoding") +
"\n" +
473 info.header (
"Accept-Language") +
"\n" +
474 info.header (
"sec-ch-ua") +
"\n" +
475 info.header (
"sec-ch-ua-mobile") +
"\n" +
476 info.header (
"sec-gpc") +
"\n";
481 else if (
Re::search (R
"(\bElectron/)", ua) >= 0)
483 else if (
Re::search (R
"(\bChrome-Lighthouse\b)", ua) >= 0)
485 else if (
Re::search (R
"(\bChrome/)", ua) >= 0)
487 else if (
Re::search (R
"(\bSafari/)", ua) >= 0)
495 else if (
Re::search (R
"(\bPython-urllib/)", ua) >= 0)
499 internals_.nickname_ =
string_format (
"%s-%08x:%x", hh, uint32_t (hash ^ (hash >> 32)), info.rport);
501 return internals_.nickname_;
514 WppConnectionP cp = internals_.wppconp();
519 const String cookie_header = cp->get_request().get_header (
"Cookie");
520 const String cookie_token =
Re::grep (
"\\bsession_auth=([^;?]+)", cookie_header, 1);
521 return cookie_token == token;
534 if (internals_.server->dir_.empty())
536 WppConnectionP cp = internals_.wppconp();
537 const auto &parts =
string_split (cp->get_resource(),
"?");
538 const String query = cp->get_uri()->get_query();
542 auto set_response = [] (WppConnectionP cp, websocketpp::http::status_code::value status,
const String &title,
const String &msg)
546 "<html><head><title>%u %s</title></head>\n<body>\n"
549 "<hr><address>%s</address>\n"
550 "<hr></body></html>\n", status, title, title,
551 msg.size() ?
"<p>" + msg +
"</p>" :
"",
552 WebSocketServer::user_agent());
554 cp->replace_header (
"Content-Type",
"text/html; charset=utf-8");
555 cp->set_status (status);
559 if (cp->get_request().get_method() ==
"GET" && parts[0] ==
"/~auth" &&
561 Re::search (
"\\btoken=" + internals_.server->token_ +
"\\b", query) >= 0)) {
563 cp->replace_header (
"Set-Cookie",
"session_auth=" + internals_.server->token_ +
"; Path=/; HttpOnly");
564 cp->replace_header (
"Location", target);
565 set_response (cp, websocketpp::http::status_code::found,
566 "Found",
"Redirecting to: <tt>" + target +
"</tt>");
567 log_status (cp->get_response_code());
573 const std::string see_other = internals_.server->see_other_;
574 if (see_other.
empty())
575 set_response (cp, websocketpp::http::status_code::method_not_allowed,
576 "Method Not Allowed",
"Authentication required.");
579 cp->replace_header (
"Location", see_other);
580 set_response (cp, websocketpp::http::status_code::see_other,
581 "See Other",
"<a href=\"" + see_other +
"\">" + see_other +
"</a>");
583 log_status (cp->get_response_code());
588 filepath = internals_.server->map_url (parts[0]);
592 filepath = Path::join (filepath,
"index.html");
595 if (cp->get_request().get_method() ==
"GET") {
597 const String accept_encoding = cp->get_request().get_header (
"Accept-Encoding");
599 bool fp =
false, fz =
false;
603 const String mimetype = WebSocketServer::mime_type (ext ? ext + 1 :
"",
true);
604 cp->append_header (
"Content-Type", mimetype);
607 const CacheType caching = mimetype ==
"text/css" ? CACHE_FOREVER : CACHE_AUTO;
610 cp->append_header (
"Cache-Control",
"no-store");
613 cp->append_header (
"Cache-Control",
"no-cache");
616 cp->append_header (
"Cache-Control",
"max-age=7, stale-while-revalidate=31536000");
619 cp->append_header (
"Cache-Control",
"max-age=31536000, public, immutable");
623 filepath = filepath +
".gz";
624 cp->append_header (
"Content-Encoding",
"gzip");
627 cp->set_body (blob.
string());
628 cp->set_status (websocketpp::http::status_code::ok);
631 set_response (cp, websocketpp::http::status_code::not_found,
633 "The requested URL was not found: <br /> <tt>" + cp->get_uri()->str() +
"</tt>");
636 set_response (cp, websocketpp::http::status_code::method_not_allowed,
637 "Method Not Allowed",
"");
639 log_status (cp->get_response_code());
644WebSocketServerImpl::make_connection (WppHdl hdl)
646 WppConnectionP cp = wppconp (hdl);
652 WebSocketConnectionP conp = make_con_ (internals, logflags_);
654 cp->set_http_handler ([conp] (WppHdl hdl) {
655 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
657 conp->http_request();
660 cp->set_fail_handler ([conp] (WppHdl hdl) {
661 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
665 cp->set_open_handler ([conp] (WppHdl hdl) {
666 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
668 internals_.server->ws_opened (conp);
670 cp->set_message_handler ([conp] (WppHdl hdl, WppServer::message_ptr msg) {
671 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
673 conp->message (msg->get_payload());
675 cp->set_close_handler ([conp] (WppHdl hdl) {
676 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
678 internals_.server->ws_closed (conp);
684WebSocketServerImpl::ws_opened (WebSocketConnectionP conp)
686 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
687 internals_.opened =
true;
688 opencons_.push_back (conp);
690 update_client_sources();
695WebSocketServerImpl::ws_closed (WebSocketConnectionP conp)
697 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
698 WppConnectionP wppconp = internals_.wppconp();
699 if (internals_.opened) {
700 internals_.opened =
false;
701 auto it =
std::find (opencons_.begin(), opencons_.end(), conp);
702 if (it != opencons_.end())
703 opencons_.erase (it);
705 for (
auto client_it = clients_.begin(); client_it != clients_.end(); ) {
706 if (client_it->second.con == wppconp) {
707 auto &[fd, client] = *client_it;
709 main_loop->cancel (&client.source_id);
710 IODEBUG (
"%s:%u:%s: closed fd=%d ob=%d\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size());
711 client_it = clients_.erase (client_it);
720WebSocketServerImpl::reset()
723 for (ssize_t i = ssize_t (opencons_.size()) - 1; i >= 0; i--)
725 WebSocketConnectionP conp = opencons_[i];
726 WebSocketConnection::Internals &internals_ = WebSocketServer::internals (*conp);
727 WppConnectionP cp = internals_.wppconp();
728 websocketpp::lib::error_code ec;
729 cp->close (websocketpp::close::status::going_away,
"", ec);
733 for (
auto &[fd, client] : clients_) {
735 main_loop->cancel (&client.source_id);
736 IODEBUG (
"%s:%u:%s: closed fd=%d ob=%d\n", __FILE__, __LINE__, __func__, fd, client.out_buffer.size());
742WebSocketServer::~WebSocketServer()
745WebSocketServer::SocketInfo
746WebSocketServer::bind_port (
const String &host,
int port)
748 SocketInfo sinfo { -1, host, port };
749 sinfo.fd =
socket (AF_INET, SOCK_STREAM, 0);
751 fatal_error (
"failed to create socket for: %s:%d: %s", host, port, strerror (errno));
755 setsockopt (sinfo.fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt));
756 addr.sin_port =
htons (port);
758 addr.sin_family = AF_INET;
759 if (host ==
"0.0.0.0")
760 addr.sin_addr.s_addr =
htonl (INADDR_ANY);
763 addr.sin_addr.s_addr =
inet_addr (host.c_str());
764 if (addr.sin_addr.s_addr == INADDR_NONE)
765 addr.sin_addr.s_addr =
htonl (INADDR_LOOPBACK);
767 if (bind (sinfo.fd, (sockaddr*) &addr, sizeof (addr)) < 0)
768 fatal_error (
"failed to bind to %s:%d, fd=%d: %s", host, port, sinfo.fd,
strerror (errno));
769 socklen_t len =
sizeof (addr);
770 if (getsockname (sinfo.fd, (sockaddr*) &addr, &len) == 0)
771 sinfo.port =
ntohs (addr.sin_port);
776WebSocketServer::create (
const MakeConnection &make,
int logflags,
const std::string &session_token)
782WebSocketServer::user_agent ()
791 return !!websocketpp::utf8_validator::validate (utf8string);
797WebSocketServer::mime_type (
const String &ext,
bool utf8)
800 static MimeMap mime_map = [] ()
807 for (
size_t i = 1; i < w.
size(); i++)
810 if (mime_map.end() != mime_map.find (w[i]))
811 warning (
"mime-types.hh: duplicate extension: %s", w[i]);
812 mime_map[w[i]] = w[0];
817 auto it = mime_map.find (ext);
818 String mimetype = it != mime_map.
end() ? it->second :
"application/octet-stream";
821 if (mimetype ==
"text/html" || mimetype ==
"text/markdown" || mimetype ==
"text/plain")
822 mimetype +=
"; charset=utf-8";
Binary large object storage container.
static Blob from_file(const String &filename)
Create Blob by loading from filename.
String string()
Copy Blob data into a zero terminated string.
static String grep(const String ®ex, const String &input, int group=0, Flags=DEFAULT)
Find regex in input and return matching string.
static ssize_t search(const String ®ex, const String &input, Flags=DEFAULT)
Find regex in input and return match position >= 0 or return < 0 otherwise.
virtual void closed()
Pairs with opened().
virtual int validate()
Return true to allow opened().
virtual void http_request()
Independent of socket ops.
virtual void opened()
Pairs with closed().
bool send_binary(const String &blob)
Returns true if binary blob was sent.
virtual void message(const String &message)
Only if opened.
virtual void failed()
Never folloed by opened().
bool send_text(const String &message)
Returns true if text message was sent.
virtual bool authenticated(const std::string &token)
Return true to allow validate().
static bool utf8_validate(const std::string &utf8string)
Validate UTF-8 string with websocketpp::utf8_validator.
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
#define assert_return(expr,...)
Return from the current function if expr is unmet and issue an assertion warning.
#define return_unless(cond,...)
Return silently if cond does not evaluate to true with return value ...
bool check(const String &file, const String &mode)
String simplify_abspath(const std::string &abspath_expression)
Remove extra slashes, './' and '../' from abspath_expression.
String abspath(const String &path, const String &incwd)
String normalize(const String &path)
Convert path to normal form.
The Anklang C++ API namespace.
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
StringS string_split(const String &string, const String &splitter, size_t maxn)
Split a string, using splitter as delimiter.
String string_from_int(int64 value)
Convert a 64bit signed integer into a string.
const char * string_find_word(const char *haystack, const char *word)
Find occurance of word in haystack.
@ NORMAL
Normal importantance, GUI event processing, RPC.
String string_url_decode(const String &urlstr, const bool form_url_encoded)
Decode URL %-sequences in a string, decode '+' if form_url_encoded.
const char * ase_version()
Provide a string containing the package version.
std::string String
Convenience alias for std::string.
bool string_startswith(const String &string, const String &fragment)
Returns whether string starts with fragment.
size_t hash(size_t seed, const T &v)
T regex_search(T... args)
T shared_from_this(T... args)
Mirrors struct pollfd for poll(3posix)