cursor = db.query( ns.c_str() , query, num , skip );
uassert( 13085 , "query failed for dbwebserver" , cursor.get() );
if ( one ) {
if ( cursor->more() ) {
BSONObj obj = cursor->next();
out << obj.jsonString(Strict,html?1:0) << '\n';
}
else {
responseCode = 404;
}
return html != 0;
}
if( html ) {
string title = string("query ") + ns;
out << start(title)
<< p(title)
<< "";
}
else {
out << "{\n";
out << " \"offset\" : " << skip << ",\n";
out << " \"rows\": [\n";
}
int howMany = 0;
while ( cursor->more() ) {
if ( howMany++ && html == 0 )
out << " ,\n";
BSONObj obj = cursor->next();
if( html ) {
if( out.tellp() > 4 * 1024 * 1024 ) {
out << "Stopping output: more than 4MB returned and in html mode\n";
break;
}
out << obj.jsonString(Strict, html?1:0) << "\n\n";
}
else {
if( out.tellp() > 50 * 1024 * 1024 ) // 50MB limit - we are using ram
break;
out << " " << obj.jsonString();
}
}
if( html ) {
out << "
\n";
if( howMany == 0 ) out << p("Collection is empty");
out << _end();
}
else {
out << "\n ],\n\n";
out << " \"total_rows\" : " << howMany << " ,\n";
out << " \"query\" : " << query.jsonString() << " ,\n";
out << " \"millis\" : " << t.millis() << '\n';
out << "}\n";
}
return html != 0;
}
// TODO Generate id and revision per couch POST spec
void handlePost( const std::string& ns,
const char *body,
BSONObj& params,
int & responseCode,
stringstream & out ) {
try {
BSONObj obj = fromjson( body );
db.insert( ns.c_str(), obj );
}
catch ( ... ) {
responseCode = 400; // Bad Request. Seems reasonable for now.
out << "{ \"ok\" : false }";
return;
}
responseCode = 201;
out << "{ \"ok\" : true }";
}
int _getOption( BSONElement e , int def ) {
if ( e.isNumber() )
return e.numberInt();
if ( e.type() == String )
return atoi( e.valuestr() );
return def;
}
DBDirectClient db;
} restHandler;
bool RestAdminAccess::haveAdminUsers() const {
AuthorizationSession* authzSession = cc().getAuthorizationSession();
return authzSession->getAuthorizationManager().hasAnyPrivilegeDocuments();
}
class LowLevelMongodStatus : public WebStatusPlugin {
public:
LowLevelMongodStatus() : WebStatusPlugin( "overview" , 5 , "(only reported if can acquire read lock quickly)" ) {}
virtual void init() {}
void _gotLock( int millis , stringstream& ss ) {
ss << "\n";
ss << "time to get readlock: " << millis << "ms\n";
ss << "# databases: " << dbHolder().sizeInfo() << '\n';
ss << "# Cursors: " << ClientCursor::totalOpen() << '\n';
ss << "replication: ";
if( *replInfo )
ss << "\nreplInfo: " << replInfo << "\n\n";
if( replSet ) {
ss << a("", "see replSetGetStatus link top of page") << "--replSet " << replSettings.replSet;
}
if ( replAllDead )
ss << "\nreplication replAllDead=" << replAllDead << "\n";
else {
ss << "\nmaster: " << replSettings.master << '\n';
ss << "slave: " << replSettings.slave << '\n';
ss << '\n';
}
BackgroundOperation::dump(ss);
ss << "
\n";
}
virtual void run( stringstream& ss ) {
Timer t;
readlocktry lk( 300 );
if ( lk.got() ) {
_gotLock( t.millis() , ss );
}
else {
ss << "\ntimed out getting lock\n";
}
}
} lowLevelMongodStatus;
}