SwiftNIO Extras

repository·main·Indexed 42 days ago

https://github.com/apple/swift-nio-extras

A collection of supplementary code, protocols, and handlers for SwiftNIO. It provides utilities for common networking tasks, including framing and codecs (LineBasedFrameDecoder, LengthFieldBasedFrameDecoder), HTTP utilities (NIOHTTPCompression), SOCKS v5 protocol implementation (NIOSOCKS), and debugging tools like NIOWritePCAPHandler and ServerQuiescingHelper.

Tokens
2K
Snippets
1
Records
16
Agent score
52%

What's inside SwiftNIO Extras

  1. Overview of NIOExtras utilities

    main

    NIOExtras is a collection of utilities designed to assist in building and debugging Swift-NIO applications. It provides tools across four main categories:

    1. Debugging Aids: ChannelHandlers for logging inbound/outbound pipeline events and logging data in packet capture (PCAP) format.
    2. Encoding and Decoding: Helpers for common data frame formats, including fixed-length frames, newline-terminated frames, length-prefixed frames, and frames defined by a context-length header.
    3. Channel Pipeline Aids: Tools like ServerQuiescingHelper for managing child channels and RequestResponseHandler for simplifying request-response flows.
    4. Data Types: Specialized types like NIOLengthFieldBitLength to support framing logic.
  2. Use NIOHTTPCompression for automatic HTTP compression and decompression

    main

    The NIOHTTPCompression module provides channel handlers to automatically manage the compression and decompression of HTTP data. When added to your pipeline, the handlers automatically manage the Content-Encoding, Content-Length, and accept-encoding HTTP headers.

    Important Considerations:

    • Performance: Compression/decompression is performed on the event loop thread, which can impact performance.
    • Efficiency: This works most effectively when there is sufficient data written between flushes.
  3. Decode common data frame formats

    main

    NIOExtras provides several decoders to handle structured data streams:

    • Fixed Length: Use FixedLengthFrameDecoder for frames with a constant size.
    • Newline Terminated: Use LineBasedFrameDecoder for text-based protocols where lines end in newlines.
    • Length Prefixed: Use LengthFieldBasedFrameDecoder for frames that include a length field in the header.
    • JSON-RPC: Use NIOJSONRPCFraming for JSON-RPC specific framing requirements.
  4. Implement SOCKS v5 protocol with NIOSOCKS

    main

    NIOSOCKS provides an implementation of the SOCKS v5 protocol (RFC1928). To use this protocol in your application, add the appropriate channel handler to the start of your ChannelPipeline.

    • For a SOCKS client: Use SOCKSClientHandler.
    • For a SOCKS server: Use SOCKSServerHandshakeHandler.

    Refer to the NIOSOCKSClient target in the repository for a complete implementation example.

  5. Install swift-nio-extras via Swift Package Manager

    main

    To use swift-nio-extras in your project, add it as a dependency in your Package.swift file.

    Prerequisites:

    • swift-nio version 2.30.0 or better.
    • zlib and its development headers installed on your system.

    Dependency Configuration:

    .package(url: "https://github.com/apple/swift-nio-extras.git", from: "1.0.0"),
  6. Debug Swift-NIO channel pipeline events

    main

    You can use specialized ChannelHandlers to monitor activity within your pipeline:

    • Use DebugInboundEventsHandler to log inbound channel pipeline events.
    • Use DebugOutboundEventsHandler to log outbound channel pipeline events.
    • Use NIOWritePCAPHandler to log data in packet capture (PCAP) format for network analysis.
  7. Reference SOCKS v5 Client and Server message types

    main

    NIOSOCKS defines specific types for the messages exchanged during the SOCKS v5 handshake and request phases.

    Client Messages:

    • ClientMessage: Base type for client messages.
    • ClientGreeting: The initial message sent by the client to the server.
    • SOCKSRequest: The message sent by the client to request a connection to a specific destination.

    Server Messages:

    • ServerMessage: Base type for server messages.
    • SelectedAuthenticationMethod: Response indicating which authentication method the server has chosen.
    • SOCKSResponse: Response to a SOCKSRequest indicating success or failure.
  8. Available utilities and handlers in swift-nio-extras

    main

    The following components are available in swift-nio-extras to assist with common networking tasks:

    Framing and Codecs

    • LineBasedFrameDecoder: Splits incoming ByteBuffers on line endings.
    • FixedLengthFrameDecoder: Splits incoming ByteBuffers by a fixed number of bytes.
    • LengthFieldBasedFrameDecoder: Splits incoming ByteBuffers by a number of bytes specified in a fixed length header contained within the buffer.
    • LengthFieldPrepender: Prepends the number of bytes to outgoing ByteBuffers as a fixed length header. Often used with LengthFieldBasedFrameDecoder.

    HTTP Utilities

    • HTTPResponseCompressor: Compresses the body of every HTTP/1 response message.
    • HTTP1ToHTTPClientCodec / HTTP1ToHTTPServerCodec: Translates HTTP/1 messages into shared HTTP types.
    • HTTPToHTTP1ClientCodec / HTTPToHTTP1ServerCodec: Translates shared HTTP types into HTTP/1 messages.
    • HTTP2FramePayloadToHTTPClientCodec / HTTP2FramePayloadToHTTPServerCodec: Translates HTTP/2 concepts into shared HTTP types.
    • HTTPResumableUploadHandler: Translates HTTP resumable uploads to regular uploads.
    • HTTPDrippingDownloadHandler: Sends a configurable stream of zeroes to a client.
    • HTTPReceiveDiscardHandler: Receives arbitrary bytes from a client and discards them.

    Debugging and Monitoring

    • DebugInboundsEventHandler: Prints all inbound events traveling through the ChannelPipeline.
    • DebugOutboundsEventHandler: Prints all outbound events traveling through the ChannelPipeline.
    • WritePCAPHandler: A ChannelHandler that writes .pcap files containing the traffic of the ChannelPipeline for inspection with Wireshark/tcpdump.

    Server and Request Management

    • QuiescingHelper: Helps to quiesce a server by notifying user code when all previously open connections have closed.
    • RequestResponseHandler: Matches a request and a promise with the corresponding response.