activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r674238 - in /activemq/activemq-cpp/trunk/src: main/decaf/net/ test/ test/decaf/net/
Date Sat, 05 Jul 2008 20:29:27 GMT
Author: tabish
Date: Sat Jul  5 13:29:26 2008
New Revision: 674238

URL: http://svn.apache.org/viewvc?rev=674238&view=rev
Log:
Implementing the URL Encoder / Decoder plus tests.

Added:
    activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.cpp
    activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.h
    activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.cpp
    activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.h
Modified:
    activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.cpp
    activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.h
    activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.cpp
    activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.h
    activemq/activemq-cpp/trunk/src/test/Makefile.am
    activemq/activemq-cpp/trunk/src/test/testRegistry.cpp

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.cpp?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.cpp Sat Jul  5 13:29:26 2008
@@ -16,10 +16,60 @@
  */
 
 #include "URLDecoder.h"
+#include <decaf/lang/Character.h>
+#include <decaf/lang/exceptions/IllegalArgumentException.h>
 
+using namespace std;
 using namespace decaf;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
 using namespace decaf::net;
 
 ////////////////////////////////////////////////////////////////////////////////
 URLDecoder::URLDecoder() {
 }
+
+////////////////////////////////////////////////////////////////////////////////
+std::string URLDecoder::decode( const std::string& src ) {
+
+    std::string decoded = "";
+
+    for( size_t i = 0; i < src.length(); ) {
+        char c = src.at(i);
+
+        if (c == '+') {
+            decoded += ' ';
+        } else if (c == '%') {
+
+            do {
+
+                if( i + 2 >= src.length() ) {
+                    throw IllegalArgumentException(
+                        __FILE__, __LINE__,
+                        "Incomplete % sequence at: %d.", i );
+                }
+
+                int d1 = Character::digit( src.at(i + 1), 16 );
+                int d2 = Character::digit( src.at(i + 2), 16 );
+
+                if( d1 == -1 || d2 == -1 ) {
+                    throw IllegalArgumentException(
+                        __FILE__, __LINE__,
+                        "Invalid % sequence (%s) at: %d.",
+                        src.substr( i, 3 ).c_str(), i );
+                }
+
+                decoded += (char)( ( d1 << 4 ) + d2 );
+                i += 3;
+            } while( i < src.length() && src.at(i) == '%' );
+
+            continue;
+        } else {
+            decoded += c;
+        }
+
+        i++;
+    }
+
+    return decoded;
+}

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.h?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/URLDecoder.h Sat Jul  5 13:29:26 2008
@@ -19,16 +19,35 @@
 #define _DECAF_NET_URLDECODER_H_
 
 #include <decaf/util/Config.h>
