esi 0.7.0

A streaming parser and executor for Edge Side Includes
Documentation
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
use esi::{Configuration, Processor};
use fastly::{Backend, Request, Response};
use std::collections::HashMap;
use std::sync::{Arc, Mutex};

/// Test that esi:eval with dca="none" processes in parent's context (spec Example 1)
/// Variables from fragment ARE accessible in parent
#[test]
fn test_eval_dca_none_parent_context() -> esi::Result<()> {
    // Parent sets pvar1=7 and pvar2=8, then evals fragment with dca="none"
    let input = r#"
<esi:assign name="pvar1" value="7"/>
<esi:assign name="pvar2" value="8"/>
  <esi:eval src="http://example.com/frag1.html" dca="none"/>
<esi:vars>pvar1 = $(pvar1)
  pvar2 = $(pvar2)
  fvar = $(fvar)
</esi:vars>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Fragment sets fvar=9 and pvar2=0
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(
                    r#"
<esi:assign name="fvar" value="9"/>
<esi:assign name="pvar2" value="0"/>"#,
                ),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    // With dca="none", fragment executes in parent context
    // So parent's pvar1=7 stays, fragment's pvar2=0 overrides parent's pvar2=8, fragment's fvar=9 is set
    assert_eq!(
        result.trim(),
        r#"pvar1 = 7
  pvar2 = 0
  fvar = 9"#,
        "Fragment should execute in parent context, variables should be shared/overridden"
    );
    Ok(())
}

/// Test that esi:eval with dca="esi" processes in isolated context (spec Example 2)
/// Variables from fragment are NOT accessible in parent
#[test]
fn test_eval_dca_esi_isolated_context() -> esi::Result<()> {
    // Same setup as Example 1, but with dca="esi"
    let input = r#"
<esi:assign name="pvar1" value="7"/>
<esi:assign name="pvar2" value="8"/>
<esi:eval src="http://example.com/frag1.html" dca="esi"/>
<esi:vars>pvar1 = $(pvar1)
  pvar2 = $(pvar2)
  fvar = $(fvar)
</esi:vars>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Fragment sets fvar=9 and pvar2=0 (same as Example 1)
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(
                    r#"
<esi:assign name="fvar" value="9"/>
<esi:assign name="pvar2" value="0"/>"#,
                ),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    // With dca="esi", fragment executes in ISOLATED context first
    // Fragment's variables DON'T affect parent, only the output (which is empty) is inserted
    assert_eq!(
        result.trim(),
        r#"pvar1 = 7
  pvar2 = 8
  fvar ="#,
        "Parent variables should remain unchanged, fragment variables should not leak"
    );
    Ok(())
}

/// Test that esi:eval with dca="esi" inserts the output from isolated processing
#[test]
fn test_eval_dca_esi_with_output() -> esi::Result<()> {
    let input = r#"
<esi:assign name="parent_var" value="'from_parent'"/>
<esi:eval src="http://example.com/fragment" dca="esi"/>
<esi:vars>After: $(fragment_var)</esi:vars>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Fragment sets a variable and outputs text
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(
                    r#"
<esi:assign name="fragment_var" value="'from_fragment'"/>
<esi:vars>Output from fragment</esi:vars>"#,
                ),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    // With dca="esi", phase 1 processes fragment in isolation (output produced, vars stay isolated)
    // Phase 2 processes that output in parent context (fragment_var not accessible)
    assert_eq!(
        result.trim(),
        "Output from fragment\nAfter:",
        "Should output text from fragment, but fragment variables should not leak to parent"
    );
    Ok(())
}

/// Test that include with dca="none" inserts content verbatim (no ESI processing)
#[test]
fn test_include_dca_none_no_processing() -> esi::Result<()> {
    let input = r#"<esi:include src="http://example.com/fragment" dca="none"/>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Return content with ESI tags - should NOT be processed
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(
                    r#"<esi:assign name="x" value="42"/><esi:vars>X is $(x)</esi:vars>"#,
                ),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    assert_eq!(
        result, r#"<esi:assign name="x" value="42"/><esi:vars>X is $(x)</esi:vars>"#,
        "dca='none' should insert content verbatim without ESI processing"
    );
    Ok(())
}

