Skip to content

Commit 0080b8a

Browse files
Merge pull request #3 from AP-ECE-UT/refactor-2
Refactor and Add HTTP Methods and Response Status
2 parents 80f1b4c + b0f0f48 commit 0080b8a

21 files changed

Lines changed: 520 additions & 500 deletions

examples/handlers.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,59 @@
11
#include "handlers.hpp"
22

3-
using namespace std;
3+
#include <cstdlib>
4+
#include <iostream>
45

56
Response* RandomNumberHandler::callback(Request* req) {
6-
Response* res = new Response;
7+
Response* res = new Response();
78
res->setHeader("Content-Type", "text/html");
8-
string body;
9+
10+
std::string randomNumber = std::to_string(std::rand() % 10 + 1);
11+
std::string body;
12+
913
body += "<!DOCTYPE html>";
10-
body += "<html>";
14+
body += "<html lang=\"en\">";
15+
16+
body += "<head>";
17+
body += " <title>Random Number Page</title>";
18+
body += "</head>";
19+
1120
body += "<body style=\"text-align: center;\">";
12-
body += "<h1>AP HTTP</h1>";
13-
body += "<p>";
14-
body += "a random number in [1, 10] is: ";
15-
body += to_string(rand() % 10 + 1);
16-
body += "</p>";
17-
body += "<p>";
18-
body += "SessionId: ";
19-
body += req->getSessionId();
20-
body += "</p>";
21+
body += " <h1>AP HTTP</h1>";
22+
body += " <p>A random number in [1, 10] is: " + randomNumber + "</p>";
23+
body += " <p>SessionId: " + req->getSessionId() + "</p>";
2124
body += "</body>";
25+
2226
body += "</html>";
2327
res->setBody(body);
2428
return res;
2529
}
2630

2731
Response* LoginHandler::callback(Request* req) {
28-
string username = req->getBodyParam("username");
29-
string password = req->getBodyParam("password");
30-
if (username == "root")
32+
std::string username = req->getBodyParam("username");
33+
std::string password = req->getBodyParam("password");
34+
if (username == "root") {
3135
throw Server::Exception("Remote root access has been disabled.");
32-
cout << "username: " << username << ",\tpassword: " << password << endl;
36+
}
37+
std::cout << "username: " << username << ",\tpassword: " << password << std::endl;
3338
Response* res = Response::redirect("/rand");
3439
res->setSessionId("SID");
3540
return res;
3641
}
3742

3843
Response* UploadHandler::callback(Request* req) {
39-
string name = req->getBodyParam("file_name");
40-
string file = req->getBodyParam("file");
44+
std::string name = req->getBodyParam("file_name");
45+
std::string file = req->getBodyParam("file");
4146
utils::writeToFile(file, name);
4247
Response* res = Response::redirect("/");
4348
return res;
4449
}
4550

46-
ColorHandler::ColorHandler(string filePath) : TemplateHandler(filePath) {}
51+
ColorHandler::ColorHandler(const std::string& filePath)
52+
: TemplateHandler(filePath) {}
4753

48-
map<string, string> ColorHandler::handle(Request* req) {
49-
map<string, string> context;
50-
string newName = "I am " + req->getQueryParam("name");
54+
std::map<std::string, std::string> ColorHandler::handle(Request* req) {
55+
std::string newName = "I am " + req->getQueryParam("name");
56+
std::map<std::string, std::string> context;
5157
context["name"] = newName;
5258
context["color"] = req->getQueryParam("color");
5359
return context;

examples/handlers.hpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
#ifndef HANDLERS_HPP_INCLUDE
22
#define HANDLERS_HPP_INCLUDE
33

4-
#include <cstdlib> // for rand and srand
5-
#include <ctime> // for time
6-
#include <iostream>
4+
#include <map>
5+
#include <string>
76

87
#include "../server/server.hpp"
98

109
class RandomNumberHandler : public RequestHandler {
1110
public:
12-
Response* callback(Request*);
11+
Response* callback(Request*) override;
1312
};
1413

1514
class LoginHandler : public RequestHandler {
1615
public:
17-
Response* callback(Request*);
16+
Response* callback(Request*) override;
1817
};
1918

2019
class UploadHandler : public RequestHandler {
2120
public:
22-
Response* callback(Request*);
21+
Response* callback(Request*) override;
2322
};
2423

2524
class ColorHandler : public TemplateHandler {
2625
public:
27-
ColorHandler(std::string filePath);
28-
std::map<std::string, std::string> handle(Request* req);
26+
ColorHandler(const std::string& filePath);
27+
std::map<std::string, std::string> handle(Request* req) override;
2928
};
3029

3130
#endif // HANDLERS_HPP_INCLUDE

examples/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ void mapServerPaths(Server& server) {
88
server.setNotFoundErrPage("static/404.html");
99
server.get("/", new ShowPage("static/home.html"));
1010
server.get("/home.png", new ShowImage("static/home.png"));
11+
server.get("/rand", new RandomNumberHandler());
1112
server.get("/login", new ShowPage("static/logincss.html"));
1213
server.post("/login", new LoginHandler());
1314
server.get("/up", new ShowPage("static/upload_form.html"));
1415
server.post("/up", new UploadHandler());
1516
server.get("/colors", new ColorHandler("template/colors.html"));
16-
server.get("/rand", new RandomNumberHandler());
1717
server.get("/music", new ShowPage("static/music.html"));
1818
server.get("/music/moonlight.mp3", new ShowFile("static/moonlight.mp3", "audio/mpeg"));
1919
}

server/route.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22

33
#include "server.hpp"
44

5-
using namespace std;
5+
Route::Route(Request::Method method, const std::string& path)
6+
: method_(method),
7+
path_(path),
8+
handler_(nullptr) {}
69

7-
Route::Route(Method _method, string _path) {
8-
method = _method;
9-
path = _path;
10+
Route::~Route() {
11+
delete handler_;
1012
}
1113

12-
void Route::setHandler(RequestHandler* _handler) { handler = _handler; }
13-
14-
bool Route::isMatch(Method _method, string url) {
15-
return (url == path) && (_method == method);
14+
void Route::setHandler(RequestHandler* handler) {
15+
handler_ = handler;
1616
}
1717

18-
Response* Route::handle(Request* req) { return handler->callback(req); }
18+
Response* Route::handle(Request* req) {
19+
return handler_->callback(req);
20+
}
1921

20-
Route::~Route() { delete handler; }
22+
bool Route::isMatch(Request::Method method, const std::string& url) {
23+
return (method_ == method) && (url == path_);
24+
}

server/route.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@
33

44
#include <string>
55

6-
#include "../utils/include.hpp"
76
#include "../utils/request.hpp"
8-
#include "../utils/response.hpp"
97

8+
class Response;
109
class RequestHandler;
1110

1211
class Route {
13-
private:
14-
Method method;
15-
std::string path;
16-
RequestHandler* handler;
17-
1812
public:
19-
Route(Method _method, std::string _path);
13+
Route(Request::Method method, const std::string& path);
2014
~Route();
21-
bool isMatch(Method, std::string url);
15+
16+
void setHandler(RequestHandler* handler);
2217
Response* handle(Request* req);
23-
void setHandler(RequestHandler* _handler);
18+
bool isMatch(Request::Method, const std::string& url);
19+
20+
private:
21+
Request::Method method_;
22+
std::string path_;
23+
RequestHandler* handler_;
2424
};
2525

2626
#endif // ROUTE_HPP_INCLUDE

0 commit comments

Comments
 (0)