/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. #include "qxtwebevent.h"
  26. #include "qxtwebcontent.h"
  27. #include <QBuffer>
  28. /*!
  29. \class QxtWebEvent
  30. \inmodule QxtWeb
  31. \brief The QxtWebEvent class is a base class of all QxtWeb event types
  32. QxtWebEvent is the base class for all QxtWeb event classes. Event objects
  33. contain event parameters.
  34. The base QxtWebEvent class contains the type of the event and a session ID for
  35. the session it relates to. Subclasses of QxtWebEvent contain additional
  36. parameters describing the particular event.
  37. */
  38. /*!
  39. \enum QxtWebEvent::EventType
  40. \value None Not an event.
  41. \value Request A request event.
  42. \value FileUpload A file upload event.
  43. \value Page A page event.
  44. \value StoreCookie A store cookie event.
  45. \value RemoveCookie A remove cookie event.
  46. \value Redirect A redirect event.
  47. */
  48. /*!
  49. * Constructs a QxtWebEvent of the specified \a type for the specified \a sessionID.
  50. */
  51. QxtWebEvent::QxtWebEvent(EventType type, int sessionID)
  52. : sessionID(sessionID), m_type(type) {}
  53. /*!
  54. * Destroys the event.
  55. */
  56. QxtWebEvent::~QxtWebEvent() {}
  57. /*!
  58. * \fn EventType QxtWebEvent::type() const
  59. * Returns the event type.
  60. */
  61. /*!
  62. * \variable QxtWebEvent::sessionID
  63. * Contains the ID of the session the event is related to.
  64. */
  65. /*!
  66. \class QxtWebRequestEvent
  67. \inmodule QxtWeb
  68. \brief The QxtWebRequestEvent class describes a request from a web browser
  69. The QxtWebRequestEvent class contains information about a request from a web
  70. browser.
  71. */
  72. /*!
  73. * Constructs a QxtWebRequestEvent for the specified \a sessionID, \a requestID and \a url.
  74. */
  75. QxtWebRequestEvent::QxtWebRequestEvent(int sessionID, int requestID, const QUrl& url)
  76. : QxtWebEvent(QxtWebEvent::Request, sessionID), requestID(requestID), url(url), originalUrl(url) {}
  77. /*!
  78. * Destroys the event and any content that may still be associated with it.
  79. */
  80. QxtWebRequestEvent::~QxtWebRequestEvent()
  81. {
  82. if (content) delete content;
  83. }
  84. /*!
  85. * \variable QxtWebRequestEvent::requestID
  86. * Contains an opaque value generated by the session manager. This request ID
  87. * must be included in the QxtWebPageEvent or QxtWebPageEvent subclass that
  88. * is the response to the request.
  89. */
  90. /*!
  91. * \variable QxtWebRequestEvent::url
  92. * Contains the request URL, possibly after rewriting by intermediate services
  93. * such as QxtWebServiceDirectory.
  94. */
  95. /*!
  96. * \variable QxtWebRequestEvent::originalUrl
  97. * Contains the request URL exactly as it was sent from the web browser.
  98. */
  99. /*!
  100. * \variable QxtWebRequestEvent::contentType
  101. * Contains the MIME type of the request body, if present.
  102. */
  103. /*!
  104. * \variable QxtWebRequestEvent::content
  105. * Contains the content of the request body, if present.
  106. */
  107. /*!
  108. * \variable QxtWebRequestEvent::cookies
  109. * Contains all of the cookies sent by the web browser.
  110. */
  111. /*!
  112. * \variable QxtWebRequestEvent::headers
  113. * Contains all of the headers sent by the web browser.
  114. *
  115. * Note that use of these values may not be portable across session maangers.
  116. */
  117. /*
  118. QxtWebFileUploadEvent::QxtWebFileUploadEvent(int sessionID)
  119. : QxtWebEvent(QxtWebEvent::FileUpload, sessionID) {}
  120. */
  121. /*!
  122. \class QxtWebErrorEvent
  123. \inmodule QxtWeb
  124. \brief The QxtWebErrorEvent class describes an error condition to be sent to a web browser
  125. The QxtWebErrorEvent class contains information about an error that will be
  126. sent to a web browser.
  127. QxtWebErrorEvent is a QxtWebPageEvent, so the \a dataSource may be replaced
  128. with a custom error page. If you choose to do this, be sure to delete the
  129. original data source automatically generated by the constructor first.
  130. */
  131. /*!
  132. * Constructs a QxtWebErrorEvent for the specified \a sessionID and \a requestID,
  133. * with the provided \a statusCode and \a statusMessage.
  134. *
  135. * The requestID is an opaque value generated by the session manager; services
  136. * will receive this value via QxtWebRequestEvent and must use it in every
  137. * event that responds to that request.
  138. */
  139. QxtWebErrorEvent::QxtWebErrorEvent(int sessionID, int requestID, int statusCode, QByteArray statusMessage)
  140. : QxtWebPageEvent(sessionID, requestID, "<html><body><h1>" + statusMessage + "</h1></body></html>\r\n")
  141. {
  142. status = statusCode;
  143. QxtWebPageEvent::statusMessage = statusMessage;
  144. }
  145. /*!
  146. \class QxtWebPageEvent
  147. \inmodule QxtWeb
  148. \brief The QxtWebPageEvent class describes a web page or other content to be sent to a web browser
  149. The QxtWebPageEvent class contains information about a web page or other similar
  150. content that will be sent to a web browser.
  151. */
  152. /*!
  153. * Constructs a QxtWebPageEvent for the specified \a sessionID and \a requestID that will
  154. * use the data from \a source as the content to be sent to the web browser.
  155. *
  156. * The requestID is an opaque value generated by the session manager; services
  157. * will receive this value via QxtWebRequestEvent and must use it in every
  158. * event that responds to that request.
  159. *
  160. * QxtWeb takes ownership of the source and will delete it when the response
  161. * is completed.
  162. */
  163. QxtWebPageEvent::QxtWebPageEvent(int sessionID, int requestID, QSharedPointer<QIODevice> source)
  164. : QxtWebEvent(QxtWebEvent::Page, sessionID), dataSource(source), chunked(true), streaming(true), requestID(requestID),
  165. status(200), statusMessage("OK"), contentType("text/html") {}
  166. /*!
  167. * Constructs a QxtWebPageEvent for the specified \a sessionID and \a requestID that will
  168. * use \a source as the content to be sent to the web browser.
  169. *
  170. * The requestID is an opaque value generated by the session manager; services
  171. * will receive this value via QxtWebRequestEvent and must use it in every
  172. * event that responds to that request.
  173. */
  174. QxtWebPageEvent::QxtWebPageEvent(int sessionID, int requestID, QByteArray source)
  175. : QxtWebEvent(QxtWebEvent::Page, sessionID), chunked(false), streaming(false), requestID(requestID),
  176. status(200), statusMessage("OK"), contentType("text/html")
  177. {
  178. QBuffer* buffer = new QBuffer;
  179. buffer->setData(source);
  180. buffer->open(QIODevice::ReadOnly);
  181. dataSource = QSharedPointer<QIODevice>( buffer );
  182. }
  183. /*!
  184. * \internal
  185. */
  186. QxtWebPageEvent::QxtWebPageEvent(QxtWebEvent::EventType typeOverride, int sessionID, int requestID, QByteArray source)
  187. : QxtWebEvent(typeOverride, sessionID), chunked(false), streaming(false), requestID(requestID),
  188. status(200), statusMessage("OK"), contentType("text/html")
  189. {
  190. QBuffer* buffer = new QBuffer;
  191. buffer->setData(source);
  192. buffer->open(QIODevice::ReadOnly);
  193. dataSource = QSharedPointer<QIODevice>( buffer );
  194. }
  195. /*!
  196. * Destroys the event
  197. */
  198. QxtWebPageEvent::~QxtWebPageEvent()
  199. {
  200. }
  201. /*!
  202. * \variable QxtWebPageEvent::dataSource
  203. * Data will be read from this device and relayed to the web browser.
  204. */
  205. /*!
  206. * \variable QxtWebPageEvent::chunked
  207. * If true, and if the web browser supports "chunked" encoding, the content
  208. * will be sent using "chunked" encoding. If false, or if the browser does not
  209. * support this encoding (for instance, HTTP/0.9 and HTTP/1.0 user agents),
  210. * HTTP keep-alive will be disabled.
  211. *
  212. * The default value is true when using the QIODevice* constructor and false
  213. * when using the QByteArray constructor.
  214. */
  215. /*!
  216. * \variable QxtWebPageEvent::streaming
  217. * If true, the data source is considered to be a source of streaming data.
  218. * The QIODevice must emit the readyRead() signal when data is available and
  219. * must emit aboutToClose() after all data has been transferred. (This can
  220. * be accomplished by invoking QIODevice::close() on it after all data is
  221. * determined to have been transferred.)
  222. *
  223. * The default value is true when using the QIODevice* constructor and false
  224. * when using the QByteArray constructor. If using a QIODevice that does not
  225. * produce streaming data, such as QFile, this \a must be set to false to
  226. * ensure correct behavior.
  227. */
  228. /*!
  229. * \variable QxtWebPageEvent::requestID
  230. * Contains the opaque requestID provided by QxtWebRequestEvent.
  231. */
  232. /*!
  233. * \variable QxtWebPageEvent::status
  234. * Contains the HTTP status code that will be sent with the response.
  235. *
  236. * The default value is 200 ("OK").
  237. */
  238. /*!
  239. * \variable QxtWebPageEvent::statusMessage
  240. * Contains the human-readable message associated with the HTTP status code
  241. * that will be sent with the response.
  242. *
  243. * The default value is "OK".
  244. */
  245. /*!
  246. * \variable QxtWebPageEvent::contentType
  247. * Contains the MIME type of the content being sent to the web browser.
  248. *
  249. * The default value is "text/html".
  250. */
  251. /*!
  252. * \variable QxtWebPageEvent::headers
  253. * Contains custom headers to be sent to the web browser.
  254. *
  255. * It is empty by default.
  256. */
  257. /*!
  258. \class QxtWebStoreCookieEvent
  259. \inmodule QxtWeb
  260. \brief The QxtWebStoreCookieEvent class describes a cookie to be sent to a web browser
  261. The QxtWebStoreCookieEvent class instructs the session manager to store
  262. a cookie on the web browser.
  263. */
  264. /*!
  265. * Constructs a QxtWebStoreCookieEvent for the specified \a sessionID that will
  266. * store a cookie with the specified \a name and \a data on the web browser.
  267. *
  268. * If an \a expiration date is supplied, it will be passed to the browser along
  269. * with the cookie. The browser will delete the cookie automatically after
  270. * the specified date. If an expiration date is not supplied, the cookie will
  271. * expire when the browser is closed.
  272. */
  273. QxtWebStoreCookieEvent::QxtWebStoreCookieEvent(int sessionID, QString name, QString data, QDateTime expiration)
  274. : QxtWebEvent(QxtWebEvent::StoreCookie, sessionID), name(name), data(data), expiration(expiration) {}
  275. /*!
  276. * \variable QxtWebStoreCookieEvent::name
  277. * Contains the name of the cookie to be stored.
  278. */
  279. /*!
  280. * \variable QxtWebStoreCookieEvent::data
  281. * Contains the content of the cookie to be stored.
  282. */
  283. /*!
  284. * \variable QxtWebStoreCookieEvent::expiration
  285. * Contains the expiration date of the cookie to be stored. If null, the
  286. * cookie will expire when the web browser is closed.
  287. */
  288. /*!
  289. \class QxtWebRemoveCookieEvent
  290. \inmodule QxtWeb
  291. \brief The QxtWebRemoveCookieEvent class describes a cookie to be deleted from a web browser
  292. The QxtWebStoreCookieEvent class instructs the session manager to remove
  293. a cookie stored on the web browser.
  294. */
  295. /*!
  296. * Constructs a QxtWebRemoveCookieEvent for the specified \a sessionID that
  297. * removed the cookie with \a name from the web browser.
  298. */
  299. QxtWebRemoveCookieEvent::QxtWebRemoveCookieEvent(int sessionID, QString name)
  300. : QxtWebEvent(QxtWebEvent::RemoveCookie, sessionID), name(name) {}
  301. /*!
  302. * \variable QxtWebRemoveCookieEvent::name
  303. * Contains the name of the cookie to be removed.
  304. */
  305. /*!
  306. \class QxtWebRedirectEvent
  307. \inmodule QxtWeb
  308. \brief The QxtWebRedirectEvent class describes a redirect event to be sent to a web browser
  309. The QxtWebRedirectEvent class instructs the web browser to load a page found at
  310. another location.
  311. The default status code, 302, indicates that the requested page was found at
  312. a different location. Other useful status codes are 301, which indicates
  313. that the web browser should always use the new URL in place of the old one,
  314. and (in HTTP/1.1) 307, which indicates that the web browser should reissue
  315. the same request (including POST data) to the new URL.
  316. */
  317. /*!
  318. * Constructs a QxtWebRedirectEvent for the specified \a sessionID and \a requestID that
  319. * instructs the browser to move to the specified \a destination URL with \a statusCode.
  320. */
  321. QxtWebRedirectEvent::QxtWebRedirectEvent(int sessionID, int requestID, const QString& destination, int statusCode)
  322. : QxtWebPageEvent(QxtWebEvent::Redirect, sessionID, requestID, QString("Redirect: <a href='%1'>%1</a>").arg(destination).toUtf8()), destination(destination)
  323. {
  324. QxtWebPageEvent::status = statusCode;
  325. QxtWebPageEvent::statusMessage = ("Redirect to " + destination).toUtf8();
  326. }
  327. /*!
  328. * \variable QxtWebRedirectEvent::destination
  329. * Contains the new location (absolute or relative) to which the browser
  330. * should redirect.
  331. */