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"
38 PlaQui::Server::Request::CHARS_DIGIT = "0123456789";
41 PlaQui::Server::Request::CHARS_LOWALPHA = "abcdefghijklmnopqrstuvwxyz";
44 PlaQui::Server::Request::CHARS_UPALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
47 PlaQui::Server::Request::CHARS_ALPHA = CHARS_LOWALPHA + CHARS_UPALPHA;
50 PlaQui::Server::Request::CHARS_ALPHANUM = CHARS_DIGIT + CHARS_ALPHA;
53 PlaQui::Server::Request::CHARS_RESERVED = ";/?:@&=+$,";
56 PlaQui::Server::Request::CHARS_MARK = "-_.!~*'()";
59 PlaQui::Server::Request::CHARS_UNRESERVED = CHARS_ALPHANUM + CHARS_MARK;
62 PlaQui::Server::Request::CHARS_HEX = CHARS_DIGIT + std::string("abcdefABCDEF");
64 PlaQui::Server::Request::~Request(void) {
67 void PlaQui::Server::Request::set_request(const std::string& req,
68 const std::string& host, unsigned port) {
70 std::cerr << __FILE__ << ": req = " << req << " | host = " << host
71 << " | port = " << port << std::endl;
74 (*this)["REMOTE_HOST"] = host;
77 (*this)["REMOTE_PORT"] = ss.str();
78 // Parseo el GET de forma rudimentaria.
79 if (request.length() < 3) {
80 // FIXME - poner excepciones lindas.
81 throw "HTTP/1.1 501 Method Not Implemented";
84 std::string::size_type pos = request.find_first_of(String::SPACE_CHARS);
85 String method = request.substr(0, pos);
86 if ((method.to_upper() == "GET") || (method.to_upper() == "POST")) {
87 (*this)["REQUEST_METHOD"] = method;
89 // FIXME - poner excepciones lindas.
90 throw "HTTP/1.1 501 Method Not Implemented";
92 // Si tiene sólo el método, no es válido.
93 request = request.substr(pos + 1);
95 if (!request.length()) {
96 // FIXME - poner excepciones lindas.
97 throw "HTTP/1.1 400 Bad Request";
99 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
100 pos = request.find_first_of(String::SPACE_CHARS);
101 if (pos != std::string::npos) {
102 // Si el resto es un protocolo válido, agrego más variables.
103 String protocol = request.substr(pos + 1);
104 protocol = protocol.trim();
105 if ((String(protocol).to_upper() == "HTTP/1.0")
106 || (String(protocol).to_upper() == "HTTP/1.1")) {
107 (*this)["SERVER_PROTOCOL"] = protocol;
108 // Si no es un error.
110 // FIXME - poner excepciones lindas.
111 throw "HTTP/1.1 400 Bad Request";
113 request = request.substr(0, pos);
115 // Agrego la URI y sus derivados.
116 if (!request.length() || (request[0] != '/')) {
117 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
118 // FIXME - poner excepciones lindas.
119 throw "HTTP/1.1 400 Bad Request";
121 (*this)["REQUEST_URI"] = request;
122 // Si tiene un query string.
123 pos = request.find("?");
124 if (pos != std::string::npos) {
125 (*this)["QUERY_STRING"] = request.substr(pos + 1);
126 // No tiene query string.
128 (*this)["QUERY_STRING"] = "";
130 (*this)["SCRIPT_NAME"] = request.substr(0, pos);
133 void PlaQui::Server::Request::parse_header(const std::string& header) {
135 std::cerr << __FILE__ << ": header = " << header << std::endl;