forked from 4store/4store
/
httpd.c
1911 lines (1683 loc) · 55.4 KB
/
httpd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
4store - a clustered RDF storage and query engine
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright 2006 Nick Lamb for Garlik.com
Copyright 2010 Martin Galpin (CORS support)
*/
#define _GNU_SOURCE
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <libgen.h>
#include <glib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <rasqal.h>
#include "4store-config.h"
#include "../common/4store.h"
#include "../common/error.h"
#include "../common/gnu-options.h"
#include "../frontend/query.h"
#include "../frontend/import.h"
#include "../frontend/update.h"
#include "httpd.h"
#define WATCHDOG_RATE 16000 /* bytes per second */
/* is this request a valid CORS request? */
#define IS_CORS(ctxt) (cors_support && \
(ctxt->method == FS_HTTP_HEAD || ctxt->method == FS_HTTP_OPTIONS \
|| ctxt->method == FS_HTTP_GET) && \
g_hash_table_lookup(ctxt->headers, "origin"))
/* file globals */
static raptor_uri *bu;
static fsp_link *fsplink;
static int has_o_index = 0;
static long all_time_import_count = 0;
static int global_import_count = 0;
static int unsafe = 0;
static int default_graph = 0;
static int soft_limit = 0; /* default value for soft limit */
static int opt_level = -1; /* default value for optimisation level */
static int cors_support = -1; /* cross-origin resource sharing (CORS) support */
static fs_query_state *query_state;
static GThreadPool* pool;
#define QUERY_THREAD_POOL_SIZE 16
static gboolean recv_fn (GIOChannel *source, GIOCondition condition, gpointer data);
static void http_import_queue_remove(client_ctxt *ctxt);
static void http_put_finished(client_ctxt *ctxt, const char *msg);
static FILE *ql_file = NULL;
static pid_t cpid = 0;
volatile static unsigned int last_query_id = 0;
/* set *set if the key value is set in kb_name, or default */
static void set_boolean(GKeyFile *keyfile, const char *kb_name, const char *key, int *set);
static void set_string(GKeyFile *keyfile, const char *kb_name, const char *key, const char **set);
static void query_log_open (const char *kb_name)
{
char *filename = g_strdup_printf("/var/log/4store/query-%s.log", kb_name);
ql_file= fopen(filename, "a");
if (ql_file) {
fprintf(ql_file, "\n# 4s-httpd for KB=%s, pid=%d #####\n", kb_name, getpid());
fflush(ql_file);
} else {
fs_error(LOG_WARNING, "couldn't open query log '%s' for appending: %s", filename, strerror(errno));
}
g_free(filename);
}
static void query_log_close ()
{
if (ql_file) {
fclose(ql_file);
ql_file = NULL;
}
}
static void query_log_reopen ()
{
query_log_close();
query_log_open(fsp_kb_name(fsplink));
}
static void query_log (client_ctxt *ctxt, const char *query)
{
if (ql_file) {
time_t now = time(NULL);
struct tm *ts = gmtime(&now);
char time_str[21];
strftime(time_str, sizeof(time_str), "%Y-%m-%dT%H:%M:%SZ", ts);
fprintf(ql_file, "##### %s Q%u\n%s\n", time_str, ctxt->query_id, query);
fflush(ql_file);
}
}
static void daemonize (void)
{
/* fork once, we don't want to be process leader */
switch(fork()) {
case 0:
break;
case -1:
fs_error(LOG_ERR, "fork() error starting daemon: %s", strerror(errno));
exit(1);
default:
_exit(0);
}
/* new session / process group */
if (setsid() == -1) {
fs_error(LOG_ERR, "setsid() failed starting daemon: %s", strerror(errno));
exit(1);
}
/* fork again, separating ourselves from our parent permanently */
switch(fork()) {
case 0:
break;
case -1:
fs_error(LOG_ERR, "fork() error starting daemon: %s", strerror(errno));
exit(1);
default:
_exit(0);
}
/* close stdin, stdout, stderr */
close(0); close(1); close(2);
/* use up some fds as a precaution against printf() getting
written to the wire */
open("/dev/null", 0);
open("/dev/null", 0);
open("/dev/null", 0);
/* move somewhere safe and known */
if (chdir("/")) {
fs_error(LOG_ERR, "chdir failed: %s", strerror(errno));
}
}
static char hexdigit[256] =
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static void url_decode (char *encoded)
{
unsigned char *chase = (unsigned char *) encoded;
while (*encoded) {
switch (*encoded) {
case '%': {
unsigned char first = encoded[1];
unsigned char second = encoded[2];
*chase = hexdigit[first] * 16 + hexdigit[second];
chase++;
encoded += 3;
break;
}
case '+':
*(chase++) = ' ';
encoded++;
break;
default:
/* no processing */
*(chase++) = *encoded;
encoded++;
break;
}
}
*chase = '\0'; /* terminate string */
}
static char *just_content_type(client_ctxt *ctxt)
{
char *result = NULL;
const char *content_type = g_hash_table_lookup(ctxt->headers, "content-type");
if (content_type) {
char **vector = g_strsplit(content_type, ";", 2);
result = g_strdup(vector[0]);
g_strfreev(vector);
}
return result;
}
static void http_send(client_ctxt *ctxt, const char *msg)
{
if (msg) {
send(ctxt->sock, msg, strlen(msg), 0 /* flags */);
} else {
fs_error(LOG_ERR, "tried to send NULL message");
}
}
static void http_header(client_ctxt *ctxt, const char *code, const char *mimetype)
{
http_send(ctxt, "HTTP/1.0 "); http_send(ctxt, code); http_send(ctxt, "\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
if (mimetype) {
http_send(ctxt, "Content-Type: "); http_send(ctxt, mimetype); http_send(ctxt, "\r\n");
}
if(IS_CORS(ctxt)) {
http_send(ctxt, "Access-Control-Allow-Origin: *\r\n");
}
http_send(ctxt, "X-Endpoint-Description: /description/"); http_send(ctxt, "\r\n");
http_send(ctxt, "\r\n");
}
static void http_code(client_ctxt *ctxt, const char *code)
{
http_send(ctxt, "HTTP/1.0 "); http_send(ctxt, code); http_send(ctxt, "\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
http_send(ctxt, "Content-Type: text/plain; charset=UTF-8\r\n");
if(IS_CORS(ctxt)) {
http_send(ctxt, "Access-Control-Allow-Origin: *\r\n");
}
http_send(ctxt, "\r\n");
http_send(ctxt, code); http_send(ctxt, "\n");
http_send(ctxt, "This is a 4store SPARQL server " GIT_REV "\n");
}
static void http_404(client_ctxt *ctxt, const char *url)
{
fs_error(LOG_INFO, "HTTP 404 for %s", url);
http_send(ctxt, "HTTP/1.0 404 Not found\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
http_send(ctxt, "Content-Type: text/plain; charset=UTF-8\r\n");
http_send(ctxt, "\r\n");
http_send(ctxt, "Not found\n");
http_send(ctxt, "This is a 4store SPARQL server " GIT_REV "\n");
http_send(ctxt, "Check /status/ for more info\n");
}
static void http_error(client_ctxt *ctxt, const char *error)
{
fs_error(LOG_INFO, "HTTP error, returning status %s", error);
http_code(ctxt, error);
}
static void http_redirect(client_ctxt *ctxt, const char *to)
{
http_send(ctxt, "HTTP/1.0 301 Moved permanently\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
http_send(ctxt, "Location: "); http_send(ctxt, to); http_send(ctxt, "\r\n");
http_send(ctxt, "Content-Type: text/html; charset=UTF-8\r\n");
http_send(ctxt, "\r\n");
http_send(ctxt, "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n");
http_send(ctxt, "<html><body><p>See <a href=\"");
http_send(ctxt, to);
http_send(ctxt, "\">"); http_send(ctxt, to); http_send(ctxt, "</a>\n");
http_send(ctxt, "<p>4store " GIT_REV "</p>");
http_send(ctxt, "</body></html>\n");
}
static void client_free(client_ctxt *ctxt)
{
if (ctxt->watchdog) {
g_source_remove(ctxt->watchdog);
}
if (ctxt->partial) {
g_byte_array_free(ctxt->partial, TRUE);
}
g_hash_table_destroy(ctxt->headers);
free(ctxt->request);
g_free(ctxt);
}
static void http_close(client_ctxt *ctxt)
{
GSource *s =
g_main_context_find_source_by_user_data(g_main_context_default(), ctxt);
if (s) g_source_destroy(s);
g_io_channel_shutdown(ctxt->ioch, TRUE, NULL);
g_io_channel_unref(ctxt->ioch);
client_free(ctxt);
}
static void http_query_worker(gpointer data, gpointer user_data)
{
client_ctxt *ctxt = (client_ctxt *) data;
ctxt->start_time = fs_time();
ctxt->qr = fs_query_execute(query_state, fsplink, bu, ctxt->query_string, ctxt->query_flags, opt_level, ctxt->soft_limit, 0);
if (ctxt->qr->errors) {
http_error(ctxt, "400 Parser error");
GSList *w = ctxt->qr->warnings;
if (w) {
http_send(ctxt, "\n");
}
while (w) {
http_send(ctxt, w->data);
http_send(ctxt, "\n");
w = w->next;
}
fs_query_free(ctxt->qr);
ctxt->qr = NULL;
if (ctxt->query_string) {
free(ctxt->query_string);
ctxt->query_string = NULL;
}
if (ctxt->output) {
g_free(ctxt->output);
ctxt->output = NULL;
}
http_close(ctxt);
return;
}
http_send(ctxt, "HTTP/1.0 200 OK\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
if(IS_CORS(ctxt)) {
http_send(ctxt, "Access-Control-Allow-Origin: *\r\n");
}
const char *accept = g_hash_table_lookup(ctxt->headers, "accept");
int rows_returned = -1;
fcntl(ctxt->sock, F_SETFL, 0 /* not O_NONBLOCK */); /* blocking */
FILE *fp = fdopen(dup(ctxt->sock), "a+");
if (fp != NULL) {
const char *type = "sparql"; /* default */
int flags = FS_RESULT_FLAG_HEADERS;
if (ctxt->output) {
type = ctxt->output;
} else if ((ctxt->qr->construct || ctxt->qr->describe) && accept && strstr(accept, "text/turtle")) {
type = "text";
fprintf(fp, "Content-Type: text/turtle\r\n\r\n");
flags = 0;
} else if ((ctxt->qr->construct || ctxt->qr->describe) && accept && strstr(accept, "application/rdf+xml")) {
type = "sparql";
} else if (accept && strstr(accept, "application/sparql-results+xml")) {
type = "sparql";
} else if (accept && strstr(accept, "application/sparql-results+json")) {
type = "json";
} else if (accept && strstr(accept, "application/json")) {
type = "json";
} else if (accept && strstr(accept, "text/tab-separated-values")) {
type = "text";
} else if (accept && strstr(accept, "text/csv")) {
type = "csv";
} else if (accept && strstr(accept, "text/plain")) {
type = "text";
fprintf(fp, "Content-Type: text/plain; charset=utf-8\r\n\r\n");
flags = 0;
}
fs_query_results_output(ctxt->qr, type, flags, fp);
rows_returned = ctxt->qr->rows_output;
fs_query_free(ctxt->qr);
ctxt->qr = NULL;
free(ctxt->query_string);
ctxt->query_string = NULL;
if (ctxt->output) {
g_free(ctxt->output);
ctxt->output = NULL;
}
fclose(fp);
}
if (ql_file) {
if (rows_returned > -1) {
fprintf(ql_file, "#### execution time for Q%u: %fs, returned %d rows.\n", ctxt->query_id, fs_time() - ctxt->start_time, rows_returned);
}
else {
fprintf(ql_file, "#### execution time for Q%u: %fs\n", ctxt->query_id, fs_time() - ctxt->start_time);
}
fflush(ql_file);
}
http_close(ctxt);
}
static void http_answer_query(client_ctxt *ctxt, const char *query)
{
ctxt->query_id = ++last_query_id;
query_log(ctxt, query);
ctxt->query_string = g_strdup(query);
ctxt->update_string = NULL;
g_source_remove_by_user_data(ctxt);
g_thread_pool_push(pool, ctxt, NULL);
}
static GSList *import_queue = NULL;
static gboolean import_watchdog (gpointer data)
{
client_ctxt *ctxt = (client_ctxt *) data;
/* something has gone wrong, we need to recover */
fs_error(LOG_ERR, "import watchdog fired");
http_put_finished(ctxt, "500 watchdog timeout fired");
return FALSE;
}
static void http_import_start(client_ctxt *ctxt)
{
/* If it's an update operation, we have a different path */
if (ctxt->update_string) {
#if RASQAL_VERSION > 917
char *message = NULL;
int ret = fs_update(query_state, ctxt->update_string, &message, unsafe);
fs_query_cache_flush(query_state, 0);
http_import_queue_remove(ctxt);
if (ret == 0) {
http_send(ctxt, "HTTP/1.0 200 OK\r\n");
} else {
http_send(ctxt, "HTTP/1.0 400 Bad argument\r\n");
}
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
http_send(ctxt, "Content-Type: text/plain; charset=utf-8\r\n\r\n");
if (message) {
http_send(ctxt, message);
g_free(message);
}
http_send(ctxt, "\n");
http_close(ctxt);
#else
http_import_queue_remove(ctxt);
http_send(ctxt, "HTTP/1.0 500 Not Implemented\r\n");
http_send(ctxt, "Server: 4s-httpd/" GIT_REV "\r\n");
http_send(ctxt, "Content-Type: text/plain; charset=utf-8\r\n\r\n");
http_send(ctxt, "SPARQL Update support requires rasqal 0.9.18 or newer\n");
http_send(ctxt, "\n");
http_close(ctxt);
#endif
return;
}
const char *length = g_hash_table_lookup(ctxt->headers, "content-length");
ctxt->bytes_left = atol(length);
ctxt->importing = 1;
fs_error(LOG_INFO, "starting import %s (%ld bytes)", ctxt->import_uri, ctxt->bytes_left);
g_io_add_watch(ctxt->ioch, G_IO_IN, recv_fn, ctxt);
const char *expect = g_hash_table_lookup(ctxt->headers, "expect");
if (expect && !strcasecmp(expect, "100-continue")) {
http_send(ctxt, "HTTP/1.0 100 Continue\r\n\r\n");
}
if (fsp_start_import_all(fsplink)) {
fs_error(LOG_ERR, "fsp_start_import_all failed");
http_import_queue_remove(ctxt);
http_error(ctxt, "500 failed during import-start");
http_close(ctxt);
return;
}
fs_rid_vector *mvec = fs_rid_vector_new(0);
fs_rid muri = fs_hash_uri(ctxt->import_uri);
fs_rid_vector_append(mvec, muri);
if (fsp_delete_model_all(fsplink, mvec) || fsp_new_model_all(fsplink, mvec)) {
fs_error(LOG_ERR, "fsp_{delete,new}_model_all failed");
fs_rid_vector_free(mvec);
fsp_stop_import_all(fsplink);
http_import_queue_remove(ctxt);
http_error(ctxt, "500 failed during import-start");
http_close(ctxt);
return;
}
fs_rid_vector_free(mvec);
char *type = just_content_type(ctxt);
if (fs_import_stream_start(fsplink, ctxt->import_uri, type, has_o_index, &global_import_count)) {
fs_error(LOG_CRIT, "failed to start stream parse");
}
g_free(type);
guint timeout = 30 + (ctxt->bytes_left / WATCHDOG_RATE);
ctxt->watchdog = g_timeout_add(1000 * timeout, import_watchdog, ctxt);
if (ctxt->bytes_left == 0) {
http_put_finished(ctxt, NULL);
}
}
static void http_post_data(client_ctxt *ctxt, char *model, const char *content_type, char *data)
{
ctxt->importing = 1;
long int length = strlen(data);
fs_error(LOG_INFO, "starting add to %s (%ld bytes)", model, length);
if (fsp_start_import_all(fsplink)) {
fs_error(LOG_ERR, "fsp_start_import_all failed");
http_import_queue_remove(ctxt);
http_error(ctxt, "500 failed during import-start");
http_close(ctxt);
return;
}
fs_import_stream_start(fsplink, model, content_type, has_o_index, &global_import_count);
guint timeout = 30 + (ctxt->bytes_left / WATCHDOG_RATE);
ctxt->watchdog = g_timeout_add(1000 * timeout, import_watchdog, ctxt);
fs_import_stream_data(fsplink, (unsigned char *)data, length);
if (ctxt->watchdog) {
g_source_remove(ctxt->watchdog);
ctxt->watchdog = 0;
}
int error_count = -1;
fs_import_stream_finish(fsplink, &global_import_count, &error_count);
all_time_import_count += global_import_count;
global_import_count = 0;
fsp_stop_import_all(fsplink);
fs_query_cache_flush(query_state, 0);
ctxt->importing = 0;
fs_error(LOG_INFO, "finished add to %s", model);
http_import_queue_remove(ctxt);
if (error_count == -1) {
fs_error(LOG_ERR, "import_stream_finish didn't complete successfully");
http_error(ctxt, "500 server problem while importing");
} else if (error_count > 0) {
http_error(ctxt, "400 RDF parser reported errors");
} else {
http_code(ctxt, "200 added successfully");
}
http_close(ctxt);
}
static void http_import_queue_remove(client_ctxt *ctxt)
{
import_queue = g_slist_remove(import_queue, ctxt);
if (import_queue) {
http_import_start((client_ctxt *) import_queue->data);
}
}
static void http_put_finished(client_ctxt *ctxt, const char *msg)
{
if (ctxt->watchdog) {
g_source_remove(ctxt->watchdog);
ctxt->watchdog = 0;
}
int error_count = -1;
fs_import_stream_finish(fsplink, &global_import_count, &error_count);
all_time_import_count += global_import_count;
global_import_count = 0;
fsp_stop_import_all(fsplink);
fs_query_cache_flush(query_state, 0);
ctxt->importing = 0;
if (ctxt->bytes_left) {
fs_error(LOG_INFO, "finished import %s (%ld bytes left)", ctxt->import_uri, ctxt->bytes_left);
} else {
fs_error(LOG_INFO, "finished import %s", ctxt->import_uri);
}
g_free(ctxt->import_uri);
ctxt->import_uri = NULL;
http_import_queue_remove(ctxt);
if (msg) {
http_error(ctxt, msg);
} else if (error_count == -1) {
fs_error(LOG_ERR, "import_stream_finish didn't complete successfully");
http_error(ctxt, "500 server problem while importing");
} else if (error_count > 0) {
http_error(ctxt, "400 RDF parser reported errors");
} else {
http_code(ctxt, "201 imported successfully");
}
http_close(ctxt);
}
static void http_put_request(client_ctxt *ctxt, gchar *url, gchar *protocol)
{
if (!strncmp(url, "/data/?graph=", 13)) { /* SPARQL 1.1 way */
url += 13;
url_decode(url);
} else if (!strncmp(url, "/data/", 6)) { /* pre-SPARQL 1.1 4store way */
url += 6;
url_decode(url);
} else if (!strncmp(url, "/", 1)) {
http_error(ctxt, "403 forbidden - invalid URI");
http_close(ctxt);
return;
}
if (!g_hash_table_lookup(ctxt->headers, "content-length")) {
http_error(ctxt, "411 content length required");
http_close(ctxt);
return;
}
ctxt->import_uri = g_strdup(url);
ctxt->update_string = NULL;
g_source_remove_by_user_data(ctxt);
if (import_queue) {
import_queue = g_slist_append(import_queue, ctxt);
} else {
import_queue = g_slist_append(import_queue, ctxt);
http_import_start(ctxt);
}
}
static void http_delete_request(client_ctxt *ctxt, gchar *url, gchar *protocol)
{
if (!strncmp(url, "/data/?graph=", 13)) { /* SPARQL 1.1 way */
url += 13;
url_decode(url);
} else if (!strncmp(url, "/data/", 6)) { /* pre-SPARQL 1.1 4store syntax */
url += 6;
url_decode(url);
} else if (!strncmp(url, "/", 1)) {
http_error(ctxt, "403 forbidden - invalid URI");
http_close(ctxt);
return;
}
/* now we have a UTF-8 raw string */
fs_rid_vector *mvec = fs_rid_vector_new(0);
fs_rid muri = fs_hash_uri(url);
fs_rid_vector_append(mvec, muri);
if (muri == fs_c.system_config) {
fs_import_reread_config();
}
if (fsp_delete_model_all(fsplink, mvec)) {
fs_error(LOG_ERR, "error while trying to delete model <%s>", url);
http_error(ctxt, "500 failed while adding new model");
} else {
fs_query_cache_flush(query_state, 0);
fs_error(LOG_INFO, "deleted model <%s>", url);
http_error(ctxt, "200 deleted successfully");
}
fs_rid_vector_free(mvec);
http_close(ctxt);
}
static void http_status_report(client_ctxt *ctxt)
{
http_send(ctxt, "HTTP/1.0 200 OK\r\n"
"Server: 4s-httpd/" GIT_REV "\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
"<html><head><title>SPARQL httpd server status</title></head>\n"
"<body><h1>SPARQL httpd server " GIT_REV " status</h1>\n"
"<h2>KB ");
http_send(ctxt, fsp_kb_name(fsplink));
http_send(ctxt, "</h2>\n");
char *running = g_strdup_printf("%u", g_thread_pool_get_num_threads(pool));
char *outstanding = g_strdup_printf("%u", g_thread_pool_unprocessed(pool));
http_send(ctxt, "<table><tr><th>Running queries</th><td>");
http_send(ctxt, running); http_send(ctxt, "</td></tr>\n");
http_send(ctxt, "<tr><th>Outstanding queries</th><td>");
http_send(ctxt, outstanding); http_send(ctxt, "</td></tr>\n");
http_send(ctxt, "</table>\n");
g_free(running);
g_free(outstanding);
http_send(ctxt, "<p><a href=\"/status/size/\">4store backend size info</a></p>\n");
http_send(ctxt, "<p><a href=\"/test/\">Execute a test query</a></p>\n");
if (import_queue) { /* this must be synchronous for now */
http_send(ctxt, "<h3>Imports in progress</h3>\n");
GSList *queue;
for (queue = import_queue; queue; queue = queue->next) {
client_ctxt *import_ctxt = (client_ctxt *) queue->data;
http_send(ctxt, import_ctxt->import_uri);
http_send(ctxt, "<br>\n");
}
char *flushed = g_strdup_printf("<p># triples from this import so far: %d</p>\n", global_import_count);
http_send(ctxt, flushed);
g_free(flushed);
}
char *triples = g_strdup_printf("<p>Total # triples imported: %ld</p>\n", all_time_import_count);
http_send(ctxt, triples);
g_free(triples);
http_send(ctxt, "</body></html>\n");
http_close(ctxt);
}
static void http_size_report(client_ctxt *ctxt)
{
http_send(ctxt, "HTTP/1.0 200 OK\r\n"
"Server: 4s-httpd/" GIT_REV "\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
"<html><head><title>SPARQL httpd server status - size</title></head>\n"
"<body><h1>SPARQL httpd server " GIT_REV " status - size</h1>\n"
"<h2>KB ");
http_send(ctxt, fsp_kb_name(fsplink));
http_send(ctxt, "</h2>\n");
fs_data_size sz, total = {0, 0, 0, 0, 0, 0};
http_send(ctxt,"<table><tr><th>Segment #</th><th>quads (s)</th><th>quads (sr)</th>\
<th>models</th><th>resources</th></tr>\n");
const int segments = fsp_link_segments(fsplink);
for (fs_segment seg = 0; seg < segments; ++seg) {
if (fsp_get_data_size(fsplink, seg, &sz)) {
http_send(ctxt, "<tr><td>Unexpected error fetching size info from segment</td></tr>\n");
} else {
char *line = g_strdup_printf("<tr><td>%d</td><td>%lld</td><td>%+lld</td><td>%lld</td><td>%lld</td></tr>\n",
seg, sz.quads_s, sz.quads_sr - sz.quads_s, sz.models_s, sz.resources);
http_send(ctxt, line);
g_free(line);
}
total.quads_s += sz.quads_s;
total.quads_sr += sz.quads_sr;
total.quads_o += sz.quads_o;
if (sz.models_s > total.models_s) total.models_s = sz.models_s;
total.resources += sz.resources;
}
char *final = g_strdup_printf("<tr><th>Total</th><td>%lld</td><td>%+lld</td><td>%lld</td><td>%lld</td></tr>\n",
total.quads_s, total.quads_sr - total.quads_s , total.models_s, total.resources);
http_send(ctxt, final);
g_free(final);
http_send(ctxt, "</body></html>\n");
http_close(ctxt);
}
static void http_service_description(client_ctxt *ctxt)
{
http_send(ctxt, "HTTP/1.0 200 OK\r\n"
"Server: 4s-httpd/" GIT_REV "\r\n"
"Content-Type: text/turtle; charset=utf-8\r\n"
"\r\n"
"@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
"@prefix sd: <http://darq.sf.net/dose/0.1#> .\n"
"@prefix saddle: <http://www.w3.org/2005/03/saddle/#> .\n"
"@prefix sparql: <http://kasei.us/2008/04/sparql#> .\n"
"@prefix void: <http://rdfs.org/ns/void#> .\n"
"[] a sd:Service ;\n"
" saddle:queryLanguage [\n"
" rdfs:label \"SPARQL\" ;\n"
" saddle:spec <http://www.w3.org/TR/rdf-sparql-query/>\n"
" ] ;\n"
" saddle:queryLanguage [\n"
" rdfs:label \"RDQL\" ;\n"
" saddle:spec <http://www.w3.org/Submission/RDQL/>\n"
" ] ;\n"
" saddle:resultFormat [\n"
" rdfs:label \"SPARQL Query Results XML\" ;\n"
" saddle:mediaType \"application/sparql-results+xml\" ;\n"
" saddle:spec <http://www.w3.org/TR/rdf-sparql-XMLres/>\n"
" ] ;\n"
" saddle:resultFormat [\n"
" rdfs:label \"RDF/XML\" ;\n"
" saddle:mediaType \"application/rdf+xml\" ;\n"
" saddle:spec <http://www.w3.org/TR/rdf-syntax/>\n"
" ] ;\n"
" saddle:resultFormat [\n"
" rdfs:label \"SPARQL Query Results JSON\" ;\n"
" saddle:mediaType \"application/sparql-results+json\" ;\n"
" saddle:spec <http://www.w3.org/TR/rdf-sparql-json-res/>\n"
" ] ;\n"
" saddle:resultFormat [\n"
" rdfs:label \"SPARQL Query Results UTF-8 text\" ;\n"
" saddle:mediaType \"text/plain\" ;\n"
" saddle:spec <http://example.org/sparql/text-format/>\n"
" ] ;\n"
" sparql:sparqlExtension <http://kasei.us/2008/04/sparql-extension/service> ;\n");
http_send(ctxt, " rdfs:label \"4store SPARQL Endpoint for ");
http_send(ctxt, fsp_kb_name(fsplink));
http_send(ctxt, " (" GIT_REV ")\" ;\n");
fs_data_size sz, total = {0, 0, 0, 0, 0, 0};
const int segments = fsp_link_segments(fsplink);
for (fs_segment seg = 0; seg < segments; ++seg) {
if (fsp_get_data_size(fsplink, seg, &sz)) {
http_send(ctxt, "# Unexpected error fetching size info from segment\n");
}
total.quads_s += sz.quads_s;
total.quads_sr += sz.quads_sr;
total.quads_o += sz.quads_o;
if (sz.models_s > total.models_s) total.models_s = sz.models_s;
total.resources += sz.resources;
}
char *size = g_strdup_printf(" sd:totalTriples %llu ;\n sd:totalResources %llu ;\n", total.quads_s, total.resources);
http_send(ctxt, size);
g_free(size);
http_send(ctxt, ".\n");
http_close(ctxt);
}
static void http_query_widget(client_ctxt *ctxt)
{
http_send(ctxt, "HTTP/1.0 200 OK\r\n"
"Server: 4s-httpd/" GIT_REV "\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"\r\n"
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">\n"
"<html><head><title>SPARQL httpd test query</title></head>\n"
"<body><h1>SPARQL httpd server " GIT_REV " test query</h1>\n"
"<h2>KB ");
http_send(ctxt, fsp_kb_name(fsplink));
http_send(ctxt, "</h2>\n");
http_send(ctxt, "<form action=\"../sparql/\" method=\"post\">\n"
"<textarea name=\"query\" cols=\"80\" rows=\"18\">\n"
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
"\nSELECT * WHERE {\n ?s ?p ?o\n} LIMIT 10\n"
"</textarea><br>\n"
"<em>Soft limit</em> <input type=\"text\" name=\"soft-limit\">\n"
"<input type=\"submit\" value=\"Execute\"><input type=\"reset\">\n"
"</form>\n");
http_send(ctxt, "</body></html>\n");
http_close(ctxt);
}
static void http_get_request(client_ctxt *ctxt, gchar *url, gchar *protocol)
{
char *default_graph = NULL; /* ignored for now */
char *qm = strchr(url, '?');
char *qs = qm ? qm + 1 : NULL;
if (qs) {
*qm = '\0';
}
char *path = url;
url_decode(path);
if (!strcmp(path, "/sparql/")) {
char *query = NULL;
while (qs) {
char *ampersand = strchr(qs, '&');
char *next = ampersand ? ampersand + 1 : NULL;
if (next) {
*ampersand = '\0';
}
char *key = qs;
char *equals = strchr(qs, '=');
char *value = equals ? equals + 1 : NULL;
if (equals) {
*equals = '\0';
}
url_decode(key);
if (!strcmp(key, "query") && value) {
url_decode(value);
query = value;
} else if (!strcmp(key, "restricted")) {
ctxt->query_flags |= FS_QUERY_RESTRICTED;
} else if (!strcmp(key, "soft-limit") && value) {
url_decode(value);
if (strlen(value)) { /* ignore empty string, default form value */
ctxt->soft_limit = atoi(value);
if(ctxt->soft_limit == 0) {
ctxt->soft_limit = -1;
}
}
} else if (!strcmp(key, "output") && value) {
url_decode(value);
ctxt->output = g_strdup(value);
} else if (!strcmp(key, "default-graph-uri") && value) {
url_decode(value);
default_graph = value;
}
qs = next;
}
if (query) {
http_answer_query(ctxt, query);
} else {
http_error(ctxt, "500 SPARQL protocol error");
http_close(ctxt);
}
} else if (!strcmp(path, "/update/")) {
http_error(ctxt, "500 SPARQL protocol error, update requests must use POST");
http_close(ctxt);
} else if (!strcmp(path, "/sparql")) {
http_redirect(ctxt, "sparql/");
http_close(ctxt);
} else if (!strcmp(path, "/status/")) {
http_status_report(ctxt);
} else if (!strcmp(path, "/status")) {
http_redirect(ctxt, "status/");
http_close(ctxt);
} else if (!strcmp(path, "/status/size/")) {
http_size_report(ctxt);
} else if (!strcmp(path, "/description/")) {
http_service_description(ctxt);
} else if (!strcmp(path, "/test/")) {
http_query_widget(ctxt);
} else if (!strcmp(path, "/test")) {
http_redirect(ctxt, "test/");
http_close(ctxt);
} else {
http_404(ctxt, url);
http_close(ctxt);
}
}
static void http_head_request(client_ctxt *ctxt, gchar *url, gchar *protocol)
{
char *qm = strchr(url, '?');
char *qs = qm ? qm + 1 : NULL;
if (qs) {
*qm = '\0';
}
char *path = url;
url_decode(path);
if (!strcmp(path, "/sparql/")) {
http_header(ctxt, "200", "application/sparql-results+xml");
} else if (!strcmp(path, "/status/")) {
http_header(ctxt, "200", "text/html; charset=UTF-8");
} else if (!strcmp(path, "/status/size/")) {
http_header(ctxt, "200", "text/html; charset=UTF-8");
} else if (!strcmp(path, "/test/")) {
http_header(ctxt, "200", "text/html; charset=UTF-8");
} else {
http_header(ctxt, "404", "text/plain; charset=UTF-8");
}
http_close(ctxt);
}
static void http_post_request(client_ctxt *ctxt, gchar *url, gchar *protocol)
{
char *default_graph = NULL; /* ignored for now */
url_decode(url);
if (!strcmp(url, "/sparql/")) {