Beispiel #1
0
    DbWebHandler * DbWebHandler::findHandler( const string& url ) {
        if ( ! _handlers )
            return 0;

        for ( unsigned i=0; i<_handlers->size(); i++ ) {
            DbWebHandler * h = (*_handlers)[i];
            if ( h->handles( url ) )
                return h;
        }

        return 0;
    }
Beispiel #2
0
        virtual void doRequest(
            const char *rq, // the full request
            string url,
            // set these and return them:
            string& responseMsg,
            int& responseCode,
            vector<string>& headers, // if completely empty, content-type: text/html will be added
            const SockAddr &from
        ) {
            if ( url.size() > 1 ) {

                if ( ! allowed( rq , headers, from ) ) {
                    responseCode = 401;
                    headers.push_back( "Content-Type: text/plain;charset=utf-8" );
                    responseMsg = "not allowed\n";
                    return;
                }

                {
                    BSONObj params;
                    const size_t pos = url.find( "?" );
                    if ( pos != string::npos ) {
                        MiniWebServer::parseParams( params , url.substr( pos + 1 ) );
                        url = url.substr(0, pos);
                    }

                    DbWebHandler * handler = DbWebHandler::findHandler( url );
                    if ( handler ) {
                        if ( handler->requiresREST( url ) && ! cmdLine.rest ) {
                            _rejectREST( responseMsg , responseCode , headers );
                        }
                        else {
                            string callback = params.getStringField("jsonp");
                            uassert(13453, "server not started with --jsonp", callback.empty() || cmdLine.jsonp);

                            handler->handle( rq , url , params , responseMsg , responseCode , headers , from );

                            if (responseCode == 200 && !callback.empty()) {
                                responseMsg = callback + '(' + responseMsg + ')';
                            }
                        }
                        return;
                    }
                }


                if ( ! cmdLine.rest ) {
                    _rejectREST( responseMsg , responseCode , headers );
                    return;
                }

                responseCode = 404;
                headers.push_back( "Content-Type: text/html;charset=utf-8" );
                responseMsg = "<html><body>unknown url</body></html>\n";
                return;
            }

            // generate home page

            if ( ! allowed( rq , headers, from ) ) {
                responseCode = 401;
                headers.push_back( "Content-Type: text/plain;charset=utf-8" );
                responseMsg = "not allowed\n";
                return;
            }

            responseCode = 200;
            stringstream ss;
            string dbname;
            {
                stringstream z;
                z << cmdLine.binaryName << ' ' << prettyHostName();
                dbname = z.str();
            }
            ss << start(dbname) << h2(dbname);
            ss << "<p><a href=\"/_commands\">List all commands</a> | \n";
            ss << "<a href=\"/_replSet\">Replica set status</a></p>\n";

            //ss << "<a href=\"/_status\">_status</a>";
            {
                const map<string, Command*> *m = Command::webCommands();
                if( m ) {
                    ss <<
                       a("",
                         "These read-only context-less commands can be executed from the web interface. "
                         "Results are json format, unless ?text=1 is appended in which case the result is output as text "
                         "for easier human viewing",
                         "Commands")
                       << ": ";
                    for( map<string, Command*>::const_iterator i = m->begin(); i != m->end(); i++ ) {
                        stringstream h;
                        i->second->help(h);
                        string help = h.str();
                        ss << "<a href=\"/" << i->first << "?text=1\"";
                        if( help != "no help defined" )
                            ss << " title=\"" << help << '"';
                        ss << ">" << i->first << "</a> ";
                    }
                    ss << '\n';
                }
            }
            ss << '\n';
            /*
                ss << "HTTP <a "
                    "title=\"click for documentation on this http interface\""
                    "href=\"http://dochub.mongodb.org/core/httpinterface\">admin port</a>:" << _port << "<p>\n";
            */

            doUnlockedStuff(ss);

            WebStatusPlugin::runAll( ss );

            ss << "</body></html>\n";
            responseMsg = ss.str();
            headers.push_back( "Content-Type: text/html;charset=utf-8" );
        }
Beispiel #3
0
void DbWebServer::doRequest(const char* rq,
                            string url,
                            string& responseMsg,
                            int& responseCode,
                            vector<string>& headers,
                            const SockAddr& from) {
    Client* client = &cc();
    auto txn = client->makeOperationContext();

    if (url.size() > 1) {
        if (!_allowed(txn.get(), rq, headers, from)) {
            responseCode = 401;
            headers.push_back("Content-Type: text/plain;charset=utf-8");
            responseMsg = "not allowed\n";
            return;
        }

        {
            BSONObj params;
            const size_t pos = url.find("?");
            if (pos != string::npos) {
                MiniWebServer::parseParams(params, url.substr(pos + 1));
                url = url.substr(0, pos);
            }

            DbWebHandler* handler = DbWebHandler::findHandler(url);
            if (handler) {
                if (handler->requiresREST(url) && !serverGlobalParams.rest) {
                    _rejectREST(responseMsg, responseCode, headers);
                } else {
                    const string callback = params.getStringField("jsonp");

                    uassert(13453,
                            "server not started with --jsonp",
                            callback.empty() || serverGlobalParams.jsonp);

                    handler->handle(
                        txn.get(), rq, url, params, responseMsg, responseCode, headers, from);

                    if (responseCode == 200 && !callback.empty()) {
                        responseMsg = callback + '(' + responseMsg + ')';
                    }
                }

                return;
            }
        }

        if (!serverGlobalParams.rest) {
            _rejectREST(responseMsg, responseCode, headers);
            return;
        }

        responseCode = 404;
        headers.push_back("Content-Type: text/html;charset=utf-8");
        responseMsg = "<html><body>unknown url</body></html>\n";
        return;
    }

    // generate home page

    if (!_allowed(txn.get(), rq, headers, from)) {
        responseCode = 401;
        headers.push_back("Content-Type: text/plain;charset=utf-8");
        responseMsg = "not allowed\n";
        return;
    }

    responseCode = 200;
    stringstream ss;
    string dbname;
    {
        stringstream z;
        z << serverGlobalParams.binaryName << ' ' << prettyHostName();
        dbname = z.str();
    }

    ss << start(dbname) << h2(dbname);
    ss << "<p><a href=\"/_commands\">List all commands</a> | \n";
    ss << "<a href=\"/_replSet\">Replica set status</a></p>\n";

    ss << a("",
            "These read-only context-less commands can be executed from the web "
            "interface. Results are json format, unless ?text=1 is appended in which "
            "case the result is output as text for easier human viewing",
            "Commands") << ": ";

    auto m = Command::commandsByBestName();

    for (Command::CommandMap::const_iterator i = m->begin(); i != m->end(); ++i) {
        if (!i->second->isWebUI())
            continue;

        stringstream h;
        i->second->help(h);

        const string help = h.str();
        ss << "<a href=\"/" << i->first << "?text=1\"";
        if (help != "no help defined") {
            ss << " title=\"" << help << '"';
        }

        ss << ">" << i->first << "</a> ";
    }

    ss << '\n';

    doUnlockedStuff(ss);

    WebStatusPlugin::runAll(txn.get(), ss);

    ss << "</body></html>\n";
    responseMsg = ss.str();
    headers.push_back("Content-Type: text/html;charset=utf-8");
}