Giter Club home page Giter Club logo

swift-http's Introduction

Swift HTTP Server

Simple HTTP implementation for Swift using POSIX socket API. Running on Mac OS X and Linux.

For Mac users: You can install new Swift compiler following this instruction then you will be able to build the code directly on your Xcode.

Compile

Run the following command to compile the source:

swift build

Run

After successfully compile, run the server with:

.build/debug/example

Now, go to http://localhost:8080 to test, the response should be: Hello World


Setting up Docker container from Dockerfile

Go to docker folder and run build.sh to build swiftbox image

cd docker
./build.sh

Run new container from swiftbox image, mount your working directory and expose the port:

docker run -it -p 8080:8080 -v /path/to/your/host/working/folder:/src swiftbox

Now you can go to /src folder, which linked to your /path/to/your/host/working/folder to start using:

cd /src
swift build

Setting up Docker container for Swift yourself

Create new ubuntu container, don't forget to expose port 8080 or whatever you want, to test the HTTP server

docker run -it -p 8080:8080 -v /path/to/your/host/working/folder:/src ubuntu

When you're in the new created ubuntu container, install some dependencies with apt-get:

apt-get update && apt-get install wget build-essential clang rsync libpython-all-dev libedit-dev libicu52 libxml2-dev

Now, download the latest Swift release for Linux:

wget https://swift.org/builds/ubuntu1404/swift-2.2-SNAPSHOT-2015-12-10-a/swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu14.04.tar.gz

Extract the downloaded zip:

tar xzf swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu14.04.tar.gz

Use rsync to move all the packages to your /usr folder:

rsync -a -v swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu14.04/usr/ /usr

You can delete the downloaded files (optional):

rm -rf swift-2.2-SNAPSHOT-2015-12-10-a-ubuntu14.04*

Now, you have Swift installed, test it with the following command:

swift --version

License

The source code is published under MIT license. Please see LICENSE.md for more detail.

swift-http's People

Contributors

adilkhn avatar camelcased avatar davidcelis avatar drnic avatar huytd avatar justinanderson avatar lmumar avatar mclark4386 avatar mz2 avatar superpan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swift-http's Issues

License?

How is your code licensed? Could you add a license file to make it explicit?

[RFC] Add request handlers

What I liked the most about this (now very dated) server written on Obj-C was how easy it was to extend it.

I propose the same for swift-http, that we add a protocol or base class for request handlers that will then be registered with the main class.

Once a request comes in, the HTTPRequest class will take the request and turn it into an object so that you have easy access to things like the HTTP method, request headers, body, URL parameters etc.

We could then either have the main HTTP class loop through an array of registered handlers and let the first handler that returns true for canHandleRequest handle the request (aka producing a response) or the developer adds an if-statement to the main.swift to figure out which handler should handle the request. Both scenarios might want to either default to a handler that returns the file as plain text or 404.

This is a pseudo-sample of what I am imagine, I am already working on a basic FileRequestHandler that simply returns the contents of a file as well as tries to figure out the proper Content-Type header based on the file extension.

The simple handler protocol

public protocol RequestHandlerProtocol {
  static func canHandleRequest(inout request: HTTPRequest) -> Bool
  static func handleRequest(inout request: HTTPRequest, inout response: HTTPResponse)
}

An example handler

public class DefaultRequestHandler: RequestHandlerProtocol {

  public static func canHandleRequest(inout request: HTTPRequest) -> Bool {
    return true
  }

  public static func handleRequest(inout request: HTTPRequest, inout response: HTTPResponse) {
    response.statusCode = 404
    response.body = "404 Not Found"
    response.addHeader("Content-Type", value: "text/plain")
  }
}

Example of the request handling (in http.swift, start function)*

// ...
let request = HTTPRequest(bytes: bufferRcv)
if DefaultRequestHandler.canHandleRequest(&request) {
    DefaultRequestHandler.handleRequest(&request, response: &response)
}
response.write(clientSocket)
// ...

Unable to build

Here is what I get on my MAC:

madhur@Madhurs-MacBook-Pro:~/swift-http (master)
$ swift build
<unknown>:0: error: no such file or directory: 'build'

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.