Skip to content

Commit 0c34c8e

Browse files
committed
Codebase modernization
1 parent ad9d328 commit 0c34c8e

7 files changed

Lines changed: 20 additions & 31 deletions

File tree

src/ByteBuffer.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,6 @@ ByteBuffer::ByteBuffer(const byte* arr, unsigned int size) {
4848
}
4949
}
5050

51-
/**
52-
* ByteBuffer Deconstructor
53-
*
54-
*/
55-
ByteBuffer::~ByteBuffer() {
56-
}
57-
5851
/**
5952
* Bytes Remaining
6053
* Returns the number of bytes from the current read position till the end of the buffer

src/ByteBuffer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919
#ifndef _BYTEBUFFER_H_
2020
#define _BYTEBUFFER_H_
2121

22-
// Default number of bytes to allocate in the backing buffer if no size is provided
23-
constexpr unsigned int DEFAULT_SIZE = 4096;
24-
2522
// If defined, utility functions within the class are enabled
2623
#define BB_UTILITY
2724

@@ -36,6 +33,9 @@ constexpr unsigned int DEFAULT_SIZE = 4096;
3633

3734
using byte = unsigned char;
3835

36+
// Default number of bytes to allocate in the backing buffer if no size is provided
37+
constexpr unsigned int DEFAULT_SIZE = 4096;
38+
3939
class ByteBuffer {
4040
private:
4141
unsigned int rpos = 0;
@@ -80,7 +80,7 @@ class ByteBuffer {
8080
public:
8181
explicit ByteBuffer(unsigned int size = DEFAULT_SIZE);
8282
explicit ByteBuffer(const byte* arr, unsigned int size);
83-
virtual ~ByteBuffer();
83+
virtual ~ByteBuffer() = default;
8484

8585
unsigned int bytesRemaining() const; // Number of bytes from the current read position till the end of the buffer
8686
void clear(); // Clear our the vector and reset read and write positions

src/HTTPServer.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ void HTTPServer::stop() {
145145

146146
if (listenSocket != INVALID_SOCKET) {
147147
// Close all open connections and delete Client's from memory
148-
for (auto& x : clientMap)
149-
disconnectClient(x.second, false);
148+
for (auto& [clfd, cl] : clientMap)
149+
disconnectClient(cl, false);
150150

151151
// Clear the map
152152
clientMap.clear();
@@ -519,8 +519,7 @@ void HTTPServer::handleGet(Client* cl, HTTPRequest* req) {
519519
dc = true;
520520

521521
// If Connection: close is specified, the connection should be terminated after the request is serviced
522-
std::string connection_val = req->getHeaderValue("Connection");
523-
if (connection_val.compare("close") == 0)
522+
if (auto con_val = req->getHeaderValue("Connection"); con_val.compare("close") == 0)
524523
dc = true;
525524

526525
sendResponse(cl, resp, dc);

src/Resource.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,15 @@ class Resource {
7272
// Get the file name
7373
std::string getName() const {
7474
std::string name = "";
75-
size_t slash_pos = location.find_last_of("/");
76-
if (slash_pos != std::string::npos)
75+
if (auto slash_pos = location.find_last_of("/"); slash_pos != std::string::npos)
7776
name = location.substr(slash_pos + 1);
7877
return name;
7978
}
8079

8180
// Get the file extension
8281
std::string getExtension() const {
8382
std::string ext = "";
84-
size_t ext_pos = location.find_last_of(".");
85-
if (ext_pos != std::string::npos)
83+
if (auto ext_pos = location.find_last_of("."); ext_pos != std::string::npos)
8684
ext = location.substr(ext_pos + 1);
8785
return ext;
8886
}

src/ResourceHost.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ ResourceHost::ResourceHost(std::string const& base) : baseDiskPath(base) {
2222
// TODO: Check to see if the baseDiskPath is a valid path
2323
}
2424

25-
ResourceHost::~ResourceHost() {
26-
}
27-
2825
/**
2926
* Looks up a MIME type in the dictionary
3027
*
@@ -89,10 +86,11 @@ Resource* ResourceHost::readFile(std::string const& path, struct stat const& sb)
8986
}
9087

9188
std::string mimetype = lookupMimeType(res->getExtension());
92-
if (mimetype.length() != 0)
89+
if (mimetype.length() != 0) {
9390
res->setMimeType(mimetype);
94-
else
91+
} else {
9592
res->setMimeType("application/octet-stream"); // default to binary
93+
}
9694

9795
res->setData(fdata, len);
9896

src/ResourceHost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class ResourceHost {
6464

6565
public:
6666
explicit ResourceHost(std::string const& base);
67-
~ResourceHost();
67+
~ResourceHost() = default;
6868

6969
// Write a resource to the file system
7070
void putResource(Resource* res, bool writeToDisk);

src/main.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "HTTPServer.h"
2626
#include "ResourceHost.h"
2727

28-
static HTTPServer* svr;
28+
static std::unique_ptr<HTTPServer> svr;
2929

3030
// Ignore signals with this function
3131
void handleSigPipe([[maybe_unused]] int snum) {
@@ -42,9 +42,12 @@ int main()
4242
// Parse config file
4343
std::map<std::string, std::string> config;
4444
std::fstream cfile;
45-
std::string line, key, val;
45+
std::string line;
46+
std::string key;
47+
std::string val;
4648
int epos = 0;
47-
int drop_uid = 0, drop_gid = 0;
49+
int drop_uid = 0;
50+
int drop_gid = 0;
4851
cfile.open("server.config");
4952
if (!cfile.is_open()) {
5053
std::cout << "Unable to open server.config file in working directory" << std::endl;
@@ -101,10 +104,9 @@ int main()
101104
signal(SIGTERM, &handleTermSig);
102105

103106
// Instantiate and start the server
104-
svr = new HTTPServer(vhosts, atoi(config["port"].c_str()), config["diskpath"], drop_uid, drop_gid);
107+
svr = std::make_unique<HTTPServer>(vhosts, atoi(config["port"].c_str()), config["diskpath"], drop_uid, drop_gid);
105108
if (!svr->start()) {
106109
svr->stop();
107-
delete svr;
108110
return -1;
109111
}
110112

@@ -113,7 +115,6 @@ int main()
113115

114116
// Stop the server
115117
svr->stop();
116-
delete svr;
117118

118119
return 0;
119120
}

0 commit comments

Comments
 (0)