forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpStack.cpp
More file actions
235 lines (206 loc) · 6.23 KB
/
HttpStack.cpp
File metadata and controls
235 lines (206 loc) · 6.23 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
/*
* Copyright (C) 2012 Yee Young Han <[email protected]> (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 "HttpStack.h"
#include "HttpStatusCode.h"
#include "Http2Define.h"
#include "Http2Settings.h"
#include "Log.h"
#include "Base64.h"
#include "MemoryDebug.h"
#include "HttpStackHttp2.hpp"
#include "HttpStackWebSocket.hpp"
const EVP_MD * CHttpStack::m_psttMd = NULL;
CHttpStack::CHttpStack() : m_pclsCallBack(NULL)
{
if( m_psttMd == NULL )
{
OpenSSL_add_all_digests();
m_psttMd = EVP_get_digestbyname( "sha1" );
}
}
CHttpStack::~CHttpStack()
{
}
/**
* @ingroup HttpStack
* @brief HTTP 서버를 시작한다.
* @param pclsSetup 설정 객체
* @param pclsCallBack callback 객체
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpStack::Start( CTcpStackSetup * pclsSetup, IHttpStackCallBack * pclsCallBack )
{
if( pclsCallBack == NULL )
{
CLog::Print( LOG_ERROR, "%s IHttpStackCallBack is NULL", __FUNCTION__ );
return false;
}
m_pclsCallBack = pclsCallBack;
return m_clsTcpStack.Start( pclsSetup, this );
}
/**
* @ingroup HttpStack
* @brief
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpStack::Stop( )
{
return m_clsTcpStack.Stop();
}
/**
* @ingroup HttpStack
* @brief openssl 알고리즘에 할당된 메모리를 제거한다.
*/
void CHttpStack::Release()
{
EVP_cleanup();
}
/**
* @ingroup HttpStack
* @brief HTTP 클라이언트가 연결 이벤트 핸들러
* @param pclsSessionInfo 세션 정보
* @return HTTP 클라이언트 연결을 허용하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpStack::InComingConnected( CTcpSessionInfo * pclsSessionInfo )
{
return true;
}
/**
* @ingroup HttpStack
* @brief HTTP 클라이언트 세션이 종료 이벤트 핸들러
* @param pclsSessionInfo 세션 정보
*/
void CHttpStack::SessionClosed( CTcpSessionInfo * pclsSessionInfo )
{
if( pclsSessionInfo->m_pclsApp )
{
CHttpStackSession * pclsApp = (CHttpStackSession *)pclsSessionInfo->m_pclsApp;
if( pclsApp->m_eType == E_HST_WEB_SOCKET )
{
m_pclsCallBack->WebSocketClosed( pclsSessionInfo->m_strIp.c_str(), pclsSessionInfo->m_iPort );
}
}
}
/**
* @ingroup HttpStack
* @brief TCP 패킷 수신 이벤트 핸들러
* @param pszPacket 수신 패킷
* @param iPacketLen 수신 패킷 길이
* @param pclsSessionInfo 세션 정보
* @returns TCP 세션을 유지하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpStack::RecvPacket( char * pszPacket, int iPacketLen, CTcpSessionInfo * pclsSessionInfo )
{
if( pclsSessionInfo->m_pclsApp == NULL )
{
pclsSessionInfo->m_pclsApp = new CHttpStackSession();
if( pclsSessionInfo->m_pclsApp == NULL )
{
CLog::Print( LOG_ERROR, "%s new error", __FUNCTION__ );
return false;
}
}
CHttpStackSession * pclsApp = (CHttpStackSession *)pclsSessionInfo->m_pclsApp;
if( pclsApp->m_eType == E_HST_HTTP_2 )
{
return RecvPacketHttp2( pszPacket, iPacketLen, pclsSessionInfo, pclsApp );
}
if( pclsApp->m_eType == E_HST_WEB_SOCKET )
{
return RecvPacketWebSocket( pszPacket, iPacketLen, pclsSessionInfo, pclsApp );
}
// HTTP 프로토콜
if( pclsApp->m_clsHttpPacket.AddPacket( pszPacket, iPacketLen ) == false )
{
CLog::Print( LOG_ERROR, "%s m_clsPacket.AddPacket error", __FUNCTION__ );
return false;
}
if( pclsApp->m_clsHttpPacket.IsCompleted() )
{
CHttpMessage * pclsRecv = pclsApp->m_clsHttpPacket.GetHttpMessage();
if( pclsRecv->IsRequest() )
{
CHttpMessage clsSend;
CHttpHeader * pclsHeader;
bool bClose = false;
if( !strcmp( pclsRecv->m_strHttpMethod.c_str(), "PRI" ) && !strcmp( pclsRecv->m_strReqUri.c_str(), "*" ) && !strncmp( pclsRecv->m_strBody.c_str(), "SM", 2 ) )
{
// HTTP/2 프로토콜
return RecvPacketHttp2Pri( pclsSessionInfo, pclsApp );
}
pclsHeader = pclsRecv->GetHeader( "Upgrade" );
if( pclsHeader && !strcmp( pclsHeader->m_strValue.c_str(), "websocket" ) )
{
// WebSocket 을 위한 HTTP 요청이면 응용으로 callback 호출하지 않고 응답 메시지를 전송한다.
if( MakeWebSocketResponse( pclsRecv, &clsSend ) == false )
{
return false;
}
pclsApp->m_eType = E_HST_WEB_SOCKET;
}
else
{
pclsHeader = pclsRecv->GetHeader( "Connection" );
if( pclsHeader == NULL || !strcmp( pclsHeader->m_strValue.c_str(), "close" ) )
{
bClose = true;
}
if( m_pclsCallBack->RecvHttpRequest( pclsRecv, &clsSend ) == false )
{
CLog::Print( LOG_ERROR, "%s RecvHttpRequest error", __FUNCTION__ );
return false;
}
}
int iNewBufLen = 8192 + (int)clsSend.m_strBody.length();
char * pszBuf = (char *)malloc( iNewBufLen );
if( pszBuf == NULL )
{
CLog::Print( LOG_ERROR, "%s malloc error", __FUNCTION__ );
return false;
}
int iBufLen = clsSend.ToString( pszBuf, iNewBufLen );
if( iBufLen == -1 )
{
CLog::Print( LOG_ERROR, "%s ToString error", __FUNCTION__ );
free( pszBuf );
return false;
}
if( pclsSessionInfo->Send( pszBuf, iBufLen ) == false )
{
CLog::Print( LOG_ERROR, "%s Send error", __FUNCTION__ );
free( pszBuf );
return false;
}
free( pszBuf );
if( bClose )
{
return false;
}
if( pclsApp->m_eType == E_HST_WEB_SOCKET )
{
m_pclsCallBack->WebSocketConnected( pclsSessionInfo->m_strIp.c_str(), pclsSessionInfo->m_iPort, pclsRecv );
}
}
else
{
CLog::Print( LOG_ERROR, "%s http request error", __FUNCTION__ );
return false;
}
}
return true;
}