00001 /* 00002 * Copyright 2010 Google Inc. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http:///www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00018 00019 #ifndef NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_ 00020 #define NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_ 00021 00022 #include <cstddef> 00023 #include "net/instaweb/util/public/basictypes.h" 00024 #include "net/instaweb/util/public/shared_string.h" 00025 #include "net/instaweb/util/public/string.h" 00026 #include "net/instaweb/util/public/string_util.h" 00027 #include "net/instaweb/util/public/writer.h" 00028 00029 namespace net_instaweb { 00030 00031 class ResponseHeaders; 00032 class MessageHandler; 00033 00037 class HTTPValue : public Writer { 00038 public: 00039 HTTPValue() : contents_size_(0) {} 00040 00042 void Clear(); 00043 00045 bool Empty() const { return storage_->empty(); } 00046 00055 void SetHeaders(ResponseHeaders* headers); 00056 00061 virtual bool Write(const StringPiece& str, MessageHandler* handler); 00062 virtual bool Flush(MessageHandler* handler); 00063 00065 bool ExtractHeaders(ResponseHeaders* headers, MessageHandler* handler) const; 00066 00070 bool ExtractContents(StringPiece* str) const; 00071 00073 bool unique() const { return storage_.unique(); } 00074 00080 bool Link(SharedString* src, ResponseHeaders* headers, 00081 MessageHandler* handler); 00082 00085 void Link(HTTPValue* src) { 00086 if (src != this) { 00087 storage_ = src->storage_; 00088 contents_size_ = src->contents_size(); 00089 } 00090 } 00091 00093 SharedString* share() { return &storage_; } 00094 00095 size_t size() const { return storage_->size(); } 00096 int64 contents_size() { return contents_size_; } 00097 00098 private: 00099 friend class HTTPValueTest; 00100 00101 char type_identifier() const { return (*storage_.get())[0]; } 00102 unsigned int SizeOfFirstChunk() const; 00103 void SetSizeOfFirstChunk(unsigned int size); 00104 int64 ComputeContentsSize() const; 00105 00108 void CopyOnWrite(); 00109 00110 SharedString storage_; 00112 int64 contents_size_; 00113 00114 DISALLOW_COPY_AND_ASSIGN(HTTPValue); 00115 }; 00116 00117 } 00118 00119 #endif ///< NET_INSTAWEB_HTTP_PUBLIC_HTTP_VALUE_H_