Sample CodeReviewed 2026-07-21View on Apple Developer

Building a resumable upload server with SwiftNIO

At a glance

Item Summary
Purpose Support HTTP resumable upload protocol in SwiftNIO by translating resumable uploads to regular uploads.
App architecture A Swift sample with the source-visible chain BaseClientCodecHTTPResumableUploadHandlerFoundationNetworking APIs.
Main patterns No named application pattern supported by the extracted structure
Project style 30 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries.
Execution model Source-visible boundaries: Sendable or @Sendable, os_unfair_lock, async declaration or closure; none alone proves a background thread.
State/event model No structured observation or publisher-scheduling marker indexed.
Key frameworks/packages HTTPTypes, NIOCore, HTTPTypesNIO, XCTest, Foundation, Remote package https://github.com/apple/swift-nio.git, Remote package https://github.com/apple/swift-nio-http2.git; these are source dependencies, not architecture labels.

Project structure

Source bundle/
├── Dependencies/
│   └── swift-http-types/
│       └── Sources/
│           ├── HTTPTypesNIOHTTP2/
│           │   ├── HTTP2ToHTTPCodec.swift
│           │   └── HTTP2HeadersStateMachine.swift
│           ├── HTTPTypes/
│           │   ├── HTTPField.swift
│           │   ├── HTTPFields.swift
│           │   ├── HTTPRequest.swift
│           │   └── HTTPResponse.swift
│           └── HTTPTypesNIOHTTP1/
│               ├── HTTP1ToHTTPCodec.swift
│               └── HTTPToHTTP1Codec.swift
└── Sources/
    └── NIOResumableUpload/
        ├── HTTPResumableUploadHandler.swift
        ├── HTTPResumableUploadProtocol.swift
        ├── HTTPResumableUpload.swift
        └── HTTPResumableUploadChannel.swift

Structure observations

  • Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
  • Primary languages: Swift.
  • The verified tree contains 0 project/configuration file(s) and 42 source declaration(s).

Overall architecture

Reference code

Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP2/HTTP2ToHTTPCodec.swift:29 — architecture anchor

private struct BaseClientCodec {
    private var headerStateMachine: HTTP2HeadersStateMachine = HTTP2HeadersStateMachine(mode: .client)
    // ...
}

Interpretation

The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Foundation.

Ownership and state

Ownership evidence

Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:45 — stored dependency or nearest verified ownership anchor

public struct HTTPField: Sendable, Hashable {
    // ...
        self.name = name
    // ...
}
Owner Object or state Relationship Mutation authority
HTTPField Name (name) stores or receives App/module collaborators
HTTPField Self (rawValue) stores or receives App/module collaborators
HTTPField DynamicTableIndexingStrategy (indexingStrategy) stores or receives App/module collaborators
_Storage Array (fields) owns value state App/module collaborators

Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.

Concurrency, scheduling, and thread safety

Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.

Concern Source mechanism Verified placement or handoff Evidence
Transfer contract Sendable or @Sendable The source declares a sendability boundary; this alone does not synchronize mutable state. Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:25
Synchronization os_unfair_lock The source references a synchronization primitive; the protected state requires surrounding review. Dependencies/swift-http-types/Sources/HTTPTypes/HTTPFields.swift:40
Suspension boundary async declaration or closure The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. Dependencies/swift-http-types/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift:59

@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.

Reference code

Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:25 — representative execution boundary

public struct HTTPField: Sendable, Hashable {
    // ...
        case automatic
    // ...
}

State propagation, frameworks, and dependencies

Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.

Category Mechanism or module Verified role Evidence
Package manifest Remote package https://github.com/apple/swift-nio.git The manifest declares a remote URL or registry dependency; direct target use and transitive position are not inferred. Dependencies/swift-http-types/Package.swift:21
Package manifest Remote package https://github.com/apple/swift-nio-http2.git The manifest declares a remote URL or registry dependency; direct target use and transitive position are not inferred. Dependencies/swift-http-types/Package.swift:22
Source import HTTPTypes The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift:21
Source import NIOCore The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Sources/HTTPTypesNIO/HTTPTypesNIO.swift:21
Source import HTTPTypesNIO The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP1/HTTP1ToHTTPCodec.swift:23
Source import XCTest The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift:21
Source import Foundation The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift:22
Source import FoundationNetworking The cited file imports this module; runtime use and architectural role are not inferred. Dependencies/swift-http-types/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift:24

receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.

Class and protocol design

Sources/NIOResumableUpload/HTTPResumableUploadHandler.swift:13 — representative type boundary

public final class HTTPResumableUploadHandler: ChannelDuplexHandler {
    public typealias InboundIn = HTTPTypeServerRequestPart
    // ...
}
Type Responsibility Depends on or conforms to
HTTPResumableUploadHandler Handles callbacks or feature events ChannelDuplexHandler
InboundRecorder Owns capture or recording work Concrete collaborators/imported frameworks
InboundRecorder Owns capture or recording work Concrete collaborators/imported frameworks
InboundRecorder Owns capture or recording work Concrete collaborators/imported frameworks
BaseClientCodec Represents a feature value or composable behavior Concrete collaborators/imported frameworks
HTTP2FramePayloadToHTTPClientCodec Owns feature behavior and collaborator lifecycle ChannelInboundHandler, ChannelOutboundHandler
BaseServerCodec Represents a feature value or composable behavior Concrete collaborators/imported frameworks
HTTP2FramePayloadToHTTPServerCodec Owns feature behavior and collaborator lifecycle ChannelInboundHandler, ChannelOutboundHandler
HTTPResumableUploadProtocol Defines a closed set of feature states or choices Concrete collaborators/imported frameworks
RequestType Defines a closed set of feature states or choices Concrete collaborators/imported frameworks

No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.

Access control

Symbol Access Verified effect Likely rationale
HTTPField (Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:44) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
HTTPField (Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:53) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
name (Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:64) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.
value (Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:74) public The symbol is visible to importing modules. Inference: make the declaration available across a module or target boundary.

Reference code

Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift:44 — representative boundary

    public init(name: Name, value: some StringProtocol) {
        self.name = name
        self.rawValue = Self.legalizeValue(ISOLatin1String(value))
    }

Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.

Logic ownership and placement

Logic Owning type or file Placement rationale
Handles callbacks or feature events HTTPResumableUploadHandler The source’s Handler suffix makes this role explicit.
Owns capture or recording work InboundRecorder The source’s Recorder suffix makes this role explicit.

Design patterns

Pattern Source evidence Purpose or tradeoff
No named application pattern Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP2/HTTP2ToHTTPCodec.swift:29 The verified source directly composes concrete framework types; this document avoids forcing a pattern name.

Naming conventions

  • Types: Handler: HTTPResumableUploadHandler; Recorder: InboundRecorder.
  • Protocols: no local protocol declaration in the scanned source.
  • Methods: processInboundData, processOutboundData, channelRead, write, handlerAdded, channelActive, channelInactive, channelReadComplete.
  • Files: Sources/NIOResumableUpload/HTTPResumableUploadHandler.swift, Sources/NIOResumableUpload/HTTPResumableUploadProtocol.swift, Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift, Dependencies/swift-http-types/Sources/HTTPTypes/HTTPFields.swift, Dependencies/swift-http-types/Sources/HTTPTypes/HTTPRequest.swift, Dependencies/swift-http-types/Sources/HTTPTypes/HTTPResponse.swift.

Architecture takeaways

  • BaseClientCodec is the main source-visible entry or composition anchor for this sample.
  • Framework work reaches HTTPTypes, NIOCore, HTTPTypesNIO, FoundationNetworking through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
  • Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
  • Access-control conclusions separate verified language visibility from the likely design rationale.
  • The source does not justify labeling the design protocol-oriented.

Source map

Source file Relevant symbols
Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP2/HTTP2ToHTTPCodec.swift BaseClientCodec, HTTP2FramePayloadToHTTPClientCodec, BaseServerCodec, HTTP2FramePayloadToHTTPServerCodec
Dependencies/swift-http-types/Sources/HTTPTypes/HTTPField.swift Cited implementation, Sendable or @Sendable, HTTPField, DynamicTableIndexingStrategy
Sources/NIOResumableUpload/HTTPResumableUploadHandler.swift HTTPResumableUploadHandler
Dependencies/swift-http-types/Sources/HTTPTypes/HTTPFields.swift os_unfair_lock, HTTPFields, _Storage
Dependencies/swift-http-types/Sources/HTTPTypesFoundation/URLSession+HTTPTypes.swift async declaration or closure, HTTPTypeConversionError
Dependencies/swift-http-types/Package.swift Cited implementation, Feature implementation
Dependencies/swift-http-types/Sources/HTTPTypesFoundation/HTTPRequest+URL.swift HTTPTypes, Foundation, Feature implementation
Dependencies/swift-http-types/Sources/HTTPTypesNIO/HTTPTypesNIO.swift NIOCore, HTTPTypePart
Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP1/HTTP1ToHTTPCodec.swift HTTPTypesNIO, HTTP1ToHTTPClientCodec, HTTP1ToHTTPServerCodec
Dependencies/swift-http-types/Tests/HTTPTypesFoundationTests/HTTPTypesFoundationTests.swift XCTest, HTTPTypesFoundationTests
Dependencies/swift-http-types/Sources/HTTPTypesFoundation/URLRequest+HTTPTypes.swift FoundationNetworking, Feature implementation
Sources/NIOResumableUpload/HTTPResumableUploadProtocol.swift HTTPResumableUploadProtocol, RequestType, UploadIncompleteFieldValue, UploadOffsetFieldValue
Dependencies/swift-http-types/Sources/HTTPTypes/HTTPRequest.swift HTTPRequest, Method
Dependencies/swift-http-types/Sources/HTTPTypes/HTTPResponse.swift HTTPResponse, Status
Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP1/HTTPToHTTP1Codec.swift HTTPToHTTP1ClientCodec, HTTPToHTTP1ServerCodec
Dependencies/swift-http-types/Sources/HTTPTypesNIOHTTP2/HTTP2HeadersStateMachine.swift HTTP2HeadersStateMachine, HeaderType