Mercurial > hg > audiodb
diff sparql/mod_audiodb/mod_audiodb.c @ 605:d7eb0a7440ad
Tidying and POST enhancement
* Ensured that the storage module is closed/freed when errors occur
* Now reads in the query via POST if provided (allows for other clients)
author | mas01mj |
---|---|
date | Tue, 18 Aug 2009 14:23:32 +0000 |
parents | 783a1a5e51b2 |
children | 32ab92399b5d |
line wrap: on
line diff
--- a/sparql/mod_audiodb/mod_audiodb.c Tue Aug 18 14:23:31 2009 +0000 +++ b/sparql/mod_audiodb/mod_audiodb.c Tue Aug 18 14:23:32 2009 +0000 @@ -53,8 +53,14 @@ static int adb_handle_sparql_req(request_rec *r) { + librdf_world* world = NULL; + librdf_storage* storage = NULL; + librdf_uri *output_uri = NULL; + + int rc = DECLINED; + if(strcmp(r->handler, "audiodb-sparql-handler") != 0) { - return DECLINED; + goto error; } adb_config* config = ap_get_module_config(r->server->module_config, @@ -70,55 +76,67 @@ const apr_table_t *form_table; apreq_handle_t *h = apreq_handle_apache2(r); - if(apreq_args(h, &form_table) != APR_SUCCESS) - return DECLINED; + + + if (r->method_number == M_POST) { + if (apreq_body(h, &form_table) != APR_SUCCESS) + goto error; + } + else { + if (apreq_args(h, &form_table) != APR_SUCCESS) + goto error; + } + const char *query_string = apr_table_get(form_table, "query"); const char *output = apr_table_get(form_table, "output"); + + if(!query_string) + query_string = "DESCRIBE "; + if(!output) - output = "xml"; + output = "json"; - librdf_world* world = librdf_new_world(); + world = librdf_new_world(); librdf_world_open(world); librdf_world_set_logger(world, NULL, log_message); if(!config->dbpath) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required"); - return DECLINED; + goto error; } + + // First make sure we actually have a valid query + librdf_query *query; + if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) + { + ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query"); + goto error; + } - librdf_storage* storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL); + storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL); if(!storage) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath); - return DECLINED; + goto error; } librdf_model *model; if (!(model = librdf_new_model(world, storage, NULL))) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model"); - return DECLINED; - } - - librdf_query *query; - if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) - { - ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query"); - return DECLINED; + goto error; } librdf_query_results *results; if (!(results = librdf_query_execute(query, model))) { ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query"); - return DECLINED; + goto error; } - librdf_uri *output_uri; - if(strcmp(output, "json") == 0) output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI ); else @@ -126,13 +144,24 @@ const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI)); - librdf_free_uri(output_uri); - librdf_storage_close(storage); - librdf_free_storage(storage); - librdf_free_world(world); ap_rprintf(r, out); + + rc = OK; - return OK; + error: + + if(output_uri) + librdf_free_uri(output_uri); + + if(storage) { + librdf_storage_close(storage); + librdf_free_storage(storage); + } + + if(world) + librdf_free_world(world); + + return rc; }