proxygen
RFC1867.h
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 #pragma once
11 
12 #include <folly/Conv.h>
14 
15 namespace proxygen {
16 
25  public:
26 
27  class Callback {
28  public:
29  virtual ~Callback() {}
30  // return < 0 to skip remainder of field callbacks?
31  virtual int onFieldStart(const std::string& name,
33  std::unique_ptr<HTTPMessage> msg,
34  uint64_t postBytesProcessed) = 0;
35  virtual int onFieldData(std::unique_ptr<folly::IOBuf>,
36  uint64_t postBytesProcessed) = 0;
40  virtual void onFieldEnd(bool endedOnBoundary,
41  uint64_t postBytesProcessed) = 0;
42  virtual void onError() = 0;
43  };
44 
45  // boundary is the parameter to Content-Type, eg:
46  //
47  // Content-type: multipart/form-data, boundary=AaB03x
48  explicit RFC1867Codec(const std::string& boundary) {
49  CHECK(!boundary.empty());
50  boundary_ = folly::to<std::string>("\n--", boundary);
52  }
53 
54  void setCallback(Callback* callback) {
55  callback_ = callback;
56  }
57 
58  // Pass the next piece of input data. Returns unparsed data that requires
59  // more input to continue
60  std::unique_ptr<folly::IOBuf> onIngress(std::unique_ptr<folly::IOBuf> data);
61 
62  // The end of input has been seen. Validate the parser state and reset
63  // for more parsing.
64  void onIngressEOM();
65 
67  return bytesProcessed_;
68  }
69 
70  private:
71  enum class ParserState {
72  START,
73  HEADERS_START,
74  HEADERS,
75  FIELD_DATA, // part, or field, not only file
76  DONE,
77  ERROR
78  };
79 
80  // HTTPCodec::Callback
82  HTTPMessage* /*msg*/) override {}
84  std::unique_ptr<HTTPMessage> msg) override;
85  void onBody(HTTPCodec::StreamID /*stream*/,
86  std::unique_ptr<folly::IOBuf> /*chain*/,
87  uint16_t /*padding*/) override {
88  parseError_ = true;
90  }
92  std::unique_ptr<HTTPHeaders> /*trailers*/) override {
93  parseError_ = true;
95  }
97  bool /*upgrade*/) override {
99  }
100 
101  void onError(HTTPCodec::StreamID /*stream*/,
102  const HTTPException& /*error*/,
103  bool /*newTxn*/) override {
104  parseError_ = true;
106  }
107 
108  folly::IOBufQueue readToBoundary(bool& foundBoundary);
109 
111  Callback* callback_{nullptr};
117  std::unique_ptr<folly::IOBuf> pendingCR_;
119  bool parseError_{false};
120 };
121 
122 }
Callback * callback_
Definition: RFC1867.h:111
folly::IOBufQueue readToBoundary(bool &foundBoundary)
Definition: RFC1867.cpp:225
void onMessageComplete(HTTPCodec::StreamID, bool) override
Definition: RFC1867.h:96
void onTrailersComplete(HTTPCodec::StreamID, std::unique_ptr< HTTPHeaders >) override
Definition: RFC1867.h:91
void onHeadersComplete(HTTPCodec::StreamID stream, std::unique_ptr< HTTPMessage > msg) override
Definition: RFC1867.cpp:178
void onMessageBegin(HTTPCodec::StreamID, HTTPMessage *) override
Definition: RFC1867.h:81
std::string boundary_
Definition: RFC1867.h:110
folly::IOBufQueue value_
Definition: RFC1867.h:116
void onBody(HTTPCodec::StreamID, std::unique_ptr< folly::IOBuf >, uint16_t) override
Definition: RFC1867.h:85
const char * name
Definition: http_parser.c:437
virtual int onFieldData(std::unique_ptr< folly::IOBuf >, uint64_t postBytesProcessed)=0
virtual void onFieldEnd(bool endedOnBoundary, uint64_t postBytesProcessed)=0
static Options cacheChainLength()
Definition: IOBufQueue.h:83
void onError(HTTPCodec::StreamID, const HTTPException &, bool) override
Definition: RFC1867.h:101
folly::IOBufQueue input_
Definition: RFC1867.h:115
void setCallback(Callback *callback) override
Definition: HTTP1xCodec.h:40
RFC1867Codec(const std::string &boundary)
Definition: RFC1867.h:48
virtual int onFieldStart(const std::string &name, folly::Optional< std::string > filename, std::unique_ptr< HTTPMessage > msg, uint64_t postBytesProcessed)=0
std::unique_ptr< folly::IOBuf > pendingCR_
Definition: RFC1867.h:117
HTTP1xCodec headerParser_
Definition: RFC1867.h:113
std::string field_
Definition: RFC1867.h:114
void setCallback(Callback *callback)
Definition: RFC1867.h:54
const char * string
Definition: Conv.cpp:212
std::unique_ptr< folly::IOBuf > onIngress(std::unique_ptr< folly::IOBuf > data)
Definition: RFC1867.cpp:61
void setParserPaused(bool paused) override
ParserState state_
Definition: RFC1867.h:112
uint64_t StreamID
Definition: HTTPCodec.h:49
uint64_t getBytesProcessed() const
Definition: RFC1867.h:66
uint64_t bytesProcessed_
Definition: RFC1867.h:118
static constexpr uint64_t data[1]
Definition: Fingerprint.cpp:43