awsim 0.5.0

AWSim — a fully offline, free AWS development environment
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
//! API Gateway proxy handler for the main `awsim` binary.
//!
//! Handles requests at `/restapis/{api_id}/{stage}/{*path}` and routes them
//! to the appropriate Lambda function via the API Gateway service state.

use std::sync::Arc;

use axum::body::Body;
use axum::extract::{Path, State};
use axum::http::{HeaderMap, Method, Response, StatusCode, Uri};
use axum::response::IntoResponse;
use bytes::Bytes;
use serde_json::json;
use tracing::{debug, warn};

use awsim_apigateway::vtl::{self, RenderContext};
use awsim_apigateway::{
    ApiGatewayService, ApiGatewayV1Service, AuthorizationOutcome, AuthorizationStep, Integration,
    IntegrationResponse, LambdaInvocation, V1ProxyMatch, apply_lambda_response,
};
use awsim_core::{RequestContext, ServiceHandler};

/// State provided to the proxy handler.
#[derive(Clone)]
pub struct ProxyState {
    /// The API Gateway v2 (HTTP APIs) service.
    pub apigw: Arc<ApiGatewayService>,
    /// The API Gateway v1 (REST APIs) service.
    pub apigw_v1: Arc<ApiGatewayV1Service>,
    /// The Lambda service (used to invoke functions).
    pub lambda: Option<Arc<dyn ServiceHandler>>,
    /// Outbound HTTP client used by HTTP / HTTP_PROXY integrations.
    /// Built once and shared so the connection pool is reused.
    pub http_client: reqwest::Client,
    pub default_account_id: String,
    pub default_region: String,
}

/// Axum handler for `/restapis/{api_id}/{stage}/_user_request_/{*path}`
/// and the bare `/restapis/{api_id}/{stage}/_user_request_` (no path).
///
/// The literal `_user_request_` segment is required so management routes
/// like `/restapis/{id}/resources/...` and `/restapis/{id}/authorizers`
/// don't get accidentally swallowed by this proxy.
pub async fn handle_proxy(
    State(state): State<ProxyState>,
    Path(params): Path<Vec<(String, String)>>,
    method: Method,
    headers: HeaderMap,
    uri: Uri,
    body: Bytes,
) -> impl IntoResponse {
    let api_id = params
        .iter()
        .find(|(k, _)| k == "api_id")
        .map(|(_, v)| v.clone())
        .unwrap_or_default();
    let stage = params
        .iter()
        .find(|(k, _)| k == "stage")
        .map(|(_, v)| v.clone())
        .unwrap_or_default();
    let path = params
        .iter()
        .find(|(k, _)| k == "path")
        .map(|(_, v)| v.clone())
        .unwrap_or_default();

    debug!(
        api_id = %api_id,
        stage = %stage,
        path = %path,
        method = %method,
        "API Gateway proxy request"
    );

    // Get the API Gateway state for the default account/region.
    let agw_state = state
        .apigw
        .store()
        .get(&state.default_account_id, &state.default_region);

    let path_with_slash = if path.is_empty() {
        "/".to_string()
    } else {
        format!("/{path}")
    };
    let query_string = uri.query().unwrap_or("");

    // Route the request through the API Gateway proxy logic.
    let proxy_result = awsim_apigateway::proxy_request(
        &api_id,
        &stage,
        method.as_str(),
        &path_with_slash,
        query_string,
        &headers,
        &body,
        &agw_state,
    )
    .await;

    if let Some(proxy) = proxy_result {
        return invoke_lambda(&state, &method, &uri, &proxy.integration_uri, proxy.event).await;
    }

    // No v2 match — try v1 (REST APIs). Same id namespace from the
    // caller's perspective; we check both stores in turn.
    let v1_state = state
        .apigw_v1
        .store()
        .get(&state.default_account_id, &state.default_region);
    let headers_map: std::collections::HashMap<String, String> = headers
        .iter()
        .filter_map(|(name, value)| {
            value
                .to_str()
                .ok()
                .map(|v| (name.as_str().to_string(), v.to_string()))
        })
        .collect();
    let v1_match = awsim_apigateway::v1::proxy_request(
        &v1_state,
        &api_id,
        &stage,
        method.as_str(),
        &path_with_slash,
        query_string,
        &headers_map,
        &body,
        &state.default_account_id,
        &state.default_region,
    );

    match v1_match {
        Some(m) => dispatch_v1(&state, &method, &uri, &headers, &body, query_string, m).await,
        None => {
            warn!(
                api_id = %api_id,
                path = %path_with_slash,
                method = %method,
                "No matching route found in API Gateway (tried both v1 and v2)"
            );
            error_response(
                StatusCode::NOT_FOUND,
                &format!("No route found for {method} {path_with_slash} in API {api_id}"),
            )
        }
    }
}

