-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhandlers.cpp
More file actions
60 lines (49 loc) · 1.83 KB
/
Copy pathhandlers.cpp
File metadata and controls
60 lines (49 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "handlers.hpp"
#include <cstdlib>
#include <iostream>
Response* RandomNumberHandler::callback(Request* req) {
Response* res = new Response();
res->setHeader("Content-Type", "text/html");
std::string randomNumber = std::to_string(std::rand() % 10 + 1);
std::string body;
body += "<!DOCTYPE html>";
body += "<html lang=\"en\">";
body += "<head>";
body += " <title>Random Number Page</title>";
body += "</head>";
body += "<body style=\"text-align: center;\">";
body += " <h1>AP HTTP</h1>";
body += " <p>A random number in [1, 10] is: " + randomNumber + "</p>";
body += " <p>SessionId: " + req->getSessionId() + "</p>";
body += "</body>";
body += "</html>";
res->setBody(body);
return res;
}
Response* LoginHandler::callback(Request* req) {
std::string username = req->getBodyParam("username");
std::string password = req->getBodyParam("password");
if (username == "root") {
throw Server::Exception("Remote root access has been disabled.");
}
std::cout << "username: " << username << ",\tpassword: " << password << std::endl;
Response* res = Response::redirect("/rand");
res->setSessionId("SID");
return res;
}
Response* UploadHandler::callback(Request* req) {
std::string name = req->getBodyParam("file_name");
std::string file = req->getBodyParam("file");
utils::writeToFile(file, name);
Response* res = Response::redirect("/");
return res;
}
ColorHandler::ColorHandler(const std::string& filePath)
: TemplateHandler(filePath) {}
std::map<std::string, std::string> ColorHandler::handle(Request* req) {
std::string newName = "I am " + req->getQueryParam("name");
std::map<std::string, std::string> context;
context["name"] = newName;
context["color"] = req->getQueryParam("color");
return context;
}