proxygen
proxygen::ZlibServerFilter Class Reference

#include <ZlibServerFilter.h>

Inheritance diagram for proxygen::ZlibServerFilter:
proxygen::Filter proxygen::RequestHandler proxygen::ResponseHandler

Public Member Functions

 ZlibServerFilter (RequestHandler *downstream, int32_t compressionLevel, uint32_t minimumCompressionSize, const std::shared_ptr< std::set< std::string >> compressibleContentTypes)
 
void sendHeaders (HTTPMessage &msg) noexceptoverride
 
void sendChunkHeader (size_t len) noexceptoverride
 
void sendBody (std::unique_ptr< folly::IOBuf > body) noexceptoverride
 
void sendEOM () noexceptoverride
 
- Public Member Functions inherited from proxygen::Filter
 Filter (RequestHandler *upstream)
 
void setResponseHandler (ResponseHandler *handler) noexceptoverride
 
void onRequest (std::unique_ptr< HTTPMessage > headers) noexceptoverride
 
void onBody (std::unique_ptr< folly::IOBuf > body) noexceptoverride
 
void onUpgrade (UpgradeProtocol protocol) noexceptoverride
 
void onEOM () noexceptoverride
 
void requestComplete () noexceptoverride
 
void onError (ProxygenError err) noexceptoverride
 
void onEgressPaused () noexceptoverride
 
void onEgressResumed () noexceptoverride
 
bool canHandleExpect () noexceptoverride
 
ExMessageHandlergetExHandler () noexceptoverride
 
void sendHeaders (HTTPMessage &msg) noexceptoverride
 
void sendChunkHeader (size_t len) noexceptoverride
 
void sendBody (std::unique_ptr< folly::IOBuf > body) noexceptoverride
 
void sendChunkTerminator () noexceptoverride
 
void sendEOM () noexceptoverride
 
void sendAbort () noexceptoverride
 
void refreshTimeout () noexceptoverride
 
void pauseIngress () noexceptoverride
 
void resumeIngress () noexceptoverride
 
ResponseHandlernewPushedResponse (PushHandler *handler) noexceptoverride
 
const wangle::TransportInfogetSetupTransportInfo () const noexceptoverride
 
void getCurrentTransportInfo (wangle::TransportInfo *tinfo) const override
 
- Public Member Functions inherited from proxygen::RequestHandler
virtual ResponseHandlergetDownstream () noexcept
 
virtual ~RequestHandler ()
 
- Public Member Functions inherited from proxygen::ResponseHandler
 ResponseHandler (RequestHandler *upstream)
 
virtual ~ResponseHandler ()
 
virtual ResponseHandlernewExMessage (ExMessageHandler *, bool=false) noexcept
 
HTTPTransactiongetTransaction () const noexcept
 

Protected Member Functions

void fail ()
 
bool isMinimumCompressibleSize (const HTTPMessage &msg) const noexcept
 
bool isCompressibleContentType (const HTTPMessage &msg) const noexcept
 

Protected Attributes

std::unique_ptr< HTTPMessageresponseMessage_
 
std::unique_ptr< ZlibStreamCompressorcompressor_ {nullptr}
 
int32_t compressionLevel_ {4}
 
uint32_t minimumCompressionSize_ {1000}
 
const std::shared_ptr< std::set< std::string > > compressibleContentTypes_
 
bool header_ {false}
 
bool chunked_ {false}
 
bool compress_ {false}
 
- Protected Attributes inherited from proxygen::RequestHandler
ResponseHandlerdownstream_ {nullptr}
 
- Protected Attributes inherited from proxygen::ResponseHandler
RequestHandlerupstream_ {nullptr}
 
HTTPTransactiontxn_ {nullptr}
 

Detailed Description

A Server filter to perform GZip compression. If there are any errors it will fall back to sending uncompressed responses.

Definition at line 25 of file ZlibServerFilter.h.

Constructor & Destructor Documentation

proxygen::ZlibServerFilter::ZlibServerFilter ( RequestHandler downstream,
int32_t  compressionLevel,
uint32_t  minimumCompressionSize,
const std::shared_ptr< std::set< std::string >>  compressibleContentTypes 
)
inlineexplicit

Definition at line 27 of file ZlibServerFilter.h.

Referenced by proxygen::ZlibServerFilterFactory::onRequest().

32  : Filter(downstream),
33  compressionLevel_(compressionLevel),
34  minimumCompressionSize_(minimumCompressionSize),
35  compressibleContentTypes_(compressibleContentTypes) {}
const std::shared_ptr< std::set< std::string > > compressibleContentTypes_
Filter(RequestHandler *upstream)
Definition: Filters.h:31

Member Function Documentation

void proxygen::ZlibServerFilter::fail ( )
inlineprotected
bool proxygen::ZlibServerFilter::isCompressibleContentType ( const HTTPMessage msg) const
inlineprotectednoexcept

