123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- /***************************************************************************
- * Copyright (C) 2009, 2010 by Axel Jaeger <axeljaeger@googlemail.com> *
- * *
- * This file is part of Glowworm. *
- * *
- * Glowworm is free software: you can redistribute it and/or modify *
- * it under the terms of the GNU General Public License as published by *
- * the Free Software Foundation, version 3 of the License. *
- * *
- * Glowworm is distributed in the hope that it will be useful, *
- * but WITHOUT ANY WARRANTY; without even the implied warranty of *
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
- * GNU General Public License for more details. *
- * *
- * You should have received a copy of the GNU General Public License *
- * along with Glowworm. If not, see <http://www.gnu.org/licenses/>. *
- ***************************************************************************/
- #include "HttpResponse.h"
- #include <QIODevice>
- HttpResponse::HttpResponse()
- : m_outputDevice(NULL)
- , m_headerSent(false)
- {
- }
- HttpResponse::~HttpResponse()
- {
- }
- QIODevice* HttpResponse::outputDevice() const
- {
- return m_outputDevice;
- }
- void HttpResponse::setOutputDevice(QIODevice* device)
- {
- m_outputDevice = device;
- }
- void HttpResponse::setStatusCode(HttpStatusCode sc)
- {
- m_statusCode = sc;
- }
- HttpResponse::HttpStatusCode HttpResponse::statusCode() const
- {
- return m_statusCode;
- }
- void HttpResponse::setMimeType(const QString & mimeType)
- {
- m_mimeType = mimeType;
- }
- QString HttpResponse::mimeType() const
- {
- return m_mimeType;
- }
- int HttpResponse::contentLength() const
- {
- return m_contentLength;
- }
- void HttpResponse::setContentLength(int nsize)
- {
- m_contentLength = nsize;
- }
- void HttpResponse::sendHeader()
- {
- Q_ASSERT (headerSent() == false);
- Q_ASSERT (m_outputDevice != NULL);
- QString header(QString("HTTP/1.0 %1 %2\r\nContent-Type: %3\r\n\r\n")
- .arg(statusCode())
- .arg(statusVerbose(statusCode()))
- .arg(mimeType()));
- if (statusCode() != HttpOK) {
- header += QString("<html><body><h1>%1 %2</h1></body></html>\r\n").arg(statusCode()).arg(statusVerbose(statusCode()));
- }
- outputDevice()->write(qPrintable(header));
- m_headerSent = true;
- }
- bool HttpResponse::headerSent() const
- {
- return m_headerSent;
- }
- QString HttpResponse::statusVerbose(HttpStatusCode sc)
- {
- switch (sc) {
- case HttpOK:
- return "OK";
- case HttpForbidden:
- return "Forbidden";
- case HttpFileNotFound:
- return "File not found";
- case HttpInternalServerError:
- return "Internal Server Error";
- }
- return QString::null;
- }
|