First commit
[anna.git] / source / http / parser / Abstract.cpp
1 // ANNA - Anna is Not 'N' 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/core/functions.hpp>
38
39 #include <anna/http/Transport.hpp>
40 #include <anna/http/Message.hpp>
41
42 #include <anna/http/parser/Abstract.hpp>
43 #include <anna/http/parser/WaitMessage.hpp>
44 #include <anna/http/parser/ReadHeader.hpp>
45 #include <anna/http/parser/WaitEndOfHeader.hpp>
46 #include <anna/http/parser/WaitChunkSize.hpp>
47 #include <anna/http/parser/ReadChunkSize.hpp>
48 #include <anna/http/parser/ReadChunkData.hpp>
49 #include <anna/http/parser/ReadChunkTrailers.hpp>
50
51 using namespace std;
52 using namespace anna;
53
54 namespace anna {
55 namespace http {
56 namespace parser {
57 WaitMessage st_waitMessage;
58 ReadHeader st_readHeader;
59 WaitEndOfHeader st_waitEndOfHeader;
60 WaitChunkSize st_waitChunkSize;
61 ReadChunkSize st_readChunkSize;
62 ReadChunkData st_readChunkData;
63 ReadChunkTrailers st_readChunkTrailers;
64 }
65 anna_assign_enum(parser::Abstract::ClassType) = {
66   "WaitMessage", "ReadHeader", "WaitEndOfHeader",
67   "WaitChunkSize", "ReadChunkSize", "ReadChunkData", "ReadChunkTrailers", NULL
68 };
69 }
70 }
71
72 string http::parser::Abstract::asString() const
73 throw() {
74   string result("http::parser::Abstract { ");
75   result += ClassType::asCString(a_classType);
76   return result += " }";
77 }
78
79
80 /*static*/
81 void http::parser::Abstract::setState(http::Transport& transport, const http::parser::Abstract::ClassType::_v classType)
82 throw() {
83   /*
84    * Se repite el Wait-Message para llenar el hueco de ClassType::None
85    */
86   static Abstract* status [] =  {
87     &st_waitMessage, &st_readHeader, &st_waitEndOfHeader,
88     &st_waitChunkSize, &st_readChunkSize, &st_readChunkData, &st_readChunkTrailers
89   };
90   transport.setParserState(status [classType]);
91 }
92
93 /*static*/
94 void http::parser::Abstract::appendExtraParameter(http::Message* message, const std::string& extraParameter)
95 throw() {
96   message->appendExtraParameter(extraParameter);
97 }
98
99 /*
100  * Es acumulativo, porque cuando se invoca a
101  * http::parser::ReadChunkData::processLine (http::Transport& transport, const DataBlock& dataBlock, const http::Token& line)
102  * desde http::Transport::calculeSize no se pasa el dataBlock real (con el mensaje completo),
103  * sino que se le pasa el trozo de chunk. Y el cálculo del tamaño de chunk se hace como diferencia de su ubicación al
104  * principio del mensaje;
105  */
106 /*static*/
107 void http::parser::Abstract::setLastChunkedByte(http::Transport& transport, const int lastChunkedByte)
108 throw() {
109   transport.a_lastChunkedByte += lastChunkedByte;
110 }
111
112 // Sólo se puede usar en caso de Mensajes Transfer-encoding: chunked
113 /*static*/
114 const DataBlock& http::parser::Abstract::getFullMessage(http::Transport& transport)
115 throw() {
116   return *transport.a_fullMessage;
117 }
118
119