YARP
Yet Another Robot Platform
HttpCarrier.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef YARP_OS_IMPL_HTTPCARRIER
8 #define YARP_OS_IMPL_HTTPCARRIER
9 
10 #include <yarp/os/NetType.h>
11 #include <yarp/os/Property.h>
14 #include <yarp/os/TwoWayStream.h>
16 
17 namespace yarp {
18 namespace os {
19 namespace impl {
20 
46  public TwoWayStream,
47  public OutputStream
48 {
49 private:
50  std::string proc;
51  std::string part;
52  bool data;
53  bool filterData;
54  bool chunked;
55  TwoWayStream* delegate;
58  std::string format;
59  std::string outer;
60  bool isWriter;
61 
62 public:
64  const char* txt,
65  const char* prefix,
66  yarp::os::Property& prop,
67  bool writer);
68 
69  virtual ~HttpTwoWayStream();
70 
71  InputStream& getInputStream() override;
72  OutputStream& getOutputStream() override;
73  const Contact& getLocalAddress() const override;
74  const Contact& getRemoteAddress() const override;
75 
76  bool isOk() const override;
77  void reset() override;
79  void write(const Bytes& b) override;
80  virtual void apply(char ch);
81  void close() override;
82  void beginPacket() override;
83  void endPacket() override;
84 
85  void flip();
86  void finish();
87  bool useJson();
88  std::string* typeHint();
89 };
90 
91 
95 class HttpCarrier :
96  public TcpCarrier
97 {
98 private:
99  std::string url, input, prefix;
100  bool urlDone;
101  bool expectPost;
102  int contentLength;
103  yarp::os::Property prop;
104  HttpTwoWayStream* stream;
105 
106 public:
107  HttpCarrier();
108 
109  Carrier* create() const override;
110 
111  std::string getName() const override;
112 
113  bool checkHeader(const Bytes& header, const char* prefix);
114 
115  bool checkHeader(const Bytes& header) override;
116  void setParameters(const Bytes& header) override;
117  void getHeader(Bytes& header) const override;
118  bool requireAck() const override;
119  bool isTextMode() const override;
120  bool supportReply() const override;
121  bool sendHeader(ConnectionState& proto) override;
122  bool expectSenderSpecifier(ConnectionState& proto) override;
123  bool expectReplyToHeader(ConnectionState& proto) override;
124  bool sendIndex(ConnectionState& proto, SizedWriter& writer) override;
125  bool expectIndex(ConnectionState& proto) override;
126  bool sendAck(ConnectionState& proto) override;
127  bool expectAck(ConnectionState& proto) override;
128  bool respondToHeader(ConnectionState& proto) override;
129 
130  bool reply(ConnectionState& proto, SizedWriter& writer) override;
131  bool write(ConnectionState& proto, SizedWriter& writer) override;
132 };
133 
134 } // namespace impl
135 } // namespace os
136 } // namespace yarp
137 
138 #endif // YARP_OS_IMPL_HTTPCARRIER
A simple abstraction for a block of bytes.
Definition: Bytes.h:25
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:45
The basic state of a connection - route, streams in use, etc.
Represents how to reach a part of a YARP network.
Definition: Contact.h:36
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:26
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:22
virtual void write(char ch)
Write a single byte to the stream.
A class for storing options and configuration information.
Definition: Property.h:34
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:33
An InputStream that reads from a string.
An OutputStream that produces a string.
A stream which can be asked to perform bidirectional communication.
Definition: TwoWayStream.h:26
Communicating via http.
Definition: HttpCarrier.h:97
Carrier * create() const override
Factory method.
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
bool isTextMode() const override
Check if carrier is textual in nature.
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
bool reply(ConnectionState &proto, SizedWriter &writer) override
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
bool checkHeader(const Bytes &header, const char *prefix)
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
bool sendIndex(ConnectionState &proto, SizedWriter &writer) override
Minimal http connection support.
Definition: HttpCarrier.h:48
bool isOk() const override
Check if the stream is ok or in an error state.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
void beginPacket() override
Mark the beginning of a logical packet.
HttpTwoWayStream(TwoWayStream *delegate, const char *txt, const char *prefix, yarp::os::Property &prop, bool writer)
void endPacket() override
Mark the end of a logical packet (see beginPacket).
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
InputStream & getInputStream() override
Get an InputStream to read from.
virtual void apply(char ch)
void reset() override
Reset the stream.
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
void write(const Bytes &b) override
Write a block of bytes to the stream.
void close() override
Terminate the stream.
Communicating between two ports via TCP.
Definition: TcpCarrier.h:21
The main, catch-all namespace for YARP.
Definition: dirs.h:16