/// Dispatch a matched v1 (REST APIs) integration based on its type. MOCK,
/// AWS/AWS_PROXY (Lambda) and HTTP/HTTP_PROXY (outbound fetch) are all
/// handled; anything else returns 501.
#[allow(clippy::too_many_arguments)]
async fn dispatch_v1(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    headers: &HeaderMap,
    body: &Bytes,
    query_string: &str,
    mut m: V1ProxyMatch,
) -> Response<Body> {
    debug!(
        integration_type = %m.integration_type,
        resource = %m.matched_resource_path,
        "v1 stage invocation matched"
    );

    // Drive the authorizer state machine to a terminal step. Custom
    // Lambda authorizers may take one Lambda call before resolving.
    let outcome = match resolve_authorization(state, method, uri, &mut m).await {
        AuthResolution::Allowed(o) => Some(o),
        AuthResolution::None => None,
        AuthResolution::Unauthorized(reason) => {
            return error_response(StatusCode::UNAUTHORIZED, &reason);
        }
        AuthResolution::Forbidden(reason) => {
            return error_response(StatusCode::FORBIDDEN, &reason);
        }
    };
    if let Some(outcome) = outcome {
        merge_authorizer_into_event(&mut m, &outcome);
    }

    let render_ctx = render_context_from_match(&m, body);
    match m.integration_type.as_str() {
        "MOCK" => dispatch_mock(&m, &render_ctx),
        "AWS_PROXY" => invoke_lambda(state, method, uri, &m.integration_uri, m.event).await,
        "AWS" => dispatch_aws_non_proxy(state, method, uri, &m, &render_ctx).await,
        // HTTP_PROXY forwards verbatim — no template processing, no
        // method/integration response mapping.
        "HTTP_PROXY" => {
            proxy_http(
                &state.http_client,
                method,
                &m.integration_uri,
                headers,
                body,
                query_string,
            )
            .await
        }
        // HTTP applies request templates before forwarding and response
        // templates on the way back.
        "HTTP" => {
            dispatch_http_non_proxy(
                &state.http_client,
                method,
                headers,
                query_string,
                &m,
                &render_ctx,
            )
            .await
        }
        other => error_response(
            StatusCode::NOT_IMPLEMENTED,
            &format!("Integration type {other} is not yet supported by AWSim"),
        ),
    }
}

enum AuthResolution {
    None,
    Allowed(AuthorizationOutcome),
    Unauthorized(String),
    Forbidden(String),
}

