|
| 1 | +#include "HttpEndpoint.hpp" |
| 2 | +#include "HttpUtils.hpp" |
| 3 | +#include "string_utilities.hpp" |
| 4 | + |
| 5 | +using namespace std; |
| 6 | + |
| 7 | +namespace httpserver |
| 8 | +{ |
| 9 | +using namespace http; |
| 10 | +//ENDPOINT |
| 11 | +HttpEndpoint::HttpEndpoint(bool family): |
| 12 | + url_complete("/"), |
| 13 | + url_modded("/"), |
| 14 | + family_url(family), |
| 15 | + reg_compiled(false) |
| 16 | +{ |
| 17 | +} |
| 18 | + |
| 19 | +HttpEndpoint::HttpEndpoint(const string& url, bool family, bool registration): |
| 20 | + url_complete(string_utilities::to_lower_copy(url)), |
| 21 | + url_modded("/"), |
| 22 | + family_url(family), |
| 23 | + reg_compiled(false) |
| 24 | +{ |
| 25 | + vector<string> parts = HttpUtils::tokenizeUrl(url); |
| 26 | + string buffered; |
| 27 | + bool first = true; |
| 28 | + if(registration) |
| 29 | + { |
| 30 | + for(unsigned int i = 0; i< parts.size(); i++) |
| 31 | + { |
| 32 | + if((parts[i] != "") && (parts[i][0] != '{')) |
| 33 | + { |
| 34 | + if(first) |
| 35 | + { |
| 36 | + if(parts[i][0] == '^') |
| 37 | + { |
| 38 | + this->url_modded = parts[i]; |
| 39 | + } |
| 40 | + else |
| 41 | + { |
| 42 | + this->url_modded += parts[i]; |
| 43 | + } |
| 44 | + first = false; |
| 45 | + } |
| 46 | + else |
| 47 | + { |
| 48 | + this->url_modded += "/" + parts[i]; |
| 49 | + } |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + if(( parts[i].size() >= 3) && (parts[i][0] == '{') && (parts[i][parts[i].size() - 1] == '}') ) |
| 54 | + { |
| 55 | + int bar = parts[i].find_first_of('|'); |
| 56 | + if(bar != (int)string::npos) |
| 57 | + { |
| 58 | + this->url_pars.push_back(parts[i].substr(1, bar - 1)); |
| 59 | + if(first) |
| 60 | + { |
| 61 | + this->url_modded += parts[i].substr(bar + 1, parts[i].size() - bar - 2); |
| 62 | + first = false; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + this->url_modded += "/"+parts[i].substr(bar + 1, parts[i].size() - bar - 2); |
| 67 | + } |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + this->url_pars.push_back(parts[i].substr(1,parts[i].size() - 2)); |
| 72 | + if(first) |
| 73 | + { |
| 74 | + this->url_modded += "([^\\/]+)"; |
| 75 | + first = false; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + this->url_modded += "/([^\\/]+)"; |
| 80 | + } |
| 81 | + } |
| 82 | + this->chunk_positions.push_back(i); |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + // RITORNARE ECCEZIONE |
| 87 | + } |
| 88 | + } |
| 89 | + this->url_pieces.push_back(parts[i]); |
| 90 | + } |
| 91 | + regcomp(&(this->re_url_modded), url_modded.c_str(), REG_EXTENDED|REG_ICASE); |
| 92 | + reg_compiled = true; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + for(unsigned int i = 0; i< parts.size(); i++) |
| 97 | + { |
| 98 | + if(first) |
| 99 | + { |
| 100 | + this->url_modded += parts[i]; |
| 101 | + first = false; |
| 102 | + } |
| 103 | + else |
| 104 | + { |
| 105 | + this->url_modded += "/" + parts[i]; |
| 106 | + } |
| 107 | + this->url_pieces.push_back(parts[i]); |
| 108 | + } |
| 109 | + } |
| 110 | +// this->re_url_modded = boost::xpressive::sregex::compile( url_modded, boost::xpressive::regex_constants::icase ); |
| 111 | +} |
| 112 | + |
| 113 | +HttpEndpoint::HttpEndpoint(const HttpEndpoint& h) |
| 114 | +{ |
| 115 | + this->url_complete = h.url_complete; |
| 116 | + this->url_modded = h.url_modded; |
| 117 | + this->family_url = h.family_url; |
| 118 | + this->reg_compiled = h.reg_compiled; |
| 119 | + if(this->reg_compiled) |
| 120 | + regcomp(&(this->re_url_modded), url_modded.c_str(), REG_EXTENDED|REG_ICASE); |
| 121 | + this->url_pars = h.url_pars; |
| 122 | + this->url_pieces = h.url_pieces; |
| 123 | + this->chunk_positions = h.chunk_positions; |
| 124 | +} |
| 125 | + |
| 126 | +HttpEndpoint::~HttpEndpoint() |
| 127 | +{ |
| 128 | + |
| 129 | + if(reg_compiled) |
| 130 | + { |
| 131 | + regfree(&(this->re_url_modded)); |
| 132 | + } |
| 133 | + |
| 134 | +} |
| 135 | + |
| 136 | +HttpEndpoint& HttpEndpoint::operator =(const HttpEndpoint& h) |
| 137 | +{ |
| 138 | + this->url_complete = h.url_complete; |
| 139 | + this->url_modded = h.url_modded; |
| 140 | + this->family_url = h.family_url; |
| 141 | + this->reg_compiled = h.reg_compiled; |
| 142 | + if(this->reg_compiled) |
| 143 | + regcomp(&(this->re_url_modded), url_modded.c_str(), REG_EXTENDED|REG_ICASE); |
| 144 | + this->url_pars = h.url_pars; |
| 145 | + this->url_pieces = h.url_pieces; |
| 146 | + this->chunk_positions = h.chunk_positions; |
| 147 | + return *this; |
| 148 | +} |
| 149 | + |
| 150 | +bool HttpEndpoint::operator <(const HttpEndpoint& b) const |
| 151 | +{ |
| 152 | + return string_utilities::to_lower_copy(this->url_modded) < string_utilities::to_lower_copy(b.url_modded); |
| 153 | +} |
| 154 | + |
| 155 | +bool HttpEndpoint::match(const HttpEndpoint& url) const |
| 156 | +{ |
| 157 | + if(this->family_url && (url.url_pieces.size() >= this->url_pieces.size())) |
| 158 | + { |
| 159 | + string nn = "/"; |
| 160 | + bool first = true; |
| 161 | + for(unsigned int i = 0; i < this->url_pieces.size(); i++) |
| 162 | + { |
| 163 | + if(first) |
| 164 | + { |
| 165 | + nn += url.url_pieces[i]; |
| 166 | + first = false; |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + nn += "/" + url.url_pieces[i]; |
| 171 | + } |
| 172 | + } |
| 173 | + return regexec(&(this->re_url_modded), nn.c_str(), 0, NULL, 0) == 0; |
| 174 | +// return boost::xpressive::regex_match(nn, this->re_url_modded); |
| 175 | + } |
| 176 | + else |
| 177 | + { |
| 178 | + return regexec(&(this->re_url_modded), url.url_modded.c_str(), 0, NULL, 0) == 0; |
| 179 | +// return boost::xpressive::regex_match(url.url_modded, this->re_url_modded); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +const std::vector<std::string> HttpEndpoint::get_url_pars() const |
| 184 | +{ |
| 185 | + return this->url_pars; |
| 186 | +} |
| 187 | + |
| 188 | +const std::vector<std::string> HttpEndpoint::get_url_pieces() const |
| 189 | +{ |
| 190 | + return this->url_pieces; |
| 191 | +} |
| 192 | + |
| 193 | +const std::vector<int> HttpEndpoint::get_chunk_positions() const |
| 194 | +{ |
| 195 | + return this->chunk_positions; |
| 196 | +} |
| 197 | + |
| 198 | +}; |
0 commit comments