HTTP Implementation for Swift on Linux and Mac OS X

Overview

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.

Comments
  • Fix error when call swift build

    Fix error when call swift build

    Fix a bug below, /swiftcode/swift-http/Package.swift:4:11: error: single-quoted string literal found, use '"' name: 'swift-http' ^~~~~~~~~~~~ "swift-http" swift-build: exit(1): ["/usr/bin/swiftc", "--driver-mode=swift", "-I", "/usr/lib/swift/pm", "-L", "/usr/lib/swift/pm", "-lPackageDescription", "/swiftcode/swift-http/Package.swift"]

    opened by appsolute 2
  • Move to SemanticVersioning convention

    Move to SemanticVersioning convention

    According to the documentation, Swift packages should follow the SemanticVersioning convention. Currently, this package is not installable as a dependency as the tag is named v0.1-linux.

    I recommend to implement SemanticVersioning so developers will be able to pull it in as a dependency.

    opened by codegefluester 2
  • Unable to build

    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'
    
    opened by madhur 2
  • Updates and added some helpers

    Updates and added some helpers

    *This updates the Dockerfile and README a bit more to fit the current state of the lib.

    *Added a method for generating/sending a basic http header *Added a method for generating/sending a basic http response

    opened by mclark4386 1
  • Split project into two modules allowing for HTTP to be imported into other projects

    Split project into two modules allowing for HTTP to be imported into other projects

    Now one can add the repo as a dependency to their Package.swift file. Then import it into their code.

    If there are any other properties/functions that you think need to be made public, let me know and I'll fix it.

    opened by camelCaseD 1
  • Socket fixes

    Socket fixes

    I was experimenting with deploying this code to a Cloud Foundry instance and ran into a few issues related to the way the sockets were being handled. With these changes it seems to work great!

    • Modify flags so that the system won't send SIGPIPE when the app writes to a socket that has been closed by the other end
    • Use the shutdown call before close to try to get the network stack to send off all its data before tearing down the connection. According to this, just calling shutdown is not guaranteed to be enough, but it seems to be sufficient for this particular case.

    Thanks for sharing this code!

    opened by briancroom 1
  • Reduce size of built Docker image

    Reduce size of built Docker image

    Shrinks the swiftbox image from 1.22 GB to 849.3 MB.

    The result of each Dockerfile line is saved to its own fs layer, so temporary files take up space in the image if they aren't removed in the same line they are created in. Also apt-get clean frees up a little space.

    opened by justinanderson 1
  • Correctly calculate Content-Length as a byte size

    Correctly calculate Content-Length as a byte size

    The HTTP server currently is calculating Content-Length as a naive character count, but the header is supposed to be the byte size of the response contents. In the case of "Hello World", the two are equivalent, but, if the response contained special characters, the Content-Length would be incorrect:

    print("moose".characters.count) // 5
    print("møøse".characters.count) // 5
    print("møøse".utf8.count) // 7
    

    For semantic reasons, and for people who are copying the source to serve their own custom responses, we should provide the correct byte length.

    Signed-off-by: David Celis [email protected]

    opened by davidcelis 1
  • Use double quotes in Package.swift to avoid a swift build error.

    Use double quotes in Package.swift to avoid a swift build error.

    At least on Ubuntu with the latest Swift & package manager build, I would get the following error before this modification:

    /home/mz2/Projects/swift-http/Package.swift:4:11: error: single-quoted string literal found, use '"'
        name: 'swift-http'
              ^~~~~~~~~~~~
              "swift-http"
    swift-build: exit(1): ["/home/mz2/usr/bin/swiftc", "--driver-mode=swift", "-I", "/home/mz2/usr/lib/swift/pm", "-L", "/home/mz2/usr/lib/swift/pm", "-lPackageDescription", "/home/mz2/Projects/swift-http/Package.swift"]
    
    opened by mz2 0
  • [RFC] Add request handlers

    [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)
    // ...
    
    opened by codegefluester 0
Releases(0.0.1)
Owner
Huy
Huy
A small, lightweight, embeddable HTTP server for Mac OS X or iOS applications

CocoaHTTPServer CocoaHTTPServer is a small, lightweight, embeddable HTTP server for Mac OS X or iOS applications. Sometimes developers need an embedde

Robbie Hanson 5.5k Jan 7, 2023
Lightweight library for web server applications in Swift on macOS and Linux powered by coroutines.

