How to handle non-UTF8?

753 views
Skip to first unread message

jimbo...@gmail.com

unread,
Mar 11, 2014, 4:48:27 PM3/11/14
to webso...@googlegroups.com
Peter,

As usual, I can't thank you enough for this library!  My websocket development time is next to nil because of how well you put it together.  Thank you!

How can I detect if websocketpp::frame::opcode::text isn't valid UTF8?  I'd like to blacklist any who do not send valid UTF8

Also, will send(hdl, message, websocketpp::frame::opcode::text) fail or give error if a non-valid UTF8 encodable std::string is passed?  If so, how can the effect be manipulated?  To avoid blacklisting, I'd like to be able to detect if an outgoing message is not valid UTF8.

Finally, are invalid UTF-8 characters permitted?  If so, is there way to forbid them?

Thank you so much in advance and for this amazing library!

Peter Thorson

unread,
Mar 11, 2014, 6:46:28 PM3/11/14
to jimbo...@gmail.com, webso...@googlegroups.com
On Mar 11, 2014, at 15:48 , jimbo...@gmail.com wrote:

How can I detect if websocketpp::frame::opcode::text isn't valid UTF8?  I'd like to blacklist any who do not send valid UTF8

If a connection receives invalid UTF8 data in a text message it will be closed with an invalid payload error (websocket close code 1007/Invalid Payload). You can check for this in your close handler via connection::get_local_close_code (or possibly also connection::get_ec).

Also, will send(hdl, message, websocketpp::frame::opcode::text) fail or give error if a non-valid UTF8 encodable std::string is passed?  If so, how can the effect be manipulated?  To avoid blacklisting, I'd like to be able to detect if an outgoing message is not valid UTF8.

endpoint::send will not let you send invalid UTF8. If you are using the versions that throw an exception you will get an exception with the invalid_payload code. If you are using the exception free version your error code after the call should indicate that it failed due to an invalid_payload.

Finally, are invalid UTF-8 characters permitted?  If so, is there way to forbid them?

You will not be able to send invalid UTF8 with the text opcode. This is not presently override-able. If you want to send invalid UTF8 you can use the binary opcode.

Reply all
Reply to author
Forward
0 new messages