vakya 0.1.0-alpha.1

Completion-native HTTP building blocks for the Karmaio runtime
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
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
use super::{
    BodyMode, Expectation, Persistence,
    bridge::{ReceiveEnd, SendBodyError, incoming, receive_body_configured, send_body},
    decode::BodyDecoder,
    encode::{BodyEncoder, BodyMetadata, prepare_response_head},
    exchange::{Direction, Exchange, Outcome},
    head::{HeadParser, RequestRole, ValidatedRequestHead},
    header_case::HeaderCaseMap,
};
use crate::{
    body::{Body, Incoming, SizeHint, TrailerHint},
    connection::ConnectionOutcome,
    error::{Error, ErrorKind},
    future::{Race, WorkBudget, cancellable_wait, race},
    io::{
        recv::{ReadStatus, RecvBuffer},
        send::write_all,
        transport::Receive,
    },
    server::{
        conn::http1::config::Config as ServerConfig,
        context::{self, Command, InformationalReceiver, RequestContext},
    },
    service::Service,
    upgrade::{self, PendingUpgrade, Upgraded},
};
use http::{
    HeaderValue, Method, Request, Response, Version,
    header::{CONNECTION, DATE},
};
use karmaio::{
    io::{AsyncWrite, IntoOwnedSplit},
    runtime::{CancellationSource, FutureExt},
};
use std::{
    cell::Cell,
    future::poll_fn,
    pin::pin,
    task::Poll,
    time::{SystemTime, UNIX_EPOCH},
};

pub(crate) async fn run<I, S, B, T>(
    io: I,
    service: S,
    strategy: T,
    config: ServerConfig,
    connection: crate::connection::ConnectionControl,
) -> Result<ConnectionOutcome<I::ReadHalf, I::WriteHalf>, Error>
where
    I: IntoOwnedSplit,
    T: Receive<I::ReadHalf>,
    S: Service<(Request<Incoming>, RequestContext), Response = Response<B>>,
    S::Error: std::error::Error + 'static,
    B: Body,
    B::Error: std::error::Error + 'static,
{
    let shutdown = karmaio::runtime::CancellationSource::new();

    connection
        .instrument(connection.drive(
            &shutdown,
            pin!(run_inner(io, service, strategy, config, &connection, &shutdown)),
        ))
        .await
}

