Unverified Commit 61af27cb authored by Dennis Jenkins's avatar Dennis Jenkins Committed by GitHub

Merge pull request #311 from knowledge4igor/code_improvements_4

Several code improvements
parents c5fd213e e8859f80
...@@ -41,7 +41,7 @@ struct Cookie { ...@@ -41,7 +41,7 @@ struct Cookie {
class CookieJar { class CookieJar {
public: public:
typedef std::unordered_map<std::string, Cookie> Storage; using Storage = std::unordered_map<std::string, Cookie>;
struct iterator : std::iterator<std::bidirectional_iterator_tag, Cookie> { struct iterator : std::iterator<std::bidirectional_iterator_tag, Cookie> {
iterator(const Storage::const_iterator& _iterator) iterator(const Storage::const_iterator& _iterator)
...@@ -59,7 +59,7 @@ public: ...@@ -59,7 +59,7 @@ public:
iterator operator++(int) { iterator operator++(int) {
iterator ret(it_); iterator ret(it_);
it_++; ++it_;
return ret; return ret;
} }
......
...@@ -18,7 +18,7 @@ ...@@ -18,7 +18,7 @@
namespace Pistache { namespace Pistache {
typedef int Fd; using Fd = int;
uint hardware_concurrency(); uint hardware_concurrency();
bool make_non_blocking(int fd); bool make_non_blocking(int fd);
...@@ -87,8 +87,10 @@ inline constexpr bool operator==(Tag lhs, Tag rhs) { ...@@ -87,8 +87,10 @@ inline constexpr bool operator==(Tag lhs, Tag rhs) {
} }
struct Event { struct Event {
explicit Event(Tag _tag) : explicit Event(Tag _tag)
tag(_tag) : flags()
, fd(-1)
, tag(_tag)
{ } { }
Flags<NotifyOn> flags; Flags<NotifyOn> flags;
...@@ -111,8 +113,8 @@ public: ...@@ -111,8 +113,8 @@ public:
std::chrono::milliseconds timeout = std::chrono::milliseconds(0)) const; std::chrono::milliseconds timeout = std::chrono::milliseconds(0)) const;
private: private:
int toEpollEvents(Flags<NotifyOn> interest) const; static int toEpollEvents(const Flags<NotifyOn>& interest);
Flags<NotifyOn> toNotifyOn(int events) const; static Flags<NotifyOn> toNotifyOn(int events);
int epoll_fd; int epoll_fd;
}; };
......
...@@ -505,9 +505,9 @@ namespace Uri { ...@@ -505,9 +505,9 @@ namespace Uri {
for(const auto &e : params) { for(const auto &e : params) {
query_url += "&" + e.first + "=" + e.second; query_url += "&" + e.first + "=" + e.second;
} }
if(not query_url.empty()) { if(!query_url.empty()) {
query_url[0] = '?'; // replace first `&` with `?` query_url[0] = '?'; // replace first `&` with `?`
} else {/* query_url is empty */} }
return query_url; return query_url;
} }
......
...@@ -25,13 +25,13 @@ uint hardware_concurrency() { ...@@ -25,13 +25,13 @@ uint hardware_concurrency() {
} }
bool make_non_blocking(int sfd) bool make_non_blocking(int fd)
{ {
int flags = fcntl (sfd, F_GETFL, 0); int flags = fcntl (fd, F_GETFL, 0);
if (flags == -1) return false; if (flags == -1) return false;
flags |= O_NONBLOCK; flags |= O_NONBLOCK;
int ret = fcntl (sfd, F_SETFL, flags); int ret = fcntl (fd, F_SETFL, flags);
if (ret == -1) return false; if (ret == -1) return false;
return true; return true;
...@@ -206,7 +206,7 @@ namespace Polling { ...@@ -206,7 +206,7 @@ namespace Polling {
} }
int int
Epoll::toEpollEvents(Flags<NotifyOn> interest) const { Epoll::toEpollEvents(const Flags<NotifyOn>& interest) {
int events = 0; int events = 0;
if (interest.hasFlag(NotifyOn::Read)) if (interest.hasFlag(NotifyOn::Read))
...@@ -222,7 +222,7 @@ namespace Polling { ...@@ -222,7 +222,7 @@ namespace Polling {
} }
Flags<NotifyOn> Flags<NotifyOn>
Epoll::toNotifyOn(int events) const { Epoll::toNotifyOn(int events) {
Flags<NotifyOn> flags; Flags<NotifyOn> flags;
if (events & EPOLLIN) if (events & EPOLLIN)
......
...@@ -3,30 +3,27 @@ ...@@ -3,30 +3,27 @@
*/ */
#include <iostream> #include <pistache/listener.h>
#include <cassert> #include <pistache/peer.h>
#include <cstring> #include <pistache/common.h>
#include <pistache/os.h>
#include <pistache/transport.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <netinet/tcp.h> #include <netinet/tcp.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <sys/epoll.h> #include <sys/epoll.h>
#include <pthread.h>
#include <signal.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <sys/sendfile.h>
#include <cerrno>
#include <pistache/listener.h> #include <chrono>
#include <pistache/peer.h> #include <memory>
#include <pistache/common.h> #include <vector>
#include <pistache/os.h>
#include <pistache/transport.h> #include <cerrno>
#include <signal.h>
using namespace std;
namespace Pistache { namespace Pistache {
namespace Tcp { namespace Tcp {
...@@ -46,9 +43,6 @@ namespace { ...@@ -46,9 +43,6 @@ namespace {
} }
} }
using Polling::NotifyOn;
void setSocketOptions(Fd fd, Flags<Options> options) { void setSocketOptions(Fd fd, Flags<Options> options) {
if (options.hasFlag(Options::ReuseAddr)) { if (options.hasFlag(Options::ReuseAddr)) {
int one = 1; int one = 1;
...@@ -324,7 +318,7 @@ Listener::handleNewConnection() { ...@@ -324,7 +318,7 @@ Listener::handleNewConnection() {
make_non_blocking(client_fd); make_non_blocking(client_fd);
auto peer = make_shared<Peer>(Address::fromUnix((struct sockaddr *)&peer_addr)); auto peer = std::make_shared<Peer>(Address::fromUnix((struct sockaddr *)&peer_addr));
peer->associateFd(client_fd); peer->associateFd(client_fd);
dispatchPeer(peer); dispatchPeer(peer);
......
...@@ -16,6 +16,7 @@ pistache_test(router_test) ...@@ -16,6 +16,7 @@ pistache_test(router_test)
pistache_test(cookie_test) pistache_test(cookie_test)
pistache_test(view_test) pistache_test(view_test)
pistache_test(http_parsing_test) pistache_test(http_parsing_test)
pistache_test(http_uri_test)
pistache_test(http_client_test) pistache_test(http_client_test)
pistache_test(net_test) pistache_test(net_test)
pistache_test(listener_test) pistache_test(listener_test)
......
#include "gtest/gtest.h"
#include <pistache/http.h>
using namespace Pistache;
TEST(http_uri_test, query_as_string_test)
{
Http::Uri::Query query1;
ASSERT_TRUE(query1.as_str().empty());
Http::Uri::Query query2;
query2.add("value1", "name1");
ASSERT_STREQ(query2.as_str().c_str(), "?value1=name1");
Http::Uri::Query query3;
query3.add("value1", "name1");
query3.add("value2", "name2");
ASSERT_STREQ(query3.as_str().c_str(), "?value2=name2&value1=name1");
}
\ No newline at end of file
...@@ -44,7 +44,7 @@ private: ...@@ -44,7 +44,7 @@ private:
Routes::Get(router, "/read/function1", Routes::bind(&StatsEndpoint::doAuth, this)); Routes::Get(router, "/read/function1", Routes::bind(&StatsEndpoint::doAuth, this));
} }
void doAuth(const Rest::Request& request, Http::ResponseWriter response) { void doAuth(const Rest::Request& /*request*/, Http::ResponseWriter response) {
std::thread worker([](Http::ResponseWriter writer) { std::thread worker([](Http::ResponseWriter writer) {
writer.send(Http::Code::Ok, "1"); writer.send(Http::Code::Ok, "1");
}, std::move(response)); }, std::move(response));
......
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