Skip to content

Commit 2865cfd

Browse files
committed
Return reference rather than copying
1 parent bbff8bc commit 2865cfd

File tree

2 files changed

+39
-53
lines changed

2 files changed

+39
-53
lines changed

src/http_request.cpp

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ using namespace std;
2929
namespace httpserver
3030
{
3131

32+
const std::string http_request::EMPTY = "";
33+
3234
void http_request::set_method(const std::string& method)
3335
{
3436
this->method = string_utilities::to_upper_copy(method);
@@ -62,26 +64,6 @@ bool http_request::check_digest_auth(
6264
return true;
6365
}
6466

65-
const std::map<std::string, std::string, http::header_comparator> http_request::get_headers() const
66-
{
67-
return this->headers;
68-
}
69-
70-
const std::map<std::string, std::string, http::header_comparator> http_request::get_footers() const
71-
{
72-
return this->footers;
73-
}
74-
75-
const std::map<std::string, std::string, http::header_comparator> http_request::get_cookies() const
76-
{
77-
return this->cookies;
78-
}
79-
80-
const std::map<std::string, std::string, http::arg_comparator> http_request::get_args() const
81-
{
82-
return this->args;
83-
}
84-
8567
std::ostream &operator<< (std::ostream &os, const http_request &r)
8668
{
8769
os << r.method << " Request [user:\"" << r.user << "\" pass:\"" << r.pass << "\"] path:\""

src/httpserver/http_request.hpp

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,13 @@ namespace http
5050
class http_request
5151
{
5252
public:
53+
static const std::string EMPTY;
5354

5455
/**
5556
* Method used to get the username eventually passed through basic authentication.
5657
* @return string representation of the username.
5758
**/
58-
const std::string get_user() const
59+
const std::string& get_user() const
5960
{
6061
return this->user;
6162
}
@@ -64,7 +65,7 @@ class http_request
6465
* Method used to get the username extracted from a digest authentication
6566
* @return the username
6667
**/
67-
const std::string get_digested_user() const
68+
const std::string& get_digested_user() const
6869
{
6970
return this->digested_user;
7071
}
@@ -73,7 +74,7 @@ class http_request
7374
* Method used to get the password eventually passed through basic authentication.
7475
* @return string representation of the password.
7576
**/
76-
const std::string get_pass() const
77+
const std::string& get_pass() const
7778
{
7879
return this->pass;
7980
}
@@ -82,7 +83,7 @@ class http_request
8283
* Method used to get the path requested
8384
* @return string representing the path requested.
8485
**/
85-
const std::string get_path() const
86+
const std::string& get_path() const
8687
{
8788
return this->path;
8889
}
@@ -91,37 +92,28 @@ class http_request
9192
* Method used to get all pieces of the path requested; considering an url splitted by '/'.
9293
* @return a vector of strings containing all pieces
9394
**/
94-
const std::vector<std::string> get_path_pieces() const
95+
const std::vector<std::string>& get_path_pieces() const
9596
{
9697
return this->post_path;
9798
}
9899

99-
/**
100-
* Method used to obtain the size of path in terms of pieces; considering an url splitted by '/'.
101-
* @return an integer representing the number of pieces
102-
**/
103-
size_t get_path_pieces_size() const
104-
{
105-
return this->post_path.size();
106-
}
107-
108100
/**
109101
* Method used to obtain a specified piece of the path; considering an url splitted by '/'.
110102
* @param index the index of the piece selected
111103
* @return the selected piece in form of string
112104
**/
113-
const std::string get_path_piece(int index) const
105+
const std::string& get_path_piece(int index) const
114106
{
115107
if(((int)(this->post_path.size())) > index)
116108
return this->post_path[index];
117-
return "";
109+
return EMPTY;
118110
}
119111

120112
/**
121113
* Method used to get the METHOD used to make the request.
122114
* @return string representing the method.
123115
**/
124-
const std::string get_method() const
116+
const std::string& get_method() const
125117
{
126118
return this->method;
127119
}
@@ -131,89 +123,101 @@ class http_request
131123
* @param result a map<string, string> > that will be filled with all headers
132124
* @result the size of the map
133125
**/
134-
const std::map<std::string, std::string, http::header_comparator> get_headers() const;
126+
const std::map<std::string, std::string, http::header_comparator>& get_headers() const
127+
{
128+
return this->headers;
129+
}
135130

136131
/**
137132
* Method used to get all footers passed with the request.
138133
* @param result a map<string, string> > that will be filled with all footers
139134
* @result the size of the map
140135
**/
141-
const std::map<std::string, std::string, http::header_comparator> get_footers() const;
136+
const std::map<std::string, std::string, http::header_comparator>& get_footers() const
137+
{
138+
return this->footers;
139+
}
142140

143141
/**
144142
* Method used to get all cookies passed with the request.
145143
* @param result a map<string, string> > that will be filled with all cookies
146144
* @result the size of the map
147145
**/
148-
const std::map<std::string, std::string, http::header_comparator> get_cookies() const;
146+
const std::map<std::string, std::string, http::header_comparator>& get_cookies() const
147+
{
148+
return this->cookies;
149+
}
149150

150151
/**
151152
* Method used to get all args passed with the request.
152153
* @param result a map<string, string> > that will be filled with all args
153154
* @result the size of the map
154155
**/
155-
const std::map<std::string, std::string, http::arg_comparator> get_args() const;
156+
const std::map<std::string, std::string, http::arg_comparator>& get_args() const
157+
{
158+
return this->args;
159+
}
156160

157161
/**
158162
* Method used to get a specific header passed with the request.
159163
* @param key the specific header to get the value from
160164
* @return the value of the header.
161165
**/
162-
const std::string get_header(const std::string& key) const
166+
const std::string& get_header(const std::string& key) const
163167
{
164168
std::map<std::string, std::string>::const_iterator it =
165169
this->headers.find(key);
166170
if(it != this->headers.end())
167171
return it->second;
168172
else
169-
return "";
173+
return EMPTY;
170174
}
171175

172-
const std::string get_cookie(const std::string& key) const
176+
const std::string& get_cookie(const std::string& key) const
173177
{
174178
std::map<std::string, std::string>::const_iterator it =
175179
this->cookies.find(key);
176180
if(it != this->cookies.end())
177181
return it->second;
178182
else
179-
return "";
183+
return EMPTY;
180184
}
181185

182186
/**
183187
* Method used to get a specific footer passed with the request.
184188
* @param key the specific footer to get the value from
185189
* @return the value of the footer.
186190
**/
187-
const std::string get_footer(const std::string& key) const
191+
const std::string& get_footer(const std::string& key) const
188192
{
189193
std::map<std::string, std::string>::const_iterator it =
190194
this->footers.find(key);
191195
if(it != this->footers.end())
192196
return it->second;
193197
else
194-
return "";
198+
return EMPTY;
195199
}
196200

197201
/**
198202
* Method used to get a specific argument passed with the request.
199203
* @param ket the specific argument to get the value from
200204
* @return the value of the arg.
201205
**/
202-
const std::string get_arg(const std::string& key) const
206+
const std::string& get_arg(const std::string& key) const
203207
{
204208
std::map<std::string, std::string>::const_iterator it =
205209
this->args.find(key);
206210
if(it != this->args.end())
207211
return it->second;
208212
else
209-
return "";
213+
return EMPTY;
210214
}
211215

212216
/**
213217
* Method used to get the content of the request.
214218
* @return the content in string representation
215219
**/
216-
const std::string get_content() const
220+
const std::string& get_content() const
217221
{
218222
return this->content;
219223
}
@@ -230,7 +234,7 @@ class http_request
230234
* Method used to get the content of the query string..
231235
* @return the query string in string representation
232236
**/
233-
const std::string get_querystring() const
237+
const std::string& get_querystring() const
234238
{
235239
return this->querystring;
236240
}
@@ -239,7 +243,7 @@ class http_request
239243
* Method used to get the version of the request.
240244
* @return the version in string representation
241245
**/
242-
const std::string get_version() const
246+
const std::string& get_version() const
243247
{
244248
return this->version;
245249
}
@@ -248,7 +252,7 @@ class http_request
248252
* Method used to get the requestor.
249253
* @return the requestor
250254
**/
251-
const std::string get_requestor() const
255+
const std::string& get_requestor() const
252256
{
253257
return this->requestor;
254258
}

0 commit comments

Comments
 (0)