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