proxygen
PushRequestHandler.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the BSD-style license found in the
6  * LICENSE file in the root directory of this source tree. An additional grant
7  * of patent rights can be found in the PATENTS file in the same directory.
8  *
9  */
10 #include "PushRequestHandler.h"
11 
15 #include <folly/FileUtil.h>
16 #include "PushStats.h"
17 
18 using namespace proxygen;
19 
20 namespace PushService {
21 
22 const std::string kPushFileName("proxygen/httpserver/samples/push/pusheen.txt");
24 
25 // Create a large body so we can give time for the push request to be made
28  while(data.size() < 1000*1000) {
29  data += gPushBody;
30  }
31  return data;
32 }
33 
34 std::string generateUrl(const HTTPMessage& message, const char* path) {
36  message.isSecure() ? "https" : "http",
37  message.getHeaders().getSingleOrEmpty(HTTP_HEADER_HOST), path, "", "");
38 }
39 
40 PushRequestHandler::PushRequestHandler(PushStats* stats) : stats_(stats) {
41  if (gPushBody.empty()) {
42  CHECK(folly::readFile(kPushFileName.c_str(), gPushBody))
43  << "Failed to read push file=" << kPushFileName;
44  }
45 }
46 
48  std::unique_ptr<HTTPMessage> headers) noexcept {
50  if (!headers->getHeaders().getSingleOrEmpty("X-PushIt").empty()) {
52  if (!downstreamPush_) {
53  // can't push
54  return;
55  }
56 
57  if(headers->getPath() == "/requestLargePush") {
58  LOG(INFO) << "sending large push ";
59 
61  .promise(generateUrl(*headers, "/largePush"),
62  headers->getHeaders().getSingleOrEmpty(HTTP_HEADER_HOST))
63  .send();
64 
66  .status(200, "OK")
68  .sendWithEOM();
69  } else {
70  LOG(INFO) << "sending small push ";
71 
73  .promise(generateUrl(*headers, "/pusheen"),
74  headers->getHeaders().getSingleOrEmpty(HTTP_HEADER_HOST))
75  .send();
76 
78  .status(200, "OK")
79  .body(gPushBody)
80  .sendWithEOM();
81  }
82  }
83 }
84 
85 void PushRequestHandler::onBody(std::unique_ptr<folly::IOBuf> body) noexcept {
86  if (body_) {
87  body_->prependChain(std::move(body));
88  } else {
89  body_ = std::move(body);
90  }
91 }
92 
95  .status(200, "OK")
96  .header("Request-Number",
97  folly::to<std::string>(stats_->getRequestCount()))
99  .sendWithEOM();
100 }
101 
103  // handler doesn't support upgrades
104 }
105 
107  delete this;
108 }
109 
111  delete this;
112 }
113 
114 }
Definition: test.c:42
bool readFile(int fd, Container &out, size_t num_bytes=std::numeric_limits< size_t >::max())
Definition: FileUtil.h:125
ResponseBuilder & status(uint16_t code, const std::string &message)
std::string generateUrl(const HTTPMessage &message, const char *path)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
virtual void recordRequest()
Definition: PushStats.h:29
proxygen::ResponseHandler * downstreamPush_
ResponseBuilder & body(std::unique_ptr< folly::IOBuf > bodyIn)
void onRequest(std::unique_ptr< proxygen::HTTPMessage > headers) noexceptoverride
requires E e noexcept(noexcept(s.error(std::move(e))))
virtual uint64_t getRequestCount()
Definition: PushStats.h:33
std::unique_ptr< folly::IOBuf > body_
std::string gPushBody
void onError(proxygen::ProxygenError err) noexceptoverride
void onBody(std::unique_ptr< folly::IOBuf > body) noexceptoverride
ResponseBuilder & header(const std::string &headerIn, const T &value)
ResponseBuilder & promise(const std::string &url, const std::string &host)
ssize_t send(NetworkSocket s, const void *buf, size_t len, int flags)
Definition: NetOps.cpp:319
HTTPHeaders & getHeaders()
Definition: HTTPMessage.h:273
static std::string createUrl(const folly::StringPiece scheme, const folly::StringPiece authority, const folly::StringPiece path, const folly::StringPiece query, const folly::StringPiece fragment)
const std::string & getSingleOrEmpty(const T &nameOrCode) const
Definition: HTTPHeaders.h:420
const std::string kPushFileName("proxygen/httpserver/samples/push/pusheen.txt")
std::string createLargeBody()
void requestComplete() noexceptoverride
const char * string
Definition: Conv.cpp:212
bool isSecure() const
Definition: HTTPMessage.h:535
ResponseHandler * downstream_
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43
virtual ResponseHandler * newPushedResponse(PushHandler *pushHandler) noexcept=0
void onUpgrade(proxygen::UpgradeProtocol proto) noexceptoverride