mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-24 04:37:50 +00:00
Motivation: In general we should try to be specific about what we're referring to. "Structured headers" is not really an accurate description of what this module works with: "Structured Header Field Values", or "Structured Field Value" is usually better. To that end this change adjusts our naming to be closer to what we want. It also changes our module names to prioritise the use of the Codable-based one. Modifications: - Renamed CodableStructuredHeaders to StructuredFieldValues. - Renamed StructuredHeaders to RawStructuredFieldValues. - Renamed a number of types from Field to FieldValue. - Renamed a number of types from Header to Field or FieldValue. - Renamed some functions along the above lines. Result: Better and more consistent terminology will be used.
207 lines
5.6 KiB
Swift
207 lines
5.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the SwiftNIO open source project
|
|
//
|
|
// Copyright (c) 2020-2021 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 Foundation
|
|
import RawStructuredFieldValues
|
|
|
|
struct Flags {
|
|
var headerType: HeaderType
|
|
|
|
init() {
|
|
// Default to item
|
|
self.headerType = .item
|
|
|
|
for argument in CommandLine.arguments.dropFirst() {
|
|
switch argument {
|
|
case "--dictionary":
|
|
self.headerType = .dictionary
|
|
|
|
case "--list":
|
|
self.headerType = .list
|
|
|
|
case "--item":
|
|
self.headerType = .item
|
|
|
|
default:
|
|
Self.helpAndExit()
|
|
}
|
|
}
|
|
}
|
|
|
|
private static func helpAndExit() -> Never {
|
|
print("Flags:")
|
|
print("")
|
|
print("\t--dictionary: Parse as dictionary field")
|
|
print("\t--list: Parse as list field")
|
|
print("\t--item: Parse as item field (default)")
|
|
exit(2)
|
|
}
|
|
}
|
|
|
|
extension Flags {
|
|
enum HeaderType {
|
|
case dictionary
|
|
case list
|
|
case item
|
|
}
|
|
}
|
|
|
|
enum Header {
|
|
case dictionary(OrderedMap<String, ItemOrInnerList>)
|
|
case list([ItemOrInnerList])
|
|
case item(Item)
|
|
|
|
func prettyPrint() {
|
|
switch self {
|
|
case .dictionary(let dict):
|
|
print("- dictionary (\(dict.count) entries):")
|
|
dict.prettyPrint(depth: 1)
|
|
case .list(let list):
|
|
print("- list (\(list.count) entries):")
|
|
list.prettyPrint(depth: 1)
|
|
case .item(let item):
|
|
print("- item:")
|
|
item.prettyPrint(depth: 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Array where Element == ItemOrInnerList {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
for (offset, element) in self.enumerated() {
|
|
print("\(tabs)- [\(offset)]:")
|
|
element.prettyPrint(depth: depth + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension OrderedMap where Key == String, Value == ItemOrInnerList {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
for (key, value) in self {
|
|
print("\(tabs)- \(key):")
|
|
value.prettyPrint(depth: depth + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension OrderedMap where Key == String, Value == BareItem {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
|
|
for (key, value) in self {
|
|
print("\(tabs)- \(key): \(value.prettyFormat())")
|
|
}
|
|
}
|
|
}
|
|
|
|
extension ItemOrInnerList {
|
|
func prettyPrint(depth: Int) {
|
|
switch self {
|
|
case .item(let item):
|
|
item.prettyPrint(depth: depth)
|
|
case .innerList(let list):
|
|
list.prettyPrint(depth: depth)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension Item {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
|
|
print("\(tabs)- item: \(self.bareItem.prettyFormat())")
|
|
print("\(tabs)- parameters (\(parameters.count) entries):")
|
|
self.parameters.prettyPrint(depth: depth + 1)
|
|
}
|
|
}
|
|
|
|
extension InnerList {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
|
|
print("\(tabs)- innerList (\(parameters.count) entries):")
|
|
self.bareInnerList.prettyPrint(depth: depth + 1)
|
|
print("\(tabs)- parameters (\(parameters.count) entries):")
|
|
self.parameters.prettyPrint(depth: depth + 1)
|
|
}
|
|
}
|
|
|
|
extension BareInnerList {
|
|
func prettyPrint(depth: Int) {
|
|
let tabs = String(repeating: "\t", count: depth)
|
|
for (offset, element) in self.enumerated() {
|
|
print("\(tabs)- [\(offset)]:")
|
|
element.prettyPrint(depth: depth + 1)
|
|
}
|
|
}
|
|
}
|
|
|
|
extension BareItem {
|
|
func prettyFormat() -> String {
|
|
switch self {
|
|
case .bool(let bool):
|
|
return "boolean \(bool)"
|
|
case .integer(let int):
|
|
return "integer \(int)"
|
|
case .string(let string):
|
|
return "string \"\(string)\""
|
|
case .token(let token):
|
|
return "token \(token)"
|
|
case .undecodedByteSequence(let bytes):
|
|
return "byte sequence \(bytes)"
|
|
case .decimal(let decimal):
|
|
let d = Decimal(sign: decimal.mantissa > 0 ? .plus : .minus,
|
|
exponent: Int(decimal.exponent), significand: Decimal(decimal.mantissa))
|
|
return "decimal \(d)"
|
|
}
|
|
}
|
|
}
|
|
|
|
func main() {
|
|
do {
|
|
let flags = Flags()
|
|
var data = FileHandle.standardInput.readDataToEndOfFile()
|
|
|
|
// We need to strip trailing newlines.
|
|
var index = data.endIndex
|
|
while index > data.startIndex {
|
|
data.formIndex(before: &index)
|
|
if data[index] != UInt8(ascii: "\n") {
|
|
break
|
|
}
|
|
}
|
|
data = data[...index]
|
|
var parser = StructuredFieldValueParser(data)
|
|
|
|
let result: Header
|
|
switch flags.headerType {
|
|
case .dictionary:
|
|
result = .dictionary(try parser.parseDictionaryFieldValue())
|
|
case .list:
|
|
result = .list(try parser.parseListFieldValue())
|
|
case .item:
|
|
result = .item(try parser.parseItemFieldValue())
|
|
}
|
|
|
|
result.prettyPrint()
|
|
} catch {
|
|
print("error: \(error)")
|
|
exit(1)
|
|
}
|
|
}
|
|
|
|
main()
|