/// Test that include with dca="esi" processes content as ESI
#[test]
fn test_include_dca_esi_processes_content() -> esi::Result<()> {
    let input = r#"<esi:include src="http://example.com/fragment" dca="esi"/>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Return ESI content - should be processed
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(
                    r#"<esi:assign name="y" value="99"/><esi:vars>Y is $(y)</esi:vars>"#,
                ),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    assert_eq!(result, "Y is 99", "dca='esi' should process content as ESI");
    Ok(())
}

/// Test that include with dca="esi" does NOT leak variables to parent namespace.
/// Per ESI spec: "It is impossible for a child to affect a parent's namespace" for include.
#[test]
fn test_include_dca_esi_isolates_namespace() -> esi::Result<()> {
    let input = r#"<esi:include src="http://example.com/fragment" dca="esi"/><esi:vars>After include: $(shared_var)</esi:vars>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Set a variable in the included ESI — should NOT leak to parent
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(r#"<esi:assign name="shared_var" value="'shared'"/>"#),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    assert_eq!(
        result, "After include: ",
        "Include with dca='esi' must not leak variables to parent namespace"
    );
    Ok(())
}

/// Test complex scenario: include respects dca, eval always processes as ESI
#[test]
fn test_eval_vs_include_dca_difference() -> esi::Result<()> {
    let input = r#"<esi:include src="http://example.com/raw"/><esi:eval src="http://example.com/processed"/>"#;

    // Track which URLs were called
    let calls = Arc::new(Mutex::new(HashMap::new()));
    let calls_clone = calls.clone();

    let dispatcher =
        move |req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            let url = req.get_url().to_string();
            calls_clone.lock().unwrap().insert(url.clone(), true);

            let content = match url.as_str() {
                "http://example.com/raw" => r#"<esi:vars>RAW</esi:vars>"#,
                "http://example.com/processed" => r#"<esi:vars>PROCESSED</esi:vars>"#,
                _ => "UNKNOWN",
            };

            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(content),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    // Include without dca should insert verbatim (ESI not processed)
    // Eval without dca defaults to "none" which processes in parent context
    assert_eq!(
        result, r#"<esi:vars>RAW</esi:vars>PROCESSED"#,
        "Include without dca should insert verbatim, eval should process as ESI"
    );

    // Verify both URLs were called
    let call_map = calls.lock().unwrap();
    assert!(call_map.contains_key("http://example.com/raw"));
    assert!(call_map.contains_key("http://example.com/processed"));
    Ok(())
}

/// Test that eval with onerror="continue" inserts nothing on failure (per ESI spec)
#[test]
fn test_eval_onerror_continue() -> esi::Result<()> {
    let input = r#"Before<esi:eval src="http://example.com/fail" onerror="continue"/>After"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            // Return a failed response
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_status(500),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    // Per ESI spec: onerror="continue" deletes the tag with no output (not even a comment)
    assert_eq!(
        result, "BeforeAfter",
        "onerror='continue' should insert nothing on failure"
    );
    Ok(())
}

/// Test nested ESI in eval
#[test]
fn test_eval_with_nested_esi() -> esi::Result<()> {
    let input = r#"<esi:eval src="http://example.com/nested"/>"#;

    let call_count = Arc::new(Mutex::new(0));
    let call_count_clone = call_count.clone();

    let dispatcher = move |req: Request,
                           _maxwait: Option<u32>|
          -> esi::Result<esi::PendingFragmentContent> {
        let url = req.get_url().to_string();
        *call_count_clone.lock().unwrap() += 1;

        let content = match url.as_str() {
            "http://example.com/nested" => {
                // Return ESI with a choose block
                r#"<esi:choose><esi:when test="1 == 1">Chosen</esi:when><esi:otherwise>Not</esi:otherwise></esi:choose>"#
            }
            _ => "UNKNOWN",
        };

        Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
            Response::from_body(content),
        )))
    };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    assert_eq!(
        result, "Chosen",
        "eval should process nested ESI constructs"
    );
    assert_eq!(
        *call_count.lock().unwrap(),
        1,
        "Should only call dispatcher once"
    );
    Ok(())
}

