HttpResponse.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /***************************************************************************
  2. * Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com> *
  3. * *
  4. * This file is part of Glowworm. *
  5. * *
  6. * Glowworm is free software: you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation, version 3 of the License. *
  9. * *
  10. * Glowworm is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with Glowworm. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #include "HttpResponse.h"
  19. #include <QIODevice>
  20. HttpResponse::HttpResponse()
  21. : m_outputDevice(NULL)
  22. , m_headerSent(false)
  23. {
  24. }
  25. HttpResponse::~HttpResponse()
  26. {
  27. }
  28. QIODevice* HttpResponse::outputDevice() const
  29. {
  30. return m_outputDevice;
  31. }
  32. void HttpResponse::setOutputDevice(QIODevice* device)
  33. {
  34. m_outputDevice = device;
  35. }
  36. void HttpResponse::setStatusCode(HttpStatusCode sc)
  37. {
  38. m_statusCode = sc;
  39. }
  40. HttpResponse::HttpStatusCode HttpResponse::statusCode() const
  41. {
  42. return m_statusCode;
  43. }
  44. void HttpResponse::setMimeType(const QString & mimeType)
  45. {
  46. m_mimeType = mimeType;
  47. }
  48. QString HttpResponse::mimeType() const
  49. {
  50. return m_mimeType;
  51. }
  52. int HttpResponse::contentLength() const
  53. {
  54. return m_contentLength;
  55. }
  56. void HttpResponse::setContentLength(int nsize)
  57. {
  58. m_contentLength = nsize;
  59. }
  60. void HttpResponse::sendHeader()
  61. {
  62. Q_ASSERT (headerSent() == false);
  63. Q_ASSERT (m_outputDevice != NULL);
  64. QString header(QString("HTTP/1.0 %1 %2\r\nContent-Type: %3\r\n\r\n")
  65. .arg(statusCode())
  66. .arg(statusVerbose(statusCode()))
  67. .arg(mimeType()));
  68. if (statusCode() != HttpOK) {
  69. header += QString("<html><body><h1>%1 %2</h1></body></html>\r\n").arg(statusCode()).arg(statusVerbose(statusCode()));
  70. }
  71. outputDevice()->write(qPrintable(header));
  72. m_headerSent = true;
  73. }
  74. bool HttpResponse::headerSent() const
  75. {
  76. return m_headerSent;
  77. }
  78. QString HttpResponse::statusVerbose(HttpStatusCode sc)
  79. {
  80. switch (sc) {
  81. case HttpOK:
  82. return "OK";
  83. case HttpForbidden:
  84. return "Forbidden";
  85. case HttpFileNotFound:
  86. return "File not found";
  87. case HttpInternalServerError:
  88. return "Internal Server Error";
  89. }
  90. return QString::null;
  91. }