async fn run_inner<I, S, B, T>(
    io: I,
    service: S,
    mut strategy: T,
    config: ServerConfig,
    connection: &crate::connection::ConnectionControl,
    shutdown: &karmaio::runtime::CancellationSource,
) -> Result<ConnectionOutcome<I::ReadHalf, I::WriteHalf>, Error>
where
    I: IntoOwnedSplit,
    T: Receive<I::ReadHalf>,
    S: Service<(Request<Incoming>, RequestContext), Response = Response<B>>,
    S::Error: std::error::Error + 'static,
    B: Body,
    B::Error: std::error::Error + 'static,
{
    let (mut reader, writer) = io.into_split();
    let mut writer = crate::io::deadline::Write {
        inner: writer,
        timeout: config.write_progress_timeout,
    };
    let mut buffer = RecvBuffer::new(config.preferred_read, config.max_retained)?;
    let mut parser = HeadParser::<RequestRole>::request(config.protocol.head)
        .with_preserved_header_case(config.protocol.preserve_header_case);
    let mut date = DateCache::default();
    let mut budget = WorkBudget::new();
    // Graceful idle-read cancellation must leave transport shutdown usable.
    let idle_read = CancellationSource::new();

    let mut exchanges = crate::trace::Exchanges::default();
    'connection: loop {
        budget.step().await;

        if connection.stopping() {
            break;
        }

        let deadline = crate::io::deadline::configured_after(config.head_timeout)?;

        let head = loop {
            if deadline.is_some_and(|deadline| std::time::Instant::now() >= deadline) {
                return Err(Error::new(ErrorKind::Timeout, "HTTP head deadline exceeded"));
            }

            match parser.head_len(buffer.bytes())? {
                Some(consumed) => {
                    let input = buffer.take_shared_prefix(consumed)?;
                    break parser.parse_shared(input)?.validate_received()?;
                }
                None => {
                    let mut read = pin!(
                        strategy
                            .read_until(&mut reader, buffer, Some(idle_read.token()), deadline)
                            .with_cancellation(shutdown.token())
                    );
                    let mut stopped = pin!(connection.stopped());

                    let (result, returned) = match race(stopped.as_mut(), read.as_mut()).await {
                        Race::First(()) => {
                            idle_read.cancel();
                            let (result, _) = read.await;
                            if let Err(error) = result
                                && !error.is_canceled()
                            {
                                return Err(error);
                            }
                            break 'connection;
                        }
                        Race::Second(result) => result,
                    };

                    buffer = returned;

                    if result? == ReadStatus::Eof {
                        if !buffer.bytes().is_empty() {
                            return Err(Error::new(
                                ErrorKind::InvalidMessage,
                                "connection ended inside a request head",
                            ));
                        }
                        break 'connection;
                    }
                }
            }
        };
        let span = exchanges.next();
        let (result, returned) = span
            .instrument(exchange(
                &mut reader,
                &mut writer,
                &mut strategy,
                buffer,
                head,
                &service,
                &config,
                &mut date,
                shutdown,
            ))
            .await;
        buffer = returned;
        if let Ok(result) = &result {
            span.outcome(match result.outcome {
                Outcome::Handoff(_) => "handoff",
                Outcome::Close => "close",
                Outcome::Reusable => "reusable",
                Outcome::Active => "active",
            });
        } else if let Err(error) = &result {
            span.failure(error.kind());
        }
        let ExchangeResult { outcome, upgrade } = result?;
        match outcome {
            Outcome::Handoff(kind) => {
                let upgraded = Upgraded::new(reader, writer.inner, buffer.take_all(), kind);
                return Ok(match upgrade.fulfill(upgraded) {
                    Ok(kind) => ConnectionOutcome::UpgradeClaimed(kind),
                    Err(upgraded) => ConnectionOutcome::Upgraded(upgraded),
                });
            }
            Outcome::Close => break,
            Outcome::Reusable => {}
            Outcome::Active => return Err(Error::new(ErrorKind::Internal, "unsettled server exchange")),
        }
    }
    writer.shutdown().with_cancellation(shutdown.token()).await?;
    Ok(ConnectionOutcome::Closed)
}

// The reader and writer are borrowed exclusively by their own retained futures.
// Neither a final head nor a completed response implies request-body completion.
struct ExchangeResult {
    outcome: Outcome,
    upgrade: PendingUpgrade,
}