/// Drive the authorizer state machine. Most cases resolve in one step;
/// custom Lambda authorizers take one Lambda invocation first, then
/// `apply_lambda_response` returns the final step. The loop bound is
/// 2 — there's no scenario in which a single authorizer needs more
/// than one Lambda round-trip.
async fn resolve_authorization(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    m: &mut V1ProxyMatch,
) -> AuthResolution {
    // Take ownership so we can re-assign as the state machine runs.
    let mut step = std::mem::replace(&mut m.authorization, AuthorizationStep::NotConfigured);
    for _ in 0..2 {
        match step {
            AuthorizationStep::NotConfigured => return AuthResolution::None,
            AuthorizationStep::Allowed(outcome) => return AuthResolution::Allowed(outcome),
            AuthorizationStep::Unauthorized(reason) => return AuthResolution::Unauthorized(reason),
            AuthorizationStep::Forbidden(reason) => return AuthResolution::Forbidden(reason),
            AuthorizationStep::InvokeLambda(invocation) => {
                let response = match invoke_authorizer_lambda(state, method, uri, &invocation).await
                {
                    Ok(v) => v,
                    Err(e) => {
                        return AuthResolution::Forbidden(format!(
                            "Lambda authorizer invocation failed: {e}"
                        ));
                    }
                };
                let cache = &state
                    .apigw_v1
                    .store()
                    .get(&state.default_account_id, &state.default_region)
                    .authorizer_cache;
                step = apply_lambda_response(cache, &invocation, &response);
            }
        }
    }
    AuthResolution::Forbidden(
        "Authorizer state machine did not converge after one Lambda round-trip".to_string(),
    )
}

async fn invoke_authorizer_lambda(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    invocation: &LambdaInvocation,
) -> Result<serde_json::Value, String> {
    let lambda = state
        .lambda
        .as_ref()
        .ok_or_else(|| "Lambda service not registered — cannot invoke authorizer".to_string())?;
    let function_name = extract_function_name(&invocation.authorizer_uri);
    let ctx = RequestContext {
        account_id: state.default_account_id.clone(),
        region: state.default_region.clone(),
        partition: awsim_core::DEFAULT_PARTITION.to_string(),
        service: "lambda".to_string(),
        access_key: None,
        request_id: uuid::Uuid::new_v4().to_string(),
        method: method.to_string(),
        uri: uri.to_string(),
        event_bus: None,
        source_ip: None,
        is_secure: false,
        internal_bypass: false,
    };
    let invoke_input = json!({
        "FunctionName": function_name,
        "InvocationType": "RequestResponse",
        "Payload": invocation.event.clone(),
    });
    let result = lambda
        .handle("Invoke", invoke_input, &ctx)
        .await
        .map_err(|e| e.message)?;
    // The Lambda service returns the function's raw return value.
    Ok(result)
}

/// Fold the authorizer's outcome into the proxy event so the integration
/// — and any VTL templates — can access it as `requestContext.authorizer`
/// or `$context.authorizer`.
fn merge_authorizer_into_event(m: &mut V1ProxyMatch, outcome: &AuthorizationOutcome) {
    let mut authorizer = serde_json::Map::new();
    authorizer.insert(
        "principalId".to_string(),
        serde_json::Value::String(outcome.principal_id.clone()),
    );
    if let Some(ctx_obj) = outcome.context.as_object() {
        for (k, v) in ctx_obj {
            authorizer.insert(k.clone(), v.clone());
        }
    }
    let authorizer_value = serde_json::Value::Object(authorizer);

    if let Some(rc) = m
        .event
        .get_mut("requestContext")
        .and_then(serde_json::Value::as_object_mut)
    {
        rc.insert("authorizer".to_string(), authorizer_value.clone());
    }
    if let Some(rc) = m.request_context.as_object_mut() {
        rc.insert("authorizer".to_string(), authorizer_value);
    }
}

fn render_context_from_match(m: &V1ProxyMatch, body: &Bytes) -> RenderContext {
    RenderContext {
        body: std::str::from_utf8(body).unwrap_or("").to_string(),
        path_params: m.path_params.clone(),
        query_params: m.query_params.clone(),
        headers: m.headers.clone(),
        stage_variables: m.stage_variables.clone(),
        request_context: m.request_context.clone(),
    }
}

