| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8779ced commit f727f0c
19 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,198 @@ | |||
| 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 | + }; | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,95 @@ | |||
| 1 | + #ifndef _http_endpoint_hpp_ | ||
| 2 | + #define _http_endpoint_hpp_ | ||
| 3 | + | ||
| 4 | + #include <vector> | ||
| 5 | + #include <regex.h> | ||
| 6 | + #include <string> | ||
| 7 | + | ||
| 8 | + namespace httpserver | ||
| 9 | + { | ||
| 10 | + | ||
| 11 | + class Webserver; | ||
| 12 | + | ||
| 13 | + /** | ||
| 14 | + * Class representing an Http Endpoint. It is an abstraction used by the APIs. | ||
| 15 | + **/ | ||
| 16 | + class HttpEndpoint | ||
| 17 | + { | ||
| 18 | + public: | ||
| 19 | + /** | ||
| 20 | + * Default constructor of the class. | ||
| 21 | + * @param family boolean that indicates if the endpoint is a family endpoint. | ||
| 22 | + * A family endpoint is an endpoint that identifies a root and all its child like the same resource. | ||
| 23 | + * For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also | ||
| 24 | + * "/path/to/res/" is automatically associated to resource "A". | ||
| 25 | + **/ | ||
| 26 | + HttpEndpoint(bool family = false); | ||
| 27 | + /** | ||
| 28 | + * Constructor of the class HttpEndpoint. It is used to initialize an HttpEndpoint starting from a string form URL. | ||
| 29 | + * @param url The string representation of the endpoint. All endpoints are in the form "/path/to/resource". | ||
| 30 | + * @param family boolean that indicates if the endpoint is a family endpoint. | ||
| 31 | + * A family endpoint is an endpoint that identifies a root and all its child like the same resource. | ||
| 32 | + * For example, if I identify "/path/" like a family endpoint and I associate to it the resource "A", also | ||
| 33 | + * "/path/to/res/" is automatically associated to resource "A". | ||
| 34 | + * @param registration boolean that indicates to the system if this is an endpoint that need to be registered to a webserver | ||
| 35 | + * or it is simply an endpoint to be used for comparisons. | ||
| 36 | + **/ | ||
| 37 | + HttpEndpoint(const std::string& url, bool family = false, bool registration = false); | ||
| 38 | + /** | ||
| 39 | + * Copy constructor. It is useful expecially to copy regex_t structure that contains dinamically allocated data. | ||
| 40 | + * @param h The HttpEndpoint to copy | ||
| 41 | + **/ | ||
| 42 | + HttpEndpoint(const HttpEndpoint& h); | ||
| 43 | + /** | ||
| 44 | + * Destructor of the class. Essentially it frees the regex dinamically allocated pattern | ||
| 45 | + **/ | ||
| 46 | + ~HttpEndpoint(); | ||
| 47 | + /** | ||
| 48 | + * Operator overload for "less than operator". It is used to order endpoints in maps. | ||
| 49 | + * @param b The HttpEndpoint to compare to | ||
| 50 | + * @return boolean indicating if this is less than b. | ||
| 51 | + **/ | ||
| 52 | + bool operator <(const HttpEndpoint& b) const; | ||
| 53 | + /** | ||
| 54 | + * Operator overload for "assignment operator". It is used to copy endpoints to existing objects. | ||
| 55 | + * Is is functional expecially to copy regex_t structure that contains dinamically allocated data. | ||
| 56 | + * @param h The HttpEndpoint to copy | ||
| 57 | + * @return a reference to the HttpEndpoint obtained | ||
| 58 | + **/ | ||
| 59 | + HttpEndpoint& operator =(const HttpEndpoint& h); | ||
| 60 | + /** | ||
| 61 | + * Method indicating if this endpoint 'matches' with the one passed. A passed endpoint matches a registered endpoint if | ||
| 62 | + * the regex represented by the registered endpoint matches the passed one. | ||
| 63 | + * @param url The endpoint to match | ||
| 64 | + * @return true if the passed endpoint matches this. | ||
| 65 | + **/ | ||
| 66 | + bool match(const HttpEndpoint& url) const; | ||
| 67 | + /** | ||
| 68 | + * Method used to get all pars defined inside an url. | ||
| 69 | + * @return a vector of strings representing all found pars. | ||
| 70 | + **/ | ||
| 71 | + const std::vector<std::string> get_url_pars() const; | ||
| 72 | + /** | ||
| 73 | + * Method used to get all pieces of an url; considering an url splitted by '/'. | ||
| 74 | + * @return a vector of strings representing all found pieces. | ||
| 75 | + **/ | ||
| 76 | + const std::vector<std::string> get_url_pieces() const; | ||
| 77 | + /** | ||
| 78 | + * Method used to get indexes of all parameters inside url | ||
| 79 | + * @return a vector of int indicating all positions. | ||
| 80 | + **/ | ||
| 81 | + const std::vector<int> get_chunk_positions() const; | ||
| 82 | + private: | ||
| 83 | + std::string url_complete; | ||
| 84 | + std::string url_modded; | ||
| 85 | + std::vector<std::string> url_pars; | ||
| 86 | + std::vector<std::string> url_pieces; | ||
| 87 | + std::vector<int> chunk_positions; | ||
| 88 | + regex_t re_url_modded; | ||
| 89 | + // boost::xpressive::sregex re_url_modded; | ||
| 90 | + bool family_url; | ||
| 91 | + bool reg_compiled; | ||
| 92 | + }; | ||
| 93 | + | ||
| 94 | + }; | ||
| 95 | + #endif | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments