Swift編寫的 HTTP 庫:SwiftyHTTP

jopen 10年前發布 | 24K 次閱讀 Apple Swift開發 SwiftyHTTP

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("---") }

Swift編寫的 HTTP 庫:SwiftyHTTP

項目主頁:http://www.baiduhome.net/lib/view/home/1406470257672

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!