/// MOCK integrations don't reach a backend. AWS resolves them by:
/// 1. Rendering the request template — its JSON output drives which
///    integration response to pick (via the `statusCode` field).
/// 2. Rendering the chosen integration response's template as the body.
fn dispatch_mock(m: &V1ProxyMatch, render_ctx: &RenderContext) -> Response<Body> {
    let request_template = pick_template(&m.integration.request_templates, render_ctx);
    let rendered_request = request_template
        .as_deref()
        .map(|t| vtl::render(t, render_ctx))
        .unwrap_or_default();
    let status_code = serde_json::from_str::<serde_json::Value>(&rendered_request)
        .ok()
        .and_then(|v| v.get("statusCode").and_then(|s| s.as_u64()))
        .unwrap_or(200);
    let status_str = status_code.to_string();

    let integration_response = m
        .integration
        .integration_responses
        .get(&status_str)
        .or_else(|| pick_default_response(&m.integration));

    let body = integration_response
        .and_then(|r| pick_template(&r.response_templates, render_ctx))
        .map(|t| vtl::render(&t, render_ctx))
        .unwrap_or_else(|| "{}".to_string());

    let mut builder = Response::builder()
        .status(StatusCode::from_u16(status_code as u16).unwrap_or(StatusCode::OK))
        .header("content-type", "application/json");
    if let Some(r) = integration_response {
        builder = apply_response_parameters(builder, &r.response_parameters, render_ctx);
    }
    builder
        .body(Body::from(body))
        .unwrap_or_else(|_| empty_500())
}

async fn dispatch_aws_non_proxy(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    m: &V1ProxyMatch,
    render_ctx: &RenderContext,
) -> Response<Body> {
    let request_template = pick_template(&m.integration.request_templates, render_ctx);
    let rendered_request = request_template
        .as_deref()
        .map(|t| vtl::render(t, render_ctx))
        .unwrap_or_else(|| render_ctx.body.clone());
    let payload = serde_json::from_str::<serde_json::Value>(&rendered_request)
        .unwrap_or_else(|_| serde_json::Value::String(rendered_request.clone()));

    let result = match invoke_lambda_raw(state, method, uri, &m.integration_uri, payload).await {
        Ok(v) => v,
        Err(resp) => return resp,
    };
    let upstream_body = match &result {
        serde_json::Value::String(s) => s.clone(),
        other => other.to_string(),
    };

    let integration_response = pick_integration_response(&m.integration, &upstream_body);
    let status = integration_response
        .map(|r| r.status_code.parse::<u16>().unwrap_or(200))
        .unwrap_or(200);

    let body = integration_response
        .and_then(|r| pick_template(&r.response_templates, render_ctx))
        .map(|t| {
            let mut body_ctx = render_ctx.clone();
            body_ctx.body = upstream_body.clone();
            vtl::render(&t, &body_ctx)
        })
        .unwrap_or(upstream_body);

    let mut builder = Response::builder()
        .status(StatusCode::from_u16(status).unwrap_or(StatusCode::OK))
        .header("content-type", "application/json");
    if let Some(r) = integration_response {
        builder = apply_response_parameters(builder, &r.response_parameters, render_ctx);
    }
    builder
        .body(Body::from(body))
        .unwrap_or_else(|_| empty_500())
}

