Skip to content

Commit 4733d4e

Browse files
committed
Add fix to get_path_pieces to return by value.
The current implementation has a bug where the method returns a reference to a temporary value.
1 parent 70d166d commit 4733d4e

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/httpserver/http_request.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class http_request
9393
* @param index the index of the piece selected
9494
* @return the selected piece in form of string
9595
**/
96-
const std::string& get_path_piece(int index) const
96+
const std::string get_path_piece(int index) const
9797
{
9898
std::vector<std::string> post_path = this->get_path_pieces();
9999
if(((int)(post_path.size())) > index)

test/integ/basic.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "littletest.hpp"
2222
#include <curl/curl.h>
2323
#include <string>
24+
#include <sstream>
2425
#include <map>
2526
#include "string_utilities.hpp"
2627
#include "httpserver.hpp"
@@ -135,6 +136,20 @@ class querystring_resource : public http_resource
135136
}
136137
};
137138

139+
class path_pieces_resource : public http_resource
140+
{
141+
public:
142+
const shared_ptr<http_response> render_GET(const http_request& req)
143+
{
144+
std::stringstream ss;
145+
for (unsigned int i = 0; i < req.get_path_pieces().size(); i++)
146+
{
147+
ss << req.get_path_piece(i) << ",";
148+
}
149+
return shared_ptr<string_response>(new string_response(ss.str(), 200, "text/plain"));
150+
}
151+
};
152+
138153
class complete_test_resource : public http_resource
139154
{
140155
public:
@@ -959,6 +974,24 @@ LT_BEGIN_AUTO_TEST(basic_suite, response_is_printable)
959974
curl_easy_cleanup(curl);
960975
LT_END_AUTO_TEST(response_is_printable)
961976

977+
LT_BEGIN_AUTO_TEST(basic_suite, long_path_pieces)
978+
path_pieces_resource resource;
979+
ws->register_resource("/settings", &resource, true);
980+
curl_global_init(CURL_GLOBAL_ALL);
981+
982+
std::string s;
983+
CURL *curl = curl_easy_init();
984+
CURLcode res;
985+
curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/settings/somestringthatisreallylong/with_really_a_lot_of_content/and_underscores_and_looooooooooooooooooong_stuff");
986+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
987+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
988+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
989+
res = curl_easy_perform(curl);
990+
LT_ASSERT_EQ(res, 0);
991+
LT_CHECK_EQ(s, "settings,somestringthatisreallylong,with_really_a_lot_of_content,and_underscores_and_looooooooooooooooooong_stuff,");
992+
curl_easy_cleanup(curl);
993+
LT_END_AUTO_TEST(long_path_pieces)
994+
962995
LT_BEGIN_AUTO_TEST_ENV()
963996
AUTORUN_TESTS()
964997
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)