Skip to content

Instantly share code, notes, and snippets.

@coryalder
Created December 3, 2015 23:04
Show Gist options
  • Save coryalder/8f68fa5e0233a1e43627 to your computer and use it in GitHub Desktop.
Save coryalder/8f68fa5e0233a1e43627 to your computer and use it in GitHub Desktop.
Example linux swift cgi
import Foundation
// work around the currently missing .componentsSeparatedByString method
extension String {
func split(on: Character) -> [String] {
var segments = [String]()
var current = ""
for char in self.characters {
if (char == on) {
segments.append(current)
current = ""
} else {
current.append(char)
}
}
if current.characters.count > 0 {
segments.append(current)
}
return segments
}
}
let dic = NSProcessInfo.processInfo().environment
//dic["QUERY_STRING"] = "this%20is%20working=pop&tacos=yes"
var cgi = [String: String]()
if let encoded = dic["QUERY_STRING"], let decoded = encoded.bridge().stringByRemovingPercentEncoding {
let sections = decoded.split("&")
for set in sections {
let kv = set.split("=")
if kv.count == 2 {
cgi[kv[0]] = kv[1]
}
}
}
print("Content-type: text/plain\n\n")
print("\(cgi)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment