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;
73 PlaQui::Server::String request(req);
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(
85 PlaQui::Server::String::SPACE_CHARS);
86 PlaQui::Server::String method = request.substr(0, pos);
87 if ((method.to_upper() == "GET") || (method.to_upper() == "POST")) {
88 (*this)["REQUEST_METHOD"] = method;
90 // FIXME - poner excepciones lindas.
91 throw "HTTP/1.1 501 Method Not Implemented";
93 // Si tiene sólo el método, no es válido.
94 request = request.substr(pos + 1);
96 if (!request.length()) {
97 // FIXME - poner excepciones lindas.
98 throw "HTTP/1.1 400 Bad Request";
100 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
101 pos = request.find_first_of(PlaQui::Server::String::SPACE_CHARS);
102 if (pos != std::string::npos) {
103 // Si el resto es un protocolo válido, agrego más variables.
104 PlaQui::Server::String protocol = request.substr(pos + 1);
105 protocol = protocol.trim();
106 if ((PlaQui::Server::String(protocol).to_upper() == "HTTP/1.0")
107 || (PlaQui::Server::String(protocol).to_upper() == "HTTP/1.1")) {
108 (*this)["SERVER_PROTOCOL"] = protocol;
109 // Si no es un error.
111 // FIXME - poner excepciones lindas.
112 throw "HTTP/1.1 400 Bad Request";
114 request = request.substr(0, pos);
116 // Agrego la URI y sus derivados.
117 if (!request.length() || (request[0] != '/')) {
118 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
119 // FIXME - poner excepciones lindas.
120 throw "HTTP/1.1 400 Bad Request";
122 (*this)["REQUEST_URI"] = request;
123 // Si tiene un query string.
124 pos = request.find("?");
125 if (pos != std::string::npos) {
126 (*this)["QUERY_STRING"] = request.substr(pos + 1);
127 // No tiene query string.
129 (*this)["QUERY_STRING"] = "";
131 (*this)["SCRIPT_NAME"] = request.substr(0, pos);
134 void PlaQui::Server::Request::parse_header(const std::string& header) {
136 std::cerr << __FILE__ << ": header = " << header << std::endl;