First commit
[anna.git] / source / http / internal / Tokenizer.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 <ctype.h>
38
39 #include <anna/config/defines.hpp>
40 #include <anna/core/DataBlock.hpp>
41
42 #include <anna/http/internal/Tokenizer.hpp>
43 #include <anna/http/internal/defines.hpp>
44 #include <anna/http/functions.hpp>
45
46 using namespace anna;
47
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;
57   int len = 0;
58   clear();
59
60   while(p < maxp) {
61     if(searchingInit == true) {
62       if(isSpace(*p) == false) {
63         init = p;
64         len = 1;
65         searchingInit = false;
66       }
67     } else {
68       if(isSpace(*p) == true) {
69         createToken(init, len);
70         searchingInit = true;
71       } else
72         len ++;
73     }
74
75     p ++;
76   }
77
78   if(searchingInit == false)
79     createToken(init, len);
80 }
81
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);
88   int pos;
89   clear();
90
91   while(p < maxp && size > 0) {
92     if((pos = find(p, size, separator)) != -1) {
93       createToken(p, pos);
94       p += (pos + lenSeparator);
95       size -= (pos + lenSeparator);
96     } else {
97       createToken(p, size);
98       p += size;
99       size = 0;
100     }
101   }
102 }
103
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;
109   int pos;
110   clear();
111
112   while(p < maxp && size > 0) {
113     if((pos = find(p, size, separator)) != -1) {
114       createToken(p, pos);
115       p += (pos + 1);
116       size -= (pos + 1);
117     } else {
118       createToken(p, size);
119       p += size;
120       size = 0;
121     }
122   }
123 }
124
125 const http::Token* http::Tokenizer::operator [](int index) const
126 throw() {
127   const_iterator ii = begin();
128   const_iterator maxii = end();
129
130   while(index && ii != maxii) {
131     ii ++;
132     index --;
133   }
134
135   return operator[](index);
136 }
137
138 int http::Tokenizer::find(const char* data, const int size, const char searched)
139 throw() {
140   for(register int i = 0; i < size; i ++, data ++) {
141     if(*data == searched)
142       return i;
143   }
144
145   return -1;
146 }
147
148 int http::Tokenizer::find(const char* data, const int size, const char* searched)
149 throw() {
150   static const int EndOfLineLen = 2;
151   int result(-1);
152   const char* w(data);
153   int s(size);
154   int pos;
155   const int slen = anna_strlen(searched);
156
157   if(slen == EndOfLineLen) {
158     while((pos = find(w, s, *searched)) != -1) {
159       if((s - pos) < EndOfLineLen)
160         break;
161
162       w += pos;
163
164       if(*w == *searched && *(w + 1) == *(searched + 1)) {
165         result = (w - data);
166         break;
167       }
168
169       w ++;
170       s -= pos;
171     }
172   } else {
173     while((pos = find(w, s, *searched)) != -1) {
174       if((s - pos) < slen)
175         break;
176
177       w += pos;
178
179       if(anna_strncmp(w, searched, slen) == 0) {
180         result = (w - data);
181         break;
182       }
183
184       w ++;
185       s -= pos;
186     }
187   }
188
189   return result;
190 }
191