]>
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"
32 // Given a constant text string, extract the leading substring up to 'end' (or \0),
33 // canonicalize its case, and store the result for use.
35 HeaderMap::CanonicalKey::CanonicalKey(const char *key
, char end
)
37 assert(key
&& key
[0]); // non-empty
38 mValue
[0] = toupper(key
[0]);
39 for (unsigned int n
= 1; n
< sizeof(mValue
) - 1; n
++) {
44 mValue
[n
] = tolower(key
[n
]);
46 // overflow -- truncate? throw? dynamic allocation? seppuko? :-)
54 void HeaderMap::add(const char *key
, const char *value
)
56 add(CanonicalKey(key
), value
);
61 // Given a standard form (Key: value), add its value to the headermap
63 void HeaderMap::add(const char *form
)
65 while (*form
&& isspace(*form
))
67 if (const char *colon
= strchr(form
, ':')) {
68 CanonicalKey
key(form
, ':');
69 const char *value
= colon
+ 1;
70 while (*value
&& isspace(*value
))
75 //@@@ signal an error? how? how bad?
81 // Internal add method, given a canonicalized key
83 void HeaderMap::add(const CanonicalKey
&key
, const char *value
)
85 Map::iterator it
= mMap
.find(key
);
89 merge(key
, mMap
[key
], value
);
94 // Locate an entry in a headermap.
95 // Find returns NULL if not found; [] creates a new entry if needed and returns
96 // a reference to the value, in good STL tradition.
98 const char *HeaderMap::find(const char *key
, const char *defaultValue
) const
100 Map::const_iterator it
= mMap
.find(CanonicalKey(key
));
101 return (it
== mMap
.end()) ? defaultValue
: it
->second
.c_str();
104 string
&HeaderMap::operator[] (const char *key
)
106 return mMap
[CanonicalKey(key
)];
111 // The default implementation of merge throws out the old contents and replaces
112 // them with the new.
114 void HeaderMap::merge(string key
, string
&old
, string newValue
)
121 // Collect the entire contents into a single string
122 // Note that this is NOT exactly what was passed in; certain canonicalizations have
123 // been done; fields are reordered; and duplicate-header fields have been coalesced.
124 //@@@ size could be pre-calculated (running counter).
126 string
HeaderMap::collect(const char *lineEnding
) const
129 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
130 value
+= it
->first
+ ": " + it
->second
+ lineEnding
;
134 size_t HeaderMap::collectLength(const char *lineEnding
) const
137 size_t sepLength
= strlen(lineEnding
);
138 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
139 size
+= it
->first
.length() + 2 + it
->second
.length() + sepLength
;
144 } // end namespace Security