First commit
[anna.git] / source / http / parser / ReadChunkSize.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 <stdio.h>
38
39 #include <anna/core/tracing/Logger.hpp>
40
41 #include <anna/http/parser/ReadChunkSize.hpp>
42
43 #include <anna/http/Transport.hpp>
44 #include <anna/http/functions.hpp>
45 #include <anna/http/internal/defines.hpp>
46 #include <anna/http/internal/EncodedBlock.hpp>
47
48 using namespace std;
49 using namespace anna;
50
51 /*
52    Se ha comprobado que una vez que parace el Transfer-encoding puede haber un
53    número indeterminado de header antes del chunk (propiemente dicho), pero
54    siempre hay una línea en blanco, que indica que la siguiente línea es la que
55    contiene el tamaño del primer chunk, etc, etc
56
57    160: 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c   nection: Keep-Al
58    176: 69 76 65 0d 0a 54 72 61 6e 73 66 65 72 2d 45 6e   ive..Transfer-En
59    192: 63 6f 64 69 6e 67 3a 20 63 68 75 6e 6b 65 64 0d   coding: chunked.
60    208: 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74   .Content-Type: t
61    224: 65 78 74 2f 70 6c 61 69 6e 0d 0a 0d 0a 31 66 66   ext/plain....1ff     <================= ojo
62    240: 38 0d 0a 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e   8..<?xml version
63    256: 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d   ="1.0" encoding=
64
65    464: 4a 61 6e 20 31 39 39 35 20 32 32 3a 30 30 3a 30   Jan 1995 22:00:0
66    480: 30 20 47 4d 54 0d 0a 43 6f 6e 74 65 6e 74 2d 54   0 GMT..Content-T
67    496: 79 70 65 3a 20 74 65 78 74 2f 68 74 6d 6c 0d 0a   ype: text/html..
68    512: 54 72 61 6e 73 66 65 72 2d 65 6e 63 6f 64 69 6e   Transfer-encodin
69    528: 67 3a 20 63 68 75 6e 6b 65 64 0d 0a 43 6f 6e 6e   g: chunked..Conn
70    544: 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69   ection: Keep-Ali
71    560: 76 65 0d 0a 0d 0a 35 34 20 20 20 20 20 0d 0a 3c   ve....54     ..<     <================= ojo
72    576: 21 2d 2d 20 6c 30 37 2e 6d 65 6d 62 65 72 2e 75   !-- l07.member.u
73    592: 6b 6c 2e 79 61 68 6f 6f 2e 63 6f 6d 20 75 6e 63   kl.yahoo.com unc
74    608: 6f 6d 70 72 65 73 73 65 64 2f 63 68 75 6e 6b 65   ompressed/chunke
75    624: 64 20 57 65 64 20 53 65 70 20 20 33 20 31 30 3a   d Wed Sep  3 10:
76    640: 31 34 3a 31 31 20 47 4d 54 20 32 30 30 38 20 2d   14:11 GMT 2008 -
77    656: 2d 3e 0a 0d 0a 30 0d 0a 0d 0a                     ->...0....
78
79    HTTP/1.1 200 OK
80    Date: Fri, 31 Dec 1999 23:59:59 GMT
81    Content-Type: text/plain
82    Transfer-Encoding: chunked
83
84    1a; ignore-stuff-here
85    abcdefghijklmnopqrstuvwxyz
86    10
87    1234567890abcdef
88    0
89    some-footer: some-value
90    another-footer: another-value
91
92    Después del tamamño del chunk puede venir un "; y parámetros extras".
93 */
94 int http::parser::ReadChunkSize::processLine(http::Transport& transport, const DataBlock& dataBlock, const http::Token& line) const
95 throw(RuntimeException) {
96   int chunkSize = 0;
97   EncodedBlock* encodedBlock = transport.getEncodedBlock();
98
99   if(line.contains(';') == false) {
100     const std::string& hexstr = line.getStringValue();
101     sscanf(hexstr.c_str(), "%x", &chunkSize);
102     encodedBlock->setExpectedSize(chunkSize);
103   } else {
104     const Tokenizer& tokenizer = transport.split(line, ';');
105     const Token* token = tokenizer [0];
106     const std::string& hexstr = token->getStringValue();
107     sscanf(hexstr.c_str(), "%x", &chunkSize);
108     encodedBlock->setExpectedSize(chunkSize);
109
110     /*
111      * Los parámetros extras se adjunta al http::Message para que estén accesibles
112      * al programador cuando se termine de recibir el mensaje.
113      * La clase EncodedBlock no es pública.
114      */
115     if((token = tokenizer [1]) != NULL) {
116       http::Message* message = transport.getInputMessage();
117       appendExtraParameter(message, token->getStringValue());
118     }
119   }
120
121   LOGDEBUG(
122     string msg("ExpectedChunkSize: ");
123     msg += anna::functions::asString(chunkSize);
124     Logger::debug(msg, ANNA_FILE_LOCATION);
125   );
126   setState(transport, (chunkSize == 0) ? ClassType::ReadChunkTrailers : ClassType::ReadChunkData);
127   return -1;
128 }
129