+#include <string>
 
 namespace decaf{
 namespace net{
 
     class DECAF_API URLDecoder {
-    public:
+    private:
 
         URLDecoder();
+
+    public:
+
         virtual ~URLDecoder() {}
 
+        /**
+         * Decodes the string argument which is assumed to be encoded in the
+         * <code>x-www-form-urlencoded</code> MIME content type.
+         * <p>
+         * '+' will be converted to space, '%' and two following hex digit
+         * characters are converted to the equivalent byte value. All other
+         * characters are passed through unmodified.
+         * <p>
+         * e.g. "A+B+C %24%25" -> "A B C $%"
+         *
+         * @param s - string The encoded string.
+         * @return The decoded version as a string.
+         */
+        static std::string decode( const std::string& src );
+
     };
 
 }}

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.cpp?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.cpp (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.cpp Sat Jul  5 13:29:26 2008
@@ -16,10 +16,41 @@
  */
 
 #include "URLEncoder.h"
+#include <decaf/lang/Character.h>
 
+using namespace std;
 using namespace decaf;
+using namespace decaf::lang;
 using namespace decaf::net;
 
 ////////////////////////////////////////////////////////////////////////////////
+const std::string URLEncoder::digits = "0123456789ABCDEF";
+
+////////////////////////////////////////////////////////////////////////////////
 URLEncoder::URLEncoder() {
 }
+
+////////////////////////////////////////////////////////////////////////////////
+std::string URLEncoder::encode( const std::string& src ) {
+
+    string encoded = "";
+    string allowables = ".-*_";
+
+    for( std::size_t i = 0; i < src.length(); i++ ) {
+
+        char ch = src.at(i);
+        if( Character::isLetterOrDigit( ch ) ||
+            allowables.find_first_of( ch, 0 ) != std::string::npos ) {
+
+            encoded += ch;
+        } else if (ch == ' ') {
+            encoded += '+';
+        } else {
+            encoded += '%';
+            encoded += digits.at( (ch & 0xf0) >> 4 );
+            encoded += digits.at( ch & 0xf );
+        }
+    }
+
+    return encoded;
+}

Modified: activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.h?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.h (original)
+++ activemq/activemq-cpp/trunk/src/main/decaf/net/URLEncoder.h Sat Jul  5 13:29:26 2008
@@ -19,16 +19,42 @@
 #define _DECAF_NET_URLENCODER_H_
 
 #include <decaf/util/Config.h>
+#include <string>
 
 namespace decaf{
 namespace net{
 
     class DECAF_API URLEncoder {
-    public:
+    private:
+
+        static const std::string digits;
+
+    private:
 
         URLEncoder();
+
+    public:
+
         virtual ~URLEncoder() {}
 
+        /**
+         * This class contains a utility method for converting a string to the
+         * format required by the <code>application/x-www-form-urlencoded</code>
+         * MIME content type.
+         * <p>
+         * All characters except letters ('a'..'z', 'A'..'Z') and numbers ('0'..'9')
+         * and characters '.', '-', '*', '_' are converted into their hexidecimal
+         * value prepended by '%'.
+         * <p>
+         * For example: '#' -> %23
+         * <p>
+         * In addition, spaces are substituted by '+'
+         *
+         * @param s - the string to be converted
+         * @returns the converted string
+         */
+        static std::string encode( const std::string& src );
+
     };
 
 }}

Modified: activemq/activemq-cpp/trunk/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/Makefile.am?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/test/Makefile.am Sat Jul  5 13:29:26 2008
@@ -109,6 +109,8 @@
   decaf/net/SocketTest.cpp \
   decaf/net/URITest.cpp \
   decaf/net/URISyntaxExceptionTest.cpp \
+  decaf/net/URLEncoderTest.cpp \
+  decaf/net/URLDecoderTest.cpp \
   decaf/util/StringTokenizerTest.cpp \
   decaf/util/Endian.cpp \
   decaf/util/DateTest.cpp \
@@ -219,6 +221,8 @@
   decaf/net/SocketTest.h \
   decaf/net/URITest.h \
   decaf/net/URISyntaxExceptionTest.h \
+  decaf/net/URLEncoderTest.h \
+  decaf/net/URLDecoderTest.h \
   decaf/util/StringTokenizerTest.h \
   decaf/util/Endian.h \
   decaf/util/DateTest.h \

Added: activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.cpp?rev=674238&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.cpp Sat Jul  5 13:29:26
2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include "URLDecoderTest.h"
+
+#include <decaf/net/URLEncoder.h>
+
+using namespace decaf;
+using namespace decaf::net;
+
+////////////////////////////////////////////////////////////////////////////////
+URLDecoderTest::URLDecoderTest() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void URLDecoderTest::testDecode() {
+
+    const std::string URL = "http://127.0.0.1:80/%24%25";
+    const std::string URL2 = "telnet://justWantToHaveFun.com:400";
+    const std::string URL3 = "file://myServer.org/a file with spaces.jpg";
+
+    CPPUNIT_ASSERT_MESSAGE(
+        "1. Incorrect encoding/decoding",
+        URLDecoder::decode( URLEncoder::encode(URL) ) == URL );
+    CPPUNIT_ASSERT_MESSAGE(
+        "2. Incorrect encoding/decoding",
+        URLDecoder::decode( URLEncoder::encode(URL2) ) == URL2 );
+    CPPUNIT_ASSERT_MESSAGE(
+        "3. Incorrect encoding/decoding",
+        URLDecoder::decode( URLEncoder::encode(URL3) ) == URL3 );
+
+}

Added: activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.h?rev=674238&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/net/URLDecoderTest.h Sat Jul  5 13:29:26 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#ifndef _DECAF_NET_URLDECODERTEST_H_
+#define _DECAF_NET_URLDECODERTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/net/URLDecoder.h>
+
+namespace decaf {
+namespace net {
+
+    class URLDecoderTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( URLDecoderTest );
+        CPPUNIT_TEST( testDecode );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        URLDecoderTest();
+        virtual ~URLDecoderTest() {}
+
+        void testDecode();
+
+    };
+
+}}
+
+#endif /* _DECAF_NET_URLDECODERTEST_H_ */

