source: cheroxy/trunk/src/CRXHttpResponse.cpp@ 54

Last change on this file since 54 was 54, checked in by cheese, 11 years ago

#1 remove unnecessary memset

File size: 5.6 KB
Line 
1/**
2 * CRXHttpResponse.cpp
3 */
4#ifdef _WIN32
5# pragma warning (disable:4996)
6#endif
7
8#include "CRXHttpResponse.h"
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13
14#define CONTENT_LENGTH "Content-Length: "
15#define TRANSFER_ENCODING "Transfer-Encoding: "
16
17CRXHttpResponse::CRXHttpResponse (void)
18 : mStatusCode (0),
19 mIsChunked (false),
20 mContentLength (0)
21{
22 /*----------------------------------------------------------------*/
23 /*----------------------------------------------------------------*/
24}
25
26CRXHttpResponse::~CRXHttpResponse (void)
27{
28 /*----------------------------------------------------------------*/
29 Reset ();
30 /*----------------------------------------------------------------*/
31}
32
33void
34CRXHttpResponse::Reset (void)
35{
36 /*----------------------------------------------------------------*/
37 Get ().clear ();
38 mContent.clear ();
39 /*----------------------------------------------------------------*/
40}
41
42void
43CRXHttpResponse::SetHeader (const char * aHeader)
44{
45 /*----------------------------------------------------------------*/
46 Set (aHeader);
47 /*----------------------------------------------------------------*/
48}
49
50std::string
51CRXHttpResponse::GetHeader (void) const
52{
53 /*----------------------------------------------------------------*/
54 /*----------------------------------------------------------------*/
55
56 return Get ();
57}
58
59void
60CRXHttpResponse::Parse (void)
61{
62 std::string aValue;
63
64 char aStatusCode[64] = {0x00, };
65 char aStatusName[64] = {0x00, };
66 char aHttpVersion[64] = {0x00, };
67
68 size_t aBegin = 0;
69 size_t aOffset = 0;
70
71 /*----------------------------------------------------------------*/
72 aValue = GetHeader ().substr (0, GetHeader ().find (CRLF, 0));
73
74 /*----------------------------------------------------------------
75 * 1. separate first line to <METHOD>, <URL> and <VERSION>
76 *----------------------------------------------------------------*/
77 sscanf (aValue.c_str (), "%[^ ] %[^ ] %[^ ]\r\n", aHttpVersion, aStatusCode, aStatusName);
78 mHttpVersion.assign (aHttpVersion);
79 mStatusString.assign (aStatusName);
80 mStatusCode = atoi (aStatusCode);
81
82 /*----------------------------------------------------------------
83 * 2. get Content-Length or check is chunked
84 *----------------------------------------------------------------*/
85 aBegin = GetHeader ().find (CONTENT_LENGTH);
86 if (aBegin == std::string::npos)
87 {
88 /* is really chunked encoding ? */
89 aBegin = GetHeader ().find (TRANSFER_ENCODING);
90 if (aBegin == std::string::npos)
91 return ;
92
93 aBegin += strlen (TRANSFER_ENCODING);
94 aOffset = GetHeader ().find (CRLF, aBegin) - aBegin;
95
96 aValue = GetHeader ().substr (aBegin, aOffset);
97 if (aValue == "chunked")
98 {
99 mIsChunked = true;
100 return ;
101 }
102 }
103
104 aBegin += strlen (CONTENT_LENGTH);
105 aOffset = GetHeader ().find (CRLF, aBegin) - aBegin;
106
107 aValue = GetHeader ().substr (aBegin, aOffset);
108 mContentLength = atoi (aValue.c_str ());
109 /*----------------------------------------------------------------*/
110}
111
112int
113CRXHttpResponse::SetContent (const char * aContent,
114 const int aLength)
115{
116 int aResult = 0;
117 unsigned int aSize = 0;
118 char * aChunkedPtr = NULL;
119
120 /*----------------------------------------------------------------*/
121 /*----------------------------------------------------------------
122 * 1. check parameter
123 *----------------------------------------------------------------*/
124 if (aLength < 0)
125 return ERROR_HTTP_RESPONSE_INVALID_LENGTH;
126
127 /*----------------------------------------------------------------
128 * 2. check content size when is NOT chunked-encoding
129 *----------------------------------------------------------------*/
130 if (!mIsChunked && mContent.size () >= mContentLength)
131 return 0;
132
133 /*----------------------------------------------------------------
134 * 3. resize memory and copy
135 *----------------------------------------------------------------*/
136 aSize = mContent.size ();
137 mContent.resize (aSize + aLength);
138 memcpy (&mContent[0] + aSize, aContent, aLength);
139
140 /*----------------------------------------------------------------
141 * 4.
142 *----------------------------------------------------------------*/
143 aResult = mContent.size ();
144 if (mIsChunked)
145 {
146 mContentLength += aLength;
147 aSize = mContent.size ();
148 if (aSize > strlen (CRLF) + strlen (CRLF2))
149 {
150 aChunkedPtr = &mContent[0] + aSize - strlen (CRLF2);
151 if (!strncmp (aChunkedPtr, CRLF2, strlen (CRLF2)))
152 {
153 for ( ; strncmp (aChunkedPtr--, CRLF, strlen (CRLF)) ; );
154 aResult = strtol (aChunkedPtr, NULL, 16);
155 }
156 }
157 }
158 /*----------------------------------------------------------------*/
159
160 return aResult;
161}
162
163int
164CRXHttpResponse::GetStatusCode (void) const
165{
166 /*----------------------------------------------------------------*/
167 /*----------------------------------------------------------------*/
168
169 return mStatusCode;
170}
171
172bool
173CRXHttpResponse::IsChunked (void) const
174{
175 /*----------------------------------------------------------------*/
176 /*----------------------------------------------------------------*/
177
178 return mIsChunked;
179}
180
181int
182CRXHttpResponse::GetContentLength (void) const
183{
184 /*----------------------------------------------------------------*/
185 /*----------------------------------------------------------------*/
186
187 return mContentLength;
188}
189
190const char *
191CRXHttpResponse::GetContentBody (void) const
192{
193 /*----------------------------------------------------------------*/
194 /*----------------------------------------------------------------*/
195
196 return mContent.empty () ? "" : &mContent[0];
197}
Note: See TracBrowser for help on using the repository browser.