/* * Connect the ows to the database specified in configuration file */ static void ows_pg(ows * o, char *con_str) { assert(o); assert(con_str); o->pg = PQconnectdb(con_str); if (PQstatus(o->pg) != CONNECTION_OK) { ows_log(o, 1, PQerrorMessage(o->pg)); ows_error(o, OWS_ERROR_CONNECTION_FAILED, "Connection to database failed", "init_OWS"); return; } if (PQsetClientEncoding(o->pg, o->db_encoding->buf)) { ows_log(o, 1, PQerrorMessage(o->pg)); ows_error(o, OWS_ERROR_CONNECTION_FAILED, "Wrong database encoding", "init_OWS"); return; } o->postgis_version = ows_psql_postgis_version(o); if (!o->postgis_version) ows_error(o, OWS_ERROR_CONNECTION_FAILED, "No PostGIS available in database", "init_OWS"); else if (ows_version_get(o->postgis_version) < 150) ows_error(o, OWS_ERROR_CONNECTION_FAILED, "PostGIS version must be at least 1.5.0", "init_OWS"); }
/* * Return an ExceptionReport as specified in OWS 1.1.0 specification */ void ows_error(ows * o, enum ows_error_code code, char *message, char *locator) { assert(o); assert(message); assert(locator); assert(!o->exit); o->exit = true; ows_log(o, 1, message); #if TINYOWS_FCGI if ((o->init && FCGI_Accept() >= 0) || !o->init) { #endif fprintf(o->output, "Content-Type: application/xml\n\n"); fprintf(o->output, "<?xml version='1.0' encoding='UTF-8'?>\n"); fprintf(o->output, "<ows:ExceptionReport\n"); fprintf(o->output, " xmlns='http://www.opengis.net/ows'\n"); fprintf(o->output, " xmlns:ows='http://www.opengis.net/ows'\n"); fprintf(o->output, " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"); fprintf(o->output, " xsi:schemaLocation='http://www.opengis.net/ows"); fprintf(o->output, " http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd'\n"); fprintf(o->output, " version='1.1.0' language='en'>\n"); fprintf(o->output, " <ows:Exception exceptionCode='%s' locator='%s'>\n", ows_error_code_string(code), locator); fprintf(o->output, " <ows:ExceptionText>%s</ows:ExceptionText>\n", message); fprintf(o->output, " </ows:Exception>\n"); fprintf(o->output, "</ows:ExceptionReport>\n"); #if TINYOWS_FCGI fflush(o->output); } #endif }
/* * Execute an SQL request */ PGresult * ows_psql_exec(ows *o, const char *sql) { assert(o); assert(sql); assert(o->pg); ows_log(o, 8, sql); return PQexecParams(o->pg, sql, 0, NULL, NULL, NULL, NULL, 0); }
int main(int argc, char *argv[]) { ows *o; char *query; o = ows_init(); o->config_file = buffer_init(); /* Config Files */ if (getenv("TINYOWS_CONFIG_FILE")) buffer_add_str(o->config_file, getenv("TINYOWS_CONFIG_FILE")); else if (getenv("TINYOWS_MAPFILE")) { buffer_add_str(o->config_file, getenv("TINYOWS_MAPFILE")); o->mapfile = true; } else buffer_add_str(o->config_file, OWS_CONFIG_FILE_PATH); LIBXML_TEST_VERSION xmlInitParser(); /* Parse the configuration file and initialize ows struct */ if (!o->exit) ows_parse_config(o, o->config_file->buf); if (!o->exit) ows_log(o, 2, "== TINYOWS STARTUP =="); /* Connect the ows to the database */ if (!o->exit) ows_pg(o, o->pg_dsn->buf); if (!o->exit) ows_log(o, 2, "== Connection PostGIS =="); /* Fill layers storage metadata */ if (!o->exit) ows_layers_storage_fill(o); if (!o->exit) ows_log(o, 2, "== Filling Storage =="); o->init = false; #if TINYOWS_FCGI if (!o->exit) ows_log(o, 2, "== FCGI START =="); while (FCGI_Accept() >= 0) { #endif query=NULL; if (!o->exit) query = cgi_getback_query(o); /* Retrieve safely query string */ if (!o->exit) ows_log(o, 4, query); /* Log input query if asked */ if (!o->exit && (!query || !strlen(query))) { /* Usage or Version command line options */ if (argc > 1) { if ( !strncmp(argv[1], "--help", 6) || !strncmp(argv[1], "-h", 2) || !strncmp(argv[1], "--check", 7)) ows_usage(o); else if ( !strncmp(argv[1], "--version", 9) || !strncmp(argv[1], "-v", 2)) fprintf(stdout, "%s\n", TINYOWS_VERSION); else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service"); } else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service"); o->exit=true; /* Have done what we have to */ } if (!o->exit) o->request = ows_request_init(); if (!o->exit) ows_kvp_or_xml(o, query); /* Method is KVP or XML ? */ if (!o->exit) { switch (o->request->method) { case OWS_METHOD_KVP: o->cgi = cgi_parse_kvp(o, query); break; case OWS_METHOD_XML: o->cgi = cgi_parse_xml(o, query); break; default: ows_error(o, OWS_ERROR_REQUEST_HTTP, "Wrong HTTP request Method", "http"); } } if (!o->exit) o->psql_requests = list_init(); if (!o->exit) ows_metadata_fill(o, o->cgi); /* Fill service's metadata */ if (!o->exit) ows_request_check(o, o->request, o->cgi, query); /* Process service request */ /* Run the right OWS service */ if (!o->exit) { switch (o->request->service) { case WFS: o->request->request.wfs = wfs_request_init(); wfs_request_check(o, o->request->request.wfs, o->cgi); if (!o->exit) wfs(o, o->request->request.wfs); break; default: ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service"); } } if (o->request) { ows_request_free(o->request); o->request=NULL; } /* We allocated memory only on post case */ if (cgi_method_post() && query) free(query); #if TINYOWS_FCGI fflush(stdout); o->exit = false; } ows_log(o, 2, "== FCGI SHUTDOWN =="); OS_LibShutdown(); #endif ows_log(o, 2, "== TINYOWS SHUTDOWN =="); ows_free(o); xmlCleanupParser(); return EXIT_SUCCESS; }