138 lines
3.6 KiB
C++
138 lines
3.6 KiB
C++
#include "Pages.h"
|
|
#include "TcpSocket.h"
|
|
#include "HttpRequest.h"
|
|
#include "HttpServer.h"
|
|
#include "SafeBuf.h"
|
|
#include "Collectiondb.h"
|
|
#include "SpiderColl.h"
|
|
#include "ip.h"
|
|
#include "Errno.h"
|
|
#include <algorithm>
|
|
|
|
|
|
static bool cmp_ip_network_order(uint32_t ip_a, uint32_t ip_b) {
|
|
return ntohl(ip_a) < ntohl(ip_b);
|
|
}
|
|
|
|
|
|
static void generatePageHtml(std::vector<uint32_t> &doleips, const char *coll, SafeBuf *sb) {
|
|
std::sort(doleips.begin(), doleips.end(), cmp_ip_network_order);
|
|
|
|
sb->safePrintf("<table cellpadding=5 style=\"max-width:25em\" border=0>"
|
|
" <tr>"
|
|
" <td>For collection <i>%s</i> (%zu IPs):</td>"
|
|
" </tr>"
|
|
"</table>\n",
|
|
coll,
|
|
doleips.size()
|
|
);
|
|
|
|
sb->safePrintf("<table %s>\n"
|
|
" <tr>\n"
|
|
" <td><b>IPs in DoledbIP table</b></td>\n"
|
|
" </tr>\n"
|
|
, TABLE_STYLE);
|
|
|
|
for(auto ip : doleips) {
|
|
char ipbuf[16];
|
|
sb->safePrintf(" <tr bgcolor=#%s><td>%s</td></tr>\n", LIGHT_BLUE, iptoa(ip,ipbuf));
|
|
}
|
|
|
|
sb->safePrintf("</table>\n");
|
|
}
|
|
|
|
|
|
static void generatePageJSON(std::vector<uint32_t> &doleips, const char *coll, SafeBuf *sb) {
|
|
//order is irrelevant for JSON, but some humans like to look at it.
|
|
std::sort(doleips.begin(), doleips.end(), cmp_ip_network_order);
|
|
|
|
sb->safePrintf("{\n"); //object start
|
|
|
|
sb->safePrintf(" \"ips\": [\n"); //value-name and start of array
|
|
|
|
bool first=true;
|
|
for(auto ip : doleips) {
|
|
if(!first)
|
|
sb->safePrintf(",\n");
|
|
char ipbuf[16];
|
|
sb->safePrintf(" \"%s\"", iptoa(ip,ipbuf));
|
|
first = false;
|
|
}
|
|
if(!first)
|
|
sb->safePrintf("\n");
|
|
|
|
sb->safePrintf(" ]\n"); //end array
|
|
|
|
sb->safePrintf("}\n"); //end object
|
|
}
|
|
|
|
|
|
static bool respondWithError(TcpSocket *s, HttpRequest *r, int32_t error, const char *errmsg) {
|
|
SafeBuf sb;
|
|
const char *contentType = NULL;
|
|
switch(r->getReplyFormat()) {
|
|
case FORMAT_HTML:
|
|
g_pages.printAdminTop(&sb, s, r, NULL);
|
|
sb.safePrintf("<p>%s</p>", errmsg);
|
|
g_pages.printAdminBottom2(&sb);
|
|
contentType = "text/html";
|
|
break;
|
|
case FORMAT_JSON:
|
|
sb.safePrintf("{\"response\":{\n"
|
|
"\t\"statusCode\":%" PRId32",\n"
|
|
"\t\"statusMsg\":\"", error);
|
|
sb.jsonEncode(errmsg);
|
|
sb.safePrintf("\"\n"
|
|
"}\n"
|
|
"}\n");
|
|
contentType = "application/json";
|
|
contentType = "application/json";
|
|
break;
|
|
default:
|
|
contentType = "application/octet-stream";
|
|
break;
|
|
}
|
|
|
|
return g_httpServer.sendDynamicPage(s, sb.getBufStart(), sb.length(), -1, false, contentType);
|
|
}
|
|
|
|
|
|
|
|
bool sendPageDoledbIPTable(TcpSocket *s, HttpRequest *r) {
|
|
const char *coll = r->getString("c", NULL, NULL);
|
|
CollectionRec *cr = g_collectiondb.getRec(coll);
|
|
if(!cr) {
|
|
return respondWithError(s, r, ENOCOLLREC, "No collection specified");
|
|
}
|
|
|
|
SpiderColl *spiderColl = cr->m_spiderColl;
|
|
if(!spiderColl) {
|
|
return respondWithError(s, r, EBADENGINEER, "No spider-collection (?)");
|
|
}
|
|
|
|
std::vector<uint32_t> doleips = spiderColl->getDoledbIpTable();
|
|
|
|
SafeBuf sb;
|
|
|
|
const char *contentType = NULL;
|
|
switch(r->getReplyFormat()) {
|
|
case FORMAT_HTML:
|
|
g_pages.printAdminTop(&sb, s, r, NULL);
|
|
generatePageHtml(doleips, coll, &sb);
|
|
g_pages.printAdminBottom2(&sb);
|
|
contentType = "text/html";
|
|
break;
|
|
case FORMAT_JSON:
|
|
generatePageJSON(doleips, coll, &sb);
|
|
contentType = "application/json";
|
|
break;
|
|
default:
|
|
contentType = "text/html";
|
|
sb.safePrintf("oops!");
|
|
break;
|
|
|
|
}
|
|
|
|
return g_httpServer.sendDynamicPage(s, sb.getBufStart(), sb.length(), -1, false, contentType);
|
|
}
|