-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTTPServer.cpp
More file actions
153 lines (134 loc) · 3.68 KB
/
Copy pathHTTPServer.cpp
File metadata and controls
153 lines (134 loc) · 3.68 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
#include "HTTPServer.h"
#include <iostream>
#include <unistd.h>
#include <cstring>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fstream>
#include <sstream>
const int MAX_CONNECTIONS = 10;
const int BUFFER_SIZE = 1024;
HTTPServer::HTTPServer(const std::string &ip_address, int port) : ip_address(ip_address), port(port)
{
createSocket();
setSocketOptions();
bindSocket();
}
HTTPServer::~HTTPServer()
{
close(server_fd);
}
void HTTPServer::createSocket()
{
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
std::cerr << "Socket creation failed" << std::endl;
exit(EXIT_FAILURE);
}
}
void HTTPServer::setSocketOptions()
{
int opt = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt)))
{
std::cerr << "Setsockopt failed" << std::endl;
exit(EXIT_FAILURE);
}
}
void HTTPServer::bindSocket()
{
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr(ip_address.c_str());
address.sin_port = htons(port);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) < 0)
{
std::cerr << "Bind failed" << std::endl;
exit(EXIT_FAILURE);
}
}
void HTTPServer::start()
{
listenForConnections();
}
void HTTPServer::listenForConnections()
{
if (listen(server_fd, MAX_CONNECTIONS) < 0)
{
std::cerr << "Listen failed" << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Server listening on " << ip_address << ":" << port << std::endl;
int new_socket;
struct sockaddr_in client_address;
socklen_t addrlen = sizeof(client_address);
while (true)
{
if ((new_socket = accept(server_fd, (struct sockaddr *)&client_address, &addrlen)) < 0)
{
std::cerr << "Accept failed" << std::endl;
exit(EXIT_FAILURE);
}
handleConnection(new_socket);
close(new_socket);
}
}
std::string HTTPServer::getContentType(const std::string &file_path)
{
if (file_path.find(".html") != std::string::npos)
{
return "text/html";
}
else if (file_path.find(".css") != std::string::npos)
{
return "text/css";
}
else if (file_path.find(".js") != std::string::npos)
{
return "application/javascript";
}
else
{
return "text/plain";
}
}
void HTTPServer::handleConnection(int new_socket)
{
char buffer[BUFFER_SIZE] = {0};
read(new_socket, buffer, BUFFER_SIZE);
std::string request(buffer);
std::istringstream iss(request);
std::string method, path, version;
iss >> method >> path >> version;
if (method == "GET")
{
if (path == "/" || path == "/index.html")
{
std::string file_path = "index.html";
std::string content_type = getContentType(file_path);
std::string response = "HTTP/1.1 200 OK\nContent-Type: " + content_type + "\n\n" + readFile(file_path);
send(new_socket, response.c_str(), response.length(), 0);
std::cout << "Response sent" << std::endl;
}
else
{
std::string response = "HTTP/1.1 404 Not Found\nContent-Type: text/html\n\n<html><body><h1>404 Not Found</h1></body></html>";
send(new_socket, response.c_str(), response.length(), 0);
std::cout << "404 Response sent" << std::endl;
}
}
}
std::string HTTPServer::readFile(const std::string &file_path)
{
std::ifstream file(file_path);
if (file)
{
std::ostringstream content;
content << file.rdbuf();
return content.str();
}
else
{
std::cerr << "Failed to open file: " << file_path << std::endl;
return "";
}
}