ec4bba657ad5c98d75e9d3df7455c13e331917bf
[shibboleth/cpp-xmltooling.git] / xmltooling / util / URLEncoder.h
1 /*
2  *  Copyright 2001-2009 Internet2
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file xmltooling/util/URLEncoder.h
19  *
20  * Interface to a URL-encoding mechanism along with a
21  * default implementation.
22  */
23
24 #ifndef __xmltool_urlenc_h__
25 #define __xmltool_urlenc_h__
26
27 #include <xmltooling/base.h>
28
29 namespace xmltooling {
30     /**
31      * Interface to a URL-encoding mechanism along with a default implementation.
32      *
33      * Since URL-encoding is not canonical, it's important that the same
34      * encoder is used during some library operations and the calling code.
35      * Applications can supply an alternative implementation to the library
36      * if required.
37      */
38     class XMLTOOL_API URLEncoder {
39         MAKE_NONCOPYABLE(URLEncoder);
40     public:
41         URLEncoder() {}
42         virtual ~URLEncoder() {}
43
44         /**
45          * Produce a URL-safe but equivalent version of the input string.
46          *
47          * @param s input string to encode
48          * @return a string object containing the result of encoding the input
49          */
50         virtual std::string encode(const char* s) const;
51
52         /**
53          * Perform an in-place decoding operation on the input string.
54          * The resulting string will be NULL-terminated.
55          *
56          * @param s input string to decode in a writable buffer
57          */
58         virtual void decode(char* s) const;
59
60     protected:
61         /**
62          * Returns true iff the input character requires encoding.
63          *
64          * @param ch    the character to check
65          * @return  true iff the character should be encoded
66          */
67         virtual bool isBad(char ch) const {
68             static char badchars[]="=&/?:\"\\+<>#%{}|^~[],`;@";
69             return (ch<=0x20 || ch>=0x7F || strchr(badchars,ch));
70         }
71     };
72 };
73
74 #endif /* __xmltool_urlenc_h__ */