int main (int argc, char *argv[]) { const char *program = raptor_basename(argv[0]); raptor_world *world; const char *uri_string; raptor_www *www; const char *user_agent = "raptor_www_test/0.1"; raptor_uri *uri; void *string = NULL; size_t string_length = 0; if(argc > 1) uri_string = argv[1]; else uri_string = "http://librdf.org/"; world = raptor_new_world(); if(!world || raptor_world_open(world)) exit(1); uri = raptor_new_uri(world, (const unsigned char*)uri_string); if(!uri) { fprintf(stderr, "%s: Failed to create Raptor URI for %s\n", program, uri_string); exit(1); } www = raptor_new_www(world); raptor_www_set_content_type_handler(www, write_content_type, (void*)stderr); raptor_www_set_user_agent(www, user_agent); /* start retrieval (always a GET) */ if(raptor_www_fetch_to_string(www, uri, &string, &string_length, malloc)) { fprintf(stderr, "%s: WWW fetch failed\n", program); } else { #if defined(RAPTOR_DEBUG) && RAPTOR_DEBUG > 1 fprintf(stderr, "%s: HTTP response status %d\n", program, www->status_code); fprintf(stderr, "%s: Returned %d bytes of content\n", program, (int)string_length); #endif } if(string) free(string); raptor_free_www(www); raptor_free_uri(uri); raptor_free_world(world); return 0; }
int main (int argc, char *argv[]) { const char *uri_string; raptor_www *www; const char *user_agent="raptor-www-test"; raptor_uri *uri; void *string=NULL; size_t string_length=0; if(argc>1) uri_string=argv[1]; else uri_string="http://librdf.org/"; raptor_uri_init(); raptor_www_init(); uri=raptor_new_uri((const unsigned char*)uri_string); if(!uri) { fprintf(stderr, "Failed to create Raptor URI for %s\n", uri_string); exit(1); } www=raptor_www_new(); raptor_www_set_content_type_handler(www, write_content_type, (void*)stderr); raptor_www_set_user_agent(www, user_agent); /* start retrieval (always a GET) */ if(raptor_www_fetch_to_string(www, uri, &string, &string_length, malloc)) { printf("WWW fetch failed\n"); } else { printf("HTTP response status %d\n", www->status_code); printf("Returned %d bytes of content\n", (int)string_length); } if(string) free(string); raptor_www_free(www); raptor_free_uri(uri); raptor_www_finish(); return 0; }
/** * 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; }