Luminescence 是为 Swift 2 设计的 HTTP 解析器。
Foundation
依赖(适用于 Linux)Luminescence 使用了在 node.js 中使用的 C 库 http_parser。
Luminescence 是 HTTP 服务器的基础
HTTPRequestParser
import Luminescence
let parser = HTTPRequestParser { request in
// Here you get your parsed requests (HTTPRequest)
}
do {
// Here you'll probably get the real data from a socket, right?
let data = "GET / HTTP/1.1\r\n\r\n"
try parser.parse(data)
} catch {
// Something bad happened :(
}
HTTPResponseParser
import Luminescence
let parser = HTTPResponseParser { response in
// Here you get your parsed responses (HTTPResponse)
}
do {
// Here you'll probably get the real data from a socket, right?
let data = "HTTP/1.1 204 No Content\r\n\r\n"
try parser.parse(data)
} catch {
// Something bad happened :(
}
import Luminescence
let parser = HTTPRequestParser { request in
// Here you get your parsed requests (HTTPRequest)
}
do {
// You can call parse as many times as you like
// passing chunks of the request or response.
// Once the parser completes it will spit the result
let data1 = "GE"
let data2 = "T / HTT"
let data3 = "P/1.1\r\n\r\n")
try parser.parse(data1)
try parser.parse(data2)
try parser.parse(data3)
// The parser supports persistent streams (keep-alive)
// so you can keep streaming requests or responses
// all you want.
let data4 = "POS"
let data5 = "T / H"
let data6 = "TTP/1.1\r\n\r\n")
try parser.parse(data4)
try parser.parse(data5)
try parser.parse(data6)
} catch {
// Something bad happened :(
}
import Luminescence
let parser = HTTPResponseParser { response in
// Here you get your parsed responses (HTTPResponse)
}
do {
// Sometimes servers return a response without Content-Length
// to close the stream you can call eof()
let data = ("HTTP/1.1 200 OK\r\n" +
"\r\n" +
"Zewo")
try parser.parse(data)
try parser.eof()
} catch {
// Something bad happened :(
}
要在命令行应用程序中使用 Luminescence
Luminescence 采用 MIT 许可协议发布。有关详细信息,请参阅 LICENSE 文件。