static void* handle_suggest(enum mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info) { ++nreq; if (building) { print_HTTP_response(conn, 412, "Busy"); return (void*)""; } std::string q = get_qs(request_info, "q"); std::string sn = get_qs(request_info, "n"); std::string cb = get_qs(request_info, "callback"); std::string type = get_qs(request_info, "type"); DCERR("handle_suggest::q:"<<q<<", sn:"<<sn<<", callback: "<<cb<<endl); unsigned int n = sn.empty() ? NMAX : atoi(sn.c_str()); if (n > NMAX) { n = NMAX; } const bool has_cb = !cb.empty(); if (has_cb && !is_valid_cb(cb)) { print_HTTP_response(conn, 400, "Invalid Request"); return (void*)""; } print_HTTP_response(conn, 200, "OK"); str_lowercase(q); vp_t results = suggest(pm, st, q, n); /* for (size_t i = 0; i < results.size(); ++i) { mg_printf(conn, "%s:%d\n", results[i].first.c_str(), results[i].second); } */ if (has_cb) { mg_printf(conn, "%s(%s);\n", cb.c_str(), results_json(q, results, type).c_str()); } else { mg_printf(conn, "%s\n", results_json(q, results, type).c_str()); } return (void*)""; }
static void handle_suggest(client_t *client, parsed_url_t &url) { ++nreq; std::string body; headers_t headers; headers["Cache-Control"] = "no-cache"; if (building) { write_response(client, 412, "Busy", headers, body); return; } std::string q = unescape_query(url.query["q"]); std::string sn = url.query["n"]; std::string cb = unescape_query(url.query["callback"]); std::string type = unescape_query(url.query["type"]); DCERR("handle_suggest::q:"<<q<<", sn:"<<sn<<", callback: "<<cb<<endl); unsigned int n = sn.empty() ? NMAX : atoi(sn.c_str()); if (n > NMAX) { n = NMAX; } if (n < 1) { n = 1; } const bool has_cb = !cb.empty(); str_lowercase(q); vp_t results = suggest(pm, st, q, n); /* for (size_t i = 0; i < results.size(); ++i) { mg_printf(conn, "%s:%d\n", results[i].first.c_str(), results[i].second); } */ headers["Content-Type"] = "text/plain; charset=UTF-8"; if (has_cb) { body = cb + "(" + results_json(q, results, type) + ");\n"; } else { body = results_json(q, results, type) + "\n"; } write_response(client, 200, "OK", headers, body); }
void Analyze::end_of_log(uint32_t packet_count, uint64_t bytes_dropped) { std::for_each(_analyzers.begin(), _analyzers.end(), [packet_count](Analyzer* c){c->end_of_log(packet_count); }); Json::Value root; root["format-version"] = "0.1"; root["timestamp"] = (Json::UInt64)start_time; root["duration"] = (Json::UInt64)(now() - start_time); results_json(root); root["packet_count"] = packet_count; root["packet-count"] = packet_count; root["bytes-dropped"] = (Json::UInt64)bytes_dropped; Json::Writer *writer; switch(_output_style) { case OUTPUT_JSON: { writer = new Json::StyledWriter(); break; } case OUTPUT_PLAINTEXT: writer = new Json::PlainTextWriter(); break; case OUTPUT_HTML: writer = new Json::HTMLWriter(); break; case OUTPUT_BRIEF: writer = new Json::BriefPlainTextWriter(); break; default: ::fprintf(stderr, "Writer not set for output style"); abort(); } std::string document = writer->write(root); fprintf(stdout, "%s", document.c_str()); }