async fn dispatch_http_non_proxy(
    client: &reqwest::Client,
    method: &Method,
    headers: &HeaderMap,
    query_string: &str,
    m: &V1ProxyMatch,
    render_ctx: &RenderContext,
) -> Response<Body> {
    let request_template = pick_template(&m.integration.request_templates, render_ctx);
    let rendered_request = request_template
        .as_deref()
        .map(|t| vtl::render(t, render_ctx))
        .unwrap_or_else(|| render_ctx.body.clone());
    let mapped_body = Bytes::from(rendered_request);

    let upstream_resp = match perform_http(
        client,
        method,
        &m.integration_uri,
        headers,
        &mapped_body,
        query_string,
    )
    .await
    {
        Ok(resp) => resp,
        Err(resp) => return resp,
    };

    let upstream_body = String::from_utf8_lossy(&upstream_resp.body).into_owned();
    let integration_response = pick_integration_response(&m.integration, &upstream_body);
    let status = integration_response
        .map(|r| r.status_code.parse::<u16>().unwrap_or(upstream_resp.status))
        .unwrap_or(upstream_resp.status);

    let body = integration_response
        .and_then(|r| pick_template(&r.response_templates, render_ctx))
        .map(|t| {
            let mut body_ctx = render_ctx.clone();
            body_ctx.body = upstream_body.clone();
            vtl::render(&t, &body_ctx)
        })
        .unwrap_or(upstream_body);

    let mut builder =
        Response::builder().status(StatusCode::from_u16(status).unwrap_or(StatusCode::OK));
    for (name, value) in upstream_resp.headers.iter() {
        builder = builder.header(name, value);
    }
    if let Some(r) = integration_response {
        builder = apply_response_parameters(builder, &r.response_parameters, render_ctx);
    }
    builder
        .body(Body::from(body))
        .unwrap_or_else(|_| empty_500())
}

/// Pick the request/response template whose key matches the request's
/// content-type. Falls back to `application/json`, then any single key.
fn pick_template(
    map: &std::collections::HashMap<String, String>,
    ctx: &RenderContext,
) -> Option<String> {
    if map.is_empty() {
        return None;
    }
    let content_type = ctx
        .headers
        .iter()
        .find(|(k, _)| k.eq_ignore_ascii_case("content-type"))
        .map(|(_, v)| v.split(';').next().unwrap_or("").trim().to_string())
        .unwrap_or_default();
    if let Some(t) = map.get(&content_type) {
        return Some(t.clone());
    }
    if let Some(t) = map.get("application/json") {
        return Some(t.clone());
    }
    map.values().next().cloned()
}

fn pick_integration_response<'a>(
    integration: &'a Integration,
    upstream: &str,
) -> Option<&'a IntegrationResponse> {
    let mut default: Option<&IntegrationResponse> = None;
    for r in integration.integration_responses.values() {
        if r.selection_pattern.is_empty() {
            default = Some(r);
            continue;
        }
        if regex_like_match(&r.selection_pattern, upstream) {
            return Some(r);
        }
    }
    default.or_else(|| pick_default_response(integration))
}

fn pick_default_response(integration: &Integration) -> Option<&IntegrationResponse> {
    integration
        .integration_responses
        .values()
        .find(|r| r.selection_pattern.is_empty())
        .or_else(|| integration.integration_responses.values().next())
}

/// Approximate AWS' regex selection pattern. We don't pull in a regex
/// crate just for this — a literal substring check covers the common
/// `5\d\d` / `4\d\d` / `Error.*` patterns well enough that templates
/// at least exercise the right branch in tests.
fn regex_like_match(pattern: &str, body: &str) -> bool {
    if pattern.is_empty() {
        return true;
    }
    if pattern == ".*" {
        return true;
    }
    body.contains(pattern.trim_start_matches('^').trim_end_matches('$'))
}

fn apply_response_parameters(
    mut builder: axum::http::response::Builder,
    parameters: &std::collections::HashMap<String, String>,
    ctx: &RenderContext,
) -> axum::http::response::Builder {
    for (key, raw_value) in parameters {
        let header_name = match key.strip_prefix("method.response.header.") {
            Some(n) => n,
            None => continue,
        };
        let resolved = resolve_response_parameter(raw_value, ctx);
        builder = builder.header(header_name, resolved);
    }
    builder
}

/// Response parameter values come in two forms: a literal `'string'` or
/// a reference like `integration.response.body.foo` / `integration.response.header.bar`.
/// Strings without a leading single quote are treated as VTL templates,
/// which covers most user-authored values.
fn resolve_response_parameter(raw: &str, ctx: &RenderContext) -> String {
    let trimmed = raw.trim();
    if trimmed.starts_with('\'') && trimmed.ends_with('\'') && trimmed.len() >= 2 {
        return trimmed[1..trimmed.len() - 1].to_string();
    }
    vtl::render(trimmed, ctx)
}

struct UpstreamResponse {
    status: u16,
    headers: HeaderMap,
    body: Bytes,
}

async fn perform_http(
    client: &reqwest::Client,
    method: &Method,
    integration_uri: &str,
    headers: &HeaderMap,
    body: &Bytes,
    query_string: &str,
) -> Result<UpstreamResponse, Response<Body>> {
    let target_url = if query_string.is_empty() {
        integration_uri.to_string()
    } else if integration_uri.contains('?') {
        format!("{integration_uri}&{query_string}")
    } else {
        format!("{integration_uri}?{query_string}")
    };

    let reqwest_method = match reqwest::Method::from_bytes(method.as_str().as_bytes()) {
        Ok(m) => m,
        Err(e) => {
            return Err(error_response(
                StatusCode::BAD_GATEWAY,
                &format!("Invalid method for upstream: {e}"),
            ));
        }
    };

    let mut req = client.request(reqwest_method, &target_url);
    for (name, value) in headers.iter() {
        let lname = name.as_str().to_ascii_lowercase();
        if lname == "host" || lname == "content-length" {
            continue;
        }
        if let Ok(v) = value.to_str() {
            req = req.header(name.as_str(), v);
        }
    }
    if !body.is_empty() {
        req = req.body(body.to_vec());
    }
    let upstream = req.send().await.map_err(|e| {
        warn!(target = %target_url, error = %e, "HTTP integration upstream request failed");
        error_response(
            StatusCode::BAD_GATEWAY,
            &format!("Upstream request failed: {e}"),
        )
    })?;

    let status = upstream.status().as_u16();
    let mut hdrs = HeaderMap::new();
    for (name, value) in upstream.headers() {
        let lname = name.as_str().to_ascii_lowercase();
        if lname == "transfer-encoding" || lname == "content-length" || lname == "connection" {
            continue;
        }
        if let Ok(v) = axum::http::HeaderValue::from_bytes(value.as_bytes()) {
            hdrs.insert(name.clone(), v);
        }
    }
    let body = upstream.bytes().await.map_err(|e| {
        warn!(target = %target_url, error = %e, "Failed to read upstream body");
        error_response(
            StatusCode::BAD_GATEWAY,
            &format!("Failed to read upstream body: {e}"),
        )
    })?;
    Ok(UpstreamResponse {
        status,
        headers: hdrs,
        body,
    })
}

fn empty_500() -> Response<Body> {
    Response::builder()
        .status(StatusCode::INTERNAL_SERVER_ERROR)
        .body(Body::empty())
        .unwrap()
}

async fn invoke_lambda_raw(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    integration_uri: &str,
    payload: serde_json::Value,
) -> Result<serde_json::Value, Response<Body>> {
    let lambda_handler = match &state.lambda {
        Some(h) => Arc::clone(h),
        None => {
            warn!("Lambda service not registered — cannot invoke function");
            return Err(error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                "Lambda service not registered",
            ));
        }
    };
    let function_name = extract_function_name(integration_uri);
    let ctx = RequestContext {
        account_id: state.default_account_id.clone(),
        region: state.default_region.clone(),
        partition: awsim_core::DEFAULT_PARTITION.to_string(),
        service: "lambda".to_string(),
        access_key: None,
        request_id: uuid::Uuid::new_v4().to_string(),
        method: method.to_string(),
        uri: uri.to_string(),
        event_bus: None,
        source_ip: None,
        is_secure: false,
        internal_bypass: false,
    };
    let invoke_input = json!({
        "FunctionName": function_name,
        "InvocationType": "RequestResponse",
        "Payload": payload,
    });
    lambda_handler
        .handle("Invoke", invoke_input, &ctx)
        .await
        .map_err(|e| {
            warn!(
                function = %function_name,
                error = %e.message,
                "Lambda invocation failed"
            );
            error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                &format!("Lambda invocation error: {}", e.message),
            )
        })
}