Added: activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.cpp?rev=674238&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.cpp Sat Jul  5 13:29:26
2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#include "URLEncoderTest.h"
+
+#include <decaf/net/URLDecoder.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::net;
+
+////////////////////////////////////////////////////////////////////////////////
+URLEncoderTest::URLEncoderTest() {}
+
+////////////////////////////////////////////////////////////////////////////////
+void URLEncoderTest::testEncode() {
+
+    const std::string URL = "http://127.0.0.1:80/%24%25";
+    const std::string URL2 = "telnet://justWantToHaveFun.com:400";
+    const std::string URL3 = "file://myServer.org/a file with spaces.jpg";
+
+    CPPUNIT_ASSERT_MESSAGE(
+            "1. Incorrect encoding/decoding",
+            URLDecoder::decode( URLEncoder::encode( URL ) ) == URL );
+    CPPUNIT_ASSERT_MESSAGE(
+            "2. Incorrect encoding/decoding",
+            URLDecoder::decode( URLEncoder::encode( URL2 ) ) == URL2 );
+    CPPUNIT_ASSERT_MESSAGE(
+            "3. Incorrect encoding/decoding",
+            URLDecoder::decode( URLEncoder::encode( URL3 ) ) == URL3 );
+
+}

Added: activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.h?rev=674238&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.h (added)
+++ activemq/activemq-cpp/trunk/src/test/decaf/net/URLEncoderTest.h Sat Jul  5 13:29:26 2008
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+#ifndef _DECAF_NET_URLENCODERTEST_H_
+#define _DECAF_NET_URLENCODERTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/net/URLEncoder.h>
+
+namespace decaf {
+namespace net {
+
+    class URLEncoderTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( URLEncoderTest );
+        CPPUNIT_TEST( testEncode );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        URLEncoderTest();
+        virtual ~URLEncoderTest() {}
+
+        void testEncode();
+
+    };
+
+}}
+
+#endif /* _DECAF_NET_URLENCODERTEST_H_ */

Modified: activemq/activemq-cpp/trunk/src/test/testRegistry.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/test/testRegistry.cpp?rev=674238&r1=674237&r2=674238&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/src/test/testRegistry.cpp (original)
+++ activemq/activemq-cpp/trunk/src/test/testRegistry.cpp Sat Jul  5 13:29:26 2008
@@ -224,6 +224,10 @@
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::URITest );
 #include <decaf/net/URISyntaxExceptionTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::URISyntaxExceptionTest );
+#include <decaf/net/URLEncoderTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::URLEncoderTest );
+#include <decaf/net/URLDecoderTest.h>
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::URLDecoderTest );
 
 #include <decaf/util/concurrent/CountDownLatchTest.h>
 CPPUNIT_TEST_SUITE_REGISTRATION( decaf::util::concurrent::CountDownLatchTest );



Mime
View raw message