[Jsmooth-cvs] jsmooth/skeletons/util-net WinHttpClient.cpp, NONE, 1.1 WinHttpClient.h, NONE, 1.1 Ma
Status: Beta
Brought to you by:
reyes
From: Rodrigo R. <re...@us...> - 2007-05-16 22:04:29
|
Update of /cvsroot/jsmooth/jsmooth/skeletons/util-net In directory sc8-pr-cvs6.sourceforge.net:/tmp/cvs-serv18780 Modified Files: Makefile.win httpdownload.cpp httpdownload.h testmain.cpp Added Files: WinHttpClient.cpp WinHttpClient.h Log Message: added wininet download class Index: Makefile.win =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-net/Makefile.win,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** Makefile.win 30 Apr 2007 20:54:32 -0000 1.4 --- Makefile.win 16 May 2007 22:04:19 -0000 1.5 *************** *** 10,15 **** FLTK-CXXFLAGS = $(shell fltk-config --cxxflags) RES = ! OBJ = HttpClient.o URL.o downloadgui.o httpdownload.o $(RES) ! LIBS = -L"$(MINGW)/lib" -L"/lib" -lws2_32 $(FLTK-LDFLAGS) CXXINCS = -Os -I"../util-core" -I"$(MINGW)/include/c++" -I"$(MINGW)/include/c++/mingw32" -I"$(MINGW)/include/c++/backward" -I"$(MINGW)/include" -I"$(JDK)/include" -I"$(JDK)/include/win32" $(FLTK-CXXFLAGS) BIN = $(PROJECTNAME).a --- 10,15 ---- FLTK-CXXFLAGS = $(shell fltk-config --cxxflags) RES = ! OBJ = WinHttpClient.o HttpClient.o URL.o downloadgui.o httpdownload.o $(RES) ! LIBS = -L"$(MINGW)/lib" -L"/lib" -lws2_32 -lwininet $(FLTK-LDFLAGS) CXXINCS = -Os -I"../util-core" -I"$(MINGW)/include/c++" -I"$(MINGW)/include/c++/mingw32" -I"$(MINGW)/include/c++/backward" -I"$(MINGW)/include" -I"$(JDK)/include" -I"$(JDK)/include/win32" $(FLTK-CXXFLAGS) BIN = $(PROJECTNAME).a Index: httpdownload.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-net/httpdownload.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** httpdownload.cpp 28 Apr 2007 08:49:33 -0000 1.3 --- httpdownload.cpp 16 May 2007 22:04:19 -0000 1.4 *************** *** 26,30 **** Fl_Window *httpWindow ; ! HttpClient httpClient; char downloadLabel[256]; unsigned long downloadThread; --- 26,30 ---- Fl_Window *httpWindow ; ! WinHttpClient httpClient; char downloadLabel[256]; unsigned long downloadThread; *************** *** 42,45 **** --- 42,47 ---- sprintf(downloadLabel, "Downloading..."); + printf("%s\n", downloadLabel); + hcg_progressbar->label(downloadLabel); hcg_progressbar->redraw(); *************** *** 91,94 **** --- 93,97 ---- } + // // Callback for the cancel button Index: testmain.cpp =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-net/testmain.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** testmain.cpp 4 Mar 2007 22:23:24 -0000 1.2 --- testmain.cpp 16 May 2007 22:04:19 -0000 1.3 *************** *** 70,76 **** // string file = httpDownload("http://localhost/jinstall-1_4_2-windows-i586.cab"); // string file = httpDownload("http://java.sun.com/products/plugin/autodl/jinstall-1_3_0_05-win.cab"); ! string file= httpDownload("http://downloads.sourceforge.net/filezilla/FileZilla_2_2_31_setup.exe?use_mirror=osdn"); printf("Downloaded: %s\n", file.c_str()); --- 70,80 ---- // string file = httpDownload("http://localhost/jinstall-1_4_2-windows-i586.cab"); + // string file = httpDownload("http://127.0.0.1/test.msi"); + // string file = httpDownload("http://travel.state.gov/passport/forms/forms_847.html"); // string file = httpDownload("http://java.sun.com/products/plugin/autodl/jinstall-1_3_0_05-win.cab"); ! string file = httpDownload("ftp://ftp.free.fr/pub/freeplayer/Freeplayer-Win32-20050905.zip"); ! ! //string file= httpDownload("http://downloads.sourceforge.net/filezilla/FileZilla_2_2_31_setup.exe?use_mirror=osdn"); printf("Downloaded: %s\n", file.c_str()); --- NEW FILE: WinHttpClient.cpp --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "WinHttpClient.h" #include "StringUtils.h" WinHttpClient::WinHttpClient() { m_connection = InternetOpen("JSmooth wrapper", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0); // INTERNET_FLAG_ASYNC); printf("Got connection: %d\n", m_connection); m_file = NULL; m_contentlength = -1; m_cancelled = false; } WinHttpClient::~WinHttpClient() { if (m_file != NULL) InternetCloseHandle(m_file); if (m_connection != NULL) InternetCloseHandle(m_connection); } bool WinHttpClient::sendGet() { if (m_connection != NULL) { m_file = InternetOpenUrl(m_connection, m_url.toString().c_str(), NULL, 0, 0, //INTERNET_FLAG_PASSIVE, 0); if (m_file != NULL) { std::string protocol = StringUtils::toLowerCase(m_url.getProtocol()); m_contentlength = -1; if (protocol == "http") { DWORD size; DWORD sizesize = sizeof(DWORD); if (HttpQueryInfo(m_file, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &size, &sizesize, NULL)) m_contentlength = size; } else if (protocol == "ftp") { DWORD size; m_contentlength = FtpGetFileSize(m_file, &size); } } } return m_file != NULL; } bool WinHttpClient::saveResponseBody() { if (m_file == NULL) return false; // // Create a temporary filename string filebase = m_url.getFileName(); string extension = FileUtils::getFileExtension(filebase); string filepath = FileUtils::createTempFileName(".httpdl."+extension); m_receivedFilePath = filepath; ofstream output(filepath.c_str(), ios::out | ios::binary); char buffer[1024]; DWORD readbytes = 0; bool result; int currentlength = 0; do { result = InternetReadFile(m_file, buffer, 1023, &readbytes); if (readbytes > 0) { output.write(buffer, readbytes); currentlength += readbytes; updateListeners(currentlength, m_contentlength); } } while (result && (readbytes > 0) && (m_cancelled == false)); output.close(); if (m_cancelled) { DeleteFile(filepath.c_str()); return false; } // // Assume that if we downloaded nothing, something went wrong... // if (currentlength <= 0) return false; return true; } void WinHttpClient::addListener(WinHttpClientListener& listener) { m_listeners.push_back(&listener); } void WinHttpClient::updateListeners(int current, int total) { for (vector<WinHttpClientListener*>::iterator i = m_listeners.begin(); i != m_listeners.end(); i++) { (*i)->httpDownloadUpdate(current, total); } } bool WinHttpClient::cancel() { m_cancelled = true; } bool WinHttpClient::isCancelled() { return m_cancelled; } void WinHttpClient::setURL(const URL& url) { m_url = url; } std::string WinHttpClient::getTemporaryFile() { return m_receivedFilePath; } --- NEW FILE: WinHttpClient.h --- /* JSmooth: a VM wrapper toolkit for Windows Copyright (C) 2003-2007 Rodrigo Reyes <re...@ch...> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifndef __WINHTTPCLIENT_H_ #define __WINHTTPCLIENT_H_ #include <string> #include <vector> #include <winsock2.h> #include <wininet.h> #include <fstream> #include <iostream> using namespace std; #include "FileUtils.h" #include "URL.h" /** * This listener class provides a way to get the download * status. Implements this class and use HttpClient::addListener() to * keep track of the status. */ class WinHttpClientListener { public: virtual void httpDownloadUpdate(int current, int total) = 0; }; /** * A simplistic HTTP client class that manages chunked content and can * only save GET request on disk. * * To use it, one should typicall call the following methods: * * HttpClient client; // creates an instance * client.addListener(updater); // to add a listener, if any * client.setURL(anUrl); * client.sendGet(); // Send the request * bool result = httpClient.saveResponseBody(); // Did it work ? * string file = httpClient.getTemporaryFile(); // Get the filepath */ class WinHttpClient { public: WinHttpClient(); ~WinHttpClient(); void addListener(WinHttpClientListener& listener); void setURL(const URL& url); bool sendGet(); bool saveResponseBody(); bool cancel(); bool isCancelled(); std::string getTemporaryFile(); protected: HINTERNET m_connection; HINTERNET m_file; int m_contentlength; bool m_cancelled; URL m_url; std::string m_receivedFilePath; vector<WinHttpClientListener*> m_listeners; void updateListeners(int current, int total); }; #endif Index: httpdownload.h =================================================================== RCS file: /cvsroot/jsmooth/jsmooth/skeletons/util-net/httpdownload.h,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** httpdownload.h 4 Mar 2007 23:21:44 -0000 1.2 --- httpdownload.h 16 May 2007 22:04:19 -0000 1.3 *************** *** 36,44 **** #include "downloadgui.h" #include "HttpClient.h" // // The listener that keeps track of the download progress and update // the progress bar. ! class HttpUpdater : public HttpClientListener { void httpDownloadUpdate(int current, int total); --- 36,45 ---- #include "downloadgui.h" #include "HttpClient.h" + #include "WinHttpClient.h" // // The listener that keeps track of the download progress and update // the progress bar. ! class HttpUpdater : public WinHttpClientListener { void httpDownloadUpdate(int current, int total); |