1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * QIHttp class declaration & implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef __QIHttp_h__
|
---|
24 | #define __QIHttp_h__
|
---|
25 |
|
---|
26 | /* Qt includes */
|
---|
27 | #include <QHttp>
|
---|
28 | #include <QTimer>
|
---|
29 |
|
---|
30 | /* Time to auto-disconnect if no network answer received. */
|
---|
31 | static const int MaxWaitTime = 20000;
|
---|
32 |
|
---|
33 | /* This is QHttp extension to unite different happens errors
|
---|
34 | * into one already present error processing mechanism. */
|
---|
35 | class QIHttp : public QHttp
|
---|
36 | {
|
---|
37 | Q_OBJECT;
|
---|
38 |
|
---|
39 | public:
|
---|
40 |
|
---|
41 | /* Additional error codes */
|
---|
42 | enum AdvancedError
|
---|
43 | {
|
---|
44 | /* Basic QHttp errors */
|
---|
45 | NoError = QHttp::NoError,
|
---|
46 | UnknownError = QHttp::UnknownError,
|
---|
47 | HostNotFound = QHttp::HostNotFound,
|
---|
48 | ConnectionRefused = QHttp::ConnectionRefused,
|
---|
49 | UnexpectedClose = QHttp::UnexpectedClose,
|
---|
50 | InvalidResponseHeader = QHttp::InvalidResponseHeader,
|
---|
51 | WrongContentLength = QHttp::WrongContentLength,
|
---|
52 | Aborted = QHttp::Aborted,
|
---|
53 | AuthenticationRequiredError = QHttp::AuthenticationRequiredError,
|
---|
54 | ProxyAuthenticationRequiredError = QHttp::ProxyAuthenticationRequiredError,
|
---|
55 |
|
---|
56 | /* Advanced QHttp errors */
|
---|
57 | TimeoutError, /* MaxWaitTime time passed with no response */
|
---|
58 | PageNotFoundError, /* Corresponds to 404 == not found header */
|
---|
59 | MovedPermanentlyError, /* Corresponds to 301 == moved permanently response */
|
---|
60 | MovedTemporarilyError /* Corresponds to 302 == moved temporarily response */
|
---|
61 | };
|
---|
62 |
|
---|
63 | QIHttp (QObject *aParent, const QString &aHostName, quint16 aPort = 80)
|
---|
64 | : QHttp (aHostName, aPort, aParent)
|
---|
65 | , mStatusCode (0)
|
---|
66 | , mErrorCode (NoError)
|
---|
67 | {
|
---|
68 | mTimeoutTimer.setSingleShot (true);
|
---|
69 | mTimeoutTimer.setInterval (MaxWaitTime);
|
---|
70 | connect (&mTimeoutTimer, SIGNAL (timeout()),
|
---|
71 | this, SLOT (timeouted()));
|
---|
72 | connect (this, SIGNAL (dataReadProgress (int, int)),
|
---|
73 | &mTimeoutTimer, SLOT (start()));
|
---|
74 | connect (this, SIGNAL (dataSendProgress (int, int)),
|
---|
75 | &mTimeoutTimer, SLOT (start()));
|
---|
76 | connect (this, SIGNAL (done (bool)),
|
---|
77 | &mTimeoutTimer, SLOT (stop()));
|
---|
78 |
|
---|
79 | connect (this, SIGNAL (responseHeaderReceived (const QHttpResponseHeader &)),
|
---|
80 | this, SLOT (processResponseHeader (const QHttpResponseHeader &)));
|
---|
81 | connect (this, SIGNAL (done (bool)), this, SLOT (processDone (bool)));
|
---|
82 | }
|
---|
83 |
|
---|
84 | AdvancedError errorCode() const { return mErrorCode; }
|
---|
85 |
|
---|
86 | QString errorString() const
|
---|
87 | {
|
---|
88 | switch (mErrorCode)
|
---|
89 | {
|
---|
90 | case TimeoutError:
|
---|
91 | return tr ("Connection timed out");
|
---|
92 | case PageNotFoundError:
|
---|
93 | return tr ("Could not locate the file on "
|
---|
94 | "the server (response: %1)").arg (mStatusCode);
|
---|
95 | case MovedPermanentlyError:
|
---|
96 | case MovedTemporarilyError:
|
---|
97 | return QString::null; /* should be redirected anyway */
|
---|
98 | default:
|
---|
99 | return QHttp::errorString();
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | int get (const QString &aPath)
|
---|
104 | {
|
---|
105 | mTimeoutTimer.start();
|
---|
106 | return QHttp::get (aPath, 0);
|
---|
107 | }
|
---|
108 |
|
---|
109 | int post (const QString &aPath)
|
---|
110 | {
|
---|
111 | mTimeoutTimer.start();
|
---|
112 | return QHttp::post (aPath, 0);
|
---|
113 | }
|
---|
114 |
|
---|
115 | int request (const QHttpRequestHeader &aHeader)
|
---|
116 | {
|
---|
117 | mTimeoutTimer.start();
|
---|
118 | return QHttp::request (aHeader);
|
---|
119 | }
|
---|
120 |
|
---|
121 | signals:
|
---|
122 |
|
---|
123 | void allIsDone (bool aError);
|
---|
124 |
|
---|
125 | private slots:
|
---|
126 |
|
---|
127 | void timeouted()
|
---|
128 | {
|
---|
129 | mErrorCode = TimeoutError;
|
---|
130 | abort();
|
---|
131 | }
|
---|
132 |
|
---|
133 | void processResponseHeader (const QHttpResponseHeader &aResponse)
|
---|
134 | {
|
---|
135 | mStatusCode = aResponse.statusCode();
|
---|
136 | switch (mStatusCode)
|
---|
137 | {
|
---|
138 | case 301:
|
---|
139 | mErrorCode = MovedPermanentlyError;
|
---|
140 | return abort();
|
---|
141 | case 302:
|
---|
142 | mErrorCode = MovedTemporarilyError;
|
---|
143 | return abort();
|
---|
144 | case 404:
|
---|
145 | mErrorCode = PageNotFoundError;
|
---|
146 | return abort();
|
---|
147 | default:
|
---|
148 | mErrorCode = (AdvancedError) QHttp::error();
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | void processDone (bool aError)
|
---|
153 | {
|
---|
154 | if (mErrorCode == NoError)
|
---|
155 | mErrorCode = (AdvancedError) QHttp::error();
|
---|
156 | emit allIsDone (aError);
|
---|
157 | }
|
---|
158 |
|
---|
159 | private:
|
---|
160 |
|
---|
161 | QTimer mTimeoutTimer;
|
---|
162 | int mStatusCode;
|
---|
163 | AdvancedError mErrorCode;
|
---|
164 | };
|
---|
165 |
|
---|
166 | #endif // __QIHttp_h__
|
---|
167 |
|
---|