2
* Copyright 2010 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: jmarantz@google.com (Joshua Marantz)
19
#ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_
20
#define NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_
22
#include <cstddef> // for size_t
23
#include "net/instaweb/util/public/basictypes.h"
24
#include "net/instaweb/util/public/shared_string.h"
25
#include "net/instaweb/util/public/string_util.h"
26
#include "net/instaweb/util/public/writer.h"
28
namespace net_instaweb {
30
class ResponseHeaders;
33
// Provides shared, ref-counted, copy-on-write storage for HTTP
34
// contents, to aid sharing between active fetches and filters, and
35
// the cache, which from which data may be evicted at any time.
36
class HTTPValue : public Writer {
38
HTTPValue() : contents_size_(0) {}
40
// Clears the value (both headers and content)
43
// Is this HTTPValue empty
44
bool Empty() const { return storage_.empty(); }
46
// Sets the HTTP headers for this value. This method may only
47
// be called once and must be called before or after all of the
48
// contents are set (using the streaming interface Write).
50
// If Clear() is called, then SetHeaders() can be called once again.
52
// Does NOT take ownership of headers.
53
// A non-const pointer is required for the response headers so that
54
// the cache fields can be updated if necessary.
55
void SetHeaders(ResponseHeaders* headers);
57
// Writes contents into the HTTPValue object. Write can be called
58
// multiple times to append more data, and can be called before
59
// or after SetHeaders. However, SetHeaders cannot be interleaved
60
// in between calls to Write.
61
virtual bool Write(const StringPiece& str, MessageHandler* handler);
62
virtual bool Flush(MessageHandler* handler);
64
// Retrieves the headers, returning false if empty.
65
bool ExtractHeaders(ResponseHeaders* headers, MessageHandler* handler) const;
67
// Retrieves the contents, returning false if empty. Note that the
68
// contents are only guaranteed valid as long as the HTTPValue
69
// object is in scope.
70
bool ExtractContents(StringPiece* str) const;
72
// Tests whether this reference is the only active one to the string object.
73
bool unique() const { return storage_.unique(); }
75
// Assigns the storage of an HTTPValue based on the provided storage. This
76
// can be used for a cache Get. Returns false if the string is not
79
// Extracts the headers into the provided ResponseHeaders buffer.
80
bool Link(SharedString* src, ResponseHeaders* headers,
81
MessageHandler* handler);
83
// Links two HTTPValues together, using the contents of 'src' and discarding
84
// the contents of this.
85
void Link(HTTPValue* src) {
87
storage_ = src->storage_; // SharedString links via assignment.
88
contents_size_ = src->contents_size();
92
// Access the shared string, for insertion into a cache via Put.
93
SharedString* share() { return &storage_; }
95
size_t size() const { return storage_.size(); }
96
int64 contents_size() { return contents_size_; }
99
friend class HTTPValueTest;
101
// Must be called with storage_ non-empty.
102
char type_identifier() const { return *storage_.data(); }
104
unsigned int SizeOfFirstChunk() const;
105
void SetSizeOfFirstChunk(unsigned int size);
106
int64 ComputeContentsSize() const;
108
// Disconnects this HTTPValue from other HTTPValues that may share the
109
// underlying storage, allowing a new buffer.
112
SharedString storage_;
113
// Member variable to keep the size of body in storage.
114
int64 contents_size_;
116
DISALLOW_COPY_AND_ASSIGN(HTTPValue);
119
} // namespace net_instaweb
121
#endif // NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_