adds frame UTF8 validation method

This commit is contained in:
Peter Thorson
2011-09-15 06:57:51 -05:00
parent e497984db3
commit 342dbd182d
2 changed files with 22 additions and 4 deletions

View File

@@ -26,7 +26,9 @@
*/
#include "websocket_frame.hpp"
#include "websocket_server.hpp"
#include "utf8_validator/utf8_validator.hpp"
#include <iostream>
#include <algorithm>
@@ -250,7 +252,9 @@ void frame::set_status(uint16_t status,const std::string message) {
char val[2];
*reinterpret_cast<uint16_t*>(&val[0]) = htons(status);
m_header[1] = message.size()+2;
m_payload[0] = val[0];
m_payload[1] = val[1];
@@ -334,7 +338,6 @@ void frame::process_extended_header() {
}
void frame::process_payload() {
// unmask payload one byte at a time
for (uint64_t i = 0; i < m_payload.size(); i++) {
m_payload[i] = (m_payload[i] ^ m_masking_key[i%4]);
}
@@ -367,6 +370,20 @@ void frame::process_payload2() {
}
}
bool frame::validate_utf8(uint32_t* state,uint32_t* codep) const {
for (size_t i = 0; i < m_payload.size(); i++) {
using utf8_validator::decode;
//std::cout << "decoding: " << std::hex << m_payload[i] << std::endl;
if (decode(state,codep,m_payload[i]) == utf8_validator::UTF8_REJECT) {
// std::cout << "bad byte" << std::endl;
return false;
}
}
return true;
}
bool frame::validate_basic_header() const {
// check for control frame size
if (get_basic_size() > BASIC_PAYLOAD_LIMIT && is_control()) {
@@ -412,4 +429,4 @@ void frame::clear_masking_key() {
m_masking_key[1] = 0;
m_masking_key[2] = 0;
m_masking_key[3] = 0;
}
}

View File

@@ -127,6 +127,7 @@ public:
void process_payload();
void process_payload2(); // experiment with more efficient masking code.
bool validate_utf8(uint32_t* state,uint32_t* codep) const;
bool validate_basic_header() const;
void generate_masking_key();
@@ -142,4 +143,4 @@ private:
}
#endif // WEBSOCKET_FRAME_HPP
#endif // WEBSOCKET_FRAME_HPP