-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathjson.cpp
More file actions
3741 lines (3452 loc) · 115 KB
/
json.cpp
File metadata and controls
3741 lines (3452 loc) · 115 KB
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
#include "common.h"
#ifdef INFORMATION
Json units(JSON arraysand objects) consist of a dictionary entry labelled ja - ... or jo - ...
Facts are stored on these.When initially created as a permanent unit, there is a special
fact to represent the empty value so the dictionary entry will not disappear across volleys.
Otherwise it would be destroyedand a new unit arbitrarily created later that has no connection
to the one the user just created.
When you add an item to an array via JsonArrayInsert, this empty fact is destroyed.And when the
last array value is destroyed via JsonArrayDelete, the marker fact is recreated.
When you add an item to an object via JsonObjectInsert or by direct assignment, the marker fact
is destroyed.When you use Assign of null to a json object, if that was the last field, then the
marker fact is recreated.
The marker fact is normally bypassed by all accesses to the items of a json object(so it does not
query or count or whatever) except when specially accessed by the above routines.
#endif
// GENERAL JSON SUPPORT
char detectedlanguage[100];
unsigned int build0jid = 0;
unsigned int build1jid = 0;
unsigned int buildbootjid = 0;
unsigned int builduserjid = 0;
unsigned int buildtransientjid = 0;
int conceptSeen = 0;
MEANING jsonconceptName;
char* jsonconceptname;
static char jsonoldlang[MAX_WORD_SIZE];
#include "jsmn.h"
static bool jsonpurify = false;
static bool curl_done_init = false;
static int jsonCreateFlags = 0;
#define MAX_JSON_LABEL 50
static char jsonLabel[MAX_JSON_LABEL + 1];
bool safeJsonParse = false;
int jsonIdIncrement = 1;
int jsonDefaults = 0;
int json_open_counter = 0;
uint64 json_open_time = 0;
bool curlFail = false;
int jsonStore = 0; // where to put json fact refs
int jsonIndex;
int jsonOpenSize = 0;
static unsigned int jsonPermanent = FACTTRANSIENT;
bool jsonNoArrayduplicate = false;
bool jsonObjectDuplicate = false;
bool jsonSetDuplicate = false;
bool jsonDontKill = false;
bool directJsonText = false;
static char* curlBufferBase = NULL;
static int objectcnt = 0;
static FunctionResult JSONpath(char* buffer, char* path, char* jsonstructure, bool raw, bool nofail);
static MEANING jcopy(MEANING M);
typedef enum {
URL_SCHEME = 0,
URL_AUTHORITY,
URL_PATH,
URL_QUERY,
URL_FRAGMENT
} urlSegment;
static bool IsTransientJson(char* name)
{
return name[3] == 't';
}
static bool IsBootJson(char* name)
{
return name[3] == 'b';
}
static bool IsDuplicate(char* name)
{
if (IsTransientJson(name)) return (name[4] == '+');
else return name[3] == '+';
}
static bool AllowDuplicates(char* name, bool checkedArgs = false)
{
bool defaultDup = false;
// duplicate/unique can be specified in JSON args to override the default or definition
if (IsValidJSONName(name, 'o'))
{
if (checkedArgs && jsonSetDuplicate) return jsonObjectDuplicate;
defaultDup = jsonDefaults & JSON_OBJECT_DUPLICATE;
}
else if (IsValidJSONName(name, 'a'))
{
if (checkedArgs && jsonSetDuplicate) return !jsonNoArrayduplicate;
defaultDup = !(jsonDefaults & JSON_ARRAY_UNIQUE);
}
else return false;
// fallback is via the actual name
if (IsDuplicate(name)) return true;
// transients will have been created with/without the +
// but permanents may have been created before the + was used
if (!IsTransientJson(name)) return defaultDup;
return false;
}
void InitJson()
{
json_open_counter = 0;
json_open_time = 0;
jsonDefaults = 0;
curlFail = false;
InitJSONNames();
}
static void JsonReuseKill(FACT* F);
static void jreusekillfact(WORDP D)
{
if (!D) return;
FACT* F = GetSubjectNondeadHead(D); // delete everything including marker
while (F)
{
FACT* G = GetSubjectNondeadNext(F);
if (F->flags & (JSON_ARRAY_FACT | JSON_OBJECT_FACT)) JsonReuseKill(F); // json object/array no longer has this fact
if (F->flags & FACTAUTODELETE) { ; } // AutoKillFact(F->object); // kill the fact
F = G;
}
}
static void JsonReuseKill(FACT* F)
{
if (!F || F->flags & FACTDEAD) return; // already dead
if (F <= factsPreBuild[currentBeforeLayer])
return; // may not kill off facts built into world
F->flags |= FACTDEAD;
if (trace & TRACE_FACT && CheckTopicTrace())
{
Log(USERLOG, "Kill: ");
TraceFact(F);
}
if (F->flags & FACTAUTODELETE) // not doing facts as fields here
{
return;
// if (F->flags & FACTSUBJECT) AutoKillFact(F->subject);
// if (F->flags & FACTVERB) AutoKillFact(F->verb);
// if (F->flags & FACTOBJECT) AutoKillFact(F->object);
}
// recurse on JSON datastructures below if they are being deleted on right side
if (F->flags & (JSON_ARRAY_VALUE | JSON_OBJECT_VALUE))
{
WORDP jsonarray = Meaning2Word(F->object);
// should it recurse to kill guy refered to?
// if no other living fact refers to it, you can also kill the referred object/array
FACT* H = GetObjectNondeadHead(jsonarray); // facts which link to this
if (!H) jkillfact(jsonarray);
}
// we dont renumber arrays because we will be destroying the whole thing
// if (F->flags & JSON_ARRAY_FACT) JsonRenumber(F);// have to renumber this array
if (planning) SpecialFact(Fact2Index(F), 0, 0); // save to restore
// if this fact has facts depending on it, they too must die
FACT* G = GetSubjectNondeadHead(F);
while (G)
{
JsonReuseKill(G);
G = GetSubjectNondeadNext(G);
}
G = GetVerbNondeadHead(F);
while (G)
{
JsonReuseKill(G);
G = GetVerbNondeadNext(G);
}
G = GetObjectNondeadHead(F);
while (G)
{
JsonReuseKill(G);
G = GetObjectNondeadNext(G);
}
F->subject = Fact2Index(factFreeList);
factFreeList = F;
}
FunctionResult JsonReuseKillCode(char* buffer)
{
char* arg = ARGUMENT(1);
if (!IsValidJSONName(arg)) return FAILRULE_BIT;
WORDP D = FindWord(arg);
FACT* F = GetSubjectNondeadHead(D);
while (F)
{
JsonReuseKill(F);
F = GetSubjectNondeadNext(F);
}
return NOPROBLEM_BIT;
}
static int JSONArgs()
{
int index = 1;
directJsonText = false;
bool used = false;
jsonCreateFlags = 0;
jsonPermanent = FACTTRANSIENT; // default
jsonNoArrayduplicate = false;
jsonObjectDuplicate = false;
jsonSetDuplicate = false;
if (jsonDefaults & JSON_ARRAY_UNIQUE) jsonNoArrayduplicate = true;
if (jsonDefaults & JSON_OBJECT_DUPLICATE) jsonObjectDuplicate = true;
char* arg1 = ARGUMENT(1);
if (*arg1 == '"') // remove quotes
{
++arg1;
size_t len = strlen(arg1);
if (arg1[len - 1] == '"') arg1[len - 1] = 0;
}
char word[MAX_WORD_SIZE];
while (*arg1)
{
arg1 = ReadCompiledWord(arg1, word);
if (!stricmp(word, (char*)"permanent"))
{
jsonPermanent = 0;
used = true;
}
else if (!stricmp(word, (char*)"boot")) // build to migrate to system boot layer
{
jsonPermanent = FACTBOOT;
used = true;
}
else if (!stricmp(word, (char*)"USER_FLAG3"))
{
jsonCreateFlags |= USER_FLAG3;
used = true;
}
else if (!stricmp(word, (char*)"USER_FLAG2"))
{
jsonCreateFlags |= USER_FLAG2;
used = true;
}
else if (!stricmp(word, (char*)"USER_FLAG1"))
{
jsonCreateFlags |= USER_FLAG1;
used = true;
}
else if (!stricmp(word, (char*)"autodelete"))
{
jsonCreateFlags |= FACTAUTODELETE;
used = true;
}
else if (!stricmp(word, (char*)"unique"))
{
jsonNoArrayduplicate = true;
jsonObjectDuplicate = false;
jsonSetDuplicate = true;
used = true;
}
else if (!stricmp(word, (char*)"duplicate"))
{
jsonObjectDuplicate = true;
jsonNoArrayduplicate = false;
jsonSetDuplicate = true;
used = true;
}
else if (!stricmp(word, (char*)"transient")) used = true;
else if (!stricmp(word, (char*)"direct")) used = directJsonText = true; // used by jsonopen
else if (!stricmp(word, (char*)"safe")) safeJsonParse = used = true;
if (!used) break; // must find at start
}
if (used) ++index;
return index;
}
void InitJSONNames()
{
objectcnt = 0; // also for json arrays
jsonIdIncrement = 1;
}
MEANING GetUniqueJsonComposite(char* prefix, unsigned int permanent)
{
int index = 0;
char* permanence = "";
char newlabel[100];
char* label = jsonLabel;
if (permanent == FACTTRANSIENT)
{
permanence = "t";
index = ++buildtransientjid;
}
else if (jsonPermanent == FACTTRANSIENT)
{
permanence = "t";
index = ++buildtransientjid;
}
else if (jsonPermanent == FACTBOOT)
{
permanence = "b";
index = ++buildbootjid;
}
else if (compiling && csapicall == NO_API_CALL && buildID == BUILD0)
{
index = ++build0jid;
sprintf(newlabel, "qx%s", jsonLabel); // build0 not api
label = newlabel;
}
else if (compiling && csapicall == NO_API_CALL && buildID == BUILD1)
{
index = ++build1jid;
sprintf(newlabel, "qy%s", jsonLabel); // buil1 not api
label = newlabel;
}
else
{
index = ++builduserjid;
sprintf(newlabel, "qu%s", jsonLabel); // api composite
label = newlabel;
}
const char* dup = (!jsonNoArrayduplicate || jsonObjectDuplicate) ? "+" : "";
char namebuff[MAX_WORD_SIZE];
sprintf(namebuff, "%s%s%s%s%d", prefix, permanence, dup, label, index);
WORDP D = StoreWord(namebuff, AS_IS);
return MakeMeaning(D);
}
bool IsValidJSONName(char* word, char type)
{
size_t n = strlen(word);
if (n < 4) return false;
// must at least start with the right prefix
if (word[0] != 'j' || word[2] != '-') return false;
if (type)
{
if (word[1] != type) return false;
}
else if (word[1] != 'a' && word[1] != 'o') return false;
// the label cannot contain certain characters
if (strchr(word, ' ') ||
strchr(word, '\\') || strchr(word, '"') || strchr(word, '\'') || strchr(word, 0x7f) || strchr(word, '\n') // refuse illegal content
|| strchr(word, '\r') || strchr(word, '\t')) return false; // must be legal unescaped json content and safe CS content
// jo-abc@def.com is not a valid JSON name
// last part must be numbers
char* end = word + n - 1;
char* at = end;
while (IsDigit(*at)) --at;
if (at == end) return false;
long len = at - word - 3;
if (IsTransientJson(word) || IsBootJson(word)) --len;
if (IsDuplicate(word)) --len;
if (len > MAX_JSON_LABEL) return false;
return true;
}
static char* IsJsonNumber(char* str)
{
if (IsDigit(*str) || (*str == '-' && IsDigit(str[1]))) // +number is illegal in json
{
// validate the number
char* at = str;
if (*at != '-') --at;
bool periodseen = false;
bool exponentseen = false;
while (*++at)
{
if (*at == '.' && !periodseen && !exponentseen) periodseen = true;
else if ((*at == 'e' || *at == 'E') && !exponentseen)
{
if (IsSign(at[1])) ++at;
exponentseen = true;
}
else if (*at == ' ' || *at == ',' || *at == '}' || *at == ']') return at;
else if (!IsDigit(*at)) return NULL; // cannot be number
}
return at;
}
return NULL;
}
static void StripEscape(char* from) // some escaped characters to normal, since cs doesnt use them
{
char* at = from;
while ((at = strchr(at, '\\')))
{
char* base = at++; // base is start of escaped string
if (*at != 'u' || !IsHexDigit(at[1]) || !IsHexDigit(at[2]) || !IsHexDigit(at[3]) || !IsHexDigit(at[4]))
{
if (*at == '"' || *at == '\\') // json escaped \" stuff is not escaped in CS
{
memmove(base, at, strlen(base)); // skip over the escaped char (in case its a \)
at = base + 1;
}
}
}
}
static int factsJsonHelper(int depth, char* jsontext, jsmntok_t* tokens, int currToken, MEANING* retMeaning, int* flags, bool key, bool nofail, int enableadjust) {
// Always build with duplicate on. create a fresh copy of whatever
jsmntok_t curr = tokens[currToken];
*flags = 0;
int retToken = currToken + 1;
size_t size = curr.end - curr.start;
char* text = jsontext + curr.start;
char endchar = text[size];
text[size] = 0; // terminating token allows us to keep mods to it from affecting other tokens
switch (curr.type) {
case JSMN_PRIMITIVE: { // true false, numbers, null
if (size >= 100)
{
if (!nofail) ReportBug((char*)"Bad Json primitive size %s", jsontext); // if we are not expecting it to fail
return 0;
}
// see if its string literal using single quotes instead of double quotes.
if (*text == '\'' && text[size - 1] == '\'')
{
*flags = JSON_STRING_VALUE; // string null
text[size - 1] = 0;
++text;
StripEscape(text);
if (jsonpurify) PurifyInput(text, text, size, INPUT_PURIFY); // change out special or illegal characters
// enableadjust bit 2 means do not return this field
if (!(enableadjust & 2)) *retMeaning = MakeMeaning(StoreWord(text, AS_IS));
else *retMeaning = 1; // lie
break;
}
if (!strncmp(text, "ja-", 3)) *flags = JSON_ARRAY_VALUE;
else if (!strnicmp(text, "jo-", 3)) *flags = JSON_OBJECT_VALUE;
else *flags = JSON_PRIMITIVE_VALUE; // json primitive type
if (*text == USERVAR_PREFIX && text[1] == '^')
{ // user function variable does not have a value we want
*flags = JSON_STRING_VALUE;
}
else if (*text == USERVAR_PREFIX || *text == SYSVAR_PREFIX || *text == '_' || *text == '\'') // variable values from CS
{
// get path to safety if any
char mainpath[MAX_WORD_SIZE];
char* path = strchr(text, '.');
char* pathbracket = strchr(text, '[');
char* first = path;
if (pathbracket && path && pathbracket < path) first = pathbracket;
if (first) strcpy(mainpath, first);
else *mainpath = 0;
if (path) *path = 0;
if (pathbracket) *pathbracket = 0;
char word1[MAX_WORD_SIZE];
FunctionResult result;
ReadShortCommandArg(text, word1, result); // get the basic item
strcpy(text, word1);
char* numberEnd = NULL;
// now see if we must process a path
if (*mainpath) // access field given
{
char word[MAX_WORD_SIZE];
result = JSONpath(word, mainpath, word, true, nofail); // raw mode
if (result != NOPROBLEM_BIT)
{
if (!nofail) ReportBug((char*)"INFO: Bad Json path building facts from template %s%s data", text, mainpath, jsontext); // if we are not expecting it to fail
return 0;
}
else strcpy(word, word);
}
if (!*text) // empty string treated as null
{
strcpy(text, (char*)"null");
*flags = JSON_STRING_VALUE; // string null
}
else if (!strcmp(text, (char*)"true") || !strcmp(text, (char*)"false"))
{
}
else if (!strncmp(text, (char*)"ja-", 3) || !strncmp(text, (char*)"jo-", 3))
{
*flags = (text[1] == 'a') ? JSON_ARRAY_VALUE : JSON_OBJECT_VALUE;
if (!(enableadjust & 2))
{
MEANING M = jcopy(MakeMeaning(StoreWord(text, AS_IS)));
WORDP D = Meaning2Word(M);
strcpy(text, D->word);
}
else *text = 0;
}
else if ((numberEnd = IsJsonNumber(text)) && numberEnd == (text + strlen(text))) { ; }
else *flags = JSON_STRING_VALUE; // cannot be number
}
if (!(enableadjust & 2)) *retMeaning = MakeMeaning(StoreWord(text, AS_IS));
else *retMeaning = 1; // lie
break;
}
case JSMN_STRING:
{
StripEscape(text);
if (jsonpurify) PurifyInput(text, text, size, INPUT_PURIFY); // change out special or illegal characters
*flags = JSON_STRING_VALUE; // string null
if (enableadjust & 8) // convert to underscore all spaces in string
{
char* under = text - 1;
while ((under = strchr(under, ' '))) *under = '_';
}
if (!(enableadjust & 2))
{
*retMeaning = MakeMeaning(StoreWord(text, AS_IS));
}
else *retMeaning = 1;
break;
}
case JSMN_OBJECT:
{
// Build the object name
MEANING objectName = 0;
if (!(enableadjust & 2))
{
objectName = GetUniqueJsonComposite((char*)"jo-");
*retMeaning = objectName;
}
else *retMeaning = 1;
for (int i = 0; i < curr.size / 2; i++) // each entry takes an id and a value
{
MEANING keyMeaning = 0;
int jflags = 0;
retToken = factsJsonHelper(depth+1,jsontext, tokens, retToken, &keyMeaning, &jflags, true, nofail, enableadjust);
if (retToken == 0) return 0;
// dont expand into facts?
int oldenable = enableadjust;
WORDP field = Meaning2Word(keyMeaning);
char* fieldname = field->word;
// high speed concepts bypass enable
// concepts": [{"name": "~badanswerwords", "values": ["confuse
if (conceptSeen && depth <= conceptSeen) // reset after completion of concept
{
conceptSeen = 0;
if (*detectedlanguage) SetLanguage(jsonoldlang); // restore prior language
}
if (!stricmp(fieldname, "concepts"))
{ // while whole of json is in default language, the concepts are in given language
// which may not have been parsed yet. so we change language since we looked ahead
conceptSeen = depth;
if (*detectedlanguage)
{
SetLanguage(detectedlanguage); // restore prior language
}
}
if ((enableadjust & 1) && field->internalBits & SETIGNORE) enableadjust |= 2;
if ((enableadjust & 4) && field->internalBits & SETUNDERSCORE) enableadjust |= 8;
MEANING valueMeaning = 0;
retToken = factsJsonHelper(depth+1,jsontext, tokens, retToken, &valueMeaning, &jflags, false, nofail, enableadjust);
if (retToken == 0) return 0;
// dont swallow null field value, discard field - but accept null input field
char* key = Meaning2Word(keyMeaning)->word;
char* value = Meaning2Word(valueMeaning)->word;
if (conceptSeen && !strcmp(fieldname, "name"))
{
if (!stricmp(value, "~noun") || !stricmp(value, "~verb") ||
!stricmp(value, "~adjective") || !stricmp(value, "~adverb") ||
!stricmp(value, "~replace_spelling"))
{
jsonconceptName = 0;
}
else
{
jsonconceptName = valueMeaning;
jsonconceptname = value;
Meaning2Word(jsonconceptName)->internalBits |= OVERRIDE_CONCEPT;
}
}
if (!(enableadjust & 2) )
CreateFact(objectName, keyMeaning, valueMeaning, jsonCreateFlags | jsonPermanent | jflags | JSON_OBJECT_FACT); // only the last value of flags matters. 5 means object fact in subject
enableadjust = oldenable; // restore
}
*flags = JSON_OBJECT_VALUE;
break;
}
case JSMN_ARRAY:
{
MEANING arrayName = 0;
if (!(enableadjust &2))
{
arrayName = GetUniqueJsonComposite((char*)"ja-");
*retMeaning = arrayName;
}
else *retMeaning = 1;
int arrayindex = 0;
for (int i = 0; i < curr.size; i++)
{
MEANING arrayMeaning = 0;
int jflags = 0;
retToken = factsJsonHelper(depth + 1, jsontext, tokens, retToken, &arrayMeaning, &jflags, false, nofail, enableadjust);
if (retToken == 0) return 0;
char* word = Meaning2Word(arrayMeaning)->word;
// concept value formats include:
bool conceptvalid = false;
if (jsonconceptName) conceptvalid = true;
char* value = Meaning2Word(arrayMeaning)->word;
if (conceptvalid)
{
char* safestart = SkipWhitespace(value);
if (strchr(value, '|')) conceptvalid = false; // remap concept field
else if (*value == '"' && (*safestart == '(' || safestart[1] == '(')) conceptvalid = false; // pattern in "
else if (*safestart == '(' && strchr(value, ')')) conceptvalid = false; // pattern in () -- skip over leading whitespace
else if (*value == '^' && value[1] == '(') conceptvalid = false;
}
if (conceptvalid) // express concept create
{
int flags = FACTTRANSIENT | OVERRIDE_MEMBER_FACT | FACTDUPLICATE;
char hold[MAX_WORD_SIZE];
char* at = hold;
if (*value == '~') strcpy(hold, value);
else strcpy(hold, JoinWords(BurstWord(value, CONTRACTIONS))); // unneeded because compile should have handled it BUG
at = hold;
if (*at == '\'')
{
flags |= ORIGINAL_ONLY;
at++;
}
arrayMeaning = MakeMeaning(StoreWord(at, AS_IS));
CreateFastFact(arrayMeaning, Mmember, jsonconceptName, flags);
}
else if (!(enableadjust & 2))
{
MEANING index = 0;
char namebuff[256];
sprintf(namebuff, "%d", arrayindex++); // Build the array index
index = MakeMeaning(StoreWord(namebuff, AS_IS));
CreateFact(arrayName, index, arrayMeaning, jsonCreateFlags | jsonPermanent | jflags | JSON_ARRAY_FACT); // flag6 means subject is arrayfact
}
}
jsonconceptName = 0;
*flags = JSON_ARRAY_VALUE;
break;
}
default:
if (!(enableadjust & 2))
{
char* str = AllocateBuffer("jsmn default"); // cant use InfiniteStack because ReportBug will;.
strncpy(str, jsontext + curr.start, size);
str[size] = 0;
FreeBuffer("jsmn default");
ReportBug((char*)"FATAL: (factsJsonHelper) Unknown JSON type encountered: %s", str);
}
}
text[size] = endchar;
currentFact = NULL;
return retToken;
}
// Define our struct for accepting LCs output
struct CurlBufferStruct {
char* buffer;
size_t size;
};
static void dump(const char* text, FILE * stream, unsigned char* ptr, size_t size) // libcurl callback when verbose is on
{
size_t i;
size_t c;
unsigned int width = 0x10;
(*printer)((char*)"%s, %10.10ld bytes (0x%8.8lx)\n", text, (long)size, (long)size);
for (i = 0; i < size; i += width)
{
(*printer)((char*)"%4.4lx: ", (long)i);
/* show hex to the left */
for (c = 0; c < width; c++)
{
if (i + c < size) (*printer)((char*)"%02x ", ptr[i + c]);
else (*printer)((char*)"%s", (char*)" ");
}
/* show data on the right */
for (c = 0; (c < width) && (i + c < size); c++) (*printer)("%c", (ptr[i + c] >= 0x20) && (ptr[i + c] < 0x80) ? ptr[i + c] : '.');
(*printer)((char*)"%s", (char*)"\n");
}
}
/*
----------------------
FUNCTION: JSONOpenCode
Function arguments :
optional argument(1) - permanent, transient, direct
ARGUMENT(1) - request method : POST, GET, POSTU, GETU
ARGUMENT(2) - URL. The URL to use in the request
ARGUMENT(3) - If a POST request, this argument contains the post data
ARGUMENT(4) - This argument contains any needed extra REQUEST headers for the request(see note above).
ARGUMENT(5) - concept of fields to ignore returning.
e.g.
$$url = "https://api.github.com/users/test/repos"
$$user_agent = ^"User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
^jsonopen(GET $$url "" $$user_agent)
# GitHub requires a valid user agent header or it will reject the request. Note, although
# not shown, if there are multiple extra headers they should be separated by the
# tilde character ("~").
E.g.
$$url = "https://en.wikipedia.org/w/api.php?action=query&titles=Main%20Page&rvprop=content&format=json"
$$user_agent = ^"myemail@hotmail.com User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)"
*/
#define REQUEST_HEADER_NVP_SEPARATOR "~"
#define REQUEST_NVP_SEPARATOR ':'
// This function reimplements the semi-standard function strlcpy so we can use it on both Windows, Linux and Mac
size_t our_strlcpy(char* dst, const char* src, size_t siz) {
char* d = dst;
const char* s = src;
size_t n = siz;
/* Copy as many bytes as will fit */
if (n != 0 && --n != 0) {
do {
if ((*d++ = *s++) == 0) break;
} while (--n != 0);
}
/* Not enough room in dst, add NUL and traverse rest of src */
if (n == 0) {
if (siz != 0) *d = '\0'; /* NUL-terminate dst */
while (*s++) { ; }
}
return(s - src - 1); /* count does not include NUL */
}
#ifdef WIN32
// If we're on Windows, just use the safe strncpy version, strncpy_s.
# define SAFE_SPRINTF sprintf_s
#else
// Use snprintf for Linux.
# define SAFE_SPRINTF snprintf
#endif
static int EncodingValue(char* name, char* field, int value)
{
size_t len = strlen(name);
char* at = strstr(field, name);
if (!at) return value; // not found
at += len;
if (at[0] != ';') return 2; // autho
if (at[1] != 'q' && at[1] != 'Q') return 2;
if (at[2] != '=' || at[3] != '0') return 2; // gzip;q=1
if (at[4] == '.') return 2; // gzip;q=0.5
return 1;
}
CURL* curl;
static int my_trace(CURL * handle, curl_infotype type, char* data, size_t size, void* userp)
{
const char* text;
(void)handle; /* prevent compiler warning */
switch (type) {
case CURLINFO_TEXT:
(*printer)("== Info: %s", data);
default: /* in case a new one is introduced to shock us */
return 0;
case CURLINFO_HEADER_OUT:
text = "=> Send header";
break;
case CURLINFO_DATA_OUT:
text = "=> Send data";
break;
case CURLINFO_SSL_DATA_OUT:
text = "=> Send SSL data";
break;
case CURLINFO_HEADER_IN:
text = "<= Recv header";
break;
case CURLINFO_DATA_IN:
text = "<= Recv data";
break;
case CURLINFO_SSL_DATA_IN:
text = "<= Recv SSL data";
break;
}
dump(text, stderr, (unsigned char*)data, size);
return 0;
}
// This is the function we pass to LC, which writes the output to a BufferStruct
static size_t CurlWriteMemoryCallback(void* ptr, size_t size, size_t nmemb, void* data) {
size_t realsize = size * nmemb;
static char* currentLoc;
static char* limit; // where we can allocate to
struct CurlBufferStruct* mem = (struct CurlBufferStruct*)data;
if (!curlBufferBase)
{
curlBufferBase = InfiniteStack(limit, "CurlWriteMemoryCallback"); // can only be released from JSONOpenCode
currentLoc = curlBufferBase;
mem->buffer = curlBufferBase;
}
mem->size += realsize;
if ((int)mem->size > (int)(limit - curlBufferBase)) ReportBug("FATAL: out of curlmemory"); // out of memory
memcpy(currentLoc, ptr, realsize); // add to buffer
currentLoc[realsize] = 0;
currentLoc += realsize;
return realsize;
}
void CurlShutdown()
{
if (curl) {
curl_easy_cleanup(curl);
curl = NULL;
}
if (curl_done_init) curl_global_cleanup();
curl_done_init = false;
}
const char* CurlVersion()
{
static char curlversion[MAX_WORD_SIZE] = "";
if (*curlversion) return(curlversion);
curl_version_info_data data = *curl_version_info(CURLVERSION_NOW);
sprintf(curlversion,"%s, %s, libz/%s", data.version, data.ssl_version, data.libz_version);
return curlversion;
}
FunctionResult InitCurl()
{
// Get curl ready -- do this ONCE only during run of CS
if (!curl_done_init) {
#ifdef WIN32
if (InitWinsock() == FAILRULE_BIT) // only init winsock one per any use- we might have done this from TCPOPEN or PGCode
{
ReportBug((char*)"INFO: Winsock init failed");
return FAILRULE_BIT;
}
#endif
curl_global_init(CURL_GLOBAL_SSL);
curl_done_init = true;
}
return NOPROBLEM_BIT;
}
char* UrlEncodePiece(char* input)
{
InitCurl();
CURL* curlEscape = curl_easy_init();
if (!curlEscape)
{
if (trace & TRACE_JSON) Log(USERLOG, "Curl easy init failed");
return NULL;
}
char* fixed = curl_easy_escape(curlEscape, input, 0);
char* limit;
char* buffer = InfiniteStack(limit, "UrlEncode");
strcpy(buffer, fixed);
curl_free(fixed);
curl_easy_cleanup(curlEscape);
ReleaseInfiniteStack();
return buffer;
}
char* encodeSegment(char** fixed, char* at, char* start, CURL * curlptr)
{
char* segment = *fixed;
if (at[-1] == '\\') return start; // escaped, allow as is
if (at != start) // url encode segment
{
strncpy(segment, start, at - start);
segment[at - start] = 0;
char* coded = curl_easy_escape(curlptr, segment, 0);
strcpy(segment, coded);
curl_free(coded);
segment += strlen(segment);
}
*segment++ = *at;
*segment = 0;
*fixed = segment;
return(at + 1);
}
// RFC 3986
static char* JSONUrlEncode(char* urlx, char* fixedUrl, CURL * curlptr)
{
char* fixed = fixedUrl;
*fixed = 0;
char* at = urlx - 1;
char* start = urlx;
char c;
urlSegment currentSegment = URL_SCHEME;
while ((c = *++at)) // url encode segments
{
if (currentSegment == URL_SCHEME) {
if (c == ':' || c == '+' || c == '-' || c == '.') {
start = encodeSegment(&fixed, at, start, curlptr);
if (start == at) continue;
if (c == ':') {
*fixed++ = *++at; // double /
*fixed++ = *++at;
start += 2;
currentSegment = URL_AUTHORITY;
}
}
}
else if (currentSegment == URL_AUTHORITY) {
if (c == '/' || c == '?' || c == '#' || c == '@' || c == ':' || c == '[' || c == ']' ||
c == '!' || c == '$' || c == '&' || c == '(' || c == ')' ||
c == '*' || c == '+' || c == ',' || c == ';' || c == '=' || c == '\'') {
start = encodeSegment(&fixed, at, start, curlptr);
if (start == at) continue;
if (c == '/') {
currentSegment = URL_PATH;
}
else if (c == '?') {
currentSegment = URL_QUERY;
}
else if (c == '#') {
currentSegment = URL_FRAGMENT;
}
}
}
else if (currentSegment == URL_PATH) {
if (c == '/' || c == '?' || c == '#' || c == ':' || c == '@' ||
c == '!' || c == '$' || c == '&' || c == '(' || c == ')' ||
c == '*' || c == '+' || c == ',' || c == ';' || c == '=' || c == '\'') {
start = encodeSegment(&fixed, at, start, curlptr);
if (start == at) continue;
if (c == '?') {
currentSegment = URL_QUERY;
}
else if (c == '#') {
currentSegment = URL_FRAGMENT;
}
}
}
else if (currentSegment == URL_QUERY) {
if (c == '#' || c == '&' || c == '=' || c == '/' || c == '?') {
start = encodeSegment(&fixed, at, start, curlptr);
if (start == at) continue;
if (c == '#') {
currentSegment = URL_FRAGMENT;
}
}
}
else if (currentSegment == URL_FRAGMENT) {
if (c == '/' || c == '?') {
start = encodeSegment(&fixed, at, start, curlptr);
}
}
}
// remaining piece
start = encodeSegment(&fixed, at, start, curlptr);
return fixedUrl;
}
// Open a URL using the given arguments and return the JSON object's returned by querying the given URL as a set of ChatScript facts.
char fieldName[1000];
char fieldValue[1000];
char headerLine[1000];
FunctionResult JSONOpenCode(char* buffer)
{
int index = JSONArgs();
size_t len;
curlBufferBase = NULL;
char* arg = NULL;
char* extraRequestHeadersRaw = NULL;
char kind = 0;
char* raw_kind = ARGUMENT(index++);
if (!stricmp(raw_kind, "POST")) kind = 'P';
else if (!stricmp(raw_kind, "GET")) kind = 'G';
else if (!stricmp(raw_kind, "POSTU")) kind = 'P';
else if (!stricmp(raw_kind, "GETU")) kind = 'G';
else if (!stricmp(raw_kind, "PUT")) kind = 'U';
else if (!stricmp(raw_kind, "DELETE")) kind = 'D';
else {
char* msg = "jsonopen- only POST, GET, PUT AND DELETE allowed\r\n";