Changeset 46 in cheroxy
- Timestamp:
- 11/26/12 15:23:31 (11 years ago)
- Location:
- trunk
- Files:
-
- 8 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/CRXHttpHeader.h
r45 r46 1 1 /** 2 * CRXHttp Message.h2 * CRXHttpHeader.h 3 3 */ 4 4 #ifndef __CRXHTTPMESSAGE_H__ … … 10 10 #define CRLF2 "\r\n\r\n" 11 11 12 class CRXHttp Message12 class CRXHttpHeader 13 13 { 14 14 protected: 15 std::string mHttp Message;15 std::string mHttpHeader; 16 16 std::string mHttpVersion; 17 17 18 18 public: 19 CRXHttp Message(void);20 virtual ~CRXHttp Message(void);19 CRXHttpHeader (void); 20 virtual ~CRXHttpHeader (void); 21 21 22 22 protected: 23 virtual void Parse (void) = 0; 24 virtual void Reset (void) = 0; 23 virtual void Parse (void) = 0; 25 24 26 25 public: 27 void SetMessage (const char * aHttpMessage); 28 std::string GetMessage (void) const; 29 30 void ResetMessage (void); 26 void Set (const char * aHeader); 27 std::string Get (void) const; 31 28 }; 32 29 -
trunk/include/CRXHttpRequest.h
r44 r46 5 5 #define __CRXHTTPREQUEST_H__ 6 6 7 #include "CRXHttp Message.h"7 #include "CRXHttpHeader.h" 8 8 9 class CRXHttpRequest : public CRXHttp Message9 class CRXHttpRequest : public CRXHttpHeader 10 10 { 11 11 private: … … 25 25 void Parse (void); 26 26 void ParseFileExtension (void); 27 void Reset (void);28 27 29 28 public: -
trunk/include/CRXHttpResponse.h
r44 r46 5 5 #define __CRXHTTPRESPONSE_H__ 6 6 7 #include "CRXHttpMessage.h" 7 #include "CRXHttpHeader.h" 8 9 #include <vector> 8 10 9 11 #define ERROR_HTTP_RESPONSE -4000 … … 13 15 #define ERROR_HTTP_RESPONSE_FAILED_TO_PARSE_CONTENT ERROR_HTTP_RESPONSE - 4 14 16 15 class CRXHttpResponse : public CRXHttp Message17 class CRXHttpResponse : public CRXHttpHeader 16 18 { 17 19 private: 18 int mStatusCode; 19 std::string mStatusString; 20 bool mIsChunked; /* Transfer-Encoding */ 21 int mContentLength; 22 23 struct __content_body__ { 24 char * mBody; 25 int mLength; 26 } mContent; 20 int mStatusCode; 21 std::string mStatusString; 22 bool mIsChunked; /* Transfer-Encoding */ 23 unsigned int mContentLength; 24 std::vector <char> mContent; 27 25 28 26 public: … … 33 31 void Parse (void); 34 32 void Reset (void); 35 void ResetContent (void);36 33 37 34 public: … … 39 36 40 37 bool IsChunked (void) const; 41 int SetResponseAll (const char * aHttpResponse, 42 const int aResponseLength); 43 void SetHeader (const char * aHttpResponse); 38 void SetHeader (const char * aHeader); 44 39 std::string GetHeader (void) const; 45 40 int SetContent (const char * aContent, -
trunk/include/CRXProxy.h
r45 r46 7 7 #include "CRXException.h" 8 8 #include "CRXSocket.h" 9 #include "CRXHttp Message.h"9 #include "CRXHttpHeader.h" 10 10 #include "CRXHttpRequest.h" 11 11 #include "CRXHttpResponse.h" -
trunk/src/CRXHttpHeader.cpp
r45 r46 1 1 /** 2 * CRXHttp Message.cpp2 * CRXHttpHeader.cpp 3 3 */ 4 4 #ifdef _WIN32 … … 6 6 #endif 7 7 8 #include "CRXHttp Message.h"8 #include "CRXHttpHeader.h" 9 9 10 CRXHttp Message::CRXHttpMessage(void)10 CRXHttpHeader::CRXHttpHeader (void) 11 11 { 12 12 /*----------------------------------------------------------------*/ … … 14 14 } 15 15 16 CRXHttp Message::~CRXHttpMessage(void)16 CRXHttpHeader::~CRXHttpHeader (void) 17 17 { 18 18 /*----------------------------------------------------------------*/ … … 21 21 22 22 void 23 CRXHttp Message::SetMessage (const char * aHttpMessage)23 CRXHttpHeader::Set (const char * aHeader) 24 24 { 25 25 /*----------------------------------------------------------------*/ 26 if (aH ttpMessage== NULL)26 if (aHeader == NULL) 27 27 return ; 28 28 29 mHttp Message.assign (aHttpMessage);29 mHttpHeader.assign (aHeader); 30 30 31 31 this->Parse (); … … 34 34 35 35 std::string 36 CRXHttp Message::GetMessage(void) const36 CRXHttpHeader::Get (void) const 37 37 { 38 38 /*----------------------------------------------------------------*/ 39 39 /*----------------------------------------------------------------*/ 40 40 41 return mHttp Message;41 return mHttpHeader; 42 42 } 43 44 void45 CRXHttpMessage::ResetMessage (void)46 {47 /*----------------------------------------------------------------*/48 mHttpMessage.clear ();49 /*----------------------------------------------------------------*/50 } -
trunk/src/CRXHttpRequest.cpp
r44 r46 21 21 { 22 22 /*----------------------------------------------------------------*/ 23 Reset ();24 23 /*----------------------------------------------------------------*/ 25 24 } 26 25 27 26 void 28 CRXHttpRequest:: Reset (void)27 CRXHttpRequest::SetHeader (const char * aHeader) 29 28 { 30 29 /*----------------------------------------------------------------*/ 31 ResetMessage (); 32 /*----------------------------------------------------------------*/ 33 } 34 35 void 36 CRXHttpRequest::SetHeader (const char * aHttpRequest) 37 { 38 /*----------------------------------------------------------------*/ 39 SetMessage (aHttpRequest); 30 Set (aHeader); 40 31 /*----------------------------------------------------------------*/ 41 32 } … … 47 38 /*----------------------------------------------------------------*/ 48 39 49 return Get Message();40 return Get (); 50 41 } 51 42 … … 66 57 67 58 /*----------------------------------------------------------------*/ 68 aHttpRequestUrl = GetHeader ().substr (0, mHttp Message.find ('\r'));59 aHttpRequestUrl = GetHeader ().substr (0, mHttpHeader.find ('\r')); 69 60 70 61 /*---------------------------------------------------------------- … … 129 120 } 130 121 131 std::string CRXHttpRequest::GetURL (void) const { return mUrl; } 132 std::string CRXHttpRequest::GetHost (void) const { return mHost; } 133 int CRXHttpRequest::GetPort (void) const { return mPort; } 134 std::string CRXHttpRequest::GetFileExtension (void) const { return mFileExtension; } 122 std::string 123 CRXHttpRequest::GetURL (void) const 124 { 125 /*----------------------------------------------------------------*/ 126 /*----------------------------------------------------------------*/ 127 128 return mUrl; 129 } 130 131 std::string 132 CRXHttpRequest::GetHost (void) const 133 { 134 /*----------------------------------------------------------------*/ 135 /*----------------------------------------------------------------*/ 136 137 return mHost; 138 } 139 140 int 141 CRXHttpRequest::GetPort (void) const 142 { 143 /*----------------------------------------------------------------*/ 144 /*----------------------------------------------------------------*/ 145 146 return mPort; 147 } 148 149 std::string 150 CRXHttpRequest::GetFileExtension (void) const 151 { 152 /*----------------------------------------------------------------*/ 153 /*----------------------------------------------------------------*/ 154 155 return mFileExtension; 156 } -
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 } -
trunk/src/CRXProxy.cpp
r45 r46 91 91 92 92 std::string aHttpHeader; 93 93 94 /*----------------------------------------------------------------*/ 94 95 for (;;) … … 116 117 CRXProxy::SendRequest (void) 117 118 { 118 int aResult = 0; 119 int aSize = mHttpRequest.GetHeader ().length (); 120 121 struct sockaddr_in aSockName; 122 socklen_t aSockLen = sizeof (aSockName); 123 124 /*----------------------------------------------------------------*/ 125 if (!mServer) 126 { 127 /* connect */ 128 aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout); 129 if (aResult < 0) 130 { 131 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER; 132 CRX_ADD_SUBCLASS_ERROR (mServer); 133 CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ()); 134 return aResult; 135 } 136 } 137 138 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aSize); 139 if (aResult != aSize) 119 int aResult = 0; 120 int aLength = mHttpRequest.GetHeader ().length (); 121 122 /*----------------------------------------------------------------*/ 123 aResult = mServer.Connect (mHttpRequest.GetHost (), mHttpRequest.GetPort (), mServerTimeout); 124 if (aResult < 0) 125 { 126 aResult = ERROR_PROXY_FAILED_TO_CONNECT_TO_SERVER; 127 CRX_ADD_SUBCLASS_ERROR (mServer); 128 CRX_ERROR_SET (aResult, "Failed to connect to server <%s>.", mHttpRequest.GetURL ().c_str ()); 129 return aResult; 130 } 131 132 aResult = mServer.Send (mHttpRequest.GetHeader ().c_str (), aLength); 133 if (aResult != aLength) 140 134 { 141 135 CRX_ADD_SUBCLASS_ERROR (mServer); … … 150 144 CRXProxy::ReceiveResponse (void) 151 145 { 152 int aResult = 0; 153 154 const unsigned int aBufferSize = 1024 * 64; 155 char aBuffer[aBufferSize + 1] = {0x00, }; 146 int aResult = 0; 147 char aBuffer; 148 149 std::string aHttpHeader; 150 bool aRemainHeader = true; 156 151 157 152 /*----------------------------------------------------------------*/ 158 153 for (;;) 159 154 { 160 memset (aBuffer, 0x00, sizeof (aBuffer)); 161 162 aResult = mServer.Receive (aBuffer, aBufferSize); 155 aResult = mServer.Receive (&aBuffer, 1); 163 156 if (aResult < 0) 164 157 { … … 169 162 } 170 163 171 aResult = mHttpResponse.SetResponseAll (aBuffer, aResult); 172 if (aResult < 0) 173 { 174 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE; 175 CRX_ERROR_SET (aResult, "Failed to set response."); 176 return aResult; 164 if (aRemainHeader) 165 { 166 aHttpHeader += aBuffer; 167 if (aHttpHeader.rfind (CRLF2) != std::string::npos) 168 { 169 mHttpResponse.SetHeader (aHttpHeader.c_str ()); 170 aRemainHeader = false; 171 } 172 } 173 else 174 { 175 aResult = mHttpResponse.SetContent (&aBuffer, aResult); 176 if (aResult < 0) 177 { 178 aResult = ERROR_PROXY_FAILED_TO_SET_RESPONSE; 179 CRX_ERROR_SET (aResult, "Failed to set response."); 180 return aResult; 181 } 182 else if (aResult == 0) 183 break; 177 184 } 178 185 } … … 185 192 CRXProxy::SendResponse (void) 186 193 { 187 int aResult = 0; 188 int aSize = 0; 189 190 /*----------------------------------------------------------------*/ 191 aSize = mHttpResponse.GetHeader ().length ();; 192 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aSize); 193 if (aResult != aSize) 194 int aResult = 0; 195 int aLength = 0; 196 /*----------------------------------------------------------------*/ 197 aLength = mHttpResponse.GetHeader ().length (); 198 aResult = mClient.Send (mHttpResponse.GetHeader ().c_str (), aLength); 199 if (aResult != aLength) 194 200 { 195 201 CRX_ADD_SUBCLASS_ERROR (mClient); … … 198 204 } 199 205 200 a Size= mHttpResponse.GetContentLength ();201 aResult = mClient.Send (mHttpResponse.GetContentBody (), a Size);202 if (aResult != a Size)206 aLength = mHttpResponse.GetContentLength (); 207 aResult = mClient.Send (mHttpResponse.GetContentBody (), aLength); 208 if (aResult != aLength) 203 209 { 204 210 CRX_ADD_SUBCLASS_ERROR (mClient); -
trunk/src/makefile
r33 r46 15 15 FILES = \ 16 16 CRXException \ 17 CRXHttp Message\17 CRXHttpHeader \ 18 18 CRXHttpRequest \ 19 19 CRXHttpResponse \ -
trunk/windows/cheroxy/cheroxy.vcproj
r32 r46 160 160 </File> 161 161 <File 162 RelativePath="..\..\src\CRXHttp Message.cpp"162 RelativePath="..\..\src\CRXHttpHeader.cpp" 163 163 > 164 164 </File> … … 198 198 </File> 199 199 <File 200 RelativePath="..\..\include\CRXHttp Message.h"200 RelativePath="..\..\include\CRXHttpHeader.h" 201 201 > 202 202 </File>
Note:
See TracChangeset
for help on using the changeset viewer.