Commit 6278d88f authored by octal's avatar octal

Renamed library to pistache

parent 7b2b963d
...@@ -13,13 +13,8 @@ else() ...@@ -13,13 +13,8 @@ else()
endif() endif()
include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include) include_directories (${CMAKE_CURRENT_SOURCE_DIR}/include)
file(GLOB HEADER_FILES "include/*.h")
install(FILES ${HEADER_FILES} DESTINATION include/pistache)
file(GLOB SERIALIZER_HEADER_FILES "include/serializer/*.h")
install(FILES ${SERIALIZER_HEADER_FILES} DESTINATION include/pistache/serializer)
add_subdirectory (src) add_subdirectory (src)
include_directories (src) include_directories (src)
add_subdirectory (examples) add_subdirectory (examples)
......
add_executable(http_server http_server.cc) function(pistache_example example_name)
target_link_libraries(http_server net_static) set(EXAMPLE_EXECUTABLE run_${example_name})
set(EXAMPLE_SOURCE ${example_name}.cc)
add_executable(hello_server hello_server.cc) add_executable(${EXAMPLE_EXECUTABLE} ${EXAMPLE_SOURCE})
target_link_libraries(hello_server net_static) target_link_libraries(${EXAMPLE_EXECUTABLE} pistache)
endfunction()
add_executable(http_client http_client.cc) pistache_example(http_server)
target_link_libraries(http_client net_static) pistache_example(http_client)
pistache_example(rest_server)
add_executable(custom_header custom_header.cc) pistache_example(custom_header)
target_link_libraries(custom_header net_static)
add_executable(rest_server rest_server.cc)
target_link_libraries(rest_server net_static)
find_package(RapidJSON) find_package(RapidJSON)
if (RapidJSON_FOUND) if (RapidJSON_FOUND)
include_directories(${RapidJSON_INCLUDE_DIRS}) include_directories(${RapidJSON_INCLUDE_DIRS})
add_executable(rest_description rest_description.cc) pistache_example(rest_description)
target_link_libraries(rest_description net_static)
endif() endif()
...@@ -4,12 +4,11 @@ ...@@ -4,12 +4,11 @@
Example of custom headers registering Example of custom headers registering
*/ */
#include "net.h" #include <pistache/net.h>
#include "http_headers.h" #include <pistache/http_headers.h>
#include "client.h"
using namespace Net; using namespace Pistache;
using namespace Net::Http; using namespace Pistache::Http;
class XProtocolVersion : public Header::Header { class XProtocolVersion : public Header::Header {
public: public:
......
...@@ -4,11 +4,14 @@ ...@@ -4,11 +4,14 @@
* Http client example * Http client example
*/ */
#include "net.h" #include <atomic>
#include "http.h"
#include "client.h"
using namespace Net; #include <pistache/net.h>
#include <pistache/http.h>
#include <pistache/client.h>
using namespace Pistache;
using namespace Pistache::Http;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc < 2) { if (argc < 2) {
......
...@@ -4,15 +4,15 @@ ...@@ -4,15 +4,15 @@
Example of an http server Example of an http server
*/ */
#include "net.h" #include <pistache/net.h>
#include "http.h" #include <pistache/http.h>
#include "peer.h" #include <pistache/peer.h>
#include "http_headers.h" #include <pistache/http_headers.h>
#include "cookie.h" #include <pistache/cookie.h>
#include "endpoint.h" #include <pistache/endpoint.h>
using namespace std; using namespace std;
using namespace Net; using namespace Pistache;
struct PrintException { struct PrintException {
void operator()(std::exception_ptr exc) const { void operator()(std::exception_ptr exc) const {
...@@ -25,7 +25,7 @@ struct PrintException { ...@@ -25,7 +25,7 @@ struct PrintException {
}; };
struct LoadMonitor { struct LoadMonitor {
LoadMonitor(const std::shared_ptr<Net::Http::Endpoint>& endpoint) LoadMonitor(const std::shared_ptr<Http::Endpoint>& endpoint)
: endpoint_(endpoint) : endpoint_(endpoint)
, interval(std::chrono::seconds(1)) , interval(std::chrono::seconds(1))
{ } { }
...@@ -49,18 +49,18 @@ struct LoadMonitor { ...@@ -49,18 +49,18 @@ struct LoadMonitor {
} }
private: private:
std::shared_ptr<Net::Http::Endpoint> endpoint_; std::shared_ptr<Http::Endpoint> endpoint_;
std::unique_ptr<std::thread> thread; std::unique_ptr<std::thread> thread;
std::chrono::seconds interval; std::chrono::seconds interval;
std::atomic<bool> shutdown_; std::atomic<bool> shutdown_;
void run() { void run() {
Net::Tcp::Listener::Load old; Tcp::Listener::Load old;
while (!shutdown_) { while (!shutdown_) {
if (!endpoint_->isBound()) continue; if (!endpoint_->isBound()) continue;
endpoint_->requestLoad(old).then([&](const Net::Tcp::Listener::Load& load) { endpoint_->requestLoad(old).then([&](const Tcp::Listener::Load& load) {
old = load; old = load;
double global = load.global; double global = load.global;
...@@ -79,18 +79,18 @@ private: ...@@ -79,18 +79,18 @@ private:
}; };
class MyHandler : public Net::Http::Handler { class MyHandler : public Http::Handler {
HTTP_PROTOTYPE(MyHandler) HTTP_PROTOTYPE(MyHandler)
void onRequest( void onRequest(
const Net::Http::Request& req, const Http::Request& req,
Net::Http::ResponseWriter response) { Http::ResponseWriter response) {
if (req.resource() == "/ping") { if (req.resource() == "/ping") {
if (req.method() == Net::Http::Method::Get) { if (req.method() == Http::Method::Get) {
using namespace Net::Http; using namespace Http;
auto query = req.query(); auto query = req.query();
if (query.has("chunked")) { if (query.has("chunked")) {
...@@ -103,22 +103,22 @@ class MyHandler : public Net::Http::Handler { ...@@ -103,22 +103,22 @@ class MyHandler : public Net::Http::Handler {
response.cookies() response.cookies()
.add(Cookie("lang", "en-US")); .add(Cookie("lang", "en-US"));
auto stream = response.stream(Net::Http::Code::Ok); auto stream = response.stream(Http::Code::Ok);
stream << "PO"; stream << "PO";
stream << "NG"; stream << "NG";
stream << ends; stream << ends;
} }
else { else {
response.send(Net::Http::Code::Ok, "PONG"); response.send(Http::Code::Ok, "PONG");
} }
} }
} }
else if (req.resource() == "/echo") { else if (req.resource() == "/echo") {
if (req.method() == Net::Http::Method::Post) { if (req.method() == Http::Method::Post) {
response.send(Net::Http::Code::Ok, req.body(), MIME(Text, Plain)); response.send(Http::Code::Ok, req.body(), MIME(Text, Plain));
} else { } else {
response.send(Net::Http::Code::Method_Not_Allowed); response.send(Http::Code::Method_Not_Allowed);
} }
} }
else if (req.resource() == "/exception") { else if (req.resource() == "/exception") {
...@@ -128,8 +128,8 @@ class MyHandler : public Net::Http::Handler { ...@@ -128,8 +128,8 @@ class MyHandler : public Net::Http::Handler {
response.timeoutAfter(std::chrono::seconds(2)); response.timeoutAfter(std::chrono::seconds(2));
} }
else if (req.resource() == "/static") { else if (req.resource() == "/static") {
if (req.method() == Net::Http::Method::Get) { if (req.method() == Http::Method::Get) {
Net::Http::serveFile(response, "README.md").then([](ssize_t bytes) {; Http::serveFile(response, "README.md").then([](ssize_t bytes) {;
std::cout << "Sent " << bytes << " bytes" << std::endl; std::cout << "Sent " << bytes << " bytes" << std::endl;
}, Async::NoExcept); }, Async::NoExcept);
} }
...@@ -139,16 +139,16 @@ class MyHandler : public Net::Http::Handler { ...@@ -139,16 +139,16 @@ class MyHandler : public Net::Http::Handler {
} }
void onTimeout(const Net::Http::Request& req, Net::Http::ResponseWriter response) { void onTimeout(const Http::Request& req, Http::ResponseWriter response) {
response response
.send(Net::Http::Code::Request_Timeout, "Timeout") .send(Http::Code::Request_Timeout, "Timeout")
.then([=](ssize_t) { }, PrintException()); .then([=](ssize_t) { }, PrintException());
} }
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Net::Port port(9080); Port port(9080);
int thr = 2; int thr = 2;
...@@ -159,17 +159,17 @@ int main(int argc, char *argv[]) { ...@@ -159,17 +159,17 @@ int main(int argc, char *argv[]) {
thr = std::stol(argv[2]); thr = std::stol(argv[2]);
} }
Net::Address addr(Net::Ipv4::any(), port); Address addr(Ipv4::any(), port);
static constexpr size_t Workers = 4; static constexpr size_t Workers = 4;
cout << "Cores = " << hardware_concurrency() << endl; cout << "Cores = " << hardware_concurrency() << endl;
cout << "Using " << thr << " threads" << endl; cout << "Using " << thr << " threads" << endl;
auto server = std::make_shared<Net::Http::Endpoint>(addr); auto server = std::make_shared<Http::Endpoint>(addr);
auto opts = Net::Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(thr) .threads(thr)
.flags(Net::Tcp::Options::InstallSignalHandler); .flags(Tcp::Options::InstallSignalHandler);
server->init(opts); server->init(opts);
server->setHandler(Http::make_handler<MyHandler>()); server->setHandler(Http::make_handler<MyHandler>());
server->serve(); server->serve();
......
...@@ -4,13 +4,14 @@ ...@@ -4,13 +4,14 @@
Example of how to use the Description mechanism Example of how to use the Description mechanism
*/ */
#include "http.h" #include <pistache/http.h>
#include "description.h" #include <pistache/description.h>
#include "endpoint.h" #include <pistache/endpoint.h>
#include "serializer/rapidjson.h"
#include <pistache/serializer/rapidjson.h>
using namespace std; using namespace std;
using namespace Net; using namespace Pistache;
namespace Generic { namespace Generic {
...@@ -22,15 +23,15 @@ void handleReady(const Rest::Request&, Http::ResponseWriter response) { ...@@ -22,15 +23,15 @@ void handleReady(const Rest::Request&, Http::ResponseWriter response) {
class BankerService { class BankerService {
public: public:
BankerService(Net::Address addr) BankerService(Address addr)
: httpEndpoint(std::make_shared<Net::Http::Endpoint>(addr)) : httpEndpoint(std::make_shared<Http::Endpoint>(addr))
, desc("Banking API", "0.1") , desc("Banking API", "0.1")
{ } { }
void init(size_t thr = 2) { void init(size_t thr = 2) {
auto opts = Net::Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(thr) .threads(thr)
.flags(Net::Tcp::Options::InstallSignalHandler); .flags(Tcp::Options::InstallSignalHandler);
httpEndpoint->init(opts); httpEndpoint->init(opts);
createDescription(); createDescription();
} }
...@@ -130,13 +131,13 @@ private: ...@@ -130,13 +131,13 @@ private:
response.send(Http::Code::Ok, "The bank is closed, come back later"); response.send(Http::Code::Ok, "The bank is closed, come back later");
} }
std::shared_ptr<Net::Http::Endpoint> httpEndpoint; std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Description desc; Rest::Description desc;
Rest::Router router; Rest::Router router;
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Net::Port port(9080); Port port(9080);
int thr = 2; int thr = 2;
...@@ -147,7 +148,7 @@ int main(int argc, char *argv[]) { ...@@ -147,7 +148,7 @@ int main(int argc, char *argv[]) {
thr = std::stol(argv[2]); thr = std::stol(argv[2]);
} }
Net::Address addr(Net::Ipv4::any(), port); Address addr(Ipv4::any(), port);
cout << "Cores = " << hardware_concurrency() << endl; cout << "Cores = " << hardware_concurrency() << endl;
cout << "Using " << thr << " threads" << endl; cout << "Using " << thr << " threads" << endl;
......
...@@ -4,15 +4,16 @@ ...@@ -4,15 +4,16 @@
Example of a REST endpoint with routing Example of a REST endpoint with routing
*/ */
#include "http.h"
#include "router.h"
#include "endpoint.h"
#include <algorithm> #include <algorithm>
#include <pistache/http.h>
#include <pistache/router.h>
#include <pistache/endpoint.h>
using namespace std; using namespace std;
using namespace Net; using namespace Pistache;
void printCookies(const Net::Http::Request& req) { void printCookies(const Http::Request& req) {
auto cookies = req.cookies(); auto cookies = req.cookies();
std::cout << "Cookies: [" << std::endl; std::cout << "Cookies: [" << std::endl;
const std::string indent(4, ' '); const std::string indent(4, ' ');
...@@ -32,14 +33,14 @@ void handleReady(const Rest::Request&, Http::ResponseWriter response) { ...@@ -32,14 +33,14 @@ void handleReady(const Rest::Request&, Http::ResponseWriter response) {
class StatsEndpoint { class StatsEndpoint {
public: public:
StatsEndpoint(Net::Address addr) StatsEndpoint(Address addr)
: httpEndpoint(std::make_shared<Net::Http::Endpoint>(addr)) : httpEndpoint(std::make_shared<Http::Endpoint>(addr))
{ } { }
void init(size_t thr = 2) { void init(size_t thr = 2) {
auto opts = Net::Http::Endpoint::options() auto opts = Http::Endpoint::options()
.threads(thr) .threads(thr)
.flags(Net::Tcp::Options::InstallSignalHandler); .flags(Tcp::Options::InstallSignalHandler);
httpEndpoint->init(opts); httpEndpoint->init(opts);
setupRoutes(); setupRoutes();
} }
...@@ -55,7 +56,7 @@ public: ...@@ -55,7 +56,7 @@ public:
private: private:
void setupRoutes() { void setupRoutes() {
using namespace Net::Rest; using namespace Rest;
Routes::Post(router, "/record/:name/:value?", Routes::bind(&StatsEndpoint::doRecordMetric, this)); Routes::Post(router, "/record/:name/:value?", Routes::bind(&StatsEndpoint::doRecordMetric, this));
Routes::Get(router, "/value/:name", Routes::bind(&StatsEndpoint::doGetMetric, this)); Routes::Get(router, "/value/:name", Routes::bind(&StatsEndpoint::doGetMetric, this));
...@@ -64,7 +65,7 @@ private: ...@@ -64,7 +65,7 @@ private:
} }
void doRecordMetric(const Rest::Request& request, Net::Http::ResponseWriter response) { void doRecordMetric(const Rest::Request& request, Http::ResponseWriter response) {
auto name = request.param(":name").as<std::string>(); auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock); Guard guard(metricsLock);
...@@ -90,7 +91,7 @@ private: ...@@ -90,7 +91,7 @@ private:
} }
void doGetMetric(const Rest::Request& request, Net::Http::ResponseWriter response) { void doGetMetric(const Rest::Request& request, Http::ResponseWriter response) {
auto name = request.param(":name").as<std::string>(); auto name = request.param(":name").as<std::string>();
Guard guard(metricsLock); Guard guard(metricsLock);
...@@ -107,7 +108,7 @@ private: ...@@ -107,7 +108,7 @@ private:
} }
void doAuth(const Rest::Request& request, Net::Http::ResponseWriter response) { void doAuth(const Rest::Request& request, Http::ResponseWriter response) {
printCookies(request); printCookies(request);
response.cookies() response.cookies()
.add(Http::Cookie("lang", "en-US")); .add(Http::Cookie("lang", "en-US"));
...@@ -144,12 +145,12 @@ private: ...@@ -144,12 +145,12 @@ private:
Lock metricsLock; Lock metricsLock;
std::vector<Metric> metrics; std::vector<Metric> metrics;
std::shared_ptr<Net::Http::Endpoint> httpEndpoint; std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Router router; Rest::Router router;
}; };
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Net::Port port(9080); Port port(9080);
int thr = 2; int thr = 2;
...@@ -160,7 +161,7 @@ int main(int argc, char *argv[]) { ...@@ -160,7 +161,7 @@ int main(int argc, char *argv[]) {
thr = std::stol(argv[2]); thr = std::stol(argv[2]);
} }
Net::Address addr(Net::Ipv4::any(), port); Address addr(Ipv4::any(), port);
cout << "Cores = " << hardware_concurrency() << endl; cout << "Cores = " << hardware_concurrency() << endl;
cout << "Using " << thr << " threads" << endl; cout << "Using " << thr << " threads" << endl;
......
...@@ -14,10 +14,11 @@ ...@@ -14,10 +14,11 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <condition_variable> #include <condition_variable>
#include <condition_variable>
#include "optional.h"
#include "typeid.h"
#include <pistache/optional.h>
#include <pistache/typeid.h>
namespace Pistache {
namespace Async { namespace Async {
class Error : public std::runtime_error { class Error : public std::runtime_error {
...@@ -1537,3 +1538,4 @@ namespace Async { ...@@ -1537,3 +1538,4 @@ namespace Async {
} }
} // namespace Async } // namespace Async
} // namespace Pistache
...@@ -5,19 +5,20 @@ ...@@ -5,19 +5,20 @@
*/ */
#pragma once #pragma once
#include "async.h"
#include "os.h"
#include "http.h"
#include "reactor.h"
#include "timer_pool.h"
#include "view.h"
#include <atomic> #include <atomic>
#include <deque>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <deque>
namespace Net { #include <pistache/async.h>
#include <pistache/os.h>
#include <pistache/http.h>
#include <pistache/timer_pool.h>
#include <pistache/reactor.h>
#include <pistache/view.h>
namespace Pistache {
namespace Http { namespace Http {
class ConnectionPool; class ConnectionPool;
...@@ -72,7 +73,7 @@ struct Connection : public std::enable_shared_from_this<Connection> { ...@@ -72,7 +73,7 @@ struct Connection : public std::enable_shared_from_this<Connection> {
Connected Connected
}; };
void connect(Net::Address addr); void connect(Address addr);
void close(); void close();
bool isIdle() const; bool isIdle() const;
bool isConnected() const; bool isConnected() const;
...@@ -131,7 +132,7 @@ private: ...@@ -131,7 +132,7 @@ private:
std::deque<RequestEntry> inflightRequests; std::deque<RequestEntry> inflightRequests;
Net::TimerPool timerPool_; TimerPool timerPool_;
Private::Parser<Http::Response> parser_; Private::Parser<Http::Response> parser_;
}; };
...@@ -354,6 +355,4 @@ private: ...@@ -354,6 +355,4 @@ private:
}; };
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -11,9 +11,10 @@ ...@@ -11,9 +11,10 @@
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
#include <netdb.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netdb.h>
#define unsafe #define unsafe
...@@ -48,10 +49,12 @@ ...@@ -48,10 +49,12 @@
#define unreachable() __builtin_unreachable() #define unreachable() __builtin_unreachable()
namespace Pistache {
namespace Const { namespace Const {
static constexpr int MaxBacklog = 128; static constexpr int MaxBacklog = 128;
static constexpr int MaxEvents = 1024; static constexpr int MaxEvents = 1024;
static constexpr int MaxBuffer = 4096; static constexpr int MaxBuffer = 4096;
static constexpr int ChunkSize = 1024; static constexpr int ChunkSize = 1024;
} } // namespace Const
} // namespace Pistache
...@@ -6,15 +6,15 @@ ...@@ -6,15 +6,15 @@
#pragma once #pragma once
#include "optional.h"
#include "http_defs.h"
#include <ctime> #include <ctime>
#include <string> #include <string>
#include <map> #include <map>
#include <unordered_map> #include <unordered_map>
namespace Net { #include <pistache/optional.h>
#include <pistache/http_defs.h>
namespace Pistache {
namespace Http { namespace Http {
struct Cookie { struct Cookie {
...@@ -95,5 +95,4 @@ private: ...@@ -95,5 +95,4 @@ private:
}; };
} // namespace Net } // namespace Net
} // namespace Pistache
} // namespace Http
...@@ -12,16 +12,15 @@ ...@@ -12,16 +12,15 @@
#include <type_traits> #include <type_traits>
#include <memory> #include <memory>
#include <algorithm> #include <algorithm>
#include "http_defs.h"
#include "mime.h"
#include "optional.h"
#include "router.h"
#include "iterator_adapter.h"
namespace Net { #include <pistache/http_defs.h>
#include <pistache/mime.h>
#include <pistache/optional.h>
#include <pistache/router.h>
#include <pistache/iterator_adapter.h>
namespace Pistache {
namespace Rest { namespace Rest {
namespace Type { namespace Type {
// Data Types // Data Types
...@@ -453,5 +452,4 @@ private: ...@@ -453,5 +452,4 @@ private:
}; };
} // namespace Rest } // namespace Rest
} // namespace Pistache
} // namespace Net
...@@ -6,12 +6,11 @@ ...@@ -6,12 +6,11 @@
#pragma once #pragma once
#include "listener.h" #include <pistache/listener.h>
#include "net.h" #include <pistache/net.h>
#include "http.h" #include <pistache/http.h>
namespace Net {
namespace Pistache {
namespace Http { namespace Http {
class Endpoint { class Endpoint {
...@@ -30,7 +29,7 @@ public: ...@@ -30,7 +29,7 @@ public:
Options(); Options();
}; };
Endpoint(); Endpoint();
Endpoint(const Net::Address& addr); Endpoint(const Address& addr);
template<typename... Args> template<typename... Args>
void initArgs(Args&& ...args) { void initArgs(Args&& ...args) {
...@@ -75,7 +74,7 @@ private: ...@@ -75,7 +74,7 @@ private:
} }
std::shared_ptr<Handler> handler_; std::shared_ptr<Handler> handler_;
Net::Tcp::Listener listener; Tcp::Listener listener;
}; };
template<typename Handler> template<typename Handler>
...@@ -96,5 +95,4 @@ void listenAndServe(Address addr, const Endpoint::Options& options) ...@@ -96,5 +95,4 @@ void listenAndServe(Address addr, const Endpoint::Options& options)
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -5,10 +5,13 @@ ...@@ -5,10 +5,13 @@
*/ */
#pragma once #pragma once
#include <type_traits> #include <type_traits>
#include <climits> #include <climits>
#include <iostream> #include <iostream>
namespace Pistache {
// Looks like gcc 4.6 does not implement std::underlying_type // Looks like gcc 4.6 does not implement std::underlying_type
namespace detail { namespace detail {
template<size_t N> struct TypeStorage; template<size_t N> struct TypeStorage;
...@@ -120,9 +123,11 @@ private: ...@@ -120,9 +123,11 @@ private:
T val; T val;
}; };
} // namespace Pistache
#define DEFINE_BITWISE_OP(Op, T) \ #define DEFINE_BITWISE_OP(Op, T) \
inline T operator Op (T lhs, T rhs) { \ inline T operator Op (T lhs, T rhs) { \
typedef detail::UnderlyingType<T>::Type UnderlyingType; \ typedef Pistache::detail::UnderlyingType<T>::Type UnderlyingType; \
return static_cast<T>( \ return static_cast<T>( \
static_cast<UnderlyingType>(lhs) Op static_cast<UnderlyingType>(rhs) \ static_cast<UnderlyingType>(lhs) Op static_cast<UnderlyingType>(rhs) \
); \ ); \
...@@ -133,8 +138,8 @@ private: ...@@ -133,8 +138,8 @@ private:
DEFINE_BITWISE_OP(|, T) DEFINE_BITWISE_OP(|, T)
template<typename T> template<typename T>
std::ostream& operator<<(std::ostream& os, Flags<T> flags) { std::ostream& operator<<(std::ostream& os, Pistache::Flags<T> flags) {
typedef typename detail::UnderlyingType<T>::Type UnderlyingType; typedef typename Pistache::detail::UnderlyingType<T>::Type UnderlyingType;
auto val = static_cast<UnderlyingType>(static_cast<T>(flags)); auto val = static_cast<UnderlyingType>(static_cast<T>(flags));
for (ssize_t i = (sizeof(UnderlyingType) * CHAR_BIT) - 1; i >= 0; --i) { for (ssize_t i = (sizeof(UnderlyingType) * CHAR_BIT) - 1; i >= 0; --i) {
...@@ -143,3 +148,4 @@ std::ostream& operator<<(std::ostream& os, Flags<T> flags) { ...@@ -143,3 +148,4 @@ std::ostream& operator<<(std::ostream& os, Flags<T> flags) {
return os; return os;
} }
...@@ -6,24 +6,25 @@ ...@@ -6,24 +6,25 @@
#pragma once #pragma once
#include <sys/timerfd.h>
#include <type_traits> #include <type_traits>
#include <stdexcept> #include <stdexcept>
#include <array> #include <array>
#include <sstream> #include <sstream>
#include "net.h"
#include "http_headers.h"
#include "http_defs.h"
#include "cookie.h"
#include "stream.h"
#include "mime.h"
#include "async.h"
#include "peer.h"
#include "tcp.h"
#include "transport.h"
namespace Net {
#include <sys/timerfd.h>
#include <pistache/net.h>
#include <pistache/http_headers.h>
#include <pistache/http_defs.h>
#include <pistache/cookie.h>
#include <pistache/stream.h>
#include <pistache/mime.h>
#include <pistache/async.h>
#include <pistache/peer.h>
#include <pistache/tcp.h>
#include <pistache/transport.h>
namespace Pistache {
namespace Http { namespace Http {
namespace details { namespace details {
...@@ -40,8 +41,8 @@ namespace details { ...@@ -40,8 +41,8 @@ namespace details {
}; };
#define HTTP_PROTOTYPE(Class) \ #define HTTP_PROTOTYPE(Class) \
PROTOTYPE_OF(Net::Tcp::Handler, Class) \ PROTOTYPE_OF(Pistache::Tcp::Handler, Class) \
typedef Net::Http::details::prototype_tag tag; typedef Pistache::Http::details::prototype_tag tag;
namespace Private { namespace Private {
class ParserBase; class ParserBase;
...@@ -338,7 +339,7 @@ inline ResponseStream& flush(ResponseStream& stream) { ...@@ -338,7 +339,7 @@ inline ResponseStream& flush(ResponseStream& stream) {
template<typename T> template<typename T>
ResponseStream& operator<<(ResponseStream& stream, const T& val) { ResponseStream& operator<<(ResponseStream& stream, const T& val) {
Net::Size<T> size; Size<T> size;
std::ostream os(&stream.buf_); std::ostream os(&stream.buf_);
os << std::hex << size(val) << crlf; os << std::hex << size(val) << crlf;
...@@ -737,7 +738,7 @@ namespace Private { ...@@ -737,7 +738,7 @@ namespace Private {
} // namespace Private } // namespace Private
class Handler : public Net::Tcp::Handler { class Handler : public Tcp::Handler {
public: public:
void onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer>& peer); void onInput(const char* buffer, size_t len, const std::shared_ptr<Tcp::Peer>& peer);
...@@ -761,7 +762,4 @@ std::shared_ptr<H> make_handler(Args&& ...args) { ...@@ -761,7 +762,4 @@ std::shared_ptr<H> make_handler(Args&& ...args) {
} }
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
*/ */
#pragma once #pragma once
#include <string> #include <string>
#include <ostream> #include <ostream>
#include <stdexcept> #include <stdexcept>
...@@ -12,8 +13,7 @@ ...@@ -12,8 +13,7 @@
#include <ctime> #include <ctime>
#include <functional> #include <functional>
namespace Net { namespace Pistache {
namespace Http { namespace Http {
#define HTTP_METHODS \ #define HTTP_METHODS \
...@@ -198,14 +198,13 @@ private: ...@@ -198,14 +198,13 @@ private:
}; };
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
namespace std { namespace std {
template<> template<>
struct hash<Net::Http::Method> { struct hash<Pistache::Http::Method> {
size_t operator()(Net::Http::Method method) const { size_t operator()(Pistache::Http::Method method) const {
return std::hash<int>()(static_cast<int>(method)); return std::hash<int>()(static_cast<int>(method));
} }
}; };
......
...@@ -6,21 +6,20 @@ ...@@ -6,21 +6,20 @@
#pragma once #pragma once
#include "mime.h"
#include "net.h"
#include "http_defs.h"
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <memory> #include <memory>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#define SAFE_HEADER_CAST #include <pistache/mime.h>
#include <pistache/net.h>
#include <pistache/http_defs.h>
namespace Net { #define SAFE_HEADER_CAST
namespace Pistache {
namespace Http { namespace Http {
namespace Header { namespace Header {
#ifdef SAFE_HEADER_CAST #ifdef SAFE_HEADER_CAST
...@@ -45,7 +44,7 @@ constexpr uint64_t hash(const char* str) ...@@ -45,7 +44,7 @@ constexpr uint64_t hash(const char* str)
#ifdef SAFE_HEADER_CAST #ifdef SAFE_HEADER_CAST
#define NAME(header_name) \ #define NAME(header_name) \
static constexpr uint64_t Hash = Net::Http::Header::detail::hash(header_name); \ static constexpr uint64_t Hash = Pistache::Http::Header::detail::hash(header_name); \
uint64_t hash() const { return Hash; } \ uint64_t hash() const { return Hash; } \
static constexpr const char *Name = header_name; \ static constexpr const char *Name = header_name; \
const char *name() const { return Name; } const char *name() const { return Name; }
...@@ -337,7 +336,7 @@ public: ...@@ -337,7 +336,7 @@ public:
{ } { }
explicit Host(const std::string& host); explicit Host(const std::string& host);
explicit Host(const std::string& host, Net::Port port) explicit Host(const std::string& host, Port port)
: host_(host) : host_(host)
, port_(port) , port_(port)
{ } { }
...@@ -346,11 +345,11 @@ public: ...@@ -346,11 +345,11 @@ public:
void write(std::ostream& os) const; void write(std::ostream& os) const;
std::string host() const { return host_; } std::string host() const { return host_; }
Net::Port port() const { return port_; } Port port() const { return port_; }
private: private:
std::string host_; std::string host_;
Net::Port port_; Port port_;
}; };
class Location : public Header { class Location : public Header {
...@@ -438,8 +437,5 @@ private: ...@@ -438,8 +437,5 @@ private:
}; };
} // namespace Header } // namespace Header
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -9,12 +9,11 @@ ...@@ -9,12 +9,11 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include "http_header.h"
namespace Net { #include <pistache/http_header.h>
namespace Pistache {
namespace Http { namespace Http {
namespace Header { namespace Header {
class Collection { class Collection {
...@@ -145,7 +144,5 @@ struct Registrar { ...@@ -145,7 +144,5 @@ struct Registrar {
} // namespace Header } // namespace Header
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#pragma once #pragma once
namespace Pistache {
template<typename Map> template<typename Map>
struct FlatMapIteratorAdapter { struct FlatMapIteratorAdapter {
typedef typename Map::key_type Key; typedef typename Map::key_type Key;
...@@ -42,3 +44,5 @@ FlatMapIteratorAdapter<Map> ...@@ -42,3 +44,5 @@ FlatMapIteratorAdapter<Map>
makeFlatMapIterator(const Map&, typename Map::const_iterator it) { makeFlatMapIterator(const Map&, typename Map::const_iterator it) {
return FlatMapIteratorAdapter<Map>(it); return FlatMapIteratorAdapter<Map>(it);
} }
} // namespace Pistache
...@@ -6,19 +6,20 @@ ...@@ -6,19 +6,20 @@
#pragma once #pragma once
#include "tcp.h"
#include "net.h"
#include "os.h"
#include "flags.h"
#include "async.h"
#include "reactor.h"
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <thread> #include <thread>
#include <sys/resource.h> #include <sys/resource.h>
namespace Net { #include <pistache/tcp.h>
#include <pistache/net.h>
#include <pistache/os.h>
#include <pistache/flags.h>
#include <pistache/async.h>
#include <pistache/reactor.h>
namespace Pistache {
namespace Tcp { namespace Tcp {
class Peer; class Peer;
...@@ -88,5 +89,4 @@ private: ...@@ -88,5 +89,4 @@ private:
}; };
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -6,15 +6,19 @@ ...@@ -6,15 +6,19 @@
*/ */
#pragma once #pragma once
#include "common.h"
#include "os.h"
#include <atomic> #include <atomic>
#include <stdexcept> #include <stdexcept>
#include <array> #include <array>
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <unistd.h> #include <unistd.h>
#include <pistache/common.h>
#include <pistache/os.h>
namespace Pistache {
static constexpr size_t CachelineSize = 64; static constexpr size_t CachelineSize = 64;
typedef char cacheline_pad_t[CachelineSize]; typedef char cacheline_pad_t[CachelineSize];
...@@ -414,3 +418,5 @@ private: ...@@ -414,3 +418,5 @@ private:
cacheline_pad_t pad1; cacheline_pad_t pad1;
std::atomic<size_t> dequeueIndex; std::atomic<size_t> dequeueIndex;
}; };
} // namespace Pistache
...@@ -6,16 +6,15 @@ ...@@ -6,16 +6,15 @@
#pragma once #pragma once
#include "optional.h"
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <stdexcept> #include <stdexcept>
#include <cassert> #include <cassert>
namespace Net { #include <pistache/optional.h>
namespace Pistache {
namespace Http { namespace Http {
namespace Mime { namespace Mime {
#define MIME_TYPES \ #define MIME_TYPES \
...@@ -213,13 +212,11 @@ inline bool operator==(const MediaType& lhs, const MediaType& rhs) { ...@@ -213,13 +212,11 @@ inline bool operator==(const MediaType& lhs, const MediaType& rhs) {
} }
} // namespace Mime } // namespace Mime
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
#define MIME(top, sub) \ #define MIME(top, sub) \
Net::Http::Mime::MediaType(Net::Http::Mime::Type::top, Net::Http::Mime::Subtype::sub) Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, Pistache::Http::Mime::Subtype::sub)
#define MIME3(top, sub, suffix) \ #define MIME3(top, sub, suffix) \
Net::Http::Mime::MediaType(Net::Http::Mime::Type::top, Net::Http::Mime::Subtype::sub, Net::Http::Mime::Suffix::suffix) Pistache::Http::Mime::MediaType(Pistache::Http::Mime::Type::top, Pistache::Http::Mime::Subtype::sub, Pistache::Http::Mime::Suffix::suffix)
...@@ -5,12 +5,14 @@ ...@@ -5,12 +5,14 @@
*/ */
#pragma once #pragma once
#include <string> #include <string>
#include <sys/socket.h>
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
#include <limits> #include <limits>
#include <sys/socket.h>
#ifndef _KERNEL_FASTOPEN #ifndef _KERNEL_FASTOPEN
#define _KERNEL_FASTOPEN #define _KERNEL_FASTOPEN
...@@ -20,7 +22,7 @@ ...@@ -20,7 +22,7 @@
#endif #endif
#endif #endif
namespace Net { namespace Pistache {
class Port { class Port {
public: public:
...@@ -147,5 +149,4 @@ struct Size<char> { ...@@ -147,5 +149,4 @@ struct Size<char> {
} }
}; };
} // namespace Pistache
} // namespace Net
...@@ -14,13 +14,15 @@ ...@@ -14,13 +14,15 @@
#include <tuple> #include <tuple>
#include <functional> #include <functional>
namespace Pistache {
template<typename T> class Optional; template<typename T> class Optional;
namespace types { namespace types {
template<typename T> template<typename T>
class Some { class Some {
public: public:
template<typename U> friend class ::Optional; template<typename U> friend class Pistache::Optional;
Some(const T &val) : val_(val) { } Some(const T &val) : val_(val) { }
Some(T &&val) : val_(std::move(val)) { } Some(T &&val) : val_(std::move(val)) { }
...@@ -376,3 +378,5 @@ Optional<T> optionally_filter(const Optional<T> &option, Func func) { ...@@ -376,3 +378,5 @@ Optional<T> optionally_filter(const Optional<T> &option, Func func) {
return None(); return None();
} }
} // namespace Pistache
...@@ -10,9 +10,13 @@ ...@@ -10,9 +10,13 @@
#include <chrono> #include <chrono>
#include <vector> #include <vector>
#include <bitset> #include <bitset>
#include <sched.h> #include <sched.h>
#include "flags.h"
#include "common.h" #include <pistache/flags.h>
#include <pistache/common.h>
namespace Pistache {
typedef int Fd; typedef int Fd;
...@@ -135,3 +139,4 @@ private: ...@@ -135,3 +139,4 @@ private:
int event_fd; int event_fd;
}; };
} // namespace Pistache
...@@ -6,17 +6,17 @@ ...@@ -6,17 +6,17 @@
#pragma once #pragma once
#include "net.h"
#include "os.h"
#include "async.h"
#include "stream.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
namespace Net { #include <pistache/net.h>
#include <pistache/os.h>
#include <pistache/async.h>
#include <pistache/stream.h>
namespace Pistache {
namespace Tcp { namespace Tcp {
class Transport; class Transport;
...@@ -68,5 +68,4 @@ private: ...@@ -68,5 +68,4 @@ private:
std::ostream& operator<<(std::ostream& os, const Peer& peer); std::ostream& operator<<(std::ostream& os, const Peer& peer);
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -9,12 +9,16 @@ ...@@ -9,12 +9,16 @@
#include <type_traits> #include <type_traits>
#include <memory> #include <memory>
namespace Pistache {
/* In a sense, a Prototype is just a class that provides a clone() method */ /* In a sense, a Prototype is just a class that provides a clone() method */
template<typename Class> template<typename Class>
struct Prototype { struct Prototype {
virtual std::shared_ptr<Class> clone() const = 0; virtual std::shared_ptr<Class> clone() const = 0;
}; };
} // namespace Pistache
#define PROTOTYPE_OF(Base, Class) \ #define PROTOTYPE_OF(Base, Class) \
private: \ private: \
std::shared_ptr<Base> clone() const { \ std::shared_ptr<Base> clone() const { \
......
...@@ -10,19 +10,22 @@ ...@@ -10,19 +10,22 @@
#pragma once #pragma once
#include "flags.h"
#include "os.h"
#include "net.h"
#include "prototype.h"
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <atomic>
#include <memory> #include <memory>
#include <unordered_map> #include <unordered_map>
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#include <atomic>
#include <pistache/flags.h>
#include <pistache/os.h>
#include <pistache/net.h>
#include <pistache/prototype.h>
namespace Pistache {
namespace Aio { namespace Aio {
// A set of fds that are ready // A set of fds that are ready
...@@ -222,3 +225,4 @@ private: ...@@ -222,3 +225,4 @@ private:
}; };
} // namespace Aio } // namespace Aio
} // namespace Pistache
/*
Mathieu Stefani, 27 février 2016
A special bind() method for REST routes
*/
#pragma once
namespace Pistache {
namespace Rest {
namespace Route {
void Get(Router& router, std::string resource, Route::Handler handler);
void Post(Router& router, std::string resource, Route::Handler handler);
void Put(Router& router, std::string resource, Route::Handler handler);
void Delete(Router& router, std::string resource, Route::Handler handler);
namespace details {
template <class... Args>
struct TypeList
{
template<size_t N>
struct At {
static_assert(N < sizeof...(Args), "Invalid index");
typedef typename std::tuple_element<N, std::tuple<Args...>>::type Type;
};
};
template<typename... Args>
void static_checks() {
static_assert(sizeof...(Args) == 2, "Function should take 2 parameters");
typedef details::TypeList<Args...> Arguments;
// Disabled now as it
// 1/ does not compile
// 2/ might not be relevant
#if 0
static_assert(std::is_same<Arguments::At<0>::Type, const Rest::Request&>::value, "First argument should be a const Rest::Request&");
static_assert(std::is_same<typename Arguments::At<0>::Type, Http::Response>::value, "Second argument should be a Http::Response");
#endif
}
}
template<typename Result, typename Cls, typename... Args, typename Obj>
Route::Handler bind(Result (Cls::*func)(Args...), Obj obj) {
details::static_checks<Args...>();
#define CALL_MEMBER_FN(obj, pmf) ((obj)->*(pmf))
return [=](const Rest::Request& request, Http::ResponseWriter response) {
CALL_MEMBER_FN(obj, func)(request, std::move(response));
};
#undef CALL_MEMBER_FN
}
template<typename Result, typename... Args>
Route::Handler bind(Result (*func)(Args...)) {
details::static_checks<Args...>();
return [=](const Rest::Request& request, Http::ResponseWriter response) {
func(request, std::move(response));
};
}
} // namespace Route
} // namespace Rest
} // namespace Pistache
...@@ -8,12 +8,12 @@ ...@@ -8,12 +8,12 @@
#include <string> #include <string>
#include <tuple> #include <tuple>
#include "http.h"
#include "http_defs.h"
#include "flags.h"
namespace Net { #include <pistache/http.h>
#include <pistache/http_defs.h>
#include <pistache/flags.h>
namespace Pistache {
namespace Rest { namespace Rest {
class Description; class Description;
...@@ -63,7 +63,7 @@ class Request; ...@@ -63,7 +63,7 @@ class Request;
struct Route { struct Route {
enum class Result { Ok, Failure }; enum class Result { Ok, Failure };
typedef std::function<Result (const Request&, Net::Http::ResponseWriter)> Handler; typedef std::function<Result (const Request&, Http::ResponseWriter)> Handler;
Route(std::string resource, Http::Method method, Handler handler) Route(std::string resource, Http::Method method, Handler handler)
: resource_(std::move(resource)) : resource_(std::move(resource))
...@@ -119,7 +119,7 @@ private: ...@@ -119,7 +119,7 @@ private:
}; };
std::string resource_; std::string resource_;
Net::Http::Method method_; Http::Method method_;
Handler handler_; Handler handler_;
/* @Performance: since we know that resource_ will live as long as the vector underneath, /* @Performance: since we know that resource_ will live as long as the vector underneath,
* we would benefit from std::experimental::string_view to store fragments. * we would benefit from std::experimental::string_view to store fragments.
...@@ -167,7 +167,7 @@ private: ...@@ -167,7 +167,7 @@ private:
namespace Private { namespace Private {
class RouterHandler : public Net::Http::Handler { class RouterHandler : public Http::Handler {
public: public:
RouterHandler(const Rest::Router& router); RouterHandler(const Rest::Router& router);
...@@ -176,7 +176,7 @@ namespace Private { ...@@ -176,7 +176,7 @@ namespace Private {
Http::ResponseWriter response); Http::ResponseWriter response);
private: private:
std::shared_ptr<Net::Tcp::Handler> clone() const { std::shared_ptr<Tcp::Handler> clone() const {
return std::make_shared<RouterHandler>(router); return std::make_shared<RouterHandler>(router);
} }
...@@ -267,7 +267,5 @@ namespace Routes { ...@@ -267,7 +267,5 @@ namespace Routes {
} }
} // namespace Routing } // namespace Routing
} // namespace Rest } // namespace Rest
} // namespace Pistache
} // namespace Net
...@@ -6,15 +6,14 @@ ...@@ -6,15 +6,14 @@
#pragma once #pragma once
#include "description.h"
#include <rapidjson/prettywriter.h> #include <rapidjson/prettywriter.h>
#include "http_defs.h"
#include "mime.h"
namespace Net { #include <pistache/description.h>
#include <pistache/http_defs.h>
#include <pistache/mime.h>
namespace Pistache {
namespace Rest { namespace Rest {
namespace Serializer { namespace Serializer {
template<typename Writer> template<typename Writer>
...@@ -212,7 +211,5 @@ std::string rapidJson(const Description& desc) { ...@@ -212,7 +211,5 @@ std::string rapidJson(const Description& desc) {
} }
} // namespace Serializer } // namespace Serializer
} // namespace Rest } // namespace Rest
} // namespace Pistache
} // namespace Net
...@@ -13,7 +13,10 @@ ...@@ -13,7 +13,10 @@
#include <vector> #include <vector>
#include <limits> #include <limits>
#include <iostream> #include <iostream>
#include "os.h"
#include <pistache/os.h>
namespace Pistache {
static constexpr char CR = 0xD; static constexpr char CR = 0xD;
static constexpr char LF = 0xA; static constexpr char LF = 0xA;
...@@ -332,3 +335,5 @@ bool match_until(std::initializer_list<char> chars, StreamCursor& cursor, CaseSe ...@@ -332,3 +335,5 @@ bool match_until(std::initializer_list<char> chars, StreamCursor& cursor, CaseSe
bool match_double(double* val, StreamCursor& cursor); bool match_double(double* val, StreamCursor& cursor);
void skip_whitespaces(StreamCursor& cursor); void skip_whitespaces(StreamCursor& cursor);
} // namespace Pistache
...@@ -6,13 +6,13 @@ ...@@ -6,13 +6,13 @@
#pragma once #pragma once
#include "flags.h"
#include "prototype.h"
#include <memory> #include <memory>
#include <stdexcept> #include <stdexcept>
namespace Net { #include <pistache/flags.h>
#include <pistache/prototype.h>
namespace Pistache {
namespace Tcp { namespace Tcp {
class Peer; class Peer;
...@@ -56,5 +56,4 @@ private: ...@@ -56,5 +56,4 @@ private:
}; };
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -14,11 +14,13 @@ ...@@ -14,11 +14,13 @@
#include <vector> #include <vector>
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
#include <unistd.h> #include <unistd.h>
#include "os.h"
#include "reactor.h"
namespace Net { #include <pistache/os.h>
#include <pistache/reactor.h>
namespace Pistache {
namespace Default { namespace Default {
static constexpr size_t InitialPoolSize = 128; static constexpr size_t InitialPoolSize = 128;
...@@ -79,4 +81,4 @@ private: ...@@ -79,4 +81,4 @@ private:
std::vector<std::shared_ptr<Entry>> timers; std::vector<std::shared_ptr<Entry>> timers;
}; };
} // namespace Net } // namespace Pistache
...@@ -6,14 +6,13 @@ ...@@ -6,14 +6,13 @@
#pragma once #pragma once
#include "reactor.h" #include <pistache/reactor.h>
#include "mailbox.h" #include <pistache/mailbox.h>
#include "optional.h" #include <pistache/optional.h>
#include "async.h" #include <pistache/async.h>
#include "stream.h" #include <pistache/stream.h>
namespace Net {
namespace Pistache {
namespace Tcp { namespace Tcp {
class Peer; class Peer;
...@@ -50,7 +49,7 @@ public: ...@@ -50,7 +49,7 @@ public:
auto it = toWrite.find(fd); auto it = toWrite.find(fd);
if (it != std::end(toWrite)) { if (it != std::end(toWrite)) {
reject(Net::Error("Multiple writes on the same fd")); reject(Pistache::Error("Multiple writes on the same fd"));
return; return;
} }
...@@ -255,5 +254,4 @@ private: ...@@ -255,5 +254,4 @@ private:
}; };
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#pragma once #pragma once
namespace Pistache {
class TypeId { class TypeId {
public: public:
template<typename T> template<typename T>
...@@ -51,9 +53,11 @@ inline bool operator<(const TypeId& lhs, const TypeId& rhs) { ...@@ -51,9 +53,11 @@ inline bool operator<(const TypeId& lhs, const TypeId& rhs) {
#undef APPLY_OP #undef APPLY_OP
} // namespace Pistache
namespace std { namespace std {
template<> struct hash<TypeId> { template<> struct hash<Pistache::TypeId> {
size_t operator()(const TypeId& id) { size_t operator()(const Pistache::TypeId& id) {
return static_cast<size_t>(id); return static_cast<size_t>(id);
} }
}; };
......
...@@ -13,6 +13,8 @@ ...@@ -13,6 +13,8 @@
#include <cstring> #include <cstring>
#include <stdexcept> #include <stdexcept>
namespace Pistache {
template<typename T> template<typename T>
class ViewBase { class ViewBase {
public: public:
...@@ -253,3 +255,5 @@ View<typename impl::ViewBuilder<Cont>::Type> make_view(const Cont& cont, size_t ...@@ -253,3 +255,5 @@ View<typename impl::ViewBuilder<Cont>::Type> make_view(const Cont& cont, size_t
{ {
return impl::ViewBuilder<Cont>::buildSized(cont, size); return impl::ViewBuilder<Cont>::buildSized(cont, size);
} }
} // namespace Pistache
...@@ -2,17 +2,41 @@ file (GLOB COMMON_SOURCE_FILES "common/*.cc") ...@@ -2,17 +2,41 @@ file (GLOB COMMON_SOURCE_FILES "common/*.cc")
file (GLOB SERVER_SOURCE_FILES "server/*.cc") file (GLOB SERVER_SOURCE_FILES "server/*.cc")
file (GLOB CLIENT_SOURCE_FILES "client/*.cc") file (GLOB CLIENT_SOURCE_FILES "client/*.cc")
file (GLOB INCLUDE_FILES ${PROJECT_SOURCE_DIR}/include/pistache/*h)
set(SOURCE_FILES set(SOURCE_FILES
${COMMON_SOURCE_FILES} ${COMMON_SOURCE_FILES}
${SERVER_SOURCE_FILES} ${SERVER_SOURCE_FILES}
${CLIENT_SOURCE_FILES} ${CLIENT_SOURCE_FILES}
${INCLUDE_FILES}
)
add_library(pistache ${SOURCE_FILES})
set_target_properties(pistache PROPERTIES
OUTPUT_NAME "pistache"
VERSION ${GENERIC_LIB_VERSION}
SOVERSION ${GENERIC_LIB_SOVERSION}
) )
add_library(net_static ${SOURCE_FILES}) target_include_directories(pistache PUBLIC
target_link_libraries(net_static pthread) $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
install(TARGETS net_static DESTINATION lib) )
set(include_install_dir "include")
set(lib_install_dir "lib/")
set(bin_install_dir "bin/")
target_link_libraries(pistache pthread)
add_library(net SHARED ${SOURCE_FILES}) install(
target_link_libraries(net pthread) TARGETS pistache
install(TARGETS net DESTINATION lib) EXPORT ${targets_export_name}
ARCHIVE DESTINATION ${lib_install_dir}
LIBRARY DESTINATION ${lib_install_dir}
RUNTIME DESTINATION ${bin_install_dir}
INCLUDES DESTINATION ${include_install_dir})
install(
DIRECTORY "${PROJECT_SOURCE_DIR}/include/pistache"
DESTINATION ${include_install_dir}
FILES_MATCHING PATTERN "*.*h")
...@@ -4,29 +4,32 @@ ...@@ -4,29 +4,32 @@
Implementation of the Http client Implementation of the Http client
*/ */
#include "client.h"
#include "stream.h"
#include <algorithm> #include <algorithm>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <netdb.h> #include <netdb.h>
using namespace Polling; #include <pistache/client.h>
#include <pistache/stream.h>
namespace Net { namespace Pistache {
using namespace Polling;
namespace Http { namespace Http {
namespace { namespace {
Net::Address httpAddr(const StringView& view) { Address httpAddr(const StringView& view) {
auto str = view.toString(); auto str = view.toString();
auto pos = str.find(':'); auto pos = str.find(':');
if (pos == std::string::npos) { if (pos == std::string::npos) {
return Net::Address(std::move(str), 80); return Address(std::move(str), 80);
} }
auto host = str.substr(0, pos); auto host = str.substr(0, pos);
auto port = std::stoi(str.substr(pos + 1)); auto port = std::stoi(str.substr(pos + 1));
return Net::Address(std::move(host), port); return Address(std::move(host), port);
} }
} }
...@@ -68,8 +71,10 @@ namespace { ...@@ -68,8 +71,10 @@ namespace {
} while (0) } while (0)
template<typename H, typename Stream, typename... Args> template<typename H, typename Stream, typename... Args>
typename std::enable_if<Header::IsHeader<H>::value, Stream&>::type typename std::enable_if<Http::Header::IsHeader<H>::value, Stream&>::type
writeHeader(Stream& stream, Args&& ...args) { writeHeader(Stream& stream, Args&& ...args) {
using Http::crlf;
H header(std::forward<Args>(args)...); H header(std::forward<Args>(args)...);
stream << H::Name << ": "; stream << H::Name << ": ";
...@@ -80,7 +85,9 @@ namespace { ...@@ -80,7 +85,9 @@ namespace {
return stream; return stream;
} }
bool writeHeaders(const Header::Collection& headers, DynamicStreamBuf& buf) { bool writeHeaders(const Http::Header::Collection& headers, DynamicStreamBuf& buf) {
using Http::crlf;
std::ostream os(&buf); std::ostream os(&buf);
for (const auto& header: headers.list()) { for (const auto& header: headers.list()) {
...@@ -92,7 +99,9 @@ namespace { ...@@ -92,7 +99,9 @@ namespace {
return true; return true;
} }
bool writeCookies(const CookieJar& cookies, DynamicStreamBuf& buf) { bool writeCookies(const Http::CookieJar& cookies, DynamicStreamBuf& buf) {
using Http::crlf;
std::ostream os(&buf); std::ostream os(&buf);
for (const auto& cookie: cookies) { for (const auto& cookie: cookies) {
OUT(os << "Cookie: "); OUT(os << "Cookie: ");
...@@ -105,6 +114,8 @@ namespace { ...@@ -105,6 +114,8 @@ namespace {
} }
bool writeRequest(const Http::Request& request, DynamicStreamBuf &buf) { bool writeRequest(const Http::Request& request, DynamicStreamBuf &buf) {
using Http::crlf;
std::ostream os(&buf); std::ostream os(&buf);
auto res = request.resource(); auto res = request.resource();
...@@ -125,10 +136,10 @@ namespace { ...@@ -125,10 +136,10 @@ namespace {
if (!writeCookies(request.cookies(), buf)) return false; if (!writeCookies(request.cookies(), buf)) return false;
if (!writeHeaders(request.headers(), buf)) return false; if (!writeHeaders(request.headers(), buf)) return false;
if (!writeHeader<Header::UserAgent>(os, UA)) return false; if (!writeHeader<Http::Header::UserAgent>(os, UA)) return false;
if (!writeHeader<Header::Host>(os, host.toString())) return false; if (!writeHeader<Http::Header::Host>(os, host.toString())) return false;
if (!body.empty()) { if (!body.empty()) {
if (!writeHeader<Header::ContentLength>(os, body.size())) return false; if (!writeHeader<Http::Header::ContentLength>(os, body.size())) return false;
} }
OUT(os << crlf); OUT(os << crlf);
...@@ -255,7 +266,7 @@ Transport::asyncSendRequestImpl( ...@@ -255,7 +266,7 @@ Transport::asyncSendRequestImpl(
} }
else { else {
cleanUp(); cleanUp();
req.reject(Net::Error::system("Could not send request")); req.reject(Error::system("Could not send request"));
} }
break; break;
} }
...@@ -363,7 +374,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) { ...@@ -363,7 +374,7 @@ Transport::handleTimeout(const std::shared_ptr<Connection>& connection) {
} }
void void
Connection::connect(Net::Address addr) Connection::connect(Address addr)
{ {
struct addrinfo hints; struct addrinfo hints;
struct addrinfo *addrs; struct addrinfo *addrs;
...@@ -474,7 +485,7 @@ Connection::handleError(const char* error) { ...@@ -474,7 +485,7 @@ Connection::handleError(const char* error) {
timerPool_.releaseTimer(req.timer); timerPool_.releaseTimer(req.timer);
} }
req.reject(Net::Error(error)); req.reject(Error(error));
req.onDone(); req.onDone();
} }
...@@ -898,5 +909,4 @@ Client::processRequestQueue() { ...@@ -898,5 +909,4 @@ Client::processRequestQueue() {
} }
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
Cookie implementation Cookie implementation
*/ */
#include "cookie.h"
#include "stream.h"
#include <iterator> #include <iterator>
#include <unordered_map> #include <unordered_map>
namespace Net { #include <pistache/cookie.h>
#include <pistache/stream.h>
namespace Pistache {
namespace Http { namespace Http {
namespace { namespace {
...@@ -226,5 +226,4 @@ CookieJar::has(const std::string& name) const { ...@@ -226,5 +226,4 @@ CookieJar::has(const std::string& name) const {
} }
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
Implementation of the description system Implementation of the description system
*/ */
#include "description.h"
#include "http_header.h"
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
namespace Net { #include <pistache/description.h>
#include <pistache/http_header.h>
namespace Pistache {
namespace Rest { namespace Rest {
const char* schemeString(Scheme scheme) { const char* schemeString(Scheme scheme) {
...@@ -447,5 +447,4 @@ Swagger::install(Rest::Router& router) { ...@@ -447,5 +447,4 @@ Swagger::install(Rest::Router& router) {
} // namespace Rest } // namespace Rest
} // namespace Pistache
} // namespace Net
...@@ -9,20 +9,21 @@ ...@@ -9,20 +9,21 @@
#include <stdexcept> #include <stdexcept>
#include <ctime> #include <ctime>
#include <iomanip> #include <iomanip>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include "common.h"
#include "http.h"
#include "net.h"
#include "peer.h"
#include "transport.h"
using namespace std; #include <pistache/common.h>
#include <pistache/http.h>
#include <pistache/net.h>
#include <pistache/peer.h>
#include <pistache/transport.h>
namespace Net { using namespace std;
namespace Pistache {
namespace Http { namespace Http {
template<typename H, typename Stream, typename... Args> template<typename H, typename Stream, typename... Args>
...@@ -673,7 +674,7 @@ serveFile(ResponseWriter& response, const char* fileName, const Mime::MediaType& ...@@ -673,7 +674,7 @@ serveFile(ResponseWriter& response, const char* fileName, const Mime::MediaType&
/* @Improvement: maybe could we check for errno here and emit a different error /* @Improvement: maybe could we check for errno here and emit a different error
message message
*/ */
throw HttpError(Net::Http::Code::Not_Found, ""); throw HttpError(Http::Code::Not_Found, "");
} }
int res = ::fstat(fd, &sb); int res = ::fstat(fd, &sb);
...@@ -796,5 +797,4 @@ Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const { ...@@ -796,5 +797,4 @@ Handler::getParser(const std::shared_ptr<Tcp::Peer>& peer) const {
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,13 +4,13 @@ ...@@ -4,13 +4,13 @@
Implementation of http definitions Implementation of http definitions
*/ */
#include "http_defs.h"
#include "common.h"
#include <iostream> #include <iostream>
#include <iomanip> #include <iomanip>
namespace Net { #include <pistache/http_defs.h>
#include <pistache/common.h>
namespace Pistache {
namespace Http { namespace Http {
namespace { namespace {
...@@ -189,5 +189,4 @@ HttpError::HttpError(int code, std::string reason) ...@@ -189,5 +189,4 @@ HttpError::HttpError(int code, std::string reason)
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,21 +4,20 @@ ...@@ -4,21 +4,20 @@
Implementation of common HTTP headers described by the RFC Implementation of common HTTP headers described by the RFC
*/ */
#include "http_header.h"
#include "common.h"
#include "http.h"
#include "stream.h"
#include <stdexcept> #include <stdexcept>
#include <iterator> #include <iterator>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
using namespace std; #include <pistache/http_header.h>
#include <pistache/common.h>
#include <pistache/http.h>
#include <pistache/stream.h>
namespace Net { using namespace std;
namespace Pistache {
namespace Http { namespace Http {
namespace Header { namespace Header {
const char* encodingString(Encoding encoding) { const char* encodingString(Encoding encoding) {
...@@ -486,7 +485,5 @@ ContentType::write(std::ostream& os) const { ...@@ -486,7 +485,5 @@ ContentType::write(std::ostream& os) const {
} }
} // namespace Header } // namespace Header
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,16 +4,15 @@ ...@@ -4,16 +4,15 @@
Headers registry Headers registry
*/ */
#include "http_headers.h"
#include <unordered_map> #include <unordered_map>
#include <iterator> #include <iterator>
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
namespace Net { #include <pistache/http_headers.h>
namespace Pistache {
namespace Http { namespace Http {
namespace Header { namespace Header {
namespace { namespace {
...@@ -190,7 +189,5 @@ Collection::getImpl(const std::string& name) const { ...@@ -190,7 +189,5 @@ Collection::getImpl(const std::string& name) const {
} }
} // namespace Header } // namespace Header
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,16 +4,15 @@ ...@@ -4,16 +4,15 @@
Implementaton of MIME Type parsing Implementaton of MIME Type parsing
*/ */
#include "mime.h"
#include "http.h"
#include <cstring> #include <cstring>
using namespace std; #include <pistache/mime.h>
#include <pistache/http.h>
namespace Net { using namespace std;
namespace Pistache {
namespace Http { namespace Http {
namespace Mime { namespace Mime {
std::string std::string
...@@ -329,8 +328,5 @@ MediaType::isValid() const { ...@@ -329,8 +328,5 @@ MediaType::isValid() const {
} }
} // namespace Mime } // namespace Mime
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -3,19 +3,20 @@ ...@@ -3,19 +3,20 @@
*/ */
#include "net.h"
#include "common.h"
#include <stdexcept> #include <stdexcept>
#include <limits> #include <limits>
#include <cstring> #include <cstring>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <iostream> #include <iostream>
#include <pistache/net.h>
#include <pistache/common.h>
using namespace std; using namespace std;
namespace Net { namespace Pistache {
Port::Port(uint16_t port) Port::Port(uint16_t port)
: port(port) : port(port)
...@@ -139,4 +140,4 @@ Error::system(const char* message) { ...@@ -139,4 +140,4 @@ Error::system(const char* message) {
} }
} // namespace Net } // namespace Pistache
...@@ -3,18 +3,23 @@ ...@@ -3,18 +3,23 @@
*/ */
#include "os.h"
#include "common.h"
#include <unistd.h>
#include <fcntl.h>
#include <fstream> #include <fstream>
#include <iterator> #include <iterator>
#include <algorithm> #include <algorithm>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <pistache/os.h>
#include <pistache/common.h>
#include <pistache/io.h>
using namespace std; using namespace std;
namespace Pistache {
int hardware_concurrency() { int hardware_concurrency() {
std::ifstream cpuinfo("/proc/cpuinfo"); std::ifstream cpuinfo("/proc/cpuinfo");
if (cpuinfo) { if (cpuinfo) {
...@@ -289,3 +294,5 @@ NotifyFd::tryRead() const { ...@@ -289,3 +294,5 @@ NotifyFd::tryRead() const {
return true; return true;
} }
} // namespace Pistache
...@@ -3,15 +3,16 @@ ...@@ -3,15 +3,16 @@
*/ */
#include "peer.h"
#include "async.h"
#include "transport.h"
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <sys/socket.h> #include <sys/socket.h>
namespace Net { #include <pistache/peer.h>
#include <pistache/async.h>
#include <pistache/transport.h>
namespace Pistache {
namespace Tcp { namespace Tcp {
using namespace std; using namespace std;
...@@ -104,5 +105,4 @@ Peer::transport() const { ...@@ -104,5 +105,4 @@ Peer::transport() const {
} }
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -4,8 +4,9 @@ ...@@ -4,8 +4,9 @@
Implementation of the Reactor Implementation of the Reactor
*/ */
#include "reactor.h" #include <pistache/reactor.h>
namespace Pistache {
namespace Aio { namespace Aio {
struct Reactor::Impl { struct Reactor::Impl {
...@@ -599,3 +600,4 @@ AsyncContext::singleThreaded() { ...@@ -599,3 +600,4 @@ AsyncContext::singleThreaded() {
} }
} // namespace Aio } // namespace Aio
} // namespace Pistache
...@@ -3,14 +3,18 @@ ...@@ -3,14 +3,18 @@
*/ */
#include "stream.h"
#include <algorithm>
#include <iostream> #include <iostream>
#include <algorithm>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <pistache/stream.h>
namespace Pistache {
FileBuffer::FileBuffer(const char* fileName) FileBuffer::FileBuffer(const char* fileName)
: fileName_(fileName) : fileName_(fileName)
{ {
...@@ -236,3 +240,5 @@ skip_whitespaces(StreamCursor& cursor) { ...@@ -236,3 +240,5 @@ skip_whitespaces(StreamCursor& cursor) {
cursor.advance(1); cursor.advance(1);
} }
} }
} // namespace Pistache
...@@ -4,11 +4,10 @@ ...@@ -4,11 +4,10 @@
TCP TCP
*/ */
#include "tcp.h" #include <pistache/tcp.h>
#include "peer.h" #include <pistache/peer.h>
namespace Net {
namespace Pistache {
namespace Tcp { namespace Tcp {
Handler::Handler() Handler::Handler()
...@@ -32,5 +31,4 @@ Handler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) { ...@@ -32,5 +31,4 @@ Handler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) {
} }
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -4,10 +4,11 @@ ...@@ -4,10 +4,11 @@
Implementation of the timer pool Implementation of the timer pool
*/ */
#include "timer_pool.h"
#include <sys/timerfd.h> #include <sys/timerfd.h>
namespace Net { #include <pistache/timer_pool.h>
namespace Pistache {
void void
TimerPool::Entry::initialize() { TimerPool::Entry::initialize() {
...@@ -75,5 +76,4 @@ TimerPool::releaseTimer(const std::shared_ptr<Entry>& timer) { ...@@ -75,5 +76,4 @@ TimerPool::releaseTimer(const std::shared_ptr<Entry>& timer) {
timer->state.store(static_cast<uint32_t>(TimerPool::Entry::State::Idle)); timer->state.store(static_cast<uint32_t>(TimerPool::Entry::State::Idle));
} }
} // namespace Net } // namespace Pistache
#include "transport.h" /* traqnsport.cc
#include "peer.h" Mathieu Stefani, 02 July 2017
#include "tcp.h"
#include "os.h" TCP transport handling
*/
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
using namespace Polling; #include <pistache/transport.h>
#include <pistache/peer.h>
#include <pistache/tcp.h>
#include <pistache/os.h>
namespace Net {
namespace Pistache {
using namespace Polling;
namespace Tcp { namespace Tcp {
...@@ -209,7 +218,7 @@ Transport::asyncWriteImpl( ...@@ -209,7 +218,7 @@ Transport::asyncWriteImpl(
} }
else { else {
cleanUp(); cleanUp();
deferred.reject(Net::Error::system("Could not write data")); deferred.reject(Pistache::Error::system("Could not write data"));
} }
break; break;
} }
...@@ -266,7 +275,7 @@ Transport::armTimerMsImpl(TimerEntry entry) { ...@@ -266,7 +275,7 @@ Transport::armTimerMsImpl(TimerEntry entry) {
int res = timerfd_settime(entry.fd, 0, &spec, 0); int res = timerfd_settime(entry.fd, 0, &spec, 0);
if (res == -1) { if (res == -1) {
entry.deferred.reject(Net::Error::system("Could not set timer time")); entry.deferred.reject(Pistache::Error::system("Could not set timer time"));
return; return;
} }
...@@ -342,10 +351,10 @@ Transport::handleTimer(TimerEntry entry) { ...@@ -342,10 +351,10 @@ Transport::handleTimer(TimerEntry entry) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
return; return;
else else
entry.deferred.reject(Net::Error::system("Could not read timerfd")); entry.deferred.reject(Pistache::Error::system("Could not read timerfd"));
} else { } else {
if (res != sizeof(numWakeups)) { if (res != sizeof(numWakeups)) {
entry.deferred.reject(Net::Error("Read invalid number of bytes for timer fd: " entry.deferred.reject(Pistache::Error("Read invalid number of bytes for timer fd: "
+ std::to_string(entry.fd))); + std::to_string(entry.fd)));
} }
else { else {
...@@ -392,5 +401,4 @@ Transport::getPeer(Polling::Tag tag) ...@@ -392,5 +401,4 @@ Transport::getPeer(Polling::Tag tag)
} }
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -5,15 +5,13 @@ ...@@ -5,15 +5,13 @@
*/ */
#include "endpoint.h" #include <pistache/endpoint.h>
#include "tcp.h" #include <pistache/tcp.h>
#include "peer.h" #include <pistache/peer.h>
namespace Net {
namespace Pistache {
namespace Http { namespace Http {
Endpoint::Options::Options() Endpoint::Options::Options()
: threads_(1) : threads_(1)
{ } { }
...@@ -39,7 +37,7 @@ Endpoint::Options::backlog(int val) { ...@@ -39,7 +37,7 @@ Endpoint::Options::backlog(int val) {
Endpoint::Endpoint() Endpoint::Endpoint()
{ } { }
Endpoint::Endpoint(const Net::Address& addr) Endpoint::Endpoint(const Address& addr)
: listener(addr) : listener(addr)
{ } { }
...@@ -92,5 +90,4 @@ Endpoint::options() { ...@@ -92,5 +90,4 @@ Endpoint::options() {
} }
} // namespace Http } // namespace Http
} // namespace Pistache
} // namespace Net
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
*/ */
#include <iostream> #include <iostream>
#include <cassert>
#include <cstring>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
...@@ -15,18 +18,16 @@ ...@@ -15,18 +18,16 @@
#include <signal.h> #include <signal.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <cassert>
#include <cstring>
#include "listener.h"
#include "peer.h"
#include "common.h"
#include "os.h"
#include "transport.h"
using namespace std; #include <pistache/listener.h>
#include <pistache/peer.h>
#include <pistache/common.h>
#include <pistache/os.h>
#include <pistache/transport.h>
namespace Net { using namespace std;
namespace Pistache {
namespace Tcp { namespace Tcp {
namespace { namespace {
...@@ -329,5 +330,4 @@ Listener::dispatchPeer(const std::shared_ptr<Peer>& peer) { ...@@ -329,5 +330,4 @@ Listener::dispatchPeer(const std::shared_ptr<Peer>& peer) {
} }
} // namespace Tcp } // namespace Tcp
} // namespace Pistache
} // namespace Net
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
Rest routing implementation Rest routing implementation
*/ */
#include "router.h"
#include "description.h"
#include <algorithm> #include <algorithm>
namespace Net { #include <pistache/router.h>
#include <pistache/description.h>
namespace Pistache {
namespace Rest { namespace Rest {
Request::Request( Request::Request(
...@@ -352,7 +352,5 @@ void Options(Router& router, std::string resource, Route::Handler handler) { ...@@ -352,7 +352,5 @@ void Options(Router& router, std::string resource, Route::Handler handler) {
} }
} // namespace Routes } // namespace Routes
} // namespace Rest } // namespace Rest
} // namespace Pistache
} // namespace Net
...@@ -3,7 +3,7 @@ function(pistache_test test_name) ...@@ -3,7 +3,7 @@ function(pistache_test test_name)
set(TEST_SOURCE ${test_name}.cc) set(TEST_SOURCE ${test_name}.cc)
add_executable(${TEST_EXECUTABLE} ${TEST_SOURCE}) add_executable(${TEST_EXECUTABLE} ${TEST_SOURCE})
target_link_libraries(${TEST_EXECUTABLE} gtest gtest_main net_static) target_link_libraries(${TEST_EXECUTABLE} gtest gtest_main pistache)
add_test(${test_name} ${TEST_EXECUTABLE}) add_test(${test_name} ${TEST_EXECUTABLE})
endfunction() endfunction()
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "async.h"
#include <thread> #include <thread>
#include <algorithm> #include <algorithm>
#include <deque> #include <deque>
...@@ -7,6 +7,10 @@ ...@@ -7,6 +7,10 @@
#include <condition_variable> #include <condition_variable>
#include <random> #include <random>
#include <pistache/async.h>
using namespace Pistache;
Async::Promise<int> doAsync(int N) Async::Promise<int> doAsync(int N)
{ {
Async::Promise<int> promise( Async::Promise<int> promise(
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "cookie.h"
using namespace Net; #include <pistache/cookie.h>
using namespace Net::Http;
using namespace Pistache;
using namespace Pistache::Http;
void parse(const char* str, std::function<void (const Cookie&)> testFunc) void parse(const char* str, std::function<void (const Cookie&)> testFunc)
{ {
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "http_headers.h"
using namespace Net::Http; #include <pistache/http_headers.h>
using namespace Pistache::Http;
TEST(headers_test, accept) { TEST(headers_test, accept) {
Header::Accept a1; Header::Accept a1;
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "mime.h"
#include "http.h"
using namespace Net::Http; #include <pistache/mime.h>
using namespace Net::Http::Mime; #include <pistache/http.h>
using namespace Pistache::Http;
using namespace Pistache::Http::Mime;
TEST(mime_test, basic_test) { TEST(mime_test, basic_test) {
MediaType m1(Type::Text, Subtype::Plain); MediaType m1(Type::Text, Subtype::Plain);
......
...@@ -6,10 +6,11 @@ ...@@ -6,10 +6,11 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "router.h"
#include <algorithm> #include <algorithm>
using namespace Net; #include <pistache/router.h>
using namespace Pistache;
bool match(const Rest::Route& route, const std::string& req) { bool match(const Rest::Route& route, const std::string& req) {
return std::get<0>(route.match(req)); return std::get<0>(route.match(req));
...@@ -75,8 +76,8 @@ bool matchSplat( ...@@ -75,8 +76,8 @@ bool matchSplat(
Rest::Route Rest::Route
makeRoute(std::string value) { makeRoute(std::string value) {
auto noop = [](const Net::Http::Request&, Net::Http::Response) { return Rest::Route::Result::Ok; }; auto noop = [](const Http::Request&, Http::Response) { return Rest::Route::Result::Ok; };
return Rest::Route(value, Net::Http::Method::Get, noop); return Rest::Route(value, Http::Method::Get, noop);
} }
TEST(router_test, test_fixed_routes) { TEST(router_test, test_fixed_routes) {
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "typeid.h" #include <pistache/typeid.h>
using namespace Pistache;
TEST(type_id_test, basic_test) { TEST(type_id_test, basic_test) {
ASSERT_EQ(TypeId::of<int>(), TypeId::of<int>()); ASSERT_EQ(TypeId::of<int>(), TypeId::of<int>());
......
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include "view.h" #include <pistache/view.h>
using namespace Pistache;
template<typename T> template<typename T>
std::vector<T> make_vec(std::initializer_list<T> list) { std::vector<T> make_vec(std::initializer_list<T> list) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment