tus-protocol 0.0.1

Rust implementation of the TUS resumable upload protocol
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
use std::collections::HashMap;

mod body;

use crate::config::Config;
use crate::error::{Error, Result};
use crate::hooks::{
    HookContext, HookEvent, HookExecutor, HookRequestInfo, execute_post_best_effort,
};
use crate::protocol::{Headers, RequestBody};
use crate::state::UploadState;
use crate::state::{StateStore, WriteMode};
use crate::storage::{AppendRequest, ChunkStream, Storage, StorageHandle};

use super::{PreHookGate, UploadCompletion, ensure_active};

/// Request fields needed before a PATCH body is accepted.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ReceiveRequest {
    /// Client-supplied Upload-Offset.
    pub client_offset: u64,
    /// Optional Upload-Length used to resolve deferred length uploads.
    pub upload_length: Option<u64>,
}

/// Result of projecting a receive body against the current upload state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct ReceiveProjection {
    /// Offset expected after the body is committed.
    pub projected_offset: u64,
    /// Whether the receive operation completes the upload.
    pub completes_upload: bool,
}

/// Receive body path selected by the protocol request.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ReceiveBodyKind {
    /// Body bytes supplied by a PATCH request.
    Patch,
    /// Initial body bytes supplied by Creation-With-Upload.
    CreationWithUpload,
}

/// Body bytes and lifecycle projection that have passed receive gates.
#[derive(Debug)]
struct PreparedReceiveBody {
    pub(crate) data: ChunkStream,
    pub(crate) projection: ReceiveProjection,
    pub(crate) response_headers: HashMap<String, String>,
    pub(crate) deferred_error: body::DeferredBodyError,
}

/// Result of accepting bytes for an existing upload.
#[derive(Debug)]
pub(crate) struct ReceiveOutcome {
    pub(crate) response_headers: HashMap<String, String>,
}

/// Result of accepting initial bytes during Creation-With-Upload.
#[derive(Debug)]
pub(crate) struct CreationWithUploadOutcome {
    pub(crate) state: UploadState,
    pub(crate) response_headers: HashMap<String, String>,
}

/// Internal Byte receive module for PATCH and Creation-With-Upload bytes.
pub(crate) struct ByteReceiver<'a, S, St, H>
where
    S: Storage + ?Sized,
    St: StateStore + ?Sized,
    H: HookExecutor + ?Sized,
{
    storage: &'a S,
    state_store: &'a St,
    hooks: &'a H,
    config: &'a Config,
    request_info: &'a HookRequestInfo,
}

impl<'a, S, St, H> ByteReceiver<'a, S, St, H>
where
    S: Storage + ?Sized,
    St: StateStore + ?Sized,
    H: HookExecutor + ?Sized,
{
    pub(crate) fn new(
        storage: &'a S,
        state_store: &'a St,
        hooks: &'a H,
        config: &'a Config,
        request_info: &'a HookRequestInfo,
    ) -> Self {
        Self {
            storage,
            state_store,
            hooks,
            config,
            request_info,
        }
    }

    pub(crate) async fn receive_patch(
        &self,
        headers: &Headers,
        state: &mut UploadState,
        request: ReceiveRequest,
        request_body: RequestBody,
    ) -> Result<ReceiveOutcome> {
        prepare_receive(self.config, state, request)?;
        let prepared = prepare_receive_body(
            self.config,
            self.hooks,
            self.request_info,
            headers,
            state,
            request_body,
            ReceiveBodyKind::Patch,
        )
        .await?;
        let response_headers = prepared.response_headers.clone();
        let completes_upload = prepared.projection.completes_upload;

        commit_receive_body(self.storage, self.state_store, state, prepared).await?;
        self.run_post_event(HookEvent::PostReceive, state).await;
        if completes_upload {
            // The completing bytes are already durable; a PreFinish rejection
            // fails this response and skips PostFinish, but cannot undo the
            // stored upload.
            self.completion().finish_gate(state.clone()).await?;
            self.completion().after_commit(state).await;
        }

        Ok(ReceiveOutcome { response_headers })
    }

    pub(crate) async fn receive_creation_with_upload(
        &self,
        headers: &Headers,
        state: UploadState,
        request_body: RequestBody,
    ) -> Result<CreationWithUploadOutcome> {
        let pre_create = PreHookGate::Create
            .run(self.hooks, self.request_info, state)
            .await?;
        let mut state = pre_create.state;
        let mut response_headers = pre_create.response_headers;
        let prepared = prepare_receive_body(
            self.config,
            self.hooks,
            self.request_info,
            headers,
            &mut state,
            request_body,
            ReceiveBodyKind::CreationWithUpload,
        )
        .await?;
        response_headers.extend(prepared.response_headers.clone());

        let handle = self.storage.create(state.id()).await?;
        state.set_storage_handle(handle);
        if let Err(err) = self.state_store.set(&state, WriteMode::CreateNew).await {
            // Best-effort rollback of the storage object only: the `CreateNew`
            // set never wrote our state record, so we must not delete the state
            // for this ID. If the failure was `AlreadyExists`, that record
            // belongs to a pre-existing upload and deleting it would corrupt a
            // different upload. Mirrors the non-CwU creation path in `post.rs`.
            self.rollback_storage(&state).await;
            return Err(err);
        }

        if let Err(err) =
            commit_receive_body(self.storage, self.state_store, &mut state, prepared).await
        {
            self.rollback_creation(&state).await;
            return Err(err);
        }

        // The upload resource is new, so a PreFinish rejection can still roll
        // the whole creation back instead of leaving a completed upload.
        if state.is_complete()
            && let Err(err) = self.completion().finish_gate(state.clone()).await
        {
            self.rollback_creation(&state).await;
            return Err(err);
        }

        self.run_post_event(HookEvent::PostCreate, &state).await;
        self.run_post_event(HookEvent::PostReceive, &state).await;
        self.completion().after_commit_if_complete(&state).await;

        Ok(CreationWithUploadOutcome {
            state,
            response_headers,
        })
    }

    /// Rolls back only the storage object created for a new upload.
    ///
    /// Used when the `CreateNew` state write itself failed: we own the storage
    /// object we just created, but no state record of ours was persisted, so
    /// the state record for this ID (if any) belongs to a different upload and
    /// must be left untouched.
    async fn rollback_storage(&self, state: &UploadState) {
        if let Some(handle) = state.storage_handle()
            && let Err(error) = self.storage.delete(&handle).await
        {
            tracing::warn!(
                upload_id = %state.id(),
                %error,
                "failed to roll back storage object while undoing upload creation"
            );
        }
    }

    /// Rolls back both the storage object and the state record for a new upload.
    ///
    /// Used after the `CreateNew` state write has succeeded, so this upload owns
    /// its state record and may delete it.
    async fn rollback_creation(&self, state: &UploadState) {
        self.rollback_storage(state).await;
        if let Err(error) = self.state_store.delete(state.id()).await {
            tracing::warn!(
                upload_id = %state.id(),
                %error,
                "failed to roll back state record while undoing upload creation"
            );
        }
    }

    fn completion(&self) -> UploadCompletion<'_, H> {
        UploadCompletion::new(self.hooks, self.request_info)
    }

    async fn run_post_event(&self, event: HookEvent, state: &UploadState) {
        let ctx = HookContext::new(event, state.clone(), self.request_info.clone());
        execute_post_best_effort(self.hooks, &ctx).await;
    }
}

/// Validates PATCH preflight state and applies deferred Upload-Length.
fn prepare_receive(
    config: &Config,
    state: &mut UploadState,
    request: ReceiveRequest,
) -> Result<()> {
    ensure_active(state)?;

    if state.is_final() {
        return Err(Error::FinalUploadModificationForbidden(
            state.id().to_string(),
        ));
    }

    // Completion is checked before the offset so a client retrying a finished
    // upload at any offset (typically `0`) gets the accurate
    // `CompletedUploadModificationForbidden` rather than an `OffsetMismatch`
    // against the full length.
    if state.is_complete() {
        return Err(Error::CompletedUploadModificationForbidden(
            state.id().to_string(),
        ));
    }

    if request.client_offset != state.offset() {
        return Err(Error::OffsetMismatch {
            expected: state.offset(),
            actual: request.client_offset,
        });
    }

    if let Some(length) = request.upload_length {
        if let Some(existing) = state.length() {
            if length != existing {
                return Err(Error::InvalidHeader {
                    header: "Upload-Length",
                    message: format!(
                        "cannot change Upload-Length after it is set (existing: {existing}, provided: {length})"
                    ),
                });
            }
        } else {
            if let Some(max_size) = config.max_size()
                && length > max_size
            {
                return Err(Error::SizeExceeded {
                    size: length,
                    max: max_size,
                });
            }
            state.set_length(length);
        }
    }

    Ok(())
}

/// Computes the maximum body bytes this receive may accept before buffering.
#[must_use]
fn receive_body_size_limit(config: &Config, state: &UploadState) -> Option<u64> {
    [
        config.max_chunk_size(),
        config
            .max_size()
            .map(|max_size| max_size.saturating_sub(state.offset())),
        state
            .length()
            .map(|length| length.saturating_sub(state.offset())),
    ]
    .into_iter()
    .flatten()
    .min()
}

/// Validates body length and computes the resulting offset.
fn validate_receive_body(
    config: &Config,
    state: &UploadState,
    body_len: u64,
) -> Result<ReceiveProjection> {
    if let Some(max_chunk) = config.max_chunk_size()
        && body_len > max_chunk
    {
        return Err(Error::SizeExceeded {
            size: body_len,
            max: max_chunk,
        });
    }

    let projected_offset = state.offset().saturating_add(body_len);

    if let Some(max_size) = config.max_size()
        && projected_offset > max_size
    {
        return Err(Error::SizeExceeded {
            size: projected_offset,
            max: max_size,
        });
    }

    if let Some(length) = state.length()
        && projected_offset > length
    {
        return Err(Error::SizeExceeded {
            size: projected_offset,
            max: length,
        });
    }

    Ok(ReceiveProjection {
        projected_offset,
        completes_upload: state
            .length()
            .is_some_and(|length| projected_offset == length),
    })
}

/// Runs receive hook gates, Body intake, and completion projection for accepted bytes.
async fn prepare_receive_body<H>(
    config: &Config,
    hooks: &H,
    request_info: &HookRequestInfo,
    headers: &Headers,
    state: &mut UploadState,
    request_body: RequestBody,
    kind: ReceiveBodyKind,
) -> Result<PreparedReceiveBody>
where
    H: HookExecutor + ?Sized,
{
    let pre_receive = PreHookGate::Receive
        .run(hooks, request_info, state.clone())
        .await?;
    let response_headers = pre_receive.response_headers;
    *state = pre_receive.state;

    let collected = collect_receive_body(config, headers, state, request_body, kind).await?;
    if matches!(kind, ReceiveBodyKind::CreationWithUpload) {
        debug_assert!(
            collected.supplied,
            "creation body collection should only run for supplied bodies"
        );
    }

    let projection = validate_body_for_receive(config, state, collected.size, kind)?;

    Ok(PreparedReceiveBody {
        data: collected.data,
        projection,
        response_headers,
        deferred_error: collected.deferred_error,
    })
}

/// Commits accepted receive bytes to storage and persists the resulting upload state.
async fn commit_receive_body<S, St>(
    storage: &S,
    state_store: &St,
    state: &mut UploadState,
    prepared: PreparedReceiveBody,
) -> Result<()>
where
    S: Storage + ?Sized,
    St: StateStore + ?Sized,
{
    let deferred_error = prepared.deferred_error.clone();
    let handle = match storage
        .append(AppendRequest::new(
            state.require_storage_handle()?,
            state.offset(),
            prepared.data,
            prepared.projection.completes_upload,
        ))
        .await
    {
        Ok(handle) => handle,
        Err(error) => return Err(deferred_error.take().unwrap_or(error)),
    };
    if let Some(error) = deferred_error.take() {
        return Err(error);
    }

    apply_receive_commit(state, prepared.projection, handle);
    state_store.set(state, WriteMode::Update).await?;

    Ok(())
}

fn apply_receive_commit(
    state: &mut UploadState,
    projection: ReceiveProjection,
    handle: StorageHandle,
) {
    state.set_storage_handle(handle);
    state.set_offset(projection.projected_offset);
}

fn receive_body_limit(config: &Config, state: &UploadState, kind: ReceiveBodyKind) -> Option<u64> {
    match kind {
        ReceiveBodyKind::Patch => receive_body_size_limit(config, state),
        ReceiveBodyKind::CreationWithUpload => creation_with_upload_body_size_limit(config, state),
    }
}

async fn collect_receive_body(
    config: &Config,
    headers: &Headers,
    state: &UploadState,
    request_body: RequestBody,
    kind: ReceiveBodyKind,
) -> Result<body::IntakeBody> {
    body::prepare(
        config,
        headers,
        receive_body_limit(config, state, kind),
        request_body,
    )
    .await
}

fn validate_body_for_receive(
    config: &Config,
    state: &UploadState,
    body_len: u64,
    kind: ReceiveBodyKind,
) -> Result<ReceiveProjection> {
    match kind {
        ReceiveBodyKind::Patch => validate_receive_body(config, state, body_len),
        ReceiveBodyKind::CreationWithUpload => {
            validate_creation_with_upload_body(config, state, body_len)
        }
    }
}

fn creation_with_upload_body_size_limit(config: &Config, state: &UploadState) -> Option<u64> {
    // `max_chunk_size` is a per-PATCH cap and is deliberately not applied here:
    // the initial data of a Creation-With-Upload request travels on the POST,
    // not a PATCH. The body is still bounded by `max_size` and the declared
    // `Upload-Length` below.
    [
        config
            .max_size()
            .map(|max_size| max_size.saturating_sub(state.offset())),
        state
            .length()
            .map(|length| length.saturating_sub(state.offset())),
    ]
    .into_iter()
    .flatten()
    .min()
}

fn validate_creation_with_upload_body(
    config: &Config,
    state: &UploadState,
    body_len: u64,
) -> Result<ReceiveProjection> {
    let projected_offset = state.offset().saturating_add(body_len);

    if let Some(max_size) = config.max_size()
        && projected_offset > max_size
    {
        return Err(Error::SizeExceeded {
            size: projected_offset,
            max: max_size,
        });
    }

    if let Some(length) = state.length()
        && projected_offset > length
    {
        return Err(Error::SizeExceeded {
            size: projected_offset,
            max: length,
        });
    }

    Ok(ReceiveProjection {
        projected_offset,
        completes_upload: state
            .length()
            .is_some_and(|length| projected_offset == length),
    })
}

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

    #[test]
    fn prepare_receive_rejects_offset_mismatch() {
        let mut state = UploadState::new("upload-1").with_length(10);
        state.set_offset(4);

        let err = prepare_receive(
            &Config::default(),
            &mut state,
            ReceiveRequest {
                client_offset: 3,
                upload_length: None,
            },
        )
        .unwrap_err();

        assert!(matches!(
            err,
            Error::OffsetMismatch {
                expected: 4,
                actual: 3
            }
        ));
    }

    #[test]
    fn prepare_receive_reports_completion_before_offset_for_retried_upload() {
        // A client retrying an already-completed upload, typically at offset 0,
        // must get the accurate completion error, not an `OffsetMismatch`
        // against the full length.
        let mut state = UploadState::new("upload-1").with_length(10);
        state.set_offset(10);
        assert!(state.is_complete());

        let err = prepare_receive(
            &Config::default(),
            &mut state,
            ReceiveRequest {
                client_offset: 0,
                upload_length: None,
            },
        )
        .unwrap_err();

        assert!(matches!(
            err,
            Error::CompletedUploadModificationForbidden(id) if id == "upload-1"
        ));
    }

    #[test]
    fn prepare_receive_sets_deferred_length() {
        let mut state = UploadState::new("upload-1");

        prepare_receive(
            &Config::default(),
            &mut state,
            ReceiveRequest {
                client_offset: 0,
                upload_length: Some(10),
            },
        )
        .unwrap();

        assert_eq!(state.length(), Some(10));
    }

    #[test]
    fn validate_receive_body_detects_completion() {
        let state = UploadState::new("upload-1").with_length(5);

        let projection = validate_receive_body(&Config::default(), &state, 5).unwrap();

        assert_eq!(projection.projected_offset, 5);
        assert!(projection.completes_upload);
    }

    #[test]
    fn validate_receive_body_rejects_body_beyond_declared_length() {
        let state = UploadState::new("upload-1").with_length(5);

        let err = validate_receive_body(&Config::default(), &state, 6).unwrap_err();

        assert!(matches!(err, Error::SizeExceeded { size: 6, max: 5 }));
    }

    #[cfg(all(feature = "storage-memory", feature = "state-memory"))]
    #[tokio::test]
    async fn creation_with_upload_id_collision_preserves_existing_state() {
        use crate::hooks::NoopHookExecutor;
        use crate::protocol::RequestBody;
        use crate::state::memory::MemoryStateStore;
        use crate::storage::memory::MemoryStorage;

        // A pre-existing upload already owns this ID.
        let store = MemoryStateStore::new();
        let existing = UploadState::new("dup").with_length(999);
        store.set(&existing, WriteMode::CreateNew).await.unwrap();

        // A Creation-With-Upload request tries to create a new upload with the
        // same ID. The `CreateNew` write fails with `AlreadyExists`, and the
        // rollback must not touch the pre-existing upload's state record.
        let storage = MemoryStorage::new();
        let hooks = NoopHookExecutor::new();
        let config = Config::default();
        let request_info = HookRequestInfo::default();
        let receiver = ByteReceiver::new(&storage, &store, &hooks, &config, &request_info);

        let headers = Headers {
            upload_length: Some(5),
            content_type: Some("application/offset+octet-stream".to_string()),
            content_length: Some(5),
            ..Default::default()
        };
        let err = receiver
            .receive_creation_with_upload(
                &headers,
                UploadState::new("dup").with_length(5),
                RequestBody::from_chunk_stream(ChunkStream::from_bytes(bytes::Bytes::from_static(
                    b"Hello",
                ))),
            )
            .await
            .unwrap_err();

        assert!(matches!(err, Error::AlreadyExists(_)));
        let survivor = store
            .get("dup")
            .await
            .unwrap()
            .expect("pre-existing upload state must survive a colliding creation");
        assert_eq!(
            survivor.length(),
            Some(999),
            "the surviving record must be the original upload, not the rejected one"
        );
    }
}