Latest 0.1
Homepage https://github.com/Zewo/HTTPParser
License MIT
Platforms ios 8.0, osx 10.9, requires ARC
Dependencies HTTP
Authors

Swift 2.2
Platforms Linux
License MIT
Slack Status

HTTPParser is an HTTP (RFC 2616) parser for Swift 2.2.

Features

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

HTTPParser wraps a fork of the C library http_parser used in node.js.

Products

HTTPParser is the base for the HTTP servers

  • Aeon – GCD based HTTP server
  • Epoch – Venice based HTTP server

Usage

HTTPRequestParser

import HTTPParser

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.1rnrn"
    try parser.parse(data)
} catch {
    // Something bad happened :(
}

HTTPResponseParser

import HTTPParser

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 Contentrnrn"
    try parser.parse(data)
} catch {
    // Something bad happened :(
}

Chunked Data and Persistent Streams

import HTTPParser

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.1rnrn")

    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.1rnrn")

    try parser.parse(data4)
    try parser.parse(data5)
    try parser.parse(data6)
} catch {
    // Something bad happened :(
}

Using EOF

import HTTPParser

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 OKrn" +
                "rn" +
                "Zewo")
    try parser.parse(data)
    try parser.eof()
} catch {
    // Something bad happened :(
}

Installation

$ git clone https://github.com/Zewo/uri_parser.git
$ cd uri_parser
$ make
$ dpkg -i uri_parser.deb
$ git clone https://github.com/Zewo/http_parser.git
$ cd http_parser
$ make
$ dpkg -i http_parser.deb
  • Add HTTPParser to your Package.swift
import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/Zewo/HTTPParser.git", majorVersion: 0, minor: 1)
    ]
)

Community

Slack

Join us on Slack.

License

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

Latest podspec

{
    "name": "HTTPParser",
    "version": "0.1",
    "license": "MIT",
    "summary": "HTTP parser for Swift 2 (Linux ready)",
    "homepage": "https://github.com/Zewo/HTTPParser",
    "authors": {
        "Paulo Faria": "paulo.faria.rl[email protected]"
    },
    "source": {
        "git": "https://github.com/Zewo/HTTPParser.git",
        "tag": "0.1"
    },
    "platforms": {
        "ios": "8.0",
        "osx": "10.9"
    },
    "source_files": [
        "Dependencies/Incandescence/*.c",
        "HTTPParser/**/*.swift"
    ],
    "xcconfig": {
        "SWIFT_INCLUDE_PATHS": "$(SRCROOT)/HTTPParser/Dependencies"
    },
    "preserve_paths": "Dependencies/*",
    "dependencies": {
        "HTTP": []
    },
    "requires_arc": true
}

Pin It on Pinterest

Share This