leptris 1.9.356.0-arm-linux → 1.9.359.0-arm-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.adoc +13 -5
  3. data/Rakefile +1 -1
  4. data/ext/leptris/native/native.c +353 -1
  5. data/lib/leptris/version.rb +1 -1
  6. data/lib/leptris/xml/cdata.rb +3 -0
  7. data/lib/leptris/xml/comment.rb +3 -0
  8. data/lib/leptris/xml/declaration.rb +59 -0
  9. data/lib/leptris/xml/doc_type.rb +33 -1
  10. data/lib/leptris/xml/document.rb +74 -2
  11. data/lib/leptris/xml/element.rb +21 -1
  12. data/lib/leptris/xml/entity_reference.rb +3 -0
  13. data/lib/leptris/xml/native.so +0 -0
  14. data/lib/leptris/xml/node_set.rb +22 -0
  15. data/lib/leptris/xml/processing_instruction.rb +3 -0
  16. data/lib/leptris/xml/sax/parser.rb +102 -78
  17. data/lib/leptris/xml/text.rb +4 -0
  18. data/lib/leptris/xml.rb +14 -0
  19. data/lib/libleptris.so +0 -0
  20. data/scripts/gem_smoke.rb +6 -3
  21. data/vendor-src/README.md +1 -1
  22. data/vendor-src/libleptris/CHANGELOG.md +95 -0
  23. data/vendor-src/libleptris/CMakeLists.txt +1 -1
  24. data/vendor-src/libleptris/TODO.xslt-full/REMAINING.md +37 -18
  25. data/vendor-src/libleptris/cli/commands/validate.c +15 -2
  26. data/vendor-src/libleptris/src/include/leptris.h +60 -4
  27. data/vendor-src/libleptris/src/leptris/dtd/content_check.c +280 -144
  28. data/vendor-src/libleptris/src/leptris/dtd/parser.c +17 -4
  29. data/vendor-src/libleptris/src/leptris/rng/parse.c +53 -8
  30. data/vendor-src/libleptris/src/leptris/rng/validate.c +63 -21
  31. data/vendor-src/libleptris/src/leptris/xinclude/xinclude.c +304 -12
  32. data/vendor-src/libleptris/src/leptris/xpath/evaluator.c +30 -0
  33. data/vendor-src/libleptris/src/leptris/xpath/evaluator.h +11 -0
  34. data/vendor-src/libleptris/src/leptris/xpath/evaluator_axes.c +3 -3
  35. data/vendor-src/libleptris/src/leptris/xpath/evaluator_path.c +4 -20
  36. data/vendor-src/libleptris/src/leptris/xpath/parser.c +4 -1
  37. data/vendor-src/libleptris/src/leptris/xpath/vm.c +41 -11
  38. data/vendor-src/libleptris/src/leptris/xpath/xpath_compiled.c +4 -3
  39. data/vendor-src/libleptris/src/leptris/xpath/xpath_public.c +2 -1
  40. data/vendor-src/libleptris/src/leptris/xsd/compile.c +981 -38
  41. data/vendor-src/libleptris/src/leptris/xsd/content_model.c +15 -3
  42. data/vendor-src/libleptris/src/leptris/xsd/validate.c +27 -9
  43. data/vendor-src/libleptris/src/leptris/xsd/xsd_internal.h +1 -0
  44. data/vendor-src/libleptris/test/cli/test_cli.cpp +50 -14
  45. data/vendor-src/libleptris/test/dtd/test_dtd_validate.cpp +80 -4
  46. data/vendor-src/libleptris/test/rng/include-cases/attr-text-choice.rng +18 -0
  47. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-mid.rng +7 -0
  48. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-shared.rng +5 -0
  49. data/vendor-src/libleptris/test/rng/include-cases/dup-inc-top.rng +6 -0
  50. data/vendor-src/libleptris/test/rng/test_rng.cpp +101 -0
  51. data/vendor-src/libleptris/test/xinclude/test_xinclude.cpp +261 -0
  52. data/vendor-src/libleptris/test/xpath/test_xpath.cpp +169 -0
  53. data/vendor-src/libleptris/test/xpath/test_xpath_conformance.cpp +8 -1
  54. data/vendor-src/libleptris/test/xsd/test_xsd_compile.cpp +128 -11
  55. data/vendor-src/libleptris/vcpkg.json +1 -1
  56. metadata +7 -2
@@ -19,12 +19,17 @@
19
19
  * carry backslashes; strrchr('/') alone left the filename in the
20
20
  * "directory" and every relative href resolved against a bogus
21
21
  * base). Returns s. */
