36 std::string*
hint =
nullptr);
52 }
else if (ch ==
'\r') {
55 }
else if (ch ==
'\0') {
59 if (ch ==
'\\' || ch ==
'\"') {
86 if (
hint !=
nullptr) {
87 if ((*
hint) ==
"list") {
90 }
else if ((*
hint) ==
"dict") {
99 }
else if (tag ==
"dict") {
105 if (bot->
size() > 0) {
118 if (
vi.asList()->size() != 2) {
130 accum +=
"\"type\": ";
134 for (
size_t i = offset; i < bot->
size(); i++) {
136 if (
boti ==
nullptr) {
170 this->isWriter = writer;
185 format = p.
check(
"format",
Value(
"html")).asString();
186 outer = p.
check(
"outer",
Value(
"auto")).asString();
187 bool admin = p.
check(
"admin");
188 bool req = p.
check(
"req");
189 if (p.
check(
"cmd")) {
191 }
else if (p.
check(
"data") || req) {
194 if (!p.
check(
"format")) {
196 p.
put(
"format",
"json");
205 for (
unsigned int i = 0; i < s.length(); i++) {
208 if ((ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9') || (ch ==
'_')) {
213 if (i != s.length() - 1) {
222 if (i != s.length() - 1) {
231 s = std::string(
"a\n") +
sFixed;
233 s = std::string(
"d\n") +
sFixed;
238 std::string from = prefix;
239 from +=
"<input type=text name=data value=\"";
242 from +=
"\"><input type=submit value=\"send data\"></form></p>\n";
247 if (s.length() <= 1) {
250 if (s.length() > 1) {
251 if (s[0] ==
'?' || s[1] ==
'?') {
254 if (s[0] ==
'd' && s[1] ==
'\n') {
258 if (s.length() >= 4) {
259 if (s[0] ==
'f' && s[1] ==
'o' && s[2] ==
'r' && s[3] ==
'm') {
262 if (s[0] ==
'd' && s[1] ==
'a' && s[2] ==
't' && s[3] ==
'a') {
265 if (s[0] ==
'f' && s[1] ==
'a' && s[2] ==
'v' && s[3] ==
'i') {
275 std::string header =
"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n";
278 header +=
"Transfer-Encoding: chunked\r\n";
283 std::string body = from;
285 body +=
"Reading data from port...\n";
290 Bytes b1((
char*)header.c_str(), header.length());
295 for (
int i = 0; i <
N; i++) {
300 Bytes b2((
char*)body.c_str(), body.length());
335 for (
int i = 0; i < (
int)s.length(); i++) {
354 if (delegate !=
nullptr) {
372 return delegate->getLocalAddress();
377 return delegate->getRemoteAddress();
392 if (chunked || isWriter) {
393 delegate->getOutputStream().
write(b);
395 for (
size_t i = 0; i < b.
length(); i++) {
409 if (
addr.isValid()) {
410 if (
addr.getCarrier() ==
"tcp" && (
addr.getRegName().find(
"/quit") == std::string::npos)) {
411 proc +=
"<a href=\"http://";
412 proc +=
addr.getHost();
416 proc +=
addr.getRegName();
418 size_t len =
addr.getRegName().length();
421 for (
size_t i = 0; i < target - len; i++) {
426 proc +=
addr.toString();
435 if ((part[0] ==
'\"' && part[1] ==
'[') || (part[0] ==
'+')) {
437 if (part[0] ==
'+') {
440 std::string
org = part;
441 part =
"<p><form method=post action='/form'>";
443 for (i = 0; i <
org.length(); i++) {
448 part +=
"<input type=hidden name=data value=\"";
455 for (i = 0; i <
org.length(); i++) {
458 if ((ch >=
'A' && ch <=
'Z') || (ch >=
'a' && ch <=
'z') || (ch >=
'0' && ch <=
'9') || (ch ==
'_')) {
465 part +=
"<input type=text name=";
467 part +=
" size=5 value=\"\">";
475 part +=
"<input type=submit value=\"go\">";
476 part +=
"</form></p>";
481 if (data || !filterData) {
482 Bytes tmp((
char*)proc.c_str(), proc.length());
483 delegate->getOutputStream().write(
tmp);
484 delegate->getOutputStream().flush();
487 if (proc[0] ==
'd' || proc[0] ==
'D') {
505 delegate->beginPacket();
510 delegate->endPacket();
525 return format ==
"json";
556 if (header.
length() == 8) {
557 std::string target = prefix;
558 for (
unsigned int i = 0; i < target.length(); i++) {
559 if (!(target[i] == header.
get()[i])) {
570 bool ok = checkHeader(header,
"GET /");
574 ok = checkHeader(header,
"POST /fo");
577 ok = !checkHeader(header,
"GET /?ac") && !checkHeader(header,
"GET /?ws");
584 if (header.
length() == 8) {
586 for (
unsigned int j = 0;
j < 8;
j++) {
587 char ch = header.
get()[
j];
605 if (header.
length() == 8) {
606 std::string target =
"GET / HT";
607 for (
int i = 0; i < 8; i++) {
608 header.
get()[i] = target[i];
631 std::string target =
"GET / HTTP/1.0\r\n";
632 std::string path = proto.
getRoute().getToName();
633 if (path.size() >= 2) {
634 target =
"GET " + path +
" HTTP/1.0\r\n";
643 Bytes b((
char*)target.c_str(), target.length());
669 if (result.empty()) {
681 if (b.
get(1).
asString() ==
"application/x-www-form-urlencoded") {
691 Bytes start(
blk.get(), contentLength);
693 blk.get()[contentLength] =
'\0';
700 prop.fromQuery(input.c_str());
701 prop.put(
"REQUEST_URI", url);
707 std::string from =
"<html><head><link href=\"http://";
708 from += home.getHost();
711 from += R
"(/web/main.css" rel="stylesheet" type="text/css"/></head><body bgcolor='#ffffcc'><h1>yarp port )";
712 from += proto.getRoute().getToName();
715 from +=
"<p>(<a href=\"http://";
716 from += home.getHost();
719 from +=
"/data=list\">All ports</a>) \n";
721 from +=
"(<a href=\"http://";
722 from +=
me.getHost();
725 from +=
"/\">connections</a>) \n";
727 from +=
"(<a href=\"http://";
728 from +=
me.getHost();
731 from +=
"/data=help\">help</a>) \n";
733 from +=
"(<a href=\"http://";
734 from +=
me.getHost();
737 from +=
"/r\">read</a>) \n";
741 from += R
"(<form method="post" action="http://)";
742 from += me.getHost();
764 Bytes b((
char*)&buf[0], 1);
768 input += std::string(buf, len);
832 if (body.length() != 0) {
837 Bytes b2((
char*)header.c_str(), header.length());
840 Bytes b3((
char*)body.c_str(), body.length());
847 std::string txt = b.
toString() +
"\r\n";
851 Bytes b2((
char*)header.c_str(), header.length());
853 Bytes b3((
char*)txt.c_str(), txt.length());
872 std::string mime = b.
check(
"mime",
Value(
"text/html")).asString();
877 if (stream !=
nullptr) {
878 if (stream->useJson()) {
880 asJson(body, &b, stream->typeHint());
890 std::string header(
"HTTP/1.1 200 OK\r\nContent-Type: ");
893 header +=
"Transfer-Encoding: chunked\r\n";
899 Bytes b2((
char*)header.c_str(), header.length());
903 for (
int i = 0; i <
N; i++) {
907 Bytes b3((
char*)body.c_str(), body.length());
914 if (stream !=
nullptr) {
920 if (stream !=
nullptr) {
927 std::string mime = b.
check(
"mime",
Value(
using_json ?
"application/json" :
"text/html")).asString();
928 std::string header(
"HTTP/1.1 200 OK\nContent-Type: ");
931 header +=
"Access-Control-Allow-Origin: *\n";
933 Bytes b2((
char*)header.c_str(), header.length());
937 Bytes b3((
char*)body.c_str(), body.length());
static bool asJson(std::string &accum, yarp::os::Bottle *bot, std::string *hint=nullptr)
static std::string quoteFree(const std::string &src)
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
size_type size() const
Gets the number of elements in the bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
A mini-server for performing network communication in the background.
void write(bool forceStrict=false)
Write the current object being returned by BufferedPort::prepare.
A simple abstraction for a block of bytes.
A base class for connection types (tcp, mcast, shmem, ...) which are called carriers in YARP.
The basic state of a connection - route, streams in use, etc.
OutputStream & os()
Shorthand for getOutputStream()
virtual const Route & getRoute() const =0
Get the route associated with this connection.
virtual TwoWayStream * giveStreams()=0
Take ownership of the streams associated with the connection.
InputStream & is()
Shorthand for getInputStream()
virtual void takeStreams(TwoWayStream *streams)=0
Provide streams to be used with the connection.
virtual TwoWayStream & getStreams()=0
Access the streams associated with the connection.
virtual void setRoute(const Route &route)=0
Set the route associated with this connection.
virtual void appendBlock(const char *data, size_t len)=0
Send a block of data to the network connection.
A dummy connection to test yarp::os::Portable implementations.
ConnectionWriter & getWriter()
Get the dummy ConnectionWriter loaded with whatever was written the ConnectionWriter since it was las...
void setTextMode(bool textmode)
Set the textMode of the dummy connection.
ConnectionReader & getReader(ConnectionWriter *replyWriter=nullptr)
Get the dummy ConnectionReader loaded with whatever was written the ConnectionWriter since it was las...
An abstraction for a block of bytes, with optional responsibility for allocating/destroying that bloc...
static Contact getNameServerContact()
Get the contact information for the port associated with the nameserver (usually "/root",...
Simple specification of the minimum functions needed from output streams.
virtual void flush()
Make sure all pending write operations are finished.
virtual bool isOk() const =0
Check if the stream is ok or in an error state.
virtual void write(char ch)
Write a single byte to the stream.
A class for storing options and configuration information.
std::string toString() const override
Return a standard text representation of the content of the object.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Information about a connection between two ports.
void setFromName(const std::string &fromName)
Set the source of the route.
Minimal requirements for an efficient Writer.
virtual void write(OutputStream &os)
virtual const char * data(size_t index) const =0
virtual size_t headerLength() const =0
virtual size_t length() const =0
A stream which can be asked to perform bidirectional communication.
virtual OutputStream & getOutputStream()=0
Get an OutputStream to write to.
A single value (typically within a Bottle).
virtual bool isString() const
Checks if value is a string.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual bool isList() const
Checks if value is a list.
virtual Bottle * asList() const
Get list value.
virtual bool isFloat64() const
Checks if value is a 64-bit floating point number.
std::string toString() const override
Return a standard text representation of the content of the object.
virtual bool isInt32() const
Checks if value is a 32-bit integer.
virtual bool isVocab32() const
Checks if value is a vocabulary identifier.
virtual std::string asString() const
Get string value.
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.
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.
virtual ~HttpTwoWayStream()
void write(const Bytes &b) override
Write a block of bytes to the stream.
void close() override
Terminate the stream.
static Contact extractAddress(const std::string &txt)
Extract an address from its text representation.
std::string to_hex_string(IntegerType i)
std::string to_string(IntegerType x)
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.