Mã:
#include "stdafx.h" #include "https.h" #define BUFFSIZE 4096 /* 4Kbytes */#define MIN(a,b) (a<b)?a:b#define HTTP_UserAgent "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 5.1)"#define HTTP_Version "1.1"#define HTTP_Accept "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"#define HTTP_AcceptLanguage "en-us"#define HTTP_AcceptCharset "UTF-8,*" HTTPS::HTTPS(){ szHost = NULL; szCookie = NULL; // Lưu Cookie szHeader = NULL; szBody = NULL; fileAttach = NULL; fileResponse = NULL; lTimeOut = 10000; szAccept = _strdup(HTTP_Accept); szAcceptCharset = _strdup(HTTP_AcceptCharset); szAcceptLanguage = _strdup(HTTP_AcceptLanguage); szUserAgent = _strdup(HTTP_UserAgent); szVersion = _strdup(HTTP_Version); lBodyLength = 0; bKeepAlive = false; bUseSSL = false; WSADATA SData; WSAStartup(0x0202,&SData);//Không thể khởi động Winsock, thoát.} HTTPS::~HTTPS(){ delete [] szAccept; szAccept = NULL; delete [] szAcceptCharset; szAcceptCharset = NULL; delete [] szAcceptLanguage; szAcceptLanguage = NULL; delete [] szUserAgent; szUserAgent = NULL; delete [] szVersion; szVersion = NULL; delete [] szHost; szHost = NULL; delete [] szHeader; szHeader = NULL; delete [] szBody; szBody = NULL; delete [] szCookie; szCookie = NULL; if (bUseSSL) { SSL_free(ssl); // Nếu có kết nối SSL thì đóng. ssl = NULL; }}int HTTPS::CreateRequest(char*& pPack,const char* pMethod,const char* pRequest) const { int iLen = strlen(pMethod) + strlen(pRequest)+ strlen(szVersion)+ strlen(pPack) + 12; char *pRet = NULL; pRet = new char[iLen];#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) sprintf_s(pRet, iLen * sizeof(char), "%s %s HTTP/%s
%s
\0", pMethod, pRequest, szVersion, pPack);#else sprintf(pRet, "%s %s HTTP/%s
%s
\0", pMethod, pRequest, szVersion, pPack);#endif delete [] pPack; pPack = pRet; return iLen - 1;}void HTTPS::RequestField(char*& pPack,const char* pKey,const char* pValue) const{ char *pPos = NULL; unsigned int iOldLen = 0; if (pValue) { if (pPack) iOldLen = strlen(pPack); pPack = (char*)realloc(pPack,iOldLen + strlen(pKey) + strlen(pValue) + 3); pPos = pPack + iOldLen; while(*pKey) *(pPos++) = *(pKey++); while(*pValue) *(pPos++) = *(pValue++); *(pPos++) = '
'; *(pPos++) = '
'; *(pPos ) = '\0'; }}char* HTTPS::RequestField(const char* pPos,const char* pKey) const{ char *pBegin = NULL; char *pEnd = NULL; char *pKeyCopy = NULL; char *pRet = NULL; int iLen = 0; if (pKey != NULL) { iLen = strlen(pKey); pKeyCopy = new char [iLen + 2]; memcpy(pKeyCopy, pKey, iLen * sizeof(char)); pKeyCopy[iLen ] = ':'; pKeyCopy[iLen + 1] = '\0'; if ((pBegin = strstr((char*)pPos,pKeyCopy))!=NULL) { pBegin += strlen(pKeyCopy); while((*pBegin == ' ') && (*(pBegin + 1) != '\0')) pBegin++; if ((pEnd = strchr(pBegin,'
')) != NULL) { iLen = pEnd - pBegin; if (iLen > 0) { pRet = new char[iLen + 1]; memcpy(pRet, pBegin, iLen * sizeof(char)); pRet[iLen] = '\0'; } } } delete [] pKeyCopy; pKeyCopy = NULL; } return pRet;}int HTTPS::Open(){ SSL_CTX *ctx = NULL; int iRet=0; iSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); // Khởi tạo Socket if(iSocket != INVALID_SOCKET) { HOSTENT *hostInfo = gethostbyname(szHost); // Nhờ DNS phân giải địa chỉ. if(hostInfo != NULL) { SOCKADDR_IN SockAddr; SockAddr.sin_port = htons(iPort); SockAddr.sin_family = AF_INET; SockAddr.sin_addr.s_addr = (*(DWORD*)hostInfo->h_addr_list[0]); iRet = connect(iSocket,(SOCKADDR*)(&SockAddr), sizeof(SockAddr)); // Kết nối tới host. if (iRet == SOCKET_ERROR) { //Lỗi... wprintf(L"=CRITICAL= | connect() failed with error code %d, WSAGetLastError = %d
", iRet, WSAGetLastError()); } if (iRet == 0) { if (bUseSSL) { SSL_library_init(); ctx = SSL_CTX_new(SSLv23_client_method()); ssl = SSL_new(ctx); SSL_CTX_free(ctx); SSL_set_fd(ssl,iSocket); iRet = SSL_get_error(ssl,SSL_connect(ssl)); if (iRet == SSL_ERROR_SSL) return 0; } bConnected = true; return iSocket; } } } else { //Lỗi... wprintf(L"=CRITICAL= | socket() failed with error code %d, WSAGetLastError = %d
", iRet, WSAGetLastError()); } return 0;}char* HTTPS::Host() const{ unsigned int iLen = 0; char *pRet = NULL; if (szHost!=NULL) { iLen = strlen(szHost); pRet = new char[iLen + 1]; memcpy(pRet, szHost, iLen * sizeof(char)); pRet[iLen] = '\0'; } return pRet;}bool HTTPS::Host(const char* pHost){ unsigned int iLen = 0; if (pHost != NULL) { delete [] szHost; szHost = NULL; iLen = strlen(pHost); szHost = new char[iLen + 1]; memcpy(szHost, pHost, iLen * sizeof(char)); szHost[iLen] = '\0'; return true; } return false;}char* HTTPS::Header() const{ unsigned int iLen = 0; char *pRet = NULL; if (szHeader != NULL) { iLen = strlen(szHeader); pRet = new char[iLen + 1]; memcpy(pRet, szHeader, iLen * sizeof(char)); pRet[iLen] = '\0'; } return pRet;}char* HTTPS::HeaderField(const char* pFieldName,int& iPos) const{ char *pPos = NULL; char *pValue = NULL; if (pFieldName != NULL) { pPos = szHeader + iPos; pValue = RequestField(pPos, pFieldName); if (pValue != NULL) { pPos = strstr(pPos, pFieldName) + strlen(pFieldName) + strlen(pValue) + 1; iPos = pPos - szHeader; return pValue; } } return NULL; }bool HTTPS::Body(const char* pBody,long lLength){ if (pBody != NULL) { delete [] szBody; szBody = new char[lLength + 1]; memcpy(szBody, pBody, lLength * sizeof(char)); szBody[lLength] = '\0'; return true; } return false;}char* HTTPS::Body(long& lLength) const{ char *pRet = NULL; lLength = 0; if (szBody != NULL) { pRet = new char[lBodyLength + 1]; memcpy(pRet, szBody, lBodyLength * sizeof(char)); pRet[lBodyLength] = '\0'; lLength = lBodyLength; return pRet; } return NULL;}bool HTTPS::Version(const char* pVersion){ if (pVersion != NULL) { delete [] szVersion; int iLen = strlen(pVersion); szVersion = new char[iLen + 1]; memcpy(szVersion, pVersion, iLen * sizeof(char)); szVersion[iLen] = '\0'; return true; } return false;}char* HTTPS::Version() const{ char *pRet = NULL; if (szVersion != NULL) { int iLen = strlen(szVersion); pRet = new char[iLen + 1]; memcpy(pRet, szVersion, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } return NULL;}bool HTTPS::UserAgent(const char *pUserAgent){ if (pUserAgent) { delete [] szUserAgent; int iLen = strlen(pUserAgent); szUserAgent = new char[iLen + 1]; memcpy(szUserAgent, pUserAgent, iLen * sizeof(char)); szUserAgent[iLen] = '\0'; return true; } return false;}char* HTTPS::UserAgent() const{ char *pRet = NULL; if (szUserAgent != NULL) { int iLen = strlen(szUserAgent); pRet = new char[iLen + 1]; memcpy(pRet,szUserAgent, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } return NULL;}bool HTTPS::Accept(const char* pAccept){ if (pAccept != NULL) { delete [] szAccept; int iLen = strlen(pAccept); szAccept = new char[iLen + 1]; memcpy(szAccept, pAccept, iLen * sizeof(char)); szAccept[iLen] = '\0'; return true; } return false;}char* HTTPS::Accept() const{ if (szAccept != NULL) { char *pRet = NULL; int iLen = strlen(szAccept); pRet = new char[iLen + 1]; memcpy(pRet,szAccept, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } return NULL;}bool HTTPS::AcceptLanguage(const char* pAcceptLanguage){ if (pAcceptLanguage) { delete [] szAcceptLanguage; int iLen = strlen(pAcceptLanguage); szAcceptLanguage = new char[iLen + 1]; memcpy(szAcceptLanguage, pAcceptLanguage, iLen * sizeof(char)); szAcceptLanguage[iLen] = '\0'; return true; } return false;}char* HTTPS::AcceptLanguage() const{ if (szAcceptLanguage!=NULL) { char *pRet = NULL; int iLen = strlen(szAcceptLanguage); pRet = new char[iLen + 1]; memcpy(pRet,szAcceptLanguage, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } return NULL;}bool HTTPS::AcceptCharset(const char* pAcceptCharset){ if (pAcceptCharset) { delete [] szAcceptCharset; int iLen = strlen(pAcceptCharset); szAcceptCharset = new char[iLen + 1]; memcpy(szAcceptCharset, pAcceptCharset, iLen * sizeof(char)); szAcceptCharset[iLen] = '\0'; return true; } return false;}char* HTTPS::AcceptCharset() const{ if (szAcceptCharset!=NULL) { char *pRet = NULL; int iLen = strlen(szAcceptCharset); pRet = new char[iLen + 1]; memcpy(pRet,szAcceptCharset, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } return NULL;}bool HTTPS::Cookie(const char* pKey,const char* pValue){ if ((pKey != NULL) && (pValue != NULL)) { unsigned int iLen = 0; char *pPos = NULL; char *pTMP = NULL; iLen = strlen(pKey) + strlen(pValue) + 3; // Thêm một ký tự '=', một ký tự ';' và một ký tự ' ' if (szCookie == NULL) { pTMP = new char[iLen + 1]; pPos = pTMP; } else { iLen += strlen(szCookie); pTMP = new char[iLen + 1]; pPos = pTMP; memcpy(pPos, szCookie, strlen(szCookie) * sizeof(char)); pPos += strlen(szCookie); delete [] szCookie;// Giải phóng bộ nhớ đã cấp phát. } memcpy(pPos, pKey, strlen(pKey) * sizeof(char)); pPos+=strlen(pKey); *(pPos++) = '='; memcpy(pPos, pValue, strlen(pValue) * sizeof(char)); pPos+=strlen(pValue); memcpy(pPos, "; ", 2 * sizeof(char)); pTMP[iLen] = '\0'; szCookie = pTMP; return true; } return false;}char* HTTPS::Cookie(const char* pKey) const{ if ((pKey != NULL) && (szCookie != NULL)) { char *pKeyCopy = NULL; char *pRet = NULL; char *beginPos = NULL; char *endPos = NULL; unsigned int iLen = 0; iLen = strlen(pKey);// Lấy chiều dài khóa. pKeyCopy = new char[iLen + 2]; memcpy(pKeyCopy, pKey, iLen * sizeof(char)); pKeyCopy[iLen ] = '='; pKeyCopy[iLen + 1] = '\0'; if (beginPos = strstr(szCookie, pKeyCopy)) // Kiểm tra xem có khóa Cookie cần tìm không. { beginPos += strlen(pKeyCopy);// Di chuyển đến vị trí bắt đầu của giá trị. endPos = strchr(beginPos, ';');// Vị trí kết thúc Cookie. iLen = endPos - beginPos;// Lấy chiều dài của giá trị. if (iLen > 0) { pRet = new char[iLen + 1]; memcpy(pRet, beginPos, iLen * sizeof(char)); pRet[iLen] = '\0'; return pRet; } } delete [] pKeyCopy; } return NULL;}bool HTTPS::Request(const char* szMethod,const char* szRequest,bool bAttachBody,bool bAttachFromFile,bool bResponseToFile){ if ((bConnected) && (szMethod != NULL)) { int iLen = 0; int iRet = 0; long lFileLength = 0; char *pTMP = NULL; RequestField(pTMP, "Host: ", szHost); RequestField(pTMP, "User-Agent: ", szUserAgent); RequestField(pTMP, "Accept: ", szAccept); RequestField(pTMP, "Accept-Charset: ",szAcceptCharset); RequestField(pTMP, "Accept-Language: ", szAcceptLanguage); RequestField(pTMP, "Connection: ", bKeepAlive?"Keep-Alive":"Close"); RequestField(pTMP, "Cookie: ", szCookie); if (bAttachBody) { char *pTMP3 = NULL; long lLength = 0; pTMP3 = new char[12];// Kiểu long có tối đa 10 ký số, thêm một dấu "-" và một ký tự kết thúc. memset(pTMP3, 0, 12 * sizeof(char)); if (bAttachFromFile) { if (fileAttach != NULL) { fseek(fileAttach, 0, SEEK_END); lFileLength = ftell(fileAttach); fseek(fileAttach, 0, SEEK_SET); lLength = lFileLength; } } else { if (szBody != NULL) lLength = lBodyLength; }#if defined(_MSC_VER) && (_MSC_VER >= 1400 ) _itoa_s(lLength, pTMP3, 11 * sizeof(char), 10);#else _itoa(lLength, pTMP3, 10);#endif RequestField(pTMP, "Content-Length: ", pTMP3); RequestField(pTMP, "Content-Type: ", "application/x-www-form-urlencoded"); delete [] pTMP3; } iLen = CreateRequest(pTMP, szMethod, szRequest); if (bUseSSL) iRet = SSL_write(ssl, pTMP, iLen); else iRet = send(iSocket, pTMP, iLen, 0); delete [] pTMP; pTMP = NULL; if (iLen == iRet) { if (bAttachBody) { char *pPos = NULL; int iLen = 0; long lSize = 0; if (bAttachFromFile) { lSize = lFileLength; pPos = new char[BUFFSIZE]; fread(pPos, MIN(BUFFSIZE, lSize) * sizeof(char), 1, fileAttach); } else { lSize = lBodyLength; pPos = szBody; } while (lSize > 0) { iLen = MIN(BUFFSIZE, lSize); if (bUseSSL) iRet = SSL_write(ssl, pPos, iLen); else iRet = send(iSocket, pPos, iLen, 0); if (iLen != iRet) return false; lSize -= iLen; if (bAttachFromFile) fread(pPos, MIN(BUFFSIZE, lSize) * sizeof(char), 1, fileAttach); else pPos += iLen; } if (bAttachFromFile) delete [] pPos; } if (receivelData(bResponseToFile) && getstatusCode() && processCookie()) return true; } } return false;}bool HTTPS::processCookie(){ if (szHeader != NULL)// Không thể lấy Cookie khi phần Header trống. { char *pPos = NULL; char *pPos1 = NULL; char *pPos2 = NULL; char *pTMP = NULL; char *pBegin = NULL; char *pEnd = NULL; char *pValue = NULL; unsigned int iLen = 0; unsigned int iOldLen = 0; delete [] szCookie; pPos = szHeader; while((pValue = RequestField(pPos, "Set-Cookie")) != NULL) { iLen = strcspn(pValue, ";"); if (pTMP != NULL) iOldLen = strlen(pTMP); pTMP = (char*)realloc(pTMP,iOldLen + iLen + 3); memcpy(pTMP + iOldLen, pValue, iLen * sizeof(char)); pTMP[iLen + iOldLen ] = ';'; pTMP[iLen + iOldLen + 1] = ' '; pTMP[iLen + iOldLen + 2] = '\0'; pPos = strstr(pPos, "Set-Cookie") + strlen(pValue) + 12; delete [] pValue; pValue = NULL; } if (pTMP != NULL) { iLen = strlen(pTMP); szCookie = new char[iLen + 1]; memcpy(szCookie, pTMP, iLen * sizeof(char)); szCookie[iLen] = '\0'; delete [] pTMP; pTMP = NULL; } return true; } return false;}bool HTTPS::receivelData(bool bFile){ int iRet = 0; fd_set fdread; timeval timeout; /* Thời gian chờ server trả lời. */ timeout.tv_sec = lTimeOut / 1000; timeout.tv_usec = lTimeOut % 1000; /* Xóa rỗng tập trước khi gọi select() */ FD_ZERO(&fdread); /* Đặt Socket vào tập hợp. */ FD_SET(iSocket, &fdread); if ((iRet = select(0, &fdread, NULL, NULL, &timeout)) == SOCKET_ERROR) { //Lỗi... wprintf(L"=CRITICAL= | select() failed with error code %d, WSAGetLastError = %d
", iRet, WSAGetLastError()); } if (iRet > 0) { if (FD_ISSET(iSocket,&fdread)) { char *pTMP = NULL; /* Nhận phần Header */ bool bDone = false; bool bEndHeader = false; char HeaderResponse; unsigned int iLen = 0; long bytesRead = 0; while(!bDone) { if (bUseSSL) bytesRead = SSL_read(ssl, &HeaderResponse, 1); else bytesRead = recv(iSocket, &HeaderResponse, 1, 0); if(bytesRead <= 0) bDone = true; switch(HeaderResponse) { case '
': break; case '
': // Nếu gặp "
" hay "
" thì kết thúc Header if (bEndHeader) bDone = true; bEndHeader = true; break; default: bEndHeader = false; break; } pTMP = (char*)realloc(pTMP, iLen + 2); *(pTMP + iLen++ ) = HeaderResponse; *(pTMP + iLen ) = '\0'; } delete [] szHeader; szHeader = new char[iLen + 1]; memcpy(szHeader, pTMP, iLen * sizeof(char)); szHeader[iLen] = '\0'; delete [] pTMP; pTMP = NULL; /* Nhận phần Body. */ char *pBodyResponse = new char[BUFFSIZE];; bool bChunked = false; bool bExit = false; long BuffSize = 0; ChunkState stt; stt.Chunk = false; stt.ChunkLength = 0; /* Nếu con trỏ tập tin bằng NULL thì không ghi vào tập tin. */ if (fileResponse == NULL) bFile = false; /* Đưa con trỏ về đâu tập tin */ if (bFile) fseek(fileResponse, 0, SEEK_SET); pTMP = RequestField(szHeader,"Transfer-Encoding"); if (pTMP != NULL) { bChunked = (strcmp(pTMP,"chunked") == 0); delete[] pTMP; pTMP = NULL; } do { /* Xóa rỗng tập trước khi gọi select() */ FD_ZERO(&fdread); /* Đặt Socket vào tập hợp. */ FD_SET(iSocket, &fdread); if ((iRet = select(0, &fdread, NULL, NULL, &timeout)) == SOCKET_ERROR) { //Lỗi... wprintf(L"=CRITICAL= | select() failed with error code %d, WSAGetLastError = %d
", iRet, WSAGetLastError()); break; } if (iRet > 0) if (FD_ISSET(iSocket, &fdread)) { /* Xóa sạch dữ liệu trước khi nhận dữ liệu mới. */ memset(pBodyResponse, 0, BUFFSIZE); if (bUseSSL) /* Đọc BUFFSIZE Bytes. */ bytesRead = SSL_read(ssl, pBodyResponse, BUFFSIZE); else /* Đọc BUFFSIZE Bytes. */ bytesRead = recv(iSocket, pBodyResponse, BUFFSIZE, 0); /* Không đọc được dữ liệu hoặc kết nối bị lỗi thì thoát. */ if (bytesRead <= 0) break; /* Giải mã Chunk lúc run-time. */ if (bChunked) DecodeChunked(&stt, pBodyResponse, bytesRead, bExit); if (bFile) /* Ghi dữ liệu vào tập tin. */ fwrite(pBodyResponse, bytesRead * sizeof(char), 1, fileResponse); else { /* Chép dữ liệu vừa nhận được vào vùng nhớ tạm. */ pTMP = (char*)realloc(pTMP, BuffSize + bytesRead + 1); memcpy(pTMP + BuffSize, pBodyResponse, bytesRead * sizeof(char)); pTMP[BuffSize + bytesRead] = '\0'; BuffSize += bytesRead; } } else break; } while ((iRet > 0) && !bExit); delete [] pBodyResponse; delete [] szBody; szBody = NULL; lBodyLength = 0; /* Nếu ghi vào tập tin hoặc không nhận được phần Body thì vùng nhớ tạm sẽ không được cấp phát. */ if (pTMP != NULL) { szBody = new char[BuffSize + 1]; memcpy(szBody, pTMP, BuffSize * sizeof(char)); szBody[BuffSize] = '\0'; lBodyLength = BuffSize; delete [] pTMP; } return true; } } return false;}bool HTTPS::getstatusCode(){ iStatusCode = 0; if (szHeader != NULL) { char *pPos = NULL; pPos = szHeader; while((*(pPos++) != ' ') && (*pPos != '\0')); iStatusCode = atoi(pPos); return true; } return false;}void HTTPS::Clear(){ iStatusCode = 0; lBodyLength = 0; delete [] szHeader; szHeader = NULL; delete [] szBody; szBody = NULL;}void HTTPS::Default(){ delete [] szAccept; szAccept = NULL; delete [] szAcceptCharset; szAcceptCharset = NULL; delete [] szAcceptLanguage; szAcceptLanguage = NULL; delete [] szUserAgent; szUserAgent = NULL; delete [] szVersion; szVersion = NULL; lTimeOut = 10000; szAccept = _strdup(HTTP_Accept); szAcceptCharset = _strdup(HTTP_AcceptCharset); szAcceptLanguage = _strdup(HTTP_AcceptLanguage); szUserAgent = _strdup(HTTP_UserAgent); szVersion = _strdup(HTTP_Version);}void HTTPS::Reset(){ delete [] szHost; szHost = NULL; delete [] szHeader; szHeader = NULL; delete [] szBody; szBody = NULL; delete [] szCookie; szCookie = NULL; iStatusCode = 0; lBodyLength = 0; bKeepAlive = false; bUseSSL = false; bConnected = false; if (bUseSSL) { SSL_free(ssl); // Nếu có kết nối SSL thì đóng. ssl = NULL; } if(iSocket != 0) { shutdown(iSocket,SD_BOTH); // Đóng kết nối closesocket(iSocket); // Hủy Socket }}void HTTPS::Close(bool bClearAll){ if (bClearAll) { delete [] szAccept; szAccept = NULL; delete [] szAcceptCharset; szAcceptCharset = NULL; delete [] szAcceptLanguage; szAcceptLanguage = NULL; delete [] szUserAgent; szUserAgent = NULL; delete [] szVersion; szVersion = NULL; lTimeOut = 10000; szAccept = _strdup(HTTP_Accept); szAcceptCharset = _strdup(HTTP_AcceptCharset); szAcceptLanguage = _strdup(HTTP_AcceptLanguage); szUserAgent = _strdup(HTTP_UserAgent); szVersion = _strdup(HTTP_Version); iStatusCode = 0; lBodyLength = 0; delete [] szHost; szHost = NULL; delete [] szHeader; szHeader = NULL; delete [] szBody; szBody = NULL; delete [] szCookie; szCookie = NULL; bKeepAlive = false; bUseSSL = false; } if (bUseSSL) { SSL_free(ssl); // Nếu có kết nối SSL thì đóng. ssl = NULL; } if(iSocket != 0) { shutdown(iSocket, SD_BOTH); // Đóng kết nối closesocket(iSocket); // Hủy Socket } bConnected = false;}void HTTPS::DecodeChunked(PChunkState state, char *data, long &length, bool &bExit) const{ long inLength = 0; long outLength = 0; char *out = NULL; char *posOut = NULL; char *posIn = NULL; posIn = data; inLength = length; out = new char[inLength]; posOut = out; do { if (state->Chunk == false) { char *CRLF = NULL; char *pLength = NULL; char *error = NULL; CRLF = strstr(posIn,"
"); if (CRLF != NULL) { int iLen = CRLF - posIn; pLength = new char[iLen + 1]; memcpy(pLength, posIn,iLen * sizeof(char)); pLength[iLen] = '\0'; state->ChunkLength = strtol(pLength, &error, 16); state->Chunk = (state->ChunkLength != 0); /* Phần tiếp theo là dữ liệu. */ posIn = CRLF + 2; /* Di chuyển qua kích thước của Chunk. */ inLength -= (iLen + 2); bExit = (state->ChunkLength == 0); } else break; } else { if (state->ChunkLength == 0) break; /* Kích thước của Chunk lớn hơn khối dữ liệu hiện tại, lấy hết khối*/ if (state->ChunkLength >= inLength) { memcpy(posOut, posIn, inLength * sizeof(char)); state->ChunkLength -= inLength; posOut += inLength; outLength += inLength; /* Đã lấy hết Chunk. */ if (state->ChunkLength == 0) state->Chunk = false; inLength = 0; } else { memcpy(posOut, posIn, state->ChunkLength * sizeof(char)); inLength -= state->ChunkLength; posIn += state->ChunkLength; posOut += state->ChunkLength; outLength += state->ChunkLength; state->Chunk = false; state->ChunkLength = 0; } } } while(inLength >0); memset(data, 0, length * sizeof(char)); memcpy(data, out, outLength * sizeof(char)); length = outLength; delete[] out;}
Pallet nhựa Lâm Đồng giải pháp hoàn hảo cho nhà kho Pallet nhựa Lâm Đồng là một giải pháp đa năng và hiệu quả trong việc vận chuyển và lưu trữ hàng hóa. Trong đó, pallet nhựa Lâm Đồng không chỉ đáp...
Pallet nhựa Lâm Đồng giải pháp...