#ifndef QWON_HTTP_CLIENT_UTIL #define QWON_HTTP_CLIENT_UTIL #include #include #include #pragma link "wininet.lib" namespace QwonWebDav{ class ParsedURL{ private: public: String serverAddress; String remoteDirectory; String remoteFilename; String myString; String Header; int Port; String ID, PW; public: parseURL(wchar_t* URL){ myString = URL; int pos; const String protocolIdentifier = "http://"; pos = myString.Pos(protocolIdentifier); //¾ø´Â ¹®ÀÚ¿­ÀÏ °æ¿ì ¸®ÅϰªÀÌ 0ÀÌ´Ù. if(pos==0) return false; //httpÇÁ·ÎÅäÄÝ·Î ½ÃÀÛÇÏÁö ¾ÊÀ¸¸é ±×¸¸µÐ´Ù. pos = pos + protocolIdentifier.Length(); myString = myString.SubString(pos, myString.Length()-pos+1); //ÇÁ·ÎÅäÄÝ ½Äº°ÀÚ Á¦°Å pos = myString.Pos("/")+1; serverAddress = myString.SubString(1, pos - 2); Port = 80; // http default for(int i=0; i=pos ; i-- ) { //char a = myString[i]; if( myString[i] =='/'){ int pos2 = i; remoteDirectory = myString.SubString(pos,pos2-pos+1); remoteFilename = myString.SubString(pos2+1, myString.Length()-(pos2+1)+1); break; } } return true; } Copy(ParsedURL &serverParsedURL) { serverParsedURL.serverAddress = serverAddress; serverParsedURL.remoteDirectory = remoteDirectory; serverParsedURL.remoteFilename = remoteFilename; serverParsedURL.myString = myString; serverParsedURL.Port = Port; serverParsedURL.ID = ID; serverParsedURL.PW = PW; return true; } Clear() { serverAddress = ""; remoteDirectory = ""; remoteFilename = ""; myString = ""; Header = ""; Port = 80; ID = "", PW = ""; return 0; } }; //--------------------------------------------------------------------------- bool __fastcall parseURL(wchar_t *URL, ParsedURL &serverParsedURL) { return serverParsedURL.parseURL(URL); } //--------------------------------------------------------------------------- // Header¸¦ È®ÀÎÇØ¼­ Á¤»óÀûÀ¸·Î ÀÛµ¿µÇ¾ú´ÂÁö È®ÀÎÇÏ´Â ºÎºÐ bool __fastcall CheckSrvData(HINTERNET hURL, String &returnValue, ParsedURL &srvURL) { LPVOID lpOutBuffer=NULL; DWORD dwSize = 0; retry: // This call will fail on the first pass, because // no buffer is allocated. if(!HttpQueryInfo(hURL,HTTP_QUERY_RAW_HEADERS, (LPVOID)lpOutBuffer,&dwSize,NULL)) { if (GetLastError()==ERROR_HTTP_HEADER_NOT_FOUND) { // Code to handle the case where the header isn't available. return TRUE; } else { // Check for an insufficient buffer. if (GetLastError()==ERROR_INSUFFICIENT_BUFFER) { // Allocate the necessary buffer. lpOutBuffer = new wchar_t[dwSize]; // Retry the call. goto retry; } else { // Error handling code. if (lpOutBuffer) delete[] lpOutBuffer; return FALSE; } } } srvURL.Header = (wchar_t*)lpOutBuffer; returnValue = srvURL.Header.SubString(10, 3); if (lpOutBuffer) delete[] lpOutBuffer; return true; } //--------------------------------------------------------------------------- // \\61.251.191.50\webdav bool __fastcall PProcessHTTPUp(HINTERNET lngServer, ParsedURL &myParsedServerURL, wchar_t * strLocalFile) {//SendRequestEx¸¦ »ç¿ëÇÏÁö ¾Ê´Â ÇÔ¼ö, Å« ÆÄÀÏÀ» Àü¼ÛÇϰųª Áß°£Áß°£ »óŸ¦ º¸°íÀÚ ÇÒ ¶§´Â ºÎÀûÇÕÇÔ. DWORD dwRead, dwWritten, Recv=0; HANDLE hRequest=NULL, hLocalFile=NULL; int nFilesize; bool bHttpSendRequest; bool bInternetWriteFile; wchar_t *cBuffer; String Header; String strRemoteFile; strRemoteFile = myParsedServerURL.remoteDirectory+myParsedServerURL.remoteFilename; //ÀÎÅÍ³Ý ÆÄÀÏÀÇ ÇÚµéÀ» ¾ò´Â´Ù. // hRequest = HttpOpenRequest(lngServer, "PUT", strRemoteFile.c_str(), NULL, NULL, // 0, INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE, 0); hRequest = HttpOpenRequest(lngServer, L"PUT", strRemoteFile.c_str(), NULL, NULL, 0, INTERNET_FLAG_NO_CACHE_WRITE, 0); if (!hRequest) goto exit; //·ÎÄà ÆÄÀÏÀÇ ÇÚµéÀ» ¾ò°í, »çÀÌÁ ÀúÀåÇÑ´Ù. hLocalFile=CreateFile(strLocalFile, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if(!hLocalFile) goto exit; nFilesize = GetFileSize(hLocalFile, NULL); cBuffer = (wchar_t *)malloc(nFilesize); //¸Þ¸ð¸® ÇÒ´ç »ç¿ë. if(!ReadFile(hLocalFile, cBuffer, nFilesize, &dwRead, NULL)) goto exit; //¸®Äù½ºÆ®¸¦ º¸³½´Ù. bHttpSendRequest = HttpSendRequest(hRequest, NULL, NULL, cBuffer, dwRead); // 200: OK // 201: Created // 401: Unauthorized // 404: Not Found if (!CheckSrvData(hRequest, Header, myParsedServerURL) || !(Header=="200" || Header=="201")) goto exit; //·ÎÄÃÆÄÀÏ ÇÚµéÀ» ¸¶¹«¸®ÇÑ´Ù. if (hLocalFile) CloseHandle(hLocalFile); //ÀÎÅÍ³ÝÆÄÀÏ ÇÚµéÀ» ¸¶¹«¸®ÇÑ´Ù. if (hRequest) InternetCloseHandle(hRequest); return bHttpSendRequest; //bHttpSendRequest¿Í µ¿ÀÏÇÑ °á°ú°ªÀ» ¸®ÅÏ exit: //·ÎÄÃÆÄÀÏ ÇÚµéÀ» ¸¶¹«¸®ÇÑ´Ù. if (hLocalFile) CloseHandle(hLocalFile); //ÀÎÅÍ³ÝÆÄÀÏ ÇÚµéÀ» ¸¶¹«¸®ÇÑ´Ù. if (hRequest) InternetCloseHandle(hRequest); return false; //bHttpSendRequest¿Í µ¿ÀÏÇÑ °á°ú°ªÀ» ¸®ÅÏ } //--------------------------------------------------------------------------- BOOL UseHttpSendReqEx(HINTERNET hRequest, DWORD dwPostSize, wchar_t *pBuffer ) { Wininet::INTERNET_BUFFERS BufferIn; DWORD dwBytesWritten; int sum=0; const int BUFFERSIZE = 1024; int dwdwBytesToWrite; // BYTE pBuffer[1024]; BOOL bRet; BufferIn.dwStructSize = sizeof( Wininet::INTERNET_BUFFERS ); // Must be set or error will occur BufferIn.Next = NULL; BufferIn.lpcszHeader = NULL; BufferIn.dwHeadersLength = 0; BufferIn.dwHeadersTotal = 0; BufferIn.lpvBuffer = NULL; BufferIn.dwBufferLength = 0; BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize BufferIn.dwOffsetLow = 0; BufferIn.dwOffsetHigh = 0; if(!HttpSendRequestEx( hRequest, &BufferIn, NULL, 0, 0)) { //printf( "Error on HttpSendRequestEx %d\n",GetLastError() ); return FALSE; } // FillMemory(pBuffer, 1024, 'D'); // Fill buffer with data bRet=TRUE; /* for(n=1; n<=(int)dwPostSize/1024 && bRet; n++) { if(bRet=InternetWriteFile( hRequest, pBuffer, 1024, &dwBytesWritten)) ;// printf( "\r%d bytes sent.", n*1024); } if(!bRet) { //printf( "\nError on InternetWriteFile %lu\n",GetLastError() ); return FALSE; } */ dwdwBytesToWrite = BUFFERSIZE; do { sum+=BUFFERSIZE; if (sum>dwPostSize) { dwdwBytesToWrite=dwPostSize%BUFFERSIZE; sum=dwPostSize; } if (dwdwBytesToWrite==0) dwdwBytesToWrite=BUFFERSIZE; if(bRet=InternetWriteFile( hRequest, pBuffer, dwdwBytesToWrite, &dwBytesWritten)) ;// printf( "\r%d bytes sent.", n*1024); if(!bRet) { //printf( "\nError on InternetWriteFile %lu\n",GetLastError() ); return FALSE; } } while(sumdwStructSize = sizeof(Wininet::INTERNET_BUFFERS); BufferIn->dwBufferTotal = nFilesize; bHttpSendRequestEx = HttpSendRequestEx(hInternetFile, BufferIn, NULL, WININET_API_FLAG_SYNC|HSR_INITIATE, 0); //bHttpSendRequestEx = HttpSendRequestEx(hInternetFile, BufferIn, NULL, 0, 0); //WININET_API_FLAG_SYNC|HSR_INITIATE °ú 0À» ¾²´Â °ÍÀÇ Â÷ÀÌÁ¡À» ¾Ë±â À§Çؼ± MSDNÀ» Á¶»çÇØ¾ßÇÔ. if(bHttpSendRequestEx == 0) //½ÇÆÐÇßÀ¸¸é { DWORD error; error = GetLastError(); //·Î±×¸¦ ÀÛ¼ºÇÑ µÚ //·ÎÄà ÆÄÀÏÀ» »ý¼ºÇÑ´Ù. HANDLE hFile=CreateFile(L"C:\\kyuwonlog.txt", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); do { //´Ù¿î·Îµå¸¦ ¹Þ¾Æ ·ÎÄà ÆÄÀÏ¿¡ ±â·ÏÇÑ´Ù. DWORD Size; wchar_t buf[BUFFER_SIZE]; InternetQueryDataAvailable(hInternetFile, &Size, 0, 0); InternetReadFile(hInternetFile, buf, Size, &dwRead); WriteFile(hFile, buf, dwRead, &dwWritten, NULL); //´Ù¿î·Îµå ¹ÞÀº ¾çÀ» ¸Þ¼¼Áö·Î Àü´Þ Recv+=dwWritten; //SendMessage(hWnd, WM_USER+10, 0, (LPARAM)Recv); //Ãë¼Ò ó¸® /*if (bStopHttpDownLoad) { break; }*/ } while(dwRead!=0); CloseHandle(hFile); if (BufferIn) delete BufferIn; return false; //½ÇÆÐ°ªÀ» ¸®ÅÏ } //¾²±â ÀÛ¾÷À» ¼öÇàÇÑ´Ù. for(int i=0; i