Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
headers.h
Go to the documentation of this file.
1 // Copyright 2011 Google Inc.
14 
16 
17 #ifndef PAGESPEED_KERNEL_HTTP_HEADERS_H_
18 #define PAGESPEED_KERNEL_HTTP_HEADERS_H_
19 
20 #include <map>
21 #include <utility>
22 
28 
29 namespace net_instaweb {
30 
31 class MessageHandler;
32 class NameValue;
33 class StringMultiMapInsensitive;
34 class Writer;
35 
37 template<class Proto> class Headers {
38  public:
40  typedef std::pair<StringPiece, StringPiece> ValueAndAttributes;
41  typedef std::multimap<StringPiece, ValueAndAttributes> CookieMultimap;
42  typedef std::multimap<StringPiece, ValueAndAttributes>::const_iterator
43  CookieMultimapConstIter;
44 
45  Headers();
46  virtual ~Headers();
47 
48  virtual void Clear();
49 
50  int major_version() const;
51  bool has_major_version() const;
52  int minor_version() const;
53  void set_major_version(int major_version);
54  void set_minor_version(int major_version);
55 
57  int NumAttributes() const;
58  const GoogleString& Name(int i) const;
59  const GoogleString& Value(int i) const;
60  void SetValue(int i, StringPiece value);
61 
78  bool Lookup(const StringPiece& name, ConstStringStarVector* values) const;
79 
84  GoogleString LookupJoined(StringPiece name) const;
85 
89  const char* Lookup1(const StringPiece& name) const;
90 
92  bool Has(const StringPiece& name) const;
93 
95  bool HasValue(const StringPiece& name, const StringPiece& value) const;
96 
98  int NumAttributeNames() const;
99 
107  void RemoveCookie(const StringPiece& cookie_name);
108 
113  void Add(const StringPiece& name, const StringPiece& value);
114 
127  bool Remove(const StringPiece& name, const StringPiece& value);
128 
130  bool RemoveAll(const StringPiece& name);
131 
134  bool RemoveAllFromSortedArray(const StringPiece* names,
135  int names_size);
136 
140  template<class StringType>
141  static bool RemoveFromHeaders(const StringType* names,
142  int names_size,
143  protobuf::RepeatedPtrField<NameValue>* headers);
144 
147  bool RemoveAllWithPrefix(const StringPiece& prefix);
148 
151  bool RemoveIfNotIn(const Headers& keep);
152 
155  virtual void Replace(const StringPiece& name, const StringPiece& value);
156 
161  virtual void UpdateFrom(const Headers<Proto>& other);
162 
164  virtual bool WriteAsBinary(Writer* writer, MessageHandler* message_handler);
165 
167  virtual bool ReadFromBinary(const StringPiece& buf, MessageHandler* handler);
168 
170  virtual bool WriteAsHttp(Writer* writer, MessageHandler* handler) const;
171 
173  void CopyToProto(Proto* proto) const;
174 
181  static bool FindValueForName(const StringPieceVector& name_equals_value_vec,
182  StringPiece name_to_find,
183  StringPiece* optional_retval);
184 
189  static bool ExtractNameAndValue(StringPiece input, StringPiece* name,
190  StringPiece* optional_retval);
191 
192  protected:
194  void SetProto(Proto* proto);
195  void CopyProto(const Proto& proto);
196 
197  void PopulateMap() const;
198 
209  const CookieMultimap* PopulateCookieMap(StringPiece header_name) const;
210 
213  virtual void UpdateHook();
214 
217  const Proto* proto() const { return proto_.get(); }
218  Proto* mutable_proto() { return proto_.get(); }
219 
220  private:
228  void AddToMap(const StringPiece& name, const StringPiece& value) const;
229 
234  mutable scoped_ptr<StringMultiMapInsensitive> map_;
235  scoped_ptr<Proto> proto_;
236 
241  mutable scoped_ptr<CookieMultimap> cookies_;
242 
243 
244 };
245 
246 }
247 
248 #endif
bool HasValue(const StringPiece &name, const StringPiece &value) const
Is value one of the values in Lookup(name)?
bool RemoveAllFromSortedArray(const StringPiece *names, int names_size)
virtual void UpdateHook()
bool RemoveAllWithPrefix(const StringPiece &prefix)
bool RemoveAll(const StringPiece &name)
Removes all headers by name. Return true if anything was removed.
const Proto * proto() const
Definition: headers.h:217
void PopulateMap() const
const is a lie, mutates map_.
static bool RemoveFromHeaders(const StringType *names, int names_size, protobuf::RepeatedPtrField< NameValue > *headers)
bool Has(const StringPiece &name) const
Does there exist a header with given name.
void RemoveCookie(const StringPiece &cookie_name)
void Add(const StringPiece &name, const StringPiece &value)
bool RemoveIfNotIn(const Headers &keep)
std::pair< StringPiece, StringPiece > ValueAndAttributes
typedef's for manipulating the cookie multimap.
Definition: headers.h:40
static bool ExtractNameAndValue(StringPiece input, StringPiece *name, StringPiece *optional_retval)
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
bool Lookup(const StringPiece &name, ConstStringStarVector *values) const
void SetProto(Proto *proto)
You need to know what you're doing to use these, so for subclasses only.
const CookieMultimap * PopulateCookieMap(StringPiece header_name) const
GoogleString LookupJoined(StringPiece name) const
int NumAttributeNames() const
NumAttributeNames is also const but not thread-safe.
Interface for writing bytes to an output stream.
Definition: writer.h:29
const char * Lookup1(const StringPiece &name) const
virtual bool WriteAsBinary(Writer *writer, MessageHandler *message_handler)
Serialize HTTP header to a binary stream.
static bool FindValueForName(const StringPieceVector &name_equals_value_vec, StringPiece name_to_find, StringPiece *optional_retval)
Definition: message_handler.h:39
virtual bool WriteAsHttp(Writer *writer, MessageHandler *handler) const
Serialize HTTP headers in HTTP format so it can be re-parsed.
Read/write API for HTTP headers (shared base class)
Definition: headers.h:37
virtual void UpdateFrom(const Headers< Proto > &other)
void CopyToProto(Proto *proto) const
Copy protobuf representation to "proto".
virtual void Replace(const StringPiece &name, const StringPiece &value)
bool Remove(const StringPiece &name, const StringPiece &value)
int NumAttributes() const
Raw access for random access to attribute name/value pairs.
virtual bool ReadFromBinary(const StringPiece &buf, MessageHandler *handler)
Read HTTP header from a binary string.