[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Gnash-commit] gnash ./ChangeLog libbase/URL.cpp libbase/URL.h...
From: |
Sandro Santilli |
Subject: |
[Gnash-commit] gnash ./ChangeLog libbase/URL.cpp libbase/URL.h... |
Date: |
Mon, 15 May 2006 11:16:50 +0000 |
CVSROOT: /sources/gnash
Module name: gnash
Branch:
Changes by: Sandro Santilli <address@hidden> 06/05/15 11:16:50
Modified files:
. : ChangeLog
libbase : URL.cpp URL.h
testsuite/libbase: URLTest.cpp
Log message:
reworked URL path normalization and added tests for it.
SMB testcases still missing.
CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/gnash/gnash/ChangeLog.diff?tr1=1.320&tr2=1.321&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/gnash/gnash/libbase/URL.cpp.diff?tr1=1.5&tr2=1.6&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/gnash/gnash/libbase/URL.h.diff?tr1=1.3&tr2=1.4&r1=text&r2=text
http://cvs.savannah.gnu.org/viewcvs/gnash/gnash/testsuite/libbase/URLTest.cpp.diff?tr1=1.3&tr2=1.4&r1=text&r2=text
Patches:
Index: gnash/ChangeLog
diff -u gnash/ChangeLog:1.320 gnash/ChangeLog:1.321
--- gnash/ChangeLog:1.320 Mon May 15 10:37:07 2006
+++ gnash/ChangeLog Mon May 15 11:16:50 2006
@@ -1,9 +1,10 @@
2006-05-15 Sandro Santilli <address@hidden>
- * libbase/URL.cpp: fixed bug in normalize_path producing
- oversized strings. Fix updir count to only consider
- "../" snippet at start of path. Made more use of C++ strings.
- * testsuite/libbase/URLTest.cpp: added check.as-like macros.
+ * libbase/URL.{cpp,h}: refactored path normalization procedure
+ to split path in components, remove unneeded ones and
+ resolve back-references.
+ * testsuite/libbase/URLTest.cpp: added check.as-like macros,
+ new tests for path normalization.
2006-05-14 Rob Savoye <address@hidden>
Index: gnash/libbase/URL.cpp
diff -u gnash/libbase/URL.cpp:1.5 gnash/libbase/URL.cpp:1.6
--- gnash/libbase/URL.cpp:1.5 Mon May 15 10:37:07 2006
+++ gnash/libbase/URL.cpp Mon May 15 11:16:50 2006
@@ -45,6 +45,7 @@
#include <string>
#include <cstring>
+#include <vector>
#include <stdexcept>
#include <cassert>
#include <algorithm>
@@ -56,6 +57,8 @@
#include <limits.h>
+using namespace std;
+
namespace gnash {
/*private*/
@@ -103,21 +106,23 @@
// What remains now is a path
_path.assign(in, last-in);
+
+ normalize_path(_path);
}
/*public*/
-URL::URL(const std::string& absolute_url)
+URL::URL(const string& absolute_url)
{
- //std::cerr << "URL(" << absolute_url << ")" << std::endl;
+ //cerr << "URL(" << absolute_url << ")" << endl;
if ( absolute_url[0] == '/'
- || absolute_url.find("://") != std::string::npos )
+ || absolute_url.find("://") != string::npos )
{
- //std::cerr << "It's absolute" << std::endl;
+ //cerr << "It's absolute" << endl;
init_absolute(absolute_url.c_str());
}
else
{
- //std::cerr << "It's relative" << std::endl;
+ //cerr << "It's relative" << endl;
char buf[PATH_MAX+1];
getcwd(buf, PATH_MAX);
char* ptr = buf+strlen(buf);
@@ -136,32 +141,61 @@
}
};
-/*private static*/
-std::string
-URL::normalize_path(const std::string& path)
-{
- std::string ret=path;
-
- // remove duplicated slashes
- std::string::iterator last = std::unique(
- ret.begin(), ret.end(), DupSlashes());
- ret.erase(last, ret.end());
+/*private*/
+void
+URL::normalize_path(string& path)
+{
- return ret;
+ assert(path[0] == '/');
+
+ //cerr << "path=" << path << endl;
+
+ vector<string> components;
+
+ string::iterator prev=path.begin();
+ for (string::iterator curr=prev+1;
+ curr != path.end();
+ ++curr )
+ {
+ if ( *curr == '/' )
+ {
+ string comp = string(prev+1, curr);
+ //cerr << "comp:" << comp << endl;
+ prev = curr;
+
+ if ( comp == "" || comp == "." ) continue;
+ if ( comp == ".." ) components.pop_back();
+ else components.push_back(comp);
+ }
+ }
+ // add last component
+ components.push_back(string(prev+1, path.end()));
+
+ //cerr << "number of dir components:" << components.size() << endl;
+ path = "";
+ for (vector<string>::iterator i=components.begin(),
+ e=components.end();
+ i!=e; ++i)
+ {
+ path += "/" + *i;
+ //cerr << "component:" << *i << endl;
+ }
+
+ //cerr << "after normalization: path=" << path << endl;
}
/*public*/
-URL::URL(const std::string& relative_url, const URL& baseurl)
+URL::URL(const string& relative_url, const URL& baseurl)
{
init_relative(relative_url, baseurl);
}
/*private*/
void
-URL::init_relative(const std::string& relative_url, const URL& baseurl)
+URL::init_relative(const string& relative_url, const URL& baseurl)
{
// If has a protocol, call absolute_url ctor
- if ( relative_url.find("://") != std::string::npos )
+ if ( relative_url.find("://") != string::npos )
{
init_absolute(relative_url.c_str());
return;
@@ -182,12 +216,12 @@
else // path-relative
{
- std::string in = relative_url;
+ string in = relative_url;
// see how many dirs we want to take
// off the baseurl path
int dirsback=0;
- std::string::size_type pos;
+ string::size_type pos;
while ( ( pos = in.find("../") ) == 0 )
{
++dirsback;
@@ -203,7 +237,7 @@
// find dirsback'th slash from end of
// baseurl path
- std::string basedir = baseurl._path.substr(0,
+ string basedir = baseurl._path.substr(0,
baseurl._path.find_last_of("/")+1);
//fprintf(stderr, "basedir=%s\n", basedir.c_str());
@@ -211,31 +245,33 @@
assert(basedir[0] == '/');
assert(*(basedir.rbegin()) == '/');
- std::string::size_type lpos = basedir.size()-1;
+ string::size_type lpos = basedir.size()-1;
for (int i=0; i<dirsback; ++i)
{
if ( lpos == 0 ) break;
- std::string::size_type pos = basedir.rfind('/', lpos-1);
+ string::size_type pos = basedir.rfind('/', lpos-1);
//fprintf(stderr, "slash %d at offset %d (rfind from %d)\n", i, pos, lpos-1);
// no more slashes found, break and set at 1
- if ( pos == std::string::npos ) lpos = 1;
+ if ( pos == string::npos ) lpos = 1;
else lpos = pos;
}
basedir.resize(lpos+1);
// get dirname from basurl path
//_path = basedir + relative_url;
- _path = basedir + normalize_path(in);
+ _path = basedir + in;
+
+ normalize_path(_path);
}
}
/*public*/
-std::string
+string
URL::str() const
{
- std::string ret = _proto;
+ string ret = _proto;
if ( _host != "" ) {
ret += "://" + _host;
@@ -248,7 +284,7 @@
return ret;
}
-std::ostream& operator<< (std::ostream& o, const URL& u)
+ostream& operator<< (ostream& o, const URL& u)
{
return o << u.str();
}
Index: gnash/libbase/URL.h
diff -u gnash/libbase/URL.h:1.3 gnash/libbase/URL.h:1.4
--- gnash/libbase/URL.h:1.3 Sun May 14 22:47:01 2006
+++ gnash/libbase/URL.h Mon May 15 11:16:50 2006
@@ -100,10 +100,11 @@
/// Normalize a 'path' component of an url
//
- /// Normalization currently only include removal
- /// of adjacent slashes.
+ /// Normalization currently include removal
+ /// of adjacent slashes, "./" dirs components
+ /// removal, and "/../" components resolution
///
- static std::string normalize_path(const std::string& path);
+ void normalize_path(std::string& path);
std::string _proto;
Index: gnash/testsuite/libbase/URLTest.cpp
diff -u gnash/testsuite/libbase/URLTest.cpp:1.3
gnash/testsuite/libbase/URLTest.cpp:1.4
--- gnash/testsuite/libbase/URLTest.cpp:1.3 Mon May 15 10:37:08 2006
+++ gnash/testsuite/libbase/URLTest.cpp Mon May 15 11:16:50 2006
@@ -137,5 +137,11 @@
check_equals ( u8.path() , "/tmp/curl.h" );
check_equals ( "/tmp/curl.h", "/tmp/curl.h" );
+ /// Test path normalization
+ check_equals (URL("/hello/world/../file").path(), "/hello/file");
+ check_equals (URL("/dir/./file").path(), "/dir/file");
+ check_equals (URL("/dir/./1/2/3/../../../...file").path(),
"/dir/...file");
+
+ // TODO: Samba paths
}