-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
217 lines (177 loc) · 4.85 KB
/
client.cpp
File metadata and controls
217 lines (177 loc) · 4.85 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "client.h"
HttpClient::HttpClient(SOCKET clientSocket, sockaddr_in client_addr, const std::string &sDir) :
m_sock(clientSocket), m_addr(client_addr), m_sDir(sDir), m_bStop(false)
{}
HttpClient::~HttpClient()
{
deinit();
}
void HttpClient::deinit()
{
if( m_sock != 0 )
{
shutdown(m_sock, 0);
closesocket(m_sock);
m_sock = 0;
}
}
void HttpClient::setDisconnectHandler(std::function<void()> &&handler)
{
onDisconnect = std::move(handler);
}
void HttpClient::printAddr()
{
std::cout << "ip = " << inet_ntoa(m_addr.sin_addr) << " port = " << ntohs(m_addr.sin_port);
}
/** Returns true on success, or false if there was an error */
bool SetSocketBlockingEnabled(int fd, bool blocking)
{
if( fd < 0 ) return false;
#ifdef WIN32
unsigned long mode = blocking ? 0 : 1;
return (ioctlsocket(fd, FIONBIO, &mode) == 0) ? true : false;
#else
int flags = fcntl(fd, F_GETFL, 0);
if( flags < 0 ) return false;
flags = blocking ? (flags&~O_NONBLOCK) : (flags | O_NONBLOCK);
return (fcntl(fd, F_SETFL, flags) == 0) ? true : false;
#endif
}
void HttpClient::execute()
{
char szRecvBuf[10000];
SetSocketBlockingEnabled(m_sock, false);
fd_set read_set;
struct timeval tv;
while( 1 )
{
FD_ZERO(&read_set);
FD_SET(m_sock, &read_set);
tv.tv_sec = 1;
tv.tv_usec = 0;
int nSelectRes = select(FD_SETSIZE, &read_set, NULL, NULL, &tv);
if( nSelectRes < 0 )
{
std::cout << "Select() fails" << std::endl;
onDisconnect();
return;
}
int nRecvRes;
if( FD_ISSET(m_sock, &read_set) )
{
nRecvRes = recv(m_sock, szRecvBuf, sizeof(szRecvBuf), 0);
if( nRecvRes <= 0 )
{
onDisconnect();
return;
}
}
else
{
if( recv(m_sock, szRecvBuf, sizeof(szRecvBuf), 0) == 0 )
break;
if( m_bStop )
break;
continue;
}
szRecvBuf[nRecvRes] = '\0';
std::istringstream split(szRecvBuf);
std::string sRequestFileName;
for( std::string token; std::getline(split, token); )
{
if( token.find("GET ") != std::string::npos )
{
std::istringstream ss(token);
std::getline(ss, sRequestFileName, ' '); // GET
std::getline(ss, sRequestFileName, ' '); // fname
}
}
if( sRequestFileName.empty() )
{
std::cout << "invalid request" << std::endl;
onDisconnect();
return;
}
std::cout << "GET " << sRequestFileName << std::endl;
std::string sUsedFileName;
if( sRequestFileName == "/" )
sUsedFileName = "/index.html";
else
{
std::stringstream ss(sRequestFileName);
std::getline(ss, sUsedFileName, '?');
//sUsedFileName = sRequestFileName;
}
#ifdef _WIN32
FILE *f = fopen((m_sDir + sUsedFileName).c_str(), "rb");
#else
FILE *f = fopen((m_sDir + sUsedFileName).c_str(), "r");
#endif
if( f == NULL )
{ // 404
std::cout << "No page " << (m_sDir + sUsedFileName) << ", disconnecting." << std::endl;
std::string s404 = "HTTP/1.1 404 Not Found\r\n\r\n";
/* std::string s404 = "HTTP/1.1 ";
s404 += sRequestFileName;
const char sz404page[] = "<html><body>404 Page not found :(</html></body>";
s404 += " 404 Not Found\r\nContent-Type: text/html;charset=win-1251\r\nContent-Length: ";
s404 += std::to_string(sizeof(sz404page));
s404 += "\r\nCache - Control: no - cache, no - store\r\n\r\n";
s404 += sz404page;*/
send(m_sock, s404.c_str(), s404.length(), 0);
onDisconnect();
//fclose(f);
return;
}
long lSize = 0;
if( fseek(f, 0, SEEK_END) == 0 )
{
lSize = ftell(f);
fseek(f, 0, SEEK_SET);
}
char *pBuf = new char[lSize + 1];
if( fread(pBuf, lSize, 1, f) != 1 )
{ // read error
delete[] pBuf;
std::cout << "Read error, disconnecting." << std::endl;
std::string s404 = "HTTP/1.1 ";
s404 += sRequestFileName;
const char sz404page[] = "<html><body>404 Page not found :(</html></body>";
s404 += " 404 Not Found\r\nContent-Type: text/html;charset=win-1251\r\nContent-Length: ";
s404 += std::to_string(sizeof(sz404page));
s404 += "\r\nCache - Control: no - cache, no - store\r\n\r\n";
s404 += sz404page;
send(m_sock, s404.c_str(), s404.length(), 0);
onDisconnect();
fclose(f);
return;
};
//std::cout << pBuf << std::endl;
std::string sMsg = "HTTP/1.1 ";
sMsg += sRequestFileName;
sMsg += " 200 OK \r\nContent-Type: text/html;charset=win-1251\r\nContent-Length: ";
//if( pBuf[1] != 'h' )
// sMsg += std::to_string(lSize + 12 + 14 - 1);
//else
sMsg += std::to_string(lSize - 1);
sMsg += "\r\nCache - Control: no - cache, no - store\r\n\r\n";
pBuf[lSize] = '\0';
// if( pBuf[1] != 'h' )
// sMsg += "<html><body>";
sMsg += pBuf;
sMsg.pop_back();
delete[] pBuf;
//if( pBuf[1] != 'h' )
// sMsg += "</body></html>";
if( send(m_sock, sMsg.c_str(), sMsg.length(), 0) != sMsg.length() )
{
std::cout << "Send fails, disconnecting." << std::endl;
onDisconnect();
fclose(f);
return;
}
std::cout << "File " << sUsedFileName << " transferred." << std::endl;
fclose(f);
}
onDisconnect();
}