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

Headers not being assigned #55

Open
NicosKaralis opened this issue Nov 19, 2018 · 2 comments
Open

Headers not being assigned #55

NicosKaralis opened this issue Nov 19, 2018 · 2 comments

Comments

@NicosKaralis
Copy link

After some debugging I've found that the headers setter is calling the getter which calls a dynamic getter and ignores the new values

This declaration

    open var headers : [String: String]? {
        get { return socket.request.allHTTPHeaderFields }
        set {
            for (field, value) in headers ?? [:] {
                socket.request.setValue(value, forHTTPHeaderField: field)
            }
        }
    }

Should be replaced with this

    open var headers : [String: String]? {
        get { return socket.request.allHTTPHeaderFields }
        set {
            for (field, value) in newValue ?? [:] {
                socket.request.setValue(value, forHTTPHeaderField: field)
            }
        }
    }

If you need a test just run this example

  let client: ActionCableClient = {
    let _client = ActionCableClient(url: Defaults.Api.cableURL)
    _client.headers = [
      "Origin": "https://server.example"
    ]
    return _client
  }()
  print(client.headers)
  client.headers = [
    "Origin": "https://server.example",
    "Accept": "accept/json"
  ]
  print(client.headers)
  if !client.isConnected {
    client.connect()
  }

https://github.com/danielrhodes/Swift-ActionCableClient/blob/master/Source/Classes/ActionCableClient.swift#L77

@NicosKaralis
Copy link
Author

After more headscreatching I've found another problem with variable names.

My server handles authentication via the Authorization header and it rejects the connection if the token is not right. But for some reason the client never got the message and kept retrying

This was the offending code on Error.swift:

    init(from error: Swift.Error) {
      switch error._code {

._code is a private variable and should not be used to identify the error. Since the error received is a WSError a more sane approach would be.

    init(from error: Swift.Error) {
      let error_code: Int
      if let ws_error = error as? WSError {
        error_code = ws_error.code
      } else {
        error_code = error._code
      }
      
      switch error_code {

We look for an WSError if it is not the correct class then we use a fallback

I'll update the pull request to include this fix

@nerzh
Copy link

nerzh commented Aug 31, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants