Skip to content

Commit

Permalink
Merge pull request #382 from qutheory/request-parser
Browse files Browse the repository at this point in the history
request parser
  • Loading branch information
tanner0101 committed Jun 13, 2016
2 parents f515340 + 93c43da commit 4673559
Show file tree
Hide file tree
Showing 87 changed files with 2,300 additions and 1,238 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Expand Up @@ -8,6 +8,9 @@ osx_image: xcode7.3
install:
- eval "$(curl -sL https://gist.githubusercontent.com/kylef/5c0475ff02b7c7671d2a/raw/02090c7ede5a637b76e6df1710e83cd0bbe7dcdf/swiftenv-install.sh)"
script:
# Fetch
- swift package fetch
- rm -rf Sources/Vapor/Generator
# Build Vapor
- swift build --configuration debug
- swift build --configuration release
Expand Down
7 changes: 1 addition & 6 deletions Package.swift
Expand Up @@ -6,21 +6,16 @@ let package = Package(
//Standards package. Contains protocols for cross-project compatability.
.Package(url: "https://github.com/open-swift/S4.git", majorVersion: 0, minor: 10),

//Provides critical String functions Foundation is missing on Linux
.Package(url: "https://github.com/Zewo/String.git", majorVersion: 0, minor: 7),

//Parses and serializes JSON - using fork until update core library
.Package(url: "https://github.com/qutheory/pure-json.git", majorVersion: 2, minor: 5),

//SHA2 + HMAC hashing. Used by the core to create session identifiers.
.Package(url: "https://github.com/CryptoKitten/HMAC.git", majorVersion: 0, minor: 7),
.Package(url: "https://github.com/CryptoKitten/SHA2.git", majorVersion: 0, minor: 7),

//Websockets
.Package(url: "https://github.com/CryptoKitten/SHA1.git", majorVersion: 0, minor: 7),

//Determines Content-Type for file extensions
.Package(url: "https://github.com/Zewo/MediaType.git", majorVersion: 0, minor: 9),

//ORM for interacting with databases
.Package(url: "https://github.com/qutheory/fluent.git", majorVersion: 0, minor: 6),

Expand Down
18 changes: 12 additions & 6 deletions Sources/Development/main.swift
Expand Up @@ -10,6 +10,12 @@ var workDir: String {
let config = Config(seed: JSON.object(["port": "8000"]), workingDirectory: workDir)
let app = Application(workDir: workDir, config: config)

let 😀: Response = Response(status: .ok)

app.get("ping") { _ in
return 😀
}

//MARK: Basic

app.get("/") { request in
Expand Down Expand Up @@ -44,8 +50,8 @@ app.socket("socket") { request, ws in
}
}

ws.onClose = { data in
print("Did close w/ packet \(data)")
ws.onClose = { ws, status, reason, clean in
print("Did close w/ status \(status) reason \(reason)")
}
}

Expand Down Expand Up @@ -267,10 +273,10 @@ app.post("multipart-image") { request in
throw Abort.badRequest
}

var headers: Response.Headers = [:]
var headers: Headers = [:]

if let mediaType = image.type {
headers["Content-Type"] = mediaType.type + "/" + mediaType.subtype
headers["Content-Type"] = mediaType
}

return Response(status: .ok, headers: headers, data: image.data)
Expand Down Expand Up @@ -306,10 +312,10 @@ app.post("multifile") { request in

let file = files[number]

var headers: Response.Headers = [:]
var headers: Headers = [:]

if let mediaType = file.type {
headers["Content-Type"] = mediaType.type + "/" + mediaType.subtype
headers["Content-Type"] = mediaType
}

return Response(status: .ok, headers: headers, data: file.data)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Config/Config.swift
Expand Up @@ -66,7 +66,7 @@ public class Config {
// --config:passwords.mongo-password=password
// --config:<name>.<path>.<to>.<value>=<actual-value>
let cliDirectory = Config.makeCLIConfig(arguments: arguments)
prioritizedDirectories.append(cliDirectory)
prioritizedDirectories.insert(cliDirectory, at: 0) // should be most important

// Json files are loaded in order of priority
// it will go like this
Expand Down
2 changes: 0 additions & 2 deletions Sources/Vapor/Content/Content.swift
@@ -1,5 +1,3 @@
import MediaType

@_exported import PathIndexable

public protocol RequestContentSubscript {}
Expand Down
605 changes: 605 additions & 0 deletions Sources/Vapor/Content/MediaType.swift

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions Sources/Vapor/Core/Application.swift
@@ -1,5 +1,4 @@
import libc
import MediaType
import Foundation
import Socks

Expand Down Expand Up @@ -177,10 +176,10 @@ public class Application {
]

self.router = routerProvided ?? BranchRouter()
self.server = serverProvided ?? StreamServer<
self.server = serverProvided ?? HTTPServer<
SynchronousTCPServer,
HTTPParser,
HTTPSerializer
HTTPRequestParser,
HTTPResponseSerializer
>.self

routes = []
Expand All @@ -204,7 +203,7 @@ public class Application {

private func restrictLogging(for environment: Environment) {
guard config.environment == .production else { return }
Log.info("Production environment detected, disabling information logs.")
console.output("Production mode enabled, disabling informational logs.", style: .info)
Log.enabledLevels = [.error, .fatal]
}
}
Expand Down Expand Up @@ -290,9 +289,7 @@ extension Sequence where Iterator.Element == String {
for item in self {
let search = "--\(string)="
if item.hasPrefix(search) {
var item = item
item.replace(string: search, with: "")
return item
return item.replacingOccurrences(of: search, with: "")
}
}

Expand All @@ -303,10 +300,12 @@ extension Sequence where Iterator.Element == String {
extension Application {
internal func serve() {
do {
Log.info("Server starting at \(host):\(port)")
console.output("Server starting at \(host):\(port)", style: .info)
// noreturn
let server = try self.server.init(host: host, port: port, responder: self)
try server.start()
} catch ServerError.bindFailed {
console.output("Could not bind to port \(port), it may be in use or require sudo.", style: .error)
} catch {
Log.error("Server start error: \(error)")
}
Expand All @@ -325,11 +324,11 @@ extension Application {
// File exists
if let fileBody = try? FileManager.readBytesFromFile(filePath) {
return Request.Handler { _ in
var headers: Response.Headers = [:]
var headers: Headers = [:]

if
let fileExtension = filePath.components(separatedBy: ".").last,
let type = mediaType(forFileExtension: fileExtension)
let type = mediaTypes[fileExtension]
{
headers["Content-Type"] = type.description
}
Expand Down
65 changes: 0 additions & 65 deletions Sources/Vapor/Fixes/StringExtensions.swift

This file was deleted.

Expand Up @@ -4,7 +4,7 @@ class FormURLEncodedMiddleware: Middleware {
if
case .buffer(let data) = request.body,
let contentType = request.contentType
where contentType.contains("application/x-www-form-urlencoded")
where contentType.contains("application/x-www-form-urlencoded")
{
var request = request
request.formURLEncoded = StructuredData(formURLEncoded: data)
Expand Down

0 comments on commit 4673559

Please sign in to comment.