Skip to content

Commit dc0a2e2

Browse files
committed
Additional use of smart pointers
1 parent 821260c commit dc0a2e2

File tree

4 files changed

+27
-37
lines changed

4 files changed

+27
-37
lines changed

src/ByteBuffer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class ByteBuffer {
4545
uint32_t bytesRemaining() const; // Number of bytes from the current read position till the end of the buffer
4646
void clear(); // Clear our the vector and reset read and write positions
4747
std::unique_ptr<ByteBuffer> clone(); // Return a new instance of a ByteBuffer with the exact same contents and the same state (rpos, wpos)
48-
//ByteBuffer compact(); // TODO?
4948
bool equals(const ByteBuffer* other) const; // Compare if the contents are equivalent
5049
void resize(uint32_t newSize);
5150
uint32_t size() const; // Size of internal vector

src/HTTPServer.cpp

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ void HTTPServer::updateEvent(int ident, short filter, u_short flags, u_int fflag
201201
*/
202202
void HTTPServer::process() {
203203
int32_t nev = 0; // Number of changed events returned by kevent
204-
Client* cl = nullptr;
205204

206205
while (canRun) {
207206
// Get a list of changed socket descriptors with a read event triggered in evList
@@ -221,7 +220,7 @@ void HTTPServer::process() {
221220
}
222221

223222
// Client descriptor has triggered an event
224-
cl = getClient(evList[i].ident); // ident contains the clients socket descriptor
223+
auto cl = getClient(evList[i].ident); // ident contains the clients socket descriptor
225224
if (cl == nullptr) {
226225
std::cout << "Could not find client" << std::endl;
227226
// Remove socket events from kqueue
@@ -281,18 +280,14 @@ void HTTPServer::acceptConnection() {
281280
// Set socket as non blocking
282281
fcntl(clfd, F_SETFL, O_NONBLOCK);
283282

284-
// Instance Client object
285-
auto cl = new Client(clfd, clientAddr);
286-
287283
// Add kqueue event to track the new client socket for READ and WRITE events
288284
updateEvent(clfd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, NULL);
289285
updateEvent(clfd, EVFILT_WRITE, EV_ADD | EV_DISABLE, 0, 0, NULL); // Disabled initially
290286

291287
// Add the client object to the client map
292-
clientMap.try_emplace(clfd, cl);
293-
294-
// Print the client's IP on connect
288+
auto cl = std::make_unique<Client>(clfd, clientAddr);
295289
std::cout << "[" << cl->getClientIP() << "] connected" << std::endl;
290+
clientMap.try_emplace(clfd, std::move(cl));
296291
}
297292

298293
/**
@@ -302,7 +297,7 @@ void HTTPServer::acceptConnection() {
302297
* @param clfd Client socket descriptor
303298
* @return Pointer to Client object if found. NULL otherwise
304299
*/
305-
Client* HTTPServer::getClient(int clfd) {
300+
std::shared_ptr<Client> HTTPServer::getClient(int clfd) {
306301
auto it = clientMap.find(clfd);
307302

308303
// Client wasn't found
@@ -321,7 +316,7 @@ Client* HTTPServer::getClient(int clfd) {
321316
* @param mapErase When true, remove the client from the client map. Needed if operations on the
322317
* client map are being performed and we don't want to remove the map entry right away
323318
*/
324-
void HTTPServer::disconnectClient(Client* cl, bool mapErase) {
319+
void HTTPServer::disconnectClient(std::shared_ptr<Client> cl, bool mapErase) {
325320
if (cl == nullptr)
326321
return;
327322

@@ -337,9 +332,6 @@ void HTTPServer::disconnectClient(Client* cl, bool mapErase) {
337332
// Remove the client from the clientMap
338333
if (mapErase)
339334
clientMap.erase(cl->getSocket());
340-
341-
// Delete the client object from memory
342-
delete cl;
343335
}
344336

345337
/**
@@ -350,7 +342,7 @@ void HTTPServer::disconnectClient(Client* cl, bool mapErase) {
350342
* @param cl Pointer to Client that sent the data
351343
* @param data_len Number of bytes waiting to be read
352344
*/
353-
void HTTPServer::readClient(Client* cl, int32_t data_len) {
345+
void HTTPServer::readClient(std::shared_ptr<Client> cl, int32_t data_len) {
354346
if (cl == nullptr)
355347
return;
356348

@@ -389,7 +381,7 @@ void HTTPServer::readClient(Client* cl, int32_t data_len) {
389381
* @param cl Pointer to Client that sent the data
390382
* @param avail_bytes Number of bytes available for writing in the send buffer
391383
*/
392-
bool HTTPServer::writeClient(Client* cl, int32_t avail_bytes) {
384+
bool HTTPServer::writeClient(std::shared_ptr<Client> cl, int32_t avail_bytes) {
393385
if (cl == nullptr)
394386
return false;
395387

@@ -453,7 +445,7 @@ bool HTTPServer::writeClient(Client* cl, int32_t avail_bytes) {
453445
* @param cl Client object where request originated from
454446
* @param req HTTPRequest object filled with raw packet data
455447
*/
456-
void HTTPServer::handleRequest(Client* cl, HTTPRequest* req) {
448+
void HTTPServer::handleRequest(std::shared_ptr<Client> cl, HTTPRequest* const req) {
457449
// Parse the request
458450
// If there's an error, report it and send a server error in response
459451
if (!req->parse()) {
@@ -496,7 +488,7 @@ void HTTPServer::handleRequest(Client* cl, HTTPRequest* req) {
496488
* @param cl Client requesting the resource
497489
* @param req State of the request
498490
*/
499-
void HTTPServer::handleGet(Client* cl, HTTPRequest* req) {
491+
void HTTPServer::handleGet(std::shared_ptr<Client> cl, HTTPRequest* const req) {
500492
auto resHost = this->getResourceHostForRequest(req);
501493

502494
// ResourceHost couldnt be determined or the Host specified by the client was invalid
@@ -546,7 +538,7 @@ void HTTPServer::handleGet(Client* cl, HTTPRequest* req) {
546538
* @param cl Client requesting the resource
547539
* @param req State of the request
548540
*/
549-
void HTTPServer::handleOptions(Client* cl, [[maybe_unused]] HTTPRequest* req) {
541+
void HTTPServer::handleOptions(std::shared_ptr<Client> cl, [[maybe_unused]] HTTPRequest* const req) {
550542
// For now, we'll always return the capabilities of the server instead of figuring it out for each resource
551543
std::string allow = "HEAD, GET, OPTIONS, TRACE";
552544

@@ -566,7 +558,7 @@ void HTTPServer::handleOptions(Client* cl, [[maybe_unused]] HTTPRequest* req) {
566558
* @param cl Client requesting the resource
567559
* @param req State of the request
568560
*/
569-
void HTTPServer::handleTrace(Client* cl, HTTPRequest* req) {
561+
void HTTPServer::handleTrace(std::shared_ptr<Client> cl, HTTPRequest* const req) {
570562
// Get a byte array representation of the request
571563
uint32_t len = req->size();
572564
auto buf = std::make_unique<uint8_t[]>(len);
@@ -591,7 +583,7 @@ void HTTPServer::handleTrace(Client* cl, HTTPRequest* req) {
591583
* @param status Status code corresponding to the enum in HTTPMessage.h
592584
* @param msg An additional message to append to the body text
593585
*/
594-
void HTTPServer::sendStatusResponse(Client* cl, int32_t status, std::string const& msg) {
586+
void HTTPServer::sendStatusResponse(std::shared_ptr<Client> cl, int32_t status, std::string const& msg) {
595587
auto resp = std::make_unique<HTTPResponse>();
596588
resp->setStatus(status);
597589

@@ -620,7 +612,7 @@ void HTTPServer::sendStatusResponse(Client* cl, int32_t status, std::string cons
620612
* @param buf ByteBuffer containing data to be sent
621613
* @param disconnect Should the server disconnect the client after sending (Optional, default = false)
622614
*/
623-
void HTTPServer::sendResponse(Client* cl, std::unique_ptr<HTTPResponse> resp, bool disconnect) {
615+
void HTTPServer::sendResponse(std::shared_ptr<Client> cl, std::unique_ptr<HTTPResponse> resp, bool disconnect) {
624616
// Server Header
625617
resp->addHeader("Server", "httpserver/1.0");
626618

@@ -653,7 +645,7 @@ void HTTPServer::sendResponse(Client* cl, std::unique_ptr<HTTPResponse> resp, bo
653645
*
654646
* @param req State of the request
655647
*/
656-
std::shared_ptr<ResourceHost> HTTPServer::getResourceHostForRequest(const HTTPRequest* req) {
648+
std::shared_ptr<ResourceHost> HTTPServer::getResourceHostForRequest(const HTTPRequest* const req) {
657649
// Determine the appropriate vhost
658650
std::string host = "";
659651

src/HTTPServer.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class HTTPServer {
5252
struct kevent evList[QUEUE_SIZE]; // Events that have triggered a filter in the kqueue (max QUEUE_SIZE at a time)
5353

5454
// Client map, maps Socket descriptor to Client object
55-
std::unordered_map<int, Client*> clientMap;
55+
std::unordered_map<int, std::shared_ptr<Client>> clientMap;
5656

5757
// Resources / File System
5858
std::vector<std::shared_ptr<ResourceHost>> hostList; // Contains all ResourceHosts
@@ -61,21 +61,21 @@ class HTTPServer {
6161
// Connection processing
6262
void updateEvent(int ident, short filter, u_short flags, u_int fflags, int32_t data, void* udata);
6363
void acceptConnection();
64-
Client* getClient(int clfd);
65-
void disconnectClient(Client* cl, bool mapErase = true);
66-
void readClient(Client* cl, int32_t data_len); // Client read event
67-
bool writeClient(Client* cl, int32_t avail_bytes); // Client write event
68-
std::shared_ptr<ResourceHost> getResourceHostForRequest(const HTTPRequest* req);
64+
std::shared_ptr<Client> getClient(int clfd);
65+
void disconnectClient(std::shared_ptr<Client> cl, bool mapErase = true);
66+
void readClient(std::shared_ptr<Client> cl, int32_t data_len); // Client read event
67+
bool writeClient(std::shared_ptr<Client> cl, int32_t avail_bytes); // Client write event
68+
std::shared_ptr<ResourceHost> getResourceHostForRequest(const HTTPRequest* const req);
6969

7070
// Request handling
71-
void handleRequest(Client* cl, HTTPRequest* req);
72-
void handleGet(Client* cl, HTTPRequest* req);
73-
void handleOptions(Client* cl, HTTPRequest* req);
74-
void handleTrace(Client* cl, HTTPRequest* req);
71+
void handleRequest(std::shared_ptr<Client> cl, HTTPRequest* const req);
72+
void handleGet(std::shared_ptr<Client> cl, HTTPRequest* const req);
73+
void handleOptions(std::shared_ptr<Client> cl, HTTPRequest* const req);
74+
void handleTrace(std::shared_ptr<Client> cl, HTTPRequest* const req);
7575

7676
// Response
77-
void sendStatusResponse(Client* cl, int32_t status, std::string const& msg = "");
78-
void sendResponse(Client* cl, std::unique_ptr<HTTPResponse> resp, bool disconnect);
77+
void sendStatusResponse(std::shared_ptr<Client> cl, int32_t status, std::string const& msg = "");
78+
void sendResponse(std::shared_ptr<Client> cl, std::unique_ptr<HTTPResponse> resp, bool disconnect);
7979

8080
public:
8181
bool canRun = false;

src/ResourceHost.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ std::unique_ptr<Resource> ResourceHost::readFile(std::string const& path, struct
102102
// Close the file
103103
file.close();
104104

105-
std::string mimetype = lookupMimeType(res->getExtension());
106-
if (mimetype.length() != 0) {
105+
if (auto mimetype = lookupMimeType(res->getExtension()); mimetype.length() != 0) {
107106
res->setMimeType(mimetype);
108107
} else {
109108
res->setMimeType("application/octet-stream"); // default to binary

0 commit comments

Comments
 (0)