mccutchen / go-httpbin

A reasonably complete and well-tested golang port of httpbin, with zero dependencies outside the go stdlib.
https://httpbingo.org
MIT License
596 stars 124 forks source link

/websocket/echo throws status code 402 #192

Closed aprilahijriyan closed 1 week ago

aprilahijriyan commented 1 week ago
image

is there something wrong?

mccutchen commented 1 week ago

@aprilahijriyan what client are you using to connect?

The issue here — I'm almost certain — is that the client you're using is not including a User-Agent header on the initial HTTP request, which is hitting this (admittedly heavy-handed) "spam" heuristic in the public https://httpbingo instance and getting rejected with the 402 Payment Required response:

        case ua == "":
            // https://github.com/mccutchen/httpbingo.org/issues/5
            //
            // this is more aggressive than strictly necessary for that particular
            // traffic pattern, but it seems reasonable to me to reject all traffic
            // that doesn't include at least *some* User-Agent identifier
            return true

As noted in https://github.com/mccutchen/httpbingo.org/issues/5, this was added to protect that public instance from some abusive traffic.

I'll push up a tweak to that heuristic to allow empty user agents for badly behaved websocket clients (I see from local testing that websocat — a fairly prominent CLI client — also fails to send a User-Agent header, so maybe this is a common problem.

mccutchen commented 1 week ago

Fix was committed in https://github.com/mccutchen/httpbingo.org/commit/eb8d654bf54dd2b2da7a224304cf01af2f0aed3e and deployed to https://httpbingo.org. Please let me know if it works for you (and please do let me know what websocket client you're using)!

Testing with websocat, which also failed with an HTTP 402 error, now works as expected:

$ websocat wss://httpbingo.org/websocket/echo   
hello
hello
world
world
aprilahijriyan commented 1 week ago

@mccutchen Thanks for response.

It seems like the main problem is with the API client I'm using. I'm using Postman.

I am currently creating an API Gateway project. I am using your project as a test case. I am running your project on my local server using Docker.

mccutchen commented 1 week ago

I am running your project on my local server using Docker.

Are you sure you're running "plain" go-httpbin on your local server? There's no code anywhere in go-httpbin that would cause it to respond with the 402 Payment Required response to the initial websocket HTTP request, but there was code that would respond this way in https://github.com/mccutchen/httpbingo.org until the fix mentioned above.

If you're still having this problem, I'd be happy to help debug further if you can write down the steps to reproduce!

aprilahijriyan commented 1 week ago

I am using the docker image from https://hub.docker.com/r/mccutchen/go-httpbin (Is this yours?)

The error above is no longer there if I test it again with httpbingo.org.

mccutchen commented 1 week ago

I think there's been some confusion here:

From my end, I am confident that this has been resolved.

Thanks again for the report, and please let me know if you're still having issues!