コード例 #1
0
ファイル: HttpCarrier.cpp プロジェクト: paulfitz/yarp
static bool asJson(yarp::os::ConstString &accum,
                   yarp::os::Value &v) {
    if (v.isInt()||v.isDouble()) {
        accum += v.toString();
        return true;
    }
    if (v.isString()||v.isVocab()) {
        yarp::os::ConstString x = v.toString();
        accum += "\"";
        for (int j=0; j<(int)x.length(); j++) {
            char ch = x[j];
            if (ch=='\n') {
                accum += '\\';
                accum += 'n';
            } else if (ch=='\r') {
                accum += '\\';
                accum += 'r';
            } else if (ch=='\0') {
                accum += '\\';
                accum += '0';
            } else {
                if (ch=='\\'||ch=='\"') {
                    accum += '\\';
                }
                accum += ch;
            }
        }
        accum += "\"";
    }
    if (v.isList()) {
        yarp::os::Bottle *bot = v.asList();
        return asJson(accum,bot);
    }
    return false;
}
コード例 #2
0
ファイル: HttpCarrier.cpp プロジェクト: paulfitz/yarp
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();
}
コード例 #3
0
ファイル: HttpCarrier.cpp プロジェクト: paulfitz/yarp
static bool asJson(yarp::os::ConstString& accum,
                   yarp::os::Bottle *bot,
                   yarp::os::impl::String *hint) {
    if (bot==NULL) return false;
    bool struc = false;
    bool struc_set = false;
    int offset = 0;
    int offset2 = 0;
    yarp::os::ConstString tag = bot->get(0).asString();
    if (hint) {
        if ((*hint)=="list") {
            struc = false;
            struc_set = true;
        } else if ((*hint)=="dict") {
            struc = true;
            struc_set = true;
        }
    }
    if (!struc_set) {
        if (tag=="list") {
            struc = false;
            offset = 1;
        } else if (tag=="dict") {
            struc = true;
            offset = 1;
        } else {
            // auto-detect
            struc = (bot->size()>1);
            if (bot->size()>0) {
                yarp::os::Value& v0 = bot->get(0);
                if (!v0.isList()) {
                    offset2 = 1;
                    offset = 1;
                }
            }
            for (int i=offset2; i<bot->size(); i++) {
                yarp::os::Value& vi = bot->get(i);
                if (!vi.isList()) {
                    struc = false;
                    break;
                }
                if (vi.asList()->size()!=2) {
                    struc = false;
                    break;
                }
            }
        }
    }
    if (struc) {
        // { ... }
        accum += "{";
        bool need_comma = false;
        if (offset2) {
            accum += "\"type\": ";
            asJson(accum,bot->get(0));
            need_comma = true;
        }
        for (int i=offset; i<bot->size(); i++) {
            yarp::os::Bottle *boti = bot->get(i).asList();
            if (boti==NULL) continue;
            if (need_comma) {
                accum += ", ";
            }
            asJson(accum,boti->get(0));
            accum += ": ";
            asJson(accum,boti->get(1));
            need_comma = true;
        }
        accum += "}";
        return true;
    }

    // [ ... ]
    accum += "[";
    if (offset2) offset--;
    for (int i=offset; i<bot->size(); i++) {
        if (i>offset) {
            accum += ", ";
        }
        asJson(accum,bot->get(i));
    }
    accum += "]";
    return true;
}
コード例 #4
0
std::string FileProperties::allPropertiesAsJson() const
{
    json::JsonObjectStreamWriter writer;
    {
        // format
        json::JsonArrayStreamWriter format;
        format << asJson();
        writer << std::make_pair("format", format.build());
    }
    {
        // video streams
        json::JsonArrayStreamWriter video;
        for(std::vector<avtranscoder::VideoProperties>::const_iterator it = _videoStreams.begin(); it != _videoStreams.end();
            ++it)
        {
            video << it->asJson();
        }
        writer << std::make_pair("video", video.build());
    }
    {
        // audio streams
        json::JsonArrayStreamWriter audio;
        for(std::vector<avtranscoder::AudioProperties>::const_iterator it = _audioStreams.begin(); it != _audioStreams.end();
            ++it)
        {
            audio << it->asJson();
        }
        writer << std::make_pair("audio", audio.build());
    }
    {
        // data streams
        json::JsonArrayStreamWriter data;
        for(std::vector<avtranscoder::DataProperties>::const_iterator it = _dataStreams.begin(); it != _dataStreams.end();
            ++it)
        {
            data << it->asJson();
        }
        writer << std::make_pair("data", data.build());
    }
    {
        // subtitle streams
        json::JsonArrayStreamWriter subtitle;
        for(std::vector<avtranscoder::SubtitleProperties>::const_iterator it = _subtitleStreams.begin();
            it != _subtitleStreams.end(); ++it)
        {
            subtitle << it->asJson();
        }
        writer << std::make_pair("subtitle", subtitle.build());
    }
    {
        // attachement streams
        json::JsonArrayStreamWriter attachement;
        for(std::vector<avtranscoder::AttachementProperties>::const_iterator it = _attachementStreams.begin();
            it != _attachementStreams.end(); ++it)
        {
            attachement << it->asJson();
        }
        writer << std::make_pair("attachement", attachement.build());
    }
    {
        // unknown streams
        json::JsonArrayStreamWriter unknown;
        for(std::vector<avtranscoder::UnknownProperties>::const_iterator it = _unknownStreams.begin();
            it != _unknownStreams.end(); ++it)
        {
            unknown << it->asJson();
        }
        writer << std::make_pair("unknown", unknown.build());
    }
    return writer.build();
}