Gemini/GeminiProtocol/GeminiRequest.swift

42 lines
951 B
Swift

//
// GeminiRequest.swift
// Gemini
//
// Created by Shadowfacts on 7/12/20.
//
import Foundation
public struct GeminiRequest {
public let url: URL
public init(url: URL) throws {
guard var components = URLComponents(url: url, resolvingAgainstBaseURL: true) else { throw Error.invalidURL }
guard components.scheme == "gemini" else { throw Error.wrongProtocol }
if components.port == nil {
components.port = 1965
}
self.url = components.url!
if self.url.absoluteString.count > 1024 {
throw Error.urlTooLong
}
}
var data: Data {
var data = url.absoluteString.data(using: .utf8)!
data.append(contentsOf: [13, 10]) // <CR><LF>
return data
}
}
public extension GeminiRequest {
enum Error: Swift.Error {
case invalidURL
case wrongProtocol
case urlTooLong
}
}