#[allow(clippy::too_many_arguments)] // Explicit disjoint connection resources; no shared driver allocation.
async fn exchange<R, W, S, B, T>(
    reader: &mut R,
    writer: &mut W,
    strategy: &mut T,
    buffer: RecvBuffer,
    head: ValidatedRequestHead,
    service: &S,
    config: &ServerConfig,
    date: &mut DateCache,
    source: &CancellationSource,
) -> (Result<ExchangeResult, Error>, RecvBuffer)
where
    W: AsyncWrite,
    T: Receive<R>,
    S: Service<(Request<Incoming>, RequestContext), Response = Response<B>>,
    S::Error: std::error::Error + 'static,
    B: Body,
    B::Error: std::error::Error + 'static,
{
    let mut state = Exchange::new(&head, config.protocol.max_informational);
    let method = head.head.method.clone();
    let version = head.head.version;
    let (upgrade, upgrade_slot) = upgrade::channel();
    let (context, informationals) = context::channel(head.persistence == Persistence::Close, upgrade_slot);
    let close = informationals.close_flag();
    let (producer, body) = incoming(head.body);
    let body = body.with_drain_config(config.drain);
    let mut request = Request::new(body);
    #[cfg(feature = "tls")]
    if let Some(info) = &config.tls_info {
        request.extensions_mut().insert(info.clone());
    }
    if let Some(header_case) = head.head.header_case {
        request.extensions_mut().insert(header_case);
    }
    *request.method_mut() = head.head.method;
    *request.uri_mut() = head.head.target;
    *request.version_mut() = version;
    *request.headers_mut() = head.head.headers;
    let receive = async {
        match producer {
            Some(mut producer) => {
                if head.expectation == Expectation::Continue && version == Version::HTTP_11 {
                    match cancellable_wait(producer.demand(), Some(source.token())).await {
                        Some(Ok(())) => {}
                        Some(Err(_)) => return (ReceiveEnd::Abandoned, buffer),
                        None => {
                            return (
                                ReceiveEnd::fail(producer, Error::new(ErrorKind::Canceled, "request receive canceled")),
                                buffer,
                            );
                        }
                    }
                    match cancellable_wait(informationals.continue_permission(), Some(source.token())).await {
                        Some(Ok(())) => {}
                        Some(Err(error)) => return (ReceiveEnd::fail(producer, error), buffer),
                        None => {
                            return (
                                ReceiveEnd::fail(producer, Error::new(ErrorKind::Canceled, "Continue demand canceled")),
                                buffer,
                            );
                        }
                    }
                }
                receive_body_configured(
                    reader,
                    strategy,
                    buffer,
                    BodyDecoder::with_limits(head.body, config.protocol.decode),
                    producer,
                    Some(source.token()),
                    config.body_progress_timeout,
                )
                .await
            }
            None => (ReceiveEnd::Complete, buffer),
        }
    };
    let mut receive = pin!(receive);
    let mut received = None;
    let response = {
        let mut call = pin!(call_service(
            writer,
            service,
            request,
            context,
            &informationals,
            version,
            config,
            source
        ));
        match race(receive.as_mut(), call.as_mut()).await {
            Race::First((ReceiveEnd::Failed(error), buffer)) => {
                source.cancel();
                let _ = call.await;
                return (Err(error), buffer);
            }
            Race::First(result) => {
                received = Some(result);
                call.await
            }
            Race::Second(result) => result,
        }
    };
    let response = match response {
        Ok(response) => response,
        Err(error) => {
            source.cancel();
            let (_, buffer) = match received {
                Some(result) => result,
                None => receive.await,
            };
            return (Err(error), buffer);
        }
    };
    // Observe body abandonment caused by the completed service before committing
    // headers. Poll once without awaiting demand or delaying an early response.
    if received.is_none() {
        received = match poll_fn(|cx| Poll::Ready(receive.as_mut().poll(cx))).await {
            Poll::Ready(result) => Some(result),
            Poll::Pending => None,
        };
    }
    if let Some((ReceiveEnd::Failed(error), buffer)) = received {
        return (Err(error), buffer);
    }
    if matches!(received, Some((ReceiveEnd::Abandoned, _))) {
        close.set(true);
    }
    let sent = {
        let send = write_response(
            writer, response, &method, version, close, date, config, source, &mut state,
        );
        let mut send = pin!(send);
        if received.is_some() {
            send.await
        } else {
            match race(receive.as_mut(), send.as_mut()).await {
                Race::First((ReceiveEnd::Failed(error), buffer)) => {
                    source.cancel();
                    let _ = send.await;
                    return (Err(error), buffer);
                }
                Race::First(result) => {
                    received = Some(result);
                    send.await
                }
                Race::Second(result) => result,
            }
        }
    };
    if sent.is_err() {
        source.cancel();
    }
    let (end, buffer) = match received {
        Some(result) => result,
        None => receive.await,
    };
    match sent {
        Ok(_) => {}
        Err(error) => return (Err(error), buffer),
    };
    match end {
        ReceiveEnd::Failed(error) => return (Err(error), buffer),
        ReceiveEnd::Abandoned => {
            return (
                Ok(ExchangeResult {
                    outcome: Outcome::Close,
                    upgrade,
                }),
                buffer,
            );
        }
        ReceiveEnd::Complete => {}
    }
    let result = (|| {
        state.settle(Direction::Request, true)?;
        state.settle(Direction::Response, true)?;
        if close.get() {
            state.close_after_exchange();
        }
        Ok(ExchangeResult {
            outcome: state.outcome(),
            upgrade,
        })
    })();
    (result, buffer)
}

