1 // vim: set noexpandtab tabstop=4 shiftwidth=4:
2 //----------------------------------------------------------------------------
4 //----------------------------------------------------------------------------
5 // This file is part of PlaQui.
7 // PlaQui is free software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the Free Software
9 // Foundation; either version 2 of the License, or (at your option) any later
12 // PlaQui is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 // FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 // You should have received a copy of the GNU General Public License along
18 // with PlaQui; if not, write to the Free Software Foundation, Inc., 59 Temple
19 // Place, Suite 330, Boston, MA 02111-1307 USA
20 //----------------------------------------------------------------------------
21 // Creado: dom oct 19 16:41:15 ART 2003
22 // Autores: Leandro Lucarella <llucare@fi.uba.ar>
23 //----------------------------------------------------------------------------
28 #include "plaqui/server/request.h"
29 #include "plaqui/server/string.h"
37 using namespace PlaQui::Server;
39 const std::string Request::CHARS_DIGIT = "0123456789";
41 const std::string Request::CHARS_LOWALPHA = "abcdefghijklmnopqrstuvwxyz";
43 const std::string Request::CHARS_UPALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
45 const std::string Request::CHARS_ALPHA = CHARS_LOWALPHA + CHARS_UPALPHA;
47 const std::string Request::CHARS_ALPHANUM = CHARS_DIGIT + CHARS_ALPHA;
49 const std::string Request::CHARS_RESERVED = ";/?:@&=+$,";
51 const std::string Request::CHARS_MARK = "-_.!~*'()";
53 const std::string Request::CHARS_UNRESERVED = CHARS_ALPHANUM + CHARS_MARK;
55 const std::string Request::CHARS_HEX = CHARS_DIGIT + std::string("abcdefABCDEF");
57 void Request::set_request(const std::string& req, const std::string& host,
60 std::cerr << __FILE__ << ": req = " << req << " | host = " << host
61 << " | port = " << port << std::endl;
64 (*this)["REMOTE_HOST"] = host;
67 (*this)["REMOTE_PORT"] = ss.str();
68 // Parseo el GET de forma rudimentaria.
69 if (request.length() < 3) {
70 // FIXME - poner excepciones lindas.
71 throw "HTTP/1.1 501 Method Not Implemented";
74 std::string::size_type pos = request.find_first_of(String::SPACE_CHARS);
75 String method = request.substr(0, pos);
76 if ((method.to_upper() == "GET") || (method.to_upper() == "POST")) {
77 (*this)["REQUEST_METHOD"] = method;
79 // FIXME - poner excepciones lindas.
80 throw "HTTP/1.1 501 Method Not Implemented";
82 // Si tiene sólo el método, no es válido.
83 request = request.substr(pos + 1);
85 if (!request.length()) {
86 // FIXME - poner excepciones lindas.
87 throw "HTTP/1.1 400 Bad Request";
89 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
90 pos = request.find_first_of(String::SPACE_CHARS);
91 if (pos != std::string::npos) {
92 // Si el resto es un protocolo válido, agrego más variables.
93 String protocol = request.substr(pos + 1);
94 protocol = protocol.trim();
95 if ((String(protocol).to_upper() == "HTTP/1.0")
96 || (String(protocol).to_upper() == "HTTP/1.1")) {
97 (*this)["SERVER_PROTOCOL"] = protocol;
100 // FIXME - poner excepciones lindas.
101 throw "HTTP/1.1 400 Bad Request";
103 request = request.substr(0, pos);
105 // Agrego la URI y sus derivados.
106 if (!request.length() || (request[0] != '/')) {
107 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
108 // FIXME - poner excepciones lindas.
109 throw "HTTP/1.1 400 Bad Request";
111 (*this)["REQUEST_URI"] = request;
112 // Si tiene un query string.
113 pos = request.find("?");
114 if (pos != std::string::npos) {
115 (*this)["QUERY_STRING"] = request.substr(pos + 1);
116 // No tiene query string.
118 (*this)["QUERY_STRING"] = "";
120 (*this)["SCRIPT_NAME"] = request.substr(0, pos);
123 void Request::parse_header(const std::string& header) {
125 std::cerr << __FILE__ << ": header = " << header << std::endl;