123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #ifndef WEBSOCKETPP_CLOSE_HPP
- #define WEBSOCKETPP_CLOSE_HPP
- #include <websocketpp/error.hpp>
- #include <websocketpp/common/network.hpp>
- #include <websocketpp/common/stdint.hpp>
- #include <websocketpp/utf8_validator.hpp>
- #include <string>
- namespace websocketpp {
- namespace close {
- namespace status {
-
- typedef uint16_t value;
-
- static value const blank = 0;
-
-
- static value const omit_handshake = 1;
-
-
- static value const force_tcp_drop = 2;
-
-
- static value const normal = 1000;
-
-
- static value const going_away = 1001;
-
- static value const protocol_error = 1002;
-
-
-
- static value const unsupported_data = 1003;
-
-
- static value const no_status = 1005;
-
-
- static value const abnormal_close = 1006;
-
-
- static value const invalid_payload = 1007;
-
-
- static value const policy_violation = 1008;
-
- static value const message_too_big = 1009;
-
-
- static value const extension_required = 1010;
-
-
- static value const internal_endpoint_error = 1011;
-
-
-
- static value const service_restart = 1012;
-
-
-
- static value const try_again_later = 1013;
-
-
-
- static value const bad_gateway = 1014;
-
-
- static value const tls_handshake = 1015;
-
-
-
- static value const subprotocol_error = 3000;
-
-
-
- static value const invalid_subprotocol_data = 3001;
-
- static value const rsv_start = 1016;
-
- static value const rsv_end = 2999;
-
-
- inline bool reserved(value code) {
- return ((code >= rsv_start && code <= rsv_end) ||
- code == 1004);
- }
-
- static value const invalid_low = 999;
-
- static value const invalid_high = 5000;
-
-
- inline bool invalid(value code) {
- return (code <= invalid_low || code >= invalid_high ||
- code == no_status || code == abnormal_close ||
- code == tls_handshake);
- }
-
-
- inline bool terminal(value code) {
- return (code == protocol_error || code == invalid_payload ||
- code == policy_violation || code == message_too_big ||
- code == internal_endpoint_error);
- }
-
-
-
- inline std::string get_string(value code) {
- switch (code) {
- case normal:
- return "Normal close";
- case going_away:
- return "Going away";
- case protocol_error:
- return "Protocol error";
- case unsupported_data:
- return "Unsupported data";
- case no_status:
- return "No status set";
- case abnormal_close:
- return "Abnormal close";
- case invalid_payload:
- return "Invalid payload";
- case policy_violation:
- return "Policy violoation";
- case message_too_big:
- return "Message too big";
- case extension_required:
- return "Extension required";
- case internal_endpoint_error:
- return "Internal endpoint error";
- case service_restart:
- return "Service restart";
- case try_again_later:
- return "Try again later";
- case bad_gateway:
- return "Bad gateway";
- case tls_handshake:
- return "TLS handshake failure";
- case subprotocol_error:
- return "Generic subprotocol error";
- case invalid_subprotocol_data:
- return "Invalid subprotocol data";
- default:
- return "Unknown";
- }
- }
- }
- union code_converter {
- uint16_t i;
- char c[2];
- };
- inline status::value extract_code(std::string const & payload, lib::error_code
- & ec)
- {
- ec = lib::error_code();
- if (payload.size() == 0) {
- return status::no_status;
- } else if (payload.size() == 1) {
- ec = make_error_code(error::bad_close_code);
- return status::protocol_error;
- }
- code_converter val;
- val.c[0] = payload[0];
- val.c[1] = payload[1];
- status::value code(ntohs(val.i));
- if (status::invalid(code)) {
- ec = make_error_code(error::invalid_close_code);
- }
- if (status::reserved(code)) {
- ec = make_error_code(error::reserved_close_code);
- }
- return code;
- }
- inline std::string extract_reason(std::string const & payload, lib::error_code
- & ec)
- {
- std::string reason;
- ec = lib::error_code();
- if (payload.size() > 2) {
- reason.append(payload.begin()+2,payload.end());
- }
- if (!websocketpp::utf8_validator::validate(reason)) {
- ec = make_error_code(error::invalid_utf8);
- }
- return reason;
- }
- }
- }
- #endif
|