main.cpp
3.46 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "webserver.h"
#include "socket/src/Socket.h"
#include "nlohmann/json.hpp"
#include <sstream>
std::string urlDecode(std::string const &e_string) {
std::string ret;
int j;
for (int i = 0; i < int(e_string.length()); i++) {
if (int(e_string[i]) == 37) {
sscanf_s(e_string.substr(i + 1, 2).c_str(), "%x", &j);
const auto ch = static_cast<char>(j);
ret += ch;
i = i + 2;
}
else {
ret += e_string[i];
}
}
return (ret);
}
void Request_Handler(webserver::http_request* r) {
Socket s = *(r->s_);
nlohmann::json data;
r->content_type_ = "application/json";
if(r->path_=="/")
{
r->status_ = "403 Forbidden";
data = { {"type","error"},{"method",r->method_},{"path",r->path_}};
}
else if (r->path_ == "/music")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/artist")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/album")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/genre")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/recent_play")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/recent_add")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else if (r->path_ == "/playlist")
{
if (r->method_ == "GET") {}
else if (r->method_ == "POST") {}
else if (r->method_ == "DELETE") {}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };
}
}
else if (r->path_ == "/search")
{
const auto& query = r->params_;
if (r->method_ == "GET")
{
auto type_it=query.find("type");
auto value_it = query.find("value");
if(type_it==r->params_.end()||value_it == r->params_.end())
{
r->status_ = "400 Bad Request";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
else
{
}
}
else
{
r->status_ = "405 Method Not Allowed";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };;
}
}
else{
r->status_ = "404 Not Found";
data = { {"type","error"},{"method",r->method_},{"path",r->path_} };
}
nlohmann::json result = {
{"status", r->status_},
{"data", data}
};
r->answer_ = result.dump();
}
int main() {
webserver(8080, Request_Handler);
}