Boost logo

Boost-Commit :

From: daniel_james_at_[hidden]
Date: 2008-02-10 14:13:55


Author: danieljames
Date: 2008-02-10 14:13:55 EST (Sun, 10 Feb 2008)
New Revision: 43216
URL: http://svn.boost.org/trac/boost/changeset/43216

Log:
Decode percent encoded characters while inspecting URLs.
Text files modified:
   branches/fix-links/tools/inspect/link_check.cpp | 37 +++++++++++++++++++++++++++++++++++--
   1 files changed, 35 insertions(+), 2 deletions(-)

Modified: branches/fix-links/tools/inspect/link_check.cpp
==============================================================================
--- branches/fix-links/tools/inspect/link_check.cpp (original)
+++ branches/fix-links/tools/inspect/link_check.cpp 2008-02-10 14:13:55 EST (Sun, 10 Feb 2008)
@@ -9,6 +9,7 @@
 #include "link_check.hpp"
 #include "boost/regex.hpp"
 #include "boost/filesystem/operations.hpp"
+#include <cstdlib>
 
 namespace fs = boost::filesystem;
 
@@ -19,6 +20,29 @@
     "\\s*=\\s*(['\"])(.*?)\\1",
     boost::regbase::normal | boost::regbase::icase);
 
+ // Decode percent encoded characters, returns an empty string if there's an
+ // error.
+ std::string decode_url(std::string const& path) {
+ std::string::size_type pos = 0, next;
+ std::string result;
+ result.reserve(path.length());
+
+ while((next = path.find('%', pos)) != std::string::npos) {
+ result.append(path, pos, next - pos);
+
+ if(path.length() - next < 3) return "";
+ char hex[3] = { path[next + 1], path[next + 2], '\0' };
+ char* end_ptr;
+ result += (char) std::strtol(hex, &end_ptr, 16);
+ if(*end_ptr) return "";
+ pos = next + 3;
+ }
+
+ result.append(path, pos, path.length());
+
+ return result;
+ }
+
 } // unnamed namespace
 
 namespace boost
@@ -121,13 +145,22 @@
         }
       }
 
+ string decoded_url = decode_url(plain_url);
+ if(decoded_url.empty()) {
+ if(!no_link_errors) {
+ ++m_invalid_errors;
+ error( library_name, source_path, string(name()) + " invalid URL: " + url );
+ }
+ return;
+ }
+
       // strip url of references to current dir
- if ( plain_url[0]=='.' && plain_url[1]=='/' ) plain_url.erase( 0, 2 );
+ if ( decoded_url[0]=='.' && decoded_url[1]=='/' ) decoded_url.erase( 0, 2 );
 
       // url is relative source_path.branch()
       // convert to target_path, which is_complete()
       path target_path;
- try { target_path = source_path.branch_path() /= path( plain_url, fs::no_check ); }
+ try { target_path = source_path.branch_path() /= path( decoded_url, fs::no_check ); }
       catch ( const fs::filesystem_error & )
       {
         if(!no_link_errors) {


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk