/
server.cpp
137 lines (110 loc) · 3.44 KB
/
server.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
133
134
135
136
137
#include "server.h"
namespace http
{
void Server::Construct()
{
m_Acceptor = NULL;
m_Socket = NULL;
}
Server::Server()
{
Construct();
}
Server::Server(int port)
{
Construct();
this->Bind(port);
}
Server::~Server()
{
if (m_Acceptor != NULL)
delete m_Acceptor;
if (m_Socket != NULL)
delete m_Socket;
}
void Server::Bind(int port)
{
m_Port = port;
m_Acceptor = new tcp::acceptor(m_IOService, tcp::endpoint(tcp::v4(), port));
m_Socket = new tcp::socket(m_IOService);;
}
http::Request* Server::Recieve()
{
// Accept the connection, blocking
m_Acceptor->accept(*m_Socket, m_Endpoint);
http::Request* req = NULL;
try
{
boost::array<char, 2048> buf;
// Read the raw HTTP request
size_t len = m_Socket->read_some(boost::asio::buffer(buf));
std::string request = std::string(buf.data(), len);
// Create a HTTP request object and return it
req = new http::Request(request);
}
catch (std::exception &e)
{
std::cerr << e.what() << std::endl;
}
return req;
}
void Server::Send(http::Response* response)
{
// Create the response line (Version StatusCode StatusString)
std::stringstream ss;
ss << response->GetVersion() << " " << response->GetStatusCode() << " " << response->GetStatusString() << "\r\n";
// Append all the headers
std::map<std::string, std::string>* headers = response->GetHeaders();
for (auto it = headers->begin(); it != headers->end(); it++)
ss << (*it).first << ": " << (*it).second << "\r\n";
ss << "\r\n";
try
{
// Send headers
boost::asio::write(*m_Socket, boost::asio::buffer(ss.str()), boost::asio::transfer_all());
std::cout << ss.str();
// Send file data
if (response->GetContent() != NULL)
boost::asio::write(*m_Socket, boost::asio::buffer(*response->GetContent()), boost::asio::transfer_all());
//else
// std::cout << "No content" << std::endl;
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}
m_Socket->close();
/* struct http::Response::StartLine* startLine = response->GetStartLine();
std::stringstream ssStartLine;
ssStartLine<< startLine->Version << " " << startLine->ResponseCode << " " << startLine->ResponseString;
std::map<std::string, std::string>* headers = response->GetHeaders();
std::stringstream ssHeaders;
for (auto it = headers->begin(); it != headers->end(); it++)
ssHeaders << (*it).first << ": " << (*it).second << "\n";
ssHeaders << "\n";
char* content = response->GetContent();
// Send start line
boost::asio::write(*m_Socket, boost::asio::buffer(ssStartLine.str()), boost::asio::transfer_all());
// Send headers
boost::asio::write(*m_Socket, boost::asio::buffer(ssHeaders.str()), boost::asio::transfer_all());
// Send file data
boost::asio::write(*m_Socket, boost::asio::buffer(content, 7), boost::asio::transfer_all());
m_Socket->close();*/
/*int file_size = boost::filesystem::file_size(p);
boost::filesystem::ifstream file(p, std::ios::binary);
char* buffer = new char[file_size];
file.read(buffer, file_size);
std::stringstream ss;
ss << "HTTP/1.1 200 OK\n";
ss << "Content-Type: " << GetMimeType(p) << "\n";
ss << "Content-Length: " << file_size << "\n";
ss << "\n";
//ss << buffer;
std::string headers = ss.str();
std::cout << "___ RESPONSE ___" << std::endl << ss.str() << std::endl;
// Send headers
boost::asio::write(socket, boost::asio::buffer(headers), boost::asio::transfer_all());
// Send file data
boost::asio::write(socket, boost::asio::buffer(buffer, file_size), boost::asio::transfer_all());*/
}
}