]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/headermap.cpp
8f6eebebc042f429a1ee177cb27172baa321c1f6
   2  * Copyright (c) 2000-2004,2011,2014 Apple Inc. All Rights Reserved. 
   4  * @APPLE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. Please obtain a copy of the License at 
  10  * http://www.opensource.apple.com/apsl/ and read it before using this 
  13  * The Original Code and all software distributed under the License are 
  14  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  15  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  16  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  17  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  18  * Please see the License for the specific language governing rights and 
  19  * limitations under the License. 
  21  * @APPLE_LICENSE_HEADER_END@ 
  26 // headermap - represent Internet-standard headers 
  28 #include "headermap.h" 
  38 // Given a constant text string, extract the leading substring up to 'end' (or \0), 
  39 // canonicalize its case, and store the result for use. 
  41 HeaderMap::CanonicalKey::CanonicalKey(const char *key
, char end
) 
  43     assert(key 
&& key
[0]);      // non-empty 
  44     mValue
[0] = toupper(key
[0]); 
  45     for (unsigned int n 
= 1; n 
< sizeof(mValue
) - 1; n
++) { 
  50         mValue
[n
] = tolower(key
[n
]); 
  52     // overflow -- truncate? throw? dynamic allocation? seppuko? :-) 
  60 void HeaderMap::add(const char *key
, const char *value
) 
  62     add(CanonicalKey(key
), value
); 
  67 // Given a standard form (Key: value), add its value to the headermap 
  69 void HeaderMap::add(const char *form
) 
  71     while (*form 
&& isspace(*form
)) 
  73     if (const char *colon 
= strchr(form
, ':')) { 
  74         CanonicalKey 
key(form
, ':'); 
  75         const char *value 
= colon 
+ 1; 
  76         while (*value 
&& isspace(*value
)) 
  81         //@@@ signal an error? how? how bad? 
  87 // Internal add method, given a canonicalized key 
  89 void HeaderMap::add(const CanonicalKey 
&key
, const char *value
) 
  91     Map::iterator it 
= mMap
.find(key
); 
  95         merge(key
, mMap
[key
], value
); 
 100 // Locate an entry in a headermap. 
 101 // Find returns NULL if not found; [] creates a new entry if needed and returns 
 102 // a reference to the value, in good STL tradition. 
 104 const char *HeaderMap::find(const char *key
, const char *defaultValue
) const 
 106     Map::const_iterator it 
= mMap
.find(CanonicalKey(key
)); 
 107     return (it 
== mMap
.end()) ? defaultValue 
: it
->second
.c_str(); 
 110 string 
&HeaderMap::operator[] (const char *key
) 
 112     return mMap
[CanonicalKey(key
)]; 
 117 // The default implementation of merge throws out the old contents and replaces 
 118 // them with the new. 
 120 void HeaderMap::merge(string key
, string 
&old
, string newValue
) 
 127 // Collect the entire contents into a single string 
 128 // Note that this is NOT exactly what was passed in; certain canonicalizations have 
 129 // been done; fields are reordered; and duplicate-header fields have been coalesced. 
 130 //@@@ size could be pre-calculated (running counter). 
 132 string 
HeaderMap::collect(const char *lineEnding
) const 
 135     for (Map::const_iterator it 
= mMap
.begin(); it 
!= mMap
.end(); it
++) 
 136         value 
+= it
->first 
+ ": " + it
->second 
+ lineEnding
; 
 140 size_t HeaderMap::collectLength(const char *lineEnding
) const 
 143     size_t sepLength 
= strlen(lineEnding
); 
 144     for (Map::const_iterator it 
= mMap
.begin(); it 
!= mMap
.end(); it
++) 
 145         size 
+= it
->first
.length() + 2 + it
->second
.length() + sepLength
; 
 150 }       // end namespace Security