bool AbstractCarrier::write(ConnectionState& proto, SizedWriter& writer) { bool ok = sendIndex(proto,writer); if (!ok) { return false; } writer.write(proto.os()); proto.os().flush(); return proto.os().isOk(); }
virtual bool write(Protocol& proto, SizedWriter& writer) { bool ok = sendIndex(proto); if (!ok) return false; writer.write(proto.os()); return proto.os().isOk(); }
bool yarp::os::impl::HttpCarrier::reply(Protocol& proto, SizedWriter& writer) { DummyConnector con; con.setTextMode(true); for (size_t i=writer.headerLength(); i<writer.length(); i++) { con.getWriter().appendBlock(writer.data(i),writer.length(i)); } Bottle b; b.read(con.getReader()); ConstString mime = b.check("mime",Value("text/html")).asString(); ConstString body; bool using_json = false; if (stream!=NULL) { if (stream->useJson()) { mime = "text/json"; asJson(body,&b,stream->typeHint()); using_json = true; } } if (b.check("web")&&!using_json) { body = b.find("web").toString(); } if (b.check("stream")&&!using_json) { String header("HTTP/1.1 200 OK\r\nContent-Type: "); header += mime; header += "\r\n"; header += "Transfer-Encoding: chunked\r\n"; header += "\r\n"; int N = 2*1024; header += NetType::toHexString(body.length()+N); header += "\r\n"; Bytes b2((char*)header.c_str(),header.length()); proto.os().write(b2); // chrome etc won't render until enough chars are received. for (int i=0; i<N; i++) { proto.os().write(' '); } Bytes b3((char*)body.c_str(),body.length()); proto.os().write(b3); proto.os().write('\r'); proto.os().write('\n'); if (stream!=NULL) { stream->flip(); } return true; } if (stream!=NULL) { stream->finish(); } // Could check response codes, mime types here. if (body.length()!=0 || using_json) { ConstString mime = b.check("mime",Value("text/html")).asString(); String header("HTTP/1.1 200 OK\nContent-Type: "); header += mime; header += "\n"; header += "Access-Control-Allow-Origin: *\n"; header += "\n"; Bytes b2((char*)header.c_str(),header.length()); proto.os().write(b2); //body = b.toString(); Bytes b3((char*)body.c_str(),body.length()); proto.os().write(b3); } else { writer.write(proto.os()); } proto.os().flush(); return proto.os().isOk(); }
bool TcpRosCarrier::reply(Protocol& proto, SizedWriter& writer) { // don't need to do anything special for now. writer.write(proto.os()); return proto.os().isOk(); }
bool XmlRpcCarrier::write(Protocol& proto, SizedWriter& writer) { //XmlRpc::setVerbosity(10); StringOutputStream sos; StringInputStream sis; writer.write(sos); sis.reset(sos.toString()); String header; if (sender) { header = NetType::readLine(sis); } String body = NetType::readLine(sis); //printf("Asked to write: hdr %s body %s\n", // header.c_str(), body.c_str()); Value v; //printf("HEADER %s\n", header.c_str()); if (header[0]=='q') { body = "yarp.quit"; // XMLRPC does not need a quit message, this should get stripped return false; } Bottle *bot = v.asList(); //Bottle aux; bot->fromString(body.c_str()); ConstString methodName; if (sender) { methodName = bot->get(0).toString(); *bot = bot->tail(); } XmlRpcValue args; if (bot->size()==1) { toXmlRpcValue(bot->get(0),args); } else { toXmlRpcValue(v,args); } //printf("xmlrpc block to write is %s\n", args.toXml().c_str()); std::string req; if (sender) { const Address& addr = host.isValid()?host:proto.getStreams().getRemoteAddress(); XmlRpcClient c(addr.getName().c_str(),(addr.getPort()>0)?addr.getPort():80); c.generateRequest(methodName.c_str(),args); req = c.getRequest(); } else { XmlRpcServerConnection c(0,NULL); c.generateResponse(args.toXml()); req = c.getResponse(); } int start = 0; //printf("converts to %s\n", req.c_str()); if (sender) { if (req.length()<8) { fprintf(stderr, "XmlRpcCarrier fail, %s:%d\n", __FILE__, __LINE__); return false; } for (int i=0; i<(int)req.length(); i++) { if (req[i] == '\n') { start++; break; } start++; } if (!firstRound) { Bytes b((char*)http.c_str(),http.length()); proto.os().write(b); } firstRound = false; } Bytes b((char*)req.c_str()+start,req.length()-start); //printf("WRITING [%s]\n", req.c_str()+start); proto.os().write(b); return proto.os().isOk(); }
bool Carrier::reply(ConnectionState& proto, SizedWriter& writer) { writer.write(proto.os()); return proto.os().isOk(); }
bool XmlRpcCarrier::write(ConnectionState& proto, SizedWriter& writer) { StringOutputStream sos; StringInputStream sis; writer.write(sos); sis.reset(sos.toString()); ConstString header; if (sender) { header = sis.readLine(); } ConstString body = sis.readLine(); Value v; if (header.length()>0 && header[0]=='q') { body = "yarp.quit"; // XMLRPC does not need a quit message, this should get stripped return false; } Bottle *bot = v.asList(); bot->fromString(body.c_str()); ConstString methodName; if (sender) { methodName = bot->get(0).toString(); *bot = bot->tail(); } XmlRpcValue args; if (bot->size()==1) { toXmlRpcValue(bot->get(0),args); } else { toXmlRpcValue(v,args); } std::string req; if (sender) { const Contact& addr = host.isValid()?host:proto.getStreams().getRemoteAddress(); XmlRpcClient c(addr.getHost().c_str(),(addr.getPort()>0)?addr.getPort():80); c.generateRequest(methodName.c_str(),args); req = c.getRequest(); } else { XmlRpcServerConnection c(0,NULL); c.generateResponse(args.toXml()); req = c.getResponse(); } int start = 0; if (sender) { if (req.length()<8) { fprintf(stderr, "XmlRpcCarrier fail, %s:%d\n", __FILE__, __LINE__); return false; } for (int i=0; i<(int)req.length(); i++) { if (req[i] == '\n') { start++; break; } start++; } if (!firstRound) { Bytes b((char*)http.c_str(),http.length()); proto.os().write(b); } firstRound = false; } Bytes b((char*)req.c_str()+start,req.length()-start); proto.os().write(b); return proto.os().isOk(); }