YARP
Yet Another Robot Platform
HttpCarrier.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006-2010 RobotCub Consortium
4  * All rights reserved.
5  *
6  * This software may be modified and distributed under the terms of the
7  * BSD-3-Clause license. See the accompanying LICENSE file for details.
8  */
9 
10 #ifndef YARP_OS_IMPL_HTTPCARRIER
11 #define YARP_OS_IMPL_HTTPCARRIER
12 
13 #include <yarp/os/NetType.h>
14 #include <yarp/os/Property.h>
17 #include <yarp/os/TwoWayStream.h>
19 
20 namespace yarp {
21 namespace os {
22 namespace impl {
23 
49  public TwoWayStream,
50  public OutputStream
51 {
52 private:
53  std::string proc;
54  std::string part;
55  bool data;
56  bool filterData;
57  bool chunked;
58  TwoWayStream* delegate;
61  std::string format;
62  std::string outer;
63  bool isWriter;
64 
65 public:
67  const char* txt,
68  const char* prefix,
69  yarp::os::Property& prop,
70  bool writer);
71 
72  virtual ~HttpTwoWayStream();
73 
74  InputStream& getInputStream() override;
75  OutputStream& getOutputStream() override;
76  const Contact& getLocalAddress() const override;
77  const Contact& getRemoteAddress() const override;
78 
79  bool isOk() const override;
80  void reset() override;
82  void write(const Bytes& b) override;
83  virtual void apply(char ch);
84  void close() override;
85  void beginPacket() override;
86  void endPacket() override;
87 
88  void flip();
89  void finish();
90  bool useJson();
91  std::string* typeHint();
92 };
93 
94 
98 class HttpCarrier :
99  public TcpCarrier
100 {
101 private:
102  std::string url, input, prefix;
103  bool urlDone;
104  bool expectPost;
105  int contentLength;
106  yarp::os::Property prop;
107  HttpTwoWayStream* stream;
108 
109 public:
110  HttpCarrier();
111 
112  Carrier* create() const override;
113 
114  std::string getName() const override;
115 
116  bool checkHeader(const Bytes& header, const char* prefix);
117 
118  bool checkHeader(const Bytes& header) override;
119  void setParameters(const Bytes& header) override;
120  void getHeader(Bytes& header) const override;
121  bool requireAck() const override;
122  bool isTextMode() const override;
123  bool supportReply() const override;
124  bool sendHeader(ConnectionState& proto) override;
125  bool expectSenderSpecifier(ConnectionState& proto) override;
126  bool expectReplyToHeader(ConnectionState& proto) override;
127  bool sendIndex(ConnectionState& proto, SizedWriter& writer) override;
128  bool expectIndex(ConnectionState& proto) override;
129  bool sendAck(ConnectionState& proto) override;
130  bool expectAck(ConnectionState& proto) override;
131  bool respondToHeader(ConnectionState& proto) override;
132 
133  bool reply(ConnectionState& proto, SizedWriter& writer) override;
134  bool write(ConnectionState& proto, SizedWriter& writer) override;
135 };
136 
137 } // namespace impl
138 } // namespace os
139 } // namespace yarp
140 
141 #endif // YARP_OS_IMPL_HTTPCARRIER
yarp::os::TwoWayStream
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:29
yarp::os::impl::HttpCarrier::expectSenderSpecifier
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
Definition: HttpCarrier.cpp:651
yarp::os::impl::HttpTwoWayStream
Minimal http connection support.
Definition: HttpCarrier.h:51
yarp::os::impl::HttpTwoWayStream::reset
void reset() override
Reset the stream.
Definition: HttpCarrier.cpp:388
yarp::os::Carrier
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
yarp::os::impl::HttpTwoWayStream::finish
void finish()
Definition: HttpCarrier.cpp:521
TcpCarrier.h
yarp::os::OutputStream::write
virtual void write(char ch)
Write a single byte to the stream.
Definition: OutputStream.cpp:17
yarp::os::impl::HttpTwoWayStream::typeHint
std::string * typeHint()
Definition: HttpCarrier.cpp:531
yarp::os::OutputStream
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
yarp::os::impl::HttpCarrier::expectAck
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
Definition: HttpCarrier.cpp:806
yarp::os::impl::HttpTwoWayStream::beginPacket
void beginPacket() override
Mark the beginning of a logical packet.
Definition: HttpCarrier.cpp:506
yarp::os::impl::HttpCarrier::sendHeader
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
Definition: HttpCarrier.cpp:632
yarp::os::impl::HttpTwoWayStream::getRemoteAddress
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
Definition: HttpCarrier.cpp:378
yarp::os::impl::HttpCarrier::HttpCarrier
HttpCarrier()
Definition: HttpCarrier.cpp:537
yarp::os::impl::HttpTwoWayStream::~HttpTwoWayStream
virtual ~HttpTwoWayStream()
Definition: HttpCarrier.cpp:355
yarp::os::impl::HttpTwoWayStream::close
void close() override
Terminate the stream.
Definition: HttpCarrier.cpp:499
yarp::os::impl::HttpTwoWayStream::getOutputStream
OutputStream & getOutputStream() override
Get an OutputStream to write to.
Definition: HttpCarrier.cpp:368
yarp::os::StringOutputStream
An OutputStream that produces a string.
Definition: StringOutputStream.h:25
StringInputStream.h
TwoWayStream.h
yarp::os::impl::HttpCarrier::getHeader
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
Definition: HttpCarrier.cpp:606
NetType.h
yarp::os::impl::HttpCarrier::sendIndex
bool sendIndex(ConnectionState &proto, SizedWriter &writer) override
Definition: HttpCarrier.cpp:784
yarp::os::impl::HttpCarrier::requireAck
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
Definition: HttpCarrier.cpp:616
yarp::os::impl::HttpTwoWayStream::getInputStream
InputStream & getInputStream() override
Get an InputStream to read from.
Definition: HttpCarrier.cpp:363
Property.h
yarp::os::impl::HttpTwoWayStream::write
void write(const Bytes &b) override
Write a block of bytes to the stream.
Definition: HttpCarrier.cpp:393
yarp::os::impl::HttpCarrier::getName
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
Definition: HttpCarrier.cpp:552
yarp::os::impl::HttpTwoWayStream::flip
void flip()
Definition: HttpCarrier.cpp:516
yarp::os::impl::HttpCarrier::checkHeader
bool checkHeader(const Bytes &header, const char *prefix)
Definition: HttpCarrier.cpp:557
yarp::os::impl::HttpCarrier::create
Carrier * create() const override
Factory method.
Definition: HttpCarrier.cpp:547
yarp::os::impl::HttpCarrier::reply
bool reply(ConnectionState &proto, SizedWriter &writer) override
Definition: HttpCarrier.cpp:865
yarp::os::impl::TcpCarrier
Communicating between two ports via TCP.
Definition: TcpCarrier.h:24
yarp::os::StringInputStream
An InputStream that reads from a string.
Definition: StringInputStream.h:25
yarp::os::impl::HttpTwoWayStream::getLocalAddress
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
Definition: HttpCarrier.cpp:373
yarp::os::Bytes
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
yarp::os::impl::HttpCarrier::expectIndex
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
Definition: HttpCarrier.cpp:792
yarp::os::impl::HttpTwoWayStream::apply
virtual void apply(char ch)
Definition: HttpCarrier.cpp:404
yarp::os::impl::HttpTwoWayStream::isOk
bool isOk() const override
Check if the stream is ok or in an error state.
Definition: HttpCarrier.cpp:383
yarp::os::impl::HttpCarrier::write
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
Definition: HttpCarrier.cpp:824
StringOutputStream.h
yarp::os::ConnectionState
The basic state of a connection - route, streams in use, etc.
Definition: ConnectionState.h:31
yarp::os::impl::HttpCarrier::isTextMode
bool isTextMode() const override
Check if carrier is textual in nature.
Definition: HttpCarrier.cpp:621
yarp::os::impl::HttpCarrier::sendAck
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
Definition: HttpCarrier.cpp:799
yarp::os::impl::HttpCarrier::respondToHeader
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
Definition: HttpCarrier.cpp:813
yarp::os::impl::HttpCarrier
Communicating via http.
Definition: HttpCarrier.h:100
yarp::os::impl::HttpCarrier::expectReplyToHeader
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
Definition: HttpCarrier.cpp:761
yarp
The main, catch-all namespace for YARP.
Definition: environment.h:18
yarp::os::impl::HttpCarrier::supportReply
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
Definition: HttpCarrier.cpp:627
yarp::os::Contact
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
yarp::os::impl::HttpTwoWayStream::endPacket
void endPacket() override
Mark the end of a logical packet (see beginPacket).
Definition: HttpCarrier.cpp:511
yarp::os::InputStream
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
yarp::os::impl::HttpTwoWayStream::useJson
bool useJson()
Definition: HttpCarrier.cpp:526
yarp::os::impl::HttpCarrier::setParameters
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Definition: HttpCarrier.cpp:585
yarp::os::SizedWriter
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37
yarp::os::impl::HttpTwoWayStream::HttpTwoWayStream
HttpTwoWayStream(TwoWayStream *delegate, const char *txt, const char *prefix, yarp::os::Property &prop, bool writer)
Definition: HttpCarrier.cpp:170