ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

getclienthandler.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "backends/networking/sdl_net/getclienthandler.h"
00024 #include "common/textconsole.h"
00025 
00026 namespace Networking {
00027 
00028 GetClientHandler::GetClientHandler(Common::SeekableReadStream *stream):
00029     _responseCode(200), _headersPrepared(false),
00030     _stream(stream), _buffer(new byte[CLIENT_HANDLER_BUFFER_SIZE]) {}
00031 
00032 GetClientHandler::~GetClientHandler() {
00033     delete _stream;
00034     delete[] _buffer;
00035 }
00036 
00037 const char *GetClientHandler::responseMessage(long responseCode) {
00038     switch (responseCode) {
00039     case 100: return "Continue";
00040     case 101: return "Switching Protocols";
00041     case 102: return "Processing";
00042 
00043     case 200: return "OK";
00044     case 201: return "Created";
00045     case 202: return "Accepted";
00046     case 203: return "Non-Authoritative Information";
00047     case 204: return "No Content";
00048     case 205: return "Reset Content";
00049     case 206: return "Partial Content";
00050     case 207: return "Multi-Status";
00051     case 226: return "IM Used";
00052 
00053     case 300: return "Multiple Choices";
00054     case 301: return "Moved Permanently";
00055     case 302: return "Moved Temporarily"; //case 302: return "Found";
00056     case 303: return "See Other";
00057     case 304: return "Not Modified";
00058     case 305: return "Use Proxy";
00059     case 306: return "RESERVED";
00060     case 307: return "Temporary Redirect";
00061 
00062     case 400: return "Bad Request";
00063     case 401: return "Unauthorized";
00064     case 402: return "Payment Required";
00065     case 403: return "Forbidden";
00066     case 404: return "Not Found";
00067     case 405: return "Method Not Allowed";
00068     case 406: return "Not Acceptable";
00069     case 407: return "Proxy Authentication Required";
00070     case 408: return "Request Timeout";
00071     case 409: return "Conflict";
00072     case 410: return "Gone";
00073     case 411: return "Length Required";
00074     case 412: return "Precondition Failed";
00075     case 413: return "Request Entity Too Large";
00076     case 414: return "Request-URI Too Large";
00077     case 415: return "Unsupported Media Type";
00078     case 416: return "Requested Range Not Satisfiable";
00079     case 417: return "Expectation Failed";
00080     case 422: return "Unprocessable Entity";
00081     case 423: return "Locked";
00082     case 424: return "Failed Dependency";
00083     case 425: return "Unordered Collection";
00084     case 426: return "Upgrade Required";
00085     case 428: return "Precondition Required";
00086     case 429: return "Too Many Requests";
00087     case 431: return "Request Header Fields Too Large";
00088     case 434: return "Requested Host Unavailable";
00089     case 449: return "Retry With";
00090     case 451: return "Unavailable For Legal Reasons";
00091 
00092     case 500: return "Internal Server Error";
00093     case 501: return "Not Implemented";
00094     case 502: return "Bad Gateway";
00095     case 503: return "Service Unavailable";
00096     case 504: return "Gateway Timeout";
00097     case 505: return "HTTP Version Not Supported";
00098     case 506: return "Variant Also Negotiates";
00099     case 507: return "Insufficient Storage";
00100     case 508: return "Loop Detected";
00101     case 509: return "Bandwidth Limit Exceeded";
00102     case 510: return "Not Extended";
00103     case 511: return "Network Authentication Required";
00104     }
00105     return "Unknown";
00106 }
00107 
00108 void GetClientHandler::prepareHeaders() {
00109     if (!_specialHeaders.contains("Content-Type"))
00110         setHeader("Content-Type", "text/html");
00111 
00112     if (!_specialHeaders.contains("Content-Length") && _stream)
00113         setHeader("Content-Length", Common::String::format("%u", _stream->size()));
00114 
00115     _headers = Common::String::format("HTTP/1.1 %ld %s\r\n", _responseCode, responseMessage(_responseCode));
00116     for (Common::HashMap<Common::String, Common::String>::iterator i = _specialHeaders.begin(); i != _specialHeaders.end(); ++i)
00117         _headers += i->_key + ": " + i->_value + "\r\n";
00118     _headers += "\r\n";
00119 
00120     _headersPrepared = true;
00121 }
00122 
00123 void GetClientHandler::handle(Client *client) {
00124     if (!client)
00125         return;
00126     if (!_headersPrepared)
00127         prepareHeaders();
00128 
00129     uint32 readBytes;
00130 
00131     // send headers first
00132     if (_headers.size() > 0) {
00133         readBytes = _headers.size();
00134         if (readBytes > CLIENT_HANDLER_BUFFER_SIZE)
00135             readBytes = CLIENT_HANDLER_BUFFER_SIZE;
00136         memcpy(_buffer, _headers.c_str(), readBytes);
00137         _headers.erase(0, readBytes);
00138     } else {
00139         if (!_stream) {
00140             client->close();
00141             return;
00142         }
00143 
00144         readBytes = _stream->read(_buffer, CLIENT_HANDLER_BUFFER_SIZE);
00145     }
00146 
00147     if (readBytes != 0)
00148         if (client->send(_buffer, readBytes) != (int)readBytes) {
00149             warning("GetClientHandler: unable to send all bytes to the client");
00150             client->close();
00151             return;
00152         }
00153 
00154     // we're done here!
00155     if (_stream->eos())
00156         client->close();
00157 }
00158 
00159 void GetClientHandler::setHeader(Common::String name, Common::String value) { _specialHeaders[name] = value; }
00160 void GetClientHandler::setResponseCode(long code) { _responseCode = code; }
00161 
00162 } // End of namespace Networking


Generated on Sat Feb 16 2019 05:00:51 for ResidualVM by doxygen 1.7.1
curved edge   curved edge