PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/qxt/qxtweb-standalone/qxtweb/qxtwebevent.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 401 lines | 52 code | 45 blank | 304 comment | 1 complexity | 1bede5184988fe57e164124840422f3d MD5 | raw file
  1/****************************************************************************
  2 **
  3 ** Copyright (C) Qxt Foundation. Some rights reserved.
  4 **
  5 ** This file is part of the QxtWeb module of the Qxt library.
  6 **
  7 ** This library is free software; you can redistribute it and/or modify it
  8 ** under the terms of the Common Public License, version 1.0, as published
  9 ** by IBM, and/or under the terms of the GNU Lesser General Public License,
 10 ** version 2.1, as published by the Free Software Foundation.
 11 **
 12 ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
 13 ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
 14 ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
 15 ** FITNESS FOR A PARTICULAR PURPOSE.
 16 **
 17 ** You should have received a copy of the CPL and the LGPL along with this
 18 ** file. See the LICENSE file and the cpl1.0.txt/lgpl-2.1.txt files
 19 ** included with the source distribution for more information.
 20 ** If you did not receive a copy of the licenses, contact the Qxt Foundation.
 21 **
 22 ** <http://libqxt.org>  <foundation@libqxt.org>
 23 **
 24 ****************************************************************************/
 25
 26#include "qxtwebevent.h"
 27#include "qxtwebcontent.h"
 28#include <QBuffer>
 29
 30/*!
 31\class QxtWebEvent
 32
 33\inmodule QxtWeb
 34
 35\brief The QxtWebEvent class is a base class of all QxtWeb event types
 36
 37QxtWebEvent is the base class for all QxtWeb event classes. Event objects
 38contain event parameters.
 39
 40The base QxtWebEvent class contains the type of the event and a session ID for
 41the session it relates to. Subclasses of QxtWebEvent contain additional
 42parameters describing the particular event.
 43*/
 44
 45/*!
 46    \enum QxtWebEvent::EventType
 47
 48    \value None Not an event.
 49    \value Request A request event.
 50    \value FileUpload A file upload event.
 51    \value Page A page event.
 52    \value StoreCookie A store cookie event.
 53    \value RemoveCookie A remove cookie event.
 54    \value Redirect A redirect event.
 55*/
 56
 57/*!
 58 * Constructs a QxtWebEvent of the specified \a type for the specified \a sessionID.
 59 */
 60QxtWebEvent::QxtWebEvent(EventType type, int sessionID)
 61        : sessionID(sessionID), m_type(type) {}
 62
 63/*!
 64 * Destroys the event.
 65 */
 66QxtWebEvent::~QxtWebEvent() {}
 67
 68/*!
 69 * \fn EventType QxtWebEvent::type() const
 70 * Returns the event type.
 71 */
 72
 73/*!
 74 * \variable QxtWebEvent::sessionID
 75 * Contains the ID of the session the event is related to.
 76 */
 77
 78/*!
 79\class QxtWebRequestEvent
 80
 81\inmodule QxtWeb
 82
 83\brief The QxtWebRequestEvent class describes a request from a web browser
 84
 85The QxtWebRequestEvent class contains information about a request from a web
 86browser.
 87*/
 88
 89/*!
 90 * Constructs a QxtWebRequestEvent for the specified \a sessionID, \a requestID and \a url.
 91 */
 92QxtWebRequestEvent::QxtWebRequestEvent(int sessionID, int requestID, const QUrl& url)
 93        : QxtWebEvent(QxtWebEvent::Request, sessionID), requestID(requestID), url(url), originalUrl(url) {}
 94
 95/*!
 96 * Destroys the event and any content that may still be associated with it.
 97 */
 98QxtWebRequestEvent::~QxtWebRequestEvent()
 99{
100    if (content) delete content;
101}
102
103/*!
104 * \variable QxtWebRequestEvent::requestID
105 * Contains an opaque value generated by the session manager. This request ID
106 * must be included in the QxtWebPageEvent or QxtWebPageEvent subclass that
107 * is the response to the request.
108 */
109
110/*!
111 * \variable QxtWebRequestEvent::url
112 * Contains the request URL, possibly after rewriting by intermediate services
113 * such as QxtWebServiceDirectory.
114 */
115
116/*!
117 * \variable QxtWebRequestEvent::originalUrl
118 * Contains the request URL exactly as it was sent from the web browser.
119 */
120
121/*!
122 * \variable QxtWebRequestEvent::contentType
123 * Contains the MIME type of the request body, if present.
124 */
125
126/*!
127 * \variable QxtWebRequestEvent::content
128 * Contains the content of the request body, if present.
129 */
130
131/*!
132 * \variable QxtWebRequestEvent::cookies
133 * Contains all of the cookies sent by the web browser.
134 */
135
136/*!
137 * \variable QxtWebRequestEvent::headers
138 * Contains all of the headers sent by the web browser.
139 *
140 * Note that use of these values may not be portable across session maangers.
141 */
142
143/*
144QxtWebFileUploadEvent::QxtWebFileUploadEvent(int sessionID)
145: QxtWebEvent(QxtWebEvent::FileUpload, sessionID) {}
146*/
147
148/*!
149\class QxtWebErrorEvent
150
151\inmodule QxtWeb
152
153\brief The QxtWebErrorEvent class describes an error condition to be sent to a web browser
154
155The QxtWebErrorEvent class contains information about an error that will be
156sent to a web browser.
157
158QxtWebErrorEvent is a QxtWebPageEvent, so the \a dataSource may be replaced
159with a custom error page. If you choose to do this, be sure to delete the
160original data source automatically generated by the constructor first.
161*/
162
163/*!
164 * Constructs a QxtWebErrorEvent for the specified \a sessionID and \a requestID,
165 * with the provided \a statusCode and \a statusMessage.
166 *
167 * The requestID is an opaque value generated by the session manager; services
168 * will receive this value via QxtWebRequestEvent and must use it in every
169 * event that responds to that request.
170 */
171QxtWebErrorEvent::QxtWebErrorEvent(int sessionID, int requestID, int statusCode, QByteArray statusMessage)
172        : QxtWebPageEvent(sessionID, requestID, "<html><body><h1>" + statusMessage + "</h1></body></html>\r\n")
173{
174    status = statusCode;
175    QxtWebPageEvent::statusMessage = statusMessage;
176}
177
178/*!
179\class QxtWebPageEvent
180
181\inmodule QxtWeb
182
183\brief The QxtWebPageEvent class describes a web page or other content to be sent to a web browser
184
185The QxtWebPageEvent class contains information about a web page or other similar
186content that will be sent to a web browser.
187*/
188
189/*!
190 * Constructs a QxtWebPageEvent for the specified \a sessionID and \a requestID that will
191 * use the data from \a source as the content to be sent to the web browser.
192 *
193 * The requestID is an opaque value generated by the session manager; services
194 * will receive this value via QxtWebRequestEvent and must use it in every
195 * event that responds to that request.
196 *
197 * QxtWeb takes ownership of the source and will delete it when the response
198 * is completed.
199 */
200QxtWebPageEvent::QxtWebPageEvent(int sessionID, int requestID, QSharedPointer<QIODevice> source)
201        : QxtWebEvent(QxtWebEvent::Page, sessionID), dataSource(source), chunked(true), streaming(true), requestID(requestID),
202        status(200), statusMessage("OK"), contentType("text/html") {}
203
204/*!
205 * Constructs a QxtWebPageEvent for the specified \a sessionID and \a requestID that will
206 * use \a source as the content to be sent to the web browser.
207 *
208 * The requestID is an opaque value generated by the session manager; services
209 * will receive this value via QxtWebRequestEvent and must use it in every
210 * event that responds to that request.
211 */
212QxtWebPageEvent::QxtWebPageEvent(int sessionID, int requestID, QByteArray source)
213        : QxtWebEvent(QxtWebEvent::Page, sessionID), chunked(false), streaming(false), requestID(requestID),
214        status(200), statusMessage("OK"), contentType("text/html")
215{
216    QBuffer* buffer = new QBuffer;
217    buffer->setData(source);
218    buffer->open(QIODevice::ReadOnly);
219    dataSource = QSharedPointer<QIODevice>( buffer );
220}
221
222/*!
223 * \internal
224 */
225QxtWebPageEvent::QxtWebPageEvent(QxtWebEvent::EventType typeOverride, int sessionID, int requestID, QByteArray source)
226        : QxtWebEvent(typeOverride, sessionID), chunked(false), streaming(false), requestID(requestID),
227        status(200), statusMessage("OK"), contentType("text/html")
228{
229    QBuffer* buffer = new QBuffer;
230    buffer->setData(source);
231    buffer->open(QIODevice::ReadOnly);
232    dataSource = QSharedPointer<QIODevice>( buffer );
233}
234
235/*!
236 * Destroys the event
237 */
238QxtWebPageEvent::~QxtWebPageEvent()
239{
240
241}
242
243/*!
244 * \variable QxtWebPageEvent::dataSource
245 * Data will be read from this device and relayed to the web browser.
246 */
247
248/*!
249 * \variable QxtWebPageEvent::chunked
250 * If true, and if the web browser supports "chunked" encoding, the content
251 * will be sent using "chunked" encoding. If false, or if the browser does not
252 * support this encoding (for instance, HTTP/0.9 and HTTP/1.0 user agents),
253 * HTTP keep-alive will be disabled.
254 *
255 * The default value is true when using the QIODevice* constructor and false
256 * when using the QByteArray constructor.
257 */
258
259/*!
260 * \variable QxtWebPageEvent::streaming
261 * If true, the data source is considered to be a source of streaming data.
262 * The QIODevice must emit the readyRead() signal when data is available and
263 * must emit aboutToClose() after all data has been transferred. (This can
264 * be accomplished by invoking QIODevice::close() on it after all data is
265 * determined to have been transferred.)
266 *
267 * The default value is true when using the QIODevice* constructor and false
268 * when using the QByteArray constructor. If using a QIODevice that does not
269 * produce streaming data, such as QFile, this \a must be set to false to
270 * ensure correct behavior.
271 */
272
273/*!
274 * \variable QxtWebPageEvent::requestID
275 * Contains the opaque requestID provided by QxtWebRequestEvent.
276 */
277
278/*!
279 * \variable QxtWebPageEvent::status
280 * Contains the HTTP status code that will be sent with the response.
281 *
282 * The default value is 200 ("OK").
283 */
284
285/*!
286 * \variable QxtWebPageEvent::statusMessage
287 * Contains the human-readable message associated with the HTTP status code
288 * that will be sent with the response.
289 *
290 * The default value is "OK".
291 */
292
293/*!
294 * \variable QxtWebPageEvent::contentType
295 * Contains the MIME type of the content being sent to the web browser.
296 *
297 * The default value is "text/html".
298 */
299
300/*!
301 * \variable QxtWebPageEvent::headers
302 * Contains custom headers to be sent to the web browser.
303 *
304 * It is empty by default.
305 */
306
307/*!
308\class QxtWebStoreCookieEvent
309
310\inmodule QxtWeb
311
312\brief The QxtWebStoreCookieEvent class describes a cookie to be sent to a web browser
313
314The QxtWebStoreCookieEvent class instructs the session manager to store
315a cookie on the web browser.
316*/
317
318/*!
319 * Constructs a QxtWebStoreCookieEvent for the specified \a sessionID that will
320 * store a cookie with the specified \a name and \a data on the web browser.
321 *
322 * If an \a expiration date is supplied, it will be passed to the browser along
323 * with the cookie. The browser will delete the cookie automatically after
324 * the specified date. If an expiration date is not supplied, the cookie will
325 * expire when the browser is closed.
326 */
327QxtWebStoreCookieEvent::QxtWebStoreCookieEvent(int sessionID, QString name, QString data, QDateTime expiration)
328        : QxtWebEvent(QxtWebEvent::StoreCookie, sessionID), name(name), data(data), expiration(expiration) {}
329
330/*!
331 * \variable QxtWebStoreCookieEvent::name
332 * Contains the name of the cookie to be stored.
333 */
334
335/*!
336 * \variable QxtWebStoreCookieEvent::data
337 * Contains the content of the cookie to be stored.
338 */
339
340/*!
341 * \variable QxtWebStoreCookieEvent::expiration
342 * Contains the expiration date of the cookie to be stored. If null, the
343 * cookie will expire when the web browser is closed.
344 */
345
346/*!
347\class QxtWebRemoveCookieEvent
348
349\inmodule QxtWeb
350
351\brief The QxtWebRemoveCookieEvent class describes a cookie to be deleted from a web browser
352
353The QxtWebStoreCookieEvent class instructs the session manager to remove
354a cookie stored on the web browser.
355*/
356
357/*!
358 * Constructs a QxtWebRemoveCookieEvent for the specified \a sessionID that
359 * removed the cookie with \a name from the web browser.
360 */
361QxtWebRemoveCookieEvent::QxtWebRemoveCookieEvent(int sessionID, QString name)
362        : QxtWebEvent(QxtWebEvent::RemoveCookie, sessionID), name(name) {}
363
364/*!
365 * \variable QxtWebRemoveCookieEvent::name
366 * Contains the name of the cookie to be removed.
367 */
368
369/*!
370\class QxtWebRedirectEvent
371
372\inmodule QxtWeb
373
374\brief The QxtWebRedirectEvent class describes a redirect event to be sent to a web browser
375
376The QxtWebRedirectEvent class instructs the web browser to load a page found at
377another location.
378
379The default status code, 302, indicates that the requested page was found at
380a different location. Other useful status codes are 301, which indicates
381that the web browser should always use the new URL in place of the old one,
382and (in HTTP/1.1) 307, which indicates that the web browser should reissue
383the same request (including POST data) to the new URL.
384*/
385
386/*!
387 * Constructs a QxtWebRedirectEvent for the specified \a sessionID and \a requestID that
388 * instructs the browser to move to the specified \a destination URL with \a statusCode.
389 */
390QxtWebRedirectEvent::QxtWebRedirectEvent(int sessionID, int requestID, const QString& destination, int statusCode)
391        : QxtWebPageEvent(QxtWebEvent::Redirect, sessionID, requestID, QString("Redirect: <a href='%1'>%1</a>").arg(destination).toUtf8()), destination(destination)
392{
393    QxtWebPageEvent::status = statusCode;
394    QxtWebPageEvent::statusMessage = ("Redirect to " + destination).toUtf8();
395}
396
397/*!
398 * \variable QxtWebRedirectEvent::destination
399 * Contains the new location (absolute or relative) to which the browser
400 * should redirect.
401 */