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

UserAgent is optional in defaultHTTPHeaders #1100

Closed
shlowdy opened this issue Mar 7, 2016 · 3 comments
Closed

UserAgent is optional in defaultHTTPHeaders #1100

shlowdy opened this issue Mar 7, 2016 · 3 comments
Assignees
Milestone

Comments

@shlowdy
Copy link

shlowdy commented Mar 7, 2016

Hello
In Manager.defaultHTTPHeaders print out the userAgent

    // User-Agent Header; see https://tools.ietf.org/html/rfc7231#section-5.5.3
    let userAgent: String = {
        if let info = NSBundle.mainBundle().infoDictionary {
            let executable = info[kCFBundleExecutableKey as String] ?? "Unknown"
            let bundle = info[kCFBundleIdentifierKey as String] ?? "Unknown"
            let version = info[kCFBundleVersionKey as String] ?? "Unknown"
            let os = NSProcessInfo.processInfo().operatingSystemVersionString ?? "Unknown"
            var mutableUserAgent = NSMutableString(string: "\(executable)/\(bundle) (\(version); OS \(os))") as CFMutableString
            let transform = NSString(string: "Any-Latin; Latin-ASCII; [:^ASCII:] Remove") as CFString
            if CFStringTransform(mutableUserAgent, UnsafeMutablePointer<CFRange>(nil), transform, false) {
                return mutableUserAgent as String
            }
        }
        return "Alamofire"
    }()
    print("useragent__\(userAgent)")
    return ["Accept-Encoding": acceptEncoding, "Accept-Language": acceptLanguage, "User-Agent": userAgent]

the output
useragent__Optional(Hello)/Optional(com.test.Hello) (Optional(1); OS Version 9.2 (Build 13C75))

It turns out that the executable, bundle, version is Optional
info[kCFBundleExecutableKey as String] as? String ?? "Unknown"
will be ok

xcode 7.2.1 iOS 9.2.1

ignore me if you already know that^^

@cnoon
Copy link
Member

cnoon commented Mar 9, 2016

Hmmmm...doesn't seem possible @shlowdy. The type on userAgent is String, not String?. Also, I haven't seen the optional arguments in the User-Agent header before.

@shlowdy
Copy link
Author

shlowdy commented Mar 11, 2016

Sorry about the title.
The userAgent is String indeed.
What I mean is these three
let executable = info[kCFBundleExecutableKey as String] ?? "Unknown"
let bundle = info[kCFBundleIdentifierKey as String] ?? "Unknown"
let version = info[kCFBundleVersionKey as String] ?? "Unknown"
they are optional
If you do this
let executable: String = info[kCFBundleExecutableKey as String] ?? "Unknown"
you will get a error

cnoon added a commit that referenced this issue Mar 19, 2016
@cnoon
Copy link
Member

cnoon commented Mar 19, 2016

Ah...thanks for clarifying @shlowdy. I definitely see what you mean. The logic works fine as is, since it's using either the AnyObject coming from the dictionary or defaulting to a String which is then stored as a String. Then, when substituting into the new mutable string, each AnyObject's `description is used to build the final String. All this logic works correctly, but can be a bit misleading.

In order to simplify, I just pushed 3052587 to make things a bit easier to digest. Hopefully that helps clarify things.

Cheers. 🍻

@cnoon cnoon closed this as completed Mar 19, 2016
@cnoon cnoon added this to the 3.3.0 milestone Mar 19, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants