-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadheader.cpp
More file actions
70 lines (59 loc) · 1.37 KB
/
readheader.cpp
File metadata and controls
70 lines (59 loc) · 1.37 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
#include "readheader.h"
using namespace std;
ReadHeader::ReadHeader(int client) :Header(client) {
readheader();
}
// read header function
void ReadHeader::readheader() {
buff_r.resize((const size_t)1024); // read the first row
int number_char = read_line();
int pos = buff_r.find_first_of(" "); // method, GET or POST
method = buff_r.substr(0, pos);
buff_r = buff_r.substr(pos + 1); // request url
pos = buff_r.find_first_of(" ");
url = buff_r.substr(0, pos);
path = "htm" + url; // file path
if (path.back() == '/') {
path += "index.html";
}
struct stat status; // check the accessibility
if (stat(path.c_str(), &status) == -1) {
while (number_char > 0 && buff_r == "\n") {
read_line();
}
// not_found();
}
else {
if ((status.st_mode & S_IFMT) == S_IFDIR) {
path += "/index.html";
}
while (number_char > 0 && buff_r == "\n") {
read_line();
}
}
}
// read line function
int ReadHeader::read_line() {
int i = 0;
char c = '0';
while (i < buff_r.length() && c != '\n') {
int n = recv(client, &c, 1, 0);
if (n > 0) {
if (c == '\r') {
n = recv(client, &c, 1, MSG_PEEK);
if (n > 0 && c == '\n') { recv(client, &c, 1, 0); }
else { c = '\n'; }
}
buff_r[i++] = c;
}
else { c = '\n'; }
}
return i;
}
// getter function
string ReadHeader::get_method() {
return method;
}
string ReadHeader::get_path() {
return path;
}