Page Speed Optimization Libraries  1.11.33.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
http_value.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_
20 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_
21 
22 #include <cstddef>
23 
24 #include "pagespeed/kernel/base/basictypes.h"
25 #include "pagespeed/kernel/base/shared_string.h"
26 #include "pagespeed/kernel/base/string.h"
27 #include "pagespeed/kernel/base/string_util.h"
28 #include "pagespeed/kernel/base/writer.h"
29 
30 namespace net_instaweb {
31 
32 class ResponseHeaders;
33 class MessageHandler;
34 
38 class HTTPValue : public Writer {
39  public:
40  HTTPValue() : contents_size_(0) {}
41 
43  void Clear();
44 
46  bool Empty() const { return storage_.empty(); }
47 
57  void SetHeaders(ResponseHeaders* headers);
58 
63  virtual bool Write(const StringPiece& str, MessageHandler* handler);
64  virtual bool Flush(MessageHandler* handler);
65 
67  bool ExtractHeaders(ResponseHeaders* headers, MessageHandler* handler) const;
68 
72  bool ExtractContents(StringPiece* str) const;
73 
75  bool unique() const { return storage_.unique(); }
76 
82  bool Link(SharedString* src, ResponseHeaders* headers,
83  MessageHandler* handler);
84 
87  void Link(HTTPValue* src) {
88  if (src != this) {
89  storage_ = src->storage_;
90  contents_size_ = src->contents_size();
91  }
92  }
93 
95  SharedString* share() { return &storage_; }
96 
97  size_t size() const { return storage_.size(); }
98  int64 contents_size() { return contents_size_; }
99 
102  static bool Decode(StringPiece encoded_value, GoogleString* http_string,
103  MessageHandler* handler);
105  static bool Encode(StringPiece http_string, GoogleString* encoded_value,
106  MessageHandler* handler);
107 
108  private:
109  friend class HTTPValueTest;
110 
112  char type_identifier() const { return *storage_.data(); }
113 
114  unsigned int SizeOfFirstChunk() const;
115  void SetSizeOfFirstChunk(unsigned int size);
116  int64 ComputeContentsSize() const;
117 
120  void CopyOnWrite();
121 
122  SharedString storage_;
124  int64 contents_size_;
125 
126 
127 };
128 
129 }
130 
131 #endif
static bool Decode(StringPiece encoded_value, GoogleString *http_string, MessageHandler *handler)
Definition: http_value.h:38
bool Link(SharedString *src, ResponseHeaders *headers, MessageHandler *handler)
static bool Encode(StringPiece http_string, GoogleString *encoded_value, MessageHandler *handler)
Convert from raw HTTP stream to HTTPValue format.
bool Empty() const
Is this HTTPValue empty.
Definition: http_value.h:46
SharedString * share()
Access the shared string, for insertion into a cache via Put.
Definition: http_value.h:95
bool ExtractContents(StringPiece *str) const
void Clear()
Clears the value (both headers and content)
bool unique() const
Tests whether this reference is the only active one to the string object.
Definition: http_value.h:75
void SetHeaders(ResponseHeaders *headers)
void Link(HTTPValue *src)
Definition: http_value.h:87
bool ExtractHeaders(ResponseHeaders *headers, MessageHandler *handler) const
Retrieves the headers, returning false if empty.
virtual bool Write(const StringPiece &str, MessageHandler *handler)