]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/headermap.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // headermap - represent Internet-standard headers
22 #include "headermap.h"
31 // Given a constant text string, extract the leading substring up to 'end' (or \0),
32 // canonicalize its case, and store the result for use.
34 HeaderMap::CanonicalKey::CanonicalKey(const char *key
, char end
)
36 assert(key
&& key
[0]); // non-empty
37 mValue
[0] = toupper(key
[0]);
38 for (unsigned int n
= 1; n
< sizeof(mValue
) - 1; n
++) {
43 mValue
[n
] = tolower(key
[n
]);
45 // overflow -- truncate? throw? dynamic allocation? seppuko? :-)
53 void HeaderMap::add(const char *key
, const char *value
)
55 add(CanonicalKey(key
), value
);
60 // Given a standard form (Key: value), add its value to the headermap
62 void HeaderMap::add(const char *form
)
64 while (*form
&& isspace(*form
))
66 if (const char *colon
= strchr(form
, ':')) {
67 CanonicalKey
key(form
, ':');
68 const char *value
= colon
+ 1;
69 while (*value
&& isspace(*value
))
74 //@@@ signal an error? how? how bad?
80 // Internal add method, given a canonicalized key
82 void HeaderMap::add(const CanonicalKey
&key
, const char *value
)
84 Map::iterator it
= mMap
.find(key
);
88 merge(key
, mMap
[key
], value
);
93 // Locate an entry in a headermap.
94 // Find returns NULL if not found; [] creates a new entry if needed and returns
95 // a reference to the value, in good STL tradition.
97 const char *HeaderMap::find(const char *key
, const char *defaultValue
) const
99 Map::const_iterator it
= mMap
.find(CanonicalKey(key
));
100 return (it
== mMap
.end()) ? defaultValue
: it
->second
.c_str();
103 string
&HeaderMap::operator[] (const char *key
)
105 return mMap
[CanonicalKey(key
)];
110 // The default implementation of merge throws out the old contents and replaces
111 // them with the new.
113 void HeaderMap::merge(string key
, string
&old
, string newValue
)
120 // Collect the entire contents into a single string
121 // Note that this is NOT exactly what was passed in; certain canonicalizations have
122 // been done; fields are reordered; and duplicate-header fields have been coalesced.
123 //@@@ size could be pre-calculated (running counter).
125 string
HeaderMap::collect(const char *lineEnding
) const
128 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
129 value
+= it
->first
+ ": " + it
->second
+ lineEnding
;
133 size_t HeaderMap::collectLength(const char *lineEnding
) const
136 size_t sepLength
= strlen(lineEnding
);
137 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
138 size
+= it
->first
.length() + 2 + it
->second
.length() + sepLength
;
143 } // end namespace Security