All Projects → AlwaysRightInstitute → Swiftyhttp

AlwaysRightInstitute / Swiftyhttp

Licence: mit
A simple GCD based HTTP client and server, written in 'pure' Swift

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Swiftyhttp

Proxy.py
⚡⚡⚡Fast, Lightweight, Pluggable, TLS interception capable proxy server focused on Network monitoring, controls & Application development, testing, debugging
Stars: ✭ 1,291 (+1022.61%)
Mutual labels:  http-server
Restbed
Corvusoft's Restbed framework brings asynchronous RESTful functionality to C++14 applications.
Stars: ✭ 1,551 (+1248.7%)
Mutual labels:  http-server
Zetaipc
A tiny .NET library to do inter-process communication (IPC) between different processes on the same machine.
Stars: ✭ 111 (-3.48%)
Mutual labels:  http-server
T Io
解决其它网络框架没有解决的用户痛点,让天下没有难开发的网络程序
Stars: ✭ 1,331 (+1057.39%)
Mutual labels:  http-server
Violetear
Go HTTP router
Stars: ✭ 100 (-13.04%)
Mutual labels:  http-server
Netty Rest
Yet another high performance REST server based on Netty
Stars: ✭ 107 (-6.96%)
Mutual labels:  http-server
Tinyhttp
🦄 0-legacy, tiny & fast web framework as a replacement of Express
Stars: ✭ 1,259 (+994.78%)
Mutual labels:  http-server
Purescript Httpure
A web framework written in PureScript.
Stars: ✭ 112 (-2.61%)
Mutual labels:  http-server
Gaea
Gaea is a Gin-based web framework, reference gin https://github.com/gin-gonic/gin
Stars: ✭ 105 (-8.7%)
Mutual labels:  http-server
Srv
minimalist http(s) server and file browser
Stars: ✭ 109 (-5.22%)
Mutual labels:  http-server
Meinheld
Meinheld is a high performance asynchronous WSGI Web Server (based on picoev)
Stars: ✭ 1,339 (+1064.35%)
Mutual labels:  http-server
Cherrypy
CherryPy is a pythonic, object-oriented HTTP framework. https://docs.cherrypy.org/
Stars: ✭ 1,363 (+1085.22%)
Mutual labels:  http-server
Httpbin
HTTP Request & Response Service, written in Python + Flask.
Stars: ✭ 10,423 (+8963.48%)
Mutual labels:  http-server
Searchspot
The service responsible for Honeypot's ElasticSearch data
Stars: ✭ 90 (-21.74%)
Mutual labels:  http-server
Reverse proxy plug
🔛 an Elixir reverse proxy Plug with HTTP/2, chunked transfer and path proxying support
Stars: ✭ 112 (-2.61%)
Mutual labels:  http-server
Piping Server Rust
Infinitely transfer between any device over pure HTTP, designed for everyone using Unix pipe and even for browser users
Stars: ✭ 88 (-23.48%)
Mutual labels:  http-server
Mofuw
mofuw is *MO*re *F*aster, *U*ltra minimal *W*ebserver.
Stars: ✭ 107 (-6.96%)
Mutual labels:  http-server
Hummingbird
Lightweight, flexible HTTP server framework written in Swift
Stars: ✭ 114 (-0.87%)
Mutual labels:  http-server
Qhttpengine
HTTP server for Qt applications
Stars: ✭ 112 (-2.61%)
Mutual labels:  http-server
Daraja Framework
A lightweight HTTP server framework for Object Pascal (Delphi 2009+/Free Pascal 3.0) based on Indy
Stars: ✭ 108 (-6.09%)
Mutual labels:  http-server

SwiftyHTTP

Note: I'm probably not going to update this any further - If you need a Swift networking toolset for the server side, consider: Macro.swift.

A simple GCD based HTTP library for Swift. This project is 'pure' Swift/C, it does not use any bridged Objective-C classes.

SwiftyHTTP is a demo on how to integrate Swift with raw C APIs. More for stealing Swift coding ideas than for actually using the code in a real project. In most real world Swift apps you have access to Cocoa, use it.

Note: This is just my second Swift project. Any suggestions on how to improve the code are welcome. I expect lots and lots :-)

First things first: Samples

Server:

let httpd = HTTPServer()
  .onRequest {
    rq, res, con in
    res.bodyAsString = "<h2>Always Right, Never Wrong!</h2>"
    con.sendResponse(res)
  }
  .listen(1337)

Server using the Node.JS like Connect bonus class:

let httpd = Connect()
  .use { rq, res, _, next in
    print("\(rq.method) \(rq.url) \(res.status)")
    next()
  }
  .use("/hello") { rq, res, con, next in
    res.bodyAsString = "Hello!"
    con.sendResponse(res)
  }
  .use("/") { rq, res, con, next in
    res.bodyAsString = "Always, almost sometimes."
    con.sendResponse(res)
  }
  .listen(1337)

Client (do not use this, use NSURLSession!):

GET("http://www.apple.com/")
  .done {
    print()
    print("request  \($0)")
    print("response \($1)")
    print("body:\n\($1.bodyAsString)")
  }
  .fail {
    print("failed \($0): \($1)")
  }
  .always { print("---") }

Targets

Updated to use Swift v0.2.2 (aka Xcode 7.3).

