Skip to content

Commit 623a4a6

Browse files
author
Patrick Fial
committed
Some optimizations
1 parent 0466d6e commit 623a4a6

10 files changed

Lines changed: 26 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
22
[![Build Status](https://travis-ci.org/patrickjane/libcex.svg?branch=master)](https://travis-ci.org/patrickjane/libcex)
3+
[![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)
34

45
# libcex
56
## Overview

include/cex/core.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ class Request
181181
/*! \brief Constructs a new Response object
182182
\param req The underlying `libevhtp` request object
183183
*/
184-
Request(evhtp_request* req);
184+
explicit Request(evhtp_request* req);
185185

186186
// base request info
187187

@@ -300,7 +300,7 @@ class Response
300300
/*! \brief Constructs a new `Response` object
301301
\param req The underlying `libevhtp` request object
302302
*/
303-
Response(evhtp_request* req);
303+
explicit Response(evhtp_request* req);
304304

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

492492
// server

include/cex/filesystem.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace cex
5757
struct FilesystemOptions
5858
{
5959
/*! \brief Constructs a new options object with defaultEncoding `utf-8` and empty rootPath*/
60-
FilesystemOptions() { defaultEncoding= "utf-8"; }
60+
FilesystemOptions() : defaultEncoding("utf-8") {}
6161

6262
std::string rootPath; /*!< \brief Specifies the root-path on the local filesystem
6363

include/cex/plist.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ class Property
3838
public:
3939

4040
/*! \brief Constructs a new property with a string value */
41-
Property(std::string value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
41+
explicit Property(const std::string& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
4242
/*! \brief Constructs a new property with a string value (moving the value) */
43-
Property(std::string&& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
43+
explicit Property(std::string&& value) : stringValue(value), longValue(0), doubleValue(0), ptrValue(0) {}
4444
/*! \brief Constructs a new property with a long value */
45-
Property(long value) : longValue(value), doubleValue(0), ptrValue(0) {}
45+
explicit Property(long value) : longValue(value), doubleValue(0), ptrValue(0) {}
4646
/*! \brief Constructs a new property with a double value */
47-
Property(double value) : longValue(0), doubleValue(value), ptrValue(0) {}
47+
explicit Property(double value) : longValue(0), doubleValue(value), ptrValue(0) {}
4848
/*! \brief Constructs a new property with a void* value */
49-
Property(void* value) : longValue(0), doubleValue(0), ptrValue(value) {}
49+
explicit Property(void* value) : longValue(0), doubleValue(0), ptrValue(value) {}
5050

5151
/*! \brief Retrieves the string value of the property. If no string value was set, returns an empty string object */
5252
std::string& getStringValue() { return stringValue; }

include/cex/session.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Set-Cookie: sessionID=D7D1AB0E9B41E9291933C28DB7110A8B6C01B47D13EF82D712676E12AF
6363

6464
struct SessionOptions
6565
{
66-
SessionOptions() { name= "sessionId"; secure= false; httpOnly= true; secure= sameSiteStrict= sameSiteLax= false; expires= 0; maxAge= 0; }
66+
SessionOptions() : name("sessionId"), secure(false), httpOnly(true), sameSiteStrict(false), sameSiteLax(false), expires(0), maxAge(0) {}
6767

6868
/*! \brief Sets the expires cookie option. Must be a relative time offset in seconds. */
6969
time_t expires;

src/filesystem.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace cex
2626

2727
static struct FilesystemOptions defaultOptions;
2828

29-
MiddlewareFunction filesystem(std::string aPath)
29+
MiddlewareFunction filesystem(const std::string& aPath)
3030
{
3131
std::shared_ptr<FilesystemOptions> opts(new FilesystemOptions());
3232
opts.get()->rootPath= aPath;

src/response.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,6 @@ int Response::end(int status)
109109

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

116114
if (!stream || !stream->good())
@@ -143,14 +141,17 @@ int Response::stream(int status, std::istream* stream)
143141
else
144142
#endif
145143
{
144+
size_t bytesRead= -1;
145+
char ioBuffer[IO_BUFFER_SIZE];
146+
146147
evhtp_send_reply_chunk_start(req, EVHTP_RES_OK);
147148

148149
while (!stream->eof() && stream->good())
149150
{
150151
stream->read(ioBuffer, IO_BUFFER_SIZE);
151152
bytesRead= stream->gcount();
152153

153-
if (bytesRead <= 0)
154+
if (bytesRead == 0)
154155
break;
155156

156157
evbuffer_add(sendBuffer, ioBuffer, bytesRead);

src/server.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ evhtp_res Server::handleBody(evhtp_request_t* req, struct evbuffer* buf, void* a
501501
{
502502
if (!((*it).get()->match(ctx->req.get())))
503503
{
504-
it++;
504+
++it;
505505
continue;
506506
}
507507

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

608608
next = [&next, &ctx, &it]()
609609
{
610-
it++;
610+
++it;
611611

612612
if (it != ctx->serv->middleWares.end())
613613
{

src/session.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ MiddlewareFunction sessionHandler(std::shared_ptr<SessionOptions> opts)
4444

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

47-
for (std::vector<std::string>::iterator it = splitted.begin(); it != splitted.end(); it++)
47+
for (std::vector<std::string>::iterator it = splitted.begin(); it != splitted.end(); ++it)
4848
{
4949
std::string cookieName, cookieValue;
5050
std::vector<std::string> cookieContents(splitString((*it).c_str(), '='));
@@ -55,7 +55,7 @@ MiddlewareFunction sessionHandler(std::shared_ptr<SessionOptions> opts)
5555
if (cookieContents.size() && cookieIt != cookieContents.end())
5656
{
5757
cookieName= *cookieIt;
58-
cookieIt++;
58+
++cookieIt;
5959

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

9493
timeinfo= localtime(&expDate);
9594

9695
if (timeinfo)
9796
{
97+
char buffer[80];
9898
strftime(buffer, 80 , "%a, %d %h %Y %T %Z", timeinfo);
9999

100100
setCookie += "; Expires=";

src/util.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,19 @@ int compress(const char* src, size_t srcLen, struct evbuffer* dest, CompressionM
112112
size_t bytesRead= 0;
113113
char out[IO_BUFFER_SIZE];
114114

115-
int windowBits = 15;
116-
int GZIP_ENCODING = 16;
117115
z_stream strm;
118116

119117
strm.zalloc = Z_NULL;
120118
strm.zfree = Z_NULL;
121119
strm.opaque = Z_NULL;
122120

123121
if (compMode == cmGZip)
122+
{
123+
int windowBits = 15;
124+
int GZIP_ENCODING = 16;
125+
124126
res = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, windowBits | GZIP_ENCODING, 8, Z_DEFAULT_STRATEGY);
127+
}
125128
else
126129
res = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
127130

0 commit comments

Comments
 (0)