netget-tool 0.3.5

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
//! One-request-per-connection HTTP/1.1 InfluxDB endpoint with owned tasks: v2 writes, Flux
//! queries at `/api/v2/query`, InfluxQL at the v1-compatible `/query`, and `/ping`.
pub mod actions;
pub mod codec;
pub mod query;
use crate::protocol::{Event, SpawnContext};
use crate::server::{
    accept_bounded::{accept_bounded, ConnectionLimiter, DEFAULT_MAX_CONNECTIONS},
    connection::ConnectionId,
};
use crate::state::{
    server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo},
    AccessLogOwner,
};
use crate::{console_error, console_info};
use anyhow::{ensure, Context, Result};
use bytes::Bytes;
use http_body_util::{BodyExt, Full, Limited};
use hyper::{
    body::Incoming,
    header::{HeaderValue, ALLOW, AUTHORIZATION, CONTENT_ENCODING, CONTENT_TYPE, RETRY_AFTER},
    server::conn::http1,
    service::service_fn,
    Method, Request, Response, StatusCode,
};
use hyper_util::rt::{TokioIo, TokioTimer};
use serde_json::json;
use std::{collections::BTreeMap, convert::Infallible, net::SocketAddr, sync::Arc, time::Duration};
use tokio::sync::Notify;

pub const HEADER_TIMEOUT: Duration = Duration::from_secs(30);
pub const BODY_TIMEOUT: Duration = Duration::from_secs(30);
pub const WRITE_TIMEOUT: Duration = Duration::from_secs(10);
pub const MAX_HEADER_BYTES: usize = 32 * 1024;
pub const MAX_HEADERS: usize = 64;
const CAP_REFUSAL:&[u8]=b"HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\nConnection: close\r\nRetry-After: 5\r\n\r\n";
#[derive(Clone)]
struct Config {
    token: Option<String>,
    llm_fallback: bool,
}
pub struct InfluxDbServer;
impl InfluxDbServer {
    pub async fn spawn(ctx: SpawnContext) -> Result<SocketAddr> {
        let token = ctx
            .startup_params
            .as_ref()
            .map(|p| p.get_optional_string("auth_token"))
            .transpose()?
            .flatten();
        if let Some(token) = &token {
            codec::validate_token(token)?;
        }
        let llm_fallback = ctx
            .startup_params
            .as_ref()
            .map(|p| p.get_optional_bool("llm_fallback"))
            .transpose()?
            .flatten()
            .unwrap_or(codec::DEFAULT_LLM_FALLBACK);
        let config = Arc::new(Config {
            token,
            llm_fallback,
        });
        let listener = tokio::net::TcpListener::bind(ctx.legacy_listen_addr()).await?;
        let local = listener.local_addr()?;
        console_info!(
            ctx.status_tx,
            "InfluxDB v2 write listening on {} (auth_required={},llm_fallback={})",
            local,
            config.token.is_some(),
            llm_fallback
        );
        let owner = ctx.state.clone();
        let server_id = ctx.server_id;
        owner.spawn_server_task(server_id,async move {
            let limiter=ConnectionLimiter::new(DEFAULT_MAX_CONNECTIONS);
            loop {
                let (stream,peer,permit)=match accept_bounded(&listener,&limiter,CAP_REFUSAL,"InfluxDB",Some(&ctx.status_tx)).await {
                    Ok(value)=>value,
                    Err(error)=>{console_error!(ctx.status_tx,"InfluxDB accept failed: {}",error);break;}
                };
                let id=ConnectionId::new(ctx.state.get_next_unified_id().await);
                let now=crate::utils::clock::Instant::now();
                ctx.state.add_connection_to_server(server_id,ConnectionState {id,remote_addr:peer,local_addr:local,bytes_sent:0,bytes_received:0,packets_sent:0,packets_received:0,last_activity:now,status:ConnectionStatus::Active,status_changed_at:now,protocol_info:ProtocolConnectionInfo::empty()}).await;
                let child_ctx=ctx.clone(); let config=config.clone();
                ctx.state.spawn_server_task(server_id,async move {
                    let _permit=permit;
                    let write_ready=Arc::new(Notify::new());
                    let service_ready=write_ready.clone();
                    let request_ctx=child_ctx.clone();
                    let service=service_fn(move |request| {
                        let ctx=request_ctx.clone(); let config=config.clone(); let ready=service_ready.clone();
                        async move {
                            let response=handle_request(request,peer,id,&ctx,&config).await;
                            ready.notify_one();
                            Ok::<_,Infallible>(response)
                        }
                    });
                    let mut builder=http1::Builder::new();
                    builder.keep_alive(false).timer(TokioTimer::new()).header_read_timeout(HEADER_TIMEOUT).max_headers(MAX_HEADERS).max_buf_size(MAX_HEADER_BYTES);
                    let connection=builder.serve_connection(TokioIo::new(stream),service);
                    tokio::pin!(connection);
                    tokio::select! {
                        result=&mut connection=>{if let Err(error)=result {console_error!(child_ctx.status_tx,"InfluxDB HTTP framing/header error: {}",error);}}
                        _=async {write_ready.notified().await;tokio::time::sleep(WRITE_TIMEOUT).await;}=>{console_error!(child_ctx.status_tx,"InfluxDB decision=fail_closed_response_write_timeout");}
                    }
                    child_ctx.state.remove_connection_from_server(server_id,id).await;
                    let _=child_ctx.status_tx.send("__UPDATE_UI__".into());
                }).await;
            }
        }).await;
        Ok(local)
    }
}
fn response(status: u16, body: serde_json::Value, retry: Option<u16>) -> Response<Full<Bytes>> {
    let bytes = if status == 204 {
        Vec::new()
    } else {
        serde_json::to_vec(&body).unwrap_or_default()
    };
    let mut r = Response::new(Full::new(Bytes::from(bytes)));
    *r.status_mut() = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    r.headers_mut()
        .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
    if let Some(n) = retry {
        if let Ok(v) = HeaderValue::from_str(&n.to_string()) {
            r.headers_mut().insert(RETRY_AFTER, v);
        }
    }
    r
}
fn code(status: u16) -> &'static str {
    match status {
        400 => "invalid",
        401 => "unauthorized",
        403 => "forbidden",
        404 => "not found",
        405 => "method not allowed",
        408 => "request timeout",
        413 => "request too large",
        415 => "unsupported media type",
        422 => "unprocessable entity",
        429 => "too many requests",
        503 => "unavailable",
        _ => "internal error",
    }
}
fn error(status: u16, message: &str) -> Response<Full<Bytes>> {
    response(status, json!({"code":code(status),"message":message}), None)
}
pub(crate) fn query_piece(s: &str) -> Result<String> {
    let b = s.as_bytes();
    let mut at = 0;
    let mut out = Vec::new();
    while at < b.len() {
        match b[at] {
            b'+' => {
                out.push(b' ');
                at += 1;
            }
            b'%' => {
                let pair = b.get(at + 1..at + 3).context("truncated query escape")?;
                let hex = std::str::from_utf8(pair)?;
                out.push(u8::from_str_radix(hex, 16).context("invalid query escape")?);
                at += 3;
            }
            c => {
                out.push(c);
                at += 1;
            }
        }
    }
    Ok(String::from_utf8(out)?)
}
fn targets(query: Option<&str>) -> Result<(String, String, codec::Precision)> {
    let mut params = BTreeMap::new();
    for field in query.context("org and bucket required")?.split('&') {
        let (key, value) = field
            .split_once('=')
            .context("query parameter requires value")?;
        let key = query_piece(key)?;
        let value = query_piece(value)?;
        ensure!(
            ["org", "bucket", "precision"].contains(&key.as_str()),
            "unsupported query parameter"
        );
        ensure!(
            params.insert(key, value).is_none(),
            "duplicate query parameter"
        );
    }
    let org = params
        .remove("org")
        .context("org required (orgID query not implemented)")?;
    let bucket = params.remove("bucket").context("bucket required")?;
    codec::validate_target(&org)?;
    codec::validate_target(&bucket)?;
    let precision =
        codec::Precision::parse(params.get("precision").map(String::as_str).unwrap_or("ns"))?;
    Ok((org, bucket, precision))
}
fn authorized(headers: &hyper::HeaderMap, expected: Option<&str>) -> bool {
    let Some(expected) = expected else {
        return true;
    };
    let values = headers.get_all(AUTHORIZATION).iter().collect::<Vec<_>>();
    if values.len() != 1 {
        return false;
    }
    let Some((scheme, token)) = values[0].to_str().ok().and_then(|v| v.split_once(' ')) else {
        return false;
    };
    if !["Token", "Bearer"].contains(&scheme) || codec::validate_token(token).is_err() {
        return false;
    }
    same_secret(expected, token)
}
/// Compare in time independent of where the first difference is.
fn same_secret(expected: &str, offered: &str) -> bool {
    let mut difference = expected.len() ^ offered.len();
    for at in 0..expected.len().max(offered.len()) {
        difference |= usize::from(
            expected.as_bytes().get(at).copied().unwrap_or(0)
                ^ offered.as_bytes().get(at).copied().unwrap_or(0),
        );
    }
    difference == 0
}
/// A query's credential: the `Token`/`Bearer` header, or for InfluxQL the v1 forms InfluxDB 2
/// accepts in its compatibility API — the token as the `p` parameter, or as the password of
/// HTTP Basic authentication. The user name is not checked, as InfluxDB 2 does not check it.
fn query_authorized(
    headers: &hyper::HeaderMap,
    request: &query::QueryRequest,
    expected: Option<&str>,
) -> bool {
    let Some(expected) = expected else {
        return true;
    };
    if authorized(headers, Some(expected)) {
        return true;
    }
    if request.language != query::Language::Influxql {
        return false;
    }
    if let Some(p) = &request.v1_password {
        return same_secret(expected, p);
    }
    let values = headers.get_all(AUTHORIZATION).iter().collect::<Vec<_>>();
    let [value] = values.as_slice() else {
        return false;
    };
    let Some(encoded) = value.to_str().ok().and_then(|v| v.strip_prefix("Basic ")) else {
        return false;
    };
    use base64::Engine as _;
    let Some(decoded) = base64::engine::general_purpose::STANDARD
        .decode(encoded.trim())
        .ok()
        .and_then(|d| String::from_utf8(d).ok())
    else {
        return false;
    };
    decoded
        .split_once(':')
        .is_some_and(|(_, password)| same_secret(expected, password))
}
fn decide(
    decision: actions::Decision,
    parsed: &codec::ParsedBatch,
) -> Result<Response<Full<Bytes>>> {
    let total = parsed.points.len() + parsed.errors.len();
    let (accepted, message) = match decision {
        actions::Decision::Reject {
            status,
            message,
            retry_after_seconds,
        } => {
            return Ok(response(
                status,
                json!({"code":code(status),"message":message,"accepted_points":0,"rejected_points":total}),
                retry_after_seconds,
            ))
        }
        actions::Decision::Accept => (
            parsed.points.iter().map(|p| p.line).collect::<Vec<_>>(),
            "Invalid line protocol".to_string(),
        ),
        actions::Decision::Partial {
            accepted_lines,
            message,
        } => {
            ensure!(
                accepted_lines
                    .iter()
                    .all(|n| parsed.points.iter().any(|p| p.line == *n)),
                "cannot accept a missing/invalid source line"
            );
            (accepted_lines, message)
        }
    };
    if accepted.len() == total && parsed.errors.is_empty() {
        return Ok(response(204, serde_json::Value::Null, None));
    }
    let line = parsed.errors.first().map(|e| e.line).or_else(|| {
        parsed
            .points
            .iter()
            .find(|p| !accepted.contains(&p.line))
            .map(|p| p.line)
    });
    Ok(response(
        400,
        json!({"code":"invalid","message":format!("partial write: {message}; accepted={} rejected={}",accepted.len(),total-accepted.len()),"line":line,"accepted_points":accepted.len(),"rejected_points":total-accepted.len()}),
        None,
    ))
}
async fn handle_request(
    request: Request<Incoming>,
    peer: SocketAddr,
    id: ConnectionId,
    ctx: &SpawnContext,
    config: &Config,
) -> Response<Full<Bytes>> {
    let (parts, body) = request.into_parts();
    let bytes = match tokio::time::timeout(
        BODY_TIMEOUT,
        Limited::new(body, codec::MAX_BODY_BYTES).collect(),
    )
    .await
    {
        Err(_) => return error(408, "Request body deadline exceeded"),
        Ok(Err(error)) => {
            return if error
                .downcast_ref::<http_body_util::LengthLimitError>()
                .is_some()
            {
                self::error(413, "Request body limit exceeded")
            } else {
                self::error(400, "Incomplete HTTP body")
            };
        }
        Ok(Ok(body)) => body.to_bytes(),
    };
    ctx.state
        .update_connection_stats(
            ctx.server_id,
            id,
            Some(bytes.len() as u64),
            None,
            Some(1),
            None,
        )
        .await;
    match parts.uri.path() {
        "/api/v2/write" => {}
        "/api/v2/query" => {
            return handle_query(&parts, &bytes, peer, id, ctx, config, query::Language::Flux).await
        }
        "/query" => {
            return handle_query(
                &parts,
                &bytes,
                peer,
                id,
                ctx,
                config,
                query::Language::Influxql,
            )
            .await
        }
        "/ping" => return ping(&parts.method),
        _ => {
            return error(
                404,
                "Only /api/v2/write, /api/v2/query, /query and /ping are served",
            )
        }
    }
    if parts.method != Method::POST {
        let mut r = error(405, "Only POST is supported");
        r.headers_mut()
            .insert(ALLOW, HeaderValue::from_static("POST"));
        return r;
    }
    let (org,bucket,precision)=match targets(parts.uri.query()) {Ok(v)=>v,Err(_)=>return error(400,"org, bucket and optional ns/us/ms/s precision required; invalid/duplicate/unsupported query")};
    if !authorized(&parts.headers, config.token.as_deref()) {
        return error(401, "unauthorized access");
    }
    if parts.headers.get_all(CONTENT_ENCODING).iter().count() > 1
        || parts.headers.get_all(CONTENT_TYPE).iter().count() > 1
    {
        return error(400, "Duplicate encoding/content type headers");
    }
    if let Some(ct) = parts.headers.get(CONTENT_TYPE) {
        let valid = ct.to_str().ok().is_some_and(|s| {
            let mut p = s.split(';');
            p.next()
                .is_some_and(|mime| mime.trim().eq_ignore_ascii_case("text/plain"))
                && p.all(|x| x.trim().eq_ignore_ascii_case("charset=utf-8"))
        });
        if !valid {
            return error(415, "Expected text/plain UTF-8 line protocol");
        }
    }
    let encoding = match parts
        .headers
        .get(CONTENT_ENCODING)
        .map(|v| v.to_str())
        .transpose()
    {
        Ok(Some(v)) => v,
        Ok(None) => "identity",
        Err(_) => return error(415, "Invalid content encoding"),
    };
    if !["identity", "gzip"].contains(&encoding) {
        return error(415, "Only identity/gzip encoding supported");
    }
    let body = match codec::decode_body(&bytes, encoding) {
        Ok(b) => b,
        Err(e) => {
            return error(
                if e.to_string().contains("byte limit") {
                    413
                } else {
                    400
                },
                "Malformed or oversized compressed body",
            )
        }
    };
    let received_ns = crate::utils::clock::SystemTime::now()
        .duration_since(crate::utils::clock::UNIX_EPOCH)
        .ok()
        .and_then(|t| i64::try_from(t.as_nanos()).ok());
    let Some(received_ns) = received_ns else {
        return error(500, "Receiver clock out of range");
    };
    let parsed = match codec::parse_batch(&body, precision, received_ns) {
        Ok(batch) => batch,
        Err(e) => {
            return error(
                if e.to_string().contains("limit") {
                    413
                } else {
                    400
                },
                "Invalid/oversized line protocol batch",
            )
        }
    };
    let event = Event::new(
        &actions::INFLUX_WRITE_EVENT,
        json!({"org":org,"bucket":bucket,"precision":precision,"points":parsed.points,"errors":parsed.errors,"authenticated":true,"auth_required":config.token.is_some(),"source_addr":peer.to_string()}),
    );
    let configured = ctx
        .state
        .get_event_handler_config(ctx.server_id)
        .await
        .is_some_and(|c| c.find_handler("influx_write").is_some());
    let decision = if configured || config.llm_fallback {
        match crate::llm::action_helper::call_llm(
            &ctx.llm_client,
            &ctx.state,
            ctx.server_id,
            Some(id),
            &event,
            &actions::InfluxDbProtocol::new(),
        )
        .await
        {
            Ok(result) if result.failures.is_empty() => {
                let decisions = result
                    .protocol_results
                    .iter()
                    .filter_map(|r| match r {
                        crate::llm::ActionResult::Custom { name, data }
                            if name == "influx_write_decision" =>
                        {
                            Some(data.clone())
                        }
                        _ => None,
                    })
                    .collect::<Vec<_>>();
                if decisions.len() > 1 {
                    return failed(ctx, id, event.data, "fail_closed_multiple_decisions").await;
                }
                match decisions
                    .first()
                    .map(|d| serde_json::from_value::<actions::Decision>(d.clone()))
                    .transpose()
                {
                    Ok(Some(d)) => d,
                    Ok(None) => actions::Decision::Accept,
                    Err(_) => {
                        return failed(ctx, id, event.data, "fail_closed_invalid_decision").await
                    }
                }
            }
            Ok(_) => return failed(ctx, id, event.data, "fail_closed_handler_action_error").await,
            Err(_) => return failed(ctx, id, event.data, "fail_closed_handler_error").await,
        }
    } else {
        ctx.state
            .record_access_log(
                AccessLogOwner::Server(ctx.server_id.as_u32()),
                "InfluxDB",
                Some(id.as_u32()),
                "influx_write",
                event.data.clone(),
                vec![json!({"type":"accept_influx_points"})],
            )
            .await;
        actions::Decision::Accept
    };
    match decide(decision, &parsed) {
        Ok(r) => r,
        Err(_) => failed(ctx, id, event.data, "fail_closed_invalid_partial_decision").await,
    }
}
async fn failed(
    ctx: &SpawnContext,
    id: ConnectionId,
    data: serde_json::Value,
    decision: &str,
) -> Response<Full<Bytes>> {
    console_error!(ctx.status_tx, "InfluxDB decision={}", decision);
    ctx.state
        .record_access_log(
            AccessLogOwner::Server(ctx.server_id.as_u32()),
            "InfluxDB",
            Some(id.as_u32()),
            "influx_handler_failed",
            data,
            vec![json!({"decision":decision})],
        )
        .await;
    error(503, "Write handler failed; no acceptance confirmed")
}

