Luminescence 0.3.1

Luminescence 0.3.1

TestsTested
LangLanguage CC
License MIT
ReleasedLast Release Nov 2015

Maintained by Paulo Faria.



  • By
  • Paulo Faria

Luminescence

codecov.io

Luminescence is an HTTP parser for Swift 2.

Features

  • [x] No Foundation dependency (Linux ready)
  • [x] Asynchronous parsing
  • [x] Handles persistent streams (keep-alive)
  • [x] Decodes chunked encoding
  • [x] Defends against buffer overflow attacks

Luminescence wraps the C library http_parser used in node.js.

Products

Luminescence is the base for the HTTP servers

  • Aeon - GCD based HTTP server
  • Epoch - Luminescence based HTTP server

Usage

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 :(
}

Chunked Data and Persistent Streams

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 :(
}

Using EOF

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 :(
}

Installation

Command Line Application

To use Luminescence in a command line application:

License

Luminescence is released under the MIT license. See LICENSE for details.