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