Skip to content

Commit 9dc6a34

Browse files
committed
Avoid returns as parameters in http_endpoint
1 parent 6826ba8 commit 9dc6a34

File tree

2 files changed

+25
-60
lines changed

2 files changed

+25
-60
lines changed

src/httpserver/http_endpoint.hpp

Lines changed: 18 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,35 @@ class webserver::http_endpoint
5050
* @param h The http_endpoint to copy
5151
**/
5252
http_endpoint(const http_endpoint& h);
53+
5354
/**
5455
* Class Destructor
5556
**/
5657
~http_endpoint(); //if inlined it causes problems during ruby wrapper compiling
58+
5759
/**
5860
* Operator overload for "less than operator". It is used to order endpoints in maps.
5961
* @param b The http_endpoint to compare to
6062
* @return boolean indicating if this is less than b.
6163
**/
6264
bool operator <(const http_endpoint& b) const;
65+
6366
/**
6467
* Operator overload for "assignment operator". It is used to copy endpoints to existing objects.
6568
* Is is functional expecially to copy regex_t structure that contains dinamically allocated data.
6669
* @param h The http_endpoint to copy
6770
* @return a reference to the http_endpoint obtained
6871
**/
6972
http_endpoint& operator =(const http_endpoint& h);
73+
7074
/**
7175
* Method indicating if this endpoint 'matches' with the one passed. A passed endpoint matches a registered endpoint if
7276
* the regex represented by the registered endpoint matches the passed one.
7377
* @param url The endpoint to match
7478
* @return true if the passed endpoint matches this.
7579
**/
7680
bool match(const http_endpoint& url) const;
81+
7782
/**
7883
* Method used to get the complete endpoint url
7984
* @return a string representing the url
@@ -82,22 +87,7 @@ class webserver::http_endpoint
8287
{
8388
return this->url_complete;
8489
}
85-
/**
86-
* Method used to get the complete endpoint url
87-
* @param result a string reference that will be filled with the url
88-
**/
89-
void get_url_complete(std::string& result) const
90-
{
91-
result = this->url_complete;
92-
}
93-
/**
94-
* Method used to find the size of the complete endpoint url
95-
* @return the size
96-
**/
97-
size_t get_url_complete_size() const
98-
{
99-
return this->url_complete.size();
100-
}
90+
10191
/**
10292
* Method used to get all pars defined inside an url.
10393
* @return a vector of strings representing all found pars.
@@ -106,11 +96,7 @@ class webserver::http_endpoint
10696
{
10797
return this->url_pars;
10898
}
109-
size_t get_url_pars(std::vector<std::string>& result) const
110-
{
111-
result = this->url_pars;
112-
return result.size();
113-
}
99+
114100
/**
115101
* Method used to get all pieces of an url; considering an url splitted by '/'.
116102
* @return a vector of strings representing all found pieces.
@@ -119,24 +105,7 @@ class webserver::http_endpoint
119105
{
120106
return this->url_pieces;
121107
}
122-
/**
123-
* Method used to get all pieces of an url; considering an url splittet by '/'.
124-
* @param result a vector of strings to fill with url pieces.
125-
* @return the size of the vector in output
126-
**/
127-
size_t get_url_pieces(std::vector<std::string>& result) const
128-
{
129-
result = this->url_pieces;
130-
return result.size();
131-
}
132-
/**
133-
* Method used to get the number of pieces the url is composed of
134-
* @return the number of pieces
135-
**/
136-
size_t get_url_pieces_num() const
137-
{
138-
return this->url_pieces.size();
139-
}
108+
140109
/**
141110
* Method used to get indexes of all parameters inside url
142111
* @return a vector of int indicating all positions.
@@ -145,16 +114,7 @@ class webserver::http_endpoint
145114
{
146115
return this->chunk_positions;
147116
}
148-
/**
149-
* Method used to get indexes of all parameters inside url
150-
* @param result a vector to fill with ints indicating chunk positions
151-
* @return the size of the vector filled
152-
**/
153-
size_t get_chunk_positions(std::vector<int>& result) const
154-
{
155-
result = this->chunk_positions;
156-
return result.size();
157-
}
117+
158118
/**
159119
* Default constructor of the class.
160120
* @param family boolean that indicates if the endpoint is a family endpoint.
@@ -169,6 +129,7 @@ class webserver::http_endpoint
169129
reg_compiled(false)
170130
{
171131
}
132+
172133
/**
173134
* Constructor of the class http_endpoint. It is used to initialize an http_endpoint starting from a string form URL.
174135
* @param url The string representation of the endpoint. All endpoints are in the form "/path/to/resource".
@@ -185,34 +146,42 @@ class webserver::http_endpoint
185146
bool registration = false,
186147
bool use_regex = true
187148
);
149+
188150
/**
189151
* The complete url extracted
190152
**/
191153
std::string url_complete;
154+
192155
/**
193156
* The url standardized in order to use standard comparisons or regexes
194157
**/
195158
std::string url_modded;
159+
196160
/**
197161
* Vector containing parameters extracted from url
198162
**/
199163
std::vector<std::string> url_pars;
164+
200165
/**
201166
* Pieces the url can be splitted into (consider '/' as separator)
202167
**/
203168
std::vector<std::string> url_pieces;
169+
204170
/**
205171
* Position of url pieces representing parameters
206172
**/
207173
std::vector<int> chunk_positions;
174+
208175
/**
209176
* Regex used in comparisons
210177
**/
211178
regex_t re_url_modded;
179+
212180
/**
213181
* Boolean indicating wheter the endpoint represents a family
214182
**/
215183
bool family_url;
184+
216185
/**
217186
* Boolean indicating if the regex is compiled
218187
**/

src/webserver.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -823,8 +823,8 @@ int webserver::finalize_answer(
823823
size_t tot_len = 0;
824824
for(it=registered_resources.begin(); it!=registered_resources.end(); ++it)
825825
{
826-
size_t endpoint_pieces_len = (*it).first.get_url_pieces_num();
827-
size_t endpoint_tot_len = (*it).first.get_url_complete_size();
826+
size_t endpoint_pieces_len = (*it).first.get_url_pieces().size();
827+
size_t endpoint_tot_len = (*it).first.get_url_complete().size();
828828
if(!found || endpoint_pieces_len > len || (endpoint_pieces_len == len && endpoint_tot_len > tot_len))
829829
{
830830
if((*it).first.match(endpoint))
@@ -838,17 +838,13 @@ int webserver::finalize_answer(
838838
}
839839
if(found)
840840
{
841-
vector<string> url_pars;
841+
vector<string> url_pars = found_endpoint->first.get_url_pars();
842842

843-
size_t pars_size = found_endpoint->first.get_url_pars(url_pars);
844-
845-
vector<string> url_pieces;
846-
endpoint.get_url_pieces(url_pieces);
847-
vector<int> chunkes;
848-
found_endpoint->first.get_chunk_positions(chunkes);
849-
for(unsigned int i = 0; i < pars_size; i++)
843+
vector<string> url_pieces = endpoint.get_url_pieces();
844+
vector<int> chunks = found_endpoint->first.get_chunk_positions();
845+
for(unsigned int i = 0; i < url_pars.size(); i++)
850846
{
851-
mr->dhr->set_arg(url_pars[i], url_pieces[chunkes[i]]);
847+
mr->dhr->set_arg(url_pars[i], url_pieces[chunks[i]]);
852848
}
853849

854850
hrm = found_endpoint->second;

0 commit comments

Comments
 (0)