Definition at line 165 of file ZlibServerFilter.h.

References compressibleContentTypes_, proxygen::HTTP_HEADER_CONTENT_TYPE, and folly::toLowerAscii().

Referenced by sendHeaders().

165  {
166 
167  auto responseContentType =
168  msg.getHeaders().getSingleOrEmpty(HTTP_HEADER_CONTENT_TYPE);
169  folly::toLowerAscii(responseContentType);
170 
171  // Handle text/html; encoding=utf-8 case
172  auto parameter_idx = responseContentType.find(';');
173  if (parameter_idx != std::string::npos) {
174  responseContentType = responseContentType.substr(0, parameter_idx);
175  }
176 
177  auto idx = compressibleContentTypes_->find(responseContentType);
178 
179  if (idx != compressibleContentTypes_->end()) {
180  return true;
181  }
182 
183  return false;
184  }
void toLowerAscii(char *str, size_t length)
Definition: String.cpp:601
const std::shared_ptr< std::set< std::string > > compressibleContentTypes_
bool proxygen::ZlibServerFilter::isMinimumCompressibleSize ( const HTTPMessage msg) const
inlineprotectednoexcept

Definition at line 152 of file ZlibServerFilter.h.

References proxygen::HTTP_HEADER_CONTENT_LENGTH, minimumCompressionSize_, and uint32_t.

Referenced by sendHeaders().

152  {
153  auto contentLengthHeader =
154  msg.getHeaders().getSingleOrEmpty(HTTP_HEADER_CONTENT_LENGTH);
155 
156  uint32_t contentLength = 0;
157  if (!contentLengthHeader.empty()) {
158  contentLength = folly::to<uint32_t>(contentLengthHeader);
159  }
160 
161  return contentLength >= minimumCompressionSize_;
162  }
void proxygen::ZlibServerFilter::sendBody ( std::unique_ptr< folly::IOBuf body)
inlineoverridevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 86 of file ZlibServerFilter.h.

References chunked_, compress_, compressor_, fail(), header_, proxygen::HTTP_HEADER_CONTENT_LENGTH, folly::gen::move, responseMessage_, proxygen::Filter::sendBody(), proxygen::Filter::sendChunkHeader(), and proxygen::Filter::sendHeaders().

86  {
87  // If not compressing, pass the body through
88  if (!compress_) {
89  DCHECK(header_ == true);
91  return;
92  }
93 
94  CHECK(compressor_ && !compressor_->hasError());
95 
96  // If it's chunked, never write the trailer, it will be written on EOM
97  auto compressed = compressor_->compress(body.get(), !chunked_);
98  if (compressor_->hasError()) {
99  return fail();
100  }
101 
102  auto compressedBodyLength = compressed->computeChainDataLength();
103 
104  if (chunked_) {
105  // Send on the swallowed chunk header.
106  Filter::sendChunkHeader(compressedBodyLength);
107  } else {
108  //Send the content length on compressed, non-chunked messages
109  DCHECK(header_ == false);
110  DCHECK(compress_ == true);
111  auto& headers = responseMessage_->getHeaders();
112  headers.set(HTTP_HEADER_CONTENT_LENGTH,
113  folly::to<std::string>(compressedBodyLength));
114 
116  header_ = true;
117  }
118 
119  Filter::sendBody(std::move(compressed));
120  }
void sendChunkHeader(size_t len) noexceptoverride
Definition: Filters.h:92
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< ZlibStreamCompressor > compressor_
std::unique_ptr< HTTPMessage > responseMessage_
void sendHeaders(HTTPMessage &msg) noexceptoverride
Definition: Filters.h:88
void sendBody(std::unique_ptr< folly::IOBuf > body) noexceptoverride
Definition: Filters.h:96
void proxygen::ZlibServerFilter::sendChunkHeader ( size_t  len)
inlineoverridevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 71 of file ZlibServerFilter.h.

References compress_, header_, and proxygen::Filter::sendChunkHeader().

71  {
72  // The headers should have always been sent since the message is chunked
73  DCHECK_EQ(header_, true) << "Headers should have already been sent.";
74 
75  // If not compressing, pass downstream, otherwise "swallow" it
76  // to send after compressing the body.
77  if (!compress_) {
79  }
80 
81  // Return without sending the chunk header.
82  return;
83  }
void sendChunkHeader(size_t len) noexceptoverride
Definition: Filters.h:92
void proxygen::ZlibServerFilter::sendEOM ( )
inlineoverridevirtualnoexcept

Implements proxygen::ResponseHandler.

Definition at line 122 of file ZlibServerFilter.h.

References chunked_, compress_, compressor_, folly::IOBuf::copyBuffer(), fail(), folly::gen::move, proxygen::Filter::sendBody(), proxygen::Filter::sendChunkHeader(), proxygen::Filter::sendChunkTerminator(), and proxygen::Filter::sendEOM().

