Permalink
Browse files

fixes #78

  • Loading branch information...
1 parent eb79122 commit 6c9d8143928c21eee8da60883a580e1831d664a5 @zaphoyd zaphoyd committed Apr 7, 2012
Showing with 17 additions and 7 deletions.
  1. +7 −4 src/md5/md5.hpp
  2. +10 −3 src/processors/hybi_legacy.hpp
View
@@ -35,16 +35,19 @@ namespace websocketpp {
// could be compiled separately
inline std::string md5_hash_string(const std::string& s) {
- char digest[17];
+ char digest[16];
md5_state_t state;
md5_init(&state);
md5_append(&state, (const md5_byte_t *)s.c_str(), s.size());
md5_finish(&state, (md5_byte_t *)digest);
-
- digest[16] = '\0';
- return std::string(digest);
+
+ std::string ret;
+ ret.resize(16);
+ std::copy(digest,digest+16,ret.begin());
+
+ return ret;
}
const char hexval[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
@@ -33,6 +33,8 @@
#include "../md5/md5.hpp"
#include "../network_utilities.hpp"
+#include <cassert>
+
namespace websocketpp {
namespace processor {
@@ -68,12 +70,17 @@ class hybi_legacy : public processor_base {
decode_client_key(request.header("Sec-WebSocket-Key2"), &key_final[4]);
// copy key3 into final key
- std::copy(request.header("Sec-WebSocket-Key3").c_str(),
- request.header("Sec-WebSocket-Key3").c_str()+8,
+ // key3 should be exactly 8 bytes. If it is more it will be truncated
+ // if it is less the final key will almost certainly be wrong.
+ // TODO: decide if it is best to silently fail here or produce some sort
+ // of warning or exception.
+ const std::string& key3 = request.header("Sec-WebSocket-Key3");
+ std::copy(key3.c_str(),
+ key3.c_str()+std::min(static_cast<size_t>(8), key3.size()),
&key_final[8]);
m_key3 = md5_hash_string(std::string(key_final,16));
-
+
response.add_header("Upgrade","websocket");
response.add_header("Connection","Upgrade");

0 comments on commit 6c9d814

Please sign in to comment.