/// Forward an incoming request to the integration URI with reqwest and
/// stream the upstream response back unchanged. Drops `host` / `content-length`
/// since reqwest sets them itself.
async fn proxy_http(
    client: &reqwest::Client,
    method: &Method,
    integration_uri: &str,
    headers: &HeaderMap,
    body: &Bytes,
    query_string: &str,
) -> Response<Body> {
    let target_url = if query_string.is_empty() {
        integration_uri.to_string()
    } else if integration_uri.contains('?') {
        format!("{integration_uri}&{query_string}")
    } else {
        format!("{integration_uri}?{query_string}")
    };

    let reqwest_method = match reqwest::Method::from_bytes(method.as_str().as_bytes()) {
        Ok(m) => m,
        Err(e) => {
            return error_response(
                StatusCode::BAD_GATEWAY,
                &format!("Invalid method for upstream: {e}"),
            );
        }
    };

    let mut req = client.request(reqwest_method, &target_url);
    for (name, value) in headers.iter() {
        let lname = name.as_str().to_ascii_lowercase();
        if lname == "host" || lname == "content-length" {
            continue;
        }
        if let Ok(v) = value.to_str() {
            req = req.header(name.as_str(), v);
        }
    }
    if !body.is_empty() {
        req = req.body(body.to_vec());
    }

    let upstream = match req.send().await {
        Ok(r) => r,
        Err(e) => {
            warn!(target = %target_url, error = %e, "HTTP proxy upstream request failed");
            return error_response(
                StatusCode::BAD_GATEWAY,
                &format!("Upstream request failed: {e}"),
            );
        }
    };

    let status =
        StatusCode::from_u16(upstream.status().as_u16()).unwrap_or(StatusCode::BAD_GATEWAY);
    let mut builder = Response::builder().status(status);
    for (name, value) in upstream.headers().iter() {
        // Skip framing headers; axum / hyper will set them based on the
        // forwarded body. Keeping them around can trigger duplicate or
        // mismatched values when the body is re-encoded.
        let lname = name.as_str().to_ascii_lowercase();
        if lname == "transfer-encoding" || lname == "content-length" || lname == "connection" {
            continue;
        }
        if let Ok(header_value) = axum::http::HeaderValue::from_bytes(value.as_bytes()) {
            builder = builder.header(name.as_str(), header_value);
        }
    }
    let bytes = match upstream.bytes().await {
        Ok(b) => b,
        Err(e) => {
            warn!(target = %target_url, error = %e, "Failed to read upstream body");
            return error_response(
                StatusCode::BAD_GATEWAY,
                &format!("Failed to read upstream body: {e}"),
            );
        }
    };
    builder.body(Body::from(bytes)).unwrap_or_else(|_| {
        Response::builder()
            .status(StatusCode::INTERNAL_SERVER_ERROR)
            .body(Body::empty())
            .unwrap()
    })
}

