Changeset 45551 in vbox for trunk/src/VBox/Frontends
- Timestamp:
- Apr 15, 2013 2:21:18 PM (12 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/net
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkReply.cpp
r45476 r45551 39 39 40 40 /* Constructor: */ 41 UINetworkReplyPrivateThread(const QNetworkRequest &request) 42 : m_request(request) 43 , m_iError(VINF_SUCCESS) 44 , m_pHttp(0) 45 { 46 } 47 48 /* API: */ 41 UINetworkReplyPrivateThread(const QNetworkRequest &request); 42 43 /* API: Read stuff: */ 49 44 const QByteArray& readAll() const { return m_reply; } 45 46 /* API: Error stuff: */ 50 47 int error() const { return m_iError; } 51 48 52 /* API: Abort stuff: */ 53 void abort() 54 { 55 /* Make sure http is created: */ 56 if (!m_pHttp) 57 return; 58 59 /* Call for http abort: */ 60 RTHttpAbort(m_pHttp); 61 } 49 /* API: HTTP stuff: */ 50 void abort(); 62 51 63 52 private: 64 53 65 /* Thread runner: */ 66 void run() 67 { 68 /* Init: */ 69 RTR3InitExeNoArguments(RTR3INIT_FLAGS_SUPLIB); 70 71 /* Create: */ 72 m_iError = RTHttpCreate(&m_pHttp); 73 74 /* Setup proxy: */ 75 UIProxyManager proxyManager(vboxGlobal().settings().proxySettings()); 76 if (proxyManager.proxyEnabled()) 77 { 78 RTHttpSetProxy(m_pHttp, 79 proxyManager.proxyHost().toAscii().constData(), 80 proxyManager.proxyPort().toUInt(), 0, 0); 81 } 82 83 /* Do we have raw headers? */ 84 QList<QByteArray> rawHeaders = m_request.rawHeaderList(); 85 if (!rawHeaders.isEmpty()) 86 { 87 /* We should format them first: */ 88 QVector<QByteArray> formattedHeaderVector; 89 QVector<const char*> formattedHeaderPointerVector; 90 /* For each existing raw-header: */ 91 foreach (const QByteArray &rawHeader, rawHeaders) 92 { 93 /* Prepare formatted representation: */ 94 QString strFormattedString = QString("%1: %2").arg(QString(rawHeader), QString(m_request.rawHeader(rawHeader))); 95 formattedHeaderVector << strFormattedString.toAscii(); 96 formattedHeaderPointerVector << formattedHeaderVector.last().constData(); 97 } 98 const char **ppFormattedHeaders = formattedHeaderPointerVector.data(); 99 RTHttpSetHeaders(m_pHttp, formattedHeaderPointerVector.size(), ppFormattedHeaders); 100 } 101 102 /* Acquire: */ 103 if (RT_SUCCESS(m_iError)) 104 { 105 char *pszBuf = 0; 106 m_iError = RTHttpGet(m_pHttp, 107 m_request.url().toString().toAscii().constData(), 108 &pszBuf); 109 m_reply = QByteArray(pszBuf); 110 RTMemFree(pszBuf); 111 } 112 113 /* Destroy: */ 114 RTHttpDestroy(m_pHttp); 115 m_pHttp = 0; 116 } 54 /* Private API: HTTP stuff: */ 55 int applyProxyRules(); 56 int applyRawHeaders(); 57 int performMainRequest(); 58 59 /* Helper: Thread runner: */ 60 void run(); 61 62 /* Static helpers: HTTP stuff: */ 63 static int abort(RTHTTP pHttp); 64 static int applyProxyRules(RTHTTP pHttp, const QString &strHostName, int iPort); 65 static int applyRawHeaders(RTHTTP pHttp, const QList<QByteArray> &headers, const QNetworkRequest &request); 66 static int performGetRequest(RTHTTP pHttp, const QNetworkRequest &request, QByteArray &reply); 117 67 118 68 /* Variables: */ … … 122 72 QByteArray m_reply; 123 73 }; 74 75 UINetworkReplyPrivateThread::UINetworkReplyPrivateThread(const QNetworkRequest &request) 76 : m_request(request) 77 , m_iError(VINF_SUCCESS) 78 , m_pHttp(0) 79 { 80 } 81 82 void UINetworkReplyPrivateThread::abort() 83 { 84 /* Call for HTTP abort: */ 85 abort(m_pHttp); 86 } 87 88 int UINetworkReplyPrivateThread::applyProxyRules() 89 { 90 /* Make sure proxy is enabled in Proxy Manager: */ 91 UIProxyManager proxyManager(vboxGlobal().settings().proxySettings()); 92 if (!proxyManager.proxyEnabled()) 93 return VINF_SUCCESS; 94 95 /* Apply proxy rules: */ 96 return applyProxyRules(m_pHttp, 97 proxyManager.proxyHost(), 98 proxyManager.proxyPort().toUInt()); 99 } 100 101 int UINetworkReplyPrivateThread::applyRawHeaders() 102 { 103 /* Make sure we have a raw headers at all: */ 104 QList<QByteArray> headers = m_request.rawHeaderList(); 105 if (headers.isEmpty()) 106 return VINF_SUCCESS; 107 108 /* Apply raw headers: */ 109 return applyRawHeaders(m_pHttp, headers, m_request); 110 } 111 112 int UINetworkReplyPrivateThread::performMainRequest() 113 { 114 /* Perform GET request: */ 115 return performGetRequest(m_pHttp, m_request, m_reply); 116 } 117 118 void UINetworkReplyPrivateThread::run() 119 { 120 /* Init: */ 121 RTR3InitExeNoArguments(RTR3INIT_FLAGS_SUPLIB); 122 123 /* Create HTTP object: */ 124 m_iError = RTHttpCreate(&m_pHttp); 125 126 /* Was HTTP created? */ 127 if (RT_SUCCESS(m_iError) && m_pHttp) 128 { 129 /* Simulate try-catch block: */ 130 do 131 { 132 /* Apply proxy-rules: */ 133 m_iError = applyProxyRules(); 134 if (!RT_SUCCESS(m_iError)) 135 break; 136 137 /* Assign raw headers: */ 138 m_iError = applyRawHeaders(); 139 if (!RT_SUCCESS(m_iError)) 140 break; 141 142 /* Perform main request: */ 143 m_iError = performMainRequest(); 144 } 145 while (0); 146 } 147 148 /* Destroy HTTP object: */ 149 if (m_pHttp) 150 { 151 RTHttpDestroy(m_pHttp); 152 m_pHttp = 0; 153 } 154 } 155 156 /* static */ 157 int UINetworkReplyPrivateThread::abort(RTHTTP pHttp) 158 { 159 /* Make sure HTTP is created: */ 160 if (!pHttp) 161 return VERR_INVALID_POINTER; 162 163 /* Call for HTTP abort: */ 164 return RTHttpAbort(pHttp); 165 } 166 167 /* static */ 168 int UINetworkReplyPrivateThread::applyProxyRules(RTHTTP pHttp, const QString &strHostName, int iPort) 169 { 170 /* Make sure HTTP is created: */ 171 if (!pHttp) 172 return VERR_INVALID_POINTER; 173 174 /* Assign HTTP proxy: */ 175 return RTHttpSetProxy(pHttp, 176 strHostName.toAscii().constData(), 177 iPort, 178 0 /* login */, 0 /* password */); 179 } 180 181 /* static */ 182 int UINetworkReplyPrivateThread::applyRawHeaders(RTHTTP pHttp, const QList<QByteArray> &headers, const QNetworkRequest &request) 183 { 184 /* Make sure HTTP is created: */ 185 if (!pHttp) 186 return VERR_INVALID_POINTER; 187 188 /* We should format them first: */ 189 QVector<QByteArray> formattedHeaders; 190 QVector<const char*> formattedHeaderPointers; 191 foreach (const QByteArray &header, headers) 192 { 193 /* Prepare formatted representation: */ 194 QString strFormattedString = QString("%1: %2").arg(QString(header), QString(request.rawHeader(header))); 195 formattedHeaders << strFormattedString.toAscii(); 196 formattedHeaderPointers << formattedHeaders.last().constData(); 197 } 198 const char **ppFormattedHeaders = formattedHeaderPointers.data(); 199 200 /* Assign HTTP headers: */ 201 return RTHttpSetHeaders(pHttp, formattedHeaderPointers.size(), ppFormattedHeaders); 202 } 203 204 /* static */ 205 int UINetworkReplyPrivateThread::performGetRequest(RTHTTP pHttp, const QNetworkRequest &request, QByteArray &reply) 206 { 207 /* Make sure HTTP is created: */ 208 if (!pHttp) 209 return VERR_INVALID_POINTER; 210 211 /* Perform blocking HTTP GET request: */ 212 char *pszBuf = 0; 213 int rc = RTHttpGet(pHttp, 214 request.url().toString().toAscii().constData(), 215 &pszBuf); 216 reply = QByteArray(pszBuf); 217 RTMemFree(pszBuf); 218 return rc; 219 } 220 124 221 125 222 /* Our network-reply object: */ -
trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkReply.h
r43707 r45551 27 27 /* GUI includes: */ 28 28 #include "UINetworkDefs.h" 29 30 /* Forward declarations: */31 class UINetworkReplyPrivateThread;32 29 33 30 /* Network-reply interface: */
Note:
See TracChangeset
for help on using the changeset viewer.