From 0f8492e881a026c0e3348c1ec77351191beb31d1 Mon Sep 17 00:00:00 2001
From: Alberto Massari <amassari@apache.org>
Date: Fri, 12 May 2006 21:21:05 +0000
Subject: [PATCH] Allow spaces in anyURIs, as they are 'legal but highly
 discouraged' (jira#1581)

git-svn-id: https://svn.apache.org/repos/asf/xerces/c/trunk@405875 13f79535-47bb-0310-9956-ffa450edef68
---
 src/xercesc/framework/psvi/XSValue.cpp         |  2 +-
 src/xercesc/util/XMLUri.cpp                    | 18 +++++++++++-------
 src/xercesc/util/XMLUri.hpp                    |  8 +++++---
 .../datatype/AnyURIDatatypeValidator.cpp       |  2 +-
 4 files changed, 18 insertions(+), 12 deletions(-)

diff --git a/src/xercesc/framework/psvi/XSValue.cpp b/src/xercesc/framework/psvi/XSValue.cpp
index fc911f131..8edafb042 100644
--- a/src/xercesc/framework/psvi/XSValue.cpp
+++ b/src/xercesc/framework/psvi/XSValue.cpp
@@ -756,7 +756,7 @@ bool XSValue::validateStrings(const XMLCh*         const content
             }            
             break;
         case XSValue::dt_anyURI:
-            if (XMLUri::isValidURI(true, content) == false) {
+            if (XMLUri::isValidURI(true, content, true) == false) {
                 isValid = false;
             }        
             break;
diff --git a/src/xercesc/util/XMLUri.cpp b/src/xercesc/util/XMLUri.cpp
index 29534a144..a0d5fdc32 100644
--- a/src/xercesc/util/XMLUri.cpp
+++ b/src/xercesc/util/XMLUri.cpp
@@ -2017,7 +2017,8 @@ void XMLUri::buildFullText()
 
 // NOTE: no check for NULL value of uriStr (caller responsiblilty)
 bool XMLUri::isValidURI(const XMLUri* const baseURI
-                       , const XMLCh* const uriStr)
+                       , const XMLCh* const uriStr
+                       , bool bAllowSpaces/*=false*/)
 {
     // get a trimmed version of uriStr
     // uriStr will NO LONGER be used in this function.
@@ -2108,7 +2109,7 @@ bool XMLUri::isValidURI(const XMLUri* const baseURI
     // we need to check if index has exceed the lenght or not
     if (index < trimmedUriSpecLen)
     {
-	    if (!processPath(trimmedUriSpec + index, trimmedUriSpecLen - index, foundScheme))
+	    if (!processPath(trimmedUriSpec + index, trimmedUriSpecLen - index, foundScheme, bAllowSpaces))
             return false;
     }
 
@@ -2118,7 +2119,7 @@ bool XMLUri::isValidURI(const XMLUri* const baseURI
 // NOTE: no check for NULL value of uriStr (caller responsiblilty)
 // NOTE: this routine is the same as above, but it uses a flag to
 //       indicate the existance of a baseURI rather than an XMLuri.
-bool XMLUri::isValidURI(bool haveBaseURI, const XMLCh* const uriStr)
+bool XMLUri::isValidURI(bool haveBaseURI, const XMLCh* const uriStr, bool bAllowSpaces/*=false*/)
 {
     // get a trimmed version of uriStr
     // uriStr will NO LONGER be used in this function.
@@ -2208,7 +2209,7 @@ bool XMLUri::isValidURI(bool haveBaseURI, const XMLCh* const uriStr)
     // we need to check if index has exceed the length or not
     if (index < trimmedUriSpecLen)
     {
-        if (!processPath(trimmedUriSpec + index, trimmedUriSpecLen - index, foundScheme))
+        if (!processPath(trimmedUriSpec + index, trimmedUriSpecLen - index, foundScheme, bAllowSpaces))
             return false;
     }
 
@@ -2417,7 +2418,8 @@ bool XMLUri::processAuthority( const XMLCh* const authSpec
 
 bool XMLUri::processPath(const XMLCh* const pathStr,
                          const int pathStrLen,
-                         const bool isSchemePresent)
+                         const bool isSchemePresent,
+                         const bool bAllowSpaces/*=false*/)
 {
     if (pathStrLen != 0)
     {
@@ -2441,7 +2443,8 @@ bool XMLUri::processPath(const XMLCh* const pathStr,
                     !XMLString::isHex(pathStr[index+2]))
                         return false;
             }
-            else if (!isUnreservedCharacter(testChar) &&
+            else if ((testChar==chSpace && !bAllowSpaces) &&
+                     !isUnreservedCharacter(testChar) &&
                      ((isOpaque && !isPathCharacter(testChar)) ||
                       (!isOpaque && !isReservedCharacter(testChar))))
             {
@@ -2473,7 +2476,8 @@ bool XMLUri::processPath(const XMLCh* const pathStr,
                         !XMLString::isHex(pathStr[index+2]))
                         return false;
                 }
-                else if (!isReservedOrUnreservedCharacter(testChar))                    
+                else if ((testChar==chSpace && !bAllowSpaces) &&
+                         !isReservedOrUnreservedCharacter(testChar))                    
                 {
                     return false;
                 }
diff --git a/src/xercesc/util/XMLUri.hpp b/src/xercesc/util/XMLUri.hpp
index 1bd536ed5..871bec198 100644
--- a/src/xercesc/util/XMLUri.hpp
+++ b/src/xercesc/util/XMLUri.hpp
@@ -291,12 +291,14 @@ public:
      * Determine whether a given string is a valid URI
      */
     static bool isValidURI( const XMLUri* const baseURI
-                          , const XMLCh* const uriStr);
+                          , const XMLCh* const uriStr
+                          , bool bAllowSpaces=false);
     /**
      * Determine whether a given string is a valid URI
      */
     static bool isValidURI( bool haveBaseURI
-                          , const XMLCh* const uriStr);
+                          , const XMLCh* const uriStr
+                          , bool bAllowSpaces=false);
 
 
     static void normalizeURI(const XMLCh*     const systemURI,
@@ -552,7 +554,7 @@ private:
     static bool processAuthority(const XMLCh* const uriStr, const int authLen);
     static bool isWellFormedAddress(const XMLCh* const addr, const int addrLen);
     static bool processPath(const XMLCh* const pathStr, const int pathStrLen,
-                            const bool isSchemePresent);
+                            const bool isSchemePresent, const bool bAllowSpaces=false);
 
     // -----------------------------------------------------------------------
     //  Data members
diff --git a/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp b/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
index 7fafdce8a..07a6c15a3 100644
--- a/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
+++ b/src/xercesc/validators/datatype/AnyURIDatatypeValidator.cpp
@@ -76,7 +76,7 @@ void AnyURIDatatypeValidator::checkValueSpace(const XMLCh* const content
         //
         if (XMLString::stringLen(content))
         {          
-              if (!XMLUri::isValidURI(true, content))
+              if (!XMLUri::isValidURI(true, content, true))
                 ThrowXMLwithMemMgr1(InvalidDatatypeValueException
                     , XMLExcepts::VALUE_URI_Malformed
                     , content
-- 
GitLab