1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
// dbwebserver.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "../util/miniwebserver.h"
#include "../util/md5.hpp"
#include "db.h"
#include "repl.h"
#include "replset.h"
#include "instance.h"
#include "security.h"
#include <pcrecpp.h>
namespace mongo {
extern int port;
extern const char *replInfo;
bool getInitialSyncCompleted();
time_t started = time(0);
/*
string toString() {
stringstream ss;
unsigned long long dt = last - start;
ss << dt/1000;
ss << '\t';
ss << timeLocked/1000 << '\t';
if( dt )
ss << (timeLocked*100)/dt << '%';
return ss.str();
}
*/
struct Timing {
Timing() {
start = timeLocked = 0;
}
unsigned long long start, timeLocked;
};
Timing tlast;
const int NStats = 32;
string lockStats[NStats];
unsigned q = 0;
extern bool cpu;
void statsThread() {
unsigned long long timeLastPass = 0;
while ( 1 ) {
{
Timer lktm;
dblock lk;
q = (q+1)%NStats;
Timing timing;
dbMutexInfo.timingInfo(timing.start, timing.timeLocked);
unsigned long long now = curTimeMicros64();
if ( timeLastPass ) {
unsigned long long dt = now - timeLastPass;
unsigned long long dlocked = timing.timeLocked - tlast.timeLocked;
{
stringstream ss;
ss << dt / 1000 << '\t';
ss << dlocked / 1000 << '\t';
if ( dt )
ss << (dlocked*100)/dt << '%';
string s = ss.str();
if ( cpu )
log() << "cpu: " << s << endl;
lockStats[q] = s;
}
}
timeLastPass = now;
tlast = timing;
}
sleepsecs(4);
}
}
unsigned byLocSize();
bool _bold;
string bold(bool x) {
_bold = x;
return x ? "<b>" : "";
}
string bold() {
return _bold ? "</b>" : "";
}
class DbWebServer : public MiniWebServer {
public:
// caller locks
void doLockedStuff(stringstream& ss) {
ss << "# databases: " << databases.size() << '\n';
if ( database ) {
ss << "curclient: " << database->name;
ss << '\n';
}
ss << bold(byLocSize()>10000) << "Cursors byLoc.size(): " << byLocSize() << bold() << '\n';
ss << "\n<b>replication</b>\n";
ss << "master: " << master << '\n';
ss << "slave: " << slave << '\n';
if ( replPair ) {
ss << "replpair:\n";
ss << replPair->getInfo();
}
bool seemCaughtUp = getInitialSyncCompleted();
if ( !seemCaughtUp ) ss << "<b>";
ss << "initialSyncCompleted: " << seemCaughtUp;
if ( !seemCaughtUp ) ss << "</b>";
ss << '\n';
ss << "\n<b>dt\ttlocked</b>\n";
unsigned i = q;
while ( 1 ) {
ss << lockStats[i] << '\n';
i = (i-1)%NStats;
if ( i == q )
break;
}
}
void doUnlockedStuff(stringstream& ss) {
ss << "port: " << port << '\n';
ss << "dblocked: " << dbMutexInfo.isLocked() << " (initial)\n";
ss << "uptime: " << time(0)-started << " seconds\n";
if ( replAllDead )
ss << "<b>replication replAllDead=" << replAllDead << "</b>\n";
ss << "\nassertions:\n";
for ( int i = 0; i < 4; i++ ) {
if ( lastAssert[i].isSet() ) {
ss << "<b>";
if ( i == 3 ) ss << "usererr";
else ss << i;
ss << "</b>" << ' ' << lastAssert[i].toString();
}
}
ss << "\nreplInfo: " << replInfo << '\n';
}
bool allowed( const char * rq , vector<string>& headers ){
if ( db.findOne( "admin.system.users" , emptyObj ).isEmpty() )
return true;
string auth = getHeader( rq , "Authorization" );
if ( auth.size() > 0 && auth.find( "Digest " ) == 0 ){
auth = auth.substr( 7 ) + ", ";
map<string,string> parms;
pcrecpp::StringPiece input( auth );
string name, val;
pcrecpp::RE re("(\\w+)=\"?(.*?)\"?, ");
while ( re.Consume( &input, &name, &val) ){
parms[name] = val;
}
BSONObj user = db.findOne( "admin.system.users" , BSON( "user" << parms["username"] ) );
if ( ! user.isEmpty() ){
string ha1 = user["pwd"].str();
string ha2 = md5simpledigest( (string)"GET" + ":" + parms["uri"] );
string r = ha1 + ":" + parms["nonce"];
if ( parms["nc"].size() && parms["cnonce"].size() && parms["qop"].size() ){
r += ":";
r += parms["nc"];
r += ":";
r += parms["cnonce"];
r += ":";
r += parms["qop"];
}
r += ":";
r += ha2;
r = md5simpledigest( r );
if ( r == parms["response"] )
return true;
}
}
stringstream authHeader;
authHeader
<< "WWW-Authenticate: "
<< "Digest realm=\"mongo\", "
<< "nonce=\"abc\", "
<< "algorithm=MD5, qop=\"auth\" "
;
headers.push_back( authHeader.str() );
return 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
)
{
//out() << "url [" << url << "]" << endl;
if ( ! allowed( rq , headers ) ){
responseCode = 401;
responseMsg = "not allowed\n";
return;
}
if ( url.size() > 1 ) {
handleRESTRequest( rq , url , responseMsg , responseCode , headers );
return;
}
responseCode = 200;
stringstream ss;
ss << "<html><head><title>";
string dbname;
{
stringstream z;
z << "db " << getHostName() << ':' << port << ' ';
dbname = z.str();
}
ss << dbname << "</title></head><body><h2>" << dbname << "</h2><p>\n<pre>";
doUnlockedStuff(ss);
int n = 2000;
Timer t;
while ( 1 ) {
if ( !dbMutexInfo.isLocked() ) {
{
dblock lk;
ss << "time to get dblock: " << t.millis() << "ms\n";
doLockedStuff(ss);
}
break;
}
sleepmillis(1);
if ( --n < 0 ) {
ss << "\n<b>timed out getting dblock</b>\n";
break;
}
}
ss << "</pre></body></html>";
responseMsg = ss.str();
}
void handleRESTRequest( const char *rq, // the full request
string url,
string& responseMsg,
int& responseCode,
vector<string>& headers // if completely empty, content-type: text/html will be added
) {
string::size_type first = url.find( "/" , 1 );
if ( first == string::npos ) {
responseCode = 400;
return;
}
string method = parseMethod( rq );
string dbname = url.substr( 1 , first - 1 );
string coll = url.substr( first + 1 );
string action = "";
map<string,string> params;
if ( coll.find( "?" ) != string::npos ) {
parseParams( params , coll.substr( coll.find( "?" ) + 1 ) );
coll = coll.substr( 0 , coll.find( "?" ) );
}
string::size_type last = coll.find_last_of( "/" );
if ( last == string::npos ) {
action = coll;
coll = "_defaultCollection";
}
else {
action = coll.substr( last + 1 );
coll = coll.substr( 0 , last );
}
for ( string::size_type i=0; i<coll.size(); i++ )
if ( coll[i] == '/' )
coll[i] = '.';
string fullns = dbname + "." + coll;
headers.push_back( (string)"x-action: " + action );
headers.push_back( (string)"x-ns: " + fullns );
headers.push_back( "Content-Type: text/plain;charset=utf-8" );
stringstream ss;
if ( method == "GET" ) {
responseCode = 200;
handleRESTQuery( fullns , action , params , responseCode , ss );
}
else if ( method == "POST" ) {
responseCode = 201;
handlePost( fullns , body( rq ) , params , responseCode , ss );
}
else {
responseCode = 400;
headers.push_back( "X_err: bad request" );
ss << "don't know how to handle a [" << method << "]";
out() << "don't know how to handle a [" << method << "]" << endl;
}
responseMsg = ss.str();
}
void handleRESTQuery( string ns , string action , map<string,string> & params , int & responseCode , stringstream & out ) {
Timer t;
int skip = _getOption( params["skip"] , 0 );
int num = _getOption( params["limit"] , _getOption( params["count" ] , 1000 ) ); // count is old, limit is new
int one = 0;
if ( params["one"].size() > 0 && tolower( params["one"][0] ) == 't' ) {
num = 1;
one = 1;
}
BSONObjBuilder queryBuilder;
for ( map<string,string>::iterator i = params.begin(); i != params.end(); i++ ) {
if ( ! i->first.find( "filter_" ) == 0 )
continue;
const char * field = i->first.substr( 7 ).c_str();
const char * val = i->second.c_str();
char * temp;
// TODO: this is how i guess if something is a number. pretty lame right now
double number = strtod( val , &temp );
if ( temp != val )
queryBuilder.append( field , number );
else
queryBuilder.append( field , val );
}
BSONObj query = queryBuilder.obj();
auto_ptr<DBClientCursor> cursor = db.query( ns.c_str() , query, num , skip );
if ( one ) {
if ( cursor->more() ) {
BSONObj obj = cursor->next();
out << obj.jsonString() << "\n";
}
else {
responseCode = 404;
}
return;
}
out << "{\n";
out << " \"offset\" : " << skip << ",\n";
out << " \"rows\": [\n";
int howMany = 0;
while ( cursor->more() ) {
if ( howMany++ )
out << " ,\n";
BSONObj obj = cursor->next();
out << " " << obj.jsonString();
}
out << "\n ]\n\n";
out << " \"total_rows\" : " << howMany << " ,\n";
out << " \"query\" : " << query.jsonString() << " ,\n";
out << " \"millis\" : " << t.millis() << " ,\n";
out << "}\n";
}
// TODO Generate id and revision per couch POST spec
void handlePost( string ns, const char *body, map<string,string> & 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( string val , int def ) {
if ( val.size() == 0 )
return def;
return atoi( val.c_str() );
}
private:
static DBDirectClient db;
};
DBDirectClient DbWebServer::db;
void webServerThread() {
boost::thread thr(statsThread);
AuthenticationInfo *ai = new AuthenticationInfo();
authInfo.reset(ai);
DbWebServer mini;
if ( mini.init(port+1000) )
mini.run();
}
} // namespace mongo
|