forked from YeeYoungHan/cpphttpstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpMessage.cpp
More file actions
522 lines (437 loc) · 13.2 KB
/
HttpMessage.cpp
File metadata and controls
522 lines (437 loc) · 13.2 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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* 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 "HttpStatusCode.h"
#include "HttpMessage.h"
#include <stdlib.h>
#include "MemoryDebug.h"
CHttpMessage::CHttpMessage() : m_iStatusCode(-1), m_iContentLength(-1), m_bChunked(false), m_iStreamIdentifier(0)
{
}
CHttpMessage::~CHttpMessage()
{
}
/**
* @ingroup HttpParser
* @brief HTTP 메시지를 파싱한다.
* @param pszText HTTP 메시지 문자열
* @param iTextLen HTTP 메시지 문자열 길이
* @returns 성공하면 파싱한 문자열 개수를 리턴하고 실패하면 -1 을 리턴한다.
*/
int CHttpMessage::Parse( const char * pszText, int iTextLen )
{
int iCurPos;
iCurPos = ParseHeader( pszText, iTextLen );
if( iCurPos == -1 ) return -1;
if( m_iContentLength > 0 )
{
if( m_iContentLength > ( iTextLen - iCurPos ) ) return -1;
m_strBody.append( pszText + iCurPos, m_iContentLength );
return iTextLen;
}
else if( m_iContentLength == -1 && iTextLen - iCurPos > 0 )
{
m_strBody.append( pszText + iCurPos );
return iTextLen;
}
return iCurPos;
}
/**
* @ingroup HttpParser
* @brief HTTP 메시지의 헤더를 파싱한다.
* @param pszText HTTP 메시지 문자열
* @param iTextLen HTTP 메시지 문자열 길이
* @returns 성공하면 파싱한 문자열 개수를 리턴하고 실패하면 -1 을 리턴한다.
*/
int CHttpMessage::ParseHeader( const char * pszText, int iTextLen )
{
if( pszText == NULL || iTextLen <= 4 ) return -1;
Clear();
int iPos, iCurPos, iNameLen;
const char * pszName, * pszValue;
CHttpHeader clsHeader;
if( !strncmp( pszText, "HTTP/", 4 ) )
{
iCurPos = ParseStatusLine( pszText, iTextLen );
}
else
{
iCurPos = ParseRequestLine( pszText, iTextLen );
}
if( iCurPos == -1 ) return -1;
while( iCurPos < iTextLen )
{
iPos = clsHeader.Parse( pszText + iCurPos, iTextLen - iCurPos );
if( iPos == -1 ) return -1;
iCurPos += iPos;
iNameLen = (int)clsHeader.m_strName.length();
if( iNameLen == 0 ) break;
pszName = clsHeader.m_strName.c_str();
pszValue = clsHeader.m_strValue.c_str();
if( !strcasecmp( pszName, "Content-Type" ) )
{
m_strContentType = pszValue;
}
else if( !strcasecmp( pszName, "Content-Length" ) )
{
m_iContentLength = atoi( pszValue );
}
else
{
if( !strcasecmp( pszName, "transfer-encoding" ) )
{
if( strstr( pszValue, "chunked" ) )
{
m_bChunked = true;
}
}
m_clsHeaderList.push_back( clsHeader );
}
}
return iCurPos;
}
/**
* @ingroup HttpParser
* @brief 내부 변수를 초기화시킨다.
*/
void CHttpMessage::Clear()
{
m_strHttpMethod.clear();
m_strReqUri.clear();
m_strHttpVersion.clear();
m_iStatusCode = -1;
m_strReasonPhrase.clear();
m_strContentType.clear();
m_iContentLength = -1;
m_clsHeaderList.clear();
m_strBody.clear();
}
/**
* @ingroup HttpParser
* @brief HTTP 메시지 문자열을 생성한다.
* @param pszText HTTP 메시지 문자열 저장 변수
* @param iTextSize HTTP 메시지 문자열 저장 변수 크기
* @param bHeaderOnly HTTP 메시지의 헤더만 문자열로 생성하고 싶으면 true 를 저장하고 body 를 포함한 HTTP 메시지를 생성하고 싶으면 false 를 저장한다.
* @returns 성공하면 저장된 HTTP 메시지 문자열 길이를 리턴하고 실패하면 -1 을 리턴한다.
*/
int CHttpMessage::ToString( char * pszText, int iTextSize, bool bHeaderOnly )
{
if( pszText == NULL || iTextSize <= 0 ) return -1;
int iLen;
if( m_strHttpVersion.empty() ) m_strHttpVersion = HTTP_VERSION;
if( m_iStatusCode > 0 )
{
if( m_strReasonPhrase.empty() )
{
m_strReasonPhrase = GetReasonPhrase( m_iStatusCode );
}
iLen = snprintf( pszText, iTextSize, "%s %d %s\r\n", m_strHttpVersion.c_str(), m_iStatusCode, m_strReasonPhrase.c_str() );
}
else
{
if( m_strHttpMethod.empty() || m_strReqUri.empty() || m_strHttpVersion.empty() ) return -1;
iLen = snprintf( pszText, iTextSize, "%s %s %s\r\n", m_strHttpMethod.c_str(), m_strReqUri.c_str(), m_strHttpVersion.c_str() );
}
if( m_strContentType.empty() == false )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "Content-Type: %s\r\n", m_strContentType.c_str() );
}
m_iContentLength = (int)m_strBody.length();
iLen += snprintf( pszText + iLen, iTextSize - iLen, "Content-Length: %d\r\n", m_iContentLength );
for( HTTP_HEADER_LIST::iterator itList = m_clsHeaderList.begin(); itList != m_clsHeaderList.end(); ++itList )
{
iLen += snprintf( pszText + iLen, iTextSize - iLen, "%s: %s\r\n", itList->m_strName.c_str(), itList->m_strValue.c_str() );
}
iLen += snprintf( pszText + iLen, iTextSize - iLen, "\r\n" );
if( m_iContentLength > 0 && bHeaderOnly == false )
{
if( m_iContentLength > ( iTextSize - iLen ) )
{
return -1;
}
memcpy( pszText + iLen, m_strBody.c_str(), m_strBody.length() );
iLen += m_iContentLength;
}
return iLen;
}
/**
* @ingroup HttpParser
* @brief HTTP 메시지 문자열을 생성한다.
* @param strText HTTP 메시지 문자열 저장 변수
* @param bHeaderOnly HTTP 메시지의 헤더만 문자열로 생성하고 싶으면 true 를 저장하고 body 를 포함한 HTTP 메시지를 생성하고 싶으면 false 를 저장한다.
* @returns 성공하면 저장된 HTTP 메시지 문자열 길이를 리턴하고 실패하면 -1 을 리턴한다.
*/
int CHttpMessage::ToString( std::string & strText, bool bHeaderOnly )
{
char szBuf[2048];
strText.clear();
if( m_strHttpVersion.empty() ) m_strHttpVersion = HTTP_VERSION;
if( m_iStatusCode > 0 )
{
if( m_strReasonPhrase.empty() )
{
m_strReasonPhrase = GetReasonPhrase( m_iStatusCode );
}
snprintf( szBuf, sizeof(szBuf), "%s %d %s\r\n", m_strHttpVersion.c_str(), m_iStatusCode, m_strReasonPhrase.c_str() );
}
else
{
if( m_strHttpMethod.empty() || m_strReqUri.empty() || m_strHttpVersion.empty() ) return -1;
snprintf( szBuf, sizeof(szBuf), "%s %s %s\r\n", m_strHttpMethod.c_str(), m_strReqUri.c_str(), m_strHttpVersion.c_str() );
}
strText.append( szBuf );
if( m_strContentType.empty() == false )
{
snprintf( szBuf, sizeof(szBuf), "Content-Type: %s\r\n", m_strContentType.c_str() );
strText.append( szBuf );
}
m_iContentLength = (int)m_strBody.length();
snprintf( szBuf, sizeof(szBuf), "Content-Length: %d\r\n", m_iContentLength );
strText.append( szBuf );
for( HTTP_HEADER_LIST::iterator itList = m_clsHeaderList.begin(); itList != m_clsHeaderList.end(); ++itList )
{
strText.append( itList->m_strName );
strText.append( ": " );
strText.append( itList->m_strValue );
strText.append( "\r\n" );
}
strText.append( "\r\n" );
if( m_iContentLength > 0 && bHeaderOnly == false )
{
strText.append( m_strBody );
}
return (int)strText.length();
}
bool CHttpMessage::AddHeader( const char * pszName, std::string & strValue )
{
if( pszName == NULL ) return false;
CHttpHeader clsHeader( pszName, strValue );
m_clsHeaderList.push_back( clsHeader );
return true;
}
/**
* @ingroup HttpParser
* @brief HTTP 헤더 자료구조에 이름과 값을 추가한다.
* @param pszName HTTP 헤더 이름
* @param pszValue HTTP 헤더 값
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpMessage::AddHeader( const char * pszName, const char * pszValue )
{
if( pszName == NULL || pszValue == NULL ) return false;
CHttpHeader clsHeader( pszName, pszValue );
m_clsHeaderList.push_back( clsHeader );
return true;
}
/**
* @ingroup HttpParser
* @brief HTTP 헤더 자료구조에 이름과 값을 추가한다.
* @param pszName HTTP 헤더 이름
* @param iValue HTTP 헤더 값
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpMessage::AddHeader( const char * pszName, int iValue )
{
char szValue[11];
snprintf( szValue, sizeof(szValue), "%d", iValue );
return AddHeader( pszName, szValue );
}
/**
* @ingroup HttpParser
* @brief HTTP 헤더 자료구조에서 이름을 검색한 후, 해당 헤더가 존재하면 값을 수정한다.
* @param pszName HTTP 헤더 이름
* @param pszValue HTTP 헤더 값
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpMessage::UpdateHeader( const char * pszName, const char * pszValue )
{
HTTP_HEADER_LIST::iterator itList;
for( itList = m_clsHeaderList.begin(); itList != m_clsHeaderList.end(); ++itList )
{
if( !strcasecmp( pszName, itList->m_strName.c_str() ) )
{
itList->m_strValue = pszValue;
return true;
}
}
return false;
}
/**
* @ingroup HttpParser
* @brief HTTP 헤더 자료구조에서 이름을 검색한 후, 해당 헤더가 존재하면 값을 수정하고 존재하지 않으면 새로 추가한다.
* @param pszName HTTP 헤더 이름
* @param pszValue HTTP 헤더 값
* @returns 성공하면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpMessage::ReplaceHeader( const char * pszName, const char * pszValue )
{
if( UpdateHeader( pszName, pszValue ) == false )
{
AddHeader( pszName, pszValue );
}
return true;
}
/**
* @ingroup HttpParser
* @brief 헤더 리스트를 검색하여서 입력된 이름과 일치하는 헤더를 리턴한다.
* @param pszName 헤더 이름
* @returns 헤더 리스트에 존재하면 헤더 객체를 리턴하고 그렇지 않으면 NULL 을 리턴한다.
*/
CHttpHeader * CHttpMessage::GetHeader( const char * pszName )
{
HTTP_HEADER_LIST::iterator itList;
for( itList = m_clsHeaderList.begin(); itList != m_clsHeaderList.end(); ++itList )
{
if( !strcasecmp( pszName, itList->m_strName.c_str() ) )
{
return &(*itList);
}
}
return NULL;
}
/**
* @ingroup HttpParser
* @brief HTTP 요청 메시지에 기본적으로 입력되어야 할 내용을 추가한다.
* @param pszMethod HTTP 메소드
* @param pclsUri HTTP URI
* @param pszUserAgent HTTP User Agent 헤더에 저장될 문자열
* @returns true 를 리턴한다.
*/
bool CHttpMessage::SetRequest( const char * pszMethod, CHttpUri * pclsUri, const char * pszUserAgent )
{
m_strHttpMethod = pszMethod;
if( pclsUri->m_strPath.empty() )
{
m_strReqUri = "/";
}
else
{
m_strReqUri = pclsUri->m_strPath;
}
m_strHttpVersion = HTTP_VERSION;
std::string strHost = pclsUri->m_strHost;
if( pclsUri->m_iPort != 80 && pclsUri->m_iPort != 443 )
{
char szPort[11];
snprintf( szPort, sizeof(szPort), ":%d", pclsUri->m_iPort );
strHost.append( szPort );
}
ReplaceHeader( "Host", strHost.c_str() );
if( pszUserAgent && strlen(pszUserAgent) > 0 )
{
ReplaceHeader( "User-Agent", pszUserAgent );
}
return true;
}
/**
* @ingroup HttpParser
* @brief HTTP 요청 메시지인지 검사한다.
* @returns HTTP 요청 메시지이면 true 를 리턴하고 그렇지 않으면 false 를 리턴한다.
*/
bool CHttpMessage::IsRequest( )
{
if( m_strHttpMethod.empty() == false ) return true;
return false;
}
/**
* @ingroup HttpParser
* @brief HTTP status line 을 파싱한다.
* @param pszText HTTP 헤더의 값을 저장한 문자열
* @param iTextLen pszText 문자열의 길이
* @returns 성공하면 파싱한 길이를 리턴하고 그렇지 않으면 -1 를 리턴한다.
*/
int CHttpMessage::ParseStatusLine( const char * pszText, int iTextLen )
{
int iPos, iStartPos = -1;
char cType = 0;
for( iPos = 0; iPos < iTextLen; ++iPos )
{
if( cType != 2 )
{
if( pszText[iPos] == ' ' )
{
switch( cType )
{
case 0:
m_strHttpVersion.append( pszText, iPos );
break;
case 1:
{
std::string strStatusCode;
strStatusCode.append( pszText + iStartPos, iPos - iStartPos );
m_iStatusCode = atoi( strStatusCode.c_str() );
break;
}
}
iStartPos = iPos + 1;
++cType;
}
}
else
{
if( pszText[iPos] == '\r' )
{
if( iPos + 1 >= iTextLen || pszText[iPos+1] != '\n' ) return -1;
m_strReasonPhrase.append( pszText + iStartPos, iPos - iStartPos );
return iPos + 2;
}
}
}
return -1;
}
/**
* @ingroup HttpParser
* @brief HTTP request line 을 파싱한다.
* @param pszText SIP 헤더의 값을 저장한 문자열
* @param iTextLen pszText 문자열의 길이
* @returns 성공하면 파싱한 길이를 리턴하고 그렇지 않으면 -1 를 리턴한다.
*/
int CHttpMessage::ParseRequestLine( const char * pszText, int iTextLen )
{
int iPos, iStartPos = -1;
char cType = 0;
for( iPos = 0; iPos < iTextLen; ++iPos )
{
if( cType != 2 )
{
if( pszText[iPos] == ' ' )
{
switch( cType )
{
case 0:
m_strHttpMethod.append( pszText, iPos );
break;
case 1:
m_strReqUri.append( pszText + iStartPos, iPos - iStartPos );
break;
}
iStartPos = iPos + 1;
++cType;
}
}
else
{
if( pszText[iPos] == '\r' )
{
if( iPos + 1 >= iTextLen || pszText[iPos+1] != '\n' ) return -1;
m_strHttpVersion.append( pszText + iStartPos, iPos - iStartPos );
return iPos + 2;
}
}
}
return -1;
}