-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.cpp
More file actions
174 lines (147 loc) · 5.2 KB
/
Copy pathServer.cpp
File metadata and controls
174 lines (147 loc) · 5.2 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
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
#include <iostream>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <fstream>
using namespace std;
char *parseFT(char path[]);
string getFileContent(string, string);
int main() {
int fdSocket, fdChildSocket, port, pid;
sockaddr_in parent, child;
char message[30000] = {0};
socklen_t addrlen = sizeof(parent);
char reponseHeader[] = "HTTP/1.1 200 OK\r\n";
char reponseHeaderFound[] = "HTTP/1.1 308 Permanent Redirect\r\n";
char contentType[] = "Content-Type: text/html;charset=UTF-8\r\n";
char contentTypecss[] = "Content-Type: text/css\r\n";
char contentTypephp[] = "Content-Type: application/php\r\n";
char loca[] = "Location: BloodBank/admin_dashboard.php";
port = 8080;
char del[2] = " ";
char delF[2] = "/";
if ((fdSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
cout << "Failed to create a socket" << endl;
}
parent.sin_port = htons(port);
parent.sin_family = AF_INET;
parent.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
if ((bind(fdSocket, (struct sockaddr *)&parent, sizeof(parent))) < 0) {
perror("bind");
exit(EXIT_FAILURE);
}
if (listen(fdSocket, 5) < 0) {
perror("listen");
exit(EXIT_FAILURE);
}
cout << "Listening for connections on port " << port << endl;
while (true) {
fdChildSocket = accept(fdSocket, (struct sockaddr *)&parent, &addrlen);
cout << "Request accepted" << endl;
if (fdChildSocket < 0) {
perror("child socket");
exit(EXIT_FAILURE);
}
ssize_t bytesread = read(fdChildSocket, message, sizeof(message) - 1);
if (bytesread == 0) {
cout << "Tried to read from empty buffer" << endl;
continue;
}
string mess(message);
if (bytesread < 0) {
perror("read");
exit(EXIT_FAILURE);
}
char *method = strtok(message, del);
char *filePath = strtok(NULL, del);
filePath = strtok(filePath, delF);
string fp(filePath);
char *fileType = parseFT(filePath);
string ft(fileType);
if (method[0] == 'G' && method[1] == 'E' && method[2] == 'T') {
// Handle GET request
char response[80000] = {0};
char length[50] = {0};
string htmlContent = getFileContent(fp, ft);
snprintf(length, sizeof(length), "Content-Length: %zu\r\n", htmlContent.length());
strcat(response, reponseHeader);
if (ft == "css") {
strcat(response, contentTypecss);
} else if (ft == "html") {
strcat(response, contentType);
}
strcat(response, length);
strcat(response, "\r\n");
strcat(response, htmlContent.c_str());
cout << "GET request handled" << endl;
send(fdChildSocket, response, strlen(response), 0);
} else if (method[0] == 'P' && method[1] == 'O' && method[2] == 'S' && method[3] == 'T') {
// Handle POST request
char response[80000] = {0};
strcat(response, reponseHeaderFound);
strcat(response, loca);
// Extract POST data
char *postData = strstr(message, "\r\n\r\n");
if (postData) {
postData += 4; // Move past the \r\n\r\n
// Execute PHP script with POST data
FILE *phpProcess = popen(("SCRIPT_FILENAME=" + fp + " REQUEST_METHOD=POST REDIRECT_STATUS=CGI cgi-fcgi -bind -connect -d / -s /var/run/php/php7.4-fpm.sock").c_str(), "w");
if (phpProcess) {
// Send POST data to PHP process
fwrite(postData, sizeof(char), strlen(postData), phpProcess);
pclose(phpProcess);
// Send response
cout << "POST request handled" << endl;
send(fdChildSocket, response, strlen(response), 0);
} else {
perror("php-cgi");
exit(EXIT_FAILURE);
}
} else {
// Handle no POST data scenario
cout << "No POST data found" << endl;
// Send appropriate error response
send(fdChildSocket, response, strlen(response), 0);
}
}
close(fdChildSocket);
message[30000] = {0};
}
close(fdSocket);
}
char *parseFT(char path[]) {
char del[2] = ".";
char *fileType = strtok(path, del);
char *last;
while (fileType != NULL) {
last = fileType;
fileType = strtok(NULL, del);
}
return last;
}
string getFileContent(string path, string ft) {
if (ft == "jpg" || ft == "png" || ft == "ico") {
return "";
}
string p = "BloodBank/";
p += path;
cout << p << endl;
ifstream myfile(p);
if (!myfile.is_open()) {
perror("file");
exit(EXIT_FAILURE);
return "";
}
string line;
string message;
while (getline(myfile, line)) {
message += line + "\n";
}
myfile.close();
return message;
}