|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
24 static std::string
quoteFree(
const std::string& src)
37 static bool asJson(std::string& accum,
39 std::string* hint =
nullptr);
41 static bool asJson(std::string& accum,
55 }
else if (ch ==
'\r') {
58 }
else if (ch ==
'\0') {
62 if (ch ==
'\\' || ch ==
'\"') {
77 static bool asJson(std::string& accum,
85 bool struc_set =
false;
89 if (hint !=
nullptr) {
90 if ((*hint) ==
"list") {
93 }
else if ((*hint) ==
"dict") {
102 }
else if (tag ==
"dict") {
107 struc = (bot->
size() > 1);
108 if (bot->
size() > 0) {
115 for (
size_t i = offset2; i < bot->
size(); i++) {
131 bool need_comma =
false;
133 accum +=
"\"type\": ";
137 for (
size_t i = offset; i < bot->
size(); i++) {
139 if (boti ==
nullptr) {
159 for (
int i = offset; (size_t)i < bot->size(); i++) {
173 this->isWriter = writer;
188 format = p.
check(
"format",
Value(
"html")).asString();
189 outer = p.
check(
"outer",
Value(
"auto")).asString();
190 bool admin = p.
check(
"admin");
191 bool req = p.
check(
"req");
192 if (p.
check(
"cmd")) {
194 }
else if (p.
check(
"data") || req) {
197 if (!p.
check(
"format")) {
199 p.
put(
"format",
"json");
208 for (
unsigned int i = 0; i < s.length(); i++) {
211 if ((ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9') || (ch ==
'_')) {
216 if (i != s.length() - 1) {
225 if (i != s.length() - 1) {
234 s = std::string(
"a\n") + sFixed;
236 s = std::string(
"d\n") + sFixed;
241 std::string from = prefix;
242 from +=
"<input type=text name=data value=\"";
245 from +=
"\"><input type=submit value=\"send data\"></form></p>\n";
248 bool classic =
false;
250 if (s.length() <= 1) {
253 if (s.length() > 1) {
254 if (s[0] ==
'?' || s[1] ==
'?') {
257 if (s[0] ==
'd' && s[1] ==
'\n') {
261 if (s.length() >= 4) {
262 if (s[0] ==
'f' && s[1] ==
'o' && s[2] ==
'r' && s[3] ==
'm') {
265 if (s[0] ==
'd' && s[1] ==
'a' && s[2] ==
't' && s[3] ==
'a') {
268 if (s[0] ==
'f' && s[1] ==
'a' && s[2] ==
'v' && s[3] ==
'i') {
278 std::string header =
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n";
281 header +=
"Transfer-Encoding: chunked\r\n";
286 std::string body = from;
288 body +=
"Reading data from port...\n";
293 Bytes b1((
char*)header.c_str(), header.length());
298 for (
int i = 0; i < N; i++) {
303 Bytes b2((
char*)body.c_str(), body.length());
338 for (
int i = 0; i < (int)s.length(); i++) {
357 if (delegate !=
nullptr) {
375 return delegate->getLocalAddress();
380 return delegate->getRemoteAddress();
395 if (chunked || isWriter) {
396 delegate->getOutputStream().write(b);
398 for (
size_t i = 0; i < b.
length(); i++) {
414 proc +=
"<a href=\"http://";
424 for (
size_t i = 0; i < target - len; i++) {
438 if ((part[0] ==
'\"' && part[1] ==
'[') || (part[0] ==
'+')) {
440 if (part[0] ==
'+') {
443 std::string org = part;
444 part =
"<p><form method=post action='/form'>";
446 for (i = 0; i < org.length(); i++) {
451 part +=
"<input type=hidden name=data value=\"";
458 for (i = 0; i < org.length(); i++) {
461 if ((ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9') || (ch ==
'_')) {
468 part +=
"<input type=text name=";
470 part +=
" size=5 value=\"\">";
478 part +=
"<input type=submit value=\"go\">";
479 part +=
"</form></p>";
484 if (data || !filterData) {
485 Bytes tmp((
char*)proc.c_str(), proc.length());
486 delegate->getOutputStream().write(tmp);
487 delegate->getOutputStream().flush();
490 if (proc[0] ==
'd' || proc[0] ==
'D') {
508 delegate->beginPacket();
513 delegate->endPacket();
528 return format ==
"json";
559 if (header.
length() == 8) {
560 std::string target = prefix;
561 for (
unsigned int i = 0; i < target.length(); i++) {
562 if (!(target[i] == header.
get()[i])) {
573 bool ok = checkHeader(header,
"GET /");
577 ok = checkHeader(header,
"POST /fo");
580 ok = !checkHeader(header,
"GET /?ac");
587 if (header.
length() == 8) {
589 for (
unsigned int j = 0; j < 8; j++) {
590 char ch = header.
get()[j];
608 if (header.
length() == 8) {
609 std::string target =
"GET / HT";
610 for (
int i = 0; i < 8; i++) {
611 header.
get()[i] = target[i];
634 std::string target =
"GET / HTTP/1.0\r\n";
636 if (path.size() >= 2) {
637 target =
"GET " + path +
" HTTP/1.0\r\n";
646 Bytes b((
char*)target.c_str(), target.length());
656 std::string remainder = proto.
is().
readLine();
658 for (
char i : remainder) {
672 if (result.empty()) {
684 if (b.
get(1).
asString() ==
"application/x-www-form-urlencoded") {
694 Bytes start(blk.
get(), contentLength);
696 blk.
get()[contentLength] =
'\0';
703 prop.fromQuery(input.c_str());
704 prop.put(
"REQUEST_URI", url);
710 std::string from =
"<html><head><link href=\"http://";
714 from += R
"(/web/main.css" rel="stylesheet" type="text/css"/></head><body bgcolor='#ffffcc'><h1>yarp port )";
718 from +=
"<p>(<a href=\"http://";
722 from +=
"/data=list\">All ports</a>) \n";
724 from +=
"(<a href=\"http://";
728 from +=
"/\">connections</a>) \n";
730 from +=
"(<a href=\"http://";
734 from +=
"/data=help\">help</a>) \n";
736 from +=
"(<a href=\"http://";
740 from +=
"/r\">read</a>) \n";
744 from += R
"(<form method="post" action="http://)";
767 Bytes b((
char*)&buf[0], 1);
771 input += std::string(buf, len);
835 if (body.length() != 0) {
840 Bytes b2((
char*)header.c_str(), header.length());
843 Bytes b3((
char*)body.c_str(), body.length());
850 std::string txt = b.
toString() +
"\r\n";
854 Bytes b2((
char*)header.c_str(), header.length());
856 Bytes b3((
char*)txt.c_str(), txt.length());
875 std::string mime = b.
check(
"mime",
Value(
"text/html")).asString();
879 bool using_json =
false;
880 if (stream !=
nullptr) {
881 if (stream->useJson()) {
883 asJson(body, &b, stream->typeHint());
888 if (b.
check(
"web") && !using_json) {
892 if (b.
check(
"stream") && !using_json) {
893 std::string header(
"HTTP/1.1 200 OK\r\nContent-Type: ");
896 header +=
"Transfer-Encoding: chunked\r\n";
902 Bytes b2((
char*)header.c_str(), header.length());
906 for (
int i = 0; i < N; i++) {
910 Bytes b3((
char*)body.c_str(), body.length());
917 if (stream !=
nullptr) {
923 if (stream !=
nullptr) {
929 if (body.length() != 0 || using_json) {
930 std::string mime = b.
check(
"mime",
Value(using_json ?
"application/json" :
"text/html")).asString();
931 std::string header(
"HTTP/1.1 200 OK\nContent-Type: ");
934 header +=
"Access-Control-Allow-Origin: *\n";
936 Bytes b2((
char*)header.c_str(), header.length());
940 Bytes b3((
char*)body.c_str(), body.length());
A dummy connection to test yarp::os::Portable implementations.
virtual const Contact & getLocalAddress() const =0
Get the address of the local side of the stream.
A stream which can be asked to perform bidirectional communication.
A simple collection of objects that can be described and transmitted in a portable way.
virtual size_t length() const =0
bool expectSenderSpecifier(ConnectionState &proto) override
Expect the name of the sending port.
Minimal http connection support.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
size_type size() const
Gets the number of elements in the bottle.
void reset() override
Reset the stream.
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
virtual TwoWayStream * giveStreams()=0
Take ownership of the streams associated with the connection.
virtual void write(char ch)
Write a single byte to the stream.
Simple specification of the minimum functions needed from output streams.
bool expectAck(ConnectionState &proto) override
Receive an acknowledgement, if expected for this carrier.
void beginPacket() override
Mark the beginning of a logical packet.
bool sendHeader(ConnectionState &proto) override
Write a header appropriate to the carrier to the connection, followed by any carrier-specific data.
const Contact & getRemoteAddress() const override
Get the address of the remote side of the stream.
virtual ~HttpTwoWayStream()
void fromString(const std::string &text)
Initializes bottle from a string.
void close() override
Terminate the stream.
OutputStream & getOutputStream() override
Get an OutputStream to write to.
virtual void write(OutputStream &os)
Information about a connection between two ports.
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void getHeader(Bytes &header) const override
Provide 8 bytes describing this connection sufficiently to allow the other side of a connection to se...
virtual OutputStream & getOutputStream()=0
Get an OutputStream to write to.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool sendIndex(ConnectionState &proto, SizedWriter &writer) override
virtual bool isString() const
Checks if value is a string.
static std::string quoteFree(const std::string &src)
bool requireAck() const override
Check if carrier has flow control, requiring sent messages to be acknowledged by recipient.
virtual size_t headerLength() const =0
InputStream & getInputStream() override
Get an InputStream to read from.
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
OutputStream & os()
Shorthand for getOutputStream()
virtual const Route & getRoute() const =0
Get the route associated with this connection.
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
std::string toString() const override
Return a standard text representation of the content of the object.
static bool asJson(std::string &accum, yarp::os::Bottle *bot, std::string *hint=nullptr)
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
virtual void setRoute(const Route &route)=0
Set the route associated with this connection.
static std::string toHexString(int x)
virtual std::string asString() const
Get string value.
void write(const Bytes &b) override
Write a block of bytes to the stream.
std::string getName() const override
Get the name of this connection type ("tcp", "mcast", "shmem", ...)
virtual void flush()
Make sure all pending write operations are finished.
bool checkHeader(const Bytes &header, const char *prefix)
Carrier * create() const override
Factory method.
bool reply(ConnectionState &proto, SizedWriter &writer) override
bool check(const std::string &key) const override
Check if there exists a property of the given name.
const Contact & getLocalAddress() const override
Get the address of the local side of the stream.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A simple abstraction for a block of bytes.
bool expectIndex(ConnectionState &proto) override
Expect a message header, if there is one for this carrier.
virtual void apply(char ch)
bool isOk() const override
Check if the stream is ok or in an error state.
virtual bool isList() const
Checks if value is a list.
bool write(ConnectionState &proto, SizedWriter &writer) override
Write a message.
static std::string toString(int x)
The basic state of a connection - route, streams in use, etc.
bool isTextMode() const override
Check if carrier is textual in nature.
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
bool sendAck(ConnectionState &proto) override
Send an acknowledgement, if needed for this carrier.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
bool respondToHeader(ConnectionState &proto) override
Respond to the header.
void setTextMode(bool textmode)
Set the textMode of the dummy connection.
An interface to the operating system, including Port based communication.
bool expectReplyToHeader(ConnectionState &proto) override
Process reply to header, if one is expected for this carrier.
void setFromName(const std::string &fromName)
Set the source of the route.
bool supportReply() const override
This flag is used by YARP to determine whether the connection can carry RPC traffic,...
const Contact & getToContact() const
Get the destination contact of the route, if available.
InputStream & is()
Shorthand for getInputStream()
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
const std::string & getToName() const
Get the destination of the route.
virtual Bottle * asList() const
Get list value.
virtual const char * data(size_t index) const =0
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
std::string toString() const override
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
static Contact getNameServerContact()
Get the contact information for the port associated with the nameserver (usually "/root",...
void endPacket() override
Mark the end of a logical packet (see beginPacket).
The components from which ports and connections are built.
void setParameters(const Bytes &header) override
Configure this carrier based on the first 8 bytes of the connection.
Minimal requirements for an efficient Writer.
virtual bool isVocab() const
Checks if value is a vocabulary identifier.
A class for storing options and configuration information.
virtual bool isInt32() const
Checks if value is a 32-bit integer.
HttpTwoWayStream(TwoWayStream *delegate, const char *txt, const char *prefix, yarp::os::Property &prop, bool writer)