@@ -201,7 +201,6 @@ void HTTPServer::updateEvent(int ident, short filter, u_short flags, u_int fflag
201201 */
202202void 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
0 commit comments