#[allow(clippy::too_many_arguments)] // Service and the sole head writer progress in the same scope.
async fn call_service<W, S, B>(
    writer: &mut W,
    service: &S,
    request: Request<Incoming>,
    context: RequestContext,
    informationals: &InformationalReceiver,
    version: Version,
    config: &ServerConfig,
    source: &CancellationSource,
) -> Result<Response<B>, Error>
where
    W: AsyncWrite,
    S: Service<(Request<Incoming>, RequestContext), Response = Response<B>>,
    S::Error: std::error::Error + 'static,
{
    let mut call = pin!(cancellable_wait(service.call((request, context)), Some(source.token())));
    let mut heads = pin!(write_informationals(writer, informationals, version, config, source));
    match race(call.as_mut(), heads.as_mut()).await {
        Race::First(result) => {
            informationals.select_final();
            if !matches!(result, Some(Ok(_))) {
                source.cancel();
            }
            // A selected final response cannot drop an in-flight informational
            // write, even if its application waiter has already disappeared.
            let written = heads.await;
            let response = match result {
                Some(Ok(response)) => response,
                Some(Err(error)) => return Err(Error::with_source(ErrorKind::Service, "HTTP service failed", error)),
                None => return Err(Error::new(ErrorKind::Canceled, "HTTP service canceled")),
            };
            written?;
            Ok(response)
        }
        Race::Second(result) => {
            source.cancel();
            let _ = call.await;
            result?;
            Err(Error::new(ErrorKind::Canceled, "informational writer stopped"))
        }
    }
}

async fn write_informationals<W: AsyncWrite>(
    writer: &mut W,
    receiver: &InformationalReceiver,
    version: Version,
    config: &ServerConfig,
    source: &CancellationSource,
) -> Result<(), Error> {
    use karmaio::runtime::FutureExt;
    let mut count = 0;
    loop {
        let command = match cancellable_wait(receiver.next(), Some(source.token())).await {
            Some(Some(command)) => command,
            Some(None) => return Ok(()),
            None => return Err(Error::new(ErrorKind::Canceled, "informational writer canceled")),
        };
        let automatic = matches!(command, Command::Continue);
        let response = match command {
            Command::Continue => Response::builder()
                .status(100)
                .body(())
                .expect("valid automatic Continue"),
            Command::Application(response) => response,
        };
        let head = (|| {
            if version != Version::HTTP_11 {
                return Err(Error::new(
                    ErrorKind::Unsupported,
                    "informational responses require HTTP/1.1",
                ));
            }
            if count == config.protocol.max_informational {
                return Err(Error::new(ErrorKind::Limit, "too many informational responses"));
            }
            let (parts, ()) = response.into_parts();
            let original_case = parts.extensions.get::<HeaderCaseMap>();
            let (head, _) = prepare_response_head(
                parts.status,
                parts.version,
                parts.headers,
                original_case,
                BodyMetadata {
                    size: SizeHint::with_exact(0),
                    trailers: TrailerHint::None,
                },
                &Method::GET,
                config.protocol.encode,
            )?;
            Ok(head)
        })();
        let head = match head {
            Ok(head) => head,
            Err(error) if !automatic => {
                receiver.complete(Err(error));
                continue;
            }
            Err(error) => return Err(error),
        };
        let (result, _) = write_all(writer, head.bytes, Some(source.token())).await.into_parts();
        let result = match result {
            Ok(_) => writer
                .flush()
                .with_cancellation(source.token())
                .await
                .map_err(Error::from),
            Err(error) => Err(error.into()),
        };
        if let Err(error) = result {
            if !automatic {
                let (driver, observer) = error.split();
                receiver.complete(Err(observer));
                return Err(driver);
            }
            return Err(error);
        }
        count += 1;
        if automatic {
            receiver.continued();
        } else {
            receiver.complete(Ok(()));
        }
    }
}

