-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.cpp
132 lines (117 loc) · 3.02 KB
/
connection.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
#include "connection.h"
#include <cstdlib>
#include <cstring>
#include <stdio.h>
/************************
* Request class methods
*************************/
Request::Request(char* r, char* u, char* h): request_method(r), request_URI(u), http_version(h) {
time_t ping;
time(&ping);
request_bday = localtime(&ping);
if(request_URI.length() == 0){
request_URI = "/";
}
else{
if(request_URI[0] != '/'){
request_URI = "/" + request_URI;
}
}
}
void Request::addHeader(const char* key, const char* value) {
std::string header_type(key);
std::string header_value(value);
if(!header_type.compare("If-Modified-Since")){
headers[header_type] = header_value;}
else{
header_type = standardize(header_type);
header_value = standardize(header_value);
headers[header_type] = header_value;
}
}
void Request::addBody(const char * body) {
message_body = std::string(body);
}
std::string Request::standardize(std::string str){
for(unsigned i = 0; i < str.length(); i++){
str[i] = tolower(str[i]);
}
return str;
}
int Request::check_keep_alive(){
if(headers.count("connection")){
if(headers["connection"].find("keep-alive") == std::string::npos){
return 0;
}
else{
std::cout<<"\nCheck works\n";
return 1;
}
}
return 0;
}
void Request::printRequest() {
std::cout<< "\nRequest Type: "<< request_method << "\n";
std::cout<<"Request URI: "<< request_URI <<"\n";
std::cout<<"HTTP Version: "<< http_version <<"\n";
std::cout << "\nOptional Headers:\n";
for(std::map<std::string, std::string>:: iterator it = headers.begin(); it != headers.end(); ++it){
std::cout << it->first << ": " << it->second << "\n";
}
std::cout<< "\n";
}
/***********************
* ConnObj class methods
************************/
ConnObj::ConnObj() {
msg_stream = NULL;
char file1[] = "privileges/userdirs.txt";
char file2[] = "privileges/getabledirs.txt";
read_privileges(file1, &userdirs);
read_privileges(file2, &getabledirs);
}
ConnObj::~ConnObj(){
userdirs.clear();
getabledirs.clear();
}
void ConnObj::read_privileges(char* filename, std::set<std::string>* auth){
FILE* input_file;
input_file = fopen(filename, "r");
if(input_file == NULL){
fprintf(stderr, "Error opening File");
}
char* nextline = NULL;
size_t sz;
char* ptr;
while(getline(&nextline, &sz, input_file) > 0){
ptr = strchr(nextline, '\n');
if (ptr != NULL){
*ptr = '\0';
}
auth->insert(std::string(nextline));
}
free(nextline);
int check = fclose(input_file);
if (check != 0){
fprintf(stderr, "Error closing file!\n");
}
}
int ConnObj::authorized(std::string type, std::string URI){
std::string dir;
int loc = URI.find_last_of("/");
if(loc == std::string::npos){
return 0;
}
else{
dir = "www" + URI.substr(0, loc + 1);
}
if(type == "GET" || type == "HEAD"){
return getabledirs.count(dir);
}
else if(type == "PUT" || type == "POST" || type == "DELETE"){
return userdirs.count(dir);
}
else{
return 0;
}
}