Mercurial > hg > audiodb
comparison 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 |
comparison
equal
deleted
inserted
replaced
604:41665dbab958 | 605:d7eb0a7440ad |
---|---|
51 return 1; | 51 return 1; |
52 } | 52 } |
53 | 53 |
54 | 54 |
55 static int adb_handle_sparql_req(request_rec *r) { | 55 static int adb_handle_sparql_req(request_rec *r) { |
56 librdf_world* world = NULL; | |
57 librdf_storage* storage = NULL; | |
58 librdf_uri *output_uri = NULL; | |
59 | |
60 int rc = DECLINED; | |
61 | |
56 if(strcmp(r->handler, "audiodb-sparql-handler") != 0) { | 62 if(strcmp(r->handler, "audiodb-sparql-handler") != 0) { |
57 return DECLINED; | 63 goto error; |
58 } | 64 } |
59 | 65 |
60 adb_config* config = ap_get_module_config(r->server->module_config, | 66 adb_config* config = ap_get_module_config(r->server->module_config, |
61 &audiodb_module); | 67 &audiodb_module); |
62 | 68 |
68 r->args = ""; | 74 r->args = ""; |
69 } | 75 } |
70 | 76 |
71 const apr_table_t *form_table; | 77 const apr_table_t *form_table; |
72 apreq_handle_t *h = apreq_handle_apache2(r); | 78 apreq_handle_t *h = apreq_handle_apache2(r); |
73 if(apreq_args(h, &form_table) != APR_SUCCESS) | 79 |
74 return DECLINED; | 80 |
81 if (r->method_number == M_POST) { | |
82 if (apreq_body(h, &form_table) != APR_SUCCESS) | |
83 goto error; | |
84 } | |
85 else { | |
86 if (apreq_args(h, &form_table) != APR_SUCCESS) | |
87 goto error; | |
88 } | |
89 | |
75 | 90 |
76 const char *query_string = apr_table_get(form_table, "query"); | 91 const char *query_string = apr_table_get(form_table, "query"); |
77 const char *output = apr_table_get(form_table, "output"); | 92 const char *output = apr_table_get(form_table, "output"); |
93 | |
94 if(!query_string) | |
95 query_string = "DESCRIBE "; | |
96 | |
78 if(!output) | 97 if(!output) |
79 output = "xml"; | 98 output = "json"; |
80 | 99 |
81 librdf_world* world = librdf_new_world(); | 100 world = librdf_new_world(); |
82 librdf_world_open(world); | 101 librdf_world_open(world); |
83 librdf_world_set_logger(world, NULL, log_message); | 102 librdf_world_set_logger(world, NULL, log_message); |
84 | 103 |
85 if(!config->dbpath) | 104 if(!config->dbpath) |
86 { | 105 { |
87 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required"); | 106 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "DatabasePath is required"); |
88 return DECLINED; | 107 goto error; |
89 } | 108 } |
109 | |
110 // First make sure we actually have a valid query | |
111 librdf_query *query; | |
112 if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) | |
113 { | |
114 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query"); | |
115 goto error; | |
116 } | |
90 | 117 |
91 librdf_storage* storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL); | 118 storage = librdf_new_storage(world, "audiodb", config->dbpath, NULL); |
92 if(!storage) | 119 if(!storage) |
93 { | 120 { |
94 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath); | 121 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to open audioDB: %s", config->dbpath); |
95 return DECLINED; | 122 goto error; |
96 } | 123 } |
97 | 124 |
98 librdf_model *model; | 125 librdf_model *model; |
99 if (!(model = librdf_new_model(world, storage, NULL))) | 126 if (!(model = librdf_new_model(world, storage, NULL))) |
100 { | 127 { |
101 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model"); | 128 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to create model"); |
102 return DECLINED; | 129 goto error; |
103 } | |
104 | |
105 librdf_query *query; | |
106 if (!(query = librdf_new_query(world, "sparql", NULL, (unsigned char*)query_string, NULL))) | |
107 { | |
108 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to parse query"); | |
109 return DECLINED; | |
110 } | 130 } |
111 | 131 |
112 librdf_query_results *results; | 132 librdf_query_results *results; |
113 if (!(results = librdf_query_execute(query, model))) | 133 if (!(results = librdf_query_execute(query, model))) |
114 { | 134 { |
115 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query"); | 135 ap_log_rerror(APLOG_MARK, APLOG_NOERRNO|APLOG_ERR, 0, r, "Unable to execute query"); |
116 return DECLINED; | 136 goto error; |
117 } | 137 } |
118 | 138 |
119 | |
120 librdf_uri *output_uri; | |
121 | 139 |
122 if(strcmp(output, "json") == 0) | 140 if(strcmp(output, "json") == 0) |
123 output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI ); | 141 output_uri = librdf_new_uri( world,(unsigned char *) JSON_URI ); |
124 else | 142 else |
125 output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI ); | 143 output_uri = librdf_new_uri( world,(unsigned char *) SPARQL_URI ); |
126 | 144 |
127 const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI)); | 145 const unsigned char* out = librdf_query_results_to_string(results, output_uri, librdf_new_uri(world, (unsigned char*) BASE_URI)); |
128 | 146 |
129 librdf_free_uri(output_uri); | |
130 librdf_storage_close(storage); | |
131 librdf_free_storage(storage); | |
132 librdf_free_world(world); | |
133 ap_rprintf(r, out); | 147 ap_rprintf(r, out); |
148 | |
149 rc = OK; | |
134 | 150 |
135 return OK; | 151 error: |
152 | |
153 if(output_uri) | |
154 librdf_free_uri(output_uri); | |
155 | |
156 if(storage) { | |
157 librdf_storage_close(storage); | |
158 librdf_free_storage(storage); | |
159 } | |
160 | |
161 if(world) | |
162 librdf_free_world(world); | |
163 | |
164 return rc; | |
136 | 165 |
137 } | 166 } |
138 | 167 |
139 static void mod_audiodb_register_hooks (apr_pool_t *p) { | 168 static void mod_audiodb_register_hooks (apr_pool_t *p) { |
140 ap_hook_handler(adb_handle_sparql_req, NULL, NULL, APR_HOOK_FIRST); | 169 ap_hook_handler(adb_handle_sparql_req, NULL, NULL, APR_HOOK_FIRST); |