Updated license
[anna.git] / source / http / parser / WaitMessage.cpp
1 // ANNA - Anna is Not Nothingness Anymore
2 //
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
4 //
5 // https://bitbucket.org/testillano/anna
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
9 // are met:
10 //
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
16 // distribution.
17 //     * Neither the name of Google Inc. 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.
20 //
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.
32 //
33 // Authors: eduardo.ramos.testillano@gmail.com
34 //          cisco.tierra@gmail.com
35
36
37 #include <anna/http/parser/WaitMessage.hpp>
38
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>
44
45 using namespace std;
46 using namespace anna;
47
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'.
51 //
52 // La expresion del mensaje HTTP sera:
53 //
54 // Mensahe HTTP := Request | Response
55 //
56 // Resquest := Method <sp> URI <sp> version <crlf> *(Header)<crlf><crlf> [Body]
57 // Response := version <sp> status_code <sp> phrase <crlf> *(Header)
58 //
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)
67     return -1;
68
69   const Tokenizer& tokenizer = transport.split(line);                             // (2)
70   const Token* token = tokenizer [tokenizer.begin()];
71
72   if(token == NULL)
73     return -1;
74
75   if(Method::asType(token) != Method::Type::None)                                  // (3)
76     if(setupRequest(transport, tokenizer) == true)
77       return -1;
78
79   if(token != NULL) {
80     if(token->match("http/1.1") || token->match("http/1.0"))                      // (4)
81       setupResponse(transport, tokenizer);
82   }
83
84   return -1;
85 }
86
87 //----------------------------------------------------------------------------------------------------
88 // (1) Verifica que en la linea solo esten los tres elementos esperados
89 //----------------------------------------------------------------------------------------------------
90 /*static*/
91 bool http::parser::WaitMessage::setupRequest(http::Transport& transport, const http::Tokenizer& tokenizer)
92 throw() {
93   bool result = true;
94   const Token* token;
95   Tokenizer::const_iterator ii = tokenizer.begin();
96
97   try {
98     if((token = tokenizer [ii ++]) == NULL)
99       return false;
100
101     const Method::Type::_v method = Method::asType(token);
102
103     if((token = tokenizer [ii ++]) == NULL)
104       return false;
105
106     const string& uri(token->getStringValue());
107
108     if((token = tokenizer [ii ++]) == NULL)
109       return false;
110
111     if(tokenizer [ii] != NULL)                                           // (1)
112       return false;
113
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&) {
121     result = false;
122   }
123
124   return result;
125 }
126
127 //----------------------------------------------------------------------------------------------------
128 // (1) IN_NEMRD_01.00.00_http_001
129 //----------------------------------------------------------------------------------------------------
130 /*static*/
131 void http::parser::WaitMessage::setupResponse(http::Transport& transport, const http::Tokenizer& tokenizer)
132 throw() {
133   const Token* token;
134   Tokenizer::const_iterator ii = tokenizer.begin();
135
136   try {
137     if((token = tokenizer [ii ++]) == NULL)
138       return;
139
140     const string& version(token->getStringValue());
141
142     if((token = tokenizer [ii ++]) == NULL)
143       return;
144
145     const int statusCode(token->getIntegerValue());
146     string reasonPhrase;
147
148     if((token = tokenizer [ii ++]) != NULL) {                               // (1)
149       reasonPhrase = token->getStringValue();
150
151       while((token = tokenizer [ii ++]) != NULL) {
152         reasonPhrase += ' ';
153         reasonPhrase += token->getStringValue();
154       }
155     }
156
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&) {
163   }
164 }