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