static int rest_get_noit_config(noit_http_rest_closure_t *restc, int npats, char **pats) { noit_http_session_ctx *ctx = restc->http_ctx; char *xml = NULL; if(npats != 0) { noit_http_response_server_error(ctx, "text/xml"); noit_http_response_end(ctx); return 0; } xml = ingestor->get_noit_config(restc->remote_cn); if(xml == NULL) { char buff[1024]; snprintf(buff, sizeof(buff), "<error><remote_cn>%s</remote_cn>" "<row_count>%d</row_count></error>\n", restc->remote_cn, 0); noit_http_response_append(ctx, buff, strlen(buff)); noit_http_response_not_found(ctx, "text/xml"); } else { noit_http_response_append(ctx, xml, strlen(xml)); noit_http_response_ok(ctx, "text/xml"); } if(xml) free(xml); noit_http_response_end(ctx); return 0; }
static int rest_show_feed(noit_http_rest_closure_t *restc, int npats, char **pats) { noit_http_session_ctx *ctx = restc->http_ctx; const char *err = "unknown error"; const char *jpath_with_sub; char jlogpath[PATH_MAX], *cp, **subs = NULL; int nsubs, i; noit_log_stream_t feed; jlog_ctx *jctx = NULL; xmlDocPtr doc = NULL; xmlNodePtr root = NULL, subnodes; feed = noit_log_stream_find("feed"); if(!feed) { err = "cannot find feed"; goto error; } jpath_with_sub = noit_log_stream_get_path(feed); strlcpy(jlogpath, jpath_with_sub, sizeof(jlogpath)); cp = strchr(jlogpath, '('); if(cp) *cp = '\0'; jctx = jlog_new(jlogpath); if((nsubs = jlog_ctx_list_subscribers(jctx, &subs)) == -1) { err = jlog_ctx_err_string(jctx); goto error; } doc = xmlNewDoc((xmlChar *)"1.0"); root = xmlNewDocNode(doc, NULL, (xmlChar *)"feed", NULL); xmlDocSetRootElement(doc, root); subnodes = xmlNewNode(NULL, (xmlChar *)"subscribers"); for(i=0; i<nsubs; i++) { xmlNewChild(subnodes, NULL, (xmlChar *)"subscriber", (xmlChar *)subs[i]); } xmlAddChild(root, subnodes); noit_http_response_ok(restc->http_ctx, "text/xml"); noit_http_response_xml(restc->http_ctx, doc); noit_http_response_end(restc->http_ctx); if(subs) jlog_ctx_list_subscribers_dispose(jctx, subs); xmlFreeDoc(doc); jlog_ctx_close(jctx); return 0; error: if(doc) xmlFreeDoc(doc); if(subs) jlog_ctx_list_subscribers_dispose(jctx, subs); noit_http_response_server_error(ctx, "text/plain"); noit_http_response_append(ctx, err, strlen(err)); noit_http_response_end(ctx); if(jctx) jlog_ctx_close(jctx); return 0; }
static int rest_delete_feed(noit_http_rest_closure_t *restc, int npats, char **pats) { noit_http_session_ctx *ctx = restc->http_ctx; const char *err = "unknown error"; const char *jpath_with_sub; char jlogpath[PATH_MAX], *cp; int rv; noit_log_stream_t feed; jlog_ctx *jctx; feed = noit_log_stream_find("feed"); if(!feed) { err = "cannot find feed"; goto error; } jpath_with_sub = noit_log_stream_get_path(feed); strlcpy(jlogpath, jpath_with_sub, sizeof(jlogpath)); cp = strchr(jlogpath, '('); if(cp) *cp = '\0'; jctx = jlog_new(jlogpath); rv = jlog_ctx_remove_subscriber(jctx, pats[0]); jlog_ctx_close(jctx); if(rv < 0) { err = jlog_ctx_err_string(jctx); goto error; } /* removed or note, we should do a sweeping cleanup */ jlog_clean(jlogpath); if(rv == 0) { noit_http_response_not_found(ctx, "text/plain"); noit_http_response_end(ctx); return 0; } noit_http_response_standard(ctx, 204, "OK", "text/plain"); noit_http_response_end(ctx); return 0; error: noit_http_response_server_error(ctx, "text/plain"); noit_http_response_append(ctx, err, strlen(err)); noit_http_response_end(ctx); return 0; }
static int handoff_request_dispatcher(noit_http_session_ctx *ctx) { char *hello = "message:hello\r\n"; if(the_one_and_only) { hello = "message:already connected\r\n"; noit_http_response_server_error(ctx, "text/plain"); noit_http_response_append(ctx, hello, strlen(hello)); noit_http_response_end(ctx); return 0; } the_one_and_only = ctx; noit_http_response_status_set(ctx, 200, "OK"); noit_http_response_option_set(ctx, NOIT_HTTP_CHUNKED); noit_http_response_header_set(ctx, "Content-Type", "text/plain"); noit_http_response_append(ctx, hello, strlen(hello)); noit_http_response_flush(ctx, noit_false); return EVENTER_EXCEPTION; }
static int noit_capabilities_rest(noit_http_rest_closure_t *restc, int n, char **p) { noit_capsvc_closure_t cl = { 0 }; const char *mtype = "application/xml"; if(n > 0 && !strcmp(p[0], ".json")) { noit_capabilities_tobuff_json(&cl, NULL); mtype = "application/json"; } else noit_capabilities_tobuff(&cl, NULL); if(!cl.buff) goto error; noit_http_response_ok(restc->http_ctx, mtype); noit_http_response_append(restc->http_ctx, cl.buff, cl.towrite); noit_http_response_end(restc->http_ctx); free(cl.buff); return 0; error: noit_http_response_server_error(restc->http_ctx, "text/html"); noit_http_response_end(restc->http_ctx); return 0; }