Moved URLEncoder into separate header, made it a global service.
authorScott Cantor <cantor.2@osu.edu>
Wed, 27 Sep 2006 17:28:49 +0000 (17:28 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 27 Sep 2006 17:28:49 +0000 (17:28 +0000)
saml/Makefile.am
saml/SAMLConfig.cpp
saml/SAMLConfig.h
saml/binding/MessageEncoder.h
saml/binding/URLEncoder.h [new file with mode: 0644]
saml/binding/impl/URLEncoder.cpp [new file with mode: 0644]
saml/saml.vcproj
saml/saml1/binding/impl/SAML1ArtifactEncoder.cpp
saml/saml2/binding/impl/SAML2ArtifactEncoder.cpp

index 1aa5ff4..cc3ee2d 100644 (file)
@@ -33,7 +33,8 @@ libsamlinclude_HEADERS = \
 samlbindinclude_HEADERS = \
        binding/ArtifactMap.h \
        binding/MessageEncoder.h \
-       binding/SAMLArtifact.h
+       binding/SAMLArtifact.h \
+       binding/URLEncoder.h
 
 encinclude_HEADERS = \
        encryption/EncryptedKeyResolver.h
@@ -88,6 +89,7 @@ libsaml_la_SOURCES = \
        SAMLConfig.cpp \
        binding/impl/ArtifactMap.cpp \
        binding/impl/SAMLArtifact.cpp \
+       binding/impl/URLEncoder.cpp \
        saml1/core/impl/AssertionsImpl.cpp \
        saml1/core/impl/AssertionsSchemaValidators.cpp \
        saml1/core/impl/ProtocolsImpl.cpp \
index 3d40148..5d68230 100644 (file)
 #include "internal.h"
 #include "exceptions.h"
 #include "SAMLConfig.h"
+#include "binding/ArtifactMap.h"
 #include "binding/MessageEncoder.h"
 #include "binding/SAMLArtifact.h"
+#include "binding/URLEncoder.h"
 #include "saml1/core/Assertions.h"
 #include "saml1/core/Protocols.h"
 #include "saml2/core/Protocols.h"
@@ -82,6 +84,18 @@ SAMLInternalConfig& SAMLInternalConfig::getInternalConfig()
     return g_config;
 }
 