/// Test that nested dca="esi" includes are rendered in correct document order (issue #45).
///
/// /page includes /header with dca="esi", and /header includes /menu with dca="esi".
/// The menu content must appear inline inside <nav>, NOT appended after </html>.
#[test]
fn test_nested_dca_esi_document_order() -> esi::Result<()> {
    let input = r#"<html>
<body>
  <header>
    <esi:include src="http://example.com/header" dca="esi" />
  </header>
  <main>Main content</main>
</body>
</html>"#;

    let dispatcher =
        |req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            let url = req.get_url_str();
            let content = if url.contains("/header") {
                r#"<h1>Site Title</h1>
<nav>
  <esi:include src="http://example.com/menu" dca="esi" />
</nav>"#
            } else if url.contains("/menu") {
                "<ul><li>Home</li><li>About</li></ul>"
            } else {
                ""
            };
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(content),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();

    // The menu must appear inside <nav>, inside <header>, before <main>.
    let nav_start = result.find("<nav>").expect("<nav> should be present");
    let menu_pos = result
        .find("<ul><li>Home</li>")
        .expect("menu content should be present");
    let nav_end = result.find("</nav>").expect("</nav> should be present");
    let main_pos = result.find("<main>").expect("<main> should be present");

    assert!(
        menu_pos > nav_start && menu_pos < nav_end,
        "Menu must appear inside <nav>. Got:\n{result}"
    );
    assert!(
        nav_end < main_pos,
        "</nav> must appear before <main>. Got:\n{result}"
    );
    Ok(())
}

/// Test three-level nested dca="esi" includes preserve document order.
#[test]
fn test_triple_nested_dca_esi_document_order() -> esi::Result<()> {
    let input =
        r#"<div class="root"><esi:include src="http://example.com/level1" dca="esi" /></div>"#;

    let dispatcher =
        |req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            let url = req.get_url_str();
            let content = if url.contains("/level1") {
                r#"[L1-before]<esi:include src="http://example.com/level2" dca="esi" />[L1-after]"#
            } else if url.contains("/level2") {
                r#"[L2-before]<esi:include src="http://example.com/level3" dca="esi" />[L2-after]"#
            } else if url.contains("/level3") {
                "[L3-content]"
            } else {
                ""
            };
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body(content),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut output = Vec::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut output, Some(&dispatcher), None)?;

    let result = String::from_utf8(output).unwrap();
    assert_eq!(
        result, r#"<div class="root">[L1-before][L2-before][L3-content][L2-after][L1-after]</div>"#,
        "Three-level nested dca='esi' must preserve document order"
    );
    Ok(())
}

/// Writer that snapshots its buffer on every flush(), so we can check
/// what data was available to the client at each flush point.
struct FlushTrackingWriter {
    data: Vec<u8>,
    /// Snapshots of `data` taken at each flush() call.
    flush_snapshots: Vec<Vec<u8>>,
}

impl FlushTrackingWriter {
    fn new() -> Self {
        Self {
            data: Vec::new(),
            flush_snapshots: Vec::new(),
        }
    }
}

impl std::io::Write for FlushTrackingWriter {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.data.extend_from_slice(buf);
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.flush_snapshots.push(self.data.clone());
        Ok(())
    }
}

