GithubHelp home page GithubHelp logo

chriseidhof / httpserver Goto Github PK

View Code? Open in Web Editor NEW

This project forked from danieleggert/httpserver

0.0 2.0 0.0 169 KB

A minimalistic embedded HTTP server written in Swift.

License: ISC License

C++ 1.61% Swift 87.62% Objective-C 10.77%

httpserver's Introduction

HTTPServer

A simple embedded HTTP server written in Swift.

Goals

This framework provides an embeddable HTTP/1.1 server. It was written to be used for testing NSURLSession based code in XCTest based test setups.

Non-Goals

This is not a replacement for Nginx.

This does not implement serving any files etc. It simply exposes an API for clients of the framework to fill in as needed.

The point is to not provide any HTTP routing. This framework is intentionally simple and bare bones (A µ-Framework if you will) -- to be used as a building block for more complex setups.

How To Use

The test shows how this can be used. A SocketServer in instanciated and the httpConnectionHandler() function passed to it to handle new incomming connections like so:

let q = dispatch_get_global_queue(0, 0)
SocketServer.withAcceptHandler {
    (channel, clientAddress) in
    httpConnectionHandler(channel, clientAddress, q) {
        (request, clientAddress, response) in
        println("Request from \(clientAddress)")
        var r = HTTPResponse(statusCode: 200, statusDescription: "Ok")
        r.bodyData = "hey".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
        r.setHeaderField("Content-Length", value: "\(r.bodyData.length)")
        r.setHeaderField("Foo", value: "Bar")
        response(r)
    }
    println("New connection")
}.analysis(ifSuccess: {
    server = $0
}, ifFailure: {
    fatalError("Unable to create HTTP server: \($0)")
})

Note how the inner most handler receives a HTTPRequest and in response calls the response handler with a HTTPResponse it generates.

Known Issues

  • Better documentaiton. ;)
  • No support for HTTP persistent connection. This could be added.
  • Clean up GCD queue usage. Currently this is a bit confusing and not streamlined.
  • Better shutdown support. It may be good to be able to wait for the server to have shut down, e.g. by exposing a dispatch_group_t.

Please file issues or send pull requests.

License

Permissive ISC License

httpserver's People

Contributors

danieleggert avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.