Ejemplo n.º 1
0
bool RESTHandler::build_message(Poco::Net::HTTPServerRequest &request, Poco::Net::HTMLForm &form, Poco::URI &url,
                                zmqpp::message &msg) {

    Json::Value root;
    bool ok = false;
    /// Find 'args' param in query or as POST body
    if (form.has("args")) {
        ok = reader.parse(form.get("args"), root);
    } else if (request.getMethod() == Poco::Net::HTTPRequest::HTTP_POST) {
        ok = reader.parse(request.stream(), root);
    }
    if (!ok || !root.isArray()) {
        return false;
    }
    if (verbose) {
        std::clog << "0\t" << url.getPath().substr(1) << std::endl;
    }
    /// Get service name as path without leading slash
    msg << url.getPath().substr(1);
    for (size_t i = 0; i < root.size(); ++i) {
        auto val = root.get(i, "");
        if (!verbose)
            msg << (val.isString() ? root.get(i, "").asString() : val.toStyledString());
        else {
            std::string s = (val.isString() ? root.get(i, "").asString() : val.toStyledString());
            msg << s;
            std::clog << (i + 1) << '\t' << s << std::endl;
        }
    }
    return true;
}
Ejemplo n.º 2
0
bool FileSystemRoute::canHandleRequest(const Poco::Net::HTTPServerRequest& request,
                                       bool isSecurePort) const
{
    // require an HTTP_GET call
    return request.getMethod() == Poco::Net::HTTPRequest::HTTP_GET &&
        BaseRoute::canHandleRequest(request, isSecurePort);
}
Ejemplo n.º 3
0
   /* virtual*/ void handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp)
    {
        resp.setStatus(Poco::Net::HTTPResponse::HTTP_OK);       //Sets the HTTP status code, Why?
        resp.setContentType("text/html");                       // set the content type of the message

        ostream& out = resp.send();        //Returns an output stream for sending the response body. The returned stream is valid until the response object is destroyed.
        out << "<h1>Hello world!</h1>"     //Body of the repsonse
       // << "<p>Count: "  << ++count         << "</p>"
        << "<p>Host: "   << req.getHost()   << "</p>"       //Returns the value of the Host header field.
        << "<p>Method: " << req.getMethod() << "</p>"
        << "<p>URI: "    << req.getURI() << "</p>";
        out.flush();
        cout << endl
        //<< "Response sent for count=" << count
        << " Response sent for URI=" << req.getURI() << endl;
    }
Ejemplo n.º 4
0
void
RemoveByAttributesHandler::handleRequest(Poco::Net::HTTPServerRequest &req, Poco::Net::HTTPServerResponse &resp)
{
    prepareApiResponse(resp);

    if (req.getMethod() != "POST") {
        resp.setStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
        resp.setReason("Bad Request");

        Error error("Only POST requests are supported");

        std::ostream & out = resp.send();
        out << error;
        out.flush();
        return;
    }


    auto job = std::make_shared<Job>(Job::Type::REMOVE_BY_ATTRIBUTES);
    try {
        // read the whole request body into memory
        std::stringstream bodystream;
        bodystream << req.stream().rdbuf();

        // parse received json into the job object
        job->fromString( bodystream.str() );
    }
    catch (std::exception &e) {
        resp.setStatus(Poco::Net::HTTPResponse::HTTP_BAD_REQUEST);
        resp.setReason("Bad Request");
        
        Error error(e.what());
        poco_warning(logger, e.what());

        std::ostream & out = resp.send();
        out << error;
        out.flush();
        return;
    }

    // return 404 if layer does not exist
    try {
        auto layer = configuration->getLayer(job->getLayerName());
    }
    catch (ConfigurationError) {
        // layer does not exist
        resp.setStatus(Poco::Net::HTTPResponse::HTTP_NOT_FOUND);
        resp.setReason("Not Found");
        
        std::stringstream msgstream;
        msgstream << "Layer \"" << job->getLayerName() << "\" does not exist";

        Error error(msgstream.str());
        poco_warning(logger, error.getMessage());

        std::ostream & out = resp.send();
        out << error;
        out.flush();
        return;
    }


    if (auto jobstorage = jobs.lock()) {
        poco_debug(logger, "pushing job to jobstorage");
        jobstorage->push(job);
    }
    else {
        resp.setStatus(Poco::Net::HTTPResponse::HTTP_INTERNAL_SERVER_ERROR);
        resp.setReason("Internal Server Error");
        
        const char * emsg = "Could not get a lock on jobstorage";
        Error error(emsg);
        poco_error(logger, emsg);

        std::ostream & out = resp.send();
        out << error;
        out.flush();
        return;
    }

    resp.setStatus(Poco::Net::HTTPResponse::HTTP_OK);
    std::ostream & out = resp.send();
    out << *job;
    out.flush();
};
Ejemplo n.º 5
0
	void handleRequest(Poco::Net::HTTPServerRequest& request, Poco::Net::HTTPServerResponse& response)
	{
		//Poco::Util::Application& app = Poco::Util::Application::instance();
		//app.logger().information("Request from " + request.clientAddress().toString());
        std::cout << "Request from " << request.clientAddress().toString() << std::endl;

        MyPartHandler partHandler;
        Poco::Net::HTMLForm form(request, request.stream(), partHandler);

        std::string spinToken, sceneString, nodeString, args;
        std::istringstream pathstream(request.getURI());
        pathstream.get(); // ignore leading slash
        getline(pathstream, spinToken, '/');
        getline(pathstream, sceneString, '/');
        getline(pathstream, nodeString, '?');

        if (sceneString.empty()) sceneString = "default";
        //if (nodeString.empty()) nodeString = "shp";
        if (form.empty()) args = "createNode shp ShapeNode";
        else args = form["args"];

        response.setChunkedTransferEncoding(true);
        response.setContentType("text/html");

        std::ostream& ostr = response.send();

        ostr <<
        "<html>\n"
        "<head>\n"
        "<title>SPIN Web Service</title>\n"
        "</head>\n"
        "<body>\n"
        "<h1>SPIN Web Service</h1>\n"
        "<h3>Enter a SPIN command in the form below:</h3>\n"
        "<table><tr><td nowrap=\"nowrap\">\n"
        "<form name=\"urlForm\" method=\"GET\" action=\"null\">\n"
        "/SPIN/"
        "<input type=\"text\" name=\"sceneID\" value=\"" << sceneString << "\" size=\"10\">\n"
        "/<input type=\"text\" name=\"nodeID\" value=\"" << nodeString << "\" size=\"10\">"
        "</form></td>\n"
        "<td nowrap=\"nowrap\">\n"
        "<form name=\"spinform\" method=\"GET\" action=\"null\">\n"
        "<input type=\"text\" name=\"args\" value=\"" << args << "\" size=\"20\">\n"
        "<input type=\"submit\" value=\"GO\" onclick=\"this.form.action='/SPIN/'+document.forms['urlForm']['sceneID'].value+'/'+document.forms['urlForm']['nodeID'].value\">\n"
        "</form>\n"
        "</tr></table>\n"
        "<p>(NOTE: you can send scene messages by leaving the node name blank)</p>\n"
        "\n";
        /*
        ostr <<
            "<html>\n"
            "<head>\n"
            "<title>SPIN Web Server Sample</title>\n"
            "</head>\n"
            "<body>\n"
            "<h1>SPIN Web Server Sample</h1>\n"
            "<h2>Tests:</h2>\n"
            "<form name=\"spinform\" method=\"GET\" action=\"null\">\n"
            "/SPIN/default/"
            "<input type=\"text\" name=\"nodeID\" value=\"shp\" size=\"15\">"
            "&nbsp;&nbsp;&nbsp;"
            "<input type=\"text\" name=\"method\" value=\"rotate\" size=\"15\">"
            " move<input type=\"text\" name=\"x\" value=\"0\" size=\"3\">"
            " <input type=\"text\" name=\"y\" value=\"0\" size=\"3\">"
            " <input type=\"text\" name=\"z\" value=\"10\" size=\"3\">\n"
            " <input type=\"submit\" value=\"GO\" onclick=\"this.form.action='/SPIN/default/'+this.form.nodeID.value\">\n"
            "</form>\n"
            "\n";
        ostr <<
            "<html>\n"
            "<head>\n"
            "<title>SPIN Web Server Sample</title>\n"
            "</head>\n"
            "<body>\n"
            "<h1>SPIN Web Server Sample</h1>\n"
            "<h2>GET Form</h2>\n"
            "<form method=\"GET\" action=\"/form\">\n"
            "<input type=\"text\" name=\"text\" size=\"31\">\n"
            "<input type=\"submit\" value=\"GET\">\n"
            "</form>\n"
            "<h2>POST Form</h2>\n"
            "<form method=\"POST\" action=\"/form\">\n"
            "<input type=\"text\" name=\"text\" size=\"31\">\n"
            "<input type=\"submit\" value=\"POST\">\n"
            "</form>\n"
            "<h2>File Upload</h2>\n"
            "<form method=\"POST\" action=\"/form\" enctype=\"multipart/form-data\">\n"
            "<input type=\"file\" name=\"file\" size=\"31\"> \n"
            "<input type=\"submit\" value=\"Upload\">\n"
            "</form>\n";
            */

        ostr << "<h2>Result</h2><p>\n";
        ostr << "Method: " << request.getMethod() << "<br>\n";
        ostr << "URI: " << request.getURI() << "<br>\n";
        Poco::Net::NameValueCollection::ConstIterator it = request.begin();
        Poco::Net::NameValueCollection::ConstIterator end = request.end();
        for (; it != end; ++it)
        {
            ostr << it->first << ": " << it->second << "<br>\n";
        }
        ostr << "</p>";
        /*
        if (!form.empty())
        {
            ostr << "<h2>Result</h2><p>\n";
            it = form.begin();
            end = form.end();
            for (; it != end; ++it)
            {
                ostr << it->first << ": " << it->second << "<br>\n";
            }
            ostr << "</p>";
        }
        */

        // --------parse

        introspect_invoke(request.getURI(), form);

        // ---------------

        if (!partHandler.name().empty())
        {
            ostr << "<h2>Upload</h2><p>\n";
            ostr << "Name: " << partHandler.name() << "<br>\n";
            ostr << "File Name: " << partHandler.fileName() << "<br>\n";
            ostr << "Type: " << partHandler.contentType() << "<br>\n";
            ostr << "Size: " << partHandler.length() << "<br>\n";
            ostr << "</p>";
        }
        ostr << "</body>\n";
    }