Reducing header overuse, non-inlining selected methods (CPPOST-35).
[shibboleth/cpp-opensaml.git] / saml / util / CommonDomainCookie.cpp
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  * 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 <xsec/framework/XSECDefs.hpp>
28 #include <xmltooling/XMLToolingConfig.h>
29 #include <xmltooling/util/URLEncoder.h>
30
31 using namespace opensaml;
32 using namespace xmltooling;
33 using namespace std;
34
35 const char CommonDomainCookie::CDCName[] = "_saml_idp";
36
37 CommonDomainCookie::CommonDomainCookie(const char* cookie)
38 {
39     if (!cookie)
40         return;
41
42     // Copy it so we can URL-decode it.
43     char* b64=strdup(cookie);
44     XMLToolingConfig::getConfig().getURLEncoder()->decode(b64);
45
46     // Chop it up and save off elements.
47     vector<string> templist;
48     char* ptr=b64;
49     while (*ptr) {
50         while (*ptr && isspace(*ptr)) ptr++;
51         char* end=ptr;
52         while (*end && !isspace(*end)) end++;
53         templist.push_back(string(ptr,end-ptr));
54         ptr=end;
55     }
56     free(b64);
57
58     // Now Base64 decode the list.
59     xsecsize_t len;
60     for (vector<string>::iterator i=templist.begin(); i!=templist.end(); ++i) {
61         XMLByte* decoded=Base64::decode(reinterpret_cast<const XMLByte*>(i->c_str()),&len);
62         if (decoded && *decoded) {
63             m_list.push_back(reinterpret_cast<char*>(decoded));
64 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
65             XMLString::release(&decoded);
66 #else
67             XMLString::release((char**)&decoded);
68 #endif
69         }
70     }
71 }
72
73 CommonDomainCookie::~CommonDomainCookie()
74 {
75 }
76
77 const vector<string>& CommonDomainCookie::get() const
78 {
79     return m_list;
80 }
81
82 const char* CommonDomainCookie::set(const char* entityID)
83 {
84     // First scan the list for this IdP.
85     for (vector<string>::iterator i=m_list.begin(); i!=m_list.end(); i++) {
86         if (*i == entityID) {
87             m_list.erase(i);
88             break;
89         }
90     }
91     
92     // Append it to the end.
93     m_list.push_back(entityID);
94     
95     // Now rebuild the delimited list.
96     xsecsize_t len;
97     string delimited;
98     for (vector<string>::const_iterator j=m_list.begin(); j!=m_list.end(); j++) {
99         if (!delimited.empty()) delimited += ' ';
100         
101         XMLByte* b64=Base64::encode(reinterpret_cast<const XMLByte*>(j->c_str()),j->length(),&len);
102         XMLByte *pos, *pos2;
103         for (pos=b64, pos2=b64; *pos2; pos2++)
104             if (isgraph(*pos2))
105                 *pos++=*pos2;
106         *pos=0;
107         
108         delimited += reinterpret_cast<char*>(b64);
109 #ifdef OPENSAML_XERCESC_HAS_XMLBYTE_RELEASE
110         XMLString::release(&b64);
111 #else
112         XMLString::release((char**)&b64);
113 #endif
114     }
115     
116     m_encoded=XMLToolingConfig::getConfig().getURLEncoder()->encode(delimited.c_str());
117     return m_encoded.c_str();
118 }