• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

swift-server/http: Historical HTTP API - please use https://github.com/swift-ser ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

swift-server/http

开源软件地址:

https://github.com/swift-server/http

开源编程语言:

Swift 68.8%

开源软件介绍:

Swift Server Project HTTP APIs

⚠️ This project is unmaintained experimental legacy code. It has been obsoleted by SwiftNIO which contains the recommended HTTP API of the Swift Server Work Group.

It remains here for historical interest only.

Getting Started

Hello World

The following code implements a very simple "Hello World!" server:

import Foundation
import HTTP

func hello(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing { 
    response.writeHeader(status: .ok) 
    response.writeBody("Hello, World!") 
    response.done() 
    return .discardBody 
} 

let server = HTTPServer()
try! server.start(port: 8080, handler: hello)

RunLoop.current.run()

The hello() function receives a HTTPRequest that describes the request and a HTTPResponseWriter used to write a response.

Data that is received as part of the request body is made available to the closure that is returned by the hello() function. In the "Hello World!" example the request body is not used, so .discardBody is returned.

Echo Server

The following code implements a very simple Echo server that responds with the contents of the incoming request:

import Foundation
import HTTP

func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing {
    response.writeHeader(status: .ok)
    return .processBody { (chunk, stop) in
        switch chunk {
        case .chunk(let data, let finishedProcessing):
            response.writeBody(data) { _ in
                finishedProcessing()
            }
        case .end:
            response.done()
        default:
            stop = true
            response.abort()
        }
    }
}

let server = HTTPServer()
try! server.start(port: 8080, handler: echo)

RunLoop.current.run()

As the Echo server needs to process the request body data and return it in the reponse, the echo() function returns a .processBody closure. This closure is called with .chunk when data is available for processing from the request, and .end when no more data is available.

Once any data chunk has been processed, finishedProcessing() should be called to signify that it has been handled.

When the response is complete, response.done() should be called.

API Documentation

Full Jazzy documentation of the API is available here:
https://swift-server.github.io/http/

Contributing

Feedback

We are actively seeking feedback on this prototype and your comments are extremely valuable. If you have any comments on the API design, the implementation, or any other aspects of this project, please email the swift-server-dev mailing list.

Writing Code

We also welcome code contributions. If you are developing on macOS, you may want to work within Xcode. This project uses the Swift Package Manager. To work on this project within Xcode you can run the Swift Package Manager command swift package generate-xcodeproj to generate an .xcodeproj to work on within Xcode.

Acknowledgements

This project is based on an inital proposal from @weissi on the swift-server-dev mailing list:
https://lists.swift.org/pipermail/swift-server-dev/Week-of-Mon-20170403/000422.html




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
jnunemaker/httparty: Makes http fun again!发布时间:2022-06-17
下一篇:
http4s/http4s: A minimal, idiomatic Scala interface for HTTP发布时间:2022-06-17
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap