proxygen
HTTPCodec.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/Portability.h>
13 #include <folly/io/IOBufQueue.h>
22 
23 namespace proxygen {
24 
25 class HTTPHeaders;
26 class HTTPMessage;
27 class HTTPTransactionHandler;
28 class HTTPErrorPage;
29 
36 class HTTPCodec {
37  public:
38 
49  using StreamID = uint64_t;
50 
52 
54 
55  static const StreamID MAX_STREAM_ID = 1u << 31;
56 
57  struct ExAttributes {
59  ExAttributes(StreamID controlStreamId, bool isUnidirectional)
60  : controlStream(controlStreamId), unidirectional(isUnidirectional) {}
61 
64  };
65 
67 
68  class PriorityQueue {
69  public:
70  virtual ~PriorityQueue() {}
71 
72  virtual void addPriorityNode(StreamID id, StreamID parent) = 0;
73  };
74 
78  class Callback {
79  public:
85  virtual void onMessageBegin(StreamID stream, HTTPMessage* msg) = 0;
86 
95  virtual void onPushMessageBegin(StreamID /* stream */,
96  StreamID /* assocStream */,
97  HTTPMessage* /* msg */) {}
98 
107  virtual void onExMessageBegin(StreamID /* stream */,
108  StreamID /* controlStream */,
109  bool /* unidirectional */,
110  HTTPMessage* /* msg */) {}
111 
118  virtual void onHeadersComplete(StreamID stream,
119  std::unique_ptr<HTTPMessage> msg) = 0;
120 
129  virtual void onBody(StreamID stream,
130  std::unique_ptr<folly::IOBuf> chain,
131  uint16_t padding) = 0;
132 
148  virtual void onChunkHeader(StreamID /* stream */, size_t /* length */) {}
149 
156  virtual void onChunkComplete(StreamID /* stream */) {}
157 
164  virtual void onTrailersComplete(StreamID stream,
165  std::unique_ptr<HTTPHeaders> trailers) = 0;
166 
173  virtual void onMessageComplete(StreamID stream, bool upgrade) = 0;
174 
181  virtual void onError(StreamID stream,
182  const HTTPException& error,
183  bool newTxn = false) = 0;
184 
192  virtual void onAbort(
193  StreamID /* stream */,
194  ErrorCode /* code */) {}
195 
206  virtual void onFrameHeader(
207  StreamID /* stream_id */,
208  uint8_t /* flags */,
209  uint64_t /* length */,
210  uint8_t /* type */,
211  uint16_t /* version */ = 0) {}
212 
220  virtual void onGoaway(
221  uint64_t /* lastGoodStreamID */,
222  ErrorCode /* code */,
223  std::unique_ptr<folly::IOBuf> /* debugData */ = nullptr) {}
224 
230  virtual void onPingRequest(uint64_t /* uniqueID */) {}
231 
237  virtual void onPingReply(uint64_t /* uniqueID */) {}
238 
243  virtual void onWindowUpdate(StreamID /* stream */, uint32_t /* amount */) {}
244 
251  virtual void onSettings(const SettingsList& /* settings */) {}
252 
257  virtual void onSettingsAck() {}
258 
263  virtual void onPriority(
264  StreamID /* stream */,
265  const HTTPMessage::HTTPPriority& /* pri */) {}
266 
272  StreamID /* stream */,
273  CodecProtocol /* protocol */,
274  const std::string& /* protocolString */,
275  HTTPMessage& /* msg */) {
276  return false;
277  }
278 
283  virtual void onGenerateFrameHeader(
284  StreamID /* stream_id */,
285  uint8_t /* type */,
286  uint64_t /* length */,
287  uint16_t /* version */ = 0) {}
288 
297  virtual void onCertificateRequest(
298  uint16_t /* requestId */,
299  std::unique_ptr<folly::IOBuf> /* authRequest */) {
300  }
301 
310  virtual void onCertificate(
311  uint16_t /* certId */,
312  std::unique_ptr<folly::IOBuf> /* authenticator */) {
313  }
314 
320  virtual uint32_t numOutgoingStreams() const { return 0; }
321 
327  virtual uint32_t numIncomingStreams() const { return 0; }
328 
329  virtual ~Callback() {}
330  };
331 
332  virtual ~HTTPCodec() {}
333 
339  HPACKTableInfo defaultHPACKTableInfo;
340  return defaultHPACKTableInfo;
341  }
342 
347  virtual CodecProtocol getProtocol() const = 0;
348 
355  virtual const std::string& getUserAgent() const = 0;
356 
362  virtual TransportDirection getTransportDirection() const = 0;
363 
367  virtual bool supportsStreamFlowControl() const {
368  return false;
369  }
370 
374  virtual bool supportsSessionFlowControl() const {
375  return false;
376  }
377 
382  virtual StreamID createStream() = 0;
383 
388  virtual void setCallback(Callback* callback) = 0;
389 
394  virtual bool isBusy() const = 0;
395 
400  virtual void setParserPaused(bool paused) = 0;
401 
407  virtual size_t onIngress(const folly::IOBuf& buf) = 0;
408 
412  virtual void onIngressEOF() = 0;
413 
418  virtual bool onIngressUpgradeMessage(const HTTPMessage& /* msg */) {
419  return true;
420  }
421 
434  virtual bool isReusable() const = 0;
435 
442  virtual bool isWaitingToDrain() const = 0;
443 
448  virtual bool closeOnEgressComplete() const = 0;
449 
454  virtual bool supportsParallelRequests() const = 0;
455 
460  virtual bool supportsPushTransactions() const = 0;
461 
466  virtual bool supportsExTransactions() const {
467  return false;
468  }
469 
475  virtual size_t generateConnectionPreface(folly::IOBufQueue& /* writeBuf */) {
476  return 0;
477  }
478 
488  const HTTPMessage& msg,
489  bool eom = false,
490  HTTPHeaderSize* size = nullptr) = 0;
491 
492  virtual void generatePushPromise(folly::IOBufQueue& /* writeBuf */,
493  StreamID /* stream */,
494  const HTTPMessage& /* msg */,
495  StreamID /* assocStream */,
496  bool /* eom = false */,
497  HTTPHeaderSize* /* size = nullptr */) {}
498 
499  virtual void generateExHeader(folly::IOBufQueue& /* writeBuf */,
500  StreamID /* stream */,
501  const HTTPMessage& /* msg */,
502  const HTTPCodec::ExAttributes& /*exAttributes*/,
503  bool /* eom = false */,
504  HTTPHeaderSize* /* size = nullptr */) {}
505 
518  virtual size_t generateBody(folly::IOBufQueue& writeBuf,
519  StreamID stream,
520  std::unique_ptr<folly::IOBuf> chain,
521  folly::Optional<uint8_t> padding,
522  bool eom) = 0;
523 
527  virtual size_t generateChunkHeader(folly::IOBufQueue& writeBuf,
528  StreamID stream,
529  size_t length) = 0;
530 
534  virtual size_t generateChunkTerminator(folly::IOBufQueue& writeBuf,
535  StreamID stream) = 0;
536 
541  virtual size_t generateTrailers(folly::IOBufQueue& writeBuf,
542  StreamID stream,
543  const HTTPHeaders& trailers) = 0;
544 
551  virtual size_t generateEOM(folly::IOBufQueue& writeBuf,
552  StreamID stream) = 0;
553 
558  virtual size_t generateRstStream(folly::IOBufQueue& writeBuf,
559  StreamID stream,
560  ErrorCode code) = 0;
561 
566  virtual size_t generateGoaway(
567  folly::IOBufQueue& writeBuf,
568  StreamID lastStream,
569  ErrorCode code,
570  std::unique_ptr<folly::IOBuf> debugData = nullptr) = 0;
571 
576  virtual size_t generatePingRequest(folly::IOBufQueue& /* writeBuf */) {
577  return 0;
578  }
579 
584  virtual size_t generatePingReply(
585  folly::IOBufQueue& /* writeBuf */,
586  uint64_t /* uniqueID */) { return 0; }
587 
592  virtual size_t generateSettings(folly::IOBufQueue& /* writeBuf */) {
593  return 0;
594  }
595 
600  virtual size_t generateSettingsAck(folly::IOBufQueue& /* writeBuf */) {
601  return 0;
602  }
603 
604  /*
605  * Generate a WINDOW_UPDATE message, if supported. The delta is the amount
606  * of ingress bytes we processed and freed from the current receive window.
607  * Returns the number of bytes written on the wire as a result of invoking
608  * this function.
609  */
610  virtual size_t generateWindowUpdate(
611  folly::IOBufQueue& /* writeBuf */,
612  StreamID /* stream */,
613  uint32_t /* delta */) {
614  return 0;
615  }
616 
617  /*
618  * Generate a PRIORITY message, if supported
619  */
620  virtual size_t generatePriority(
621  folly::IOBufQueue& /* writeBuf */,
622  StreamID /* stream */,
623  const HTTPMessage::HTTPPriority& /* pri */) {
624  return 0;
625  }
626 
627  /*
628  * Generate a CERTIFICATE_REQUEST message, if supported in the protocol
629  * implemented by the codec.
630  */
632  folly::IOBufQueue& /* writeBuf */,
633  uint16_t /* requestId */,
634  std::unique_ptr<folly::IOBuf> /* chain */) {
635  return 0;
636  }
637 
638  /*
639  * Generate a CERTIFICATE message, if supported in the protocol
640  * implemented by the codec.
641  */
642  virtual size_t generateCertificate(
643  folly::IOBufQueue& /* writeBuf */,
644  uint16_t /* certId */,
645  std::unique_ptr<folly::IOBuf> /* certData */) {
646  return 0;
647  }
648 
649  /*
650  * The below interfaces need only be implemented if the codec supports
651  * settings
652  */
654  return nullptr;
655  }
656 
657  virtual const HTTPSettings* getIngressSettings() const {
658  return nullptr;
659  }
660 
665  virtual void enableDoubleGoawayDrain() {}
666 
670  virtual void setHeaderCodecStats(HeaderCodec::Stats* /* stats */) {}
671 
675  virtual StreamID getLastIncomingStreamID() const { return 0; }
676 
680  virtual uint32_t getDefaultWindowSize() const { return 0; }
681 
696  virtual size_t addPriorityNodes(
697  PriorityQueue& /* queue */,
698  folly::IOBufQueue& /* writeBuf */,
699  uint8_t /* maxLevel */) {
700  return 0;
701  }
702 
706  virtual StreamID mapPriorityToDependency(uint8_t /* priority */) const {
707  return 0;
708  }
709 
713  virtual int8_t mapDependencyToPriority(StreamID /* parent */) const {
714  return -1;
715  }
716 };
717 
718 }
virtual const HTTPSettings * getIngressSettings() const
Definition: HTTPCodec.h:657
virtual size_t onIngress(const folly::IOBuf &buf)=0
virtual size_t generatePingReply(folly::IOBufQueue &, uint64_t)
Definition: HTTPCodec.h:584
static const folly::Optional< uint8_t > NoPadding
Definition: HTTPCodec.h:53
virtual bool closeOnEgressComplete() const =0
virtual StreamID createStream()=0
virtual HTTPSettings * getEgressSettings()
Definition: HTTPCodec.h:653
virtual bool supportsSessionFlowControl() const
Definition: HTTPCodec.h:374
static const StreamID MAX_STREAM_ID
Definition: HTTPCodec.h:55
virtual bool supportsPushTransactions() const =0
virtual void generateHeader(folly::IOBufQueue &writeBuf, StreamID stream, const HTTPMessage &msg, bool eom=false, HTTPHeaderSize *size=nullptr)=0
virtual void setCallback(Callback *callback)=0
virtual void onSettings(const SettingsList &)
Definition: HTTPCodec.h:251
virtual void onChunkHeader(StreamID, size_t)
Definition: HTTPCodec.h:148
virtual void onExMessageBegin(StreamID, StreamID, bool, HTTPMessage *)
Definition: HTTPCodec.h:107
virtual void onGenerateFrameHeader(StreamID, uint8_t, uint64_t, uint16_t=0)
Definition: HTTPCodec.h:283
virtual void onPingReply(uint64_t)
Definition: HTTPCodec.h:237
virtual size_t generateChunkTerminator(folly::IOBufQueue &writeBuf, StreamID stream)=0
virtual HPACKTableInfo getHPACKTableInfo() const
Definition: HTTPCodec.h:338
virtual void onCertificate(uint16_t, std::unique_ptr< folly::IOBuf >)
Definition: HTTPCodec.h:310
virtual void onCertificateRequest(uint16_t, std::unique_ptr< folly::IOBuf >)
Definition: HTTPCodec.h:297
virtual CodecProtocol getProtocol() const =0
virtual void generatePushPromise(folly::IOBufQueue &, StreamID, const HTTPMessage &, StreamID, bool, HTTPHeaderSize *)
Definition: HTTPCodec.h:492
virtual void onPriority(StreamID, const HTTPMessage::HTTPPriority &)
Definition: HTTPCodec.h:263
virtual ~HTTPCodec()
Definition: HTTPCodec.h:332
requires And< SemiMovable< VN >... > &&SemiMovable< E > auto error(E e)
Definition: error.h:48
virtual size_t generateBody(folly::IOBufQueue &writeBuf, StreamID stream, std::unique_ptr< folly::IOBuf > chain, folly::Optional< uint8_t > padding, bool eom)=0
virtual size_t generateRstStream(folly::IOBufQueue &writeBuf, StreamID stream, ErrorCode code)=0
ExAttributes(StreamID controlStreamId, bool isUnidirectional)
Definition: HTTPCodec.h:59
virtual size_t generateTrailers(folly::IOBufQueue &writeBuf, StreamID stream, const HTTPHeaders &trailers)=0
virtual bool isWaitingToDrain() const =0
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
virtual void onIngressEOF()=0
virtual const std::string & getUserAgent() const =0
virtual void onPingRequest(uint64_t)
Definition: HTTPCodec.h:230
void writeBuf(const Buf &buf, folly::io::Appender &out)
virtual size_t generatePriority(folly::IOBufQueue &, StreamID, const HTTPMessage::HTTPPriority &)
Definition: HTTPCodec.h:620
virtual uint32_t numIncomingStreams() const
Definition: HTTPCodec.h:327
virtual bool supportsStreamFlowControl() const
Definition: HTTPCodec.h:367
virtual size_t generateCertificate(folly::IOBufQueue &, uint16_t, std::unique_ptr< folly::IOBuf >)
Definition: HTTPCodec.h:642
virtual bool supportsExTransactions() const
Definition: HTTPCodec.h:466
virtual void generateExHeader(folly::IOBufQueue &, StreamID, const HTTPMessage &, const HTTPCodec::ExAttributes &, bool, HTTPHeaderSize *)
Definition: HTTPCodec.h:499
virtual void onFrameHeader(StreamID, uint8_t, uint64_t, uint8_t, uint16_t=0)
Definition: HTTPCodec.h:206
virtual size_t generateConnectionPreface(folly::IOBufQueue &)
Definition: HTTPCodec.h:475
virtual size_t generateSettingsAck(folly::IOBufQueue &)
Definition: HTTPCodec.h:600
virtual size_t addPriorityNodes(PriorityQueue &, folly::IOBufQueue &, uint8_t)
Definition: HTTPCodec.h:696
virtual uint32_t getDefaultWindowSize() const
Definition: HTTPCodec.h:680
virtual size_t generateChunkHeader(folly::IOBufQueue &writeBuf, StreamID stream, size_t length)=0
virtual StreamID mapPriorityToDependency(uint8_t) const
Definition: HTTPCodec.h:706
virtual bool onIngressUpgradeMessage(const HTTPMessage &)
Definition: HTTPCodec.h:418
virtual void enableDoubleGoawayDrain()
Definition: HTTPCodec.h:665
virtual size_t generateEOM(folly::IOBufQueue &writeBuf, StreamID stream)=0
virtual void onSettingsAck()
Definition: HTTPCodec.h:257
virtual int8_t mapDependencyToPriority(StreamID) const
Definition: HTTPCodec.h:713
std::tuple< uint32_t, bool, uint8_t > HTTPPriority
Definition: HTTPMessage.h:592
std::vector< HTTPSetting > SettingsList
Definition: HTTPSettings.h:81
virtual StreamID getLastIncomingStreamID() const
Definition: HTTPCodec.h:675
virtual void onGoaway(uint64_t, ErrorCode, std::unique_ptr< folly::IOBuf >=nullptr)
Definition: HTTPCodec.h:220
virtual uint32_t numOutgoingStreams() const
Definition: HTTPCodec.h:320
virtual void onAbort(StreamID, ErrorCode)
Definition: HTTPCodec.h:192
const char * string
Definition: Conv.cpp:212
virtual bool isReusable() const =0
static const folly::Optional< StreamID > NoStream
Definition: HTTPCodec.h:51
uint64_t StreamID
Definition: HTTPCodec.h:49
virtual size_t generateWindowUpdate(folly::IOBufQueue &, StreamID, uint32_t)
Definition: HTTPCodec.h:610
virtual void onWindowUpdate(StreamID, uint32_t)
Definition: HTTPCodec.h:243
virtual void onChunkComplete(StreamID)
Definition: HTTPCodec.h:156
virtual void setHeaderCodecStats(HeaderCodec::Stats *)
Definition: HTTPCodec.h:670
virtual size_t generateSettings(folly::IOBufQueue &)
Definition: HTTPCodec.h:592
virtual size_t generateGoaway(folly::IOBufQueue &writeBuf, StreamID lastStream, ErrorCode code, std::unique_ptr< folly::IOBuf > debugData=nullptr)=0
virtual void onPushMessageBegin(StreamID, StreamID, HTTPMessage *)
Definition: HTTPCodec.h:95
virtual bool isBusy() const =0
virtual void setParserPaused(bool paused)=0
virtual size_t generatePingRequest(folly::IOBufQueue &)
Definition: HTTPCodec.h:576
virtual bool supportsParallelRequests() const =0
folly::Function< void()> parent
Definition: AtFork.cpp:34
virtual TransportDirection getTransportDirection() const =0
virtual bool onNativeProtocolUpgrade(StreamID, CodecProtocol, const std::string &, HTTPMessage &)
Definition: HTTPCodec.h:271
virtual size_t generateCertificateRequest(folly::IOBufQueue &, uint16_t, std::unique_ptr< folly::IOBuf >)
Definition: HTTPCodec.h:631
static const folly::Optional< ExAttributes > NoExAttributes
Definition: HTTPCodec.h:66