Swift编写的 HTTP 库:SwiftyHTTP
jopen
10年前
SwiftyHTTP 是简单的,基于 GCD 的 HTTP 客户端和服务器,采用纯 Swift 编写。SwiftyHTTP 是如何集成 Swift 和 C APIs 的示例。
Server:
let httpd = HTTPServer() .onRequest { rq, res, con in res.bodyAsString = "<h2>Always Right, Never Wrong!</h2>" con.sendResponse(res) } .listen(1337)
Server using the Node.JS like Connect bonus class:
let httpd = Connect() .use { rq, res, _, next in println("\(rq.method) \(rq.url) \(res.status)") next() } .use("/hello") { rq, res, con, next in res.bodyAsString = "Hello!" con.sendResponse(res) } .use("/") { rq, res, con, next in res.bodyAsString = "Always, almost sometimes." con.sendResponse(res) } .listen(1337)
Client (do not use this, use NSURLSession!):
GET("http://www.apple.com/") .done { println() println("request \($0)") println("response \($1)") println("body:\n\($1.bodyAsString)") } .fail { println("failed \($0): \($1)") } .always { println("---") }