122  {
123 
124  // Need to send the gzip trailer for compressed chunked messages
125  if (compress_ && chunked_) {
126 
127  auto emptyBuffer = folly::IOBuf::copyBuffer("");
128  CHECK(compressor_ && !compressor_->hasError());
129  auto compressed = compressor_->compress(emptyBuffer.get(), true);
130 
131  if (compressor_->hasError()) {
132  fail();
133  return;
134  }
135 
136  // "Inject" a chunk with the gzip trailer.
137  Filter::sendChunkHeader(compressed->computeChainDataLength());
138  Filter::sendBody(std::move(compressed));
140  }
141 
142  Filter::sendEOM();
143  }
void sendChunkHeader(size_t len) noexceptoverride
Definition: Filters.h:92
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
std::unique_ptr< ZlibStreamCompressor > compressor_
void sendChunkTerminator() noexceptoverride
Definition: Filters.h:100
void sendEOM() noexceptoverride
Definition: Filters.h:104
static std::unique_ptr< IOBuf > copyBuffer(const void *buf, std::size_t size, std::size_t headroom=0, std::size_t minTailroom=0)
Definition: IOBuf.h:1587
void sendBody(std::unique_ptr< folly::IOBuf > body) noexceptoverride
Definition: Filters.h:96
void proxygen::ZlibServerFilter::sendHeaders ( HTTPMessage msg)
inlineoverridevirtualnoexcept

NOTE: We take response message as non-const reference, to allow filters between your handler and client to be able to modify response if they want to.

eg. a compression filter might want to change the content-encoding

Implements proxygen::ResponseHandler.

Definition at line 37 of file ZlibServerFilter.h.

References chunked_, compress_, compressionLevel_, compressor_, fail(), proxygen::GZIP, header_, proxygen::HTTP_HEADER_CONTENT_ENCODING, isCompressibleContentType(), isMinimumCompressibleSize(), responseMessage_, and proxygen::Filter::sendHeaders().

37  {
38  DCHECK(compressor_ == nullptr);
39  DCHECK(header_ == false);
40 
41  chunked_ = msg.getIsChunked();
42 
43  // Make final determination of whether to compress
46 
47  // Add the gzip header
48  if (compress_) {
49  auto& headers = msg.getHeaders();
50  headers.set(HTTP_HEADER_CONTENT_ENCODING, "gzip");
51  }
52 
53  // Initialize compressor
54  compressor_ = std::make_unique<ZlibStreamCompressor>(
56  if (!compressor_ || compressor_->hasError()) {
57  fail();
58  return;
59  }
60 
61  // If it's chunked or not being compressed then the headers can be sent
62  // if it's compressed and one body, then need to calculate content length.
63  if (chunked_ || !compress_) {
65  header_ = true;
66  } else {
67  responseMessage_ = std::make_unique<HTTPMessage>(msg);
68  }
69  }
std::unique_ptr< ZlibStreamCompressor > compressor_
bool isCompressibleContentType(const HTTPMessage &msg) const noexcept
std::unique_ptr< HTTPMessage > responseMessage_
void sendHeaders(HTTPMessage &msg) noexceptoverride
Definition: Filters.h:88
bool isMinimumCompressibleSize(const HTTPMessage &msg) const noexcept

Member Data Documentation

bool proxygen::ZlibServerFilter::chunked_ {false}
protected

Definition at line 192 of file ZlibServerFilter.h.

Referenced by sendBody(), sendEOM(), and sendHeaders().

bool proxygen::ZlibServerFilter::compress_ {false}
protected

Definition at line 193 of file ZlibServerFilter.h.

Referenced by sendBody(), sendChunkHeader(), sendEOM(), and sendHeaders().

const std::shared_ptr<std::set<std::string> > proxygen::ZlibServerFilter::compressibleContentTypes_
protected
int32_t proxygen::ZlibServerFilter::compressionLevel_ {4}
protected
std::unique_ptr<ZlibStreamCompressor> proxygen::ZlibServerFilter::compressor_ {nullptr}
protected

Definition at line 187 of file ZlibServerFilter.h.

Referenced by sendBody(), sendEOM(), and sendHeaders().

bool proxygen::ZlibServerFilter::header_ {false}
protected

Definition at line 191 of file ZlibServerFilter.h.

Referenced by sendBody(), sendChunkHeader(), and sendHeaders().

uint32_t proxygen::ZlibServerFilter::minimumCompressionSize_ {1000}
protected
std::unique_ptr<HTTPMessage> proxygen::ZlibServerFilter::responseMessage_
protected

Definition at line 186 of file ZlibServerFilter.h.

Referenced by sendBody(), and sendHeaders().


The documentation for this class was generated from the following file: