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 | MovedTemporarilyError /* Corresponds to 302 == moved temporarily response */
|
---|
60 | };
|
---|
61 |
|
---|
62 | QIHttp (QObject *aParent, const QString &aHostName, quint16 aPort = 80)
|
---|
63 | : QHttp (aHostName, aPort, aParent)
|
---|
64 | , mStatusCode (0)
|
---|
65 | , mErrorCode (NoError)
|
---|
66 | {
|
---|
67 | mTimeoutTimer.setSingleShot (true);
|
---|
68 | mTimeoutTimer.setInterval (MaxWaitTime);
|
---|
69 | connect (&mTimeoutTimer, SIGNAL (timeout()),
|
---|
70 | this, SLOT (timeouted()));
|
---|
71 | connect (this, SIGNAL (dataReadProgress (int, int)),
|
---|
72 | &mTimeoutTimer, SLOT (start()));
|
---|
73 | connect (this, SIGNAL (dataSendProgress (int, int)),
|
---|
74 | &mTimeoutTimer, SLOT (start()));
|
---|
75 | connect (this, SIGNAL (done (bool)),
|
---|
76 | &mTimeoutTimer, SLOT (stop()));
|
---|
77 |
|
---|
78 | connect (this, SIGNAL (responseHeaderReceived (const QHttpResponseHeader &)),
|
---|
79 | this, SLOT (processResponseHeader (const QHttpResponseHeader &)));
|
---|
80 | connect (this, SIGNAL (done (bool)), this, SLOT (processDone (bool)));
|
---|
81 | }
|
---|
82 |
|
---|
83 | AdvancedError errorCode() const { return mErrorCode; }
|
---|
84 |
|
---|
85 | QString errorString() const
|
---|
86 | {
|
---|
87 | switch (mErrorCode)
|
---|
88 | {
|
---|
89 | case TimeoutError:
|
---|
90 | return tr ("Connection timed out");
|
---|
91 | case PageNotFoundError:
|
---|
92 | return tr ("Could not locate the file on "
|
---|
93 | "the server (response: %1)").arg (mStatusCode);
|
---|
94 | case MovedTemporarilyError:
|
---|
95 | return QString::null; /* should be redirected anyway */
|
---|
96 | default:
|
---|
97 | return QHttp::errorString();
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | int get (const QString &aPath)
|
---|
102 | {
|
---|
103 | mTimeoutTimer.start();
|
---|
104 | return QHttp::get (aPath, 0);
|
---|
105 | }
|
---|
106 |
|
---|
107 | int post (const QString &aPath)
|
---|
108 | {
|
---|
109 | mTimeoutTimer.start();
|
---|
110 | return QHttp::post (aPath, 0);
|
---|
111 | }
|
---|
112 |
|
---|
113 | int request (const QHttpRequestHeader &aHeader)
|
---|
114 | {
|
---|
115 | mTimeoutTimer.start();
|
---|
116 | return QHttp::request (aHeader);
|
---|
117 | }
|
---|
118 |
|
---|
119 | signals:
|
---|
120 |
|
---|
121 | void allIsDone (bool aError);
|
---|
122 |
|
---|
123 | private slots:
|
---|
124 |
|
---|
125 | void timeouted()
|
---|
126 | {
|
---|
127 | mErrorCode = TimeoutError;
|
---|
128 | abort();
|
---|
129 | }
|
---|
130 |
|
---|
131 | void processResponseHeader (const QHttpResponseHeader &aResponse)
|
---|
132 | {
|
---|
133 | mStatusCode = aResponse.statusCode();
|
---|
134 | switch (mStatusCode)
|
---|
135 | {
|
---|
136 | case 302:
|
---|
137 | mErrorCode = MovedTemporarilyError;
|
---|
138 | return abort();
|
---|
139 | case 404:
|
---|
140 | mErrorCode = PageNotFoundError;
|
---|
141 | return abort();
|
---|
142 | default:
|
---|
143 | mErrorCode = (AdvancedError) QHttp::error();
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 | void processDone (bool aError)
|
---|
148 | {
|
---|
149 | if (mErrorCode == NoError)
|
---|
150 | mErrorCode = (AdvancedError) QHttp::error();
|
---|
151 | emit allIsDone (aError);
|
---|
152 | }
|
---|
153 |
|
---|
154 | private:
|
---|
155 |
|
---|
156 | QTimer mTimeoutTimer;
|
---|
157 | int mStatusCode;
|
---|
158 | AdvancedError mErrorCode;
|
---|
159 | };
|
---|
160 |
|
---|
161 | #endif // __QIHttp_h__
|
---|
162 |
|
---|