22
- static char* rng_dir_of(char* s) {
22
+ /* Cut the last path segment off `s`. Returns 0 when s carries no
23
+ * separator (a bare relative filename) — callers must then use "."
24
+ * as the base, or the FILENAME becomes the include directory and
25
+ * every relative include of the schema fails (#1709 follow-up). */
26
+ static int rng_dir_of(char* s) {
23
27
  char* slash = strrchr(s, '/');
24
28
  char* bslash = strrchr(s, '\\');
25
29
  if (bslash && (!slash || bslash > slash)) slash = bslash;
26
- if (slash) *slash = 0;
27
- return s;
30
+ if (!slash) return 0;
31
+ *slash = 0;
32
+ return 1;
28
33
  }
29
34
 
30
35
  static RngPattern* pat_new(RngPatternKind kind) {
@@ -255,6 +260,35 @@ static void merge_start(RngGrammar* g, RngPattern* body) {
255
260
  g->start = ch;
256
261
  }
257
262
 
263
+ /* Deep-copy a pattern subtree. The include merge used to MOVE the
264
+ * included grammar's body into the including grammar — but an
265
+ * include reachable through TWO paths (relaton's basicdoc: direct
266
+ * AND via biblio) leaves both grammars' define entries pointing at
267
+ * ONE shared tree; the second path's merge then mutates/frees nodes
268
+ * the first path still walks (leptris#1709). Name-based ref
269
+ * resolution (find_define at validate time) makes the copy safe —
270
+ * REF patterns carry only their name. */
271
+ static char* rng_strdup_or_null(const char* s) {
272
+ return s ? leptris_strdup(s) : NULL;
273
+ }
274
+ static RngPattern* rng_pattern_deep_copy(const RngPattern* p) {
275
+ if (!p) return NULL;
276
+ RngPattern* c = pat_new(p->kind);
277
+ c->first_child = rng_pattern_deep_copy(p->first_child);
278
+ c->next = rng_pattern_deep_copy(p->next);
279
+ c->name = rng_strdup_or_null(p->name);
280
+ c->ns = rng_strdup_or_null(p->ns);
281
+ c->datatype = rng_strdup_or_null(p->datatype);
282
+ c->datatype_lib = rng_strdup_or_null(p->datatype_lib);
283
+ c->value = rng_strdup_or_null(p->value);
284
+ /* define: name-based resolution at validate time relinks; the
285
+ * copied subtree must not share the source grammar's (freed)
286
+ * define entries. */
287
+ c->define = NULL;
288
+ c->any_name = p->any_name;
289
+ return c;
290
+ }
291
+
258
292
  /* INTERLEAVE and CHOICE are associative: a merge wrapper whose
259
293
  * child list already carries a same-kind combinator flattens into
260
294
  * the parent scope. The validator's single-pass interleave walk has
@@ -399,7 +433,7 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
399
433
  }
400
434
  char nbase[1024];
401
435
  snprintf(nbase, sizeof(nbase), "%s", path);
402
- rng_dir_of(nbase);
436
+ if (!rng_dir_of(nbase)) snprintf(nbase, sizeof(nbase), ".");
403
437
 
404
438
  char ierr[256];
405
439
  ierr[0] = 0;
@@ -516,7 +550,16 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
516
550
  while (d) {
517
551
  RngDefine* next = d->next;
518
552
  RngPattern* wrap = pat_new(RNG_GROUP);
519
- wrap->first_child = d->body;
553
+ /* COPY, not move: the included grammar may be reachable
554
+ * through another include path whose defines still walk
555
+ * this tree (leptris#1709, adoption-by-copy). The original
556
+ * tree is solely owned by THIS include-parse instance —
557
+ * free it, or every include hop leaks its define bodies
558
+ * (Linux-ASAN/LSan: 80-byte RngPattern trees per merged
559
+ * define). */
560
+ wrap->first_child = rng_pattern_deep_copy(d->body);
561
+ rng_pattern_free(d->body);
562
+ d->body = NULL;
520
563
  if (!add_define(g, leptris_strdup(d->name),
521
564
  d->combine ? leptris_strdup(d->combine) : NULL,
522
565
  wrap, err, errsz)) {
@@ -525,6 +568,7 @@ static int handle_include(RngGrammar* g, LeptrisElement inc,
525
568
  free(d);
526
569
  while (next) {
527
570
  RngDefine* n2 = next->next;
571
+ rng_pattern_free(next->body);
528
572
  free(next->name);
529
573
  free(next->combine);
530
574
  free(next);
@@ -646,7 +690,7 @@ static int resolve_external_refs(RngGrammar* g, RngPattern** slot,
646
690
  }
647
691
  char nbase[1024];
648
692
  snprintf(nbase, sizeof(nbase), "%s", path);
649
- rng_dir_of(nbase);
693
+ if (!rng_dir_of(nbase)) snprintf(nbase, sizeof(nbase), ".");
650
694
  struct leptris_relaxng* ex = rng_parse_doc(edoc, nbase[0] ? nbase : NULL);
651
695
  leptris_document_free(edoc);
652
696
  if (!ex || !ex->grammar || ex->error) {
@@ -861,10 +905,11 @@ struct leptris_relaxng* rng_parse_file(const char* path) {
861
905
  return NULL;
862
906
  }
863
907
 
864
- /* Include hrefs resolve relative to the schema file's directory. */
908
+ /* Include hrefs resolve relative to the schema file's directory;
909
+ * a bare relative filename resolves its includes against "." */
865
910
  char base[512];
866
911
  snprintf(base, sizeof(base), "%s", path);
867
- rng_dir_of(base);
912
+ if (!rng_dir_of(base)) snprintf(base, sizeof(base), ".");
868
913
 
869
914
  struct leptris_relaxng* rng =
870
915
  rng_parse_doc(doc, base[0] ? base : NULL);
@@ -241,6 +241,12 @@ static int attr_leaves_satisfied(RngVal* v, RngPattern* head,
241
241
  *has_leaf = 1;
242
242
  if (token_matches_leaf(value, c)) return 1;
243
243
  continue;
244
+ case RNG_TEXT:
245
+ /* <text/> in an attribute's content matches ANY
246
+ * value (bDateAttributes: enum OR open text —
247
+ * leptris#1709's bipm half). */
248
+ *has_leaf = 1;
249
+ return 1;
244
250
  case RNG_REF: {
245
251
  RngDefine* d = find_define(v->g, c->name);
246
252
  if (d && d->body &&
@@ -435,30 +441,55 @@ static size_t match_seq(RngVal* v, RngPattern* p, LeptrisNodeRef* kids,
435
441
  return i;
436
442
  }
437
443
  case RNG_INTERLEAVE: {
438
- /* Any order; each child pattern is used exactly once and
439
- * all of them must match. */
440
- size_t count = 0;
441
- for (RngPattern* c = p->first_child; c; c = c->next) count++;
442
- int used[32] = {0};
443
- for (size_t k = 0; k < count; k++) {
444
- int matched_any = 0;
445
- size_t ci = 0;
446
- for (RngPattern* c = p->first_child; c;
447
- c = c->next, ci++) {
448
- if (ci < 32 && used[ci]) continue;
449
- size_t r = match_seq(v, c, kids, n, idx);
450
- if (r != (size_t)-1) {
451
- matched_any = 1;
452
- idx = r;
453
- if (ci < 32) used[ci] = 1;
454
- break;
444
+ /* Any order; every branch must be satisfied. A single
445
+ * assignment pass is unsound against interleaved
446
+ * zeroOrMore branches: a branch whose turn precedes the
447
+ * elements it matches zero-widths, gets marked used, and
448
+ * strands them (leptris#1709: relaton-ieee's 2nd/3rd
449
+ * <ics>). Consume to a fixpoint — branches take turns
450
+ * while ANY of them advances — then the remainder must
451
+ * be zero-width satisfiable. */
452
+ RngPattern* ch[64];
453
+ size_t cn = 0;
454
+ for (RngPattern* c = p->first_child; c && cn < 64;
455
+ c = c->next)
456
+ ch[cn++] = c;
457
+ int used[64] = {0};
458
+ int progressed = 1;
459
+ size_t i = idx;
460
+ while (progressed) {
461
+ progressed = 0;
462
+ for (size_t b = 0; b < cn; b++) {
463
+ if (used[b]) continue;
464
+ size_t r = match_seq(v, ch[b], kids, n, i);
465
+ if (r != (size_t)-1 && r > i) {
466
+ used[b] = 1;
467
+ i = r;
468
+ progressed = 1;
469
+ } else if (r != (size_t)-1) {
470
+ /* zero-width here: leave the branch unused so
471
+ * a later position can still feed it. */
472
+ v->failed = 0;
473
+ v->err[0] = 0;
474
+ } else {
475
+ v->failed = 0;
476
+ v->err[0] = 0;
455
477
  }
456
- v->failed = 0;
457
- v->err[0] = 0;
458
478
  }
459
- if (!matched_any) return (size_t)-1;
460
479
  }
461
- return idx;
480
+ for (size_t b = 0; b < cn; b++) {
481
+ if (used[b]) continue;
482
+ size_t r = match_seq(v, ch[b], kids, n, i);
483
+ if (r == (size_t)-1 || r != i) {
484
+ /* A branch that consumes here contradicts the
485
+ * fixpoint above — treat as unsatisfied. */
486
+ return (size_t)-1;
487
+ }
488
+ v->failed = 0;
489
+ v->err[0] = 0;
490
+ used[b] = 1;
491
+ }
492
+ return i;
462
493
  }
463
494
  case RNG_OPTIONAL: {
464
495
  size_t r = match_seq(v, p->first_child, kids, n, idx);
@@ -1719,6 +1750,17 @@ static void diagnose_element(RngVal* v, RngPattern* p, LeptrisElement e) {
1719
1750
  "element \"%s\" not allowed anywhere; expected %s",
1720
1751
  x, expected);
1721
1752
  }
1753
+ /* leptris#1704 (Jing parity): the skipped element still
1754
+ * gets its attributes validated — against its OWN
1755
+ * definition — or schema-attribute constraints (align
1756
+ * enums) go invisible exactly where documents are most
1757
+ * broken. Jing reports both, position first. */
1758
+ {
1759
+ RngDefine* own = find_define(v->g, x);
1760
+ if (own && own->body)
1761
+ diagnose_attrs(v, dpat_new(v, RNG_ELEMENT, own->body),
1762
+ kid);
1763
+ }
1722
1764
  /* Skip the offending child; position unchanged. */
1723
1765
  }
1724
1766
  if (!nullable_state(v, state)) {
@@ -514,10 +514,14 @@ static LeptrisElement xptr_select(LeptrisDocument doc, const char* value) {
514
514
  return found;
515
515
  }
516
516
 
517
- /* Forward decl for internal recursion-aware variant. */
517
+ /* Forward decls: internal recursion variant + catalog lookup. */
518
518
  static LeptrisStatus xinclude_process_internal(struct leptris_document* doc,
519
519
  const char* base_url,
520
- int depth);
520
+ int depth,
521
+ const leptris_xinclude_opts* opts);
522
+ static void xinclude_catalog_lookup(const char* catalog_path,
523
+ const char* uri, char* out,
524
+ size_t out_size);
521
525
 
522
526
  /* TODO 117 Phase C: cycle detection.
523
527
  *
@@ -562,19 +566,163 @@ static CycleNode* cycle_pop(CycleNode* ancestors) {
562
566
  * (parse error, alloc failure), 1 otherwise. The `depth` parameter
563
567
  * (TODO 92 Phase 5) tracks how many xi:include layers we've descended
564
568
  * through; XINCLUDE_MAX_DEPTH stops mutual-recursion attacks. */
569
+ /* ---- TODO.assemble 03: xml:base (RFC 3986) ------------------------------
570
+ *
571
+ * The effective base of an element = merge(inherited_base, this
572
+ * element's xml:base). merge_uri implements RFC 3986 §5.2 for the
573
+ * cases hrefs actually exercise: absolute refs replace; scheme-
574
+ * relative ("//host/x") keeps the scheme; root-relative ("/x")
575
+ * keeps scheme+authority; otherwise the reference rides the base's
576
+ * path directory with dot-segments removed. */
577
+ static int uri_is_absolute(const char* s) {
578
+ for (const char* p = s; *p; p++) {
579
+ if (*p == ':') return p != s;
580
+ if (*p == '/' || *p == '?' || *p == '#') return 0;
581
+ }
582
+ return 0;
583
+ }
584
+
585
+ static void copy_dir_of(char* out, size_t out_size, const char* uri) {
586
+ const char* slash = strrchr(uri, '/');
587
+ if (!slash) {
588
+ out[0] = 0;
589
+ return;
590
+ }
591
+ size_t bl = (size_t)(slash - uri);
592
+ if (bl >= out_size) bl = out_size - 1;
593
+ memcpy(out, uri, bl);
594
+ out[bl] = 0;
595
+ }
596
+
597
+ static void remove_dot_segments(char* path) {
598
+ /* Rebuild the path segment by segment, dropping "." segments and
599
+ * popping ".." against the last emitted one (RFC 3986 §5.2.4 for
600
+ * the path-only form — no query/fragment here). */
601
+ char tmp[8192];
602
+ size_t tl = strlen(path);
603
+ if (tl >= sizeof(tmp)) return; /* oversized: leave as-is */
604
+ memcpy(tmp, path, tl + 1);
605
+
606
+ char* out = path;
607
+ size_t out_len = 0;
608
+ const char* r = tmp;
609
+ int absolute = (*r == '/');
610
+ if (absolute) r++;
611
+
612
+ while (*r) {
613
+ const char* seg = r;
614
+ while (*r && *r != '/') r++;
615
+ size_t sl = (size_t)(r - seg);
616
+ int had_slash = (*r == '/');
617
+ if (had_slash) r++;
618
+
619
+ if (sl == 1 && seg[0] == '.') continue;
620
+ if (sl == 2 && seg[0] == '.' && seg[1] == '.') {
621
+ if (out_len > 0) {
622
+ while (out_len > 0 && out[out_len - 1] != '/') out_len--;
623
+ if (out_len > 0) out_len--; /* the separator itself */
624
+ }
625
+ out[out_len] = 0;
626
+ continue;
627
+ }
628
+ if (out_len == 0) {
629
+ if (absolute) out[out_len++] = '/';
630
+ } else if (out[out_len - 1] != '/') {
631
+ out[out_len++] = '/';
632
+ }
633
+ memcpy(out + out_len, seg, sl);
634
+ out_len += sl;
635
+ out[out_len] = 0;
636
+ }
637
+ if (tl > 0 && tmp[tl - 1] == '/' &&
638
+ !(out_len > 0 && out[out_len - 1] == '/')) {
639
+ if (out_len == 0) {
640
+ if (absolute) out[out_len++] = '/';
641
+ } else if (out[out_len - 1] != '/') {
642
+ out[out_len++] = '/';
643
+ }
644
+ out[out_len] = 0;
645
+ }
646
+ }
647
+
648
+ static int merge_uri(const char* base, const char* ref, char* out,
649
+ size_t out_size) {
650
+ if (!ref || !ref[0]) {
651
+ snprintf(out, out_size, "%s", base ? base : "");
652
+ return 0;
653
+ }
654
+ if (!base || !base[0] || uri_is_absolute(ref)) {
655
+ snprintf(out, out_size, "%s", ref);
656
+ return 0;
657
+ }
658
+ /* Filesystem-absolute reference: used as-is (the processor's
659
+ * base is a plain directory prefix, not a URI with authority). */
660
+ if (ref[0] == '/') {
661
+ snprintf(out, out_size, "%s", ref);
662
+ return 0;
663
+ }
664
+ if (strncmp(ref, "//", 2) == 0) {
665
+ /* scheme-relative: reuse the base's scheme */
666
+ const char* colon = strchr(base, ':');
667
+ if (colon) {
668
+ int n = snprintf(out, out_size, "%.*s:%s",
669
+ (int)(colon - base), base, ref);
670
+ return (n < 0 || (size_t)n >= out_size);
671
+ }
672
+ snprintf(out, out_size, "%s", ref);
673
+ return 0;
674
+ }
675
+ /* strip query/fragment from the base for the directory merge */
676
+ char base_path[4096];
677
+ snprintf(base_path, sizeof(base_path), "%s", base);
678
+ char* cut = strpbrk(base_path, "?#");
679
+ if (cut) *cut = 0;
680
+ char dir[4096];
681
+ copy_dir_of(dir, sizeof(dir), base_path);
682
+ char joined[8192];
683
+ if (dir[0])
684
+ snprintf(joined, sizeof(joined), "%s/%s", dir, ref);
685
+ else
686
+ snprintf(joined, sizeof(joined), "%s", ref);
687
+ remove_dot_segments(joined);
688
+ snprintf(out, out_size, "%s", joined);
689
+ return 0;
690
+ }
691
+
692
+ /* The element's effective base: nearest xml:base (this element or
693
+ * nearest ancestor carrying one), merged over the inherited base. */
694
+ static void effective_xml_base(LeptrisElement elem, const char* inherited,
695
+ char* out, size_t out_size) {
696
+ const char* xb = get_attr_value(elem, "xml:base");
697
+ if (xb && xb[0] && inherited && inherited[0] &&
698
+ !uri_is_absolute(xb)) {
699
+ merge_uri(inherited, xb, out, out_size);
700
+ } else if (xb && xb[0]) {
701
+ snprintf(out, out_size, "%s", xb);
702
+ } else {
703
+ snprintf(out, out_size, "%s", inherited ? inherited : "");
704
+ }
705
+ }
706
+
565
707
  static int process_element_xinclude(LeptrisElement elem,
566
708
  struct leptris_document* doc,
567
709
  const char* base_url,
710
+ const leptris_xinclude_opts* opts,
568
711
  int depth,
569
712
  CycleNode* ancestors) {
570
713
  if (!elem) return 1;
571
714
 
715
+ char subtree_base[4096];
716
+ effective_xml_base(elem, base_url, subtree_base, sizeof(subtree_base));
717
+ base_url = subtree_base;
718
+
572
719
  LeptrisNodeRef child = leptris_node_first_child((LeptrisNodeRef)elem);
573
720
  while (child) {
574
721
  LeptrisNodeRef next = leptris_node_next_sibling(child);
575
722
  if (leptris_node_get_type(child) == LEPTRIS_NODE_TYPE_ELEMENT) {
576
723
  int rc = process_element_xinclude((LeptrisElement)child, doc,
577
- base_url, depth, ancestors);
724
+ base_url, opts, depth,
725
+ ancestors);
578
726
  if (!rc) return 0;
579
727
  }
580
728
  child = next;
@@ -608,8 +756,35 @@ static int process_element_xinclude(LeptrisElement elem,
608
756
  if (!is_xml && (frag || (xptr_attr && xptr_attr[0]))) resource_error = 1;
609
757
 
610
758
  char full_path[4096];
611
- if (!resource_error &&
612
- !join_path(full_path, sizeof(full_path), base_url, href_base)) return 1;
759
+ if (!resource_error) {
760
+ int rv;
761
+ if (base_url && base_url[0])
762
+ rv = merge_uri(base_url, href_base, full_path, sizeof(full_path));
763
+ else
764
+ rv = merge_uri(NULL, href_base, full_path, sizeof(full_path));
765
+ if (rv) return 1;
766
+ }
767
+ /* XML Catalog lookup (exact <uri name=...> mappings) rewrites
768
+ * the URI before the policy gate — catalogs exist precisely to
769
+ * map remote names onto local resources. */
770
+ char catalog_hit[4096];
771
+ catalog_hit[0] = 0;
772
+ if (!resource_error && opts && opts->catalog_path && opts->catalog_path[0]) {
773
+ xinclude_catalog_lookup(opts->catalog_path, full_path, catalog_hit,
774
+ sizeof(catalog_hit));
775
+ }
776
+ const char* fetch_uri =
777
+ catalog_hit[0] ? catalog_hit : full_path;
778
+ /* TODO.assemble 04: network policy — remote schemes are resource
779
+ * errors unless the caller explicitly enables them; the gate runs
780
+ * on the POST-catalog URI (a catalog mapping may localize it). */
781
+ int no_network = opts ? opts->no_network : 1;
782
+ if (!resource_error && no_network && uri_is_absolute(fetch_uri) &&
783
+ (strncmp(fetch_uri, "http://", 7) == 0 ||
784
+ strncmp(fetch_uri, "https://", 8) == 0 ||
785
+ strncmp(fetch_uri, "ftp://", 6) == 0 ||
786
+ strncmp(fetch_uri, "ftps://", 7) == 0))
787
+ resource_error = 1;
613
788
 
614
789
  /* TODO 117 Phase C: cycle detection. If `full_path` is already in
615
790
  * the ancestor chain, we'd be re-including something we're
@@ -619,9 +794,21 @@ static int process_element_xinclude(LeptrisElement elem,
619
794
  }
620
795
 
621
796
  size_t content_len = 0;
622
- char* content = resource_error
623
- ? NULL
624
- : load_file_content(full_path, &content_len);
797
+ char* content = NULL;
798
+ int resolver_handled = 0;
799
+ if (!resource_error && opts && opts->resolver) {
800
+ int rr = opts->resolver(fetch_uri, opts->resolver_user, &content,
801
+ &content_len);
802
+ if (rr == 1) {
803
+ resolver_handled = 1;
804
+ } else if (rr < 0) {
805
+ content = NULL; /* forced resource error -> fallback */
806
+ } else {
807
+ content = NULL;
808
+ }
809
+ }
810
+ if (!resource_error && !resolver_handled && !content)
811
+ content = load_file_content(fetch_uri, &content_len);
625
812
 
626
813
  /* The substitute node — element for parse="xml", text for parse="text",
627
814
  * NULL on failure (fallback path below). */
@@ -641,7 +828,21 @@ static int process_element_xinclude(LeptrisElement elem,
641
828
  /* Resolve any xi:include nested inside the included doc
642
829
  * before splicing — XInclude processes recursively.
643
830
  * Bump depth so mutual-recursion stops at MAX_DEPTH. */
644
- xinclude_process_internal(included_doc, base_url, depth + 1);
831
+ /* TODO.assemble 03: nested hrefs inside the included
832
+ * document resolve against the INCLUDED document's
833
+ * own location, not the includer's base. */
834
+ {
835
+ char inc_dir[4096];
836
+ copy_dir_of(inc_dir, sizeof(inc_dir), full_path);
837
+ size_t dl = strlen(inc_dir);
838
+ if (dl + 1 < sizeof(inc_dir) && (dl == 0 ||
839
+ inc_dir[dl-1] != '/')) {
840
+ inc_dir[dl++] = '/';
841
+ inc_dir[dl] = 0;
842
+ }
843
+ xinclude_process_internal(included_doc, inc_dir,
844
+ depth + 1, opts);
845
+ }
645
846
  LeptrisElement inc_root = leptris_document_root(included_doc);
646
847
  if (inc_root) {
647
848
  /* Phase 4 of TODO 92: xpointer attribute selects
@@ -775,11 +976,84 @@ fallback:
775
976
  return 1;
776
977
  }
777
978
 
979
+ /* ---- TODO.assemble 04: XML Catalog (minimal) ----------------------------
980
+ *
981
+ * Parses an XML Catalog file and answers EXACT <uri name="..." uri=
982
+ * "..."/> mappings. The file is re-read per lookup (catalogs are
983
+ * small, XInclude counts are depth-bounded); a missing or malformed
984
+ * catalog simply yields no rewrite. */
985
+ static void xinclude_catalog_lookup(const char* catalog_path,
986
+ const char* uri, char* out,
987
+ size_t out_size) {
988
+ out[0] = 0;
989
+ size_t clen = 0;
990
+ char* text = load_file_content(catalog_path, &clen);
991
+ if (!text) return;
992
+ /* Exact <uri name="..." uri="..."/> mappings; attribute order is
993
+ * free. Malformed input simply yields no rewrite. */
994
+ const char* p = text;
995
+ while ((p = strchr(p, '<')) != NULL) {
996
+ p++;
997
+ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')
998
+ p++;
999
+ if (strncmp(p, "uri", 3) != 0 ||
1000
+ (p[3] != ' ' && p[3] != '\t' && p[3] != '\n' && p[3] != '\r' &&
1001
+ p[3] != '/' && p[3] != '>')) {
1002
+ continue;
1003
+ }
1004
+ const char* tag_end = strchr(p, '>');
1005
+ if (!tag_end) break;
1006
+ const char* name_v = NULL;
1007
+ const char* uri_v = NULL;
1008
+ const char* uri_v_end = NULL;
1009
+ const char* q = p + 3;
1010
+ while (q < tag_end) {
1011
+ const char* eq = strchr(q, '=');
1012
+ if (!eq || eq >= tag_end) break;
1013
+ const char* key = eq;
1014
+ while (key > p && key[-1] != ' ' && key[-1] != '\t' &&
1015
+ key[-1] != '\n' && key[-1] != '\r')
1016
+ key--;
1017
+ size_t kl = (size_t)(eq - key);
1018
+ const char* vq = eq + 1;
1019
+ while (vq < tag_end && (*vq == ' ' || *vq == '\t'))
1020
+ vq++;
1021
+ if (vq >= tag_end || (*vq != '"' && *vq != '\'')) {
1022
+ q = eq + 1;
1023
+ continue;
1024
+ }
1025
+ char quote = *vq++;
1026
+ const char* vend = memchr(vq, quote, (size_t)(tag_end - vq));
1027
+ if (!vend) break;
1028
+ if ((kl == 4 && strncmp(key, "name", 4) == 0) &&
1029
+ (size_t)(vend - vq) == strlen(uri) &&
1030
+ strncmp(vq, uri, vend - vq) == 0)
1031
+ name_v = vq;
1032
+ if (kl == 3 && strncmp(key, "uri", 3) == 0) {
1033
+ uri_v = vq;
1034
+ uri_v_end = vend;
1035
+ }
1036
+ if (name_v && uri_v) {
1037
+ size_t urlen = (size_t)(uri_v_end - uri_v);
1038
+ if (urlen >= out_size) urlen = out_size - 1;
1039
+ memcpy(out, uri_v, urlen);
1040
+ out[urlen] = 0;
1041
+ free(text);
1042
+ return;
1043
+ }
1044
+ q = vend + 1;
1045
+ }
1046
+ p = tag_end + 1;
1047
+ }
1048
+ free(text);
1049
+ }
1050
+
778
1051
  /* Internal recursion-aware variant. Aborts with LEPTRIS_ERROR_INVALID_ARG
779
1052
  * if depth exceeds XINCLUDE_MAX_DEPTH (TODO 92 Phase 5). */
780
1053
  static LeptrisStatus xinclude_process_internal(struct leptris_document* doc,
781
1054
  const char* base_url,
782
- int depth) {
1055
+ int depth,
1056
+ const leptris_xinclude_opts* opts) {
783
1057
  if (!doc) return LEPTRIS_ERROR_NULL_ARG;
784
1058
  if (depth > XINCLUDE_MAX_DEPTH) return LEPTRIS_ERROR_INVALID_ARG;
785
1059
 
@@ -789,11 +1063,29 @@ static LeptrisStatus xinclude_process_internal(struct leptris_document* doc,
789
1063
  /* TODO 117 Phase C: thread empty ancestor list into the walk.
790
1064
  * The first include pushes its own URI, so the second visit to the
791
1065
  * same URI is detected as a cycle. */
792
- int rc = process_element_xinclude(root, doc, base_url, depth, NULL);
1066
+ int rc = process_element_xinclude(root, doc, base_url, opts, depth,
1067
+ NULL);
793
1068
  if (!rc) return LEPTRIS_ERROR_IO;
794
1069
  return LEPTRIS_OK;
795
1070
  }
796
1071
 
797
1072
  LEPTRIS_API LeptrisStatus leptris_xinclude_process(LeptrisDocument doc, const char* base_url) {
798
- return xinclude_process_internal(doc, base_url, 0);
1073
+ /* no_network defaults to 1 for the legacy entry too — the remote
1074
+ * refusal is the documented posture (TODO.assemble 04). */
1075
+ leptris_xinclude_opts defaults;
1076
+ memset(&defaults, 0, sizeof(defaults));
1077
+ defaults.no_network = 1;
1078
+ return xinclude_process_internal(doc, base_url, 0, &defaults);
1079
+ }
1080
+
1081
+ LEPTRIS_API LeptrisStatus leptris_xinclude_process_opts(
1082
+ LeptrisDocument doc, const char* base_url,
1083
+ const leptris_xinclude_opts* opts) {
1084
+ leptris_xinclude_opts defaults;
1085
+ if (!opts) {
1086
+ memset(&defaults, 0, sizeof(defaults));
1087
+ defaults.no_network = 1;
1088
+ opts = &defaults;
1089
+ }
1090
+ return xinclude_process_internal(doc, base_url, 0, opts);
799
1091
  }
@@ -10,6 +10,7 @@
10
10
  #include "functions.h"
11
11
  #include "xpath_variables.h"
12
12
  #include "../dom/element.h" /* For LeptrisElement structure */
13
+ #include "../dom/document_node.h"
13
14
  #include "../common/port.h" /* LEPTRIS_THREAD_LOCAL */
14
15
  #include "lexer.h"
15
16
  #include "parser.h"
@@ -99,6 +100,35 @@ XPathContext* xpath_context_new(struct leptris_document* document,
99
100
  * that hold the context for the duration of one eval (the common
100
101
  * case — both leptris_xpath_eval and leptris_xpath_eval_with_vars)
101
102
  * skip the malloc/free pair. */
103
+ const char* xpath_expr_prefix_uri(XPathContext* ctx,
104
+ const char* prefix,
105
+ size_t prefix_len) {
106
+ if (!prefix || prefix_len == 0) return NULL;
107
+ const char* bound = ctx
108
+ ? leptris_xpath_ns_lookup(
109
+ (const struct leptris_xpath_ns_map*)ctx->ns_set,
110
+ prefix, prefix_len)
111
+ : NULL;
112
+ if (bound) return bound;
113
+ if (prefix_len != 5 || memcmp(prefix, "xmlns", 5) != 0)
114
+ return NULL;
115
+ /* In-scope default namespace: the innermost declaration with no
116
+ * prefix, walking ancestor-or-self from the context element. */
117
+ LeptrisElement cur = ctx ? ctx->context_node : NULL;
118
+ if (cur && ((LeptrisNode*)cur)->type == LEPTRIS_NODE_TYPE_DOCUMENT) {
119
+ struct leptris_document* dd = ((LeptrisDocumentNode*)cur)->doc;
120
+ cur = (LeptrisElement)dd->new_dom_root;
121
+ if (!cur) cur = dd->root;
122
+ }
123
+ for (; cur; cur = leptris_element_get_parent(cur)) {
124
+ for (struct leptris_namespace* decl = leptris_elem_namespaces(cur);
125
+ decl; decl = decl->next)
126
+ if (!decl->prefix && decl->uri && decl->uri[0])
127
+ return decl->uri;
128
+ }
129
+ return NULL;
130
+ }
131
+
102
132
  void xpath_context_init(XPathContext* context,
103
133
  struct leptris_document* document,
104
134
  LeptrisElement context_node) {
@@ -48,6 +48,17 @@ typedef struct leptris_xsd_schema* LeptrisXsdSchema;
48
48
  XPathContext* xpath_context_new(struct leptris_document* document,
49
49
  LeptrisElement context_node);
50
50
 
51
+ /* Resolve an EXPRESSION name-test prefix to a namespace URI:
52
+ * explicit bindings first; then the libxml2/nokogiri quirk — the
53
+ * reserved xmlns: prefix resolves to the in-scope DEFAULT
54
+ * namespace of the evaluation context (leptris-ruby#469). Safe to
55
+ * hijack: no valid document carries an element literally prefixed
56
+ * xmlns:, so a conformant query can never need it for anything
57
+ * else. NULL when the prefix is unbound and not xmlns:. */
58
+ const char* xpath_expr_prefix_uri(XPathContext* ctx,
59
+ const char* prefix,
60
+ size_t prefix_len);
61
+
51
62
  /**
52
63
  * Free XPath context
53
64
  *