proxygen
FileServerHandler Class Reference
Inheritance diagram for FileServerHandler:
wangle::HandlerAdapter< std::string > wangle::Handler< std::string, std::string, std::string, std::string > wangle::HandlerBase< HandlerContext< std::string, std::string > >

Public Member Functions

void read (Context *ctx, std::string filename) override
 
void readException (Context *ctx, exception_wrapper ew) override
 
void transportActive (Context *ctx) override
 
- Public Member Functions inherited from wangle::HandlerAdapter< std::string >
void read (Context *ctx, std::stringmsg) override
 
folly::Future< folly::Unitwrite (Context *ctx, std::stringmsg) override
 
- Public Member Functions inherited from wangle::Handler< std::string, std::string, std::string, std::string >
 ~Handler () override=default
 
virtual void readEOF (Context *ctx)
 
virtual void transportInactive (Context *ctx)
 
virtual folly::Future< folly::UnitwriteException (Context *ctx, folly::exception_wrapper e)
 
virtual folly::Future< folly::Unitclose (Context *ctx)
 
- Public Member Functions inherited from wangle::HandlerBase< HandlerContext< std::string, std::string > >
virtual ~HandlerBase ()=default
 
virtual void attachPipeline (HandlerContext< std::string, std::string > *)
 
virtual void detachPipeline (HandlerContext< std::string, std::string > *)
 
HandlerContext< std::string, std::string > * getContext ()
 

Additional Inherited Members

- Public Types inherited from wangle::HandlerAdapter< std::string >
typedef Handler< std::string, std::string, std::string, std::string >::Context Context
 
- Public Types inherited from wangle::Handler< std::string, std::string, std::string, std::string >
typedef std::string rin
 
typedef std::string rout
 
typedef std::string win
 
typedef std::string wout
 
typedef HandlerContext< std::string, std::stringContext
 
- Static Public Attributes inherited from wangle::Handler< std::string, std::string, std::string, std::string >
static const HandlerDir dir
 

Detailed Description

Definition at line 33 of file FileServer.cpp.

Member Function Documentation

void FileServerHandler::read ( Context ctx,
std::string  filename 
)
inlineoverridevirtual

Implements wangle::Handler< std::string, std::string, std::string, std::string >.

Definition at line 35 of file FileServer.cpp.

References folly::netops::close(), folly::exceptionStr(), wangle::HandlerContext< In, Out >::getPipelineShared(), wangle::HandlerContext< In, Out >::getTransport(), folly::gen::guard(), folly::sformat(), and fizz::detail::write().

35  {
36  if (filename == "bye") {
37  close(ctx);
38  }
39 
40  int fd = open(filename.c_str(), O_RDONLY);
41  if (fd == -1) {
42  write(ctx, sformat("Error opening {}: {}\r\n",
43  filename,
44  strerror(errno)));
45  return;
46  }
47 
48  struct stat buf;
49  if (fstat(fd, &buf) == -1) {
50  write(ctx, sformat("Could not stat file {}: {}\r\n",
51  filename,
52  strerror(errno)));
53  return;
54  }
55 
56  FileRegion fileRegion(fd, 0, buf.st_size);
57  auto guard = ctx->getPipelineShared();
58  fileRegion.transferTo(ctx->getTransport())
59  .onError([this, guard, ctx, filename](const std::exception& e){
60  write(ctx, sformat("Error sending file {}: {}\r\n",
61  filename,
62  exceptionStr(e)));
63  });
64  }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
fbstring exceptionStr(const std::exception &e)
folly::Future< folly::Unit > write(Context *ctx, std::stringmsg) override
Definition: Handler.h:167
GuardImpl guard(ErrorHandler &&handler)
Definition: Base.h:840
virtual folly::Future< folly::Unit > close(Context *ctx)
Definition: Handler.h:79
void FileServerHandler::readException ( Context ctx,
exception_wrapper  ew 
)
inlineoverridevirtual

Reimplemented from wangle::Handler< std::string, std::string, std::string, std::string >.

Definition at line 66 of file FileServer.cpp.

References folly::netops::close(), folly::exceptionStr(), folly::sformat(), and fizz::detail::write().

66  {
67  write(ctx, sformat("Error: {}\r\n", exceptionStr(ew)))
68  .thenValue([this, ctx](auto&&) { close(ctx); });
69  }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
fbstring exceptionStr(const std::exception &e)
folly::Future< folly::Unit > write(Context *ctx, std::stringmsg) override
Definition: Handler.h:167
virtual folly::Future< folly::Unit > close(Context *ctx)
Definition: Handler.h:79
void FileServerHandler::transportActive ( Context ctx)
inlineoverridevirtual

Reimplemented from wangle::Handler< std::string, std::string, std::string, std::string >.

Definition at line 71 of file FileServer.cpp.

References folly::SocketAddress::describe(), wangle::HandlerContext< In, Out >::getTransport(), and fizz::detail::write().

71  {
72  SocketAddress localAddress;
73  ctx->getTransport()->getLocalAddress(&localAddress);
74  write(ctx, "Welcome to " + localAddress.describe() + "!\r\n");
75  write(ctx, "Type the name of a file and it will be streamed to you!\r\n");
76  write(ctx, "Type 'bye' to exit.\r\n");
77  }
std::string describe() const
folly::Future< folly::Unit > write(Context *ctx, std::stringmsg) override
Definition: Handler.h:167

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