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
A simple abstraction for a block of bytes.
Definition: Bytes.h:28
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
Definition: Carrier.h:48
The basic state of a connection - route, streams in use, etc.
Represents how to reach a part of a YARP network.
Definition: Contact.h:39
Simple specification of the minimum functions needed from input streams.
Definition: InputStream.h:29
Simple specification of the minimum functions needed from output streams.
Definition: OutputStream.h:25
virtual void write(char ch)
Write a single byte to the stream.
A class for storing options and configuration information.
Definition: Property.h:37
Minimal requirements for an efficient Writer.
Definition: SizedWriter.h:36
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:29
Communicating via http.
Definition: HttpCarrier.h:100
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:51
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:24
The main, catch-all namespace for YARP.
Definition: environment.h:18