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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290 | //
// This file is part of the aMule Project.
//
// Copyright (c) 2004-2011 shakraw ( shakraw@users.sourceforge.net )
// Copyright (c) 2003-2011 aMule Team ( admin@amule.org / http://www.amule.org )
// Copyright (c) 2002-2011 Merkur ( devs@emule-project.net / http://www.emule-project.net )
//
// Any parts of this program derived from the xMule, lMule or eMule project,
// or contributed by third-party developers are copyrighted by their
// respective authors.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
//
#include "WebSocket.h"
#ifdef ENABLE_UPNP
# include "UPnPBase.h"
#endif
CWebSocket::CWebSocket(CWebServerBase *parent)
{
m_pHead = 0;
m_pTail = 0;
m_pBuf = new char [4096];<--- Class 'CWebSocket' does not have a destructor which is recommended since it has dynamic memory/resource allocation(s).
m_dwBufSize = 4096;<--- Variable 'm_dwBufSize' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_dwBufSize' a value by passing the value to the constructor in the initialization list.
m_dwRecv = 0;<--- Variable 'm_dwRecv' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_dwRecv' a value by passing the value to the constructor in the initialization list.
m_dwHttpHeaderLen = 0;<--- Variable 'm_dwHttpHeaderLen' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_dwHttpHeaderLen' a value by passing the value to the constructor in the initialization list.
m_dwHttpContentLen = 0;<--- Variable 'm_dwHttpContentLen' is assigned in constructor body. Consider performing initialization in initialization list. [+]When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_dwHttpContentLen' a value by passing the value to the constructor in the initialization list.
m_Cookie = 0;
m_IsGet = false;
m_IsPost = false;
m_pParent = parent;
#ifndef ASIO_SOCKETS
SetEventHandler(*parent, ID_WEBCLIENTSOCKET_EVENT);
SetNotify(wxSOCKET_INPUT_FLAG | wxSOCKET_OUTPUT_FLAG | wxSOCKET_LOST_FLAG);
#endif
Notify(true);
}
void CWebSocket::OnLost()
{
Close();
Destroy();
}
void CWebSocket::OnReceive(int)
{
uint32 read = Read(m_pBuf + m_dwRecv, m_dwBufSize - m_dwRecv);
m_dwRecv += read;
while ((m_dwRecv == m_dwBufSize) && (read != 0) && (!LastError())) {
// Buffer is too small. Make it bigger.
uint32 newsize = m_dwBufSize + (m_dwBufSize >> 1);
char* newbuffer = new char[newsize];
char* oldbuffer = m_pBuf;
memcpy(newbuffer, oldbuffer, m_dwBufSize);
delete[] oldbuffer;
m_pBuf = newbuffer;
m_dwBufSize = newsize;
// And read again
read = Read(m_pBuf + m_dwRecv, m_dwBufSize - m_dwRecv);
m_dwRecv += read;
}
if (read == 0) {
if (LastError()) {
Close();
return ;
}
}
m_pBuf[m_dwRecv] = '\0';
//
// Check what kind of request is that
if ( !m_IsGet && !m_IsPost && m_dwRecv >= 4) {
if ( !strncasecmp(m_pBuf, "GET", 3) ) {
m_IsGet = true;
} else if ( !strncasecmp(m_pBuf, "POST", 4) ) {
m_IsPost = true;
} else {
// unknown request - close the socket
Close();
return ;
}
}
//
// RFC1945:
//
//
// "GET" must have last line empty
if ( m_IsGet ) {
if ( !strncasecmp(m_pBuf + m_dwRecv - 4, "\r\n\r\n", 4) ) {
//
// Process request
OnRequestReceived(m_pBuf, 0, 0);
}
}
//
// "POST" have "Content-Length"
if ( m_IsPost ) {
char *cont_len = strstr(m_pBuf, "Content-Length");
// do we have received all the line ?
if ( cont_len && strstr(cont_len, "\r\n\r\n") ) {
cont_len += strlen("Content-Length:");
// can be white space following
while ( isspace(*cont_len) ) cont_len++;
int len = atoi(cont_len);
if ( !len ) {
Close();
return ;
}
// do we have all of data ?
char *cont = strstr(m_pBuf, "\r\n\r\n");
cont += 4;
if ( cont - m_pBuf + len <= (int)m_dwRecv ) {
OnRequestReceived(m_pBuf, cont, len);
}
}
}
}
void CWebSocket::OnSend(int)
{
while (m_pHead && m_pHead->m_pToSend) {
uint32 nRes = Write(m_pHead->m_pToSend, m_pHead->m_dwSize);
if (nRes >= m_pHead->m_dwSize) {
// erase this chunk
CChunk* pNext = m_pHead->m_pNext;
delete m_pHead;
if (!(m_pHead = pNext)) {
m_pTail = NULL;
}
} else {
if (LastError()) {
Close();
break;
} else if (nRes > 0) {
m_pHead->m_pToSend += nRes;
m_pHead->m_dwSize -= nRes;
} else {
// blocks
break;
}
}
}
}
void CWebSocket::OnRequestReceived(char* pHeader, char* pData, uint32 dwDataLen)
{
bool is_post = false;
if ( strncmp(pHeader, "GET", 3) == 0 ) {
} else if ( strncmp(pHeader, "POST", 4) == 0 ) {
is_post = true;
} else {
// invalid request
return ;
}
char *path = strchr(pHeader, ' ');
if ( !path ) {
return;
}
*path++ = 0;
pHeader = strchr(path, ' ');
if ( !pHeader ) {
return;
}
*pHeader++ = 0;
wxString sURL(char2unicode(path));
if ( is_post ) {
wxString sData(char2unicode(pData));
sURL += wxT("?") + sData.Left(dwDataLen);
}
//
// Find session cookie.
//
int sessid = 0;
char *current_cookie = strstr(pHeader, "Cookie: ");
if ( current_cookie == NULL ) {
current_cookie = strstr(pHeader, "cookie: ");
}
if ( current_cookie ) {
current_cookie = strstr(current_cookie, "amuleweb_session_id");
if ( current_cookie ) {
char *value = strchr(current_cookie, '=');<--- Variable 'value' can be declared as pointer to const
if ( value ) {
sessid = atoi(++value);
}
}
}
ThreadData Data = { CParsedUrl(sURL), sURL, sessid, this };
wxString sFile = Data.parsedURL.File();
if (sFile.Length() > 4 ) {
wxString url_ext = sFile.Right( sFile.Length() - sFile.Find('.', true) ).MakeLower();
if ( (url_ext==wxT(".gif")) || (url_ext==wxT(".jpg")) || (url_ext==wxT(".ico")) ||
(url_ext==wxT(".png")) || (url_ext==wxT(".bmp")) || (url_ext==wxT(".jpeg")) ) {
m_pParent->ProcessImgFileReq(Data);
} else {
m_pParent->ProcessURL(Data);
}
} else {
m_pParent->ProcessURL(Data);
}
//
// Done processing, reset state
//
m_dwRecv = 0;
m_IsGet = 0;
m_IsPost = 0;
}
void CWebSocket::SendContent(const char* szStdResponse, const void* pContent, uint32 dwContentSize) {
char szBuf[0x1000]; // 0x1000 is safe because it's just used for the header
int nLen = snprintf(szBuf, sizeof(szBuf), "HTTP/1.1 200 OK\r\n%sContent-Length: %d\r\n\r\n", szStdResponse, dwContentSize);
SendData(szBuf, nLen);
SendData(pContent, dwContentSize);
}
void CWebSocket::SendHttpHeaders(const char* szType, bool use_gzip, uint32 content_len, int session_id)
{
char szBuf[0x1000];
char cookie[256];
if ( session_id ) {
snprintf(cookie, sizeof(cookie), "Set-Cookie: amuleweb_session_id=%d\r\n", session_id);
} else {
cookie[0] = 0;
}
snprintf(szBuf, sizeof(szBuf), "HTTP/1.1 200 OK\r\nServer: aMule\r\nPragma: no-cache\r\nExpires: 0\r\n"
"Cache-Control: no-cache, no-store, must-revalidate\r\n"
"%s"
"Connection: close\r\nContent-Type: %s\r\n"
"Content-Length: %d\r\n%s\r\n",
cookie, szType, content_len, (use_gzip ? "Content-Encoding: gzip\r\n" : ""));
SendData(szBuf, strlen(szBuf));
}
void CWebSocket::SendData(const void* pData, uint32 dwDataSize)
{
if (!dwDataSize) { // sanity
return;
}
const char * data = (const char*) pData;<--- C-style pointer casting [+]C-style pointer casting detected. C++ offers four different kinds of casts as replacements: static_cast, const_cast, dynamic_cast and reinterpret_cast. A C-style cast could evaluate to any of those automatically, thus it is considered safer if the programmer explicitly states which kind of cast is expected.
bool outputRequired = !m_pHead;
// push it to our tails
CChunk* pChunk = new CChunk;
pChunk->m_pNext = NULL;
pChunk->m_dwSize = dwDataSize;
pChunk->m_pData = new char[dwDataSize];
memcpy(pChunk->m_pData, data, dwDataSize);
// push it to the end of our queue
pChunk->m_pToSend = pChunk->m_pData;
if (m_pTail) {
m_pTail->m_pNext = pChunk;
} else {
m_pHead = pChunk;
}
m_pTail = pChunk;
if (outputRequired) {
OnSend(0);
}
}
// File_checked_for_headers
|