1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
37 #include <anna/http/parser/WaitMessage.hpp>
39 #include <anna/http/internal/Token.hpp>
40 #include <anna/http/Transport.hpp>
41 #include <anna/http/Method.hpp>
42 #include <anna/http/Request.hpp>
43 #include <anna/http/Response.hpp>
48 //----------------------------------------------------------------------------------------------------
49 // Todavia no se ha identificado el comienzo del mensaje HTTP. Comprueba el primer token de la
50 // linea para comprobar si se puede identificar una mensaje HTTP de tipo 'Response' o 'Request'.
52 // La expresion del mensaje HTTP sera:
54 // Mensahe HTTP := Request | Response
56 // Resquest := Method <sp> URI <sp> version <crlf> *(Header)<crlf><crlf> [Body]
57 // Response := version <sp> status_code <sp> phrase <crlf> *(Header)
59 // (1) Si recibimos una linea vacia o con solo CRLF's
60 // (2) Separa el contenido de la linea en los tokens. El separador seran espacios en blanco, tabs, etc.
61 // (3) Si estamos recibiendo un Request.
62 // (4) Si estamos recibiendo un Response
63 //----------------------------------------------------------------------------------------------------
64 int http::parser::WaitMessage::processLine(http::Transport& transport, const DataBlock&, const http::Token& line) const
65 throw(RuntimeException) {
66 if(line.getSize() == 0) // (1)
69 const Tokenizer& tokenizer = transport.split(line); // (2)
70 const Token* token = tokenizer [tokenizer.begin()];
75 if(Method::asType(token) != Method::Type::None) // (3)
76 if(setupRequest(transport, tokenizer) == true)
80 if(token->match("http/1.1") || token->match("http/1.0")) // (4)
81 setupResponse(transport, tokenizer);
87 //----------------------------------------------------------------------------------------------------
88 // (1) Verifica que en la linea solo esten los tres elementos esperados
89 //----------------------------------------------------------------------------------------------------
91 bool http::parser::WaitMessage::setupRequest(http::Transport& transport, const http::Tokenizer& tokenizer)
95 Tokenizer::const_iterator ii = tokenizer.begin();
98 if((token = tokenizer [ii ++]) == NULL)
101 const Method::Type::_v method = Method::asType(token);
103 if((token = tokenizer [ii ++]) == NULL)
106 const string& uri(token->getStringValue());
108 if((token = tokenizer [ii ++]) == NULL)
111 if(tokenizer [ii] != NULL) // (1)
114 const string& version(token->getStringValue());
115 http::Request* request = static_cast <http::Request*>(transport.allocateInputMessage(Message::Type::Request));
116 request->setMethod(method);
117 request->setURI(uri);
118 request->setVersion(version);
119 setState(transport, ClassType::ReadHeader);
120 } catch(RuntimeException&) {
127 //----------------------------------------------------------------------------------------------------
128 // (1) IN_NEMRD_01.00.00_http_001
129 //----------------------------------------------------------------------------------------------------
131 void http::parser::WaitMessage::setupResponse(http::Transport& transport, const http::Tokenizer& tokenizer)
134 Tokenizer::const_iterator ii = tokenizer.begin();
137 if((token = tokenizer [ii ++]) == NULL)
140 const string& version(token->getStringValue());
142 if((token = tokenizer [ii ++]) == NULL)
145 const int statusCode(token->getIntegerValue());
148 if((token = tokenizer [ii ++]) != NULL) { // (1)
149 reasonPhrase = token->getStringValue();
151 while((token = tokenizer [ii ++]) != NULL) {
153 reasonPhrase += token->getStringValue();
157 http::Response* response = static_cast <http::Response*>(transport.allocateInputMessage(Message::Type::Response));
158 response->setVersion(version);
159 response->setStatusCode(statusCode);
160 response->setReasonPhrase(reasonPhrase);
161 setState(transport, ClassType::ReadHeader);
162 } catch(RuntimeException&) {