├── README.md ├── Example Server ├── Public │ ├── favicon.ico │ ├── favicon.png │ ├── img │ │ ├── header.jpg │ │ └── portfolio │ │ │ ├── 1.jpg │ │ │ ├── 2.jpg │ │ │ ├── 3.jpg │ │ │ ├── 4.jpg │ │ │ ├── 5.jpg │ │ │ └── 6.jpg │ ├── font-awesome │ │ └── fonts │ │ │ ├── FontAwesome.otf │ │ │ ├── fontawesome-webfont.eot │ │ │ ├── fontawesome-webfont.ttf │ │ │ ├── fontawesome-webfont.woff │ │ │ └── fontawesome-webfont.woff2 │ ├── fonts │ │ ├── glyphicons-halflings-regular.eot │ │ ├── glyphicons-halflings-regular.ttf │ │ ├── glyphicons-halflings-regular.woff │ │ └── glyphicons-halflings-regular.woff2 │ ├── js │ │ ├── cbpAnimatedHeader.js │ │ ├── jquery.fittext.js │ │ ├── creative.js │ │ └── classie.js │ └── login.html ├── Views │ ├── user.html │ └── tasks.mustache ├── main.h ├── Deliver.swift ├── Authentication │ └── Authenticator.swift ├── Responders │ ├── ParametersResponder.swift │ ├── LoginResponder.swift │ ├── JSONResponder.swift │ └── DatabaseResponder.swift └── main.swift ├── HTTP.xcodeproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── Swifter.xcscmblueprint ├── .gitignore ├── Server Tests ├── HTTPResponse+Test.swift ├── Info.plist └── ServerTests.swift ├── LICENSE.md ├── Middleware └── Middleware.swift ├── Responder └── Responder.swift ├── Libuv ├── Handle.swift ├── Libuv.h ├── include │ ├── uv-threadpool.h │ ├── uv-version.h │ └── uv-darwin.h ├── Buffer.swift ├── TCPStream.swift ├── SocketAddress.swift ├── UVError.swift ├── src │ ├── version.c │ └── unix │ │ ├── spinlock.h │ │ ├── atomic-ops.h │ │ ├── dl.c │ │ └── proctitle.c ├── Pack.swift ├── Libuv.swift └── UVRunLoop.swift ├── Run Loop └── RunLoop.swift ├── Keep Alive Type └── KeepAliveType.swift ├── Example Client ├── main.h └── main.swift ├── Dispatch └── Dispatch.h ├── HTTP Parser └── HTTPParser.h ├── Socket └── Socket.h ├── HTTP Router ├── HTTPRouter.h ├── HTTPMethodRouter.swift └── HTTPPathRouter.swift ├── Parameterizable └── Parameterizable.swift ├── HTTP Server ├── HTTPServer.h └── HTTPServer.swift ├── Regular Expression └── RegularExpression.h ├── Socket Stream ├── SocketStream.h └── SocketStream.swift ├── UV HTTP Server ├── UVHTTPServer.h └── UVHTTPServer.swift ├── Respondable └── Respondable.swift ├── HTTP Client ├── HTTPClient.h └── HTTPClient.swift ├── JSON ├── JSONParser+Data.swift ├── JSONParseError.swift └── StringUtils.swift ├── PostgreSQL └── PostgreSQL.h ├── FakeRunLoop └── FakeRunLoop.swift ├── FastCGIServer ├── FastCGIServer.h └── FastCGIServer.c ├── Redirect Responder └── RedirectResponder.swift ├── Stream └── Stream.swift ├── Text Response └── HTTPResponse+Text.swift ├── JSON Response └── HTTPResponse+JSON.swift ├── Parameters Middleware └── ParametersMiddleware.swift ├── Mustache ├── Error │ └── MustacheError.swift ├── Compiling │ └── TemplateAST │ │ ├── VariableTag.swift │ │ ├── SectionTag.swift │ │ └── TemplateAST.swift ├── Rendering │ └── ExpressionInvocation.swift ├── Parsing │ └── TemplateToken.swift ├── Goodies │ ├── HTMLEscape.swift │ └── URLEscape.swift └── Generation │ └── ExpressionGenerator.swift ├── Keep Alive Midleware └── KeepAliveMiddleware.swift ├── HTTP Serializer ├── HTTPRequestSerializer.swift └── HTTPResponseSerializer.swift ├── URI └── URI.swift ├── Headers Middleware └── HeadersMiddleware.swift ├── File Responder └── FileResponder.swift ├── Logger Middleware └── LoggerMiddleware.swift ├── URL Encoded Parser Middleware └── FormURLEncodedParserMiddleware.swift ├── Error └── Error.swift ├── Lexicon └── Dictionary+Lexicon.swift ├── Anthology └── CollectionType+Anthology.swift ├── HTML Response └── HTTPResponse+HTML.swift ├── Text Parser Middleware └── TextParserMiddleware.swift ├── File Response └── HTTPResponse+File.swift ├── JSON Parser Middleware └── JSONParserMiddleware.swift ├── Template Response └── HTTPResponse+Template.swift ├── Media Type └── MediaType.swift ├── Basic Authentication Middleware └── BasicAuthenticationMiddleware.swift ├── HTTP Error └── HTTPError.swift ├── Server └── Server.swift ├── Server Router └── ServerRouter.swift ├── File └── File.swift ├── Request Middleware Result └── RequestMiddlewareResult.swift ├── Data └── Data.swift └── HTTP Method └── HTTPMethod.swift /README.md: -------------------------------------------------------------------------------- 1 | [Deprecated] 2 | -------------------------------------------------------------------------------- /Example Server/Public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/favicon.ico -------------------------------------------------------------------------------- /Example Server/Public/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/favicon.png -------------------------------------------------------------------------------- /Example Server/Public/img/header.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/header.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/1.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/2.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/3.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/4.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/5.jpg -------------------------------------------------------------------------------- /Example Server/Public/img/portfolio/6.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/img/portfolio/6.jpg -------------------------------------------------------------------------------- /Example Server/Public/font-awesome/fonts/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/font-awesome/fonts/FontAwesome.otf -------------------------------------------------------------------------------- /Example Server/Public/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /Example Server/Public/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /Example Server/Public/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /Example Server/Public/font-awesome/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/font-awesome/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /Example Server/Public/font-awesome/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/font-awesome/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /Example Server/Public/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /Example Server/Public/font-awesome/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/font-awesome/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /Example Server/Public/font-awesome/fonts/fontawesome-webfont.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZewoGraveyard/SwiftHTTPServer/HEAD/Example Server/Public/font-awesome/fonts/fontawesome-webfont.woff2 -------------------------------------------------------------------------------- /HTTP.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Swift ### 2 | # Xcode 3 | # 4 | build/ 5 | Build/ 6 | *.pbxuser 7 | !default.pbxuser 8 | *.mode1v3 9 | !default.mode1v3 10 | *.mode2v3 11 | !default.mode2v3 12 | *.perspectivev3 13 | !default.perspectivev3 14 | xcuserdata 15 | *.xccheckout 16 | *.moved-aside 17 | DerivedData 18 | *.hmap 19 | *.ipa 20 | *.xcuserstate -------------------------------------------------------------------------------- /Server Tests/HTTPResponse+Test.swift: -------------------------------------------------------------------------------- 1 | extension HTTPResponse { 2 | 3 | var isJSON: Bool { 4 | 5 | guard let contentType = headers["content-type"] else { 6 | 7 | return false 8 | 9 | } 10 | 11 | return contentType == "application/json" 12 | 13 | } 14 | 15 | func equalsJSON(candidateJSON: JSON) -> Bool { 16 | 17 | if !isJSON { 18 | 19 | return false 20 | 21 | } 22 | 23 | guard let responseJSON = try? JSONParser.parse(body) else { 24 | 25 | return false 26 | 27 | } 28 | 29 | return candidateJSON == responseJSON 30 | 31 | } 32 | 33 | } -------------------------------------------------------------------------------- /Example Server/Views/user.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | User 6 | 7 | 8 |

Url:

9 |
{{ URI }}
10 |

Method: {{ method }}

11 |

Headers:

12 | {{# each(headers) }} 13 |
{{ @key }}: {{.}}
14 | {{/}} 15 |

Params:

16 | {{# each(params) }} 17 |
{{ @key }}: {{.}}
18 | {{/}} 19 |

Data:

20 | {{# each(data) }} 21 |
{{ @key }}: {{.}}
22 | {{/}} 23 | 24 | -------------------------------------------------------------------------------- /Server Tests/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | 1 23 | 24 | 25 | -------------------------------------------------------------------------------- /Example Server/Views/tasks.mustache: -------------------------------------------------------------------------------- 1 | { 2 | "collection" : { 3 | "href": "{{ site }}", 4 | "version": "1.0", 5 | "links": [ 6 | {"rel" : "author", "href" : "mailto:paulo.faria.rl@gmail.com", "prompt" : "Author"}, 7 | {"rel" : "queries", "href" : "{{ site }};queries", "prompt" : "Queries"}, 8 | {"rel" : "template", "href" : "{{ site }};template", "prompt" : "Template"} 9 | ], 10 | "items": [ 11 | {{# each(items) }} 12 | { 13 | "href" : "{{ site }}{{ .id }}", 14 | "data" : [ 15 | {"name" : "description", "value" : "{{ .description }}", "prompt" : "Description"}, 16 | {"name" : "completed", "value" : "{{ .completed }}", "prompt" : "Completed"}, 17 | {"name" : "dateDue", "value" : "{{ .dueDate }}", "prompt" : "Due Date"} 18 | ] 19 | }, 20 | {{/}} 21 | {} 22 | ] 23 | } 24 | } -------------------------------------------------------------------------------- /Example Server/Public/js/cbpAnimatedHeader.js: -------------------------------------------------------------------------------- 1 | /** 2 | * cbpAnimatedHeader.js v1.0.0 3 | * http://www.codrops.com 4 | * 5 | * Licensed under the MIT license. 6 | * http://www.opensource.org/licenses/mit-license.php 7 | * 8 | * Copyright 2013, Codrops 9 | * http://www.codrops.com 10 | */ 11 | var cbpAnimatedHeader = (function() { 12 | 13 | var docElem = document.documentElement, 14 | header = document.querySelector( '.navbar-default' ), 15 | didScroll = false, 16 | changeHeaderOn = 300; 17 | 18 | function init() { 19 | window.addEventListener( 'scroll', function( event ) { 20 | if( !didScroll ) { 21 | didScroll = true; 22 | setTimeout( scrollPage, 250 ); 23 | } 24 | }, false ); 25 | } 26 | 27 | function scrollPage() { 28 | var sy = scrollY(); 29 | if ( sy >= changeHeaderOn ) { 30 | classie.add( header, 'navbar-shrink' ); 31 | } 32 | else { 33 | classie.remove( header, 'navbar-shrink' ); 34 | } 35 | didScroll = false; 36 | } 37 | 38 | function scrollY() { 39 | return window.pageYOffset || docElem.scrollTop; 40 | } 41 | 42 | init(); 43 | 44 | })(); -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Zewo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Middleware/Middleware.swift: -------------------------------------------------------------------------------- 1 | // Middleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct Middleware {} 26 | -------------------------------------------------------------------------------- /Responder/Responder.swift: -------------------------------------------------------------------------------- 1 | // Responder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct Responder {} 26 | -------------------------------------------------------------------------------- /Example Server/Public/js/jquery.fittext.js: -------------------------------------------------------------------------------- 1 | /*global jQuery */ 2 | /*! 3 | * FitText.js 1.2 4 | * 5 | * Copyright 2011, Dave Rupert http://daverupert.com 6 | * Released under the WTFPL license 7 | * http://sam.zoy.org/wtfpl/ 8 | * 9 | * Date: Thu May 05 14:23:00 2011 -0600 10 | */ 11 | 12 | (function( $ ){ 13 | 14 | $.fn.fitText = function( kompressor, options ) { 15 | 16 | // Setup options 17 | var compressor = kompressor || 1, 18 | settings = $.extend({ 19 | 'minFontSize' : Number.NEGATIVE_INFINITY, 20 | 'maxFontSize' : Number.POSITIVE_INFINITY 21 | }, options); 22 | 23 | return this.each(function(){ 24 | 25 | // Store the object 26 | var $this = $(this); 27 | 28 | // Resizer() resizes items based on the object width divided by the compressor * 10 29 | var resizer = function () { 30 | $this.css('font-size', Math.max(Math.min($this.width() / (compressor*10), parseFloat(settings.maxFontSize)), parseFloat(settings.minFontSize))); 31 | }; 32 | 33 | // Call once to set. 34 | resizer(); 35 | 36 | // Call on resize. Opera debounces their resize by default. 37 | $(window).on('resize.fittext orientationchange.fittext', resizer); 38 | 39 | }); 40 | 41 | }; 42 | 43 | })( jQuery ); 44 | -------------------------------------------------------------------------------- /Libuv/Handle.swift: -------------------------------------------------------------------------------- 1 | // Handle.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | typealias HandleRef = UnsafeMutablePointer -------------------------------------------------------------------------------- /Run Loop/RunLoop.swift: -------------------------------------------------------------------------------- 1 | // RunLoop.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol RunLoop { 26 | 27 | func run() 28 | func close() 29 | 30 | } -------------------------------------------------------------------------------- /Libuv/Libuv.h: -------------------------------------------------------------------------------- 1 | // Libuv.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef Libuv_h 26 | #define Libuv_h 27 | 28 | #include "uv.h" 29 | 30 | #endif /* Libuv_h */ 31 | -------------------------------------------------------------------------------- /Keep Alive Type/KeepAliveType.swift: -------------------------------------------------------------------------------- 1 | // KeepAliveType.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol KeepAliveType { 26 | 27 | var keepAlive: Bool { get } 28 | 29 | } -------------------------------------------------------------------------------- /Example Client/main.h: -------------------------------------------------------------------------------- 1 | // main.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef BridgingHeader_h 26 | #define BridgingHeader_h 27 | 28 | #include "HTTPClient.h" 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /Dispatch/Dispatch.h: -------------------------------------------------------------------------------- 1 | // Dispatch.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef Dispatch_h 26 | #define Dispatch_h 27 | 28 | #include 29 | 30 | #endif /* Dispatch_h */ 31 | -------------------------------------------------------------------------------- /HTTP Parser/HTTPParser.h: -------------------------------------------------------------------------------- 1 | // HTTPParser.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef HTTPParser_h 26 | #define HTTPParser_h 27 | 28 | #include "http_parser.h" 29 | 30 | #endif /* HTTPParser_h */ 31 | -------------------------------------------------------------------------------- /Socket/Socket.h: -------------------------------------------------------------------------------- 1 | // Socket.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef Socket_h 26 | #define Socket_h 27 | 28 | #include 29 | #include 30 | 31 | #endif /* Socket_h */ 32 | -------------------------------------------------------------------------------- /HTTP Router/HTTPRouter.h: -------------------------------------------------------------------------------- 1 | // HTTPParser.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef HTTPRouter_h 26 | #define HTTPRouter_h 27 | 28 | #include "RegularExpression.h" 29 | 30 | #endif /* HTTPRouter_h */ 31 | -------------------------------------------------------------------------------- /Parameterizable/Parameterizable.swift: -------------------------------------------------------------------------------- 1 | // Parameterizable.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | //protocol Parameterizable { 26 | // 27 | // var parameters: [String: String] { get set } 28 | // 29 | //} -------------------------------------------------------------------------------- /HTTP Server/HTTPServer.h: -------------------------------------------------------------------------------- 1 | // HTTPServer.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef HTTPServer_h 26 | #define HTTPServer_h 27 | 28 | #include "SocketStream.h" 29 | #include "HTTPParser.h" 30 | 31 | #endif /* HTTPServer_h */ 32 | -------------------------------------------------------------------------------- /Regular Expression/RegularExpression.h: -------------------------------------------------------------------------------- 1 | // RegularExpression.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef RegularExpression_h 26 | #define RegularExpression_h 27 | 28 | #include 29 | 30 | #endif /* RegularExpression_h */ 31 | -------------------------------------------------------------------------------- /Socket Stream/SocketStream.h: -------------------------------------------------------------------------------- 1 | // SocketStream.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef SocketStream_h 26 | #define SocketStream_h 27 | 28 | #include "Dispatch.h" 29 | #include "Socket.h" 30 | 31 | #endif /* SocketStream_h */ 32 | -------------------------------------------------------------------------------- /UV HTTP Server/UVHTTPServer.h: -------------------------------------------------------------------------------- 1 | // UVHTTPServer.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef UVHTTPServer_h 26 | #define UVHTTPServer_h 27 | 28 | #include "Libuv.h" 29 | #include "HTTPParser.h" 30 | 31 | #endif /* UVHTTPServer_h */ 32 | -------------------------------------------------------------------------------- /Respondable/Respondable.swift: -------------------------------------------------------------------------------- 1 | // Respondable.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol Respondable { 26 | 27 | typealias Request 28 | typealias Response 29 | 30 | var respond: Request throws -> Response { get } 31 | 32 | } -------------------------------------------------------------------------------- /HTTP Client/HTTPClient.h: -------------------------------------------------------------------------------- 1 | // HTTPClient.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef HTTPClient_h 26 | #define HTTPClient_h 27 | 28 | #include "HTTPParser.h" 29 | #include "Dispatch.h" 30 | #include "Socket.h" 31 | 32 | #endif /* HTTPClient_h */ 33 | -------------------------------------------------------------------------------- /Example Server/main.h: -------------------------------------------------------------------------------- 1 | // main.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef BridgingHeader_h 26 | #define BridgingHeader_h 27 | 28 | #include "HTTPServer.h" 29 | #include "UVHTTPServer.h" 30 | #include "FastCGIServer.h" 31 | 32 | #include "HTTPRouter.h" 33 | 34 | #endif 35 | -------------------------------------------------------------------------------- /JSON/JSONParser+Data.swift: -------------------------------------------------------------------------------- 1 | // JSONParser+Data.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension JSONParser { 26 | 27 | public static func parse(source: Data) throws -> JSON { 28 | 29 | return try GenericJSONParser(source.bytes).parse() 30 | 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Example Server/Public/js/creative.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * Start Bootstrap - Creative Bootstrap Theme (http://startbootstrap.com) 3 | * Code licensed under the Apache License v2.0. 4 | * For details, see http://www.apache.org/licenses/LICENSE-2.0. 5 | */ 6 | 7 | (function($) { 8 | "use strict"; // Start of use strict 9 | 10 | // jQuery for page scrolling feature - requires jQuery Easing plugin 11 | $('a.page-scroll').bind('click', function(event) { 12 | var $anchor = $(this); 13 | $('html, body').stop().animate({ 14 | scrollTop: ($($anchor.attr('href')).offset().top - 50) 15 | }, 1250, 'easeInOutExpo'); 16 | event.preventDefault(); 17 | }); 18 | 19 | // Highlight the top nav as scrolling occurs 20 | $('body').scrollspy({ 21 | target: '.navbar-fixed-top', 22 | offset: 51 23 | }) 24 | 25 | // Closes the Responsive Menu on Menu Item Click 26 | $('.navbar-collapse ul li a').click(function() { 27 | $('.navbar-toggle:visible').click(); 28 | }); 29 | 30 | // Fit Text Plugin for Main Header 31 | $("h1").fitText( 32 | 1.2, { 33 | minFontSize: '35px', 34 | maxFontSize: '65px' 35 | } 36 | ); 37 | 38 | // Offset for Main Navigation 39 | $('#mainNav').affix({ 40 | offset: { 41 | top: 100 42 | } 43 | }) 44 | 45 | // Initialize WOW.js Scrolling Animations 46 | new WOW().init(); 47 | 48 | })(jQuery); // End of use strict 49 | -------------------------------------------------------------------------------- /PostgreSQL/PostgreSQL.h: -------------------------------------------------------------------------------- 1 | // PostgreSQL.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef PostgreSQL_h 26 | #define PostgreSQL_h 27 | 28 | #ifdef __APPLE__ 29 | #if !TARGET_OS_IPHONE && !TARGET_IPHONE_SIMULATOR && TARGET_OS_MAC 30 | #include 31 | #endif 32 | #endif 33 | 34 | #endif /* PostgreSQL_h */ 35 | -------------------------------------------------------------------------------- /FakeRunLoop/FakeRunLoop.swift: -------------------------------------------------------------------------------- 1 | // FakeRunLoop.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct FakeRunLoop : RunLoop { 26 | 27 | let semaphore = Dispatch.createSemaphore(0) 28 | 29 | func run() { 30 | 31 | semaphore.wait() 32 | 33 | } 34 | 35 | func close() { 36 | 37 | semaphore.signal() 38 | 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /FastCGIServer/FastCGIServer.h: -------------------------------------------------------------------------------- 1 | // FCGI.h 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #ifndef FCGI_h 26 | #define FCGI_h 27 | 28 | #include 29 | 30 | extern char **environ; 31 | int FCGIAccept(); 32 | int FCGIWriteString(const char *string); 33 | size_t FCGIReadBuffer(void *buffer, size_t size); 34 | size_t FCGIWriteBuffer(void *buffer, size_t size); 35 | 36 | #endif /* FCGI_h */ 37 | -------------------------------------------------------------------------------- /Redirect Responder/RedirectResponder.swift: -------------------------------------------------------------------------------- 1 | // RedirectResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Responder { 26 | 27 | static func redirect(location: String) -> HTTPRequest -> HTTPResponse { 28 | 29 | return { request in 30 | 31 | return HTTPResponse(status: .MovedPermanently, headers: ["location": location]) 32 | 33 | } 34 | 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /Stream/Stream.swift: -------------------------------------------------------------------------------- 1 | // Stream.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol Stream { 26 | 27 | func readData(handler: Data -> Void) throws 28 | func writeData(data: Data, completion: (Void -> Void)?) 29 | func close() 30 | 31 | } 32 | 33 | extension Stream { 34 | 35 | func writeData(data: Data) { 36 | 37 | writeData(data, completion: nil) 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /Libuv/include/uv-threadpool.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | /* 23 | * This file is private to libuv. It provides common functionality to both 24 | * Windows and Unix backends. 25 | */ 26 | 27 | #ifndef UV_THREADPOOL_H_ 28 | #define UV_THREADPOOL_H_ 29 | 30 | struct uv__work { 31 | void (*work)(struct uv__work *w); 32 | void (*done)(struct uv__work *w, int status); 33 | struct uv_loop_s* loop; 34 | void* wq[2]; 35 | }; 36 | 37 | #endif /* UV_THREADPOOL_H_ */ 38 | -------------------------------------------------------------------------------- /Example Server/Deliver.swift: -------------------------------------------------------------------------------- 1 | // Deliver.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | public func deliver(handler: T -> Void, runQueue: DispatchQueue = Dispatch.defaultQueue, resultQueue: DispatchQueue = Dispatch.mainQueue, run: Void -> T) { 26 | 27 | Dispatch.async(queue: runQueue) { 28 | 29 | let result = run() 30 | 31 | Dispatch.async(queue: resultQueue) { 32 | 33 | handler(result) 34 | 35 | } 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Libuv/Buffer.swift: -------------------------------------------------------------------------------- 1 | // Buffer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | typealias BufferRef = UnsafePointer 26 | typealias MutableBufferRef = UnsafeMutablePointer 27 | 28 | func alloc_buffer(_: HandleRef, suggestedSize: Int, buffer: MutableBufferRef) { 29 | 30 | buffer.memory = uv_buf_init(UnsafeMutablePointer.alloc(suggestedSize), UInt32(suggestedSize)) 31 | 32 | } 33 | 34 | func free_buffer(buffer: BufferRef) { 35 | 36 | free(buffer.memory.base) 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Text Response/HTTPResponse+Text.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponse+Text.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension HTTPResponse { 26 | 27 | init( 28 | status: HTTPStatus = .OK, 29 | headers: [String: String] = [:], 30 | text: String) { 31 | 32 | self.init( 33 | status: status, 34 | headers: headers + ["content-type": "text/plain"], 35 | body: Data(string: text) 36 | ) 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Libuv/TCPStream.swift: -------------------------------------------------------------------------------- 1 | // TCPStream.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | class TCPStream : UVStream { 26 | 27 | let socket = UnsafeMutablePointer.alloc(1) 28 | 29 | init(loop: UVRunLoop = UVRunLoop.defaultLoop) { 30 | 31 | super.init(UnsafeMutablePointer(self.socket)) 32 | uv_tcp_init(loop.loop, socket) 33 | 34 | } 35 | 36 | func bind(address: SocketAddress) { 37 | 38 | uv_tcp_bind(socket, address.address, 0) 39 | 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /JSON Response/HTTPResponse+JSON.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponse+JSON.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension HTTPResponse { 26 | 27 | init( 28 | status: HTTPStatus = .OK, 29 | headers: [String: String] = [:], 30 | json: JSON) { 31 | 32 | self.init( 33 | status: status, 34 | headers: headers + ["content-type": "application/json"], 35 | body: Data(string: "\(json)") 36 | ) 37 | 38 | } 39 | 40 | } -------------------------------------------------------------------------------- /Parameters Middleware/ParametersMiddleware.swift: -------------------------------------------------------------------------------- 1 | // ParametersMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func addParameters(parameters: [String: String]) -> HTTPRequest throws -> RequestMiddlewareResult { 28 | 29 | return { (var request) in 30 | 31 | request.parameters = request.parameters + parameters 32 | return .Request(request) 33 | 34 | } 35 | 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Libuv/SocketAddress.swift: -------------------------------------------------------------------------------- 1 | // SocketAddress.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | class SocketAddress { 26 | 27 | var internetAdress = UnsafeMutablePointer.alloc(1) 28 | 29 | var address: UnsafePointer { 30 | 31 | return UnsafePointer(internetAdress) 32 | 33 | } 34 | 35 | init(host: String, port: Int) { 36 | 37 | uv_ip4_addr(host, Int32(port), internetAdress) 38 | 39 | } 40 | 41 | deinit { 42 | 43 | internetAdress.dealloc(1) 44 | 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /Example Server/Public/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Login 8 | 9 | 10 |
11 | 31 |
32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /Example Server/Authentication/Authenticator.swift: -------------------------------------------------------------------------------- 1 | // Authenticator.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct Authenticator { 26 | 27 | static func authenticate(username: String, password: String) throws -> [String: Any] { 28 | 29 | if username == "username" && password == "password" { 30 | 31 | return [ 32 | 33 | "user": User(name: "Name") 34 | 35 | ] 36 | 37 | } 38 | 39 | throw HTTPError.Unauthorized(description: "Unable to authenticate user. Wrong credentials") 40 | 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /Mustache/Error/MustacheError.swift: -------------------------------------------------------------------------------- 1 | // MustacheError.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum MustacheError: ErrorType { 26 | 27 | case Box(String) 28 | case Render(String) 29 | case Parse(String) 30 | case TemplateNotFound(String) 31 | 32 | var reason: String { 33 | 34 | switch self { 35 | 36 | case Box(let reason): return reason 37 | case Render(let reason): return reason 38 | case Parse(let reason): return reason 39 | case TemplateNotFound(let reason): return reason 40 | 41 | } 42 | 43 | } 44 | 45 | } -------------------------------------------------------------------------------- /FastCGIServer/FastCGIServer.c: -------------------------------------------------------------------------------- 1 | // FCGI.c 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | #include "FastCGIServer.h" 26 | #include 27 | 28 | int FCGIAccept() { 29 | 30 | return FCGI_Accept(); 31 | 32 | } 33 | 34 | int FCGIWriteString(const char *string) { 35 | 36 | return FCGI_printf(string); 37 | 38 | } 39 | 40 | size_t FCGIReadBuffer(void *buffer, size_t size) { 41 | 42 | return FCGI_fread(buffer, 1, size, (&_fcgi_sF[0])); 43 | 44 | } 45 | 46 | size_t FCGIWriteBuffer(void *buffer, size_t size) { 47 | 48 | return FCGI_fwrite(buffer, size, 1, (&_fcgi_sF[1])); 49 | 50 | } -------------------------------------------------------------------------------- /HTTP.xcodeproj/project.xcworkspace/xcshareddata/Swifter.xcscmblueprint: -------------------------------------------------------------------------------- 1 | { 2 | "DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "D5AA1969FAEF825657C3CE7CB10DB37383E896D0", 3 | "DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : { 4 | 5 | }, 6 | "DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : { 7 | "D5AA1969FAEF825657C3CE7CB10DB37383E896D0" : 0, 8 | "75224C288D14A799245F99223249EF48FF5DC280" : 0 9 | }, 10 | "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "6F6EE92F-5058-4110-A55D-DFBC76A28E89", 11 | "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { 12 | "D5AA1969FAEF825657C3CE7CB10DB37383E896D0" : "swifter-master\/", 13 | "75224C288D14A799245F99223249EF48FF5DC280" : "swifter" 14 | }, 15 | "DVTSourceControlWorkspaceBlueprintNameKey" : "Swifter", 16 | "DVTSourceControlWorkspaceBlueprintVersion" : 203, 17 | "DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "Swifter.xcodeproj", 18 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [ 19 | { 20 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "github.com:glock45\/swifter.git", 21 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 22 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "75224C288D14A799245F99223249EF48FF5DC280" 23 | }, 24 | { 25 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/paulofaria\/httpserver.git", 26 | "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", 27 | "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "D5AA1969FAEF825657C3CE7CB10DB37383E896D0" 28 | } 29 | ] 30 | } -------------------------------------------------------------------------------- /Keep Alive Midleware/KeepAliveMiddleware.swift: -------------------------------------------------------------------------------- 1 | // KeepAliveMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func keepAlive(respond: HTTPRequest -> HTTPResponse) -> (HTTPRequest -> HTTPResponse) { 28 | 29 | return { request in 30 | 31 | var response = respond(request) 32 | 33 | if request.keepAlive { 34 | 35 | response.headers = response.headers + ["connection": "keep-alive"] 36 | 37 | } 38 | 39 | return response 40 | 41 | } 42 | 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /HTTP Serializer/HTTPRequestSerializer.swift: -------------------------------------------------------------------------------- 1 | // HTTPRequestSerializer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct HTTPRequestSerializer { 26 | 27 | static func serializeRequest(stream stream: Stream, request: HTTPRequest) { 28 | 29 | stream.writeData(Data(string: "\(request.method) \(request.uri) HTTP/1.3\r\n")) 30 | 31 | for (name, value) in request.headers { 32 | 33 | stream.writeData(Data(string: "\(name): \(value)\r\n")) 34 | 35 | } 36 | 37 | stream.writeData(Data(string: "\r\n")) 38 | stream.writeData(request.body) 39 | 40 | } 41 | 42 | } 43 | 44 | -------------------------------------------------------------------------------- /UV HTTP Server/UVHTTPServer.swift: -------------------------------------------------------------------------------- 1 | // UVHTTPServer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct UVHTTPServer: Server { 26 | 27 | let runLoop: RunLoop = UVRunLoop.defaultLoop 28 | let acceptTCPClient = UVAcceptTCPClient 29 | let parseRequest = HTTPRequestParser.parseRequest 30 | let respond: HTTPRequest -> HTTPResponse 31 | let serializeResponse = HTTPResponseSerializer.serializeResponse 32 | 33 | init(respond: (request: HTTPRequest) -> HTTPResponse) { 34 | 35 | self.respond = respond >>> Middleware.keepAlive >>> Middleware.addHeaders(["server": "HTTP Server"]) 36 | 37 | } 38 | 39 | } -------------------------------------------------------------------------------- /Libuv/UVError.swift: -------------------------------------------------------------------------------- 1 | // UVError.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum UVError : ErrorType { 26 | 27 | case Error(code: Int32) 28 | 29 | } 30 | 31 | extension UVError : CustomStringConvertible { 32 | 33 | var description: String { 34 | 35 | switch self { 36 | 37 | case .Error(let code): 38 | 39 | let errorName = String.fromCString(uv_err_name(code)) ?? "Unknown error" 40 | let errorDescription = String.fromCString(uv_strerror(code)) ?? "Unkown Cause" 41 | 42 | return "\(errorName): \(errorDescription)" 43 | 44 | } 45 | 46 | } 47 | 48 | } -------------------------------------------------------------------------------- /Example Client/main.swift: -------------------------------------------------------------------------------- 1 | // main.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | let address: String = "www.apple.com" 26 | let port: TCPPort = 80 27 | 28 | let request = HTTPRequest( 29 | method: .GET, 30 | uri: URI("/")!, 31 | headers: [ 32 | "host": "www.apple.com:80" 33 | ] 34 | ) 35 | 36 | Log.info(request) 37 | 38 | HTTPClient.sendRequest(request, address: address, port: port) { result in 39 | 40 | result.success { response in 41 | 42 | Log.info(response) 43 | 44 | } 45 | 46 | result.failure { error in 47 | 48 | Log.error(error) 49 | 50 | } 51 | 52 | } 53 | 54 | Dispatch.main() 55 | -------------------------------------------------------------------------------- /Example Server/Responders/ParametersResponder.swift: -------------------------------------------------------------------------------- 1 | // ParametersResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct ParametersResponder { 26 | 27 | static func respond(request: HTTPRequest) throws -> HTTPResponse { 28 | 29 | let info: [String: MustacheBoxable] = [ 30 | "URI": request.uri.absolute, 31 | "method": request.method.description, 32 | "headers": request.headers, 33 | "params": request.parameters, 34 | "data": request.data 35 | ] 36 | 37 | return try HTTPResponse(templatePath: "Views/user.html", templateData: info) 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /URI/URI.swift: -------------------------------------------------------------------------------- 1 | // URI.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct URI { 26 | 27 | let path: String 28 | let query: String 29 | 30 | let absolute: String 31 | 32 | init?(_ string: String) { 33 | 34 | let tokens = string.splitBy("?") 35 | 36 | self.path = tokens[0] 37 | 38 | if tokens.count > 1 { 39 | 40 | self.query = tokens[1] 41 | 42 | } else { 43 | 44 | self.query = "" 45 | 46 | } 47 | 48 | self.absolute = string 49 | 50 | } 51 | 52 | } 53 | 54 | extension URI : CustomStringConvertible { 55 | 56 | var description: String { 57 | 58 | return absolute 59 | 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /HTTP Serializer/HTTPResponseSerializer.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponseSerializer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct HTTPResponseSerializer { 26 | 27 | static func serializeResponse(stream stream: Stream, response: HTTPResponse, completion: Void -> Void) { 28 | 29 | var data = Data(string: "\(response.version) \(response.status.statusCode) \(response.status.reasonPhrase)\r\n") 30 | 31 | for (name, value) in response.headers { 32 | 33 | data += Data(string: "\(name): \(value)\r\n") 34 | 35 | } 36 | 37 | data += Data(string: "\r\n") 38 | data += response.body 39 | 40 | stream.writeData(data, completion: completion) 41 | 42 | } 43 | 44 | } 45 | 46 | -------------------------------------------------------------------------------- /Headers Middleware/HeadersMiddleware.swift: -------------------------------------------------------------------------------- 1 | // HeaderMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func addHeaders(headers: [String: String]) -> HTTPRequest -> RequestMiddlewareResult { 28 | 29 | return { (var request) in 30 | 31 | request.headers += headers 32 | return .Request(request) 33 | 34 | } 35 | 36 | } 37 | 38 | static func addHeaders(headers: [String: String]) -> (HTTPResponse -> HTTPResponse) { 39 | 40 | return { (var response) in 41 | 42 | response.headers += headers 43 | return response 44 | 45 | } 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Libuv/src/version.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | 24 | #define UV_STRINGIFY(v) UV_STRINGIFY_HELPER(v) 25 | #define UV_STRINGIFY_HELPER(v) #v 26 | 27 | #define UV_VERSION_STRING_BASE UV_STRINGIFY(UV_VERSION_MAJOR) "." \ 28 | UV_STRINGIFY(UV_VERSION_MINOR) "." \ 29 | UV_STRINGIFY(UV_VERSION_PATCH) 30 | 31 | #if UV_VERSION_IS_RELEASE 32 | # define UV_VERSION_STRING UV_VERSION_STRING_BASE 33 | #else 34 | # define UV_VERSION_STRING UV_VERSION_STRING_BASE "-" UV_VERSION_SUFFIX 35 | #endif 36 | 37 | 38 | unsigned int uv_version(void) { 39 | return UV_VERSION_HEX; 40 | } 41 | 42 | 43 | const char* uv_version_string(void) { 44 | return UV_VERSION_STRING; 45 | } 46 | -------------------------------------------------------------------------------- /HTTP Client/HTTPClient.swift: -------------------------------------------------------------------------------- 1 | // HTTPClient.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct HTTPClient { 26 | 27 | static func sendRequest(request: HTTPRequest, address: String, port: TCPPort, result: Result -> Void) { 28 | 29 | strive(result) { success, failure in 30 | 31 | let socket = try Socket(address: address, port: port) 32 | let socketStream = SocketStream(socket: socket) 33 | 34 | HTTPRequestSerializer.serializeRequest(stream: socketStream, request: request) 35 | HTTPResponseParser.parseResponse(stream: socketStream) { response in 36 | 37 | success(response) 38 | socketStream.close() 39 | 40 | } 41 | 42 | } 43 | 44 | } 45 | 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Example Server/Responders/LoginResponder.swift: -------------------------------------------------------------------------------- 1 | // LoginResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct LoginResponder { 26 | 27 | static func show(request: HTTPRequest) throws -> HTTPResponse { 28 | 29 | return try HTTPResponse(HTMLPath: "Public/login.html") 30 | 31 | } 32 | 33 | static func authenticate(request: HTTPRequest) throws -> HTTPResponse { 34 | 35 | let email = try request.getParameter("email") 36 | let password = try request.getParameter("password") 37 | 38 | if email == "email@email.com" && password == "password" { 39 | 40 | return HTTPResponse(HTML: "logged in") 41 | 42 | } else { 43 | 44 | return HTTPResponse(status: .Unauthorized) 45 | 46 | } 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /File Responder/FileResponder.swift: -------------------------------------------------------------------------------- 1 | // FileResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Responder { 26 | 27 | static func file(baseDirectory baseDirectory: String)(var path: String) -> HTTPRequest throws -> HTTPResponse { 28 | 29 | if path == "/" { path = "/index.html" } 30 | 31 | return { request in 32 | 33 | if request.method != .GET { 34 | 35 | return HTTPResponse(status: .MethodNotAllowed) 36 | 37 | } 38 | 39 | return try HTTPResponse(baseDirectory: baseDirectory, filePath: path) 40 | 41 | } 42 | 43 | } 44 | 45 | static func file(baseDirectory: String, path: String) -> HTTPRequest throws -> HTTPResponse { 46 | 47 | return file(baseDirectory: baseDirectory)(path: path) 48 | 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Logger Middleware/LoggerMiddleware.swift: -------------------------------------------------------------------------------- 1 | // LoggerMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | private let logQueue = Dispatch.createQueue() 26 | 27 | extension Middleware { 28 | 29 | static func log(request: HTTPRequest, response: HTTPResponse) { 30 | 31 | Dispatch.async(queue: logQueue) { 32 | 33 | Log.info(request) 34 | Log.info(response) 35 | 36 | } 37 | 38 | } 39 | 40 | static func logRequest(request: HTTPRequest) -> RequestMiddlewareResult { 41 | 42 | Log.info(request) 43 | return .Request(request) 44 | 45 | } 46 | 47 | static func logResponse(response: HTTPResponse) -> HTTPResponse { 48 | 49 | Log.info(response) 50 | return response 51 | 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /Libuv/include/uv-version.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_VERSION_H 23 | #define UV_VERSION_H 24 | 25 | /* 26 | * Versions with the same major number are ABI stable. API is allowed to 27 | * evolve between minor releases, but only in a backwards compatible way. 28 | * Make sure you update the -soname directives in configure.ac 29 | * and uv.gyp whenever you bump UV_VERSION_MAJOR or UV_VERSION_MINOR (but 30 | * not UV_VERSION_PATCH.) 31 | */ 32 | 33 | #define UV_VERSION_MAJOR 1 34 | #define UV_VERSION_MINOR 7 35 | #define UV_VERSION_PATCH 0 36 | #define UV_VERSION_IS_RELEASE 1 37 | #define UV_VERSION_SUFFIX "" 38 | 39 | #define UV_VERSION_HEX ((UV_VERSION_MAJOR << 16) | \ 40 | (UV_VERSION_MINOR << 8) | \ 41 | (UV_VERSION_PATCH)) 42 | 43 | #endif /* UV_VERSION_H */ 44 | -------------------------------------------------------------------------------- /Example Server/main.swift: -------------------------------------------------------------------------------- 1 | // ExampleServer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct ExampleServer { 26 | 27 | static let respond = Middleware.parseURLEncoded >>> HTTPRouter { router in 28 | 29 | router.get("/login", LoginResponder.show) 30 | router.post("/login", LoginResponder.authenticate) 31 | 32 | router.resources("/users") { 33 | 34 | Middleware.basicAuthentication(Authenticator.authenticate) >>> UserResponder() 35 | 36 | } 37 | 38 | router.get("/json", JSONResponder.get) 39 | router.post("/json", Middleware.parseJSON >>> JSONResponder.post) 40 | router.get("/redirect", Responder.redirect("http://www.google.com")) 41 | router.any("/parameters/:id/", ParametersResponder.respond) 42 | 43 | } >>> HTTPError.respondError >>> Middleware.log 44 | 45 | } 46 | 47 | HTTPServer(respond: ExampleServer.respond).start() -------------------------------------------------------------------------------- /Libuv/Pack.swift: -------------------------------------------------------------------------------- 1 | // Pack.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | final class Pack { 26 | 27 | let unpack: A 28 | 29 | init(_ value: A) { 30 | 31 | unpack = value 32 | 33 | } 34 | 35 | } 36 | 37 | func retainedVoidPointer(x: A?) -> UnsafeMutablePointer { 38 | 39 | guard let value = x else { return UnsafeMutablePointer() } 40 | let unmanaged = Unmanaged.passRetained(Pack(value)) 41 | return UnsafeMutablePointer(unmanaged.toOpaque()) 42 | 43 | } 44 | 45 | func fromVoidPointer(x: UnsafeMutablePointer) -> A? { 46 | 47 | guard x != nil else { return nil } 48 | return Unmanaged>.fromOpaque(COpaquePointer(x)).takeUnretainedValue().unpack 49 | 50 | } 51 | 52 | func releaseVoidPointer(x: UnsafeMutablePointer) -> A? { 53 | 54 | guard x != nil else { return nil } 55 | return Unmanaged>.fromOpaque(COpaquePointer(x)).takeRetainedValue().unpack 56 | 57 | } -------------------------------------------------------------------------------- /URL Encoded Parser Middleware/FormURLEncodedParserMiddleware.swift: -------------------------------------------------------------------------------- 1 | // FormURLEncodedParserMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func parseURLEncoded(var request: HTTPRequest) throws -> RequestMiddlewareResult { 28 | 29 | guard let contentType = request.headers["content-type"] where MediaType(contentType).type == "application/x-www-form-urlencoded" else { 30 | 31 | return .Request(request) 32 | 33 | } 34 | 35 | guard let bodyString = String(data: request.body) else { 36 | 37 | throw Error.Generic("Could not create FormURLEncodedBody from data", "Data is not UTF-8 encoded") 38 | 39 | } 40 | 41 | let parameters = bodyString.queryParameters ?? [:] 42 | request.parameters = request.parameters + parameters 43 | 44 | return .Request(request) 45 | 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /Error/Error.swift: -------------------------------------------------------------------------------- 1 | // Error.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | typealias ErrorDescription = String 26 | typealias ErrorReason = String 27 | 28 | var lastPOSIXErrorDescription: String? { 29 | 30 | return POSIXErrorDescription(errno) 31 | 32 | } 33 | 34 | func POSIXErrorDescription(errorNumber: Int32) -> String? { 35 | 36 | return String.fromCString(UnsafePointer(strerror(errorNumber))) 37 | 38 | } 39 | 40 | enum Error : ErrorType { 41 | 42 | case Generic(ErrorDescription, ErrorReason) 43 | 44 | static func lastSystemError(reason reason: String) -> Error { 45 | 46 | if let errorDescription = lastPOSIXErrorDescription { 47 | 48 | return Error.Generic(errorDescription, reason) 49 | 50 | } 51 | 52 | return Error.Generic("Unknown Error", reason) 53 | 54 | } 55 | 56 | static func defaultFailureHandler(error: ErrorType) { 57 | 58 | Log.error("Error: \(error)") 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Libuv/src/unix/spinlock.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013, Ben Noordhuis 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef UV_SPINLOCK_H_ 17 | #define UV_SPINLOCK_H_ 18 | 19 | #include "internal.h" /* ACCESS_ONCE, UV_UNUSED */ 20 | #include "atomic-ops.h" 21 | 22 | #define UV_SPINLOCK_INITIALIZER { 0 } 23 | 24 | typedef struct { 25 | int lock; 26 | } uv_spinlock_t; 27 | 28 | UV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock)); 29 | UV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock)); 30 | UV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock)); 31 | UV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock)); 32 | 33 | UV_UNUSED(static void uv_spinlock_init(uv_spinlock_t* spinlock)) { 34 | ACCESS_ONCE(int, spinlock->lock) = 0; 35 | } 36 | 37 | UV_UNUSED(static void uv_spinlock_lock(uv_spinlock_t* spinlock)) { 38 | while (!uv_spinlock_trylock(spinlock)) cpu_relax(); 39 | } 40 | 41 | UV_UNUSED(static void uv_spinlock_unlock(uv_spinlock_t* spinlock)) { 42 | ACCESS_ONCE(int, spinlock->lock) = 0; 43 | } 44 | 45 | UV_UNUSED(static int uv_spinlock_trylock(uv_spinlock_t* spinlock)) { 46 | /* TODO(bnoordhuis) Maybe change to a ticket lock to guarantee fair queueing. 47 | * Not really critical until we have locks that are (frequently) contended 48 | * for by several threads. 49 | */ 50 | return 0 == cmpxchgi(&spinlock->lock, 0, 1); 51 | } 52 | 53 | #endif /* UV_SPINLOCK_H_ */ 54 | -------------------------------------------------------------------------------- /Libuv/Libuv.swift: -------------------------------------------------------------------------------- 1 | // Libuv.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Data { 26 | 27 | func withBufferRef(callback: BufferRef -> Void) { 28 | 29 | var data = uv_buf_init(UnsafeMutablePointer(bytes), UInt32(length)) 30 | withUnsafePointer(&data, callback) 31 | 32 | } 33 | 34 | } 35 | 36 | func UVAcceptTCPClient(port port: Int, handleClient: (client: Stream) -> Void) throws { 37 | 38 | let server = TCPStream() 39 | let address = SocketAddress(host: "0.0.0.0", port: port) 40 | 41 | server.bind(address) 42 | 43 | try server.listen(128) { status in 44 | 45 | guard status >= 0 else { return } 46 | 47 | let client = TCPStream() 48 | 49 | do { 50 | 51 | try server.accept(client) 52 | handleClient(client: client) 53 | 54 | } catch { 55 | 56 | client.close() 57 | 58 | } 59 | 60 | } 61 | 62 | } -------------------------------------------------------------------------------- /Lexicon/Dictionary+Lexicon.swift: -------------------------------------------------------------------------------- 1 | // Dictionary+Lexicon.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | func +(var lhs: [Key: Value], rhs: [Key: Value]) -> [Key: Value] { 26 | 27 | for (key, value) in rhs { 28 | 29 | lhs[key] = value 30 | 31 | } 32 | 33 | return lhs 34 | 35 | } 36 | 37 | func +=(inout lhs: [Key: Value], rhs: [Key: Value]) { 38 | 39 | lhs = lhs + rhs 40 | 41 | } 42 | 43 | func dictionaryFromKeys(keys: [Key], values: [Value]) throws -> [Key: Value] { 44 | 45 | if keys.count != values.count { 46 | 47 | throw Error.Generic("Could not create dictionary from arrays of values and keys" , "Size of the arrays don't match. Keys array size is \(keys.count), values array size is \(values.count)") 48 | 49 | } 50 | 51 | var dictionary: [Key: Value] = [:] 52 | 53 | for (index, key) in keys.enumerate() { 54 | 55 | dictionary[key] = values[index] 56 | 57 | } 58 | 59 | return dictionary 60 | 61 | } -------------------------------------------------------------------------------- /Anthology/CollectionType+Anthology.swift: -------------------------------------------------------------------------------- 1 | // CollectionType+Anthology.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension CollectionType { 26 | 27 | /// Returns the first value of `self` 28 | /// that satisfy the predicate `isMatch`. 29 | func find(@noescape isMatch: (Self.Generator.Element) -> Bool) -> Self.Generator.Element? { 30 | 31 | for element in self { 32 | 33 | if isMatch(element) { 34 | 35 | return element 36 | 37 | } 38 | 39 | } 40 | 41 | return .None 42 | 43 | } 44 | 45 | /// Returns the first value of index 46 | /// that satisfy the predicate `isMatch`. 47 | func findIndex(@noescape isMatch: (Self.Generator.Element) -> Bool) -> Int? { 48 | 49 | for (index, element) in self.enumerate() { 50 | 51 | if isMatch(element) { 52 | 53 | return index 54 | 55 | } 56 | 57 | } 58 | 59 | return .None 60 | 61 | } 62 | 63 | } -------------------------------------------------------------------------------- /Example Server/Public/js/classie.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * classie - class helper functions 3 | * from bonzo https://github.com/ded/bonzo 4 | * 5 | * classie.has( elem, 'my-class' ) -> true/false 6 | * classie.add( elem, 'my-new-class' ) 7 | * classie.remove( elem, 'my-unwanted-class' ) 8 | * classie.toggle( elem, 'my-class' ) 9 | */ 10 | 11 | /*jshint browser: true, strict: true, undef: true */ 12 | /*global define: false */ 13 | 14 | ( function( window ) { 15 | 16 | 'use strict'; 17 | 18 | // class helper functions from bonzo https://github.com/ded/bonzo 19 | 20 | function classReg( className ) { 21 | return new RegExp("(^|\\s+)" + className + "(\\s+|$)"); 22 | } 23 | 24 | // classList support for class management 25 | // altho to be fair, the api sucks because it won't accept multiple classes at once 26 | var hasClass, addClass, removeClass; 27 | 28 | if ( 'classList' in document.documentElement ) { 29 | hasClass = function( elem, c ) { 30 | return elem.classList.contains( c ); 31 | }; 32 | addClass = function( elem, c ) { 33 | elem.classList.add( c ); 34 | }; 35 | removeClass = function( elem, c ) { 36 | elem.classList.remove( c ); 37 | }; 38 | } 39 | else { 40 | hasClass = function( elem, c ) { 41 | return classReg( c ).test( elem.className ); 42 | }; 43 | addClass = function( elem, c ) { 44 | if ( !hasClass( elem, c ) ) { 45 | elem.className = elem.className + ' ' + c; 46 | } 47 | }; 48 | removeClass = function( elem, c ) { 49 | elem.className = elem.className.replace( classReg( c ), ' ' ); 50 | }; 51 | } 52 | 53 | function toggleClass( elem, c ) { 54 | var fn = hasClass( elem, c ) ? removeClass : addClass; 55 | fn( elem, c ); 56 | } 57 | 58 | var classie = { 59 | // full names 60 | hasClass: hasClass, 61 | addClass: addClass, 62 | removeClass: removeClass, 63 | toggleClass: toggleClass, 64 | // short names 65 | has: hasClass, 66 | add: addClass, 67 | remove: removeClass, 68 | toggle: toggleClass 69 | }; 70 | 71 | // transport 72 | if ( typeof define === 'function' && define.amd ) { 73 | // AMD 74 | define( classie ); 75 | } else { 76 | // browser global 77 | window.classie = classie; 78 | } 79 | 80 | })( window ); 81 | -------------------------------------------------------------------------------- /HTML Response/HTTPResponse+HTML.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponse+HTML.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension HTTPResponse { 26 | 27 | init( 28 | status: HTTPStatus = .OK, 29 | headers: [String: String] = [:], 30 | HTML: String) { 31 | 32 | self.init( 33 | status: status, 34 | headers: headers + ["content-type": "text/html"], 35 | body: Data(string: HTML) 36 | ) 37 | 38 | } 39 | 40 | init( 41 | status: HTTPStatus = .OK, 42 | headers: [String: String] = [:], 43 | HTMLPath: String) throws { 44 | 45 | guard let file = File(path: HTMLPath) else { 46 | 47 | throw Error.Generic("File Body", "Could not find file \(HTMLPath)") 48 | 49 | } 50 | 51 | self.init( 52 | status: status, 53 | headers: headers + ["content-type": "text/html"], 54 | body: file.data 55 | ) 56 | 57 | } 58 | 59 | } -------------------------------------------------------------------------------- /Text Parser Middleware/TextParserMiddleware.swift: -------------------------------------------------------------------------------- 1 | // TextParserMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func parseText(key key: String)(var request: HTTPRequest) throws -> RequestMiddlewareResult { 28 | 29 | guard let contentType = request.headers["content-type"] where MediaType(contentType).type == "text/plain" else { 30 | 31 | return .Request(request) 32 | 33 | } 34 | 35 | guard let text = String(data: request.body) else { 36 | 37 | throw Error.Generic("Could not create TextBody from data", "Data is not UTF-8 encoded") 38 | 39 | } 40 | 41 | request.parameters = request.parameters + [key: text] 42 | 43 | return .Request(request) 44 | 45 | } 46 | 47 | static func parseText(request: HTTPRequest) throws -> RequestMiddlewareResult { 48 | 49 | return try parseText(key: "body")(request: request) 50 | 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /Mustache/Compiling/TemplateAST/VariableTag.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | /** 24 | VariableTag represents a variable tag such as {{name}} or {{{name}}}. 25 | */ 26 | class VariableTag: Tag { 27 | let token: TemplateToken 28 | let contentType: ContentType 29 | 30 | init(contentType: ContentType, token: TemplateToken) { 31 | self.contentType = contentType 32 | self.token = token 33 | super.init(type: .Variable, innerTemplateString: "", tagDelimiterPair: token.tagDelimiterPair!) 34 | } 35 | 36 | /** 37 | VariableTag is an internal class, but it inherits the Printable protocol 38 | from its public superclass Tag. Return a nice user-friendly description: 39 | */ 40 | override var description: String { 41 | return "\(token.templateSubstring) at \(token.locationDescription)" 42 | } 43 | 44 | /** 45 | Inherited from the public super class Tag. Variable have no inner content. 46 | */ 47 | override func render(context: Context) throws -> Rendering { 48 | return Rendering("", contentType) 49 | } 50 | } -------------------------------------------------------------------------------- /File Response/HTTPResponse+File.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponse+File.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension HTTPResponse { 26 | 27 | init( 28 | status: HTTPStatus = .OK, 29 | headers: [String: String] = [:], 30 | baseDirectory: String, 31 | filePath: String, 32 | contentType: String? = .None) throws { 33 | 34 | guard let file = File(path: baseDirectory + filePath) else { 35 | 36 | throw HTTPError.NotFound(description: "Resource not found: \(filePath)") 37 | 38 | } 39 | 40 | if let contentType = contentType { 41 | 42 | self.init( 43 | status: status, 44 | headers: headers + ["content-type": contentType], 45 | body: file.data 46 | ) 47 | 48 | } else { 49 | 50 | self.init( 51 | status: status, 52 | headers: headers, 53 | body: file.data 54 | ) 55 | 56 | } 57 | 58 | } 59 | 60 | } -------------------------------------------------------------------------------- /JSON Parser Middleware/JSONParserMiddleware.swift: -------------------------------------------------------------------------------- 1 | // JSONParserMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func parseJSON(key key: String)(var request: HTTPRequest) throws -> RequestMiddlewareResult { 28 | 29 | // TODO: put contenty type as computed property on httprequest 30 | guard let contentType = request.headers["content-type"] where MediaType(contentType).type == "application/json" else { 31 | 32 | return .Request(request) 33 | 34 | } 35 | 36 | let json = try JSONParser.parse(request.body) 37 | request.data = request.data + [key: json] 38 | 39 | return .Request(request) 40 | 41 | } 42 | 43 | static func parseJSON(request: HTTPRequest) throws -> RequestMiddlewareResult { 44 | 45 | return try parseJSON(key: "JSON")(request: request) 46 | 47 | } 48 | 49 | } 50 | 51 | extension HTTPRequest { 52 | 53 | var json: JSON? { 54 | 55 | return try? getData("JSON") 56 | 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /Libuv/UVRunLoop.swift: -------------------------------------------------------------------------------- 1 | // RunLoop.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | typealias LoopRef = UnsafeMutablePointer 26 | 27 | class UVRunLoop : RunLoop { 28 | 29 | enum RunMode { 30 | 31 | case Default 32 | case Once 33 | case NoWait 34 | 35 | var rawValue: uv_run_mode { 36 | 37 | switch self { 38 | 39 | case Default: return UV_RUN_DEFAULT 40 | case Once: return UV_RUN_ONCE 41 | case NoWait: return UV_RUN_NOWAIT 42 | 43 | } 44 | 45 | } 46 | 47 | } 48 | 49 | let loop: LoopRef 50 | 51 | init(loop: LoopRef = UnsafeMutablePointer.alloc(1)) { 52 | 53 | self.loop = loop 54 | uv_loop_init(loop) 55 | 56 | } 57 | 58 | deinit { 59 | 60 | close() 61 | 62 | } 63 | 64 | func run() { 65 | 66 | uv_run(loop, RunMode.Default.rawValue) 67 | 68 | } 69 | 70 | func close() { 71 | 72 | uv_loop_close(loop) 73 | loop.dealloc(1) 74 | 75 | } 76 | 77 | static let defaultLoop = UVRunLoop(loop: uv_default_loop()) 78 | 79 | } 80 | -------------------------------------------------------------------------------- /Template Response/HTTPResponse+Template.swift: -------------------------------------------------------------------------------- 1 | // HTTPResponse+Template.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension HTTPResponse { 26 | 27 | init( 28 | status: HTTPStatus = .OK, 29 | headers: [String: String] = [:], 30 | templatePath: String, 31 | templateData: MustacheBoxable) throws { 32 | 33 | guard let templateFile = File(path: templatePath) else { 34 | 35 | throw Error.Generic("Template Response Body", "Could not find template at path: \(templatePath)") 36 | 37 | } 38 | 39 | guard let templateString = String(data: templateFile.data) else { 40 | 41 | throw Error.Generic("Template Response Body", "Template at path: \(templatePath); is not UTF-8 encoded") 42 | 43 | } 44 | 45 | let template = try Template(string: templateString) 46 | let rendering = try template.render(Box(boxable: templateData)) 47 | 48 | self.init( 49 | status: status, 50 | headers: headers + ["content-type": "text/html"], 51 | body: Data(string: rendering) 52 | ) 53 | 54 | } 55 | 56 | } -------------------------------------------------------------------------------- /Example Server/Responders/JSONResponder.swift: -------------------------------------------------------------------------------- 1 | // JSONResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct JSONResponder { 26 | 27 | static func get(request: HTTPRequest) -> HTTPResponse { 28 | 29 | let json: JSON = [ 30 | 31 | "null": nil, 32 | "string": "Foo Bar", 33 | "boolean": true, 34 | "array": [ 35 | "1", 36 | 2, 37 | nil, 38 | true, 39 | ["1", 2, nil, false], 40 | ["a": "b"] 41 | ], 42 | "object": [ 43 | "a": "1", 44 | "b": 2, 45 | "c": nil, 46 | "d": false, 47 | "e": ["1", 2, nil, false], 48 | "f": ["a": "b"] 49 | ], 50 | "number": 1969 51 | 52 | ] 53 | 54 | return HTTPResponse(json: json) 55 | 56 | } 57 | 58 | static func post(request: HTTPRequest) throws -> HTTPResponse { 59 | 60 | var json: JSON = try request.getData("JSON") 61 | 62 | json["number"] = 321 63 | json["array"][0] = 3 64 | json["array"][2] = 1 65 | 66 | return HTTPResponse(json: json) 67 | 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /Libuv/src/unix/atomic-ops.h: -------------------------------------------------------------------------------- 1 | /* Copyright (c) 2013, Ben Noordhuis 2 | * 3 | * Permission to use, copy, modify, and/or distribute this software for any 4 | * purpose with or without fee is hereby granted, provided that the above 5 | * copyright notice and this permission notice appear in all copies. 6 | * 7 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 8 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 9 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 10 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 11 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 12 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 13 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 14 | */ 15 | 16 | #ifndef UV_ATOMIC_OPS_H_ 17 | #define UV_ATOMIC_OPS_H_ 18 | 19 | #include "internal.h" /* UV_UNUSED */ 20 | 21 | UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)); 22 | UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval)); 23 | UV_UNUSED(static void cpu_relax(void)); 24 | 25 | /* Prefer hand-rolled assembly over the gcc builtins because the latter also 26 | * issue full memory barriers. 27 | */ 28 | UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)) { 29 | #if defined(__i386__) || defined(__x86_64__) 30 | int out; 31 | __asm__ __volatile__ ("lock; cmpxchg %2, %1;" 32 | : "=a" (out), "+m" (*(volatile int*) ptr) 33 | : "r" (newval), "0" (oldval) 34 | : "memory"); 35 | return out; 36 | #else 37 | return __sync_val_compare_and_swap(ptr, oldval, newval); 38 | #endif 39 | } 40 | 41 | UV_UNUSED(static long cmpxchgl(long* ptr, long oldval, long newval)) { 42 | #if defined(__i386__) || defined(__x86_64__) 43 | long out; 44 | __asm__ __volatile__ ("lock; cmpxchg %2, %1;" 45 | : "=a" (out), "+m" (*(volatile long*) ptr) 46 | : "r" (newval), "0" (oldval) 47 | : "memory"); 48 | return out; 49 | #else 50 | return __sync_val_compare_and_swap(ptr, oldval, newval); 51 | #endif 52 | } 53 | 54 | UV_UNUSED(static void cpu_relax(void)) { 55 | #if defined(__i386__) || defined(__x86_64__) 56 | __asm__ __volatile__ ("rep; nop"); /* a.k.a. PAUSE */ 57 | #endif 58 | } 59 | 60 | #endif /* UV_ATOMIC_OPS_H_ */ 61 | -------------------------------------------------------------------------------- /Server Tests/ServerTests.swift: -------------------------------------------------------------------------------- 1 | // ServerTests.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | import XCTest 26 | 27 | class ServerTests: XCTestCase { 28 | 29 | override func setUp() { 30 | 31 | super.setUp() 32 | 33 | } 34 | 35 | func testJSON() { 36 | 37 | let json: JSON = [ 38 | 39 | "null": nil, 40 | "string": "Foo Bar", 41 | "boolean": true, 42 | "array": [ 43 | "1", 44 | 2, 45 | nil, 46 | true, 47 | ["1", 2, nil, false], 48 | ["a": "b"] 49 | ], 50 | "object": [ 51 | "a": "1", 52 | "b": 2, 53 | "c": nil, 54 | "d": false, 55 | "e": ["1", 2, nil, false], 56 | "f": ["a": "b"] 57 | ], 58 | "number": 1969 59 | 60 | ] 61 | 62 | let request = HTTPRequest(method: .GET, uri: URI("/json")!) 63 | let response = ExampleServer.respond(request) 64 | XCTAssert(response.status == .OK) 65 | XCTAssert(response.equalsJSON(json)) 66 | 67 | } 68 | 69 | override func tearDown() { 70 | 71 | super.tearDown() 72 | 73 | } 74 | 75 | } 76 | -------------------------------------------------------------------------------- /HTTP Router/HTTPMethodRouter.swift: -------------------------------------------------------------------------------- 1 | // HTTPMethodRouter.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct HTTPMethodRouter: ServerRouter { 26 | 27 | var routes: [HTTPMethodRoute] = [] 28 | 29 | let fallback: (method: HTTPMethod) -> HTTPRequest throws -> HTTPResponse 30 | 31 | init(fallback: (method: HTTPMethod) -> HTTPRequest throws -> HTTPResponse) { 32 | 33 | self.fallback = fallback 34 | 35 | } 36 | 37 | var respond: HTTPRequest throws -> HTTPResponse { 38 | 39 | return getRespond( 40 | key: HTTPRequest.methodRouterKey, 41 | fallback: fallback 42 | ) 43 | 44 | } 45 | 46 | } 47 | 48 | struct HTTPMethodRoute: ServerRoute { 49 | 50 | let key: HTTPMethod 51 | let respond: HTTPRequest throws -> HTTPResponse 52 | 53 | init(key: HTTPMethod, respond: HTTPRequest throws -> HTTPResponse) { 54 | 55 | self.key = key 56 | self.respond = respond 57 | 58 | } 59 | 60 | func matchesKey(key: HTTPMethod) -> Bool { 61 | 62 | return self.key == key 63 | 64 | } 65 | 66 | var respondForKey: (key: HTTPMethod) -> (HTTPRequest throws -> HTTPResponse) { 67 | 68 | return { (key: HTTPMethod) in 69 | 70 | return self.respond 71 | 72 | } 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /Mustache/Compiling/TemplateAST/SectionTag.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | /** 24 | A SectionTag represents a regular or inverted section tag such as 25 | {{#section}}...{{/section}} or {{^section}}...{{/section}}. 26 | */ 27 | class SectionTag: Tag { 28 | let openingToken: TemplateToken 29 | let innerTemplateAST: TemplateAST 30 | 31 | init(innerTemplateAST: TemplateAST, openingToken: TemplateToken, innerTemplateString: String) { 32 | self.innerTemplateAST = innerTemplateAST 33 | self.openingToken = openingToken 34 | super.init(type: .Section, innerTemplateString: innerTemplateString, tagDelimiterPair: openingToken.tagDelimiterPair!) 35 | } 36 | 37 | /** 38 | SectionTag is an internal class, but it inherits the Printable protocol from 39 | its public superclass Tag. Return a nice user-friendly description: 40 | */ 41 | override var description: String { 42 | return "\(openingToken.templateSubstring) at \(openingToken.locationDescription)" 43 | } 44 | 45 | /** 46 | Inherited from the public super class Tag. 47 | */ 48 | override func render(context: Context) throws -> Rendering { 49 | let renderingEngine = RenderingEngine(templateAST: innerTemplateAST, context: context) 50 | return try renderingEngine.render() 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /Media Type/MediaType.swift: -------------------------------------------------------------------------------- 1 | // MediaType.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | // TODO: Think a better way to deal with media types 26 | struct MediaType : CustomStringConvertible { 27 | 28 | let type: String 29 | let parameters: [String: String] 30 | 31 | var description: String { 32 | 33 | return "\(type);" + parameters.reduce("") { $0 + " \($1.0)=\($1.1)" } 34 | 35 | } 36 | 37 | init(_ string: String) { 38 | 39 | let mediaTypeTokens = string.splitBy(";") 40 | let mediaType = mediaTypeTokens.first! 41 | var parameters: [String: String] = [:] 42 | 43 | if mediaTypeTokens.count == 2 { 44 | 45 | let parametersTokens = mediaTypeTokens[1].trim().splitBy(" ") 46 | 47 | for parametersToken in parametersTokens { 48 | 49 | let parameterTokens = parametersToken.splitBy("=") 50 | 51 | if parameterTokens.count == 2 { 52 | 53 | let key = parameterTokens[0] 54 | let value = parameterTokens[1] 55 | parameters[key] = value 56 | 57 | } 58 | 59 | 60 | } 61 | 62 | } 63 | 64 | self.type = mediaType.lowercaseString 65 | self.parameters = parameters 66 | 67 | } 68 | 69 | } -------------------------------------------------------------------------------- /Libuv/src/unix/dl.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #include "uv.h" 23 | #include "internal.h" 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | 30 | static int uv__dlerror(uv_lib_t* lib); 31 | 32 | 33 | int uv_dlopen(const char* filename, uv_lib_t* lib) { 34 | dlerror(); /* Reset error status. */ 35 | lib->errmsg = NULL; 36 | lib->handle = dlopen(filename, RTLD_LAZY); 37 | return lib->handle ? 0 : uv__dlerror(lib); 38 | } 39 | 40 | 41 | void uv_dlclose(uv_lib_t* lib) { 42 | if (lib->errmsg) { 43 | uv__free(lib->errmsg); 44 | lib->errmsg = NULL; 45 | } 46 | 47 | if (lib->handle) { 48 | /* Ignore errors. No good way to signal them without leaking memory. */ 49 | dlclose(lib->handle); 50 | lib->handle = NULL; 51 | } 52 | } 53 | 54 | 55 | int uv_dlsym(uv_lib_t* lib, const char* name, void** ptr) { 56 | dlerror(); /* Reset error status. */ 57 | *ptr = dlsym(lib->handle, name); 58 | return uv__dlerror(lib); 59 | } 60 | 61 | 62 | const char* uv_dlerror(const uv_lib_t* lib) { 63 | return lib->errmsg ? lib->errmsg : "no error"; 64 | } 65 | 66 | 67 | static int uv__dlerror(uv_lib_t* lib) { 68 | const char* errmsg; 69 | 70 | if (lib->errmsg) 71 | uv__free(lib->errmsg); 72 | 73 | errmsg = dlerror(); 74 | 75 | if (errmsg) { 76 | lib->errmsg = uv__strdup(errmsg); 77 | return -1; 78 | } 79 | else { 80 | lib->errmsg = NULL; 81 | return 0; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /Basic Authentication Middleware/BasicAuthenticationMiddleware.swift: -------------------------------------------------------------------------------- 1 | // BasicAuthenticationMiddleware.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | extension Middleware { 26 | 27 | static func basicAuthentication(authenticate: (username: String, password: String) throws -> [String: Any])(var request: HTTPRequest) throws -> RequestMiddlewareResult { 28 | 29 | if let authorization = request.headers["authorization"] { 30 | 31 | let authorizationTokens = authorization.splitBy(" ") 32 | 33 | if authorizationTokens.count == 2 && authorizationTokens[0] == "Basic" { 34 | 35 | let encodedCredentials = authorizationTokens[1] 36 | let decodedCredentials = encodedCredentials.base64Decode 37 | 38 | let decodedCredentialsTokens = decodedCredentials.splitBy(":") 39 | 40 | if decodedCredentialsTokens.count == 2 { 41 | 42 | let username = decodedCredentialsTokens[0] 43 | let password = decodedCredentialsTokens[1] 44 | 45 | let data = try authenticate(username: username, password: password) 46 | 47 | request.data = request.data + data 48 | return .Request(request) 49 | 50 | } 51 | 52 | } 53 | 54 | } 55 | 56 | let response = HTTPResponse(status: .Unauthorized, text: "Unauthorized") 57 | return .Response(response) 58 | 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /HTTP Error/HTTPError.swift: -------------------------------------------------------------------------------- 1 | // HTTPError.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum HTTPError : ErrorType { 26 | 27 | case BadRequest(description: String) 28 | case Unauthorized(description: String) 29 | case NotFound(description: String) 30 | 31 | static func respondError(error: ErrorType) -> HTTPResponse { 32 | 33 | let response: HTTPResponse 34 | 35 | switch error { 36 | 37 | case HTTPError.BadRequest(let description): 38 | response = HTTPResponse(status: .BadRequest, text: "\(description)") 39 | 40 | case HTTPError.Unauthorized(let description): 41 | response = HTTPResponse(status: .Unauthorized, text: "\(description)") 42 | 43 | case HTTPError.NotFound(let description): 44 | response = HTTPResponse(status: .NotFound, text: "\(description)") 45 | 46 | default: 47 | response = HTTPResponse(status: .InternalServerError, text: "\(error)") 48 | 49 | } 50 | 51 | Log.error(error) 52 | return response 53 | 54 | } 55 | 56 | } 57 | 58 | extension HTTPError : CustomStringConvertible { 59 | 60 | var description: String { 61 | 62 | switch self { 63 | 64 | case .BadRequest(let description): 65 | return description 66 | 67 | case .Unauthorized(let description): 68 | return description 69 | 70 | case .NotFound(let description): 71 | return description 72 | 73 | } 74 | 75 | } 76 | 77 | } -------------------------------------------------------------------------------- /Example Server/Responders/DatabaseResponder.swift: -------------------------------------------------------------------------------- 1 | // DatabaseResponder.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct DatabaseResponder { 26 | 27 | static func get(request: HTTPRequest) throws -> HTTPResponse { 28 | 29 | #if os(OSX) 30 | 31 | let connection = PostgreSQL.Connection(database: "postgres") 32 | let database = try PostgreSQL(connection: connection) 33 | 34 | try database.transaction { 35 | 36 | try database.command("DECLARE myportal CURSOR FOR select * from pg_database") 37 | 38 | let result = try database.query("FETCH ALL in myportal") 39 | 40 | for fieldName in result.fieldNames { 41 | 42 | let string = String(format: "%-15s", fieldName) 43 | print(string, appendNewline: false) 44 | 45 | } 46 | 47 | print("\n") 48 | 49 | for tuple in result.tuples { 50 | 51 | for field in tuple.fields { 52 | 53 | let string = String(format: "%-15s", field.value) 54 | print(string, appendNewline: false) 55 | 56 | } 57 | 58 | print("") 59 | 60 | } 61 | 62 | database.clear(result) 63 | 64 | try database.command("CLOSE myportal") 65 | 66 | } 67 | 68 | database.finish() 69 | 70 | #endif 71 | 72 | return HTTPResponse() 73 | 74 | } 75 | 76 | } -------------------------------------------------------------------------------- /JSON/JSONParseError.swift: -------------------------------------------------------------------------------- 1 | // JSONParseError.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum JSONParseError: ErrorType, CustomStringConvertible { 26 | 27 | case UnexpectedTokenError(ErrorReason, Parser) 28 | case InsufficientTokenError(ErrorReason, Parser) 29 | case ExtraTokenError(ErrorReason, Parser) 30 | case NonStringKeyError(ErrorReason, Parser) 31 | case InvalidStringError(ErrorReason, Parser) 32 | case InvalidNumberError(ErrorReason, Parser) 33 | 34 | // TODO: Check what this actually prints. 35 | var description: String { 36 | 37 | switch self { 38 | 39 | case UnexpectedTokenError(let reason, let parser): 40 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 41 | 42 | case InsufficientTokenError(let reason, let parser): 43 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 44 | 45 | case ExtraTokenError(let reason, let parser): 46 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 47 | 48 | case NonStringKeyError(let reason, let parser): 49 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 50 | 51 | case InvalidStringError(let reason, let parser): 52 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 53 | 54 | case InvalidNumberError(let reason, let parser): 55 | return "\(self)[\(parser.lineNumber):\(parser.columnNumber)]: \(reason)" 56 | 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | protocol Parser { 64 | 65 | var lineNumber: Int { get } 66 | var columnNumber: Int { get } 67 | 68 | } 69 | -------------------------------------------------------------------------------- /Server/Server.swift: -------------------------------------------------------------------------------- 1 | // Server.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol Server { 26 | 27 | typealias Request 28 | typealias Response 29 | 30 | var runLoop: RunLoop { get } 31 | var acceptTCPClient: (port: Int, handleClient: (client: Stream) -> Void) throws -> Void { get } 32 | var parseRequest: (stream: Stream, completion: Request -> Void) -> Void { get } 33 | var respond: Request -> Response { get } 34 | var serializeResponse: (stream: Stream, response: Response, completion: Void -> Void) -> Void { get } 35 | 36 | } 37 | 38 | extension Server { 39 | 40 | func start(port port: Int = 8080, failure: ErrorType -> Void = Error.defaultFailureHandler) { 41 | 42 | do { 43 | 44 | try acceptTCPClient(port: port) { client in 45 | 46 | self.parseRequest(stream: client) { request in 47 | 48 | let response = self.respond(request) 49 | self.serializeResponse(stream: client, response: response) { 50 | 51 | if !self.keepAliveRequest(request) { 52 | 53 | client.close() 54 | 55 | } 56 | 57 | } 58 | 59 | } 60 | 61 | } 62 | 63 | runLoop.run() 64 | 65 | } catch { 66 | 67 | failure(error) 68 | 69 | } 70 | 71 | } 72 | 73 | func stop() { 74 | 75 | runLoop.close() 76 | 77 | } 78 | 79 | private func keepAliveRequest(request: Request) -> Bool { 80 | 81 | return (request as? KeepAliveType)?.keepAlive ?? false 82 | 83 | } 84 | 85 | } -------------------------------------------------------------------------------- /Server Router/ServerRouter.swift: -------------------------------------------------------------------------------- 1 | // ServerRouter.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | protocol ServerRoute { 26 | 27 | typealias Key 28 | typealias Request 29 | typealias Response 30 | 31 | var key: Key { get } 32 | var respondForKey: (key: Key) -> Request throws -> Response { get } 33 | 34 | init(key: Key, respond: Request throws -> Response) 35 | func matchesKey(key: Key) -> Bool 36 | 37 | } 38 | 39 | protocol ServerRouter { 40 | 41 | typealias Route : ServerRoute 42 | var routes: [Route] { get set } 43 | 44 | } 45 | 46 | extension ServerRouter { 47 | 48 | var keys: [Route.Key] { 49 | 50 | return routes.map { $0.key } 51 | 52 | } 53 | 54 | mutating func route(key: Route.Key, respond: Route.Request throws -> Route.Response) { 55 | 56 | let route = Route(key: key, respond: respond) 57 | routes.append(route) 58 | 59 | } 60 | 61 | private var routerRespond: (key: Route.Key) -> (Route.Request throws -> Route.Response)? { 62 | 63 | return { (key: Route.Key) in 64 | 65 | if let route = self.routes.find({$0.matchesKey(key)}) { 66 | 67 | return route.respondForKey(key: key) 68 | 69 | } 70 | 71 | return nil 72 | 73 | } 74 | 75 | } 76 | 77 | func getRespond(key getKey: Route.Request -> () -> Route.Key, 78 | fallback: (key: Route.Key) -> (Route.Request throws -> Route.Response)) -> (Route.Request throws -> Route.Response) { 79 | 80 | return { (request: Route.Request) in 81 | 82 | let key = getKey(request)() 83 | let respond = self.routerRespond(key: key) ?? fallback(key: key) 84 | return try respond(request) 85 | 86 | } 87 | 88 | } 89 | 90 | } -------------------------------------------------------------------------------- /Mustache/Rendering/ExpressionInvocation.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | struct ExpressionInvocation { 24 | 25 | let expression: Expression 26 | 27 | func invokeWithContext(context: Context) throws -> MustacheBox { 28 | 29 | return try evaluate(context: context, expression: expression) 30 | 31 | } 32 | 33 | private func evaluate(context context: Context, expression: Expression) throws -> MustacheBox { 34 | 35 | switch expression { 36 | 37 | case .ImplicitIterator: 38 | // {{ . }} 39 | return context.topBox 40 | 41 | case .Identifier(let identifier): 42 | // {{ identifier }} 43 | return context[identifier] 44 | 45 | case .Scoped(let baseExpression, let identifier): 46 | // {{ .identifier }} 47 | return try evaluate(context: context, expression: baseExpression.expression)[identifier] 48 | 49 | case .Filter(let filterExpression, let argumentExpression, let partialApplication): 50 | // {{ () }} 51 | let filterBox = try evaluate(context: context, expression: filterExpression.expression) 52 | 53 | guard let filter = filterBox.filter else { 54 | 55 | if filterBox.isEmpty { 56 | 57 | throw MustacheError.Render("Missing filter") 58 | 59 | } else { 60 | 61 | throw MustacheError.Render("Not a filter") 62 | 63 | } 64 | 65 | } 66 | 67 | let argumentBox = try evaluate(context: context, expression: argumentExpression.expression) 68 | return try filter(box: argumentBox, partialApplication: partialApplication) 69 | 70 | } 71 | 72 | } 73 | 74 | } 75 | -------------------------------------------------------------------------------- /File/File.swift: -------------------------------------------------------------------------------- 1 | // File.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | // TODO: Method that creates a directory 26 | struct File { 27 | 28 | let data: Data 29 | let path: String 30 | 31 | // TODO: make it throw 32 | init?(path: String) { 33 | 34 | guard let data = File.getData(path) else { 35 | 36 | return nil 37 | 38 | } 39 | 40 | self.data = data 41 | self.path = path 42 | 43 | } 44 | 45 | // TODO: make it throw 46 | init?(path: String, data: Data) { 47 | 48 | guard let data = File.saveData(data, atPath: path) else { 49 | 50 | return nil 51 | 52 | } 53 | 54 | self.data = data 55 | self.path = path 56 | 57 | } 58 | 59 | private static func saveData(data: Data, atPath path: String) -> Data? { 60 | 61 | let file = fopen(path, "w") 62 | 63 | if file == .None { 64 | 65 | return .None 66 | 67 | } 68 | 69 | fwrite(data.bytes, 1, data.length, file) 70 | fclose(file) 71 | 72 | return data 73 | 74 | } 75 | 76 | private static func getData(path: String) -> Data? { 77 | 78 | let file = fopen(path, "r") 79 | 80 | if file == nil { 81 | 82 | return .None 83 | 84 | } 85 | 86 | var array: [UInt8] = [] 87 | 88 | while true { 89 | 90 | let element = fgetc(file) 91 | 92 | if element == -1 { 93 | 94 | break 95 | 96 | } 97 | 98 | if feof(file) != 0 { 99 | 100 | break 101 | 102 | } 103 | 104 | array.append(UInt8(element)) 105 | 106 | } 107 | 108 | fclose(file) 109 | 110 | return Data(bytes: array) 111 | 112 | } 113 | 114 | } 115 | -------------------------------------------------------------------------------- /Request Middleware Result/RequestMiddlewareResult.swift: -------------------------------------------------------------------------------- 1 | // RequestMiddlewareResult.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum RequestMiddlewareResult { 26 | 27 | case Request(RequestType) 28 | case Response(ResponseType) 29 | 30 | } 31 | 32 | func >>>(middleware: Request throws -> RequestMiddlewareResult, responder: T) -> Request throws -> Response { 33 | 34 | return middleware >>> responder.respond 35 | 36 | } 37 | 38 | func >>>(middlewareA: Request throws -> RequestMiddlewareResult, middlewareB: Request throws -> RequestMiddlewareResult) -> Request throws -> RequestMiddlewareResult { 39 | 40 | return { (request: Request) -> RequestMiddlewareResult in 41 | 42 | switch try middlewareA(request) { 43 | 44 | case .Request(let request): 45 | return try middlewareB(request) 46 | 47 | case .Response(let response): 48 | return .Response(response) 49 | 50 | } 51 | 52 | } 53 | 54 | } 55 | 56 | func >>>(middleware: (Request throws -> RequestMiddlewareResult)?, respond: Request throws -> Response) -> (Request throws -> Response) { 57 | 58 | return { (request: Request) -> Response in 59 | 60 | if let middleware = middleware { 61 | 62 | switch try middleware(request) { 63 | 64 | case .Request(let request): 65 | return try respond(request) 66 | 67 | case .Response(let response): 68 | return response 69 | 70 | } 71 | 72 | } else { 73 | 74 | return try respond(request) 75 | 76 | } 77 | 78 | } 79 | 80 | } -------------------------------------------------------------------------------- /Mustache/Parsing/TemplateToken.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | struct TemplateToken { 25 | enum Type { 26 | case Text(text: String) 27 | case EscapedVariable(content: String, tagDelimiterPair: TagDelimiterPair) 28 | case UnescapedVariable(content: String, tagDelimiterPair: TagDelimiterPair) 29 | case Comment 30 | case Section(content: String, tagDelimiterPair: TagDelimiterPair) 31 | case InvertedSection(content: String, tagDelimiterPair: TagDelimiterPair) 32 | case Close(content: String) 33 | case Partial(content: String) 34 | case SetDelimiters 35 | case Pragma(content: String) 36 | case InheritedPartial(content: String) 37 | case InheritableSection(content: String) 38 | } 39 | 40 | let type: Type 41 | let lineNumber: Int 42 | let templateID: TemplateID? 43 | let templateString: String 44 | let range: Range 45 | 46 | var templateSubstring: String { return templateString[range] } 47 | 48 | var tagDelimiterPair: TagDelimiterPair? { 49 | switch type { 50 | case .EscapedVariable(content: _, tagDelimiterPair: let tagDelimiterPair): 51 | return tagDelimiterPair 52 | case .UnescapedVariable(content: _, tagDelimiterPair: let tagDelimiterPair): 53 | return tagDelimiterPair 54 | case .Section(content: _, tagDelimiterPair: let tagDelimiterPair): 55 | return tagDelimiterPair 56 | case .InvertedSection(content: _, tagDelimiterPair: let tagDelimiterPair): 57 | return tagDelimiterPair 58 | default: 59 | return nil 60 | } 61 | } 62 | 63 | var locationDescription: String { 64 | if let templateID = templateID { 65 | return "line \(lineNumber) of template \(templateID)" 66 | } else { 67 | return "line \(lineNumber)" 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /HTTP Router/HTTPPathRouter.swift: -------------------------------------------------------------------------------- 1 | // HTTPPathRouter.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | struct HTTPPathRouter : ServerRouter { 26 | 27 | var routes: [HTTPPathRoute] = [] 28 | 29 | let fallback: (path: String) -> HTTPRequest throws -> HTTPResponse 30 | 31 | init(fallback: (path: String) -> HTTPRequest throws -> HTTPResponse) { 32 | 33 | self.fallback = fallback 34 | 35 | } 36 | 37 | var respond: HTTPRequest throws -> HTTPResponse { 38 | 39 | return getRespond( 40 | key: HTTPRequest.pathRouterKey, 41 | fallback: fallback 42 | ) 43 | 44 | } 45 | 46 | } 47 | 48 | struct HTTPPathRoute: ServerRoute { 49 | 50 | let key: String 51 | let respond: HTTPRequest throws -> HTTPResponse 52 | 53 | private let parameterKeys: [String] 54 | private let regularExpression: RegularExpression 55 | 56 | init(key: String, respond: HTTPRequest throws -> HTTPResponse) { 57 | 58 | let parameterRegularExpression = try! RegularExpression(pattern: ":([[:alnum:]]+)") 59 | let pattern = try! parameterRegularExpression.replace(key, withTemplate: "([[:alnum:]]+)") 60 | 61 | self.key = key 62 | self.parameterKeys = try! parameterRegularExpression.groups(key) 63 | self.regularExpression = try! RegularExpression(pattern: "^" + pattern + "$") 64 | self.respond = respond 65 | 66 | } 67 | 68 | func matchesKey(key: String) -> Bool { 69 | 70 | return try! regularExpression.matches(key) 71 | 72 | } 73 | 74 | var respondForKey: (key: String) -> (HTTPRequest throws -> HTTPResponse) { 75 | 76 | return { (key: String) in 77 | 78 | let values = try! self.regularExpression.groups(key) 79 | let parameters = try! dictionaryFromKeys(self.parameterKeys, values: values) 80 | return Middleware.addParameters(parameters) >>> self.respond 81 | 82 | } 83 | 84 | } 85 | 86 | } -------------------------------------------------------------------------------- /JSON/StringUtils.swift: -------------------------------------------------------------------------------- 1 | // StringUtils.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | let unescapeMapping: [UnicodeScalar: UnicodeScalar] = [ 26 | 27 | "t": "\t", 28 | "r": "\r", 29 | "n": "\n" 30 | 31 | ] 32 | 33 | let escapeMapping: [Character: String] = [ 34 | 35 | "\r": "\\r", 36 | "\n": "\\n", 37 | "\t": "\\t", 38 | "\\": "\\\\", 39 | "\"": "\\\"", 40 | 41 | "\u{2028}": "\\u2028", // LINE SEPARATOR 42 | "\u{2029}": "\\u2029", // PARAGRAPH SEPARATOR 43 | 44 | // XXX: countElements("\r\n") is 1 in Swift 1.0 45 | "\r\n": "\\r\\n" 46 | 47 | ] 48 | 49 | let hexMapping: [UnicodeScalar: UInt32] = [ 50 | 51 | "0": 0x0, 52 | "1": 0x1, 53 | "2": 0x2, 54 | "3": 0x3, 55 | "4": 0x4, 56 | "5": 0x5, 57 | "6": 0x6, 58 | "7": 0x7, 59 | "8": 0x8, 60 | "9": 0x9, 61 | "a": 0xA, "A": 0xA, 62 | "b": 0xB, "B": 0xB, 63 | "c": 0xC, "C": 0xC, 64 | "d": 0xD, "D": 0xD, 65 | "e": 0xE, "E": 0xE, 66 | "f": 0xF, "F": 0xF 67 | 68 | ] 69 | 70 | let digitMapping: [UnicodeScalar:Int] = [ 71 | 72 | "0": 0, 73 | "1": 1, 74 | "2": 2, 75 | "3": 3, 76 | "4": 4, 77 | "5": 5, 78 | "6": 6, 79 | "7": 7, 80 | "8": 8, 81 | "9": 9 82 | ] 83 | 84 | public func escapeAsJSONString(source : String) -> String { 85 | 86 | var s = "\"" 87 | 88 | for c in source.characters { 89 | 90 | if let escapedSymbol = escapeMapping[c] { 91 | 92 | s.appendContentsOf(escapedSymbol) 93 | 94 | } else { 95 | 96 | s.append(c) 97 | 98 | } 99 | 100 | } 101 | 102 | s.appendContentsOf("\"") 103 | 104 | return s 105 | 106 | } 107 | 108 | func digitToInt(byte: UInt8) -> Int? { 109 | 110 | return digitMapping[UnicodeScalar(byte)] 111 | 112 | } 113 | 114 | func hexToDigit(byte: UInt8) -> UInt32? { 115 | 116 | return hexMapping[UnicodeScalar(byte)] 117 | 118 | } 119 | 120 | -------------------------------------------------------------------------------- /Mustache/Goodies/HTMLEscape.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | class HTMLEscape : MustacheBoxable { 24 | 25 | var mustacheBox: MustacheBox { 26 | // Return a multi-facetted box, because HTMLEscape interacts in 27 | // various ways with Mustache rendering. 28 | return Box( 29 | // It has a value: 30 | value: self, 31 | 32 | // HTMLEscape can be used as a filter: {{ HTMLEscape(x) }}: 33 | filter: Filter(filter), 34 | 35 | // HTMLEscape escapes all variable tags: {{# HTMLEscape }}...{{ x }}...{{/ HTMLEscape }} 36 | willRender: willRender) 37 | } 38 | 39 | // This function is used for evaluating `HTMLEscape(x)` expressions. 40 | private func filter(rendering: Rendering) throws -> Rendering { 41 | return Rendering(escapeHTML(rendering.string), rendering.contentType) 42 | } 43 | 44 | // A WillRenderFunction: this function lets HTMLEscape change values that 45 | // are about to be rendered to their escaped counterpart. 46 | // 47 | // It is activated as soon as the formatter enters the context stack, when 48 | // used in a section {{# HTMLEscape }}...{{/ HTMLEscape }}. 49 | private func willRender(tag: Tag, box: MustacheBox) -> MustacheBox { 50 | switch tag.type { 51 | case .Variable: 52 | // {{ value }} 53 | // We don't know if the box contains a String, so let's escape its 54 | // rendering. 55 | return Box(render: { (info: RenderingInfo) -> Rendering in 56 | let rendering = try box.render(info: info) 57 | return try self.filter(rendering) 58 | }) 59 | case .Section: 60 | // {{# value }}...{{/ value }} 61 | // {{^ value }}...{{/ value }} 62 | // Leave sections untouched, so that loops and conditions are not 63 | // affected by the formatter. 64 | 65 | return box 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Data/Data.swift: -------------------------------------------------------------------------------- 1 | // Data.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | public struct Data { 26 | 27 | let bytes: [UInt8] 28 | 29 | var length: Int { 30 | 31 | return bytes.count 32 | 33 | } 34 | 35 | init() { 36 | 37 | self.bytes = [] 38 | 39 | } 40 | 41 | init(bytes: UnsafePointer, length: Int) { 42 | 43 | var buffer: [UInt8] = [UInt8](count: length, repeatedValue: 0) 44 | memcpy(&buffer, bytes, length) 45 | self.bytes = buffer 46 | 47 | } 48 | 49 | init(bytes: [UInt8]) { 50 | 51 | self.bytes = bytes 52 | 53 | } 54 | 55 | init(bytes: [Int8]) { 56 | 57 | var buffer: [UInt8] = [UInt8](count: bytes.count, repeatedValue: 0) 58 | memcpy(&buffer, bytes, bytes.count) 59 | self.bytes = buffer 60 | 61 | } 62 | 63 | init(string: String) { 64 | 65 | var buffer: [UInt8] = [UInt8](count: string.utf8.count, repeatedValue: 0) 66 | memcpy(&buffer, string.bytes, string.utf8.count) 67 | self.bytes = buffer 68 | 69 | } 70 | 71 | var empty: Bool { 72 | 73 | return bytes.count == 0 74 | 75 | } 76 | 77 | } 78 | 79 | extension Data : CustomStringConvertible { 80 | 81 | public var description: String { 82 | 83 | if let string = String(data: self) { 84 | 85 | return string 86 | 87 | } else { 88 | 89 | return "Data: Unable to convert data to UTF-8 string." 90 | 91 | } 92 | 93 | } 94 | 95 | } 96 | 97 | extension Data : SequenceType { 98 | 99 | public func generate() -> IndexingGenerator<[UInt8]> { 100 | 101 | return IndexingGenerator(bytes) 102 | 103 | } 104 | 105 | } 106 | 107 | func +(lhs: Data, rhs: Data) -> Data { 108 | 109 | return Data(bytes: lhs.bytes + rhs.bytes) 110 | 111 | } 112 | 113 | func +=(inout lhs: Data, rhs: Data) { 114 | 115 | lhs = lhs + rhs 116 | 117 | } 118 | -------------------------------------------------------------------------------- /Mustache/Generation/ExpressionGenerator.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | extension Expression : CustomDebugStringConvertible { 25 | /// A textual representation of `self`, suitable for debugging. 26 | var debugDescription: String { 27 | let string = ExpressionGenerator().stringFromExpression(self) 28 | return "Expression(\(string))" 29 | } 30 | } 31 | 32 | class ExpressionGenerator { 33 | let configuration: Configuration 34 | 35 | init(configuration: Configuration? = nil) { 36 | self.configuration = configuration ?? DefaultConfiguration 37 | } 38 | 39 | func stringFromExpression(expression: Expression) -> String { 40 | buffer = "" 41 | renderExpression(expression) 42 | return buffer 43 | } 44 | 45 | func renderExpression(expression: Expression) { 46 | switch expression { 47 | case .ImplicitIterator: 48 | // {{ . }} 49 | 50 | buffer.appendContentsOf(".") 51 | 52 | case .Identifier(let identifier): 53 | // {{ identifier }} 54 | 55 | buffer.appendContentsOf(identifier) 56 | 57 | case .Scoped(let baseExpression, let identifier): 58 | // {{ .identifier }} 59 | 60 | renderExpression(baseExpression.expression) 61 | buffer.appendContentsOf(".") 62 | buffer.appendContentsOf(identifier) 63 | 64 | case .Filter(let filterExpression, let argumentExpression, _): 65 | // {{ () }} 66 | // 67 | // Support for variadic filters is not implemented: 68 | // `f(a,b)` is rendered `f(a)(b)`. 69 | 70 | renderExpression(filterExpression.expression) 71 | buffer.appendContentsOf("(") 72 | renderExpression(argumentExpression.expression) 73 | buffer.appendContentsOf(")") 74 | } 75 | } 76 | 77 | private var buffer: String = "" 78 | } 79 | -------------------------------------------------------------------------------- /Mustache/Compiling/TemplateAST/TemplateAST.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | 24 | /** 25 | The abstract syntax tree of a template 26 | */ 27 | final class TemplateAST { 28 | 29 | // A template AST can be "defined" or "undefined". 30 | // 31 | // Undefined template ASTs are used when parsing templates which embed a 32 | // partial tag which refers to themselves. The compiler would emit a 33 | // PartialNode which contains a reference to an undefined (yet) template 34 | // AST. At the end of the compilation the undefined template AST would 35 | // become defined. 36 | // 37 | // See TemplateRepository.templateAST(named:relativeToTemplateID:error:). 38 | enum Type { 39 | case Undefined 40 | case Defined(nodes: [TemplateASTNode], contentType: ContentType) 41 | } 42 | var type: Type 43 | 44 | private init(type: Type) { 45 | self.type = type 46 | } 47 | 48 | 49 | /** 50 | Returns an undefined TemplateAST. 51 | */ 52 | convenience init() { 53 | self.init(type: Type.Undefined) 54 | } 55 | 56 | /** 57 | Returns a defined TemplateAST. 58 | */ 59 | convenience init(nodes: [TemplateASTNode], contentType: ContentType) { 60 | self.init(type: Type.Defined(nodes: nodes, contentType: contentType)) 61 | } 62 | 63 | /** 64 | Returns nil if the template AST is undefined. 65 | */ 66 | var nodes: [TemplateASTNode]! { 67 | switch type { 68 | case .Undefined: 69 | return nil 70 | case .Defined(let nodes, _): 71 | return nodes 72 | } 73 | } 74 | 75 | /** 76 | Returns nil if the template AST is undefined. 77 | */ 78 | var contentType: ContentType! { 79 | switch type { 80 | case .Undefined: 81 | return nil 82 | case .Defined(_, let contentType): 83 | return contentType 84 | } 85 | } 86 | 87 | func updateFromTemplateAST(templateAST: TemplateAST) { 88 | self.type = templateAST.type 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /HTTP Method/HTTPMethod.swift: -------------------------------------------------------------------------------- 1 | // HTTPMethod.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | enum HTTPMethod { 26 | 27 | case GET 28 | case HEAD 29 | case POST 30 | case PUT 31 | case DELETE 32 | case TRACE 33 | case OPTIONS 34 | case CONNECT 35 | case PATCH 36 | 37 | case Unrecognized(method: String) 38 | 39 | init(string: String) { 40 | 41 | switch string { 42 | 43 | case "GET": self = GET 44 | case "HEAD": self = HEAD 45 | case "POST": self = POST 46 | case "PUT": self = PUT 47 | case "DELETE": self = DELETE 48 | case "TRACE": self = TRACE 49 | case "OPTIONS": self = OPTIONS 50 | case "CONNECT": self = CONNECT 51 | case "PATCH": self = PATCH 52 | default: 53 | self = Unrecognized(method: string) 54 | Log.warning("Unrecognized HTTPMethod: \(string)") 55 | 56 | } 57 | 58 | } 59 | 60 | } 61 | 62 | extension HTTPMethod: Hashable { 63 | 64 | var hashValue: Int { 65 | 66 | return description.hashValue 67 | 68 | } 69 | 70 | } 71 | 72 | func ==(lhs: HTTPMethod, rhs: HTTPMethod) -> Bool { 73 | 74 | return lhs.hashValue == rhs.hashValue 75 | 76 | } 77 | 78 | extension HTTPMethod: CustomStringConvertible { 79 | 80 | var description: String { 81 | 82 | switch self { 83 | 84 | case .GET: return "GET" 85 | case .HEAD: return "HEAD" 86 | case .POST: return "POST" 87 | case .PUT: return "PUT" 88 | case .DELETE: return "DELETE" 89 | case .TRACE: return "TRACE" 90 | case .OPTIONS: return "OPTIONS" 91 | case .CONNECT: return "CONNECT" 92 | case .PATCH: return "PATCH" 93 | case .Unrecognized(let method): return method 94 | 95 | } 96 | 97 | } 98 | 99 | } 100 | -------------------------------------------------------------------------------- /Socket Stream/SocketStream.swift: -------------------------------------------------------------------------------- 1 | // SocketStream.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | final class SocketStream : Stream { 26 | 27 | let socket: Socket 28 | let channel: DispatchChannel 29 | 30 | init(socket: Socket) { 31 | 32 | self.socket = socket 33 | self.channel = Dispatch.createChannel(.Stream, fileDescriptor: socket.fileDescriptor) { error in 34 | 35 | if let error = error { Log.error(error) } 36 | 37 | } 38 | 39 | self.channel.setLowWater(1) 40 | 41 | } 42 | 43 | func readData(handler: Data -> Void) throws { 44 | 45 | Dispatch.read(channel) { (done: Bool, buffer: UnsafePointer, length: Int, error: ErrorType?) in 46 | 47 | let data = Data(bytes: buffer, length: length) 48 | handler(data) 49 | 50 | } 51 | 52 | } 53 | 54 | func writeData(data: Data, completion: (Void -> Void)? = nil) { 55 | 56 | let buffer = UnsafePointer(data.bytes) 57 | let length = data.length 58 | 59 | Dispatch.write(channel, dataBuffer: buffer, dataLength: length) { (done: Bool, buffer: UnsafePointer, length: Int, error: ErrorType?) in 60 | 61 | completion?() 62 | 63 | } 64 | 65 | } 66 | 67 | func close() { 68 | 69 | channel.close() 70 | socket.release() 71 | 72 | } 73 | 74 | } 75 | 76 | func acceptClient(port port: Int, handleClient: (client: Stream) -> Void) throws { 77 | 78 | let socket = try Socket(port: TCPPort(port), maxConnections: 128) 79 | 80 | Dispatch.async { 81 | 82 | while true { 83 | 84 | do { 85 | 86 | let clientSocket = try socket.acceptClient() 87 | let socketStream = SocketStream(socket: clientSocket) 88 | handleClient(client: socketStream) 89 | 90 | } catch { 91 | 92 | Log.error(error) 93 | 94 | } 95 | 96 | } 97 | 98 | } 99 | 100 | } -------------------------------------------------------------------------------- /Mustache/Goodies/URLEscape.swift: -------------------------------------------------------------------------------- 1 | // The MIT License 2 | // 3 | // Copyright (c) 2015 Gwendal Roué 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | //class URLEscape : MustacheBoxable { 24 | // 25 | // var mustacheBox: MustacheBox { 26 | // // Return a multi-facetted box, because URLEscape interacts in 27 | // // various ways with Mustache rendering. 28 | // return Box( 29 | // // It has a value: 30 | // value: self, 31 | // 32 | // // URLEscape can be used as a filter: {{ URLEscape(x) }}: 33 | // filter: Filter(filter), 34 | // 35 | // // URLEscape escapes all variable tags: {{# URLEscape }}...{{ x }}...{{/ URLEscape }} 36 | // willRender: willRender) 37 | // } 38 | // 39 | // // This function is used for evaluating `URLEscape(x)` expressions. 40 | // private func filter(rendering: Rendering) throws -> Rendering { 41 | // return Rendering(URLEscape.escapeURL(rendering.string), rendering.contentType) 42 | // } 43 | // 44 | // // A WillRenderFunction: this function lets URLEscape change values that 45 | // // are about to be rendered to their escaped counterpart. 46 | // // 47 | // // It is activated as soon as the formatter enters the context stack, when 48 | // // used in a section {{# URLEscape }}...{{/ URLEscape }}. 49 | // private func willRender(tag: Tag, box: MustacheBox) -> MustacheBox { 50 | // switch tag.type { 51 | // case .Variable: 52 | // // We don't know if the box contains a String, so let's escape its 53 | // // rendering. 54 | // return Box({ (info: RenderingInfo) -> Rendering in 55 | // let rendering = try box.render(info: info) 56 | // return try self.filter(rendering) 57 | // 58 | // }) 59 | // case .Section: 60 | // return box 61 | // } 62 | // } 63 | // 64 | // private class func escapeURL(string: String) -> String { 65 | // let s = NSCharacterSet.URLQueryAllowedCharacterSet().mutableCopy() as! NSMutableCharacterSet 66 | // s.removeCharactersInString("?&=") 67 | // return string.stringByAddingPercentEncodingWithAllowedCharacters(s) ?? "" 68 | // } 69 | //} 70 | -------------------------------------------------------------------------------- /Libuv/src/unix/proctitle.c: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * Permission is hereby granted, free of charge, to any person obtaining a copy 3 | * of this software and associated documentation files (the "Software"), to 4 | * deal in the Software without restriction, including without limitation the 5 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 6 | * sell copies of the Software, and to permit persons to whom the Software is 7 | * furnished to do so, subject to the following conditions: 8 | * 9 | * The above copyright notice and this permission notice shall be included in 10 | * all copies or substantial portions of the Software. 11 | * 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 13 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 15 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 18 | * IN THE SOFTWARE. 19 | */ 20 | 21 | #include "uv.h" 22 | #include "internal.h" 23 | 24 | #include 25 | #include 26 | 27 | extern void uv__set_process_title(const char* title); 28 | 29 | static void* args_mem; 30 | 31 | static struct { 32 | char* str; 33 | size_t len; 34 | } process_title; 35 | 36 | 37 | char** uv_setup_args(int argc, char** argv) { 38 | char** new_argv; 39 | size_t size; 40 | char* s; 41 | int i; 42 | 43 | if (argc <= 0) 44 | return argv; 45 | 46 | /* Calculate how much memory we need for the argv strings. */ 47 | size = 0; 48 | for (i = 0; i < argc; i++) 49 | size += strlen(argv[i]) + 1; 50 | 51 | process_title.str = argv[0]; 52 | process_title.len = argv[argc - 1] + strlen(argv[argc - 1]) - argv[0]; 53 | assert(process_title.len + 1 == size); /* argv memory should be adjacent. */ 54 | 55 | /* Add space for the argv pointers. */ 56 | size += (argc + 1) * sizeof(char*); 57 | 58 | new_argv = uv__malloc(size); 59 | if (new_argv == NULL) 60 | return argv; 61 | args_mem = new_argv; 62 | 63 | /* Copy over the strings and set up the pointer table. */ 64 | s = (char*) &new_argv[argc + 1]; 65 | for (i = 0; i < argc; i++) { 66 | size = strlen(argv[i]) + 1; 67 | memcpy(s, argv[i], size); 68 | new_argv[i] = s; 69 | s += size; 70 | } 71 | new_argv[i] = NULL; 72 | 73 | return new_argv; 74 | } 75 | 76 | 77 | int uv_set_process_title(const char* title) { 78 | if (process_title.len == 0) 79 | return 0; 80 | 81 | /* No need to terminate, byte after is always '\0'. */ 82 | strncpy(process_title.str, title, process_title.len); 83 | uv__set_process_title(title); 84 | 85 | return 0; 86 | } 87 | 88 | 89 | int uv_get_process_title(char* buffer, size_t size) { 90 | if (process_title.len > 0) 91 | strncpy(buffer, process_title.str, size); 92 | else if (size > 0) 93 | buffer[0] = '\0'; 94 | 95 | return 0; 96 | } 97 | 98 | 99 | UV_DESTRUCTOR(static void free_args_mem(void)) { 100 | uv__free(args_mem); /* Keep valgrind happy. */ 101 | args_mem = NULL; 102 | } 103 | -------------------------------------------------------------------------------- /Libuv/include/uv-darwin.h: -------------------------------------------------------------------------------- 1 | /* Copyright Joyent, Inc. and other Node contributors. All rights reserved. 2 | * 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy 4 | * of this software and associated documentation files (the "Software"), to 5 | * deal in the Software without restriction, including without limitation the 6 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 7 | * sell copies of the Software, and to permit persons to whom the Software is 8 | * furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in 11 | * all copies or substantial portions of the Software. 12 | * 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 19 | * IN THE SOFTWARE. 20 | */ 21 | 22 | #ifndef UV_DARWIN_H 23 | #define UV_DARWIN_H 24 | 25 | #if defined(__APPLE__) && defined(__MACH__) 26 | # include 27 | # include 28 | # include 29 | # include 30 | # define UV_PLATFORM_SEM_T semaphore_t 31 | #endif 32 | 33 | #define UV_IO_PRIVATE_PLATFORM_FIELDS \ 34 | int rcount; \ 35 | int wcount; \ 36 | 37 | #define UV_PLATFORM_LOOP_FIELDS \ 38 | uv_thread_t cf_thread; \ 39 | void* _cf_reserved; \ 40 | void* cf_state; \ 41 | uv_mutex_t cf_mutex; \ 42 | uv_sem_t cf_sem; \ 43 | void* cf_signals[2]; \ 44 | 45 | #define UV_PLATFORM_FS_EVENT_FIELDS \ 46 | uv__io_t event_watcher; \ 47 | char* realpath; \ 48 | int realpath_len; \ 49 | int cf_flags; \ 50 | uv_async_t* cf_cb; \ 51 | void* cf_events[2]; \ 52 | void* cf_member[2]; \ 53 | int cf_error; \ 54 | uv_mutex_t cf_mutex; \ 55 | 56 | #define UV_STREAM_PRIVATE_PLATFORM_FIELDS \ 57 | void* select; \ 58 | 59 | #define UV_HAVE_KQUEUE 1 60 | 61 | #endif /* UV_DARWIN_H */ 62 | -------------------------------------------------------------------------------- /HTTP Server/HTTPServer.swift: -------------------------------------------------------------------------------- 1 | // HTTPServer.swift 2 | // 3 | // The MIT License (MIT) 4 | // 5 | // Copyright (c) 2015 Zewo 6 | // 7 | // Permission is hereby granted, free of charge, to any person obtaining a copy 8 | // of this software and associated documentation files (the "Software"), to deal 9 | // in the Software without restriction, including without limitation the rights 10 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | // copies of the Software, and to permit persons to whom the Software is 12 | // furnished to do so, subject to the following conditions: 13 | // 14 | // The above copyright notice and this permission notice shall be included in all 15 | // copies or substantial portions of the Software. 16 | // 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 | // SOFTWARE. 24 | 25 | /** 26 | HTTPServer parses HTTPRequest from the client and passes it to the respond function. The respond function returns 27 | an HTTPResponse which is serialized by the HTTPServer and sent back to the client. 28 | */ 29 | struct HTTPServer : Server { 30 | 31 | let runLoop: RunLoop = FakeRunLoop() 32 | let acceptTCPClient = acceptClient 33 | let parseRequest = HTTPRequestParser.parseRequest 34 | let respond: HTTPRequest -> HTTPResponse 35 | let serializeResponse = HTTPResponseSerializer.serializeResponse 36 | 37 | /** 38 | Initializes an HTTPServer with a function that receives an HTTPRequest and returns an HTTPResponse. 39 | 40 | Below we have an example of a simple respond function that always returns an HTTPResponse with status 200 OK. 41 | 42 | let respond = { (request: HTTPRequest) in 43 | 44 | return HTTPResponse(status: .OK) 45 | 46 | } 47 | 48 | let server = HTTPServer(respond: respond) 49 | server.start() 50 | 51 | The most common way to create a respond function is through an HTTPRouter. Because the HTTPRouter can throw errors 52 | we have to associate it with a function that can turns an error into an HTTPResponse. 53 | 54 | The framework provides HTTPError.respondError but you can write your own function if you wish. We associate 55 | HTTPRouter with HTTPError.respondError through the >>> operator. 56 | 57 | let respond = HTTPRouter { router in 58 | 59 | router.get("/ok") { (request: HTTPRequest) in 60 | 61 | return HTTPResponse(status: .OK) 62 | 63 | } 64 | 65 | router.get("/fail") { (request: HTTPRequest) in 66 | 67 | return HTTPResponse(status: .BadRequest) 68 | 69 | } 70 | 71 | } >>> HTTPError.respondError 72 | 73 | let server = HTTPServer(respond: respond) 74 | server.start() 75 | 76 | - parameter respond: function that turns an HTTPRequest into an HTTPResponse 77 | - returns: HTTPServer 78 | */ 79 | init(respond: HTTPRequest -> HTTPResponse) { 80 | 81 | self.respond = respond >>> Middleware.keepAlive >>> Middleware.addHeaders(["server": "HTTP Server"]) 82 | 83 | } 84 | 85 | } --------------------------------------------------------------------------------