mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-09 09:18:42 +09:00
Dactyloidae iOS initial commit
This commit is contained in:
parent
daa6179d22
commit
7154a0497e
2123 changed files with 197052 additions and 0 deletions
55
mobile/ios/Client/Extensions/NSURLExtensionsMailTo.swift
Normal file
55
mobile/ios/Client/Extensions/NSURLExtensionsMailTo.swift
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import Foundation
|
||||
|
||||
/**
|
||||
* Data structure containing metadata associated with a mailto: link. For additional details,
|
||||
* see RFC 2368 https://tools.ietf.org/html/rfc2368
|
||||
*/
|
||||
public struct MailToMetadata {
|
||||
public let to: String
|
||||
public let headers: [String: String]
|
||||
}
|
||||
|
||||
public extension URL {
|
||||
|
||||
/**
|
||||
Extracts the metadata associated with a mailto: URL according to RFC 2368
|
||||
https://tools.ietf.org/html/rfc2368
|
||||
*/
|
||||
func mailToMetadata() -> MailToMetadata? {
|
||||
guard scheme == "mailto" else {
|
||||
return nil
|
||||
}
|
||||
let urlString = absoluteString
|
||||
|
||||
// Extract 'to' value
|
||||
let toStart = urlString.characters.index(urlString.startIndex, offsetBy: "mailto:".characters.count)
|
||||
let toEnd = urlString.characters.index(of: "?") ?? urlString.endIndex
|
||||
|
||||
let to = urlString.substring(with: toStart..<toEnd)
|
||||
|
||||
guard toEnd != urlString.endIndex else {
|
||||
return MailToMetadata(to: to, headers: [String: String]())
|
||||
}
|
||||
|
||||
// Extract headers
|
||||
let headersString = urlString.substring(with: urlString.index(toEnd, offsetBy: 1)..<urlString.endIndex)
|
||||
var headers = [String: String]()
|
||||
let headerComponents = headersString.components(separatedBy: "&")
|
||||
|
||||
headerComponents.forEach { headerPair in
|
||||
let components = headerPair.components(separatedBy: "=")
|
||||
guard components.count == 2 else {
|
||||
return
|
||||
}
|
||||
|
||||
let (hname, hvalue) = (components[0], components[1])
|
||||
headers[hname] = hvalue
|
||||
}
|
||||
|
||||
return MailToMetadata(to: to, headers: headers)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue