EHS Embedded HTTP Server  1.5.0.132
samples/wscommon.h
00001 /*
00002  * This file has been derived from the WebSockets++ project at
00003  * https://github.com/zaphoyd/websocketpp which is licensed under a BSD-license.
00004  *
00005  * Copyright (c) 2011, Peter Thorson. All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *     * Redistributions of source code must retain the above copyright
00010  *       notice, this list of conditions and the following disclaimer.
00011  *     * Redistributions in binary form must reproduce the above copyright
00012  *       notice, this list of conditions and the following disclaimer in the
00013  *       documentation and/or other materials provided with the distribution.
00014  *     * Neither the name of the WebSocket++ Project nor the
00015  *       names of its contributors may be used to endorse or promote products
00016  *       derived from this software without specific prior written permission.
00017  * 
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
00019  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
00020  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
00021  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
00022  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00024  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00025  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00026  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00027  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  * 
00029  */
00030 
00031 #ifndef WEBSOCKET_CONSTANTS_HPP
00032 #define WEBSOCKET_CONSTANTS_HPP
00033 
00034 #ifndef __STDC_LIMIT_MACROS
00035   #define __STDC_LIMIT_MACROS
00036 #endif
00037 #include <boost/cstdint.hpp>
00038 
00039 // SIZE_MAX appears to be a compiler thing not an OS header thing.
00040 // make sure it is defined.
00041 #ifndef SIZE_MAX
00042     #define SIZE_MAX ((size_t)(-1))
00043 #endif
00044 
00045 #include <exception>
00046 #include <string>
00047 #include <vector>
00048 
00049 #include <boost/shared_ptr.hpp>
00050 
00051 // Defaults
00052 namespace wspp {
00053     static const std::string USER_AGENT = "EHS";
00054     
00055     typedef std::vector<unsigned char> binary_string;
00056     typedef boost::shared_ptr<binary_string> binary_string_ptr;
00057     
00058     typedef std::string utf8_string;
00059     typedef boost::shared_ptr<utf8_string> utf8_string_ptr;
00060     
00061     const uint64_t DEFAULT_MAX_MESSAGE_SIZE = 0xFFFFFF; // ~16MB
00062     
00063     const size_t DEFAULT_READ_THRESHOLD = 1; // 512 would be a more sane value for this
00064     const bool DEFAULT_SILENT_CLOSE = false; // true
00065     
00066     const size_t MAX_THREAD_POOL_SIZE = 64;
00067     
00068     const uint16_t DEFAULT_PORT = 80;
00069     const uint16_t DEFAULT_SECURE_PORT = 443;
00070     
00071     inline uint16_t default_port(bool secure) {
00072         return (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT);
00073     }   
00074     
00075     namespace session {
00076         namespace state {
00077             enum value {
00078                 CONNECTING = 0,
00079                 OPEN = 1,
00080                 CLOSING = 2,
00081                 CLOSED = 3
00082             };
00083         }
00084     }
00085     
00086     namespace close {
00087     namespace status {
00088         enum value {
00089             INVALID_END = 999,
00090             NORMAL = 1000,
00091             GOING_AWAY = 1001,
00092             PROTOCOL_ERROR = 1002,
00093             UNSUPPORTED_DATA = 1003,
00094             RSV_ADHOC_1 = 1004,
00095             NO_STATUS = 1005,
00096             ABNORMAL_CLOSE = 1006,
00097             INVALID_PAYLOAD = 1007,
00098             POLICY_VIOLATION = 1008,
00099             MESSAGE_TOO_BIG = 1009,
00100             EXTENSION_REQUIRE = 1010,
00101             INTERNAL_ENDPOINT_ERROR = 1011,
00102             RSV_ADHOC_2 = 1012,
00103             RSV_ADHOC_3 = 1013,
00104             RSV_ADHOC_4 = 1014,
00105             TLS_HANDSHAKE = 1015,
00106             RSV_START = 1016,
00107             RSV_END = 2999,
00108             INVALID_START = 5000
00109         };
00110         
00111         inline bool reserved(value s) {
00112             return ((s >= RSV_START && s <= RSV_END) || s == RSV_ADHOC_1 
00113                     || s == RSV_ADHOC_2 || s == RSV_ADHOC_3 || s == RSV_ADHOC_4);
00114         }
00115         
00116         // Codes invalid on the wire
00117         inline bool invalid(value s) {
00118             return ((s <= INVALID_END || s >= INVALID_START) || 
00119                     s == NO_STATUS || 
00120                     s == ABNORMAL_CLOSE || 
00121                     s == TLS_HANDSHAKE);
00122         }
00123         
00124         // TODO functions for application ranges?
00125     } // namespace status
00126     } // namespace close
00127     
00128     namespace frame {
00129         // Opcodes are 4 bits
00130         // See spec section 5.2
00131         namespace opcode {
00132             enum value {
00133                 CONTINUATION = 0x0,
00134                 TEXT = 0x1,
00135                 BINARY = 0x2,
00136                 RSV3 = 0x3,
00137                 RSV4 = 0x4,
00138                 RSV5 = 0x5,
00139                 RSV6 = 0x6,
00140                 RSV7 = 0x7,
00141                 CLOSE = 0x8,
00142                 PING = 0x9,
00143                 PONG = 0xA,
00144                 CONTROL_RSVB = 0xB,
00145                 CONTROL_RSVC = 0xC,
00146                 CONTROL_RSVD = 0xD,
00147                 CONTROL_RSVE = 0xE,
00148                 CONTROL_RSVF = 0xF
00149             };
00150             
00151             inline bool reserved(value v) {
00152                 return (v >= RSV3 && v <= RSV7) || 
00153                 (v >= CONTROL_RSVB && v <= CONTROL_RSVF);
00154             }
00155             
00156             inline bool invalid(value v) {
00157                 return (v > 0xF || v < 0);
00158             }
00159             
00160             inline bool is_control(value v) {
00161                 return v >= 0x8;
00162             }
00163         }
00164         
00165         namespace limits {
00166             static const uint8_t PAYLOAD_SIZE_BASIC = 125;
00167             static const uint16_t PAYLOAD_SIZE_EXTENDED = 0xFFFF; // 2^16, 65535
00168             static const uint64_t PAYLOAD_SIZE_JUMBO = 0x7FFFFFFFFFFFFFFFLL;//2^63
00169         }
00170     } // namespace frame
00171 }
00172 
00173 #endif // WEBSOCKET_CONSTANTS_HPP