Skip to content

Commit

Permalink
Some optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Fial committed Mar 5, 2019
1 parent 0466d6e commit 623a4a6
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 21 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
[![Build Status](https://travis-ci.org/patrickjane/libcex.svg?branch=master)](https://travis-ci.org/patrickjane/libcex)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/8b21dce6d8e846eb9ad5b0dffe1eba10)](https://www.codacy.com/app/patrickjane/libcex?utm_source=github.com&utm_medium=referral&utm_content=patrickjane/libcex&utm_campaign=Badge_Grade)

# libcex
## Overview
Expand Down
6 changes: 3 additions & 3 deletions include/cex/core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class Request
/*! \brief Constructs a new Response object
\param req The underlying `libevhtp` request object
*/
Request(evhtp_request* req);
explicit Request(evhtp_request* req);

// base request info

Expand Down Expand Up @@ -300,7 +300,7 @@ class Response
/*! \brief Constructs a new `Response` object
\param req The underlying `libevhtp` request object
*/
Response(evhtp_request* req);
explicit Response(evhtp_request* req);

/*! \brief Sets a HTTP header to a given value
\param name Name of the HTTP header
Expand Down Expand Up @@ -486,7 +486,7 @@ class Server
/*! \brief Constructs a new server with the given config.
\param config The Config object which defines the server options/configuration
*/
Server(Config& config);
explicit Server(Config& config);
virtual ~Server();

// server
Expand Down
2 changes: 1 addition & 1 deletion include/cex/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ namespace cex
struct FilesystemOptions
{
/*! \brief Constructs a new options object with defaultEncoding `utf-8` and empty rootPath*/
FilesystemOptions() { defaultEncoding= "utf-8"; }
FilesystemOptions() : defaultEncoding("utf-8") {}

std::string rootPath; /*!< \brief Specifies the root-path on the local filesystem
Expand Down
10 changes: 5 additions & 5 deletions include/cex/plist.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ class Property
public:

/*! \brief Constructs a new property with a string value */
Property(std::string value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
explicit Property(const std::string& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
/*! \brief Constructs a new property with a string value (moving the value) */
Property(std::string&& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
explicit Property(std::string&& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
/*! \brief Constructs a new property with a long value */
Property(long value) : longValue(value), doubleValue(0), ptrValue(0) {}
explicit Property(long value) : longValue(value), doubleValue(0), ptrValue(0) {}
/*! \brief Constructs a new property with a double value */
Property(double value) : longValue(0), doubleValue(value), ptrValue(0) {}
explicit Property(double value) : longValue(0), doubleValue(value), ptrValue(0) {}
/*! \brief Constructs a new property with a void* value */
Property(void* value) : longValue(0), doubleValue(0), ptrValue(value) {}
explicit Property(void* value) : longValue(0), doubleValue(0), ptrValue(value) {}

/*! \brief Retrieves the string value of the property. If no string value was set, returns an empty string object */
std::string& getStringValue() { return stringValue; }
Expand Down
2 changes: 1 addition & 1 deletion include/cex/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Set-Cookie: sessionID=D7D1AB0E9B41E9291933C28DB7110A8B6C01B47D13EF82D712676E12AF

struct SessionOptions
{
SessionOptions() { name= "sessionId"; secure= false; httpOnly= true; secure= sameSiteStrict= sameSiteLax= false; expires= 0; maxAge= 0; }
SessionOptions() : name("sessionId"), secure(false), httpOnly(true), sameSiteStrict(false), sameSiteLax(false), expires(0), maxAge(0) {}

/*! \brief Sets the expires cookie option. Must be a relative time offset in seconds. */
time_t expires;
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace cex

static struct FilesystemOptions defaultOptions;

MiddlewareFunction filesystem(std::string aPath)
MiddlewareFunction filesystem(const std::string& aPath)
{
std::shared_ptr<FilesystemOptions> opts(new FilesystemOptions());
opts.get()->rootPath= aPath;
Expand Down
7 changes: 4 additions & 3 deletions src/response.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,6 @@ int Response::end(int status)

int Response::stream(int status, std::istream* stream)
{
size_t bytesRead= -1;
char ioBuffer[IO_BUFFER_SIZE];
evbuffer* sendBuffer;

if (!stream || !stream->good())
Expand Down Expand Up @@ -143,14 +141,17 @@ int Response::stream(int status, std::istream* stream)
else
#endif
{
size_t bytesRead= -1;
char ioBuffer[IO_BUFFER_SIZE];

evhtp_send_reply_chunk_start(req, EVHTP_RES_OK);

while (!stream->eof() && stream->good())
{
stream->read(ioBuffer, IO_BUFFER_SIZE);
bytesRead= stream->gcount();

if (bytesRead <= 0)
if (bytesRead == 0)
break;

evbuffer_add(sendBuffer, ioBuffer, bytesRead);
Expand Down
4 changes: 2 additions & 2 deletions src/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ evhtp_res Server::handleBody(evhtp_request_t* req, struct evbuffer* buf, void* a
{
if (!((*it).get()->match(ctx->req.get())))
{
it++;
++it;
continue;
}

Expand Down Expand Up @@ -607,7 +607,7 @@ void Server::handleRequest(evhtp_request* req, void* arg)

next = [&next, &ctx, &it]()
{
it++;
++it;

if (it != ctx->serv->middleWares.end())
{
Expand Down
6 changes: 3 additions & 3 deletions src/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ MiddlewareFunction sessionHandler(std::shared_ptr<SessionOptions> opts)

std::vector<std::string> splitted(splitString(cookie, ';'));

for (std::vector<std::string>::iterator it = splitted.begin(); it != splitted.end(); it++)
for (std::vector<std::string>::iterator it = splitted.begin(); it != splitted.end(); ++it)
{
std::string cookieName, cookieValue;
std::vector<std::string> cookieContents(splitString((*it).c_str(), '='));
Expand All @@ -55,7 +55,7 @@ MiddlewareFunction sessionHandler(std::shared_ptr<SessionOptions> opts)
if (cookieContents.size() && cookieIt != cookieContents.end())
{
cookieName= *cookieIt;
cookieIt++;
++cookieIt;

if (cookieIt != cookieContents.end())
{
Expand Down Expand Up @@ -89,12 +89,12 @@ MiddlewareFunction sessionHandler(std::shared_ptr<SessionOptions> opts)
{
time_t expDate = time(0) + theOpts->expires;
struct tm* timeinfo;
char buffer[80];

timeinfo= localtime(&expDate);

if (timeinfo)
{
char buffer[80];
strftime(buffer, 80 , "%a, %d %h %Y %T %Z", timeinfo);

setCookie += "; Expires=";
Expand Down
7 changes: 5 additions & 2 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,19 @@ int compress(const char* src, size_t srcLen, struct evbuffer* dest, CompressionM
size_t bytesRead= 0;
char out[IO_BUFFER_SIZE];

int windowBits = 15;
int GZIP_ENCODING = 16;
z_stream strm;

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;

if (compMode == cmGZip)
{
int windowBits = 15;
int GZIP_ENCODING = 16;

res = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
}
else
res = deflateInit(&strm, Z_DEFAULT_COMPRESSION);

Expand Down

0 comments on commit 623a4a6

Please sign in to comment.