]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/headermap.cpp
e5215e3d87ee7ab0fc0b52b82f9b1fbfd28b55b7
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"
29 // Given a constant text string, extract the leading substring up to 'end' (or \0),
30 // canonicalize its case, and store the result for use.
32 HeaderMap::CanonicalKey::CanonicalKey(const char *key
, char end
)
34 assert(key
&& key
[0]); // non-empty
35 mValue
[0] = toupper(key
[0]);
36 for (unsigned int n
= 1; n
< sizeof(mValue
) - 1; n
++) {
41 mValue
[n
] = tolower(key
[n
]);
43 // overflow -- truncate? throw? dynamic allocation? seppuko? :-)
51 void HeaderMap::add(const char *key
, const char *value
)
53 add(CanonicalKey(key
), value
);
58 // Given a standard form (Key: value), add its value to the headermap
60 void HeaderMap::add(const char *form
)
62 while (*form
&& isspace(*form
))
64 if (const char *colon
= strchr(form
, ':')) {
65 CanonicalKey
key(form
, ':');
66 const char *value
= colon
+ 1;
67 while (*value
&& isspace(*value
))
72 //@@@ signal an error? how? how bad?
78 // Internal add method, given a canonicalized key
80 void HeaderMap::add(const CanonicalKey
&key
, const char *value
)
82 Map::iterator it
= mMap
.find(key
);
86 merge(key
, mMap
[key
], value
);
91 // Locate an entry in a headermap.
92 // Find returns NULL if not found; [] creates a new entry if needed and returns
93 // a reference to the value, in good STL tradition.
95 const char *HeaderMap::find(const char *key
, const char *defaultValue
) const
97 Map::const_iterator it
= mMap
.find(CanonicalKey(key
));
98 return (it
== mMap
.end()) ? defaultValue
: it
->second
.c_str();
101 string
&HeaderMap::operator[] (const char *key
)
103 return mMap
[CanonicalKey(key
)];
108 // The default implementation of merge throws out the old contents and replaces
109 // them with the new.
111 void HeaderMap::merge(string key
, string
&old
, string newValue
)
118 // Collect the entire contents into a single string
119 // Note that this is NOT exactly what was passed in; certain canonicalizations have
120 // been done; fields are reordered; and duplicate-header fields have been coalesced.
121 //@@@ size could be pre-calculated (running counter).
123 string
HeaderMap::collect(const char *lineEnding
) const
126 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
127 value
+= it
->first
+ ": " + it
->second
+ lineEnding
;
131 size_t HeaderMap::collectLength(const char *lineEnding
) const
134 size_t sepLength
= strlen(lineEnding
);
135 for (Map::const_iterator it
= mMap
.begin(); it
!= mMap
.end(); it
++)
136 size
+= it
->first
.length() + 2 + it
->second
.length() + sepLength
;
141 } // end namespace Security