EHS Embedded HTTP Server  1.5.1.173
wscommon.h
1 /*
2  * This file has been derived from the WebSockets++ project at
3  * https://github.com/zaphoyd/websocketpp which is licensed under a BSD-license.
4  *
5  * Copyright (c) 2011, Peter Thorson. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the WebSocket++ Project nor the
15  * names of its contributors may be used to endorse or promote products
16  * derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  */
30 
31 #ifndef WEBSOCKET_CONSTANTS_HPP
32 #define WEBSOCKET_CONSTANTS_HPP
33 
34 #ifndef __STDC_LIMIT_MACROS
35  #define __STDC_LIMIT_MACROS
36 #endif
37 #include <boost/cstdint.hpp>
38 
39 // SIZE_MAX appears to be a compiler thing not an OS header thing.
40 // make sure it is defined.
41 #ifndef SIZE_MAX
42  #define SIZE_MAX ((size_t)(-1))
43 #endif
44 
45 #include <exception>
46 #include <string>
47 #include <vector>
48 
49 #include <boost/shared_ptr.hpp>
50 
51 // Defaults
52 namespace wspp {
53  static const std::string USER_AGENT = "EHS";
54 
55  typedef std::vector<unsigned char> binary_string;
56  typedef boost::shared_ptr<binary_string> binary_string_ptr;
57 
58  typedef std::string utf8_string;
59  typedef boost::shared_ptr<utf8_string> utf8_string_ptr;
60 
61  const uint64_t DEFAULT_MAX_MESSAGE_SIZE = 0xFFFFFF; // ~16MB
62 
63  const size_t DEFAULT_READ_THRESHOLD = 1; // 512 would be a more sane value for this
64  const bool DEFAULT_SILENT_CLOSE = false; // true
65 
66  const size_t MAX_THREAD_POOL_SIZE = 64;
67 
68  const uint16_t DEFAULT_PORT = 80;
69  const uint16_t DEFAULT_SECURE_PORT = 443;
70 
71  inline uint16_t default_port(bool secure) {
72  return (secure ? DEFAULT_SECURE_PORT : DEFAULT_PORT);
73  }
74 
75  namespace session {
76  namespace state {
77  enum value {
78  CONNECTING = 0,
79  OPEN = 1,
80  CLOSING = 2,
81  CLOSED = 3
82  };
83  }
84  }
85 
86  namespace close {
87  namespace status {
88  enum value {
89  INVALID_END = 999,
90  NORMAL = 1000,
91  GOING_AWAY = 1001,
92  PROTOCOL_ERROR = 1002,
93  UNSUPPORTED_DATA = 1003,
94  RSV_ADHOC_1 = 1004,
95  NO_STATUS = 1005,
96  ABNORMAL_CLOSE = 1006,
97  INVALID_PAYLOAD = 1007,
98  POLICY_VIOLATION = 1008,
99  MESSAGE_TOO_BIG = 1009,
100  EXTENSION_REQUIRE = 1010,
101  INTERNAL_ENDPOINT_ERROR = 1011,
102  RSV_ADHOC_2 = 1012,
103  RSV_ADHOC_3 = 1013,
104  RSV_ADHOC_4 = 1014,
105  TLS_HANDSHAKE = 1015,
106  RSV_START = 1016,
107  RSV_END = 2999,
108  INVALID_START = 5000
109  };
110 
111  inline bool reserved(value s) {
112  return ((s >= RSV_START && s <= RSV_END) || s == RSV_ADHOC_1
113  || s == RSV_ADHOC_2 || s == RSV_ADHOC_3 || s == RSV_ADHOC_4);
114  }
115 
116  // Codes invalid on the wire
117  inline bool invalid(value s) {
118  return ((s <= INVALID_END || s >= INVALID_START) ||
119  s == NO_STATUS ||
120  s == ABNORMAL_CLOSE ||
121  s == TLS_HANDSHAKE);
122  }
123 
124  // TODO functions for application ranges?
125  } // namespace status
126  } // namespace close
127 
128  namespace frame {
129  // Opcodes are 4 bits
130  // See spec section 5.2
131  namespace opcode {
132  enum value {
133  CONTINUATION = 0x0,
134  TEXT = 0x1,
135  BINARY = 0x2,
136  RSV3 = 0x3,
137  RSV4 = 0x4,
138  RSV5 = 0x5,
139  RSV6 = 0x6,
140  RSV7 = 0x7,
141  CLOSE = 0x8,
142  PING = 0x9,
143  PONG = 0xA,
144  CONTROL_RSVB = 0xB,
145  CONTROL_RSVC = 0xC,
146  CONTROL_RSVD = 0xD,
147  CONTROL_RSVE = 0xE,
148  CONTROL_RSVF = 0xF
149  };
150 
151  inline bool reserved(value v) {
152  return (v >= RSV3 && v <= RSV7) ||
153  (v >= CONTROL_RSVB && v <= CONTROL_RSVF);
154  }
155 
156  inline bool invalid(value v) {
157  return (v > 0xF || v < 0);
158  }
159 
160  inline bool is_control(value v) {
161  return v >= 0x8;
162  }
163  }
164 
165  namespace limits {
166  static const uint8_t PAYLOAD_SIZE_BASIC = 125;
167  static const uint16_t PAYLOAD_SIZE_EXTENDED = 0xFFFF; // 2^16, 65535
168  static const uint64_t PAYLOAD_SIZE_JUMBO = 0x7FFFFFFFFFFFFFFFLL;//2^63
169  }
170  } // namespace frame
171 }
172 
173 #endif // WEBSOCKET_CONSTANTS_HPP
Definition: wscommon.h:52