/// Regression: content before an include should be flushed, not held
/// until the include resolves. Without flush() the StreamingBody buffers
/// everything and the client sees nothing until processing finishes.
#[test]
fn test_streaming_flush_before_pending_include() -> esi::Result<()> {
    let input = r#"<html><body>
<header>Header</header>
<esi:include src="http://example.com/slow-fragment" />
<footer>Footer</footer>
</body></html>"#;

    let dispatcher =
        |_req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                Response::from_body("FRAGMENT"),
            )))
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut writer = FlushTrackingWriter::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut writer, Some(&dispatcher), None)?;

    let result = String::from_utf8(writer.data).unwrap();
    assert!(
        result.contains("<header>Header</header>"),
        "Should contain header"
    );
    assert!(result.contains("FRAGMENT"), "Should contain fragment");
    assert!(
        result.contains("<footer>Footer</footer>"),
        "Should contain footer"
    );

    assert!(
        !writer.flush_snapshots.is_empty(),
        "expected at least one flush during processing"
    );

    // The content before the include must already be present at the first flush.
    let first_flush = String::from_utf8_lossy(&writer.flush_snapshots[0]);
    assert!(
        first_flush.contains("<header>Header</header>"),
        "First flush should contain content before the include. Got: {first_flush}"
    );

    Ok(())
}

/// End-to-end streaming test with a real slow backend (httpbin.org/delay/1).
///
/// The outer doc has fast content before a dca="esi" include whose fragment
/// itself contains a slow nested include. We verify that the fast content
/// is flushed to the client *before* the slow include resolves — i.e. the
/// stream doesn't stall at the outermost include boundary.
///
/// Requires network; ~1s. Run with: `cargo test -- --ignored`
#[test]
#[ignore]
fn test_nested_dca_esi_real_async_streaming() -> esi::Result<()> {
    let input = r#"<h1>Title</h1>
<esi:include src="http://example.com/parent" dca="esi" />
<footer>End</footer>"#;

    let dispatcher =
        |req: Request, _maxwait: Option<u32>| -> esi::Result<esi::PendingFragmentContent> {
            let url = req.get_url_str().to_string();
            if url.contains("example.com/parent") {
                // Immediate response; body has a nested include that hits a slow endpoint
                Ok(esi::PendingFragmentContent::CompletedRequest(Box::new(
                    Response::from_body(
                        "<p>Fast content</p>\n<esi:include src=\"https://httpbin.org/delay/1\" />",
                    ),
                )))
            } else {
                // Actually hit httpbin — this is the slow request
                let backend = Backend::builder("httpbin.org", "httpbin.org")
                    .enable_ssl()
                    .sni_hostname("httpbin.org")
                    .finish()
                    .map_err(|e| {
                        esi::ESIError::FragmentRequestError(format!(
                            "failed to create httpbin backend: {e}"
                        ))
                    })?;
                let pending = req.send_async(backend)?;
                Ok(esi::PendingFragmentContent::PendingRequest(Box::new(
                    pending,
                )))
            }
        };

    let reader = std::io::BufReader::new(std::io::Cursor::new(input.as_bytes()));
    let mut writer = FlushTrackingWriter::new();
    let mut processor = Processor::new(None, Configuration::default());
    processor.process_stream(reader, &mut writer, Some(&dispatcher), None)?;

    let result = String::from_utf8(writer.data).unwrap();

    assert!(result.contains("<h1>Title</h1>"));
    assert!(result.contains("<p>Fast content</p>"));
    assert!(result.contains("<footer>End</footer>"));

    // The key check: some flush snapshot must contain the fast content but
    // NOT the footer. The footer can't appear until the slow nested include
    // finishes, so if we see this split it means the stream didn't stall.
    let has_incremental_flush = writer.flush_snapshots.iter().any(|snap| {
        let s = String::from_utf8_lossy(snap);
        s.contains("<p>Fast content</p>") && !s.contains("<footer>End</footer>")
    });
    assert!(
        has_incremental_flush,
        "fast content should be flushed before the footer; snapshots: {:?}",
        writer
            .flush_snapshots
            .iter()
            .map(|s| String::from_utf8_lossy(s).to_string())
            .collect::<Vec<_>>()
    );

    Ok(())
}