Changeset 46 in cheroxy for trunk/src/CRXHttpResponse.cpp
- Timestamp:
- 11/26/12 15:23:31 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/CRXHttpResponse.cpp
r45 r46 36 36 { 37 37 /*----------------------------------------------------------------*/ 38 ResetMessage();39 ResetContent();38 Get ().clear (); 39 mContent.clear (); 40 40 /*----------------------------------------------------------------*/ 41 41 } 42 42 43 43 void 44 CRXHttpResponse:: ResetContent (void)44 CRXHttpResponse::SetHeader (const char * aHeader) 45 45 { 46 46 /*----------------------------------------------------------------*/ 47 if (mContent.mBody) 48 free (mContent.mBody); 49 50 memset (&mContent, 0x00, sizeof (mContent)); 51 /*----------------------------------------------------------------*/ 52 } 53 54 int 55 CRXHttpResponse::SetResponseAll (const char * aHttpResponse, 56 const int aResponseLength) 57 { 58 int aResult = 0; 59 60 const char * aEndOfHeader = NULL; 61 std::string aHeader; 62 63 int aHeaderLength = 0; 64 65 /*----------------------------------------------------------------*/ 66 if (aResponseLength <= 0) 67 return ERROR_HTTP_RESPONSE_INVALID_LENGTH; 68 69 if (GetHeader ().rfind (CRLF2) == std::string::npos) 70 { 71 aEndOfHeader = strstr (aHttpResponse, CRLF2); 72 if (aEndOfHeader == NULL) 73 return ERROR_HTTP_RESPONSE_INVALID_FORMAT; 74 75 aEndOfHeader += strlen (CRLF2); 76 aHeaderLength = aEndOfHeader - aHttpResponse; 77 aHeader.assign (aHttpResponse, aHeaderLength); 78 79 SetHeader (aHeader.c_str ()); 80 } 81 82 aResult = SetContent (aHttpResponse + aHeaderLength, aResponseLength - aHeaderLength); 83 if (aResult) 84 { 85 aResult = ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT; 86 } 87 /*----------------------------------------------------------------*/ 88 89 return aResult; 90 } 91 92 void 93 CRXHttpResponse::SetHeader (const char * aHttpMessage) 94 { 95 /*----------------------------------------------------------------*/ 96 SetMessage (aHttpMessage); 47 Set (aHeader); 97 48 /*----------------------------------------------------------------*/ 98 49 } … … 104 55 /*----------------------------------------------------------------*/ 105 56 106 return Get Message();57 return Get (); 107 58 } 108 59 … … 164 115 const int aLength) 165 116 { 166 int aResult 167 168 char * a ReallocPtr = NULL;117 int aResult = 0; 118 int aSize = 0; 119 char * aChunkedPtr = NULL; 169 120 170 121 /*----------------------------------------------------------------*/ 171 if (!mIsChunked) 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.begin ()._Myptr + aSize, aContent, aLength); 140 141 /*---------------------------------------------------------------- 142 * 4. 143 *----------------------------------------------------------------*/ 144 aResult = mContent.size (); 145 if (mIsChunked) 172 146 { 173 if (mContentLength <= mContent.mLength) 174 return aResult; 175 176 if (mContent.mBody == NULL) 147 mContentLength += aLength; 148 aSize = mContent.size (); 149 if ((unsigned int)aSize > strlen (CRLF) + strlen (CRLF2)) 177 150 { 178 mContent.mBody = static_cast<char *> (calloc (mContentLength, 1));179 if ( mContent.mBody == NULL)151 aChunkedPtr = mContent.begin ()._Myptr + aSize - strlen (CRLF2); 152 if (!strncmp (aChunkedPtr, CRLF2, strlen (CRLF2))) 180 153 { 181 return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION; 154 for ( ; strncmp (aChunkedPtr--, CRLF, strlen (CRLF)) ; ); 155 aResult = strtol (aChunkedPtr, NULL, 16); 182 156 } 183 157 } 184 158 } 185 else186 {187 mContentLength += aLength;188 aReallocPtr = static_cast<char *> (realloc (mContent.mBody, mContentLength));189 if (aReallocPtr == NULL)190 {191 return ERROR_HTTP_RESPONSE_FAILED_TO_MEMORY_ALLOCATION;192 }193 mContent.mBody = aReallocPtr;194 }195 196 memcpy (mContent.mBody + mContent.mLength, aContent, aLength);197 mContent.mLength += aLength;198 159 /*----------------------------------------------------------------*/ 199 160 … … 201 162 } 202 163 203 int CRXHttpResponse::GetStatusCode (void) const { return mStatusCode; } 204 bool CRXHttpResponse::IsChunked (void) const { return mIsChunked; } 205 int CRXHttpResponse::GetContentLength (void) const { return mContentLength; } 206 const char * CRXHttpResponse::GetContentBody (void) const { return mContent.mBody; } 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 }
Note:
See TracChangeset
for help on using the changeset viewer.