-
Notifications
You must be signed in to change notification settings - Fork 0
/
requests.cpp
73 lines (57 loc) · 2.5 KB
/
requests.cpp
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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <map>
#include <cstdlib>
void addTask(const std::string& description);
std::string listTasks();
void completeTask(int taskId);
// Parse CGI-style form data (e.g., action=add&description=New+Task)
std::map<std::string, std::string> parseFormData(const std::string& query) {
std::map<std::string, std::string> formData;
size_t pos = 0;
std::string token;
std::string queryCopy = query;
while ((pos = queryCopy.find("&")) != std::string::npos) {
token = queryCopy.substr(0, pos);
size_t delimiterPos = token.find("=");
if (delimiterPos != std::string::npos) {
formData[token.substr(0, delimiterPos)] = token.substr(delimiterPos + 1);
}
queryCopy.erase(0, pos + 1);
}
size_t delimiterPos = queryCopy.find("=");
if (delimiterPos != std::string::npos) {
formData[queryCopy.substr(0, delimiterPos)] = queryCopy.substr(delimiterPos + 1);
}
return formData;
}
int main() {
std::cout << "Content-type: text/html\n\n";
std::string queryString = getenv("QUERY_STRING") ? getenv("QUERY_STRING") : "";
if (!queryString.empty()) {
std::map<std::string, std::string> formData = parseFormData(queryString);
if (formData.find("action") != formData.end() && formData["action"] == "add" && formData.find("description") != formData.end()) {
std::string description = formData["description"];
addTask(description);
std::cout << "<p>Task added: " << description << "</p>";
}
if (formData.find("action") != formData.end() && formData["action"] == "complete" && formData.find("id") != formData.end()) {
int taskId = std::stoi(formData["id"]);
completeTask(taskId);
std::cout << "<p>Task ID " << taskId << " marked as completed.</p>";
}
}
std::cout << "<h1>Task List</h1>";
std::cout << listTasks();
std::cout << "<form method='get'>";
std::cout << "<input type='text' name='description' placeholder='New Task'>";
std::cout << "<input type='hidden' name='action' value='add'>";
std::cout << "<input type='submit' value='Add Task'>";
std::cout << "</form>";
std::cout << "<form method='get'>";
std::cout << "<input type='number' name='id' placeholder='Task ID'>";
std::cout << "<input type='hidden' name='action' value='complete'>";
std::cout << "<input type='submit' value='Mark Completed'>";
std::cout << "</form>";
return 0;
}