-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpServer.cpp
More file actions
133 lines (111 loc) · 4.76 KB
/
httpServer.cpp
File metadata and controls
133 lines (111 loc) · 4.76 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
#include "httpServer.h"
#include "bits/stdc++.h"
#include "unistd.h"
#include "sstream"
using namespace std;
namespace
{
const int BUFFER_SIZE = 30720;
void log(const string &message)
{
cout << message << endl;
}
void exitingWithError(const string &errorMessage)
{
log("Error : " + errorMessage);
exit(1);
}
}
namespace http
{
TcpServer::TcpServer(string ip_address, int port) :
localIpAddress(ip_address),
localPort(port),
localSocket(),
localNewSocket(),
localIncomingMessage(),
localSocketAddress(),
localSocketAddressLen(sizeof(localSocketAddress)),
localServerMessage(buildResponse()){
// naming the socket and socket-address
localSocketAddress.sin_family = AF_INET; // socket-family
localSocketAddress.sin_port = htons(localPort); // assining socket with port
localSocketAddress.sin_addr.s_addr = inet_addr(localIpAddress.c_str());
if(initServer() != 0){
ostringstream ss;
ss <<"Failed to start server with port number: "<< ntohs(localSocketAddress.sin_port);
log((ss.str()));
}
}
TcpServer::~TcpServer(){
endServer();
}
int TcpServer::initServer(){
localSocket = socket(AF_INET , SOCK_STREAM, 0);
if(localSocket < 0){
exitingWithError("Not able to create a socket!");
return 1;
}
if(bind(localSocket , (sockaddr*)&localSocketAddress , localSocketAddressLen) < 0){
exitingWithError("Cannot Connect socket to address");
return 1;
}
return 0;
}
void TcpServer::endServer(){
close(localSocket);
close(localNewSocket);
exit(0);
}
void TcpServer :: startListen(){
if(listen(localSocket, 20) < 0){
exitingWithError("Socket List failed");
}
ostringstream ss;
ss << "\n*** Listening on ADDRESS: " << inet_ntoa(localSocketAddress.sin_addr) << " PORT: " << ntohs(localSocketAddress.sin_port) << " ***\n\n";
log(ss.str());
int bytesReceived;
/*
--------Creating new connection----------
------ Received Request from client -----
*/
while(true){
log("--------Creating new connection----------\n\n\n");
acceptConnection(localNewSocket);
char buffer[BUFFER_SIZE] = {0};
bytesReceived = read(localNewSocket , buffer , BUFFER_SIZE);
if(bytesReceived < 0){
exitingWithError("Failed to read bytes from client socket connection");
}
ostringstream ss;
ss << "------ Received Request from client -----\n\n\n";
log(ss.str());
sendResponse();
close(localNewSocket);
}
}
void TcpServer :: acceptConnection(int &newSocket){
newSocket = accept(localSocket, (sockaddr *)&localSocketAddress, &localSocketAddressLen);
if (newSocket < 0){
std::ostringstream ss;
ss << "Server failed to accept incoming connection from ADDRESS: " << inet_ntoa(localSocketAddress.sin_addr) << "; PORT: " << ntohs(localSocketAddress.sin_port);
exitingWithError(ss.str());
}
}
string TcpServer::buildResponse(){
string htmlFile = "<!DOCTYPE html><html lang=\"en\"><body><h1> HTTP-webserver-from-scratch </h1><p> Listening from server on port 8080! </p></body></html>";
ostringstream ss;
ss << "HTTP/1.1 200 OK\nContent-Type: text/html\nContent-Length: " << htmlFile.size() << "\n\n"<< htmlFile;
return ss.str();
}
void TcpServer::sendResponse(){
long bytesSent;
bytesSent = write(localNewSocket, localServerMessage.c_str(), localServerMessage.size());
if (bytesSent == localServerMessage.size()){
log("------ Server Response sent to client ------\n\n");
}
else{
log("Error sending response to client");
}
}
}