Why Zewo? • Support • Community • Contributing Zewo Zewo is a lightweight library for web applications in Swift. What sets Zewo apart? Zewo is not a w

Zewo 1.9k Dec 22, 2022
A Ruby on Rails inspired Web Framework for Swift that runs on Linux and OS X

IMPORTANT! We don't see any way how to make web development as great as Ruby on Rails or Django with a very static nature of current Swift. We hope th

Saulius Grigaitis 2k Dec 5, 2022
Swift backend / server framework (Pure Swift, Supports Linux)

NetworkObjects NetworkObjects is a #PureSwift backend. This framework compiles for OS X, iOS and Linux and serves as the foundation for building power

Alsey Coleman Miller 258 Oct 6, 2022
PillowTalk - An iOS & SwiftUI server monitor tool for linux based machines using remote proc file system with script execution.

An iOS & SwiftUI server monitor tool for linux based machines using remote proc file system with script execution.

Lakr Aream 416 Dec 16, 2022
A Swift web framework and HTTP server.

A Swift Web Framework and HTTP Server Summary Kitura is a web framework and web server that is created for web services written in Swift. For more inf

Kitura 7.6k Dec 27, 2022
GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps.

GCDWebServer is a modern and lightweight GCD based HTTP 1.1 server designed to be embedded in iOS, macOS & tvOS apps. It was written from scr

Pierre-Olivier Latour 6.3k Dec 27, 2022
Tiny http server engine written in Swift programming language.

What is Swifter? Tiny http server engine written in Swift programming language. Branches * stable - lands on CocoaPods and others. Supports the latest

null 3.6k Dec 31, 2022
Swift HTTP server using the pre-fork worker model

Curassow Curassow is a Swift Nest HTTP Server. It uses the pre-fork worker model and it's similar to Python's Gunicorn and Ruby's Unicorn. It exposes

Kyle Fuller Archive 397 Oct 30, 2022
💧 A server-side Swift HTTP web framework.

Vapor is an HTTP web framework for Swift. It provides a beautifully expressive and easy-to-use foundation for your next website, API, or cloud project

Vapor 22.4k Jan 3, 2023
libuv base Swift web HTTP server framework

Notice Trevi now open a Trevi Community. Yoseob/Trevi project split up into respective Trevi, lime, middlewares and sys packages at our community. If

leeyoseob 46 Jan 29, 2022
A lightweight library for writing HTTP web servers with Swift

Taylor Disclaimer: Not actively working on it anymore. You can check out some alternatives Swift 2.0 required. Working with Xcode 7.1. Disclaimer: It

Jorge Izquierdo 925 Nov 17, 2022
Tiny http server engine written in Swift programming language.

What is Swifter? Tiny http server engine written in Swift programming language. Branches * stable - lands on CocoaPods and others. Supports the latest

null 3.6k Jan 5, 2023
Server-side Swift. The Perfect core toolset and framework for Swift Developers. (For mobile back-end development, website and API development, and more…)

Perfect: Server-Side Swift 简体中文 Perfect: Server-Side Swift Perfect is a complete and powerful toolbox, framework, and application server for Linux, iO

PerfectlySoft Inc. 13.9k Dec 29, 2022
A minimal, fast and unopinionated web framework for Swift

![Fire Image] (http://i.imgur.com/1qR6Nl4.png) Blackfire An extremely fast Swift web framework ?? Getting Started If you're familiar with express.js t

Elliott Minns 908 Dec 2, 2022
A Swift Multiplatform Single-threaded Non-blocking Web and Networking Framework

Serverside non-blocking IO in Swift Ask questions in our Slack channel! Lightning (formerly Edge) Node Lightning is an HTTP Server and TCP Client/Serv

SkyLab 316 Oct 6, 2022
Swift Express is a simple, yet unopinionated web application server written in Swift

Documentation <h5 align="right"><a href="http://demo.swiftexpress.io/">Live ?? server running Demo <img src="https://cdn0.iconfinder.com/data/icons/

Crossroad Labs 850 Dec 2, 2022
Simple server APIs in Swift

Simple server APIs in Swift

null 4 Apr 25, 2022
🪶 Feather is a modern Swift-based content management system powered by Vapor 4.

Feather CMS ?? ?? Feather is a modern Swift-based content management system powered by Vapor 4. ?? Click to join the chat on Discord. Requirements To

Phu Huynh 0 Oct 20, 2021