Skip to content

Commit

Permalink
Merge pull request #18 from noppoMan/fix-query-protocol-bug
Browse files Browse the repository at this point in the history
make correct requests for query protocol
  • Loading branch information
noppoMan authored May 15, 2017
2 parents 2e52f69 + ac4c8c1 commit 6ad0477
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
35 changes: 29 additions & 6 deletions Sources/Core/AWSClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import Dispatch
import SwiftyJSON
import Prorsum

extension Characters {
public static let uriAWSQueryAllowed: Characters = ["!", "$", "&", "\'", "(", ")", "*", "+", "-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "=", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "~"
]
}

extension Prorsum.Body {
func asData() -> Data {
switch self {
Expand Down Expand Up @@ -153,7 +158,7 @@ public struct AWSClient {
responseBody = .json(dictionary)
}

case .restxml:
case .restxml, .query:
let xmlNode = try XML2Parser(data: data).parse()
responseBody = .xml(xmlNode)

Expand All @@ -166,9 +171,6 @@ public struct AWSClient {
default:
responseBody = .buffer(data)
}

default:
responseBody = .buffer(data)
}
}

Expand All @@ -183,6 +185,9 @@ public struct AWSClient {
var message: String?

if responseBody.isXML() {
if let dict = bodyDict["ErrorResponse"] as? [String: Any] {
bodyDict = dict
}
let errorDict = bodyDict["Error"] as? [String: Any]
code = errorDict?["Code"] as? String
message = errorDict?["Message"] as? String
Expand Down Expand Up @@ -223,6 +228,9 @@ public struct AWSClient {
outputDict = try JSONSerialization.jsonObject(with: str.data(using: .utf8)!, options: []) as? [String: Any] ?? [:]
if let childOutputDict = outputDict[operationName+"Response"] as? [String: Any] {
outputDict = childOutputDict
if let childOutputDict = outputDict[operationName+"Result"] as? [String: Any] {
outputDict = childOutputDict
}
}

case .buffer(let data):
Expand Down Expand Up @@ -290,14 +298,29 @@ public struct AWSClient {
}

case .query:
let dict = try ctx.input.serializeToDictionary()
let params = dict.map({ "\($0.key)=\($0.value)" }).joined(separator: "&")
var dict = try ctx.input.serializeToDictionary()
dict["Action"] = operationName
dict["Version"] = apiVersion

var queryItems = [String]()
let keys = Array(dict.keys).sorted {$0.localizedCompare($1) == ComparisonResult.orderedAscending }

for key in keys {
if let value = dict[key] {
queryItems.append("\(key)=\(value)")
}
}

let params = queryItems.joined(separator: "&").percentEncoded(allowing: Characters.uriAWSQueryAllowed)

if path.contains("?") {
path += "&" + params
} else {
path += "?" + params
}

body = .text(params)

case .restxml:
if let payload = ctx.Shape.payload, let payloadBody = mirror.getAttribute(forKey: payload.toSwiftVariableCase()) {
body = Body(anyValue: payloadBody)
Expand Down
3 changes: 3 additions & 0 deletions Sources/Core/Signers/V4.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ extension Signers {
}
}


queries = queries.sorted { $0.name.localizedCompare($1.name) == ComparisonResult.orderedAscending }

let url = URL(string: url.absoluteString.components(separatedBy: "?")[0]+"?"+queries.asStringForURL)!

let sig = signature(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Core/XML/XMLNodeSerializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class XMLNodeSerializer {
return jsonStr
}

return "{" + _serialize(nodeTree: [node]) + "}"
return ("{" + _serialize(nodeTree: [node]) + "}").replacingOccurrences(of: "\n", with: "", options: .regularExpression)

}
}
Expand Down

0 comments on commit 6ad0477

Please sign in to comment.