/
PageCatdb.cpp
302 lines (285 loc) · 8.07 KB
/
PageCatdb.cpp
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
#include "gb-include.h"
//#include "CollectionRec.h"
#include "Pages.h"
#include "Msg2a.h"
#include "Msg8b.h"
static void sendReplyWrapper ( void *state );
static bool sendReply ( void *state );
static void gotCatInfoWrapper ( void *state );//, CatRec *rec );
class StateCatdb {
public:
TcpSocket *m_socket;
HttpRequest m_r;
char m_coll[MAX_COLL_LEN];
int32_t m_collLen;
Msg2a m_msg2a;
Msg8b m_msg8b;
Url m_url;
CatRec m_catRec;
bool m_catLookup;
bool m_genCatdb;
int64_t m_startTime;
};
// . returns false if blocked, true otherwise
// . sets g_errno on error
bool sendPageCatdb ( TcpSocket *s , HttpRequest *r ) {
// are we the admin?
//bool isAdmin = g_collectiondb.hasPermission ( r , s );
// get the collection record
CollectionRec *cr = g_collectiondb.getRec ( r );
if ( ! cr ) {
log("admin: No collection record found "
"for specified collection name. Could not add sites to "
"tagdb. Returning HTTP status of 500.");
return g_httpServer.sendErrorReply ( s , 500 ,
"collection does not exist");
}
/*
bool isAssassin = cr->isAssassin ( s->m_ip );
if ( isAdmin ) isAssassin = true;
// bail if permission denied
if ( ! isAssassin && ! cr->hasPermission ( r , s ) ) {
log("admin: Bad collection name or password. Could not add "
"sites to tagdb. Permission denied.");
return sendPagexxxx ( s , r ,
"Collection name or "
"password is incorrect");
}
*/
// no permmission?
bool isMasterAdmin = g_conf.isMasterAdmin ( s , r );
bool isCollAdmin = g_conf.isCollAdmin ( s , r );
if ( ! isMasterAdmin &&
! isCollAdmin ) {
g_errno = ENOPERM;
g_httpServer.sendErrorReply(s,g_errno,mstrerror(g_errno));
return true;
}
// get the collection
int32_t collLen = 0;
char *coll = r->getString("c", &collLen, NULL);
// check for generate catdb command
int32_t genCatdb = r->getLong("gencatdb", 0);
// check for a lookup url
int32_t urlLen = 0;
char *url = r->getString("caturl", &urlLen, NULL);
// create the State
StateCatdb *st;
try { st = new (StateCatdb); }
catch ( ... ) {
g_errno = ENOMEM;
log("catdb: Unable to allocate %"INT32" bytes for StateCatdb",
(int32_t)sizeof(StateCatdb) );
return true;
}
mnew ( st, sizeof(StateCatdb), "PageCatdb" );
// fill the state
st->m_socket = s;
st->m_r.copy(r);
// copy collection
if (collLen > MAX_COLL_LEN) collLen = MAX_COLL_LEN - 1;
gbmemcpy(st->m_coll, coll, collLen);
st->m_coll[collLen] = '\0';
st->m_collLen = collLen;
// defaults
st->m_catLookup = false;
st->m_genCatdb = false;
st->m_startTime = gettimeofdayInMilliseconds();
// generate catdb if requested
if (genCatdb == 1) {
st->m_genCatdb = true;
if (!st->m_msg2a.makeCatdb ( st->m_coll,
st->m_collLen,
false,
st,
sendReplyWrapper ) )
return false;
}
// update catdb from .new files
else if (genCatdb == 2) {
st->m_genCatdb = true;
if (!st->m_msg2a.makeCatdb ( st->m_coll,
st->m_collLen,
true,
st,
sendReplyWrapper ) )
return false;
}
// lookup a url if requested
else if (url && urlLen > 0) {
st->m_catLookup = true;
// set the url
st->m_url.set(url, urlLen);
// call msg8b to lookup in catdb
if (!st->m_msg8b.getCatRec ( &st->m_url,
NULL,//st->m_coll,
0,//st->m_collLen,
true,
1,
&st->m_catRec,
st,
gotCatInfoWrapper))
//RDB_CATDB ) )
//RDB_TAGDB ) )
return false;
}
// otherwise return the regular page
return sendReply ( st );
}
void sendReplyWrapper ( void *state ) {
sendReply ( state );
}
void gotCatInfoWrapper ( void *state ) { // , CatRec *rec ) {
sendReply ( state );
}
bool sendReply ( void *state ) {
StateCatdb *st = (StateCatdb*)state;
// check for error
if (g_errno) {
if (st->m_catLookup)
log("PageCatdb: Msg8b had error getting Site Rec: %s",
mstrerror(g_errno));
else
log("PageCatdb: Msg2a had error generating Catdb: %s",
mstrerror(g_errno));
st->m_catLookup = false;
g_errno = 0;
}
int64_t endTime = gettimeofdayInMilliseconds();
// page buffer
SafeBuf sb;
sb.reserve(64*1024);
// . print standard header
// . do not print big links if only an assassin, just print host ids
g_pages.printAdminTop ( &sb, st->m_socket , &st->m_r );
sb.safePrintf(
"<style>"
".poo { background-color:#%s;}\n"
"</style>\n" ,
LIGHT_BLUE );
sb.safePrintf ( "<table %s>"
"<tr><td colspan=2>"
"<center><font size=+1><b>Catdb</b></font></center>"
"</td></tr>", TABLE_STYLE );
// instructions
sb.safePrintf("<tr bgcolor=#%s>"
"<td colspan=3>"
"<font size=-2>"
"<center>"
"Don't just start using this, you need to follow the "
"instructions in the <i>admin guide</i> for adding "
"DMOZ support."
"</center>"
"</font>"
"</td>"
"</tr>"
,DARK_BLUE
);
// print the generate Catdb link
sb.safePrintf ( "<tr class=poo><td>Update Catdb from DMOZ data.</td>"
"<td><center>"
"<a href=\"/admin/catdb?c=%s&gencatdb=2\">"
"Update Catdb</a> "
"</center></td></tr>",
st->m_coll );
sb.safePrintf ( "<tr class=poo>"
"<td>Generate New Catdb from DMOZ data.</td>"
"<td><center>"
"<a href=\"/admin/catdb?c=%s&gencatdb=1\">"
"Generate Catdb</a> "
"</center></td></tr>",
st->m_coll );
if (st->m_genCatdb)
sb.safePrintf ( "<tr class=poo>"
"<td> Catdb Generation took %"INT64" ms."
"</td></tr>",
endTime - st->m_startTime );
// print Url Category Lookup
sb.safePrintf ( "<tr class=poo><td>Lookup Category of Url.</td>"
"<td><input type=text name=caturl size=80"
" value=\"");
if (st->m_catLookup) {
sb.safeMemcpy(st->m_url.getUrl(), st->m_url.getUrlLen());
}
sb.safePrintf("\"></center></td></tr>" );
// print Url Info if Lookup was done
if (st->m_catLookup) {
sb.safePrintf("<tr><td>");
// print the url
sb.safeMemcpy(st->m_url.getUrl(), st->m_url.getUrlLen());
sb.safePrintf(" (%"INT64" ms)</td><td>",
endTime - st->m_startTime );
// print each category id and path
for (int32_t i = 0; i < st->m_catRec.m_numCatids; i++) {
sb.safePrintf("<b>[%"INT32"] ",
st->m_catRec.m_catids[i]);
g_categories->printPathFromId(&sb,
st->m_catRec.m_catids[i]);
sb.safePrintf("</b><br>");
// lookup title and summary
char title[1024];
int32_t titleLen = 0;
char summ[4096];
int32_t summLen = 0;
char anchor[256];
unsigned char anchorLen = 0;
g_categories->getTitleAndSummary(
st->m_url.getUrl(),
st->m_url.getUrlLen(),
st->m_catRec.m_catids[i],
title,
&titleLen,
1023,
summ,
&summLen,
4098,
anchor,
&anchorLen,
255 );
title[titleLen] = '\0';
summ[summLen] = '\0';
anchor[anchorLen] = '\0';
// print title and summary
sb.safePrintf("<b>Title:</b> %s<br>"
"<b>Summary:</b> %s<br>",
title, summ);
if (anchorLen > 0)
sb.safePrintf("<b>Anchor:</b> %s<br>",
anchor);
sb.safePrintf("<br>");
}
sb.safePrintf("<b>Filenum:</b> %"INT32"<br>",
st->m_catRec.m_filenum);
// print indirect catids
if (st->m_catRec.m_numIndCatids > 0) {
sb.safePrintf("<hr><b>Indirect Catids [%"INT32"]:"
"</b><br>\n",
st->m_catRec.m_numIndCatids );
for (int32_t i = 0;
i < st->m_catRec.m_numIndCatids; i++) {
sb.safePrintf("%"UINT32"<br>",
st->m_catRec.m_indCatids[i]);
}
}
sb.safePrintf("</td></tr>");
}
// end it
sb.safePrintf ( "</center></td></tr></table>" );
// print submit button
sb.safePrintf ( "<br><center>"
"<input type=submit value=\"Submit\" border=0>"
"</form></center>" );
// print the final tail
//p += g_httpServer.printTail ( p , pend - p );
// clear g_errno, if any, so our reply send goes through
g_errno = 0;
// extract the socket
TcpSocket *s = st->m_socket;
// clear the state
mdelete ( st, sizeof(StateCatdb), "PageCatdb" );
delete st;
// . send this page
// . encapsulates in html header and tail
// . make a Mime
return g_httpServer.sendDynamicPage(s , sb.getBufStart(), sb.length());
}