#[allow(clippy::too_many_arguments)] // Scoped writer state, all borrowed rather than cloned or shared.
async fn write_response<W: AsyncWrite, B: Body>(
    writer: &mut W,
    response: Response<B>,
    method: &Method,
    request_version: Version,
    close: &Cell<bool>,
    date: &mut DateCache,
    config: &ServerConfig,
    source: &CancellationSource,
    state: &mut Exchange,
) -> Result<Persistence, Error>
where
    B::Error: std::error::Error + 'static,
{
    let (mut parts, mut body) = response.into_parts();
    if parts.status.is_informational() && parts.status != http::StatusCode::SWITCHING_PROTOCOLS {
        return Err(Error::new(
            ErrorKind::Unsupported,
            "informational responses and transport handoff require their dedicated APIs",
        ));
    }
    if close.get() {
        parts.headers.append(CONNECTION, HeaderValue::from_static("close"));
    }
    if config.auto_date && !parts.headers.contains_key(DATE) {
        parts.headers.insert(DATE, date.value());
    }
    let version = if request_version == Version::HTTP_10 {
        Version::HTTP_10
    } else {
        parts.version
    };
    let metadata = BodyMetadata {
        size: body.size_hint(),
        trailers: body.trailer_hint(),
    };
    let original_case = parts.extensions.get::<HeaderCaseMap>();
    let (head, validated) = prepare_response_head(
        parts.status,
        version,
        parts.headers,
        original_case,
        metadata,
        method,
        config.protocol.encode,
    )?;
    if head.upgrade.is_some() && close.get() {
        return Err(Error::new(
            ErrorKind::Upgrade,
            "closing exchange cannot transfer transport ownership",
        ));
    }
    state.sent_response(&validated)?;
    crate::trace::response_head(validated.head.status.as_u16());
    drop(validated); // The encoded bytes and exchange decisions now own what is needed.
    let mut encoder = BodyEncoder::new(head.mode, metadata, config.protocol.encode)?;
    let (result, _) = write_all(writer, head.bytes, Some(source.token())).await.into_parts();
    result?;
    // HEAD and 304 metadata describes a representation, not a producer to poll.
    if head.mode != BodyMode::None {
        send_body(writer, &mut body, &mut encoder, Some(source.token()))
            .await
            .map_err(|error| match error {
                SendBodyError::Body(error) => Error::with_source(ErrorKind::Body, "response body failed", error),
                SendBodyError::Encode(error) => error.into(),
                SendBodyError::Io(error) => error.into(),
                SendBodyError::Canceled => Error::new(ErrorKind::Canceled, "response production canceled"),
            })?;
    }
    // Custom buffered transports may need an explicit flush before the exchange
    // is considered sent. Apply the scope before this operation is first polled.
    use karmaio::runtime::FutureExt;
    writer.flush().with_cancellation(source.token()).await?;
    Ok(head.persistence)
}

#[derive(Default)]
struct DateCache(Option<(u64, HeaderValue)>);

impl DateCache {
    fn value(&mut self) -> HeaderValue {
        let now = SystemTime::now();
        let second = now.duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
        if let Some((cached, value)) = &self.0
            && *cached == second
        {
            return value.clone();
        }
        let value = HeaderValue::from_str(&httpdate::fmt_http_date(now)).expect("HTTP date is a valid header value");
        self.0 = Some((second, value.clone()));
        value
    }
}