c++ - How to interpret the browser's Sec-websocket-key for server handshake? -
i have written basic http server in c++ supports regular websites, trying extend support websockets. of networking , parsing done own code, no libraries -- except copied , pasted code computing sha1 , converting base64.
i reading several guides describe how handshake incoming websocket connection, such this: http://www.altdev.co/2012/01/23/writing-your-own-websocket-server/ , http://enterprisewebbook.com/ch8_websockets.html
i stuck on part server needs take key client's header , append magic string onto it, hash sha1, encode in base64, , send result part of accept header.
the specific part giving me trouble '==' in key, , '=' in resulting string sent server. can't find information stating these -- drop == key before appending magic string it, or keep them on? also, can't figure out '=' @ end of final server answer comes -- , why server's answer has single '=' , client's key has 2 '='
lastly, confused why magic string server uses in base16, key sent client's browser base64 -- need convert server string base 64 before appending it, or not matter?
and when convert base 64, converting if each ascii symbol digit (so 'a' in string 10), or converting binary representation of entire string string representing original binary digits of base64?
sorry if stupid question, don't web programming it's confusing me when articles assume reader knows part of information.
pseudo code:
key_decoded = base64_decode(key64) magic = "258eafa5-e914-47da-95ca-c5ab0dc85b11" response = sha1(append(key_decoded, magic)) response_encoded = base64_encode(response) the '=' @ end of base64 encoded data there pad data required length. don't have it. (see here: why base64 encoded string have = sign @ end) different lengths of input data may require different amounts of padding, hence 2 encoded values have different amounts of padding in example.
when convert base64, don't assume input data, input bytes. not convert 'a' 10 (you seem thinking ascii characters supposed representing hexadecimal digits, unrelated)
Comments
Post a Comment