/// `GET|HEAD /ping`: 204, as InfluxDB answers it, naming what answered.
fn ping(method: &Method) -> Response<Full<Bytes>> {
    if method != Method::GET && method != Method::HEAD {
        let mut r = error(405, "Only GET and HEAD are supported");
        r.headers_mut()
            .insert(ALLOW, HeaderValue::from_static("GET, HEAD"));
        return r;
    }
    let mut r = response(204, serde_json::Value::Null, None);
    r.headers_mut().remove(CONTENT_TYPE);
    r.headers_mut()
        .insert("X-Influxdb-Version", HeaderValue::from_static("netget"));
    r.headers_mut()
        .insert("X-Influxdb-Build", HeaderValue::from_static("OSS"));
    r
}
/// An error in the dialect's own shape: v2's `{"code","message"}` for Flux, v1's `{"error"}`
/// for InfluxQL.
fn query_error(language: query::Language, status: u16, message: &str) -> Response<Full<Bytes>> {
    match language {
        query::Language::Flux => error(status, message),
        query::Language::Influxql => response(status, json!({ "error": message }), None),
    }
}
fn body_response(status: u16, content_type: &'static str, body: Vec<u8>) -> Response<Full<Bytes>> {
    let mut r = Response::new(Full::new(Bytes::from(body)));
    *r.status_mut() = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    r.headers_mut()
        .insert(CONTENT_TYPE, HeaderValue::from_static(content_type));
    r
}
async fn handle_query(
    parts: &hyper::http::request::Parts,
    body: &[u8],
    peer: SocketAddr,
    id: ConnectionId,
    ctx: &SpawnContext,
    config: &Config,
    language: query::Language,
) -> Response<Full<Bytes>> {
    let allowed = match language {
        query::Language::Flux => parts.method == Method::POST,
        query::Language::Influxql => parts.method == Method::POST || parts.method == Method::GET,
    };
    if !allowed {
        let mut r = query_error(
            language,
            405,
            "Method not supported for this query endpoint",
        );
        let allow = match language {
            query::Language::Flux => "POST",
            query::Language::Influxql => "GET, POST",
        };
        r.headers_mut()
            .insert(ALLOW, HeaderValue::from_static(allow));
        return r;
    }
    if parts
        .headers
        .get(CONTENT_ENCODING)
        .is_some_and(|e| !e.as_bytes().eq_ignore_ascii_case(b"identity"))
    {
        return query_error(language, 415, "Compressed query bodies are not accepted");
    }
    let content_type = parts
        .headers
        .get(CONTENT_TYPE)
        .and_then(|v| v.to_str().ok());
    let parsed = match language {
        query::Language::Flux => query::parse_flux(parts.uri.query(), content_type, body),
        query::Language::Influxql => query::parse_influxql(parts.uri.query(), content_type, body),
    };
    let request = match parsed {
        Ok(request) => request,
        Err(e) => {
            return query_error(
                language,
                400,
                &crate::utils::truncate_for_log(&e.to_string(), 512),
            )
        }
    };
    if !query_authorized(&parts.headers, &request, config.token.as_deref()) {
        return query_error(language, 401, "unauthorized access");
    }
    let mut data = serde_json::to_value(&request).unwrap_or_else(|_| json!({}));
    data["authenticated"] = json!(true);
    data["auth_required"] = json!(config.token.is_some());
    data["source_addr"] = json!(peer.to_string());
    let event = Event::new(&actions::INFLUX_QUERY_EVENT, data);
    let configured = ctx
        .state
        .get_event_handler_config(ctx.server_id)
        .await
        .is_some_and(|c| c.find_handler("influx_query").is_some());
    if !configured && !config.llm_fallback {
        // Nothing here holds data, so without a handler or the model there is no answer — and
        // an empty result would be an assertion that there is no data, which nobody made.
        return query_failed(
            ctx,
            id,
            event.data,
            language,
            "fail_closed_no_query_handler",
        )
        .await;
    }
    let answers = match crate::llm::action_helper::call_llm(
        &ctx.llm_client,
        &ctx.state,
        ctx.server_id,
        Some(id),
        &event,
        &actions::InfluxDbProtocol::new(),
    )
    .await
    {
        Ok(result) if result.failures.is_empty() => result
            .protocol_results
            .iter()
            .filter_map(|r| match r {
                crate::llm::ActionResult::Custom { name, data }
                    if name == actions::QUERY_ANSWER =>
                {
                    Some(data.clone())
                }
                _ => None,
            })
            .collect::<Vec<_>>(),
        Ok(_) => {
            return query_failed(
                ctx,
                id,
                event.data,
                language,
                "fail_closed_handler_action_error",
            )
            .await
        }
        Err(_) => {
            return query_failed(ctx, id, event.data, language, "fail_closed_handler_error").await
        }
    };
    let answer = match answers.as_slice() {
        [answer] => answer,
        [] => return query_failed(ctx, id, event.data, language, "fail_closed_no_answer").await,
        _ => {
            return query_failed(
                ctx,
                id,
                event.data,
                language,
                "fail_closed_multiple_answers",
            )
            .await
        }
    };
    match render_answer(answer, &request) {
        Ok(response) => response,
        Err(_) => query_failed(ctx, id, event.data, language, "fail_closed_invalid_answer").await,
    }
}
/// Render the model's answer in the request's dialect.
fn render_answer(
    answer: &serde_json::Value,
    request: &query::QueryRequest,
) -> Result<Response<Full<Bytes>>> {
    if answer["mode"] == "error" {
        let status = u16::try_from(answer["status"].as_u64().context("status")?)?;
        let message = answer["message"].as_str().context("message")?;
        return Ok(query_error(request.language, status, message));
    }
    let action = &answer["action"];
    match request.language {
        query::Language::Flux => {
            ensure!(
                action.get("statements").is_none(),
                "a Flux query is answered with 'tables', not 'statements'"
            );
            let tables =
                query::tables_from_json(action.get("tables").context("tables")?, &mut 0, &mut 0)?;
            Ok(body_response(
                200,
                "text/csv; charset=utf-8",
                query::render_flux(&tables)?.into_bytes(),
            ))
        }
        query::Language::Influxql => {
            let results = query::statements_from_json(action)?;
            ensure!(
                results.len() == request.statements.len(),
                "{} answers for {} statements",
                results.len(),
                request.statements.len()
            );
            Ok(body_response(
                200,
                "application/json",
                query::render_influxql(&results, request.epoch.as_deref())?,
            ))
        }
    }
}
async fn query_failed(
    ctx: &SpawnContext,
    id: ConnectionId,
    data: serde_json::Value,
    language: query::Language,
    decision: &str,
) -> Response<Full<Bytes>> {
    console_error!(ctx.status_tx, "InfluxDB query decision={}", decision);
    ctx.state
        .record_access_log(
            AccessLogOwner::Server(ctx.server_id.as_u32()),
            "InfluxDB",
            Some(id.as_u32()),
            "influx_query_failed",
            data,
            vec![json!({ "decision": decision })],
        )
        .await;
    query_error(
        language,
        503,
        "Query handler failed; no answer is available",
    )
}