async fn invoke_lambda(
    state: &ProxyState,
    method: &Method,
    uri: &Uri,
    integration_uri: &str,
    event: serde_json::Value,
) -> Response<Body> {
    let lambda_handler = match &state.lambda {
        Some(h) => Arc::clone(h),
        None => {
            warn!("Lambda service not registered — cannot invoke function");
            return error_response(
                StatusCode::SERVICE_UNAVAILABLE,
                "Lambda service not registered",
            );
        }
    };

    let function_name = extract_function_name(integration_uri);
    let ctx = RequestContext {
        account_id: state.default_account_id.clone(),
        region: state.default_region.clone(),
        partition: awsim_core::DEFAULT_PARTITION.to_string(),
        service: "lambda".to_string(),
        access_key: None,
        request_id: uuid::Uuid::new_v4().to_string(),
        method: method.to_string(),
        uri: uri.to_string(),
        event_bus: None,
        source_ip: None,
        is_secure: false,
        internal_bypass: false,
    };
    let invoke_input = json!({
        "FunctionName": function_name,
        "InvocationType": "RequestResponse",
        "Payload": event,
    });

    match lambda_handler.handle("Invoke", invoke_input, &ctx).await {
        Ok(result) => lambda_response_to_http(result),
        Err(e) => {
            warn!(
                function = %function_name,
                error = %e.message,
                "Lambda invocation failed"
            );
            error_response(
                StatusCode::INTERNAL_SERVER_ERROR,
                &format!("Lambda invocation error: {}", e.message),
            )
        }
    }
}

/// Convert a Lambda Invoke result into an HTTP response.
///
/// Lambda v2 payload format response:
/// ```json
/// {
///   "statusCode": 200,
///   "headers": { "content-type": "application/json" },
///   "body": "{...}",
///   "isBase64Encoded": false
/// }
/// ```
fn lambda_response_to_http(result: serde_json::Value) -> Response<Body> {
    // If the result has a "statusCode" field, treat it as a proxy response.
    if let Some(status_code) = result.get("statusCode").and_then(|v| v.as_u64()) {
        let status =
            StatusCode::from_u16(status_code as u16).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);

        let mut builder = Response::builder().status(status);

        // Apply response headers from Lambda.
        if let Some(resp_headers) = result.get("headers").and_then(|v| v.as_object()) {
            for (key, value) in resp_headers {
                if let Some(v) = value.as_str()
                    && let Ok(header_value) = v.parse::<axum::http::HeaderValue>()
                {
                    builder = builder.header(key.as_str(), header_value);
                }
            }
        }

        let is_base64 = result
            .get("isBase64Encoded")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);

        let body_bytes: Bytes = match result.get("body") {
            None => Bytes::new(),
            Some(serde_json::Value::Null) => Bytes::new(),
            Some(serde_json::Value::String(s)) => {
                if is_base64 {
                    use base64::Engine;
                    base64::engine::general_purpose::STANDARD
                        .decode(s)
                        .unwrap_or_else(|_| s.as_bytes().to_vec())
                        .into()
                } else {
                    Bytes::from(s.clone())
                }
            }
            Some(other) => Bytes::from(other.to_string()),
        };

        builder.body(Body::from(body_bytes)).unwrap_or_else(|_| {
            Response::builder()
                .status(StatusCode::INTERNAL_SERVER_ERROR)
                .body(Body::empty())
                .unwrap()
        })
    } else {
        // Raw result — serialize as JSON.
        let body = serde_json::to_vec(&result).unwrap_or_default();
        Response::builder()
            .status(StatusCode::OK)
            .header("content-type", "application/json")
            .body(Body::from(body))
            .unwrap()
    }
}

/// Extract a Lambda function name from an ARN or plain name.
///
/// Handles:
/// - Full ARN: `arn:aws:lambda:us-east-1:000000000000:function:my-func`
/// - Plain name: `my-func`
fn extract_function_name(uri: &str) -> String {
    if uri.starts_with("arn:aws:lambda:") {
        // ARN format: arn:aws:lambda:{region}:{account}:function:{name}
        uri.split(':').next_back().unwrap_or(uri).to_string()
    } else {
        uri.to_string()
    }
}

fn error_response(status: StatusCode, message: &str) -> Response<Body> {
    let body = serde_json::to_vec(&json!({
        "message": message,
    }))
    .unwrap_or_default();

    Response::builder()
        .status(status)
        .header("content-type", "application/json")
        .body(Body::from(body))
        .unwrap()
}