Skip to content

Commit

Permalink
Fix some serialization issues
Browse files Browse the repository at this point in the history
Summary:
Fixes T50478.

- Fix unicode serialization inside `Dropbox-Api-Arg` headers
- Fix serialization of unions with an associated struct value.

Reviewers: kannan, leahculver

Reviewed By: leahculver

Subscribers: kelkabany

Maniphest Tasks: T50478

Differential Revision: https://tails.corp.dropbox.com/D125815
  • Loading branch information
Ryan Pearl committed Jul 27, 2015
1 parent 0c82abb commit 8fc2eed
Show file tree
Hide file tree
Showing 3 changed files with 1,041 additions and 97 deletions.
26 changes: 19 additions & 7 deletions Source/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,24 @@ public enum CallError<ErrorType> : Printable {
}
}

func utf8Decode(data: NSData) -> String? {
if let nsstring = NSString(data: data, encoding: NSUTF8StringEncoding) {
return nsstring as String
} else {
return nil
func utf8Decode(data: NSData) -> String {
return NSString(data: data, encoding: NSUTF8StringEncoding)! as String
}

func asciiEscape(s: String) -> String {
var out : String = ""

for char in s.unicodeScalars {
var esc = "\(char)"
if !char.isASCII() {
esc = NSString(format:"\\u%04x", char.value) as String
} else {
esc = "\(char)"
}
out += esc

}
return out
}


Expand Down Expand Up @@ -144,7 +156,7 @@ public class BabelUploadRequest<RType : JSONSerializer, EType : JSONSerializer>
mutableRequest.addValue("application/octet-stream", forHTTPHeaderField: "Content-Type")
mutableRequest.HTTPBody = body
if let data = dumpJSON(params) {
let value = utf8Decode(data)
let value = asciiEscape(utf8Decode(data))
mutableRequest.addValue(value, forHTTPHeaderField: "Dropbox-Api-Arg")
}

Expand Down Expand Up @@ -188,7 +200,7 @@ public class BabelDownloadRequest<RType : JSONSerializer, EType : JSONSerializer
requestEncoder: ({ convertible, _ in
var mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
if let data = dumpJSON(params) {
let value = utf8Decode(data)
let value = asciiEscape(utf8Decode(data))
mutableRequest.addValue(value, forHTTPHeaderField: "Dropbox-Api-Arg")
}

Expand Down
Loading

0 comments on commit 8fc2eed

Please sign in to comment.