Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing not parsing form encoded values #57

Merged
merged 1 commit into from
Feb 23, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 49 additions & 2 deletions Sources/RequestData+Target.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2016 Tanner Nelson. All rights reserved.
//

import Foundation
import PureJsonSerializer

/**
Expand Down Expand Up @@ -39,8 +40,20 @@ public extension Request {
public let json: Json?

internal init(query: [String : String] = [:], bytes: [UInt8]) {
self.query = query
self.json = try? Json.deserialize(bytes)
var mutableQuery = query

do {
self.json = try Json.deserialize(bytes)
} catch {
self.json = nil

// Will overwrite keys if they are duplicated from `query`
Data.parsePostData(bytes).forEach { key, val in
mutableQuery[key] = val
}
}

self.query = mutableQuery
}

// MARK: Subscripting
Expand All @@ -52,6 +65,40 @@ public extension Request {
public subscript(idx: Int) -> Node? {
return json?[idx]
}

/**
Checks for form encoding of body if Json fails

- parameter body: byte array from body

- returns: a key value pair dictionary
*/
static func parsePostData(body: [UInt8]) -> [String: String] {
if let bodyString = NSString(bytes: body, length: body.count, encoding: NSUTF8StringEncoding) {
return self.parseData(bodyString.description)
}

return [:]
}

/**
Parses `key=value` pair data separated by `&`.

- returns: String dictionary of parsed data
*/
static func parseData(string: String) -> [String: String] {
var data: [String: String] = [:]

for pair in string.split("&") {
let tokens = pair.split(1, separator: "=")

if let name = tokens.first, value = tokens.last {
data[name.removePercentEncoding()] = value.removePercentEncoding()
}
}

return data
}
}
}

Expand Down