-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.cpp
More file actions
100 lines (80 loc) · 2.72 KB
/
http_server.cpp
File metadata and controls
100 lines (80 loc) · 2.72 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
#include "./headers/http_server.h"
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <netinet/in.h>
#include <sstream>
#include <sys/socket.h>
#include <unistd.h>
void HttpServer::runServer(int serverSocket) {
if (serverSocket == -1) {
std::cerr << "Invalid HTTP socket file descriptor!" << std::endl;
throw std::runtime_error("Invalid HTTP socket fd for listening");
}
std::cout << "HTTP Server is running and listening on port..." << std::endl;
while (true) {
sockaddr_in client_address;
socklen_t client_len = sizeof(client_address);
int client_socket =
accept(serverSocket, (struct sockaddr *)&client_address, &client_len);
if (client_socket == -1) {
std::cerr << "HTTP Server: Failed to accept proxy connection"
<< std::endl;
continue;
}
std::cout << "HTTP Server: Proxy connected!" << std::endl;
char requestBuffer[4096];
int bytesRead =
recv(client_socket, requestBuffer, sizeof(requestBuffer) - 1, 0);
if (bytesRead < 0) {
std::cerr << "HTTP Server: Failed to read proxy request" << std::endl;
close(client_socket);
continue;
}
requestBuffer[bytesRead] = '\0';
std::string request(requestBuffer);
std::string requestedPath = "/";
// std::cout << request;
size_t pos = request.find("GET ");
if (pos != std::string::npos) {
size_t start = pos + 4;
size_t end = request.find(" ", start);
requestedPath = request.substr(start, end - start);
}
std::string projectPath = "/home/bok1c4/Projects/Web-Service/content/";
std::string file = "";
if (requestedPath == "/") {
file = "index.html";
} else if (requestedPath == "/about") {
file = "about.html";
} else if (requestedPath == "/contact") {
file = "contact.html";
} else {
file = "404.html";
}
projectPath += file;
// Read the custom HTML file
std::ifstream htmlFile(projectPath);
if (!htmlFile.is_open()) {
std::cerr << "Failed to open HTML file!" << std::endl;
close(client_socket);
continue;
}
// Load the HTML content into a memory (string)
std::ostringstream buffer;
buffer << htmlFile.rdbuf();
std::string htmlContent = buffer.str();
// Build the HTTP response with the HTML content
std::ostringstream response;
response << "HTTP/1.1 200 OK\r\n"
<< "Content-Type: text/html\r\n"
<< "Content-Length: " << htmlContent.size() << "\r\n"
<< "\r\n"
<< htmlContent;
// Send the HTTP response to the client
send(client_socket, response.str().c_str(), response.str().size(), 0);
// Close the client socket
close(client_socket);
}
}