-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathHttpCallBack.cpp
More file actions
349 lines (299 loc) · 9.36 KB
/
HttpCallBack.cpp
File metadata and controls
349 lines (299 loc) · 9.36 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
*
* 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 3 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "HttpCallBack.h"
#include "HttpStatusCode.h"
#include "FileUtility.h"
#include "Directory.h"
#include "Log.h"
#include "UserMap.h"
#include "RtpThread.h"
#include "MemoryDebug.h"
CHttpStack gclsHttpStack;
CHttpCallBack gclsHttpCallBack;
CHttpCallBack::CHttpCallBack() : m_bStop(false)
{
}
CHttpCallBack::~CHttpCallBack()
{
}
/**
* @ingroup TestWebRtc
* @brief HTTP 요청 수신 이벤트 callback
* @param pclsRequest HTTP 요청 메시지
* @param pclsResponse HTTP 응답 메시지 - 응용에서 저장한다.
* @returns 응용에서 HTTP 응답 메시지를 정상적으로 생성하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpCallBack::RecvHttpRequest( CHttpMessage * pclsRequest, CHttpMessage * pclsResponse )
{
std::string strPath = m_strDocumentRoot;
std::string strExt;
//CLog::Print( LOG_DEBUG, "req uri[%s]", pclsRequest->m_strReqUri.c_str() );
// 보안상 .. 을 포함한 URL 을 무시한다.
if( strstr( pclsRequest->m_strReqUri.c_str(), ".." ) )
{
pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
return true;
}
#ifdef _DEBUG
// 메모리 누수 검사를 위해서 exit.html 을 수신하면 프로그램을 종료한다.
if( !strcmp( pclsRequest->m_strReqUri.c_str(), "/exit.html" ) )
{
pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
m_bStop = true;
return true;
}
#endif
if( !strcmp( pclsRequest->m_strReqUri.c_str(), "/" ) )
{
CDirectory::AppendName( strPath, "index.html" );
}
else
{
#ifdef WIN32
ReplaceString( pclsRequest->m_strReqUri, "/", "\\" );
#endif
strPath.append( pclsRequest->m_strReqUri );
}
if( IsExistFile( strPath.c_str() ) == false )
{
pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
return true;
}
// 파일별 Content-Type 을 설정한다.
GetFileExt( strPath.c_str(), strExt );
const char * pszExt = strExt.c_str();
if( !strcmp( pszExt, "html" ) || !strcmp( pszExt, "htm" ) )
{
pclsResponse->m_strContentType = "text/html";
}
else if( !strcmp( pszExt, "css" ) )
{
pclsResponse->m_strContentType = "text/css";
}
else if( !strcmp( pszExt, "js" ) )
{
pclsResponse->m_strContentType = "text/javascript";
}
else if( !strcmp( pszExt, "png" ) || !strcmp( pszExt, "gif" ) )
{
pclsResponse->m_strContentType = "image/";
pclsResponse->m_strContentType.append( pszExt );
}
else if( !strcmp( pszExt, "jpg" ) || !strcmp( pszExt, "jpeg" ) )
{
pclsResponse->m_strContentType = "image/jpeg";
}
else
{
pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
return true;
}
// 파일을 읽어서 HTTP body 에 저장한다.
FILE * fd = fopen( strPath.c_str(), "rb" );
if( fd == NULL )
{
pclsResponse->m_iStatusCode = HTTP_NOT_FOUND;
return true;
}
int n;
char szBuf[8192];
while( ( n = fread( szBuf, 1, sizeof(szBuf), fd ) ) > 0 )
{
pclsResponse->m_strBody.append( szBuf, n );
}
fclose( fd );
pclsResponse->m_iStatusCode = HTTP_OK;
return true;
}
/**
* @ingroup TestWebRtc
* @brief WebSocket 클라이언트 TCP 연결 시작 이벤트 callback
* @param pszClientIp WebSocket 클라이언트 IP 주소
* @param iClientPort WebSocket 클라이언트 포트 번호
*/
void CHttpCallBack::WebSocketConnected( const char * pszClientIp, int iClientPort )
{
printf( "WebSocket[%s:%d] connected\n", pszClientIp, iClientPort );
}
/**
* @ingroup TestWebRtc
* @brief WebSocket 클라이언트 TCP 연결 종료 이벤트 callback
* @param pszClientIp WebSocket 클라이언트 IP 주소
* @param iClientPort WebSocket 클라이언트 포트 번호
*/
void CHttpCallBack::WebSocketClosed( const char * pszClientIp, int iClientPort )
{
printf( "WebSocket[%s:%d] closed\n", pszClientIp, iClientPort );
std::string strUserId;
CUserInfo clsUserInfo;
if( gclsUserMap.SelectUserId( pszClientIp, iClientPort, strUserId ) )
{
if( gclsUserMap.Select( strUserId.c_str(), clsUserInfo ) )
{
gclsUserMap.Delete( strUserId.c_str() );
}
}
}
/**
* @ingroup TestWebRtc
* @brief WebSocket 클라이언트 데이터 수신 이벤트 callback
* @param pszClientIp WebSocket 클라이언트 IP 주소
* @param iClientPort WebSocket 클라이언트 포트 번호
* @param strData WebSocket 클라이언트가 전송한 데이터
* @param pclsSession HTTP 세션 정보
* @returns WebSocket 클라이언트 연결을 유지하려면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpCallBack::WebSocketData( const char * pszClientIp, int iClientPort, std::string & strData, CHttpStackSession * pclsSession )
{
CLog::Print( LOG_NETWORK, "WebSocket[%s:%d] recv[%s]", pszClientIp, iClientPort, strData.c_str() );
printf( "WebSocket[%s:%d] recv[%s]\n", pszClientIp, iClientPort, strData.c_str() );
STRING_VECTOR clsList;
SplitString( strData.c_str(), clsList, '|' );
int iCount = clsList.size();
if( iCount < 2 )
{
return false;
}
bool bReq = true;
if( strcmp( clsList[0].c_str(), "req" ) ) bReq = false;
const char * pszCommand = clsList[1].c_str();
std::string strUserId;
if( !strcmp( pszCommand, "register" ) )
{
if( iCount < 5 )
{
printf( "register request arg is not correct\n" );
return false;
}
if( gclsUserMap.Insert( clsList[2].c_str(), clsList[3].c_str(), clsList[4].c_str(), pszClientIp, iClientPort, pclsSession->m_strUserAgent.c_str() ) == false )
{
Send( pszClientIp, iClientPort, "res|register|500" );
}
else
{
Send( pszClientIp, iClientPort, "res|register|200" );
}
}
else if( !strcmp( pszCommand, "invite" ) )
{
if( bReq )
{
if( iCount < 4 )
{
printf( "invite request arg is not correct\n" );
return false;
}
const char * pszToId = clsList[2].c_str();
const char * pszSdp = clsList[3].c_str();
CUserInfo clsUserInfo;
CRtpThreadArg * pclsRtpArg = new CRtpThreadArg();
if( pclsRtpArg == NULL )
{
Send( pszClientIp, iClientPort, "res|invite|500" );
return true;
}
pclsRtpArg->m_strUserId = strUserId;
pclsRtpArg->m_strToId = pszToId;
pclsRtpArg->m_strSdp = pszSdp;
pclsRtpArg->m_bStartCall = true;
if( pclsRtpArg->CreateSocket() == false ||
gclsUserMap.Update( strUserId.c_str(), pclsRtpArg, false ) == false ||
StartRtpThread( pclsRtpArg ) == false )
{
delete pclsRtpArg;
Send( pszClientIp, iClientPort, "res|invite|500" );
return true;
}
}
else
{
if( iCount < 3 )
{
printf( "invite response arg is not correct\n" );
return false;
}
CUserInfo clsUserInfo;
if( gclsUserMap.SelectUserId( pszClientIp, iClientPort, strUserId ) == false || gclsUserMap.Select( strUserId.c_str(), clsUserInfo ) == false )
{
return true;
}
int iStatus = atoi( clsList[2].c_str() );
if( iStatus == 200 && iCount >= 4 )
{
CRtpThreadArg * pclsRtpArg = new CRtpThreadArg();
if( pclsRtpArg == NULL )
{
Send( pszClientIp, iClientPort, "req|bye" );
return true;
}
const char * pszSdp = clsList[3].c_str();
pclsRtpArg->m_strUserId = strUserId;
pclsRtpArg->m_strSdp = pszSdp;
pclsRtpArg->m_bStartCall = false;
if( pclsRtpArg->CreateSocket() == false ||
gclsUserMap.Update( strUserId.c_str(), pclsRtpArg, false ) == false ||
StartRtpThread( pclsRtpArg ) == false )
{
delete pclsRtpArg;
Send( pszClientIp, iClientPort, "req|bye" );
return true;
}
}
}
}
else if( !strcmp( pszCommand, "bye" ) )
{
std::string strUserId;
CUserInfo clsUserInfo;
if( gclsUserMap.SelectUserId( pszClientIp, iClientPort, strUserId ) == false || gclsUserMap.Select( strUserId.c_str(), clsUserInfo ) == false )
{
Send( pszClientIp, iClientPort, "res|invite|403" );
return true;
}
if( clsUserInfo.m_pclsRtpArg )
{
clsUserInfo.m_pclsRtpArg->m_bStop = true;
gclsUserMap.Update( strUserId.c_str(), clsUserInfo.m_pclsRtpArg, true );
}
}
return true;
}
/**
* @ingroup TestWebRtc
* @brief WebSocket 클라이언트로 패킷을 전송한다.
* @param pszClientIp WebSocket 클라이언트 IP 주소
* @param iClientPort WebSocket 클라이언트 포트 번호
* @param fmt 전송 문자열
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpCallBack::Send( const char * pszClientIp, int iClientPort, const char * fmt, ... )
{
va_list ap;
char szBuf[8192];
int iBufLen;
va_start( ap, fmt );
iBufLen = vsnprintf( szBuf, sizeof(szBuf)-1, fmt, ap );
va_end( ap );
if( gclsHttpStack.SendWebSocketPacket( pszClientIp, iClientPort, szBuf, iBufLen ) )
{
printf( "WebSocket[%s:%d] send[%s]\n", pszClientIp, iClientPort, szBuf );
CLog::Print( LOG_NETWORK, "WebSocket[%s:%d] send[%s]", pszClientIp, iClientPort, szBuf );
return true;
}
return false;
}