Files
swift-nio-imap/Sources/NIOIMAPCore/Grammar/Body/Field/Encoding.swift
T
David Evans 9c2c43f0a3 Add support for nil encoding to address iCloud issues (#685)
Resolves #684

iCloud is sending nil for message encoding types, which isn't allowed according to the spec. That said, we have to support it regardless, so we now support nil for the message encoding.

Also added some body parsing tests as there apparently weren't any "combined" ones like this. Additionally as there are multiple ways of saying "multipart/mixed" and other "multipart/*" subtypes, we now check and merge them.
2022-02-09 16:16:20 +00:00

66 lines
2.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import struct NIO.ByteBuffer
extension BodyStructure {
/// Represents the body transfer encoding as defined in MIME-IMB.
/// Recommended reading: RFC 2045
public struct Encoding: CustomDebugStringConvertible, Hashable {
/// Represents 7-bit encoding, octets with a value larger than 127 are forbidden.
public static var sevenBit: Self { Self("7BIT") }
/// Represents 8-bit encoding, octets may be in the range 1-255. *NUL* octets are forbidden.
public static var eightBit: Self { Self("8BIT") }
/// Represents binary data where octets of any value are allowed, including *NUL*
public static var binary: Self { Self("BINARY") }
/// Represents an arbitrary sequence of octets in a non-human-readable form.
public static var base64: Self { Self("BASE64") }
/// Represents octets that mostly are printable characters in the US-ASCII character set.
public static var quotedPrintable: Self { Self("QUOTED-PRINTABLE") }
/// The uppercased encoding name
internal let stringValue: String
/// The uppercased encoding name
public var debugDescription: String { stringValue }
/// Creates a new `Encoding` representation. Note that the `stringValue` will be uppercased to make the type easily `Equatable`.
/// - parameter stringValue: The string representation of the new `Encoding`. Will be upper-cased.
public init(_ stringValue: String) {
self.stringValue = stringValue.uppercased()
}
}
}
extension String {
public init(_ other: BodyStructure.Encoding) {
self = other.stringValue
}
}
// MARK: - Encoding
extension EncodeBuffer {
@discardableResult mutating func writeBodyEncoding(_ encoding: BodyStructure.Encoding?) -> Int {
guard let encoding = encoding else {
return self.writeNil()
}
return self.writeString("\"\(encoding.stringValue)\"")
}
}