a3s-gateway 0.2.5

A3S Gateway - AI-native API gateway with reverse proxy, routing, and agent orchestration
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
//! gRPC proxy — HTTP/2 (h2c) request forwarding
//!
//! Forwards gRPC requests to upstream backends using HTTP/2 cleartext (h2c).
//! Supports unary, server-streaming, client-streaming, and bidirectional RPCs.

#![allow(dead_code)]

use crate::error::{GatewayError, Result};
use crate::service::Backend;
use bytes::Bytes;
use std::sync::Arc;
use std::time::Duration;

/// gRPC content type prefix
const GRPC_CONTENT_TYPE: &str = "application/grpc";

/// gRPC proxy — forwards gRPC requests over HTTP/2
pub struct GrpcProxy {
    client: reqwest::Client,
    timeout: Duration,
}

impl GrpcProxy {
    /// Create a new gRPC proxy with default settings
    pub fn new() -> Self {
        Self::with_timeout(Duration::from_secs(60))
    }

    /// Create with custom timeout
    pub fn with_timeout(timeout: Duration) -> Self {
        let client = reqwest::Client::builder()
            .timeout(timeout)
            .http2_prior_knowledge()
            .pool_max_idle_per_host(50)
            .build()
            .unwrap_or_default();

        Self { client, timeout }
    }

    /// Forward a gRPC request to the selected backend
    pub async fn forward(
        &self,
        backend: &Arc<Backend>,
        method: &http::Method,
        uri: &http::Uri,
        headers: &http::HeaderMap,
        body: Bytes,
    ) -> Result<GrpcResponse> {
        backend.inc_connections();
        let result = self.do_forward(backend, method, uri, headers, body).await;
        backend.dec_connections();
        result
    }

    async fn do_forward(
        &self,
        backend: &Arc<Backend>,
        _method: &http::Method,
        uri: &http::Uri,
        headers: &http::HeaderMap,
        body: Bytes,
    ) -> Result<GrpcResponse> {
        // Build upstream URL: h2c://host:port/service/method
        let backend_url = backend.url.trim_end_matches('/');
        let scheme = if backend_url.starts_with("h2c://") {
            "http"
        } else if backend_url.starts_with("https://") {
            "https"
        } else {
            "http"
        };

        let host = extract_grpc_host(backend_url);
        let path = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("/");
        let upstream_url = format!("{}://{}{}", scheme, host, path);

        // gRPC always uses POST
        let mut req_builder = self.client.post(&upstream_url);

        // Forward headers, preserving gRPC-specific ones
        for (key, value) in headers.iter() {
            let name = key.as_str();
            if !is_grpc_hop_by_hop(name) {
                req_builder = req_builder.header(key.clone(), value.clone());
            }
        }

        // Ensure content-type is set
        req_builder = req_builder.header("content-type", GRPC_CONTENT_TYPE);

        // Forward body
        req_builder = req_builder.body(body);

        let response = req_builder.send().await.map_err(|e| {
            if e.is_timeout() {
                GatewayError::UpstreamTimeout(self.timeout.as_millis() as u64)
            } else if e.is_connect() {
                GatewayError::ServiceUnavailable(format!(
                    "Cannot connect to gRPC backend {}: {}",
                    backend.url, e
                ))
            } else {
                GatewayError::Http(e)
            }
        })?;

        let status = response.status();
        let resp_headers = response.headers().clone();

        // Extract grpc-status from headers (trailers in HTTP/2)
        let grpc_status = resp_headers
            .get("grpc-status")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<i32>().ok())
            .unwrap_or(-1);

        let grpc_message = resp_headers
            .get("grpc-message")
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string());

        let resp_body = response.bytes().await.map_err(GatewayError::Http)?;

        Ok(GrpcResponse {
            http_status: status,
            headers: resp_headers,
            body: resp_body,
            grpc_status,
            grpc_message,
        })
    }

    /// Get the timeout
    pub fn timeout(&self) -> Duration {
        self.timeout
    }
}

impl Default for GrpcProxy {
    fn default() -> Self {
        Self::new()
    }
}

/// Response from a gRPC upstream
pub struct GrpcResponse {
    /// HTTP status code
    pub http_status: reqwest::StatusCode,
    /// Response headers
    pub headers: reqwest::header::HeaderMap,
    /// Response body (protobuf-encoded)
    pub body: Bytes,
    /// gRPC status code (0 = OK)
    pub grpc_status: i32,
    /// gRPC status message
    pub grpc_message: Option<String>,
}

impl GrpcResponse {
    /// Check if the gRPC call succeeded
    pub fn is_ok(&self) -> bool {
        self.grpc_status == 0
    }
}

/// Check if a request looks like a gRPC request
pub fn is_grpc_request(headers: &http::HeaderMap) -> bool {
    headers
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .map(|ct| ct.starts_with(GRPC_CONTENT_TYPE))
        .unwrap_or(false)
}

/// Extract host:port from a gRPC backend URL
fn extract_grpc_host(url: &str) -> &str {
    if let Some(rest) = url.strip_prefix("h2c://") {
        rest.trim_end_matches('/')
    } else if let Some(rest) = url.strip_prefix("http://") {
        rest.trim_end_matches('/')
    } else if let Some(rest) = url.strip_prefix("https://") {
        rest.trim_end_matches('/')
    } else {
        url.trim_end_matches('/')
    }
}

/// Headers that should not be forwarded in gRPC proxying
fn is_grpc_hop_by_hop(name: &str) -> bool {
    matches!(
        name.to_lowercase().as_str(),
        "connection" | "keep-alive" | "transfer-encoding" | "upgrade"
    )
}

/// Standard gRPC status codes
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum GrpcStatus {
    Ok = 0,
    Cancelled = 1,
    Unknown = 2,
    InvalidArgument = 3,
    DeadlineExceeded = 4,
    NotFound = 5,
    AlreadyExists = 6,
    PermissionDenied = 7,
    ResourceExhausted = 8,
    FailedPrecondition = 9,
    Aborted = 10,
    OutOfRange = 11,
    Unimplemented = 12,
    Internal = 13,
    Unavailable = 14,
    DataLoss = 15,
    Unauthenticated = 16,
}

impl GrpcStatus {
    /// Parse from integer code
    pub fn from_code(code: i32) -> Option<Self> {
        match code {
            0 => Some(Self::Ok),
            1 => Some(Self::Cancelled),
            2 => Some(Self::Unknown),
            3 => Some(Self::InvalidArgument),
            4 => Some(Self::DeadlineExceeded),
            5 => Some(Self::NotFound),
            6 => Some(Self::AlreadyExists),
            7 => Some(Self::PermissionDenied),
            8 => Some(Self::ResourceExhausted),
            9 => Some(Self::FailedPrecondition),
            10 => Some(Self::Aborted),
            11 => Some(Self::OutOfRange),
            12 => Some(Self::Unimplemented),
            13 => Some(Self::Internal),
            14 => Some(Self::Unavailable),
            15 => Some(Self::DataLoss),
            16 => Some(Self::Unauthenticated),
            _ => None,
        }
    }

    /// Get the status name
    pub fn name(&self) -> &'static str {
        match self {
            Self::Ok => "OK",
            Self::Cancelled => "CANCELLED",
            Self::Unknown => "UNKNOWN",
            Self::InvalidArgument => "INVALID_ARGUMENT",
            Self::DeadlineExceeded => "DEADLINE_EXCEEDED",
            Self::NotFound => "NOT_FOUND",
            Self::AlreadyExists => "ALREADY_EXISTS",
            Self::PermissionDenied => "PERMISSION_DENIED",
            Self::ResourceExhausted => "RESOURCE_EXHAUSTED",
            Self::FailedPrecondition => "FAILED_PRECONDITION",
            Self::Aborted => "ABORTED",
            Self::OutOfRange => "OUT_OF_RANGE",
            Self::Unimplemented => "UNIMPLEMENTED",
            Self::Internal => "INTERNAL",
            Self::Unavailable => "UNAVAILABLE",
            Self::DataLoss => "DATA_LOSS",
            Self::Unauthenticated => "UNAUTHENTICATED",
        }
    }
}

impl std::fmt::Display for GrpcStatus {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ({})", self.name(), *self as i32)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // --- GrpcProxy construction ---

    #[test]
    fn test_grpc_proxy_default() {
        let proxy = GrpcProxy::default();
        assert_eq!(proxy.timeout(), Duration::from_secs(60));
    }

    #[test]
    fn test_grpc_proxy_custom_timeout() {
        let proxy = GrpcProxy::with_timeout(Duration::from_secs(120));
        assert_eq!(proxy.timeout(), Duration::from_secs(120));
    }

    // --- is_grpc_request ---

    #[test]
    fn test_is_grpc_request_true() {
        let mut headers = http::HeaderMap::new();
        headers.insert("content-type", "application/grpc".parse().unwrap());
        assert!(is_grpc_request(&headers));
    }

    #[test]
    fn test_is_grpc_request_with_proto() {
        let mut headers = http::HeaderMap::new();
        headers.insert("content-type", "application/grpc+proto".parse().unwrap());
        assert!(is_grpc_request(&headers));
    }

    #[test]
    fn test_is_grpc_request_false() {
        let mut headers = http::HeaderMap::new();
        headers.insert("content-type", "application/json".parse().unwrap());
        assert!(!is_grpc_request(&headers));
    }

    #[test]
    fn test_is_grpc_request_no_content_type() {
        let headers = http::HeaderMap::new();
        assert!(!is_grpc_request(&headers));
    }

    // --- extract_grpc_host ---

    #[test]
    fn test_extract_grpc_host_h2c() {
        assert_eq!(
            extract_grpc_host("h2c://127.0.0.1:50051"),
            "127.0.0.1:50051"
        );
    }

    #[test]
    fn test_extract_grpc_host_http() {
        assert_eq!(
            extract_grpc_host("http://grpc.local:50051"),
            "grpc.local:50051"
        );
    }

    #[test]
    fn test_extract_grpc_host_https() {
        assert_eq!(
            extract_grpc_host("https://grpc.local:443"),
            "grpc.local:443"
        );
    }

    #[test]
    fn test_extract_grpc_host_bare() {
        assert_eq!(extract_grpc_host("127.0.0.1:50051"), "127.0.0.1:50051");
    }

    #[test]
    fn test_extract_grpc_host_trailing_slash() {
        assert_eq!(
            extract_grpc_host("h2c://127.0.0.1:50051/"),
            "127.0.0.1:50051"
        );
    }

    // --- is_grpc_hop_by_hop ---

    #[test]
    fn test_grpc_hop_by_hop() {
        assert!(is_grpc_hop_by_hop("connection"));
        assert!(is_grpc_hop_by_hop("Connection"));
        assert!(is_grpc_hop_by_hop("transfer-encoding"));
        assert!(is_grpc_hop_by_hop("upgrade"));
        assert!(!is_grpc_hop_by_hop("content-type"));
        assert!(!is_grpc_hop_by_hop("grpc-status"));
        assert!(!is_grpc_hop_by_hop("authorization"));
    }

    // --- GrpcStatus ---

    #[test]
    fn test_grpc_status_from_code() {
        assert_eq!(GrpcStatus::from_code(0), Some(GrpcStatus::Ok));
        assert_eq!(GrpcStatus::from_code(1), Some(GrpcStatus::Cancelled));
        assert_eq!(GrpcStatus::from_code(4), Some(GrpcStatus::DeadlineExceeded));
        assert_eq!(GrpcStatus::from_code(13), Some(GrpcStatus::Internal));
        assert_eq!(GrpcStatus::from_code(14), Some(GrpcStatus::Unavailable));
        assert_eq!(GrpcStatus::from_code(16), Some(GrpcStatus::Unauthenticated));
        assert_eq!(GrpcStatus::from_code(99), None);
        assert_eq!(GrpcStatus::from_code(-1), None);
    }

    #[test]
    fn test_grpc_status_name() {
        assert_eq!(GrpcStatus::Ok.name(), "OK");
        assert_eq!(GrpcStatus::NotFound.name(), "NOT_FOUND");
        assert_eq!(GrpcStatus::Internal.name(), "INTERNAL");
        assert_eq!(GrpcStatus::Unavailable.name(), "UNAVAILABLE");
    }

    #[test]
    fn test_grpc_status_display() {
        assert_eq!(GrpcStatus::Ok.to_string(), "OK (0)");
        assert_eq!(GrpcStatus::NotFound.to_string(), "NOT_FOUND (5)");
        assert_eq!(GrpcStatus::Internal.to_string(), "INTERNAL (13)");
    }

    #[test]
    fn test_grpc_status_all_codes() {
        for code in 0..=16 {
            let status = GrpcStatus::from_code(code);
            assert!(status.is_some(), "Code {} should be valid", code);
            assert_eq!(status.unwrap() as i32, code);
        }
    }

    // --- GrpcResponse ---

    #[test]
    fn test_grpc_response_is_ok() {
        let resp = GrpcResponse {
            http_status: reqwest::StatusCode::OK,
            headers: reqwest::header::HeaderMap::new(),
            body: Bytes::new(),
            grpc_status: 0,
            grpc_message: None,
        };
        assert!(resp.is_ok());
    }

    #[test]
    fn test_grpc_response_is_not_ok() {
        let resp = GrpcResponse {
            http_status: reqwest::StatusCode::OK,
            headers: reqwest::header::HeaderMap::new(),
            body: Bytes::new(),
            grpc_status: 13,
            grpc_message: Some("internal error".to_string()),
        };
        assert!(!resp.is_ok());
        assert_eq!(resp.grpc_message.as_deref(), Some("internal error"));
    }
}