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
39 #include <anna/config/defines.hpp>
40 #include <anna/core/DataBlock.hpp>
42 #include <anna/http/internal/Tokenizer.hpp>
43 #include <anna/http/internal/defines.hpp>
44 #include <anna/http/functions.hpp>
48 //--------------------------------------------------------------------------------------------
49 // Extrae los datos del bloque de memoria separando por espacios en blancos, tabs, etc, etc
50 //--------------------------------------------------------------------------------------------
51 void http::Tokenizer::apply(const DataBlock& data)
52 throw(RuntimeException) {
53 const char* p = data.getData();
54 const char* maxp = p + data.getSize();
55 bool searchingInit = true;
56 const char* init = NULL;
61 if(searchingInit == true) {
62 if(isSpace(*p) == false) {
65 searchingInit = false;
68 if(isSpace(*p) == true) {
69 createToken(init, len);
78 if(searchingInit == false)
79 createToken(init, len);
82 void http::Tokenizer::apply(const DataBlock& data, const char* separator)
83 throw(RuntimeException) {
84 const char* p = data.getData();
85 int size = data.getSize();
86 const char* maxp = p + size;
87 const int lenSeparator = anna_strlen(separator);
91 while(p < maxp && size > 0) {
92 if((pos = find(p, size, separator)) != -1) {
94 p += (pos + lenSeparator);
95 size -= (pos + lenSeparator);
104 void http::Tokenizer::apply(const DataBlock& data, const char separator)
105 throw(RuntimeException) {
106 const char* p = data.getData();
107 int size = data.getSize();
108 const char* maxp = p + size;
112 while(p < maxp && size > 0) {
113 if((pos = find(p, size, separator)) != -1) {
118 createToken(p, size);
125 const http::Token* http::Tokenizer::operator [](int index) const
127 const_iterator ii = begin();
128 const_iterator maxii = end();
130 while(index && ii != maxii) {
135 return operator[](index);
138 int http::Tokenizer::find(const char* data, const int size, const char searched)
140 for(register int i = 0; i < size; i ++, data ++) {
141 if(*data == searched)
148 int http::Tokenizer::find(const char* data, const int size, const char* searched)
150 static const int EndOfLineLen = 2;
155 const int slen = anna_strlen(searched);
157 if(slen == EndOfLineLen) {
158 while((pos = find(w, s, *searched)) != -1) {
159 if((s - pos) < EndOfLineLen)
164 if(*w == *searched && *(w + 1) == *(searched + 1)) {
173 while((pos = find(w, s, *searched)) != -1) {
179 if(anna_strncmp(w, searched, slen) == 0) {