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 void PlaQui::Server::Request::set_request(const std::string& req,
65 const std::string& host, unsigned port) {
67 std::cerr << __FILE__ << ": req = " << req << " | host = " << host
68 << " | port = " << port << std::endl;
70 PlaQui::Server::String request(req);
71 (*this)["REMOTE_HOST"] = host;
74 (*this)["REMOTE_PORT"] = ss.str();
75 // Parseo el GET de forma rudimentaria.
76 if (request.length() < 3) {
77 // FIXME - poner excepciones lindas.
78 throw "HTTP/1.1 501 Method Not Implemented";
81 std::string::size_type pos = request.find_first_of(
82 PlaQui::Server::String::SPACE_CHARS);
83 PlaQui::Server::String method = request.substr(0, pos);
84 if ((method.to_upper() == "GET") || (method.to_upper() == "POST")) {
85 (*this)["REQUEST_METHOD"] = method;
87 // FIXME - poner excepciones lindas.
88 throw "HTTP/1.1 501 Method Not Implemented";
90 // Si tiene sólo el método, no es válido.
91 request = request.substr(pos + 1);
93 if (!request.length()) {
94 // FIXME - poner excepciones lindas.
95 throw "HTTP/1.1 400 Bad Request";
97 // Si tiene más espacios, tengo la URI y el protocolo (o un error).
98 pos = request.find_first_of(PlaQui::Server::String::SPACE_CHARS);
99 if (pos != std::string::npos) {
100 // Si el resto es un protocolo válido, agrego más variables.
101 PlaQui::Server::String protocol = request.substr(pos + 1);
102 protocol = protocol.trim();
103 if ((PlaQui::Server::String(protocol).to_upper() == "HTTP/1.0")
104 || (PlaQui::Server::String(protocol).to_upper() == "HTTP/1.1")) {
105 (*this)["SERVER_PROTOCOL"] = protocol;
106 // Si no es un error.
108 // FIXME - poner excepciones lindas.
109 throw "HTTP/1.1 400 Bad Request";
111 request = request.substr(0, pos);
113 // Agrego la URI y sus derivados.
114 if (!request.length() || (request[0] != '/')) {
115 // FIXME || request.find_first_not_of(";/?:@&=+$,")) {
116 // FIXME - poner excepciones lindas.
117 throw "HTTP/1.1 400 Bad Request";
119 (*this)["REQUEST_URI"] = request;
120 // Si tiene un query string.
121 pos = request.find("?");
122 if (pos != std::string::npos) {
123 (*this)["QUERY_STRING"] = request.substr(pos + 1);
124 // No tiene query string.
126 (*this)["QUERY_STRING"] = "";
128 (*this)["SCRIPT_NAME"] = request.substr(0, pos);
131 void PlaQui::Server::Request::parse_header(const std::string& header) {
133 std::cerr << __FILE__ << ": header = " << header << std::endl;