39 QObject * parent = 0);
75#ifdef VP1HttpGetFile_cxx
79 const QString& localtargetfile,
80 const QString& expectedMD5Sum,
84 m_urltofile(urltofile),
85 m_localtargetfile(localtargetfile),
86 m_expectedMD5Sum(expectedMD5Sum),
88 m_errorString(
"Download not finished"),
90 m_downloadFinishedStatus(NOTFINISHED),
134 qint64 currentsize = fi.exists() ? fi.size() : -1;
153 // Input validation //
156 //Input validation ==> MD5 sum
158 if ( !m_expectedMD5Sum.isEmpty() && !VP1MD5Sum::validMD5Sum(m_expectedMD5Sum) ) {
159 endInFailure("Invalid target md5sum: "+m_expectedMD5Sum,CLEANUPLOCALFILE);
163 //Input validation ==> URL
165 if (m_urltofile.isEmpty()) {
166 endInFailure("Empty URL",DONTTOUCHLOCALFILE);
170 QUrl url(m_urltofile.contains("://") ? m_urltofile : "http://" + m_urltofile);
171 if (!url.isValid()||url.host().isEmpty()) {
172 endInFailure("Invalid URL: "+m_urltofile,DONTTOUCHLOCALFILE);
175 //Input validation ==> Target file
177 //File can not exist already and we abort if we are already
178 //downloading a file to that location:
179 QFileInfo fi(m_localtargetfile);
181 //In one case we might still end well: if we know the target
182 //checksum and if the file has it (this also does away with
183 //several problems of "simultaneous downloads started":
184 if (!m_expectedMD5Sum.isEmpty()&&VP1MD5Sum::sumMatches(m_localtargetfile,m_expectedMD5Sum))
186 endInFailure ("Download target already exists: "+m_localtargetfile,DONTTOUCHLOCALFILE);
189 if (!fi.dir().exists()) {
190 endInFailure("Directory ("+fi.dir().absolutePath()+") for download target does not exist: "+m_localtargetfile,DONTTOUCHLOCALFILE);
195 QFile file(m_localtargetfile);
196 if (!file.open(QIODevice::WriteOnly)) {
197 endInFailure("Could not open file for write access: "+m_localtargetfile,DONTTOUCHLOCALFILE);
202 // HTTP request and event loop //
205 m_http = new QHttp(0);
207 connect(m_http,SIGNAL(done(bool)),this,SLOT(done(bool)));
208 connect(m_http,SIGNAL(dataReadProgress(int,int)),this,SLOT(dataReadProgress()));
210 QHttpRequestHeader header("GET", url.path()+(url.hasQuery()?"?"+url.encodedQuery():QString("")));
211 // ^^^ Todo: We could support username/passwd part of url also.
212 header.setValue("Host", url.host());
213 header.setValue("User-Agent", "ATLASVP1");
214 m_http->setHost(url.host());
215 m_lastChangeTime = QDateTime::currentDateTime().toTime_t();
218 m_http->request(header,0,&file);
220 connect(&timer, SIGNAL(timeout()), this, SLOT(checkForStall()));
221 timer.start(1000);//1s
226 m_http->blockSignals(true);
227 if (m_http->hasPendingRequests())
228 m_http->clearPendingRequests();
229 if (m_http->currentId()!=0)
237 if (m_downloadFinishedStatus!=NOERRORS) {
238 switch(m_downloadFinishedStatus) {
240 endInFailure("Download finished with error "+m_http->errorString(),CLEANUPLOCALFILE);
243 endInFailure("Download stalled",CLEANUPLOCALFILE);
245 case SIZEDECREASEDERROR:
246 endInFailure("Aborted since file was either removed or decreased in size.",CLEANUPLOCALFILE);
249 endInFailure("Unexpected download status (SHOULD NEVER HAPPEN)",CLEANUPLOCALFILE);
258 if (!m_expectedMD5Sum.isEmpty()) {
259 bool match = VP1MD5Sum::sumMatches(m_localtargetfile,m_expectedMD5Sum);
261 endInFailure("Checksum did not match",CLEANUPLOCALFILE);
char data[hepevt_bytes_allocation_ATLAS]
QString errorString() const
const QString m_localtargetfile
virtual ~VP1HttpGetFile_DownloadThread()
DOWNLOADFINISHSTATUS m_downloadFinishedStatus
const QString & urltofile() const
unsigned m_lastChangeTime
void endInFailure(const QString &, CLEANUPOPT)
const QString & data() const
const QString m_urltofile
VP1HttpGetFile_DownloadThread(const QString &urltofile, const QString &localtargetfile, const QString &expectedMD5Sum, const QString &data, QObject *parent=0)
const QString m_expectedMD5Sum
const QString & localtargetfile() const
const QString & expectedMD5Sum() const