|
1 | 1 | #include "handlers.hpp" |
2 | 2 |
|
3 | | -using namespace std; |
| 3 | +#include <cstdlib> |
| 4 | +#include <iostream> |
4 | 5 |
|
5 | 6 | Response* RandomNumberHandler::callback(Request* req) { |
6 | | - Response* res = new Response; |
| 7 | + Response* res = new Response(); |
7 | 8 | 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 | + |
9 | 13 | 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 | + |
11 | 20 | 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>"; |
21 | 24 | body += "</body>"; |
| 25 | + |
22 | 26 | body += "</html>"; |
23 | 27 | res->setBody(body); |
24 | 28 | return res; |
25 | 29 | } |
26 | 30 |
|
27 | 31 | 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") { |
31 | 35 | 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; |
33 | 38 | Response* res = Response::redirect("/rand"); |
34 | 39 | res->setSessionId("SID"); |
35 | 40 | return res; |
36 | 41 | } |
37 | 42 |
|
38 | 43 | 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"); |
41 | 46 | utils::writeToFile(file, name); |
42 | 47 | Response* res = Response::redirect("/"); |
43 | 48 | return res; |
44 | 49 | } |
45 | 50 |
|
46 | | -ColorHandler::ColorHandler(string filePath) : TemplateHandler(filePath) {} |
| 51 | +ColorHandler::ColorHandler(const std::string& filePath) |
| 52 | + : TemplateHandler(filePath) {} |
47 | 53 |
|
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; |
51 | 57 | context["name"] = newName; |
52 | 58 | context["color"] = req->getQueryParam("color"); |
53 | 59 | return context; |
|
0 commit comments