From 0dcea3817b3615ba6b0b9521870835a0a4dde426 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 11 Apr 2012 14:16:00 +0200 Subject: [PATCH 1/7] Added a normalize function in a new file utility.hpp and a test case for it in uri_test.cpp --- boost/network/uri/utility.hpp | 76 +++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 boost/network/uri/utility.hpp diff --git a/boost/network/uri/utility.hpp b/boost/network/uri/utility.hpp new file mode 100644 index 000000000..c292bc3de --- /dev/null +++ b/boost/network/uri/utility.hpp @@ -0,0 +1,76 @@ +// Copyright Fredrik Olofsson 2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +#ifndef __CPPNETLIB_NETWORK_URI_UTILITY__ +#define __CPPNETLIB_NETWORK_URI_UTILITY__ + +#include + +namespace boost +{ + namespace network + { + namespace uri + { + /** + * Normalize the given path and returns it. The path will be resolved and any attempt + * to point to an file above (www) root will be removed. The returned string will allways + * start with an slash ("/"). The implemenation in its current form does not handle procent + * coding (%20). Examples: + * + * "/test/test/../" : -> "/test" + * "../../../" : -> "/" + */ + std::string normalize(std::string); + } + } +} + + + +namespace boost +{ + namespace network + { + namespace uri + { + std::string normalize(std::string path) + { + if(path.empty() || path.front() != '/') + path.insert(0, "/"); + + std::size_t query_offset = path.find("?"), + temp = 0, + pos = 0, + counter = 0; + + while((pos = path.find("//")) != std::string::npos && pos < query_offset) + path.replace(pos,2,"/"); + + + while((pos = path.find("../")) != std::string::npos && pos < query_offset) + { + temp = pos; + while(0 < temp && counter < 2) + { + if(path[--temp] == '/') + ++counter; + } + path.replace(temp, pos-temp+3,"/"); //length of ../ is 3 + counter = 0;//back to default + } + + //remove the last '/' in the path section if not the last on. + pos = ((query_offset == std::string::npos) ? path.size() : query_offset) - 1; + if(path[pos] == '/' && pos != 0) + path.erase(pos,1); + + return path; + } + } + } +} + + +#endif \ No newline at end of file From 7219ad715ed3e321610bb7e83ff1201b7a4822c6 Mon Sep 17 00:00:00 2001 From: idleman Date: Wed, 11 Apr 2012 15:24:57 +0300 Subject: [PATCH 2/7] Update libs/network/test/uri/uri_test.cpp --- libs/network/test/uri/uri_test.cpp | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libs/network/test/uri/uri_test.cpp b/libs/network/test/uri/uri_test.cpp index 0ee158079..a32af0a7b 100644 --- a/libs/network/test/uri/uri_test.cpp +++ b/libs/network/test/uri/uri_test.cpp @@ -1,4 +1,4 @@ -// Copyright 2009, 2010, 2011 Dean Michael Berris, Jeroen Habraken, Glyn Matthews. +// Copyright 2009, 2010, 2011 Dean Michael Berris, Jeroen Habraken, Glyn Matthews, Fredrik Olofsson. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt of copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -463,3 +464,14 @@ BOOST_AUTO_TEST_CASE(issue_104_test) { instance.reset(); BOOST_CHECK_EQUAL(uri::scheme(copy), "http"); } + +BOOST_AUTO_TEST_CASE(normalize_string) { + BOOST_CHECK_EQUAL(uri::normalize(""), "/"); + BOOST_CHECK_EQUAL(uri::normalize("/"), "/"); + BOOST_CHECK_EQUAL(uri::normalize("/../"), "/"); + BOOST_CHECK_EQUAL(uri::normalize("/test/../../../"), "/"); + BOOST_CHECK_EQUAL(uri::normalize("../../test"), "/test"); + BOOST_CHECK_EQUAL(uri::normalize("/test/"), "/test"); + BOOST_CHECK_EQUAL(uri::normalize("/test/test/../"), "/test"); + BOOST_CHECK_EQUAL(uri::normalize("/../?test=test¶m2=../p"), "/?test=test¶m2=../p"); +} \ No newline at end of file From 0c90ce044e6de485f66dd657a6d4ff9bba18279d Mon Sep 17 00:00:00 2001 From: idleman Date: Wed, 11 Apr 2012 17:00:16 +0300 Subject: [PATCH 3/7] =?UTF-8?q?Fixed=20an=20issue=20so=20the=20handler=20d?= =?UTF-8?q?on=C2=B4t=20get=20copied.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- boost/network/protocol/http/server/async_connection.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boost/network/protocol/http/server/async_connection.hpp b/boost/network/protocol/http/server/async_connection.hpp index 377ec5738..d1002cf01 100644 --- a/boost/network/protocol/http/server/async_connection.hpp +++ b/boost/network/protocol/http/server/async_connection.hpp @@ -447,7 +447,7 @@ namespace boost { namespace network { namespace http { thread_pool().post( boost::bind( &Handler::operator(), - handler, + &handler, cref(request_), async_connection::shared_from_this())); return; From f2825caac8132b730d0920a707f3657a473d6101 Mon Sep 17 00:00:00 2001 From: Fredrik Olofsson Date: Wed, 18 Apr 2012 02:42:46 +0200 Subject: [PATCH 4/7] Added a normalize function in a new file: uri/normalize.hpp --- boost/network/uri/normalize.hpp | 82 ++++++++++++++++++++++++++++++ boost/network/uri/utility.hpp | 76 --------------------------- libs/network/test/uri/uri_test.cpp | 16 +++++- 3 files changed, 97 insertions(+), 77 deletions(-) create mode 100644 boost/network/uri/normalize.hpp delete mode 100644 boost/network/uri/utility.hpp diff --git a/boost/network/uri/normalize.hpp b/boost/network/uri/normalize.hpp new file mode 100644 index 000000000..b83d1ea0f --- /dev/null +++ b/boost/network/uri/normalize.hpp @@ -0,0 +1,82 @@ +// Copyright Fredrik Olofsson 2012. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +#ifndef __CPPNETLIB_NETWORK_URI_NORMALIZE__ +#define __CPPNETLIB_NETWORK_URI_NORMALIZE__ + +#pragma once + +#include + +namespace boost +{ + namespace network + { + namespace uri + { + /** + * Normalize a given path and returns it. The path will be resolved and any attempt to + * point to a file above (www) root will be removed. The returned string will allways + * start with a slash ("/"). The path must be an instance of std::basic_string<>. The + * function does handle wide characters as well, so both std::string and std::wstring + * are accepted. Examples: + * + * "/test/test/../" : -> "/test" + * "../../../" : -> "/" + */ + template + inline std::basic_string normalize(std::basic_string path) + { + //why pass by value? See http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/ + + typedef std::basic_string StringType; + + //so the method can handle wide characters as well, otherwise must we use L"" or "" depending on char/wchar_t. + static StringType slash1(1, '/'); + static StringType slash2(2, '/'); + static StringType dot2(2, '.'); + static StringType dot2_slash1(dot2 + slash1); + + if(path.empty() || path.front() != '/') + path.insert(0, slash1); + + std::size_t //query_offset = path.find(question), + temp = 0, + pos = 0, + counter = 0; + + while((pos = path.find(slash2)) != StringType::npos) // && pos < query_offset) + path.replace(pos,2,slash1); + + + while((pos = path.find(dot2_slash1)) != StringType::npos)// && pos < query_offset) + { + temp = pos; + while(0 < temp && counter < 2) + { + if(path[--temp] == '/') + ++counter; + } + path.replace(temp, pos-temp+3,slash1); + counter = 0; + } + + pos = /*((query_offset == StringType::npos) ? */ path.size() /*: query_offset)*/ - 1; + if(path[pos] == '/' && pos != 0) + path.erase(pos,1); + + return path; + } + + //A simple helper method, deduce const wchar_t/char * to std::basic_string + template + inline std::basic_string normalize(const Type* path) + { + return normalize(std::basic_string(path)); + } + } + } +} + +#endif \ No newline at end of file diff --git a/boost/network/uri/utility.hpp b/boost/network/uri/utility.hpp deleted file mode 100644 index c292bc3de..000000000 --- a/boost/network/uri/utility.hpp +++ /dev/null @@ -1,76 +0,0 @@ -// Copyright Fredrik Olofsson 2012. -// Distributed under the Boost Software License, Version 1.0. -// (See accompanying file LICENSE_1_0.txt or copy at -// http://www.boost.org/LICENSE_1_0.txt) -#ifndef __CPPNETLIB_NETWORK_URI_UTILITY__ -#define __CPPNETLIB_NETWORK_URI_UTILITY__ - -#include - -namespace boost -{ - namespace network - { - namespace uri - { - /** - * Normalize the given path and returns it. The path will be resolved and any attempt - * to point to an file above (www) root will be removed. The returned string will allways - * start with an slash ("/"). The implemenation in its current form does not handle procent - * coding (%20). Examples: - * - * "/test/test/../" : -> "/test" - * "../../../" : -> "/" - */ - std::string normalize(std::string); - } - } -} - - - -namespace boost -{ - namespace network - { - namespace uri - { - std::string normalize(std::string path) - { - if(path.empty() || path.front() != '/') - path.insert(0, "/"); - - std::size_t query_offset = path.find("?"), - temp = 0, - pos = 0, - counter = 0; - - while((pos = path.find("//")) != std::string::npos && pos < query_offset) - path.replace(pos,2,"/"); - - - while((pos = path.find("../")) != std::string::npos && pos < query_offset) - { - temp = pos; - while(0 < temp && counter < 2) - { - if(path[--temp] == '/') - ++counter; - } - path.replace(temp, pos-temp+3,"/"); //length of ../ is 3 - counter = 0;//back to default - } - - //remove the last '/' in the path section if not the last on. - pos = ((query_offset == std::string::npos) ? path.size() : query_offset) - 1; - if(path[pos] == '/' && pos != 0) - path.erase(pos,1); - - return path; - } - } - } -} - - -#endif \ No newline at end of file diff --git a/libs/network/test/uri/uri_test.cpp b/libs/network/test/uri/uri_test.cpp index 0ee158079..5aff5797a 100644 --- a/libs/network/test/uri/uri_test.cpp +++ b/libs/network/test/uri/uri_test.cpp @@ -1,4 +1,4 @@ -// Copyright 2009, 2010, 2011 Dean Michael Berris, Jeroen Habraken, Glyn Matthews. +// Copyright 2009, 2010, 2011, 2012 Dean Michael Berris, Jeroen Habraken, Glyn Matthews, Fredrik Olofsson. // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt of copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -463,3 +464,16 @@ BOOST_AUTO_TEST_CASE(issue_104_test) { instance.reset(); BOOST_CHECK_EQUAL(uri::scheme(copy), "http"); } + +BOOST_AUTO_TEST_CASE(normalize_empty_string) { + BOOST_CHECK_EQUAL(uri::normalize(""), "/"); +} +BOOST_AUTO_TEST_CASE(normalize_backslash_string) { + BOOST_CHECK_EQUAL(uri::normalize("../../../"), "/"); +} +BOOST_AUTO_TEST_CASE(normalize_relative_string) { + BOOST_CHECK_EQUAL(uri::normalize("/test/test/test/../test2"), "/test/test/test2"); +} +BOOST_AUTO_TEST_CASE(normalize_slashend_string) { + BOOST_CHECK_EQUAL(uri::normalize("/test/"), "/test"); +} \ No newline at end of file From 13b82ed52f2e2a871c96d7dc4907e6fe116878b8 Mon Sep 17 00:00:00 2001 From: idleman Date: Wed, 18 Apr 2012 03:54:48 +0300 Subject: [PATCH 5/7] Removed old tests --- libs/network/test/uri/uri_test.cpp | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/libs/network/test/uri/uri_test.cpp b/libs/network/test/uri/uri_test.cpp index a3220cf37..946846cfe 100644 --- a/libs/network/test/uri/uri_test.cpp +++ b/libs/network/test/uri/uri_test.cpp @@ -1,8 +1,4 @@ -<<<<<<< HEAD // Copyright 2009, 2010, 2011, 2012 Dean Michael Berris, Jeroen Habraken, Glyn Matthews, Fredrik Olofsson. -======= -// Copyright 2009, 2010, 2011 Dean Michael Berris, Jeroen Habraken, Glyn Matthews, Fredrik Olofsson. ->>>>>>> 0c90ce044e6de485f66dd657a6d4ff9bba18279d // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt of copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -11,7 +7,6 @@ #include #include #include -#include #include #include #include @@ -470,7 +465,6 @@ BOOST_AUTO_TEST_CASE(issue_104_test) { BOOST_CHECK_EQUAL(uri::scheme(copy), "http"); } -<<<<<<< HEAD BOOST_AUTO_TEST_CASE(normalize_empty_string) { BOOST_CHECK_EQUAL(uri::normalize(""), "/"); } @@ -479,18 +473,4 @@ BOOST_AUTO_TEST_CASE(normalize_backslash_string) { } BOOST_AUTO_TEST_CASE(normalize_relative_string) { BOOST_CHECK_EQUAL(uri::normalize("/test/test/test/../test2"), "/test/test/test2"); -} -BOOST_AUTO_TEST_CASE(normalize_slashend_string) { - BOOST_CHECK_EQUAL(uri::normalize("/test/"), "/test"); -======= -BOOST_AUTO_TEST_CASE(normalize_string) { - BOOST_CHECK_EQUAL(uri::normalize(""), "/"); - BOOST_CHECK_EQUAL(uri::normalize("/"), "/"); - BOOST_CHECK_EQUAL(uri::normalize("/../"), "/"); - BOOST_CHECK_EQUAL(uri::normalize("/test/../../../"), "/"); - BOOST_CHECK_EQUAL(uri::normalize("../../test"), "/test"); - BOOST_CHECK_EQUAL(uri::normalize("/test/"), "/test"); - BOOST_CHECK_EQUAL(uri::normalize("/test/test/../"), "/test"); - BOOST_CHECK_EQUAL(uri::normalize("/../?test=test¶m2=../p"), "/?test=test¶m2=../p"); ->>>>>>> 0c90ce044e6de485f66dd657a6d4ff9bba18279d } \ No newline at end of file From fb8d9865df1b0aaee5024a4908003f75d04946c7 Mon Sep 17 00:00:00 2001 From: idleman Date: Wed, 18 Apr 2012 13:22:22 +0300 Subject: [PATCH 6/7] Update boost/network/protocol/http/server/async_connection.hpp --- boost/network/protocol/http/server/async_connection.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boost/network/protocol/http/server/async_connection.hpp b/boost/network/protocol/http/server/async_connection.hpp index d1002cf01..377ec5738 100644 --- a/boost/network/protocol/http/server/async_connection.hpp +++ b/boost/network/protocol/http/server/async_connection.hpp @@ -447,7 +447,7 @@ namespace boost { namespace network { namespace http { thread_pool().post( boost::bind( &Handler::operator(), - &handler, + handler, cref(request_), async_connection::shared_from_this())); return; From 692f12f61b80ac989bfcc726c1ea2581d90b0223 Mon Sep 17 00:00:00 2001 From: idleman Date: Wed, 18 Apr 2012 13:25:54 +0300 Subject: [PATCH 7/7] Update libs/network/test/uri/uri_test.cpp --- libs/network/test/uri/uri_test.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libs/network/test/uri/uri_test.cpp b/libs/network/test/uri/uri_test.cpp index 946846cfe..452786df4 100644 --- a/libs/network/test/uri/uri_test.cpp +++ b/libs/network/test/uri/uri_test.cpp @@ -1,4 +1,5 @@ -// Copyright 2009, 2010, 2011, 2012 Dean Michael Berris, Jeroen Habraken, Glyn Matthews, Fredrik Olofsson. +// Copyright 2009, 2010, 2011 Dean Michael Berris, Jeroen Habraken, Glyn Matthews +// Copyright 2012 Fredrik Olofsson // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt of copy at // http://www.boost.org/LICENSE_1_0.txt) @@ -468,9 +469,15 @@ BOOST_AUTO_TEST_CASE(issue_104_test) { BOOST_AUTO_TEST_CASE(normalize_empty_string) { BOOST_CHECK_EQUAL(uri::normalize(""), "/"); } + BOOST_AUTO_TEST_CASE(normalize_backslash_string) { BOOST_CHECK_EQUAL(uri::normalize("../../../"), "/"); } + BOOST_AUTO_TEST_CASE(normalize_relative_string) { BOOST_CHECK_EQUAL(uri::normalize("/test/test/test/../test2"), "/test/test/test2"); +} + +BOOST_AUTO_TEST_CASE(normalize_slashend_string) { + BOOST_CHECK_EQUAL(uri::normalize("/test/"), "/test"); } \ No newline at end of file