The project includes three targets:

  • SwiftyHTTP
  • SwiftyServer
  • SwiftyClient

I suggest you start out looking at the SwiftyServer.

SwiftyHTTP

A framework containing the HTTP classes and relevant extensions. It has a few 'subprojects':

  • Foundation
  • Sockets
  • Parser
  • HTTP
Foundation

This has just the 'RawByteBuffer' class. Which is kinda like a UInt8 array. I bet there are better ways to implement this! Please suggest some! :-)

Also a few - highly inefficient - extensions to convert between String's and CString's. I would love some suggestions on those as well.

But remember: NSxyz is forbidden for this venture! :-)

Sockets

Just a local copy of the SwiftSockets project - I wish GIT had proper externals ;-) (https://github.com/AlwaysRightInstitute/SwiftSockets)

Parser

This uses the C HTTP parser which is also used in Node.JS. It had to modified a tinsy bit - the Swift C bridge doesn't support bitfields. Those had to be removed from the http_parser struct.

It also contains the main request/response classes: HTTPRequest and HTTPResponse, both subclasses of HTTPMessage. And enums for HTTP status values (like 💰Required) and request methods (GET etc).

HTTP

HTTPConnectionPool is an abstract base class and manages open connections, either incoming or outgoing. The HTTPConnection sits on top of the SwiftSockets and manages one HTTP connection (it connects the socket to the parser).

HTTPServer is the server class. Uses SwiftSockets to listen for incoming connections. See above for a sample.

As a bonus - this also has a tiny Connect class - which is modelled after the Node.JS Connect thingy (which in turn is apparently modelled after RoR Rack). It allows you to hook up a set of blocks for request processing, instead of having just a single entry point. Not sure I like that stuff, but it seems to fit into Swift quite well. Find a sample above.

Finally there is a simple HTTP client. Doesn't do anything fancy. Do not - ever

  • use this. Use NSURLSession and companions.

SwiftyServer

Great httpd server - great in counting the requests it got sent. This is not actually serving any files ;-) Comes along as a Cocoa app. Compile it, run it, then connect to it in the browser via http://127.0.0.1:1337/Awesome-O!

SwiftyClient

Just a demo on how to do HTTP requests via SwiftyHTTP. No, it doesn't do JSON decoding and such.

Again: You do NOT want to use it in a real iOS/OSX app! Use NSURLSession and companions - it gives you plenty of extra features you want to have for realz.

Goals

  • [x] Max line length: 80 characters
  • [ ] Great error handling
    • [x] PS style great error handling
    • [x] print() error handling
    • [ ] Swift 2 try/throw/catch
      • [ ] Real error handling
  • [x] Twisted (no blocking reads or writes)
    • [x] Async reads and writes
      • [x] Never block on reads
      • [x] Never block on listen
    • [ ] Async connect()
  • [x] No NS'ism
  • [ ] Use as many language features Swift provides
    • [x] Generics
      • [x] Generic function
      • [x] typealias
    • [x] Closures
      • [x] weak self
      • [x] trailing closures
      • [x] implicit parameters
    • [x] Unowned
    • [x] Extensions on structs
    • [x] Extensions to organize classes
    • [x] Protocols on structs
    • [ ] Swift 2 protocol extensions
    • [x] Tuples
    • [x] Trailing closures
    • [ ] @Lazy
    • [x] Pure Swift weak delegates via @class
    • [x] Optionals
      • [x] Implicitly unwrapped optionals
    • [x] Convenience initializers
    • [x] Failable initializers
    • [x] Class variables on structs
    • [x] CConstPointer, CConstVoidPointer
      • [x] withCString {}
    • [x] UnsafePointer
    • [x] sizeof()
    • [x] Standard Protocols
      • [x] Printable
      • [x] BooleanType (aka LogicValue)
      • [x] OutputStreamType
      • [x] Equatable
        • [x] Equatable on Enums with Associated Values
      • [x] Hashable
      • [x] SequenceType (GeneratorOf)
      • [x] Literal Convertibles
        • [x] StringLiteralConvertible
        • [x] IntegerLiteralConvertible
    • [x] Left shift AND right shift
    • [x] Enums on steroids
      • [x] RawRepresentable
    • [ ] Dynamic type system, reflection
    • [x] Operator overloading
    • [x] UCS-4 identifiers (🐔🐔🐔)
    • [ ] RTF source code with images and code sections in different fonts
    • [x] Nested classes/types
    • [ ] Patterns
      • [x] Use wildcard pattern to ignore value
    • [ ] @autoclosure
    • [ ] unsafeBitCast (was reinterpretCast)
    • [x] final
    • [x] Nil coalescing operator
    • [ ] dynamic
    • [ ] Swift 2
      • [ ] availability
      • [x] guard
      • [x] defer
      • [ ] C function pointers
      • [x] debugPrint
      • [x] lowercaseString
    • [x] #if os(Linux)
    • [ ] #if swift(>=2.2)
  • [ ] Swift Package Manager
    • [ ] GNUmakefile support
  • [ ] Linux support

Why?!

This is an experiment to get acquainted with Swift. To check whether something real can be implemented in 'pure' Swift. Meaning, without using any Objective-C Cocoa classes (no NS'ism). Or in other words: Can you use Swift without writing all the 'real' code in wrapped Objective-C? :-)

Contact

@helje5 | [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].