diff -r fd5765c5068c -r f210681d9dd0 src/server/plugins/postgresql/service.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/server/plugins/postgresql/service.c Thu Jan 27 15:47:18 2022 +0100 @@ -0,0 +1,101 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2022 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "service.h" + +#include +#include + +#include "resource.h" + +int pg_query(pblock *pb, Session *sn, Request *rq) { + char *respool = pblock_findval("resource", pb); + char *query = pblock_findval("query", pb); + + if(!respool) { + log_ereport(LOG_MISCONFIG, "pg-query: missing resource parameter"); + return REQ_ABORTED; + } + if(!query) { + log_ereport(LOG_MISCONFIG, "pg-query: missing query parameter"); + return REQ_ABORTED; + } + + // Get the named resource and check if the PG connection works + ResourceData *res = resourcepool_lookup(sn, rq, respool, 0); + if(!res) { + log_ereport(LOG_FAILURE, "pg-query: cannot get resource '%s'", respool); + return REQ_ABORTED; + } + + PGconn *connection = res->data; + if(pg_check_connection(LOG_FAILURE, respool, connection)) { + return REQ_ABORTED; + } + + // execute query + PGresult *result = PQexec(connection, query); + if(!result) return REQ_ABORTED; + + // start response + pblock_remove("content-type", rq->srvhdrs); + pblock_nvinsert("content-type", "text/html", rq->srvhdrs); + + protocol_status(sn, rq, 200, NULL); + http_start_response(sn, rq); + + // (html) header + net_printf(sn->csd, "%s", "\n\n"); + + int nfields = PQnfields(result); + if(nfields > 0) { + net_printf(sn->csd, "\n\n"); + for(int i=0;icsd, "\n", PQfname(result, i)); + } + net_printf(sn->csd, "\n"); + + int nrows = PQntuples(result); + for(int r=0;rcsd, "\n"); + for(int c=0;ccsd, "\n", PQgetvalue(result, r, c)); + } + net_printf(sn->csd, "\n"); + } + + + net_printf(sn->csd, "
%s
%s
\n"); + } else { + net_printf(sn->csd, "

%s

\n", PQresStatus(PQresultStatus(result))); + } + + net_printf(sn->csd, "%s", "\n"); + + return REQ_PROCEED; +}