Remove dynamic exceptions
[anna.git] / source / http / parser / ReadChunkData.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite                           //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7
8
9 #include <anna/core/tracing/Logger.hpp>
10
11 #include <anna/http/parser/ReadChunkData.hpp>
12
13 #include <anna/http/Transport.hpp>
14 #include <anna/http/functions.hpp>
15 #include <anna/http/internal/defines.hpp>
16 #include <anna/http/internal/EncodedBlock.hpp>
17
18 using namespace std;
19 using namespace anna;
20
21 /*
22    Se ha comprobado que una vez que parace el Transfer-encoding puede haber un
23    número indeterminado de header antes del chunk (propiemente dicho), pero
24    siempre hay una línea en blanco, que indica que la siguiente línea es la que
25    contiene el tamaño del primer chunk, etc, etc
26
27    160: 6e 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c   nection: Keep-Al
28    176: 69 76 65 0d 0a 54 72 61 6e 73 66 65 72 2d 45 6e   ive..Transfer-En
29    192: 63 6f 64 69 6e 67 3a 20 63 68 75 6e 6b 65 64 0d   coding: chunked.
30    208: 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20 74   .Content-Type: t
31    224: 65 78 74 2f 70 6c 61 69 6e 0d 0a 0d 0a 31 66 66   ext/plain....1ff     <================= ojo
32    240: 38 0d 0a 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e   8..<?xml version
33    256: 3d 22 31 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d   ="1.0" encoding=
34
35    464: 4a 61 6e 20 31 39 39 35 20 32 32 3a 30 30 3a 30   Jan 1995 22:00:0
36    480: 30 20 47 4d 54 0d 0a 43 6f 6e 74 65 6e 74 2d 54   0 GMT..Content-T
37    496: 79 70 65 3a 20 74 65 78 74 2f 68 74 6d 6c 0d 0a   ype: text/html..
38    512: 54 72 61 6e 73 66 65 72 2d 65 6e 63 6f 64 69 6e   Transfer-encodin
39    528: 67 3a 20 63 68 75 6e 6b 65 64 0d 0a 43 6f 6e 6e   g: chunked..Conn
40    544: 65 63 74 69 6f 6e 3a 20 4b 65 65 70 2d 41 6c 69   ection: Keep-Ali
41    560: 76 65 0d 0a 0d 0a 35 34 20 20 20 20 20 0d 0a 3c   ve....54     ..<     <================= ojo
42    576: 21 2d 2d 20 6c 30 37 2e 6d 65 6d 62 65 72 2e 75   !-- l07.member.u
43    592: 6b 6c 2e 79 61 68 6f 6f 2e 63 6f 6d 20 75 6e 63   kl.yahoo.com unc
44    608: 6f 6d 70 72 65 73 73 65 64 2f 63 68 75 6e 6b 65   ompressed/chunke
45    624: 64 20 57 65 64 20 53 65 70 20 20 33 20 31 30 3a   d Wed Sep  3 10:
46    640: 31 34 3a 31 31 20 47 4d 54 20 32 30 30 38 20 2d   14:11 GMT 2008 -
47    656: 2d 3e 0a 0d 0a 30 0d 0a 0d 0a                     ->...0....
48 */
49 int http::parser::ReadChunkData::processLine(http::Transport& transport, const DataBlock& slice, const http::Token& line) const
50 noexcept(false) {
51   EncodedBlock* encodedBlock = transport.getEncodedBlock();
52
53   if(encodedBlock->append(line) == EncodedBlock::State::Completed)
54     setState(transport, ClassType::ReadChunkSize);
55   else {
56     // Marca el comienzo del chunk para que el http::Transport no lo vuelva a procesar
57     // cuando llegen el resto de los trozos.
58     const int chunkedByte = line.calculeOffset(slice) + line.getSize();
59     parser::Abstract::setLastChunkedByte(transport, chunkedByte);
60     LOGDEBUG(
61       string msg("http::parser::ReadChunkData::processLine | ChunkByte: ");
62       msg += anna::functions::asString(chunkedByte);
63       Logger::debug(msg, ANNA_FILE_LOCATION);
64     );
65   }
66
67   return -1;
68 }
69