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 {
class CookieJar {
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> {
iterator(const Storage::const_iterator& _iterator)
......@@ -59,7 +59,7 @@ public:
iterator operator++(int) {
iterator ret(it_);
it_++;
++it_;
return ret;
}
......
......@@ -18,7 +18,7 @@
namespace Pistache {
typedef int Fd;
using Fd = int;
uint hardware_concurrency();
bool make_non_blocking(int fd);
......@@ -87,8 +87,10 @@ inline constexpr bool operator==(Tag lhs, Tag rhs) {
}
struct Event {
explicit Event(Tag _tag) :
tag(_tag)
explicit Event(Tag _tag)
: flags()
, fd(-1)
, tag(_tag)
{ }
Flags<NotifyOn> flags;
......@@ -111,8 +113,8 @@ public:
std::chrono::milliseconds timeout = std::chrono::milliseconds(0)) const;
private:
int toEpollEvents(Flags<NotifyOn> interest) const;
Flags<NotifyOn> toNotifyOn(int events) const;
static int toEpollEvents(const Flags<NotifyOn>& interest);
static Flags<NotifyOn> toNotifyOn(int events);
int epoll_fd;
};
......
......@@ -505,9 +505,9 @@ namespace Uri {
for(const auto &e : params) {
query_url += "&" + e.first + "=" + e.second;
}
if(not query_url.empty()) {
if(!query_url.empty()) {
query_url[0] = '?'; // replace first `&` with `?`
} else {/* query_url is empty */}
}
return query_url;
}
......
......@@ -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;
flags |= O_NONBLOCK;
int ret = fcntl (sfd, F_SETFL, flags);
int ret = fcntl (fd, F_SETFL, flags);
if (ret == -1) return false;
return true;
......@@ -206,7 +206,7 @@ namespace Polling {
}
int
Epoll::toEpollEvents(Flags<NotifyOn> interest) const {
Epoll::toEpollEvents(const Flags<NotifyOn>& interest) {
int events = 0;
if (interest.hasFlag(NotifyOn::Read))
......@@ -222,7 +222,7 @@ namespace Polling {
}
Flags<NotifyOn>
Epoll::toNotifyOn(int events) const {
Epoll::toNotifyOn(int events) {
Flags<NotifyOn> flags;
if (events & EPOLLIN)
......
......@@ -3,30 +3,27 @@
*/
#include <iostream>
#include <cassert>
#include <cstring>
#include <pistache/listener.h>
#include <pistache/peer.h>
#include <pistache/common.h>
#include <pistache/os.h>
#include <pistache/transport.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <pthread.h>
#include <signal.h>
#include <sys/timerfd.h>
#include <sys/sendfile.h>
#include <cerrno>
#include <pistache/listener.h>
#include <pistache/peer.h>
#include <pistache/common.h>
#include <pistache/os.h>
#include <pistache/transport.h>
#include <chrono>
#include <memory>
#include <vector>
#include <cerrno>
#include <signal.h>
using namespace std;
namespace Pistache {
namespace Tcp {
......@@ -46,9 +43,6 @@ namespace {
}
}
using Polling::NotifyOn;
void setSocketOptions(Fd fd, Flags<Options> options) {
if (options.hasFlag(Options::ReuseAddr)) {
int one = 1;
......@@ -324,7 +318,7 @@ Listener::handleNewConnection() {
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);
dispatchPeer(peer);
......
......@@ -16,6 +16,7 @@ pistache_test(router_test)
pistache_test(cookie_test)
pistache_test(view_test)
pistache_test(http_parsing_test)
pistache_test(http_uri_test)
pistache_test(http_client_test)
pistache_test(net_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:
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) {
writer.send(Http::Code::Ok, "1");
}, 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