Moved URLEncoder down to tooling lib, added exception->querystring method.
[shibboleth/cpp-opensaml.git] / saml / util / CommonDomainCookie.cpp
1 /*
2  *  Copyright 2001-2007 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  * CommonDomainCookie.cpp
19  * 
20  * Helper class for maintaining discovery cookie. 
21  */
22
23 #include "internal.h"
24 #include "util/CommonDomainCookie.h"
25
26 #include <xercesc/util/Base64.hpp>
27 #include <xmltooling/XMLToolingConfig.h>
28 #include <xmltooling/util/URLEncoder.h>
29
30 using namespace opensaml;
31 using namespace xmltooling;
32 using namespace std;
33
34 const char CommonDomainCookie::CDCName[] = "_saml_idp";
35
36 CommonDomainCookie::CommonDomainCookie(const char* cookie)
37 {
38     if (!cookie)
39         return;
40
41     // Copy it so we can URL-decode it.
42     char* b64=strdup(cookie);
43     XMLToolingConfig::getConfig().getURLEncoder()->decode(b64);
44
45     // Chop it up and save off elements.
46     vector<string> templist;
47     char* ptr=b64;
48     while (*ptr) {
49         while (*ptr && isspace(*ptr)) ptr++;
50         char* end=ptr;
51         while (*end && !isspace(*end)) end++;
52         templist.push_back(string(ptr,end-ptr));
53         ptr=end;
54     }
55     free(b64);
56
57     // Now Base64 decode the list.
58     unsigned int len;
59     for (vector<string>::iterator i=templist.begin(); i!=templist.end(); ++i) {
60         XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(i->c_str()),&len);
61         if (decoded && *decoded) {
62             m_list.push_back(reinterpret_cast<char*>(decoded));
63             XMLString::release(&decoded);
64         }
65     }
66 }
67
68 const char* CommonDomainCookie::set(const char* entityID)
69 {
70     // First scan the list for this IdP.
71     for (vector<string>::iterator i=m_list.begin(); i!=m_list.end(); i++) {
72         if (*i == entityID) {
73             m_list.erase(i);
74             break;
75         }
76     }
77     
78     // Append it to the end.
79     m_list.push_back(entityID);
80     
81     // Now rebuild the delimited list.
82     unsigned int len;
83     string delimited;
84     for (vector<string>::const_iterator j=m_list.begin(); j!=m_list.end(); j++) {
85         if (!delimited.empty()) delimited += ' ';
86         
87         XMLByte* b64=Base64::encode(reinterpret_cast<const XMLByte*>(j->c_str()),j->length(),&len);
88         XMLByte *pos, *pos2;
89         for (pos=b64, pos2=b64; *pos2; pos2++)
90             if (isgraph(*pos2))
91                 *pos++=*pos2;
92         *pos=0;
93         
94         delimited += reinterpret_cast<char*>(b64);
95         XMLString::release(&b64);
96     }
97     
98     m_encoded=XMLToolingConfig::getConfig().getURLEncoder()->encode(delimited.c_str());
99     return m_encoded.c_str();
100 }