-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fixed "invalid HTTP upgrade" issue #301
Conversation
@Elethier do you happen to know what these "garbage" bytes were and where they are coming from? Seems peculiar the server would send that in it's HTTP response randomly and it would be nice to know what exactly those bytes are, before just ignoring them. Do have a way to reproduce the behavior? Love to look at it on my end as well. |
@acmacalister I'll have to spend some time to reproduce the issue, as we've had this fix in Swift 2.2 for a while, and I finally had time to migrate it. Here's some information about what I was seeing at the time:
I am still unsure if the extra 6 bytes was a server-side problem, but the Wireshark packet capture seems to suggest it's a client-side issue. What's weird is that I was clearly seeing a 6-byte fragment coming out of Apple's NSStream API when it shouldn't have. The only conclusion I can come up with is that there's a bug in Apple's code somewhere. Also, it looks like this problem may have shown up in another person's app, since a Github issue was opened about it. Sadly, I do not have a good way to reproduce the behavior, as our app's server is a piece of hardware running custom firmware. I'm not sure I can give you any good information on what it's doing, unfortunately. |
Just as an FYI, we're also seeing this behavior. We're using starscream with an elixir/phoenix server via PhoenixSwiftClient in case that helps. |
@Elethier @acmacalister FYI, we tried using the above fix and it resolved the issue for us. Any thoughts on getting this merged in? I agree that it'd be nice to know why this is happening before blindly fixing it, but I'm not sure there are any adverse affects of having this in in the meantime. |
Woops, should have pinged @daltoniam I think. |
@Elethier @markquezada I appreciate the info. I understand the desire to merge this, but here is my line of thinking. Just ignoring the payload up the point of looking for the start of an
right after this line: https://github.com/daltoniam/Starscream/blob/master/Source/WebSocket.swift#L454 My theory is that the memory is being reallocated over the top of old memory and is not zero filled, leading toward those useless bytes showing up in the input stream. |
@acmacalister Ah, interesting. If your theory is true, that could be a pretty bad bug right? Totally agree with your assessment. We'll try investigating a bit more and report back. Thanks for the info! |
Yeah, it would certainly be a bug. A fairly common one when working in language that have unsafe memory (aka C). The thought process had been that the |
@acmacalister I like this plan, it would make much more sense that it's a bug within the socket library. I'll investigate and let you know if your proposed change fixes the issues we're seeing. |
@acmacalister Just as an FYI, we tried adding the line you mentioned but it didn't seem to have any effect. We're still seeing the "invalid HTTP upgrade" error. @Elethier did you have any luck? |
@markquezada @acmacalister I'm still seeing the error show up, unfortunately. @acmacalister you meant adding But the idea of an NSData object not being cleared might still be valid. I haven't looked at it too much, but what happens to the fragBuffer object on a disconnect? Is it cleared? Because the garbage bytes I was seeing were always within the fragBuffer. Maybe adding I'll have to wait until tomorrow to test it, though. |
I didn't think the You could see if replacing these lines with let buffer = calloc(BUFFER_MAX, MemoryLayout<UInt8>.size).assumingMemoryBound(to: UInt8.self) if you're concerned about unzero'd memory, but the read should be overwriting any garbage bytes at the start of the buffer anyway, I would think. Also the documentation for NSMutableData does not say calling Also open source Foundation implementation for NSMutableData just ignores the capacity parameter. |
@nuclearace The |
Ah yeah, if that frag buffer isn't cleared it might contain some stale data. I never reuse the same socket so I wouldn't have run into this issue most likely. |
I checked in some cleanup stuff to master. I think it might fix this but clearing the fragBuffer. Let me know if that works and we will cut a new version with the fix! |
… swiftdiscord * 'master' of https://github.com/daltoniam/Starscream: possible fix for daltoniam#301 connect delegate before firing message delegates. Fixed disconnect getting called before connect Fixed issues with not cancelling block operations properly Added guard against multiple disconnects
@daltoniam Looks like your change fixed the issues on my end. @markquezada, did you try the fix on your end? |
@Elethier Looks like we're still experiencing this issue, even with the code from master. Not sure what to try next. |
@markquezada When I have some time, I'll retest the fix. We only see the issue sporadically, so maybe I got lucky. |
@markquezada Just retested today, I'm not seeing "Invalid HTTP Upgrade" with the code on the master branch. How are you testing this issue? I put a breakpoint on line 545 in Websocket.swift: |
@Elethier sorry about the delay. We ended up doing more testing and found out that the issue does seem to be fixed. We do still get an "Invalid HTTP Upgrade" message but we realized that it's occurring at the proper time in response to a @daltoniam 👍 from me to ship a release. Looks like the work you did in master fixed this for us. Thank you! |
@markquezada Good to hear that the changes fixed your problems, too. @daltoniam Thanks for fixing this in the master branch. I'll close this pull request. |
Hi guys. I updated my code from Here was my code using NSURL
now I am using code below
I have integrated the library using pods. is this issue fixed in pods too. https://www.dropbox.com/s/wp6cf0ftl8cmjtk/Screenshot%202017-02-02%2019.17.33.png?dl=0 |
@qadirsuh The fix for this issue has not made it into a release yet. Can you confirm that you have the changes in this commit in your local version of Starscream? If you're using version 2.0.2, I don't think this change will be in your project. Perhaps now would be a good time to issue another release... |
@Elethier yes I am using 2.0.2. just confirmed it from pod.lock file
Here is my pod.lock file
|
@qadirsuh As I said, the changes to fix "invalid HTTP upgrade" aren't in 2.0.2, they're just on master. I recommend you replace this line in your Podfile:
with this:
This will let you use the pre-release version. But use this version with caution; it's better to use a release tag when you can. |
Thanks @Elethier for pointing this, I have changed my pod as you suggested above. I am still experiencing the same error
pod.lock file now showing like below.
did I miss any thing still? |
… swiftdiscord * 'master' of https://github.com/daltoniam/Starscream: updated changelog version bump possible fix for daltoniam#301 connect delegate before firing message delegates. Fixed disconnect getting called before connect Fixed issues with not cancelling block operations properly Added guard against multiple disconnects
I was getting occasional errors from my app stating "invalid HTTP upgrade". It sounds like a few other users of the library were also seeing this issue. I tracked down the problem: there were some extra garbage bytes at the beginning of the HTTP response from the server. These garbage bytes come out of NSStream, and are identified as a leading fragment for the response packet. The extra bytes didn't show up when I was inspecting the packets using Wireshark.
To fix the issue, I added some code in
processHTTP
to look for the start of the response, and ignore any bytes before the response.I should also note that I was having trouble with running the tests, as stated in issue #297. Let me know if there's any test failures.
@daltoniam Please review.