Gemini/GeminiProtocol/GeminiRequest.swift

42 lines
951 B
Swift
Raw Normal View History

2020-07-13 03:09:37 +00:00
//
// GeminiRequest.swift
// Gemini
//
// Created by Shadowfacts on 7/12/20.
//
import Foundation
2020-07-13 22:22:36 +00:00
public struct GeminiRequest {
public let url: URL
2020-07-13 03:09:37 +00:00
2020-07-13 22:22:36 +00:00
public init(url: URL) throws {
2020-07-15 02:39:38 +00:00
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 {
2020-07-13 03:09:37 +00:00
throw Error.urlTooLong
}
}
var data: Data {
var data = url.absoluteString.data(using: .utf8)!
data.append(contentsOf: [13, 10]) // <CR><LF>
return data
}
}
2020-07-13 22:22:36 +00:00
public extension GeminiRequest {
2020-07-13 03:09:37 +00:00
enum Error: Swift.Error {
2020-07-15 02:39:38 +00:00
case invalidURL
case wrongProtocol
2020-07-13 03:09:37 +00:00
case urlTooLong
}
}