+void SAMLConfig::setArtifactMap(ArtifactMap* artifactMap)
+{
+    delete m_artifactMap;
+    m_artifactMap = artifactMap;
+}
+
+void SAMLConfig::setURLEncoder(URLEncoder* urlEncoder)
+{
+    delete m_urlEncoder;
+    m_urlEncoder = urlEncoder;
+}
+
 bool SAMLInternalConfig::init(bool initXMLTooling)
 {
 #ifdef _DEBUG
@@ -109,6 +123,8 @@ bool SAMLInternalConfig::init(bool initXMLTooling)
     saml2md::registerMetadataProviders();
     saml2md::registerMetadataFilters();
     registerTrustEngines();
+    
+    m_urlEncoder = new URLEncoder();
 
     log.info("library initialization complete");
     return true;
@@ -134,6 +150,8 @@ void SAMLInternalConfig::term(bool termXMLTooling)
 
     delete m_artifactMap;
     m_artifactMap = NULL;
+    delete m_urlEncoder;
+    m_urlEncoder = NULL;
 
     if (termXMLTooling) {
         XMLToolingConfig::getConfig().term();
index 90e00ad..1d0dbd1 100644 (file)
@@ -24,7 +24,6 @@
 #define __saml_config_h__\r
 \r
 #include <saml/base.h>\r
-#include <saml/binding/ArtifactMap.h>\r
 \r
 #include <xmltooling/PluginManager.h>\r
 #include <xmltooling/XMLToolingConfig.h>\r
  */\r
 namespace opensaml {\r
 \r
+    class SAML_API ArtifactMap;\r
     class SAML_API MessageEncoder;\r
     class SAML_API MessageDecoder;\r
     class SAML_API SAMLArtifact;\r
     class SAML_API TrustEngine;\r
+    class SAML_API URLEncoder;\r
 \r
     namespace saml2md {\r
         class SAML_API MetadataProvider;\r
@@ -100,10 +101,7 @@ namespace opensaml {
          * \r
          * @param artifactMap   new ArtifactMap instance to store\r
          */\r
-        void setArtifactMap(ArtifactMap* artifactMap) {\r
-            delete m_artifactMap;\r
-            m_artifactMap = artifactMap;\r
-        }\r
+        void setArtifactMap(ArtifactMap* artifactMap);\r
         \r
         /**\r
          * Returns the global ArtifactMap instance.\r
@@ -113,6 +111,24 @@ namespace opensaml {
         ArtifactMap* getArtifactMap() const {\r
             return m_artifactMap;\r
         }\r
+\r
+        /**\r
+         * Sets the global URLEncoder instance.\r
+         * This method must be externally synchronized with any code that uses the object.\r
+         * Any previously set object is destroyed.\r
+         * \r
+         * @param urlEncoder   new URLEncoder instance to store\r
+         */\r
+        void setURLEncoder(URLEncoder* urlEncoder);\r
+        \r
+        /**\r
+         * Returns the global URLEncoder instance.\r
+         * \r
+         * @return  global URLEncoder\r
+         */\r
+        URLEncoder* getURLEncoder() const {\r
+            return m_urlEncoder;\r
+        }\r
         \r
         /**\r
          * Generate random information using the underlying security library\r
@@ -179,10 +195,13 @@ namespace opensaml {
         xmltooling::PluginManager<saml2md::MetadataFilter,const DOMElement*> MetadataFilterManager;\r
 \r
     protected:\r
-        SAMLConfig() : m_artifactMap(NULL) {}\r
+        SAMLConfig() : m_artifactMap(NULL), m_urlEncoder(NULL) {}\r
         \r
         /** Global ArtifactMap instance for use by artifact-related functions. */\r
         ArtifactMap* m_artifactMap;\r
+\r
+        /** Global URLEncoder instance for use by URL-related functions. */\r
+        URLEncoder* m_urlEncoder;\r
     };\r
 \r
 #if defined (_MSC_VER)\r
index 81e39d2..2a5fc71 100644 (file)
@@ -48,39 +48,6 @@ namespace opensaml {
         virtual ~MessageEncoder() {}\r
 \r
         /**\r
-         * Interface to caller-supplied URL-encoding mechanism.\r
-         * \r
-         * Since URL-encoding is not canonical, it's important that the same\r
-         * encoder is used during some binding-specific signature operations.\r
-         */\r
-        class SAML_API URLEncoder {\r
-            MAKE_NONCOPYABLE(URLEncoder);\r
-        protected:\r
-            URLEncoder() {}\r
-        public:\r
-            virtual ~URLEncoder() {}\r
-            \r
-            /**\r
-             * Produce a URL-safe but equivalent version of the input string.\r
-             * \r
-             * @param s input string to encode\r
-             * @return a string object containing the result of encoding the input\r
-             */\r
-            virtual std::string encode(const char* s) const=0;\r
-        };\r
-        \r
-        /**\r
-         * Provides a URLEncoder implementation for the MessageEncoder to use.\r
-         * The implementation's lifetime must be longer than the lifetime of this object.\r
-         * This method must be externally synchronized. \r
-         * \r
-         * @param urlEncoder    a URLEncoder implementation to use\r
-         */\r
-        void setURLEncoder(const URLEncoder* urlEncoder) {\r
-            m_urlEncoder = urlEncoder;\r
-        }\r
-\r
-        /**\r
          * Interface to caller-supplied artifact generation mechanism.\r
          * \r
          * Generating an artifact for storage and retrieval requires knowledge of\r
@@ -162,7 +129,7 @@ namespace opensaml {
             ) const=0;\r
 \r
     protected:\r
-        MessageEncoder() : m_urlEncoder(NULL), m_artifactGenerator(NULL) {}\r
+        MessageEncoder() : m_artifactGenerator(NULL) {}\r
         \r
         /**\r
          * Helper function to build a new XML Signature with KeyInfo, based\r
@@ -177,9 +144,6 @@ namespace opensaml {
             const XMLCh* sigAlgorithm=NULL\r
             ) const;\r
         \r
-        /** Pointer to a URLEncoder implementation. */\r
-        const URLEncoder* m_urlEncoder;\r
-        \r
         /** Pointer to an ArtifactGenerator implementation. */\r
         const ArtifactGenerator* m_artifactGenerator;\r
     };\r
diff --git a/saml/binding/URLEncoder.h b/saml/binding/URLEncoder.h
new file mode 100644 (file)
index 0000000..2585d6d
--- /dev/null
@@ -0,0 +1,74 @@
+/*\r
+ *  Copyright 2001-2006 Internet2\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+/**\r
+ * @file saml/binding/URLEncoder.h\r
+ * \r
+ * Interface to a URL-encoding mechanism along with a\r
+ * default implementation.\r
+ */\r
+\r
+#ifndef __saml_urlenc_h__\r
+#define __saml_urlenc_h__\r
+\r
+#include <saml/base.h>\r
+\r
+namespace opensaml {\r
+    /**\r
+     * Interface to a URL-encoding mechanism along with a default implementation.\r
+     * \r
+     * Since URL-encoding is not canonical, it's important that the same\r
+     * encoder is used during some library operations and the calling code.\r
+     * Applications can supply an alternative implementation to the library\r
+     * if required.\r
+     */\r
+    class SAML_API URLEncoder {\r
+        MAKE_NONCOPYABLE(URLEncoder);\r
+    public:\r
+        URLEncoder() {}\r
+        virtual ~URLEncoder() {}\r
+        \r
+        /**\r
+         * Produce a URL-safe but equivalent version of the input string.\r
+         * \r
+         * @param s input string to encode\r
+         * @return a string object containing the result of encoding the input\r
+         */\r
+        virtual std::string encode(const char* s) const;\r
+        \r
+        /**\r
+         * Perform an in-place decoding operation on the input string.\r
+         * The resulting string will be NULL-terminated.\r
+         * \r
+         * @param s input string to decode in a writable buffer\r
+         */\r
+        virtual void decode(char* s) const;\r
+        \r
+    protected:\r
+        /**\r
+         * Returns true iff the input character requires encoding.\r
+         * \r
+         * @param ch    the character to check\r
+         * @return  true iff the character should be encoded \r
+         */\r
+        virtual bool isBad(char ch) const {\r
+            static char badchars[]="=&/?:\"\\+<>#%{}|^~[]`;@";\r
+            return (strchr(badchars,ch) || ch<=0x20 || ch>=0x7F);\r
+        }\r
+    };\r
+};\r
+\r
+#endif /* __saml_urlenc_h__ */\r
diff --git a/saml/binding/impl/URLEncoder.cpp b/saml/binding/impl/URLEncoder.cpp
new file mode 100644 (file)
index 0000000..81f0a51
--- /dev/null
@@ -0,0 +1,77 @@
+/*
+ *  Copyright 2001-2006 Internet2
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * URLEncoder.cpp
+ * 
+ * Interface to a URL-encoding mechanism along with a
+ * default implementation. 
+ */
+
+#include "internal.h"
+#include "binding/URLEncoder.h"
+
+using namespace opensaml;
+using namespace std;
+
+static char x2c(char *what)
+{
+    register char digit;
+
+    digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
+    digit *= 16;
+    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
+    return(digit);
+}
+
+void URLEncoder::decode(char* s) const
+{
+    register int x,y;
+
+    for(x=0,y=0;s[y];++x,++y)
+    {
+        if((s[x] = s[y]) == '%')
+        {
+            s[x] = x2c(&s[y+1]);
+            y+=2;
+        }
+        else if (s[x] == '+')
+        {
+            s[x] = ' ';
+        }
+    }
+    s[x] = '\0';
+}
+
+static inline char hexchar(unsigned short s)
+{
+    return (s<=9) ? ('0' + s) : ('A' + s - 10);
+}
+
+string URLEncoder::encode(const char* s) const
+{
+    string ret;
+    for (; *s; s++) {
+        if (isBad(*s)) {
+            ret+='%';
+            ret+=hexchar(*s >> 4);
+            ret+=hexchar(*s & 0x0F);
+        }
+        else
+            ret+=*s;
+    }
+    return ret;
+}
index 625c424..4a8d469 100644 (file)
                                                RelativePath=".\binding\impl\SAMLArtifact.cpp"\r
                                                >\r
                                        </File>\r
+                                       <File\r
+                                               RelativePath=".\binding\impl\URLEncoder.cpp"\r
+                                               >\r
+                                       </File>\r
                                </Filter>\r
                        </Filter>\r
                </Filter>\r
                                        RelativePath=".\binding\SAMLArtifact.h"\r
                                        >\r
                                </File>\r
+                               <File\r
+                                       RelativePath=".\binding\URLEncoder.h"\r
+                                       >\r
+                               </File>\r
                        </Filter>\r
                </Filter>\r
                <Filter\r
index 197ecb4..cc76f7c 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "internal.h"
 #include "exceptions.h"
+#include "saml/binding/ArtifactMap.h"
 #include "saml/binding/SAMLArtifact.h"
 #include "saml1/binding/SAML1ArtifactEncoder.h"
 #include "saml1/core/Assertions.h"
index 386b07c..eec06cb 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "internal.h"
 #include "exceptions.h"
+#include "saml/binding/ArtifactMap.h"
 #include "saml2/binding/SAML2Artifact.h"
 #include "saml2/binding/SAML2ArtifactEncoder.h"
 #include "saml2/core/Protocols.h"