static int test_read_from_string(raptor_world *world, const char* test_string, size_t test_string_len, const unsigned int expected_len) { raptor_iostream *iostr = NULL; char buffer[READ_BUFFER_SIZE]; unsigned long count; int rc = 0; const char* const label="read iostream from a string"; #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 fprintf(stderr, "%s: Testing %s\n", program, label); #endif iostr = raptor_new_iostream_from_string(world, (void*)test_string, test_string_len); if(!iostr) { fprintf(stderr, "%s: Failed to create %s\n", program, label); rc = 1; goto tidy; } count = raptor_iostream_read_bytes(buffer, 1, test_string_len, iostr); if(count != expected_len) { fprintf(stderr, "%s: %s read %d bytes, expected %d\n", program, label, (int)count, (int)expected_len); rc = 1; } if(!raptor_iostream_read_eof(iostr)) { fprintf(stderr, "%s: %s not EOF as expected\n", program, label); rc = 1; } if(strncmp(buffer, test_string, test_string_len)) { fprintf(stderr, "%s: %s returned '%s' expected '%s'\n", program, label, buffer, test_string); rc = 1; } tidy: if(iostr) raptor_free_iostream(iostr); if(rc) fprintf(stderr, "%s: FAILED Testing %s\n", program, label); return rc; }
/** * rasqal_service_execute: * @svc: rasqal service * * Execute a rasqal sparql protocol service * * Return value: query results or NULL on failure */ rasqal_query_results* rasqal_service_execute(rasqal_service* svc) { rasqal_query_results* results = NULL; unsigned char* result_string = NULL; size_t result_length; raptor_iostream* read_iostr = NULL; raptor_uri* read_base_uri = NULL; rasqal_variables_table* vars_table = NULL; rasqal_query_results_formatter* read_formatter = NULL; raptor_uri* retrieval_uri = NULL; raptor_stringbuffer* uri_sb = NULL; size_t len; unsigned char* str; raptor_world* raptor_world_ptr = rasqal_world_get_raptor(svc->world); if(!svc->www) { svc->www = raptor_new_www(raptor_world_ptr); if(!svc->www) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create WWW"); goto error; } } svc->started = 0; svc->final_uri = NULL; svc->sb = raptor_new_stringbuffer(); svc->content_type = NULL; if(svc->format) raptor_www_set_http_accept(svc->www, svc->format); else raptor_www_set_http_accept(svc->www, DEFAULT_FORMAT); raptor_www_set_write_bytes_handler(svc->www, rasqal_service_write_bytes, svc); raptor_www_set_content_type_handler(svc->www, rasqal_service_content_type_handler, svc); /* Construct a URI to retrieve following SPARQL protocol HTTP * binding from concatenation of * * 1. service_uri * 2. '?' * 3. "query=" query_string * 4. "&default-graph-uri=" background graph URI if any * 5. "&named-graph-uri=" named graph URI for all named graphs * with URI-escaping of the values */ uri_sb = raptor_new_stringbuffer(); if(!uri_sb) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create stringbuffer"); goto error; } str = raptor_uri_as_counted_string(svc->service_uri, &len); raptor_stringbuffer_append_counted_string(uri_sb, str, len, 1); raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"?", 1, 1); if(svc->query_string) { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"query=", 6, 1); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, svc->query_string, svc->query_string_len, 1); } if(svc->data_graphs) { rasqal_data_graph* dg; int i; int bg_graph_count; for(i = 0, bg_graph_count = 0; (dg = (rasqal_data_graph*)raptor_sequence_get_at(svc->data_graphs, i)); i++) { unsigned char* graph_str; size_t graph_len; raptor_uri* graph_uri; if(dg->flags & RASQAL_DATA_GRAPH_BACKGROUND) { if(bg_graph_count++) { if(bg_graph_count == 2) { /* Warn once, only when the second BG is seen */ rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_WARN, NULL, "Attempted to add use background graphs"); } /* always skip after first BG */ continue; } raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&default-graph-uri=", 19, 1); graph_uri = dg->uri; } else { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&named-graph-uri=", 17, 1); graph_uri = dg->name_uri; } graph_str = raptor_uri_as_counted_string(graph_uri, &graph_len); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, (const char*)graph_str, graph_len, 1); } } str = raptor_stringbuffer_as_string(uri_sb); retrieval_uri = raptor_new_uri(raptor_world_ptr, str); if(!retrieval_uri) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } raptor_free_stringbuffer(uri_sb); uri_sb = NULL; if(raptor_www_fetch(svc->www, retrieval_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to fetch retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } vars_table = rasqal_new_variables_table(svc->world); if(!vars_table) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create variables table"); goto error; } results = rasqal_new_query_results(svc->world, NULL, RASQAL_QUERY_RESULTS_BINDINGS, vars_table); /* (results takes a reference/copy to vars_table) */ rasqal_free_variables_table(vars_table); vars_table = NULL; if(!results) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query results"); goto error; } result_length = raptor_stringbuffer_length(svc->sb); result_string = raptor_stringbuffer_as_string(svc->sb); read_iostr = raptor_new_iostream_from_string(raptor_world_ptr, result_string, result_length); if(!read_iostr) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create iostream from string"); rasqal_free_query_results(results); results = NULL; goto error; } read_base_uri = svc->final_uri ? svc->final_uri : svc->service_uri; read_formatter = rasqal_new_query_results_formatter(svc->world, /* format name */ NULL, svc->content_type, /* format URI */ NULL); if(!read_formatter) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query formatter for type %s", svc->content_type); rasqal_free_query_results(results); results = NULL; goto error; } if(rasqal_query_results_formatter_read(svc->world, read_iostr, read_formatter, results, read_base_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to read from query formatter"); rasqal_free_query_results(results); results = NULL; goto error; } error: if(retrieval_uri) raptor_free_uri(retrieval_uri); if(uri_sb) raptor_free_stringbuffer(uri_sb); if(read_formatter) rasqal_free_query_results_formatter(read_formatter); if(read_iostr) raptor_free_iostream(read_iostr); if(vars_table) rasqal_free_variables_table(vars_table); if(svc->final_uri) { raptor_free_uri(svc->final_uri); svc->final_uri = NULL; } if(svc->content_type) { RASQAL_FREE(cstring, svc->content_type); svc->content_type = NULL; } if(svc->sb) { raptor_free_stringbuffer(svc->sb); svc->sb = NULL; } return results; }