-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpRequest.cpp
193 lines (153 loc) · 3.85 KB
/
HttpRequest.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
* HttpRequest.cpp
*
* Created on: May 28, 2020
* Author: voukatas
*/
#include <cstdio>
#include <sys/socket.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <errno.h>
#include <cstring>
#include <vector>
#include "HttpRequest.h"
#include "config.h"
#include "Logger/Logger.h"
#include "Logger/loglvl.h"
HttpRequest::HttpRequest(int clientSocket):
clientSocket{clientSocket}{}
int HttpRequest::readData(void)
{
char buffer[MAXDATASIZE] = { 0 };
int numbytes;
//wait data from client
numbytes = recv(clientSocket, buffer, MAXDATASIZE - 1, 0);
if ( numbytes <= 0)
{
//peer has performed an orderly shutdown
if( numbytes == 0)
{
LoggerSpace::Logger::instance().log(Loglvl::INFO,"Server: recv peer has performed an orderly shutdown: " + std::string(std::strerror(errno)));
}
else
{
LoggerSpace::Logger::instance().log(Loglvl::INFO,"Server: recv: " + std::string(std::strerror(errno)));
}
//close the socket immediately
close(clientSocket);
return -1;// connection closed, nothing to do so return
}
//set the last byte to NUL termination string
buffer[numbytes] = '\0';
//request = std::string(buffer);
request.str(std::string(buffer));
return 0;
}
std::string HttpRequest::getRequest()
{
return request.str();
}
std::string HttpRequest::getMethodName()
{
return method_name;
}
std::string& HttpRequest::getPath()
{
return path;
}
std::string HttpRequest::getProtocol()
{
return protocol;
}
int HttpRequest::isReqValid()
{
//Only GET method is currently supported
if(method_name != "GET")
{
return -2;
}
//client ask for the default page
if(path.empty())
{
return 0;
}
//check if the path is a file and if not send an error response
if( !HttpRequest::is_path_file(path) )
{
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"path: "+path+" NOT A FILE\n");
return -1;
}
return 0;
}
void HttpRequest::parseReq()
{
//ToDo: check for failures
std::string line;
std::getline(request, line);
HttpRequest::parseReqFirstLine(line);
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"client sent:\n"+request.str());
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"client line:\n" + line);
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"client sent method:"+method_name);
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"client sent path:"+path);
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"client sent protocol:"+protocol);
while(std::getline(request, line))
{
HttpRequest::parseReqFields(line);
}
}
void HttpRequest::parseReqFirstLine(std::string &line)
{
std::vector<std::string> splitedLine;
std::string delimiter = " ";
size_t pos = 0;
std::string token;
while ((pos = line.find(delimiter)) != std::string::npos)
{
token = line.substr(0, pos);
splitedLine.push_back(token);
line.erase(0, pos + delimiter.length());
}
splitedLine.push_back(line);
method_name = splitedLine.at(0);
path = splitedLine.at(1).erase(0,1);//remove the /
protocol = splitedLine.at(2);
}
void HttpRequest::parseReqFields(std::string& line)
{
std::stringstream ss(line);
std::string s;
if(line == "\r" || line == "")
{
return;
}
std::string delimiter = ":";
std::string field = line.substr(0, line.find(delimiter));
std::string value = line.substr(line.find(delimiter)+2);
fieldMap[field] = value;
LoggerSpace::Logger::instance().log(Loglvl::DEBUG,"parseReqFields: field="+field+" value="+fieldMap[field]);
}
std::map<std::string, std::string>& HttpRequest::getFieldMap()
{
return fieldMap;
}
//checks if the given path is a regular file
bool HttpRequest::is_path_file(std::string path_value)
{
const char * path = path_value.c_str();
struct stat s;
if( stat(path,&s) == 0 )
{
if( s.st_mode & S_IFREG )//S_IFDIR//for directory
{
return true;
}
}
return false;
}
void HttpRequest::setRequest(std::string req)
{
request.str(req);
}