Merge commit '02855d7fed46d3c1aa1f2cefbcf4a42720575c3f' as 'src/websocketpp'

This commit is contained in:
Vinnie Falco
2015-01-20 09:35:00 -08:00
209 changed files with 37062 additions and 0 deletions

18
src/websocketpp/.gitattributes vendored Normal file
View File

@@ -0,0 +1,18 @@
# Lineendings
*.sln eol=crlf
*.vcproj eol=crlf
*.vcxproj* eol=crlf
# Whitespace rules
# strict (no trailing, no tabs)
*.cpp whitespace=trailing-space,space-before-tab,tab-in-indent,cr-at-eol
*.hpp whitespace=trailing-space,space-before-tab,tab-in-indent,cr-at-eol
*.c whitespace=trailing-space,space-before-tab,tab-in-indent,cr-at-eol
*.h whitespace=trailing-space,space-before-tab,tab-in-indent,cr-at-eol
# normal (no trailing)
*.sql whitespace=trailing-space,space-before-tab,cr-at-eol
*.txt whitespace=trailing-space,space-before-tab,cr-at-eol
# special files which must ignore whitespace
*.patch whitespace=-trailing-space

86
src/websocketpp/.gitignore vendored Normal file
View File

@@ -0,0 +1,86 @@
# make .git* files visible to git
!.gitignore
!.gitattributes
.DS_Store
#vim stuff
*~
*.swp
*.o
*.so
*.so.?
*.so.?.?.?
*.a
*.dylib
lib/*
# CMake
*.cmake
*.dir
CMakeFiles
INSTALL.*
ZERO_CHECK.*
CMakeCache.txt
install_manifest.txt
# Windows/Visual Studio
*.vcproj*
*.sln
*.suo
*.ncb
*/Debug/*
*/*/Debug/*
*/Release/*
*/*/Release/*
*/RelWithDebInfo/*
*/*/RelWithDebInfo/*
objs_shared/
objs_static/
examples/chat_server/chat_server
examples/echo_server/echo_server
examples/chat_client/chat_client
examples/echo_client/echo_client
test/basic/tests
libwebsocketpp.dylib.0.1.0
websocketpp.xcodeproj/xcuserdata/*
websocketpp.xcodeproj/project.xcworkspace/xcuserdata/*
policy_based_notes.hpp
examples/echo_server_tls/echo_server_tls
examples/fuzzing_client/fuzzing_client
examples/stress_client/stress_client
examples/broadcast_server_tls/broadcast_server
test/basic/perf
examples/echo_server_tls/echo_server_tls
examples/concurrent_server/concurrent_server
examples/fuzzing_server_tls/fuzzing_server
examples/wsperf/wsperf
.sconsign.dblite
build/
doxygen/
examples/wsperf/wsperf_client
*.out
*.log
*.opensdf
*.sdf
*.vcxproj
*.vcxproj.filters
*.user
install

View File

@@ -0,0 +1,23 @@
language: cpp
compiler:
- gcc
before_install:
- sudo apt-get install libboost-regex1.48-dev libboost-system1.48-dev libboost-thread1.48-dev libboost-test1.48-dev libboost-random1.48-dev -y
env:
global:
- BOOST_INCLUDES=/usr/include
- BOOST_LIBS=/usr/lib
script: scons -j 2 && scons test
branches:
only:
- master
- permessage-deflate
- experimental
- 0.3.x-cmake
- develop
notifications:
recipients:
- travis@zaphoyd.com
email:
on_success: change
on_failure: always

View File

@@ -0,0 +1,244 @@
############ Setup project and cmake
# Project name
project (websocketpp)
# Minimum cmake requirement. We should require a quite recent
# cmake for the dependency find macros etc. to be up to date.
cmake_minimum_required (VERSION 2.6)
set (WEBSOCKETPP_MAJOR_VERSION 0)
set (WEBSOCKETPP_MINOR_VERSION 4)
set (WEBSOCKETPP_PATCH_VERSION 0)
set (WEBSOCKETPP_VERSION ${WEBSOCKETPP_MAJOR_VERSION}.${WEBSOCKETPP_MINOR_VERSION}.${WEBSOCKETPP_PATCH_VERSION})
set(INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
if (WIN32 AND NOT CYGWIN)
set (DEF_INSTALL_CMAKE_DIR cmake)
else ()
set (DEF_INSTALL_CMAKE_DIR lib/cmake/websocketpp)
endif ()
set (INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files")
# Make relative paths absolute (needed later on)
foreach (p INCLUDE CMAKE)
set (var INSTALL_${p}_DIR)
if (NOT IS_ABSOLUTE "${${var}}")
set (${var} "${CMAKE_INSTALL_PREFIX}/${${var}}")
endif ()
endforeach ()
# Set CMake library search policy
if (COMMAND cmake_policy)
cmake_policy (SET CMP0003 NEW)
cmake_policy (SET CMP0005 NEW)
endif ()
# Disable unnecessary build types
set (CMAKE_CONFIGURATION_TYPES "Release;RelWithDebInfo;Debug" CACHE STRING "Configurations" FORCE)
# Include our cmake macros
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include (CMakeHelpers)
############ Paths
set (WEBSOCKETPP_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set (WEBSOCKETPP_INCLUDE ${WEBSOCKETPP_ROOT}/websocketpp)
set (WEBSOCKETPP_BUILD_ROOT ${CMAKE_CURRENT_BINARY_DIR})
set (WEBSOCKETPP_BIN ${WEBSOCKETPP_BUILD_ROOT}/bin)
set (WEBSOCKETPP_LIB ${WEBSOCKETPP_BUILD_ROOT}/lib)
# CMake install step prefix. I assume linux users want the prefix to
# be the default /usr or /usr/local so this is only adjusted on Windows.
# - Windows: Build the INSTALL project in your solution file.
# - Linux/OSX: make install.
if (MSVC)
set (CMAKE_INSTALL_PREFIX "${WEBSOCKETPP_ROOT}/install")
endif ()
############ Build customization
# Override from command line "CMake -D<OPTION>=TRUE/FALSE/0/1/ON/OFF"
option (ENABLE_CPP11 "Build websocketpp with CPP11 features enabled." TRUE)
option (BUILD_EXAMPLES "Build websocketpp examples." FALSE)
option (BUILD_TESTS "Build websocketpp tests." FALSE)
if (BUILD_TESTS OR BUILD_EXAMPLES)
############ Compiler specific setup
set (WEBSOCKETPP_PLATFORM_LIBS "")
set (WEBSOCKETPP_PLATFORM_TSL_LIBS "")
set (WEBSOCKETPP_BOOST_LIBS "")
# VC9 and C++11 reasoning
if (ENABLE_CPP11 AND MSVC AND MSVC90)
message("* Detected Visual Studio 9 2008, disabling C++11 support.")
set (ENABLE_CPP11 FALSE)
endif ()
# Detect clang. Not officially reported by cmake.
execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-v" ERROR_VARIABLE CXX_VER_STDERR)
if ("${CXX_VER_STDERR}" MATCHES ".*clang.*")
set (CMAKE_COMPILER_IS_CLANGXX 1)
endif ()
# C++11 defines
if (ENABLE_CPP11)
if (MSVC)
add_definitions (-D_WEBSOCKETPP_CPP11_FUNCTIONAL_)
add_definitions (-D_WEBSOCKETPP_CPP11_SYSTEM_ERROR_)
add_definitions (-D_WEBSOCKETPP_CPP11_RANDOM_DEVICE_)
add_definitions (-D_WEBSOCKETPP_CPP11_MEMORY_)
else()
add_definitions (-D_WEBSOCKETPP_CPP11_STL_)
endif()
endif ()
# Visual studio
if (MSVC)
set (WEBSOCKETPP_BOOST_LIBS system thread)
set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /GL /Gy /GF /Ox /Ob2 /Ot /Oi /MP /arch:SSE2 /fp:fast")
set (CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /INCREMENTAL:NO /OPT:REF /OPT:ICF")
add_definitions (/W3 /wd4996 /wd4995 /wd4355)
add_definitions (-DUNICODE -D_UNICODE)
add_definitions (-D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS)
add_definitions (-DNOMINMAX)
endif ()
# g++
if (CMAKE_COMPILER_IS_GNUCXX)
set (WEBSOCKETPP_PLATFORM_LIBS pthread rt)
set (WEBSOCKETPP_PLATFORM_TSL_LIBS ssl crypto)
set (WEBSOCKETPP_BOOST_LIBS system thread)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
if (NOT APPLE)
add_definitions (-DNDEBUG -Wall -Wcast-align) # todo: should we use CMAKE_C_FLAGS for these?
endif ()
# Try to detect version. Note: Not tested!
execute_process (COMMAND ${CMAKE_CXX_COMPILER} "-dumpversion" OUTPUT_VARIABLE GCC_VERSION)
if ("${GCC_VERSION}" STRGREATER "4.4.0")
message("* C++11 support partially enabled due to GCC version ${GCC_VERSION}")
set (WEBSOCKETPP_BOOST_LIBS system thread)
endif ()
endif ()
# clang
if (CMAKE_COMPILER_IS_CLANGXX)
if (NOT APPLE)
set (WEBSOCKETPP_PLATFORM_LIBS pthread rt)
else()
set (WEBSOCKETPP_PLATFORM_LIBS pthread)
endif()
set (WEBSOCKETPP_PLATFORM_TSL_LIBS ssl crypto)
set (WEBSOCKETPP_BOOST_LIBS system thread)
set (CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++0x -stdlib=libc++") # todo: is libc++ really needed here?
if (NOT APPLE)
add_definitions (-DNDEBUG -Wall -Wno-padded) # todo: should we use CMAKE_C_FLAGS for these?
endif ()
endif ()
# OSX, can override above.
if (APPLE)
add_definitions (-DNDEBUG -Wall)
endif ()
if (BUILD_EXAMPLES)
list (APPEND WEBSOCKETPP_BOOST_LIBS random)
endif()
############ Dependencies
# Set BOOST_ROOT env variable or pass with cmake -DBOOST_ROOT=path.
# BOOST_ROOT can also be defined by a previous run from cmake cache.
if (NOT "$ENV{BOOST_ROOT_CPP11}" STREQUAL "")
# Scons documentation for BOOST_ROOT_CPP11:
# "look for optional second boostroot compiled with clang's libc++ STL library
# this prevents warnings/errors when linking code built with two different
# incompatible STL libraries."
file (TO_CMAKE_PATH "$ENV{BOOST_ROOT_CPP11}" BOOST_ROOT)
set (BOOST_ROOT ${BOOST_ROOT} CACHE PATH "BOOST_ROOT dependency path" FORCE)
endif ()
if ("${BOOST_ROOT}" STREQUAL "")
file (TO_CMAKE_PATH "$ENV{BOOST_ROOT}" BOOST_ROOT)
# Cache BOOST_ROOT for runs that do not define $ENV{BOOST_ROOT}.
set (BOOST_ROOT ${BOOST_ROOT} CACHE PATH "BOOST_ROOT dependency path" FORCE)
endif ()
message ("* Configuring Boost")
message (STATUS "-- Using BOOST_ROOT")
message (STATUS " " ${BOOST_ROOT})
if (MSVC)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_USE_STATIC_LIBS TRUE)
else ()
set (Boost_USE_MULTITHREADED FALSE)
set (Boost_USE_STATIC_LIBS FALSE)
endif ()
set (Boost_FIND_REQUIRED TRUE)
set (Boost_FIND_QUIETLY TRUE)
set (Boost_DEBUG FALSE)
set (Boost_USE_MULTITHREADED TRUE)
set (Boost_ADDITIONAL_VERSIONS "1.39.0" "1.40.0" "1.41.0" "1.42.0" "1.43.0" "1.44.0" "1.46.1") # todo: someone who knows better spesify these!
find_package (Boost 1.39.0 COMPONENTS "${WEBSOCKETPP_BOOST_LIBS}")
if (Boost_FOUND)
# Boost is a project wide global dependency.
include_directories (${Boost_INCLUDE_DIRS})
link_directories (${Boost_LIBRARY_DIRS})
# Pretty print status
message (STATUS "-- Include Directories")
foreach (include_dir ${Boost_INCLUDE_DIRS})
message (STATUS " " ${include_dir})
endforeach ()
message (STATUS "-- Library Directories")
foreach (library_dir ${Boost_LIBRARY_DIRS})
message (STATUS " " ${library_dir})
endforeach ()
message (STATUS "-- Libraries")
foreach (boost_lib ${Boost_LIBRARIES})
message (STATUS " " ${boost_lib})
endforeach ()
message ("")
else ()
message (FATAL_ERROR "Failed to find required dependency: boost")
endif ()
find_package(OpenSSL)
endif()
############ Add projects
# Add main library
add_subdirectory (websocketpp)
# Add examples
if (BUILD_EXAMPLES)
add_subdirectory (examples)
endif ()
# Add tests
if (BUILD_TESTS)
add_subdirectory (test)
endif ()
print_used_build_config()
export (PACKAGE websocketpp)
configure_file (websocketpp-config.cmake.in "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/websocketpp-config.cmake" @ONLY)
configure_file (websocketpp-configVersion.cmake.in "${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/websocketpp-configVersion.cmake" @ONLY)
# Install the websocketpp-config.cmake and websocketpp-configVersion.cmake
install (FILES
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/websocketpp-config.cmake"
"${PROJECT_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/websocketpp-configVersion.cmake"
DESTINATION "${INSTALL_CMAKE_DIR}" COMPONENT dev)

145
src/websocketpp/COPYING Normal file
View File

@@ -0,0 +1,145 @@
Main Library:
Copyright (c) 2014, Peter Thorson. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the WebSocket++ Project nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Bundled Libraries:
****** Base 64 Library (base64/base64.hpp) ******
base64.hpp is a repackaging of the base64.cpp and base64.h files into a
single header suitable for use as a header only library. This conversion was
done by Peter Thorson (webmaster@zaphoyd.com) in 2012. All modifications to
the code are redistributed under the same license as the original, which is
listed below.
base64.cpp and base64.h
Copyright (C) 2004-2008 René Nyffenegger
This source code is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this source code must not be misrepresented; you must not
claim that you wrote the original source code. If you use this source code
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original source code.
3. This notice may not be removed or altered from any source distribution.
René Nyffenegger rene.nyffenegger@adp-gmbh.ch
****** SHA1 Library (sha1/sha1.hpp) ******
sha1.hpp is a repackaging of the sha1.cpp and sha1.h files from the shallsha1
library (http://code.google.com/p/smallsha1/) into a single header suitable for
use as a header only library. This conversion was done by Peter Thorson
(webmaster@zaphoyd.com) in 2013. All modifications to the code are redistributed
under the same license as the original, which is listed below.
Copyright (c) 2011, Micael Hildenborg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of Micael Hildenborg nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Micael Hildenborg ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Micael Hildenborg BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
****** MD5 Library (common/md5.hpp) ******
md5.hpp is a reformulation of the md5.h and md5.c code from
http://www.opensource.apple.com/source/cups/cups-59/cups/md5.c to allow it to
function as a component of a header only library. This conversion was done by
Peter Thorson (webmaster@zaphoyd.com) in 2012 for the WebSocket++ project. The
changes are released under the same license as the original (listed below)
Copyright (C) 1999, 2002 Aladdin Enterprises. All rights reserved.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
L. Peter Deutsch
ghost@aladdin.com
****** UTF8 Validation logic (utf8_validation.hpp) ******
utf8_validation.hpp is adapted from code originally written by Bjoern Hoehrmann
<bjoern@hoehrmann.de>. See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for
details.
The original license:
Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

1874
src/websocketpp/Doxyfile Normal file

File diff suppressed because it is too large Load Diff

250
src/websocketpp/SConstruct Normal file
View File

@@ -0,0 +1,250 @@
import os, sys, commands
env = Environment(ENV = os.environ)
# figure out a better way to configure this
if os.environ.has_key('CXX'):
env['CXX'] = os.environ['CXX']
if os.environ.has_key('DEBUG'):
env['DEBUG'] = os.environ['DEBUG']
if os.environ.has_key('CXXFLAGS'):
#env['CXXFLAGS'] = os.environ['CXXFLAGS']
env.Append(CXXFLAGS = os.environ['CXXFLAGS'])
if os.environ.has_key('LINKFLAGS'):
#env['LDFLAGS'] = os.environ['LDFLAGS']
env.Append(LINKFLAGS = os.environ['LINKFLAGS'])
## Boost
##
## Note: You need to either set BOOSTROOT to the root of a stock Boost distribution
## or set BOOST_INCLUDES and BOOST_LIBS if Boost comes with your OS distro e.g. and
## needs BOOST_INCLUDES=/usr/include/boost and BOOST_LIBS=/usr/lib like Ubuntu.
##
if os.environ.has_key('BOOSTROOT'):
os.environ['BOOST_ROOT'] = os.environ['BOOSTROOT']
if os.environ.has_key('BOOST_ROOT'):
env['BOOST_INCLUDES'] = os.environ['BOOST_ROOT']
env['BOOST_LIBS'] = os.path.join(os.environ['BOOST_ROOT'], 'stage', 'lib')
elif os.environ.has_key('BOOST_INCLUDES') and os.environ.has_key('BOOST_LIBS'):
env['BOOST_INCLUDES'] = os.environ['BOOST_INCLUDES']
env['BOOST_LIBS'] = os.environ['BOOST_LIBS']
else:
raise SCons.Errors.UserError, "Neither BOOST_ROOT, nor BOOST_INCLUDES + BOOST_LIBS was set!"
if os.environ.has_key('WSPP_ENABLE_CPP11'):
env['WSPP_ENABLE_CPP11'] = True
else:
env['WSPP_ENABLE_CPP11'] = False
boost_linkshared = False
def boostlibs(libnames,localenv):
if localenv['PLATFORM'].startswith('win'):
# Win/VC++ supports autolinking. nothing to do.
# http://www.boost.org/doc/libs/1_49_0/more/getting_started/windows.html#auto-linking
return []
else:
libs = []
prefix = localenv['SHLIBPREFIX'] if boost_linkshared else localenv['LIBPREFIX']
suffix = localenv['SHLIBSUFFIX'] if boost_linkshared else localenv['LIBSUFFIX']
for name in libnames:
lib = File(os.path.join(localenv['BOOST_LIBS'], '%sboost_%s%s' % (prefix, name, suffix)))
libs.append(lib)
return libs
if env['PLATFORM'].startswith('win'):
env.Append(CPPDEFINES = ['WIN32',
'NDEBUG',
'WIN32_LEAN_AND_MEAN',
'_WIN32_WINNT=0x0600',
'_CONSOLE',
'BOOST_TEST_DYN_LINK',
'NOMINMAX',
'_WEBSOCKETPP_CPP11_MEMORY_',
'_WEBSOCKETPP_CPP11_FUNCTIONAL_'])
arch_flags = '/arch:SSE2'
opt_flags = '/Ox /Oi /fp:fast'
warn_flags = '/W3 /wd4996 /wd4995 /wd4355'
env['CCFLAGS'] = '%s /EHsc /GR /GS- /MD /nologo %s %s' % (warn_flags, arch_flags, opt_flags)
env['LINKFLAGS'] = '/INCREMENTAL:NO /MANIFEST /NOLOGO /OPT:REF /OPT:ICF /MACHINE:X86'
elif env['PLATFORM'] == 'posix':
if env.has_key('DEBUG'):
env.Append(CCFLAGS = ['-g', '-O0'])
else:
env.Append(CPPDEFINES = ['NDEBUG'])
env.Append(CCFLAGS = ['-O1', '-fomit-frame-pointer'])
env.Append(CCFLAGS = ['-Wall'])
#env['LINKFLAGS'] = ''
elif env['PLATFORM'] == 'darwin':
if not os.environ.has_key('CXX'):
env['CXX'] = "clang++"
if env.has_key('DEBUG'):
env.Append(CCFLAGS = ['-g', '-O0'])
else:
env.Append(CPPDEFINES = ['NDEBUG'])
env.Append(CCFLAGS = ['-O1', '-fomit-frame-pointer'])
env.Append(CCFLAGS = ['-Wall'])
#env['LINKFLAGS'] = ''
if env['PLATFORM'].startswith('win'):
#env['LIBPATH'] = env['BOOST_LIBS']
pass
else:
env['LIBPATH'] = ['/usr/lib',
'/usr/local/lib'] #, env['BOOST_LIBS']
# Compiler specific warning flags
if env['CXX'].startswith('g++'):
#env.Append(CCFLAGS = ['-Wconversion'])
env.Append(CCFLAGS = ['-Wcast-align'])
env.Append(CCFLAGS = ['-Wshadow'])
env.Append(CCFLAGS = ['-Wunused-parameter'])
elif env['CXX'].startswith('clang++'):
#env.Append(CCFLAGS = ['-Wcast-align'])
#env.Append(CCFLAGS = ['-Wglobal-constructors'])
#env.Append(CCFLAGS = ['-Wconversion'])
env.Append(CCFLAGS = ['-Wno-padded'])
env.Append(CCFLAGS = ['-Wshadow'])
env.Append(CCFLAGS = ['-Wunused-parameter'])
# Wpadded
# Wsign-conversion
platform_libs = []
tls_libs = []
tls_build = False
if env['PLATFORM'] == 'posix':
platform_libs = ['pthread', 'rt']
tls_libs = ['ssl', 'crypto']
tls_build = True
elif env['PLATFORM'] == 'darwin':
tls_libs = ['ssl', 'crypto']
tls_build = True
elif env['PLATFORM'].startswith('win'):
# Win/VC++ supports autolinking. nothing to do.
pass
## Append WebSocket++ path
env.Append(CPPPATH = ['#'])
##### Set up C++11 environment
polyfill_libs = [] # boost libraries used as drop in replacements for incomplete
# C++11 STL implementations
env_cpp11 = env.Clone ()
if env_cpp11['CXX'].startswith('g++'):
# TODO: check g++ version
GCC_VERSION = commands.getoutput(env_cpp11['CXX'] + ' -dumpversion')
if GCC_VERSION > "4.4.0":
print "C++11 build environment partially enabled"
env_cpp11.Append(WSPP_CPP11_ENABLED = "true",CXXFLAGS = ['-std=c++0x'],TOOLSET = ['g++'],CPPDEFINES = ['_WEBSOCKETPP_CPP11_STL_'])
else:
print "C++11 build environment is not supported on this version of G++"
elif env_cpp11['CXX'].startswith('clang++'):
print "C++11 build environment enabled"
env.Append(CXXFLANGS = ['-stdlib=libc++'],LINKFLAGS=['-stdlib=libc++'])
env_cpp11.Append(WSPP_CPP11_ENABLED = "true",CXXFLAGS = ['-std=c++0x','-stdlib=libc++'],LINKFLAGS = ['-stdlib=libc++'],TOOLSET = ['clang++'],CPPDEFINES = ['_WEBSOCKETPP_CPP11_STL_'])
# look for optional second boostroot compiled with clang's libc++ STL library
# this prevents warnings/errors when linking code built with two different
# incompatible STL libraries.
if os.environ.has_key('BOOST_ROOT_CPP11'):
env_cpp11['BOOST_INCLUDES'] = os.environ['BOOST_ROOT_CPP11']
env_cpp11['BOOST_LIBS'] = os.path.join(os.environ['BOOST_ROOT_CPP11'], 'stage', 'lib')
elif os.environ.has_key('BOOST_INCLUDES_CPP11') and os.environ.has_key('BOOST_LIBS_CPP11'):
env_cpp11['BOOST_INCLUDES'] = os.environ['BOOST_INCLUDES_CPP11']
env_cpp11['BOOST_LIBS'] = os.environ['BOOST_LIBS_CPP11']
else:
print "C++11 build environment disabled"
# if the build system is known to allow the isystem modifier for library include
# values then use it for the boost libraries. Otherwise just add them to the
# regular CPPPATH values.
if env['CXX'].startswith('g++') or env['CXX'].startswith('clang'):
env.Append(CPPFLAGS = '-isystem ' + env['BOOST_INCLUDES'])
else:
env.Append(CPPPATH = [env['BOOST_INCLUDES']])
env.Append(LIBPATH = [env['BOOST_LIBS']])
# if the build system is known to allow the isystem modifier for library include
# values then use it for the boost libraries. Otherwise just add them to the
# regular CPPPATH values.
if env_cpp11['CXX'].startswith('g++') or env_cpp11['CXX'].startswith('clang'):
env_cpp11.Append(CPPFLAGS = '-isystem ' + env_cpp11['BOOST_INCLUDES'])
else:
env_cpp11.Append(CPPPATH = [env_cpp11['BOOST_INCLUDES']])
env_cpp11.Append(LIBPATH = [env_cpp11['BOOST_LIBS']])
releasedir = 'build/release/'
debugdir = 'build/debug/'
testdir = 'build/test/'
builddir = releasedir
Export('env')
Export('env_cpp11')
Export('platform_libs')
Export('boostlibs')
Export('tls_libs')
Export('polyfill_libs')
## END OF CONFIG !!
## TARGETS:
if not env['PLATFORM'].startswith('win'):
# Unit tests, add test folders with SConscript files to to_test list.
to_test = ['utility','http','logger','random','processors','message_buffer','extension','transport/iostream','transport/asio','roles','endpoint','connection','transport'] #,'http','processors','connection'
for t in to_test:
new_tests = SConscript('#/test/'+t+'/SConscript',variant_dir = testdir + t, duplicate = 0)
for a in new_tests:
new_alias = Alias('test', [a], a.abspath)
AlwaysBuild(new_alias)
# Main test application
#main = SConscript('#/examples/dev/SConscript',variant_dir = builddir + 'dev',duplicate = 0)
# echo_server
echo_server = SConscript('#/examples/echo_server/SConscript',variant_dir = builddir + 'echo_server',duplicate = 0)
# echo_server_tls
if tls_build:
echo_server_tls = SConscript('#/examples/echo_server_tls/SConscript',variant_dir = builddir + 'echo_server_tls',duplicate = 0)
echo_server_both = SConscript('#/examples/echo_server_both/SConscript',variant_dir = builddir + 'echo_server_both',duplicate = 0)
# broadcast_server
broadcast_server = SConscript('#/examples/broadcast_server/SConscript',variant_dir = builddir + 'broadcast_server',duplicate = 0)
# testee_server
testee_server = SConscript('#/examples/testee_server/SConscript',variant_dir = builddir + 'testee_server',duplicate = 0)
# testee_client
testee_client = SConscript('#/examples/testee_client/SConscript',variant_dir = builddir + 'testee_client',duplicate = 0)
# utility_client
utility_client = SConscript('#/examples/utility_client/SConscript',variant_dir = builddir + 'utility_client',duplicate = 0)
# debug_client
debug_client = SConscript('#/examples/debug_client/SConscript',variant_dir = builddir + 'debug_client',duplicate = 0)
# debug_server
debug_server = SConscript('#/examples/debug_server/SConscript',variant_dir = builddir + 'debug_server',duplicate = 0)
# subprotocol_server
subprotocol_server = SConscript('#/examples/subprotocol_server/SConscript',variant_dir = builddir + 'subprotocol_server',duplicate = 0)
if not env['PLATFORM'].startswith('win'):
# iostream_server
iostream_server = SConscript('#/examples/iostream_server/SConscript',variant_dir = builddir + 'iostream_server',duplicate = 0)
# telemetry_client
telemetry_client = SConscript('#/examples/telemetry_client/SConscript',variant_dir = builddir + 'telemetry_client',duplicate = 0)
# print_server
print_server = SConscript('#/examples/print_server/SConscript',variant_dir = builddir + 'print_server',duplicate = 0)

View File

@@ -0,0 +1,184 @@
0.4.0 - 2014-11-04
- BREAKING API CHANGE: All WebSocket++ methods now throw an exception of type
`websocketpp::exception` which derives from `std::exception`. This normalizes
all exception types under the standard exception hierarchy and allows
WebSocket++ exceptions to be caught in the same statement as others. The error
code that was previously thrown is wrapped in the exception object and can be
accessed via the `websocketpp::exception::code()` method.
- BREAKING API CHANGE: Custom logging policies have some new required
constructors that take generic config settings rather than pointers to
std::ostreams. This allows writing logging policies that do not involve the
use of std::ostream. This does not affect anyone using the built in logging
policies.
- BREAKING UTILITY CHANGE: `websocketpp::lib::net::htonll` and
`websocketpp::lib::net::ntohll` have been prefixed with an underscore to avoid
conflicts with similarly named macros in some operating systems. If you are
using the WebSocket++ provided 64 bit host/network byte order functions you
will need to switch to the prefixed versions.
- BREAKING UTILITY CHANGE: The signature of `base64_encode` has changed from
`websocketpp::base64_encode(unsigned char const *, unsigned int)` to
`websocketpp::base64_encode(unsigned char const *, size_t)`.
- BREAKING UTILITY CHANGE: The signature of `sha1::calc` has changed from
`websocketpp::sha1::calc(void const *, int, unsigned char *)` to
`websocketpp::sha1::calc(void const *, size_t, unsigned char *)`
- Feature: Adds incomplete `minimal_server` and `minimal_client` configs that
can be used to build custom configs without pulling in the dependencies of
`core` or `core_client`. These configs will offer a stable base config to
future-proof custom configs.
- Improvement: Core library no longer has std::iostream as a dependency.
std::iostream is still required for the optional iostream logging policy and
iostream transport.
- Bug: C++11 Chrono support was being incorrectly detected by the `boost_config`
header. Thank you Max Dmitrichenko for reporting and a patch.
- Bug: use of `std::put_time` is now guarded by a unique flag rather than a
chrono library flag. Thank you Max Dmitrichenko for reporting.
- Bug: Fixes non-thread safe use of std::localtime. #347 #383
- Compatibility: Adjust usage of std::min to be more compatible with systems
that define a min(...) macro.
- Compatibility: Removes unused parameters from all library, test, and example
code. This assists with those developing with -Werror and -Wunused-parameter
#376
- Compatibility: Renames ntohll and htonll methods to avoid conflicts with
platform specific macros. #358 #381, #382 Thank you logotype, unphased,
svendjo
- Cleanup: Removes unused functions, fixes variable shadow warnings, normalizes
all whitespace in library, examples, and tests to 4 spaces. #376
0.3.0 - 2014-08-10
- Feature: Adds `start_perpetual` and `stop_perpetual` methods to asio transport
These may be used to replace manually managed `asio::io_service::work` objects
- Feature: Allow setting pong and handshake timeouts at runtime.
- Feature: Allows changing the listen backlog queue length.
- Feature: Split tcp init into pre and post init.
- Feature: Adds URI method to extract query string from URI. Thank you Banaan
for code. #298
- Feature: Adds a compile time switch to asio transport config to disable
certain multithreading features (some locks, asio strands)
- Feature: Adds the ability to pause reading on a connection. Paused connections
will not read more data from their socket, allowing TCP flow control to work
without blocking the main thread.
- Feature: Adds the ability to specify whether or not to use the `SO_REUSEADDR`
TCP socket option. The default for this value has been changed from `true` to
`false`.
- Feature: Adds the ability to specify a maximum message size.
- Feature: Adds `close::status::get_string(...)` method to look up a human
readable string given a close code value.
- Feature: Adds `connection::read_all(...)` method to iostream transport as a
convenience method for reading all data into the connection buffer without the
end user needing to manually loop on `read_some`.
- Improvement: Open, close, and pong timeouts can be disabled entirely by
setting their duration to 0.
- Improvement: Numerous performance improvements. Including: tuned default
buffer sizes based on profiling, caching of handler binding for async
reads/writes, non-malloc allocators for read/write handlers, disabling of a
number of questionably useful range sanity checks in tight inner loops.
- Improvement: Cleaned up the handling of TLS related errors. TLS errors will
now be reported with more detail on the info channel rather than all being
`tls_short_read` or `pass_through`. In addition, many cases where a TLS short
read was in fact expected are no longer classified as errors. Expected TLS
short reads and quasi-expected socket shutdown related errors will no longer
be reported as unclean WebSocket shutdowns to the application. Information
about them will remain in the info error channel for debugging purposes.
- Improvement: `start_accept` and `listen` errors are now reported to the caller
either via an exception or an ec parameter.
- Improvement: Outgoing writes are now batched for improved message throughput
and reduced system call and TCP frame overhead.
- Bug: Fix some cases of calls to empty lib::function objects.
- Bug: Fix memory leak of connection objects due to cached handlers holding on to
reference counted pointers. #310 Thank you otaras for reporting.
- Bug: Fix issue with const endpoint accessors (such as `get_user_agent`) not
compiling due to non-const mutex use. #292 Thank you logofive for reporting.
- Bug: Fix handler allocation crash with multithreaded `io_service`.
- Bug: Fixes incorrect whitespace handling in header parsing. #301 Thank you
Wolfram Schroers for reporting
- Bug: Fix a crash when parsing empty HTTP headers. Thank you Thingol for
reporting.
- Bug: Fix a crash following use of the `stop_listening` function. Thank you
Thingol for reporting.
- Bug: Fix use of variable names that shadow function parameters. The library
should compile cleanly with -Wshadow now. Thank you giszo for reporting. #318
- Bug: Fix an issue where `set_open_handshake_timeout` was ignored by server
code. Thank you Robin Rowe for reporting.
- Bug: Fix an issue where custom timeout values weren't being propagated from
endpoints to new connections.
- Bug: Fix a number of memory leaks related to server connection failures. #323
#333 #334 #335 Thank you droppy and aydany for reporting and patches.
reporting.
- Compatibility: Fix compile time conflict with Visual Studio's MIN/MAX macros.
Thank you Robin Rowe for reporting.
- Documentation: Examples and test suite build system now defaults to clang on
OS X
0.3.0-alpha4 - 2013-10-11
- HTTP requests ending normally are no longer logged as errors. Thank you Banaan
for reporting. #294
- Eliminates spurious expired timers in certain error conditions. Thank you
Banaan for reporting. #295
- Consolidates all bundled library licenses into the COPYING file. #294
- Updates bundled sha1 library to one with a cleaner interface and more
straight-forward license. Thank you lotodore for reporting and Evgeni Golov
for reviewing. #294
- Re-introduces strands to asio transport, allowing `io_service` thread pools to
be used (with some limitations).
- Removes endpoint code that kept track of a connection list that was never used
anywhere. Removes a lock and reduces connection creation/deletion complexity
from O(log n) to O(1) in the number of connections.
- A number of internal changes to transport APIs
- Deprecates iostream transport `readsome` in favor of `read_some` which is more
consistent with the naming of the rest of the library.
- Adds preliminary signaling to iostream transport of eof and fatal transport
errors
- Updates transport code to use shared pointers rather than raw pointers to
prevent asio from retaining pointers to connection methods after the
connection goes out of scope. #293 Thank you otaras for reporting.
- Fixes an issue where custom headers couldn't be set for client connections
Thank you Jerry Win and Wolfram Schroers for reporting.
- Fixes a compile error on visual studio when using interrupts. Thank you Javier
Rey Neira for reporting this.
- Adds new 1012 and 1013 close codes per IANA registry
- Add `set_remote_endpoint` method to iostream transport.
- Add `set_secure` method to iostream transport.
- Fix typo in .gitattributes file. Thank you jstarasov for reporting this. #280
- Add missing locale include. Thank you Toninoso for reporting this. #281
- Refactors `asio_transport` endpoint and adds full documentation and exception
free varients of all methods.
- Removes `asio_transport` endpoint method cancel(). Use `stop_listen()` instead
- Wrap internal `io_service` `run_one()` method
- Suppress error when trying to shut down a connection that was already closed
0.3.0-alpha3 - 2013-07-16
- Minor refactor to bundled sha1 library
- HTTP header comparisons are now case insensitive. #220, #275
- Refactors URI to be exception free and not use regular expressions. This
eliminates the dependency on boost or C++11 regex libraries allowing native
C++11 usage on GCC 4.4 and higher and significantly reduces staticly built
binary sizes.
- Updates handling of Server and User-Agent headers to better handle custom
settings and allow suppression of these headers for security purposes.
- Fix issue where pong timeout handler always fired. Thank you Steven Klassen
for reporting this bug.
- Add ping and pong endpoint wrapper methods
- Add `get_request()` pass through method to connection to allow calling methods
specific to the HTTP policy in use.
- Fix issue compile error with `WEBSOCKETPP_STRICT_MASKING` enabled and another
issue where `WEBSOCKETPP_STRICT_MASKING` was not applied to incoming messages.
Thank you Petter Norby for reporting and testing these bugs. #264
- Add additional macro guards for use with boost_config. Thank you breyed
for testing and code. #261
0.3.0-alpha2 - 2013-06-09
- Fix a regression that caused servers being sent two close frames in a row
to end a connection uncleanly. #259
- Fix a regression that caused spurious frames following a legitimate close
frames to erroneously trigger handlers. #258
- Change default HTTP response error code when no http_handler is defined from
500/Internal Server Error to 426/Upgrade Required
- Remove timezone from logger timestamp to work around issues with the Windows
implimentation of strftime. Thank you breyed for testing and code. #257
- Switch integer literals to char literals to improve VCPP compatibility.
Thank you breyed for testing and code. #257
- Add MSVCPP warning suppression for the bundled SHA1 library. Thank you breyed
for testing and code. #257
0.3.0-alpha1 - 2013-06-09
- Initial Release

View File

@@ -0,0 +1,78 @@
# Print build configuration
macro (print_used_build_config)
message ("\n=========== Used Build Configuration =============\n")
message (STATUS "ENABLE_CPP11 = " ${ENABLE_CPP11})
message (STATUS "BUILD_EXAMPLES = " ${BUILD_EXAMPLES})
message (STATUS "BUILD_TESTS = " ${BUILD_TESTS})
message ("")
message (STATUS "WEBSOCKETPP_ROOT = " ${WEBSOCKETPP_ROOT})
message (STATUS "WEBSOCKETPP_BIN = " ${WEBSOCKETPP_BIN})
message (STATUS "WEBSOCKETPP_LIB = " ${WEBSOCKETPP_LIB})
message (STATUS "Install prefix = " ${CMAKE_INSTALL_PREFIX})
message ("")
message (STATUS "WEBSOCKETPP_BOOST_LIBS = ${WEBSOCKETPP_BOOST_LIBS}")
message (STATUS "WEBSOCKETPP_PLATFORM_LIBS = ${WEBSOCKETPP_PLATFORM_LIBS}")
message (STATUS "WEBSOCKETPP_PLATFORM_TSL_LIBS = ${WEBSOCKETPP_PLATFORM_TSL_LIBS}")
message ("")
endmacro ()
# Adds the given folder_name into the source files of the current project.
# Use this macro when your module contains .cpp and .h files in several subdirectories.
# Your sources variable needs to be WSPP_SOURCE_FILES and headers variable WSPP_HEADER_FILES.
macro(add_source_folder folder_name)
file(GLOB H_FILES_IN_FOLDER_${folder_name} ${folder_name}/*.hpp ${folder_name}/*.h)
file(GLOB CPP_FILES_IN_FOLDER_${folder_name} ${folder_name}/*.cpp ${folder_name}/*.c)
source_group("Header Files\\${folder_name}" FILES ${H_FILES_IN_FOLDER_${folder_name}})
source_group("Source Files\\${folder_name}" FILES ${CPP_FILES_IN_FOLDER_${folder_name}})
set(WSPP_HEADER_FILES ${WSPP_HEADER_FILES} ${H_FILES_IN_FOLDER_${folder_name}})
set(WSPP_SOURCE_FILES ${WSPP_SOURCE_FILES} ${CPP_FILES_IN_FOLDER_${folder_name}})
endmacro()
# Initialize target.
macro (init_target NAME)
set (TARGET_NAME ${NAME})
message ("** " ${TARGET_NAME})
# Include our own module path. This makes #include "x.h"
# work in project subfolders to include the main directory headers.
include_directories (${CMAKE_CURRENT_SOURCE_DIR})
endmacro ()
# Build executable for executables
macro (build_executable TARGET_NAME)
set (TARGET_LIB_TYPE "EXECUTABLE")
message (STATUS "-- Build Type:")
message (STATUS " " ${TARGET_LIB_TYPE})
add_executable (${TARGET_NAME} ${ARGN})
include_directories (${WEBSOCKETPP_ROOT} ${WEBSOCKETPP_INCLUDE})
set_target_properties (${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${WEBSOCKETPP_BIN})
set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX d)
endmacro ()
# Finalize target for all types
macro (final_target)
if ("${TARGET_LIB_TYPE}" STREQUAL "EXECUTABLE")
install (TARGETS ${TARGET_NAME}
RUNTIME DESTINATION "bin"
CONFIGURATIONS ${CMAKE_CONFIGURATION_TYPES})
endif ()
# install headers, directly from current source dir and look for subfolders with headers
file (GLOB_RECURSE TARGET_INSTALL_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.hpp)
foreach (hppfile ${TARGET_INSTALL_HEADERS})
get_filename_component (currdir ${hppfile} PATH)
install (FILES ${hppfile} DESTINATION "include/${TARGET_NAME}/${currdir}")
endforeach()
endmacro ()
macro (link_boost)
target_link_libraries (${TARGET_NAME} ${Boost_LIBRARIES})
endmacro ()
macro (link_openssl)
target_link_libraries (${TARGET_NAME} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
endmacro ()

View File

@@ -0,0 +1,52 @@
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
class broadcast_server {
public:
broadcast_server() {
m_server.init_asio();
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
}
void on_open(connection_hdl hdl) {
m_connections.insert(hdl);
}
void on_close(connection_hdl hdl) {
m_connections.erase(hdl);
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
for (auto it : m_connections) {
m_server.send(it,msg);
}
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
};
int main() {
broadcast_server server;
server.run(9002);
}

View File

@@ -0,0 +1,65 @@
#include <functional>
#include <mutex>
#include <set>
#include <thread>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
class count_server {
public:
count_server() : m_count(0) {
m_server.init_asio();
m_server.set_open_handler(bind(&count_server::on_open,this,_1));
m_server.set_close_handler(bind(&count_server::on_close,this,_1));
}
void on_open(connection_hdl hdl) {
std::lock_guard<std::mutex> lock(m_mutex);
m_connections.insert(hdl);
}
void on_close(connection_hdl hdl) {
std::lock_guard<std::mutex> lock(m_mutex);
m_connections.erase(hdl);
}
void count() {
while (1) {
sleep(1);
m_count++;
std::stringstream ss;
ss << m_count;
std::lock_guard<std::mutex> lock(m_mutex);
for (auto it : m_connections) {
m_server.send(it,ss.str(),websocketpp::frame::opcode::text);
}
}
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
int m_count;
server m_server;
con_list m_connections;
std::mutex m_mutex;
};
int main() {
count_server server;
std::thread t(std::bind(&count_server::count,&server));
server.run(9002);
}

View File

@@ -0,0 +1,6 @@
file (GLOB SDIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
foreach (SUBDIR ${SDIRS})
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt")
add_subdirectory (${SUBDIR})
endif ()
endforeach ()

View File

@@ -0,0 +1,88 @@
#include <iostream>
#include <map>
#include <exception>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
struct connection_data {
int sessionid;
std::string name;
};
class print_server {
public:
print_server() : m_next_sessionid(1) {
m_server.init_asio();
m_server.set_open_handler(bind(&print_server::on_open,this,::_1));
m_server.set_close_handler(bind(&print_server::on_close,this,::_1));
m_server.set_message_handler(bind(&print_server::on_message,this,::_1,::_2));
}
void on_open(connection_hdl hdl) {
connection_data data;
data.sessionid = m_next_sessionid++;
data.name = "";
m_connections[hdl] = data;
}
void on_close(connection_hdl hdl) {
connection_data& data = get_data_from_hdl(hdl);
std::cout << "Closing connection " << data.name
<< " with sessionid " << data.sessionid << std::endl;
m_connections.erase(hdl);
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_data& data = get_data_from_hdl(hdl);
if (data.name == "") {
data.name = msg->get_payload();
std::cout << "Setting name of connection with sessionid "
<< data.sessionid << " to " << data.name << std::endl;
} else {
std::cout << "Got a message from connection " << data.name
<< " with sessionid " << data.sessionid << std::endl;
}
}
connection_data& get_data_from_hdl(connection_hdl hdl) {
auto it = m_connections.find(hdl);
if (it == m_connections.end()) {
// this connection is not in the list. This really shouldn't happen
// and probably means something else is wrong.
throw std::invalid_argument("No data available for session");
}
return it->second;
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
private:
typedef std::map<connection_hdl,connection_data,std::owner_less<connection_hdl>> con_list;
int m_next_sessionid;
server m_server;
con_list m_connections;
};
int main() {
print_server server;
server.run(9002);
}

View File

@@ -0,0 +1,23 @@
## Broadcast Server example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('broadcast_server', ["broadcast_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','thread'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('broadcast_server', ["broadcast_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,159 @@
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
/*#include <boost/thread.hpp>
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>*/
#include <websocketpp/common/thread.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
/* on_open insert connection_hdl into channel
* on_close remove connection_hdl from channel
* on_message queue send to all channels
*/
enum action_type {
SUBSCRIBE,
UNSUBSCRIBE,
MESSAGE
};
struct action {
action(action_type t, connection_hdl h) : type(t), hdl(h) {}
action(action_type t, connection_hdl h, server::message_ptr m)
: type(t), hdl(h), msg(m) {}
action_type type;
websocketpp::connection_hdl hdl;
server::message_ptr msg;
};
class broadcast_server {
public:
broadcast_server() {
// Initialize Asio Transport
m_server.init_asio();
// Register handler callbacks
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
}
void run(uint16_t port) {
// listen on specified port
m_server.listen(port);
// Start the server accept loop
m_server.start_accept();
// Start the ASIO io_service run loop
try {
m_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
void on_open(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_close(connection_hdl hdl) {
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_close" << std::endl;
m_actions.push(action(UNSUBSCRIBE,hdl));
lock.unlock();
m_action_cond.notify_one();
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
// queue message up for sending by processing thread
unique_lock<mutex> lock(m_action_lock);
//std::cout << "on_message" << std::endl;
m_actions.push(action(MESSAGE,hdl,msg));
lock.unlock();
m_action_cond.notify_one();
}
void process_messages() {
while(1) {
unique_lock<mutex> lock(m_action_lock);
while(m_actions.empty()) {
m_action_cond.wait(lock);
}
action a = m_actions.front();
m_actions.pop();
lock.unlock();
if (a.type == SUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.insert(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
m_connections.erase(a.hdl);
} else if (a.type == MESSAGE) {
unique_lock<mutex> con_lock(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
m_server.send(*it,a.msg);
}
} else {
// undefined.
}
}
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
std::queue<action> m_actions;
mutex m_action_lock;
mutex m_connection_lock;
condition_variable m_action_cond;
};
int main() {
try {
broadcast_server server_instance;
// Start a thread to run the processing loop
thread t(bind(&broadcast_server::process_messages,&server_instance));
// Run the asio loop with the main thread
server_instance.run(9002);
t.join();
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
}
}

View File

@@ -0,0 +1,24 @@
## Debug client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env_cpp11.Program('debug_client', ["debug_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env.Program('debug_client', ["debug_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,152 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** ====== WARNING ========
* This example is presently used as a scratch space. It may or may not be broken
* at any given time.
*/
#include <websocketpp/config/asio_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
#include <chrono>
typedef websocketpp::client<websocketpp::config::asio_tls_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_tls_client::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
typedef client::connection_ptr connection_ptr;
class perftest {
public:
typedef perftest type;
typedef std::chrono::duration<int,std::micro> dur_type;
perftest () {
m_endpoint.set_access_channels(websocketpp::log::alevel::all);
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
m_endpoint.init_asio();
// Register our handlers
m_endpoint.set_socket_init_handler(bind(&type::on_socket_init,this,::_1));
m_endpoint.set_tls_init_handler(bind(&type::on_tls_init,this,::_1));
m_endpoint.set_message_handler(bind(&type::on_message,this,::_1,::_2));
m_endpoint.set_open_handler(bind(&type::on_open,this,::_1));
m_endpoint.set_close_handler(bind(&type::on_close,this,::_1));
}
void start(std::string uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
}
//con->set_proxy("http://humupdates.uchicago.edu:8443");
m_endpoint.connect(con);
// Start the ASIO io_service run loop
m_start = std::chrono::high_resolution_clock::now();
m_endpoint.run();
}
void on_socket_init(websocketpp::connection_hdl) {
m_socket_init = std::chrono::high_resolution_clock::now();
}
context_ptr on_tls_init(websocketpp::connection_hdl) {
m_tls_init = std::chrono::high_resolution_clock::now();
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return ctx;
}
void on_open(websocketpp::connection_hdl hdl) {
m_open = std::chrono::high_resolution_clock::now();
m_endpoint.send(hdl, "", websocketpp::frame::opcode::text);
}
void on_message(websocketpp::connection_hdl hdl, message_ptr) {
m_message = std::chrono::high_resolution_clock::now();
m_endpoint.close(hdl,websocketpp::close::status::going_away,"");
}
void on_close(websocketpp::connection_hdl) {
m_close = std::chrono::high_resolution_clock::now();
std::cout << "Socket Init: " << std::chrono::duration_cast<dur_type>(m_socket_init-m_start).count() << std::endl;
std::cout << "TLS Init: " << std::chrono::duration_cast<dur_type>(m_tls_init-m_start).count() << std::endl;
std::cout << "Open: " << std::chrono::duration_cast<dur_type>(m_open-m_start).count() << std::endl;
std::cout << "Message: " << std::chrono::duration_cast<dur_type>(m_message-m_start).count() << std::endl;
std::cout << "Close: " << std::chrono::duration_cast<dur_type>(m_close-m_start).count() << std::endl;
}
private:
client m_endpoint;
std::chrono::high_resolution_clock::time_point m_start;
std::chrono::high_resolution_clock::time_point m_socket_init;
std::chrono::high_resolution_clock::time_point m_tls_init;
std::chrono::high_resolution_clock::time_point m_open;
std::chrono::high_resolution_clock::time_point m_message;
std::chrono::high_resolution_clock::time_point m_close;
};
int main(int argc, char* argv[]) {
std::string uri = "wss://echo.websocket.org";
if (argc == 2) {
uri = argv[1];
}
try {
perftest endpoint;
endpoint.start(uri);
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,10 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (debug_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Debug server example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('debug_server', ["debug_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('debug_server', ["debug_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,92 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/** ====== WARNING ========
* This example is presently used as a scratch space. It may or may not be broken
* at any given time.
*/
#include <websocketpp/config/debug_asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
typedef websocketpp::server<websocketpp::config::debug_asio> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
int main() {
// Create a server endpoint
server echo_server;
try {
// Set logging settings
echo_server.set_access_channels(websocketpp::log::alevel::all);
echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
echo_server.init_asio();
echo_server.set_reuse_addr(true);
// Register our message handler
echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
// Listen on port 9012
echo_server.listen(9012);
// Start the server accept loop
echo_server.start_accept();
// Start the ASIO io_service run loop
echo_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,18 @@
## Main development example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env_cpp11 = env_cpp11.Clone ()
prgs = []
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system','timer','chrono'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('main', ["main.cpp"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,200 @@
//#ifndef _WEBSOCKETPP_CPP11_STL_
// #define _WEBSOCKETPP_CPP11_STL_
//#endif
#include <random>
#include <boost/timer/timer.hpp>
#include <websocketpp/config/core.hpp>
//#include <websocketpp/security/none.hpp>
//#include <websocketpp/concurrency/none.hpp>
//#include <websocketpp/concurrency/stl.hpp>
//#include <websocketpp/transport/iostream.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <sstream>
//typedef websocketpp::concurrency::stl concurrency;
//typedef websocketpp::transport::iostream<concurrency> transport;
//typedef websocketpp::server<concurrency,transport> server;
typedef websocketpp::server<websocketpp::config::core> server;
/*class handler : public server::handler {
bool validate(connection_ptr con) {
std::cout << "handler validate" << std::endl;
if (con->get_origin() != "http://www.example.com") {
con->set_status(websocketpp::http::status_code::FORBIDDEN);
return false;
}
return true;
}
void http(connection_ptr con) {
std::cout << "handler http" << std::endl;
}
void on_load(connection_ptr con, ptr old_handler) {
std::cout << "handler on_load" << std::endl;
}
void on_unload(connection_ptr con, ptr new_handler) {
std::cout << "handler on_unload" << std::endl;
}
void on_open(connection_ptr con) {
std::cout << "handler on_open" << std::endl;
}
void on_fail(connection_ptr con) {
std::cout << "handler on_fail" << std::endl;
}
void on_message(connection_ptr con, message_ptr msg) {
std::cout << "handler on_message" << std::endl;
}
void on_close(connection_ptr con) {
std::cout << "handler on_close" << std::endl;
}
};*/
int main() {
typedef websocketpp::message_buffer::message<websocketpp::message_buffer::alloc::con_msg_manager>
message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_man_type;
con_msg_man_type::ptr manager = websocketpp::lib::make_shared<con_msg_man_type>();
size_t foo = 1024;
message_type::ptr input = manager->get_message(websocketpp::frame::opcode::TEXT,foo);
message_type::ptr output = manager->get_message(websocketpp::frame::opcode::TEXT,foo);
websocketpp::frame::masking_key_type key;
std::random_device dev;
key.i = 0x12345678;
double m = 18094238402394.0824923;
/*std::cout << "Some Math" << std::endl;
{
boost::timer::auto_cpu_timer t;
for (int i = 0; i < foo; i++) {
m /= 1.001;
}
}*/
std::cout << m << std::endl;
std::cout << "Random Gen" << std::endl;
{
boost::timer::auto_cpu_timer t;
input->get_raw_payload().replace(0,foo,foo,'\0');
output->get_raw_payload().replace(0,foo,foo,'\0');
}
std::cout << "Out of place accelerated" << std::endl;
{
boost::timer::auto_cpu_timer t;
websocketpp::frame::word_mask_exact(reinterpret_cast<uint8_t*>(const_cast<char*>(input->get_raw_payload().data())), reinterpret_cast<uint8_t*>(const_cast<char*>(output->get_raw_payload().data())), foo, key);
}
std::cout << websocketpp::utility::to_hex(input->get_payload().c_str(),20) << std::endl;
std::cout << websocketpp::utility::to_hex(output->get_payload().c_str(),20) << std::endl;
input->get_raw_payload().replace(0,foo,foo,'\0');
output->get_raw_payload().replace(0,foo,foo,'\0');
std::cout << "In place accelerated" << std::endl;
{
boost::timer::auto_cpu_timer t;
websocketpp::frame::word_mask_exact(reinterpret_cast<uint8_t*>(const_cast<char*>(input->get_raw_payload().data())), reinterpret_cast<uint8_t*>(const_cast<char*>(input->get_raw_payload().data())), foo, key);
}
std::cout << websocketpp::utility::to_hex(input->get_payload().c_str(),20) << std::endl;
std::cout << websocketpp::utility::to_hex(output->get_payload().c_str(),20) << std::endl;
input->get_raw_payload().replace(0,foo,foo,'\0');
output->get_raw_payload().replace(0,foo,foo,'\0');
std::cout << "Out of place byte by byte" << std::endl;
{
boost::timer::auto_cpu_timer t;
websocketpp::frame::byte_mask(input->get_raw_payload().begin(), input->get_raw_payload().end(), output->get_raw_payload().begin(), key);
}
std::cout << websocketpp::utility::to_hex(input->get_payload().c_str(),20) << std::endl;
std::cout << websocketpp::utility::to_hex(output->get_payload().c_str(),20) << std::endl;
input->get_raw_payload().replace(0,foo,foo,'\0');
output->get_raw_payload().replace(0,foo,foo,'\0');
std::cout << "In place byte by byte" << std::endl;
{
boost::timer::auto_cpu_timer t;
websocketpp::frame::byte_mask(input->get_raw_payload().begin(), input->get_raw_payload().end(), input->get_raw_payload().begin(), key);
}
std::cout << websocketpp::utility::to_hex(input->get_payload().c_str(),20) << std::endl;
std::cout << websocketpp::utility::to_hex(output->get_payload().c_str(),20) << std::endl;
input->get_raw_payload().replace(0,foo,foo,'a');
output->get_raw_payload().replace(0,foo,foo,'b');
std::cout << "Copy" << std::endl;
{
boost::timer::auto_cpu_timer t;
std::copy(input->get_raw_payload().begin(), input->get_raw_payload().end(), output->get_raw_payload().begin());
}
std::cout << websocketpp::utility::to_hex(input->get_payload().c_str(),20) << std::endl;
std::cout << websocketpp::utility::to_hex(output->get_payload().c_str(),20) << std::endl;
/*server::handler::ptr h(new handler());
server test_server(h);
server::connection_ptr con;
std::stringstream output;
test_server.register_ostream(&output);
con = test_server.get_connection();
con->start();
//foo.handle_accept(con,true);
std::stringstream input;
input << "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
//input << "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
input >> *con;
std::stringstream input2;
input2 << "messageabc2";
input2 >> *con;
std::stringstream input3;
input3 << "messageabc3";
input3 >> *con;
std::stringstream input4;
input4 << "close";
input4 >> *con;
std::cout << "connection output:" << std::endl;
std::cout << output.str() << std::endl;*/
}

View File

@@ -0,0 +1,10 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (echo_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Main development example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('echo_server', ["echo_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('echo_server', ["echo_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2012, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#ifndef WEBSOCKETPP_ECHO_SERVER_HANDLER_HPP
#define WEBSOCKETPP_ECHO_SERVER_HANDLER_HPP
class echo_handler : public server::handler {
void on_message(connection_ptr con, std::string msg) {
con->write(msg);
}
};
#endif // WEBSOCKETPP_ECHO_SERVER_HANDLER_HPP

View File

@@ -0,0 +1,60 @@
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
int main() {
// Create a server endpoint
server echo_server;
try {
// Set logging settings
echo_server.set_access_channels(websocketpp::log::alevel::all);
echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
echo_server.init_asio();
// Register our message handler
echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
// Listen on port 9002
echo_server.listen(9002);
// Start the server accept loop
echo_server.start_accept();
// Start the ASIO io_service run loop
echo_server.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,15 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (OPENSSL_FOUND)
init_target (echo_server_both)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl()
final_target ()
endif()

View File

@@ -0,0 +1,24 @@
## Combo plain+tls echo server
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env_cpp11.Program('echo_server_both', ["echo_server_both.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env.Program('echo_server_both', ["echo_server_both.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,86 @@
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
// define types for two different server endpoints, one for each config we are
// using
typedef websocketpp::server<websocketpp::config::asio> server_plain;
typedef websocketpp::server<websocketpp::config::asio_tls> server_tls;
// alias some of the bind related functions as they are a bit long
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// type of the ssl context pointer is long so alias it
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
// The shared on_message handler takes a template parameter so the function can
// resolve any endpoint dependent types like message_ptr or connection_ptr
template <typename EndpointType>
void on_message(EndpointType* s, websocketpp::connection_hdl hdl,
typename EndpointType::message_ptr msg)
{
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
// No change to TLS init methods from echo_server_tls
std::string get_password() {
return "test";
}
context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
context_ptr ctx(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
ctx->set_password_callback(bind(&get_password));
ctx->use_certificate_chain_file("server.pem");
ctx->use_private_key_file("server.pem", boost::asio::ssl::context::pem);
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return ctx;
}
int main() {
// set up an external io_service to run both endpoints on. This is not
// strictly necessary, but simplifies thread management a bit.
boost::asio::io_service ios;
// set up plain endpoint
server_plain endpoint_plain;
// initialize asio with our external io_service rather than an internal one
endpoint_plain.init_asio(&ios);
endpoint_plain.set_message_handler(
bind(&on_message<server_plain>,&endpoint_plain,::_1,::_2));
endpoint_plain.listen(80);
endpoint_plain.start_accept();
// set up tls endpoint
server_tls endpoint_tls;
endpoint_tls.init_asio(&ios);
endpoint_tls.set_message_handler(
bind(&on_message<server_tls>,&endpoint_tls,::_1,::_2));
// TLS endpoint has an extra handler for the tls init
endpoint_tls.set_tls_init_handler(bind(&on_tls_init,::_1));
// tls endpoint listens on a different port
endpoint_tls.listen(443);
endpoint_tls.start_accept();
// Start the ASIO io_service run loop running both endpoints
ios.run();
}

View File

@@ -0,0 +1,58 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,A0ED66EF872A48A9
gXuvKojXzApVhhPVNdRliiajbC4PtwQG5c8TA7JADLgwOR7o9t6KtXEr37bDRpvB
9aO9P+SJaK5OOp3XKPGthOdqv+tvCRTlmzmC8GjPLBX389DWT2xoGu7JkGwDtdSm
rnF49Rlp5bfjpACk5xKNiKeDo1CWfeEJzw9Kto0g+5eMaEdors64oPzjXs3geA2g
TxCJSHv9qSX6++pCLKKCUTbyzidAxV/Zb0AAubt5V40QKqX4HhSwwstFnTaX3tlb
3QOdY+y04VIkM6d7qN5W8M7NzRkMpZ1qBpQcUMpkhQcRzWP2wub5AAff9D2GntRd
4Dz1vn3u41U3Okdr0CNj+iH7byCzuokoAhk6ZQEN6WB+GTpGgfBXdtUZrfpb0MKm
UNYP5AF2AmUqJRXhViTDVtu/V2tHF3LGuNT+W2Dz+spFZEq0byEO0N858eR0dikc
6jOASvNQbSwD0+mkgBC1gXKKU3ngj2gpJUwljeACdWFd8N2egrZfyI05CmX7vPNC
NXbs7k2buWNdjP4/D8IM+HDVidWzQa/kG/qokXKqllem9Egg37lUucwnP3cX2/Hw
U2mfaBWzeZtqc+GqRp08rYIql+Reai3sUYlQMnNk01prVY47UQb+dxuqjaxGV5Xx
Xkx0s2mfQnNRjL4S7Hjhqelufi6GpkCQ2EGsPpA+6K1ztZ0ame9Q2BE1SXeM/6vU
rxT5nRrCxueyXAyQSGcqMX9//gSeK8WWBqG/c1IAMVDa0NWrJeOJhSziE+ta3B0m
bHAPBY6vh0iB3lLdRlbUOPbC6R1TpxMOs+6Vbs2+OTifFpvOVymEoZq/nroyg68P
vn5uCKogwWA7o8EArf/UTlIwWJmH9bgILdZKld4wMel2HQg16RDzm+mEXAJi52a/
FC+fgfphdxltmUJ+rqOyR4AHULjaTWUQqTIB6sdlzgmES1nXAiE71zX//KFqomar
O60SPPk3C1bs0x5DsvmGJa8SIfDhyd+D7NPyqwEKqrZsaotYGklNkfqxa6pa8mrc
ejxquW1PK4FvBk26+osu5a90Jih0PcQM7DUMMr2WHdTiMSXWAiK2ToYF8Itt25Qv
Cd0CsSYw9CJkXNr1u1+mObheaY9QYOmztnSJLy4ZO2JsMhqNwuAueIcwmhXOREq7
kzlnGMgJcuSeAS/OBNj8Zgx0c7QQ0kzc+YmnOCsqoMtPsu/CsXJ4iJiM3Tki/2jT
bywrTiQwE6R3a/87GREOREX+WLicZBWX3k9/4tBL5XSe1p5wPpuIRQUDvAGNfNHP
JN7kujDF4SehilF1qtvCygAwvxHFDj+EwhXKNDKJzoZZIM15rAk3k92n2j6nz1qH
a3xOU05yydOlO6F6w51I1QoDddmkzCRNB0TeO3D6rekHsCK1aDWmC+qRcm2ZFtVz
sY6fdZN2NEmMQokIh9Opi1f8CSYSizPESMzdu2SF0xVO9n/IGIkn1ksK04O2BZo0
X3LBPHLfCRsQNY1eF17bj07fYU2oPZKs/XzJiwxkqK6LFvpeAVaYrtg9fqRO/UVe
QhUIj3BL550ocEpa15xLehLrmwzYiW5zwGjSHQ4EgZluGLCwyKGTh4QswEJRA9Rt
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIE0DCCA7igAwIBAgIJAM5MuKJezXq0MA0GCSqGSIb3DQEBBQUAMIGgMQswCQYD
VQQGEwJVUzERMA8GA1UECBMISWxsaW5vaXMxEDAOBgNVBAcTB0NoaWNhZ28xGDAW
BgNVBAoTD1phcGhveWQgU3R1ZGlvczEUMBIGA1UECxMLV2ViU29ja2V0KysxFjAU
BgNVBAMTDVBldGVyIFRob3Jzb24xJDAiBgkqhkiG9w0BCQEWFXdlYm1hc3RlckB6
YXBob3lkLmNvbTAeFw0xMTExMTUyMTIwMDZaFw0xMjExMTQyMTIwMDZaMIGgMQsw
CQYDVQQGEwJVUzERMA8GA1UECBMISWxsaW5vaXMxEDAOBgNVBAcTB0NoaWNhZ28x
GDAWBgNVBAoTD1phcGhveWQgU3R1ZGlvczEUMBIGA1UECxMLV2ViU29ja2V0Kysx
FjAUBgNVBAMTDVBldGVyIFRob3Jzb24xJDAiBgkqhkiG9w0BCQEWFXdlYm1hc3Rl
ckB6YXBob3lkLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANR0
tdwAnIB8I9qRZ7QbzEWY95RpM7GIn0u/9oH90PzdHiE0rXSkKT+yw3XUzH0iw5t0
5dEwSC+srSP5Vm4cA6kXc94agVVaPW89tGcdP4fHptCruSrzQsDXELCPl5UUvMpA
YUcGisdXYPN/EeOoqb9wKWxoW5mREsyyeWWS89fYN5qU/d0QpbSvEWghqLbL/ZS2
hOlXT9LufOeA+vHiV1/T/h5xC7ecIH02YDQw1EnqxbPmkLPcWThztLS9FiufNDRM
Rhcoaj2b9VDHvDwdbeA0T5v5qNdG34LaapYOelxzQMOtM0f9Dgqehodyxl2qm9mR
lq432dlOEzDnVCPNHwECAwEAAaOCAQkwggEFMB0GA1UdDgQWBBTTPKfNMnKOykhv
+vKS7vql5JsMyzCB1QYDVR0jBIHNMIHKgBTTPKfNMnKOykhv+vKS7vql5JsMy6GB
pqSBozCBoDELMAkGA1UEBhMCVVMxETAPBgNVBAgTCElsbGlub2lzMRAwDgYDVQQH
EwdDaGljYWdvMRgwFgYDVQQKEw9aYXBob3lkIFN0dWRpb3MxFDASBgNVBAsTC1dl
YlNvY2tldCsrMRYwFAYDVQQDEw1QZXRlciBUaG9yc29uMSQwIgYJKoZIhvcNAQkB
FhV3ZWJtYXN0ZXJAemFwaG95ZC5jb22CCQDOTLiiXs16tDAMBgNVHRMEBTADAQH/
MA0GCSqGSIb3DQEBBQUAA4IBAQB+SH0s/hrv5VYqgX6SNLzxdSLvCVsUkCdTpxwY
wOJ84XmYcXDMhKDtZqLtOtN6pfEwVusFlC9mkieuunztCnWNmsSG83RuljJPjFSi
1d4Id4bKEQkQ4cfnjoHKivRrViWLnxuNnLzC6tpyGH/35kKWhhr6T58AXerFgVw3
mHvLPTr1DuhdAZA0ZuvuseVAFFAjI3RetSySwHJE3ak8KswDVfLi6E3XxMVsIWTS
/iFsC2WwoZQlljya2V/kRYIhu+uCdqJ01wunn2BvmURPSgr4GTBF0FQ9JGpNbXxM
TAU7oQJgyFc5sCcuEgPTO0dWVQTvdZVgay4tkmduKDRkmJBF
-----END CERTIFICATE-----

View File

@@ -0,0 +1,15 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (OPENSSL_FOUND)
init_target (echo_server_tls)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl()
final_target ()
endif()

View File

@@ -0,0 +1,24 @@
## Main development example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env_cpp11.Program('echo_server_tls', ["echo_server_tls.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs] + [tls_libs]
prgs += env.Program('echo_server_tls', ["echo_server_tls.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,72 @@
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
typedef websocketpp::server<websocketpp::config::asio_tls> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio::message_type::ptr message_ptr;
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
std::string get_password() {
return "test";
}
context_ptr on_tls_init(websocketpp::connection_hdl hdl) {
std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::tlsv1);
try {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::single_dh_use);
ctx->set_password_callback(bind(&get_password));
ctx->use_certificate_chain_file("server.pem");
ctx->use_private_key_file("server.pem", boost::asio::ssl::context::pem);
} catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
return ctx;
}
int main() {
// Create a server endpoint
server echo_server;
// Initialize ASIO
echo_server.init_asio();
// Register our message handler
echo_server.set_message_handler(bind(&on_message,&echo_server,::_1,::_2));
echo_server.set_tls_init_handler(bind(&on_tls_init,::_1));
// Listen on port 9002
echo_server.listen(9002);
// Start the server accept loop
echo_server.start_accept();
// Start the ASIO io_service run loop
echo_server.run();
}

View File

@@ -0,0 +1,58 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,A0ED66EF872A48A9
gXuvKojXzApVhhPVNdRliiajbC4PtwQG5c8TA7JADLgwOR7o9t6KtXEr37bDRpvB
9aO9P+SJaK5OOp3XKPGthOdqv+tvCRTlmzmC8GjPLBX389DWT2xoGu7JkGwDtdSm
rnF49Rlp5bfjpACk5xKNiKeDo1CWfeEJzw9Kto0g+5eMaEdors64oPzjXs3geA2g
TxCJSHv9qSX6++pCLKKCUTbyzidAxV/Zb0AAubt5V40QKqX4HhSwwstFnTaX3tlb
3QOdY+y04VIkM6d7qN5W8M7NzRkMpZ1qBpQcUMpkhQcRzWP2wub5AAff9D2GntRd
4Dz1vn3u41U3Okdr0CNj+iH7byCzuokoAhk6ZQEN6WB+GTpGgfBXdtUZrfpb0MKm
UNYP5AF2AmUqJRXhViTDVtu/V2tHF3LGuNT+W2Dz+spFZEq0byEO0N858eR0dikc
6jOASvNQbSwD0+mkgBC1gXKKU3ngj2gpJUwljeACdWFd8N2egrZfyI05CmX7vPNC
NXbs7k2buWNdjP4/D8IM+HDVidWzQa/kG/qokXKqllem9Egg37lUucwnP3cX2/Hw
U2mfaBWzeZtqc+GqRp08rYIql+Reai3sUYlQMnNk01prVY47UQb+dxuqjaxGV5Xx
Xkx0s2mfQnNRjL4S7Hjhqelufi6GpkCQ2EGsPpA+6K1ztZ0ame9Q2BE1SXeM/6vU
rxT5nRrCxueyXAyQSGcqMX9//gSeK8WWBqG/c1IAMVDa0NWrJeOJhSziE+ta3B0m
bHAPBY6vh0iB3lLdRlbUOPbC6R1TpxMOs+6Vbs2+OTifFpvOVymEoZq/nroyg68P
vn5uCKogwWA7o8EArf/UTlIwWJmH9bgILdZKld4wMel2HQg16RDzm+mEXAJi52a/
FC+fgfphdxltmUJ+rqOyR4AHULjaTWUQqTIB6sdlzgmES1nXAiE71zX//KFqomar
O60SPPk3C1bs0x5DsvmGJa8SIfDhyd+D7NPyqwEKqrZsaotYGklNkfqxa6pa8mrc
ejxquW1PK4FvBk26+osu5a90Jih0PcQM7DUMMr2WHdTiMSXWAiK2ToYF8Itt25Qv
Cd0CsSYw9CJkXNr1u1+mObheaY9QYOmztnSJLy4ZO2JsMhqNwuAueIcwmhXOREq7
kzlnGMgJcuSeAS/OBNj8Zgx0c7QQ0kzc+YmnOCsqoMtPsu/CsXJ4iJiM3Tki/2jT
bywrTiQwE6R3a/87GREOREX+WLicZBWX3k9/4tBL5XSe1p5wPpuIRQUDvAGNfNHP
JN7kujDF4SehilF1qtvCygAwvxHFDj+EwhXKNDKJzoZZIM15rAk3k92n2j6nz1qH
a3xOU05yydOlO6F6w51I1QoDddmkzCRNB0TeO3D6rekHsCK1aDWmC+qRcm2ZFtVz
sY6fdZN2NEmMQokIh9Opi1f8CSYSizPESMzdu2SF0xVO9n/IGIkn1ksK04O2BZo0
X3LBPHLfCRsQNY1eF17bj07fYU2oPZKs/XzJiwxkqK6LFvpeAVaYrtg9fqRO/UVe
QhUIj3BL550ocEpa15xLehLrmwzYiW5zwGjSHQ4EgZluGLCwyKGTh4QswEJRA9Rt
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIE0DCCA7igAwIBAgIJAM5MuKJezXq0MA0GCSqGSIb3DQEBBQUAMIGgMQswCQYD
VQQGEwJVUzERMA8GA1UECBMISWxsaW5vaXMxEDAOBgNVBAcTB0NoaWNhZ28xGDAW
BgNVBAoTD1phcGhveWQgU3R1ZGlvczEUMBIGA1UECxMLV2ViU29ja2V0KysxFjAU
BgNVBAMTDVBldGVyIFRob3Jzb24xJDAiBgkqhkiG9w0BCQEWFXdlYm1hc3RlckB6
YXBob3lkLmNvbTAeFw0xMTExMTUyMTIwMDZaFw0xMjExMTQyMTIwMDZaMIGgMQsw
CQYDVQQGEwJVUzERMA8GA1UECBMISWxsaW5vaXMxEDAOBgNVBAcTB0NoaWNhZ28x
GDAWBgNVBAoTD1phcGhveWQgU3R1ZGlvczEUMBIGA1UECxMLV2ViU29ja2V0Kysx
FjAUBgNVBAMTDVBldGVyIFRob3Jzb24xJDAiBgkqhkiG9w0BCQEWFXdlYm1hc3Rl
ckB6YXBob3lkLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBANR0
tdwAnIB8I9qRZ7QbzEWY95RpM7GIn0u/9oH90PzdHiE0rXSkKT+yw3XUzH0iw5t0
5dEwSC+srSP5Vm4cA6kXc94agVVaPW89tGcdP4fHptCruSrzQsDXELCPl5UUvMpA
YUcGisdXYPN/EeOoqb9wKWxoW5mREsyyeWWS89fYN5qU/d0QpbSvEWghqLbL/ZS2
hOlXT9LufOeA+vHiV1/T/h5xC7ecIH02YDQw1EnqxbPmkLPcWThztLS9FiufNDRM
Rhcoaj2b9VDHvDwdbeA0T5v5qNdG34LaapYOelxzQMOtM0f9Dgqehodyxl2qm9mR
lq432dlOEzDnVCPNHwECAwEAAaOCAQkwggEFMB0GA1UdDgQWBBTTPKfNMnKOykhv
+vKS7vql5JsMyzCB1QYDVR0jBIHNMIHKgBTTPKfNMnKOykhv+vKS7vql5JsMy6GB
pqSBozCBoDELMAkGA1UEBhMCVVMxETAPBgNVBAgTCElsbGlub2lzMRAwDgYDVQQH
EwdDaGljYWdvMRgwFgYDVQQKEw9aYXBob3lkIFN0dWRpb3MxFDASBgNVBAsTC1dl
YlNvY2tldCsrMRYwFAYDVQQDEw1QZXRlciBUaG9yc29uMSQwIgYJKoZIhvcNAQkB
FhV3ZWJtYXN0ZXJAemFwaG95ZC5jb22CCQDOTLiiXs16tDAMBgNVHRMEBTADAQH/
MA0GCSqGSIb3DQEBBQUAA4IBAQB+SH0s/hrv5VYqgX6SNLzxdSLvCVsUkCdTpxwY
wOJ84XmYcXDMhKDtZqLtOtN6pfEwVusFlC9mkieuunztCnWNmsSG83RuljJPjFSi
1d4Id4bKEQkQ4cfnjoHKivRrViWLnxuNnLzC6tpyGH/35kKWhhr6T58AXerFgVw3
mHvLPTr1DuhdAZA0ZuvuseVAFFAjI3RetSySwHJE3ak8KswDVfLi6E3XxMVsIWTS
/iFsC2WwoZQlljya2V/kRYIhu+uCdqJ01wunn2BvmURPSgr4GTBF0FQ9JGpNbXxM
TAU7oQJgyFc5sCcuEgPTO0dWVQTvdZVgay4tkmduKDRkmJBF
-----END CERTIFICATE-----

View File

@@ -0,0 +1,87 @@
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
struct connection_data {
int sessionid;
std::string name;
};
struct custom_config : public websocketpp::config::asio {
// pull default settings from our core config
typedef websocketpp::config::asio core;
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
typedef core::rng_type rng_type;
typedef core::transport_type transport_type;
typedef core::endpoint_base endpoint_base;
// Set a custom connection_base class
typedef connection_data connection_base;
};
typedef websocketpp::server<custom_config> server;
typedef server::connection_ptr connection_ptr;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
class print_server {
public:
print_server() : m_next_sessionid(1) {
m_server.init_asio();
m_server.set_open_handler(bind(&print_server::on_open,this,::_1));
m_server.set_close_handler(bind(&print_server::on_close,this,::_1));
m_server.set_message_handler(bind(&print_server::on_message,this,::_1,::_2));
}
void on_open(connection_hdl hdl) {
connection_ptr con = m_server.get_con_from_hdl(hdl);
con->sessionid = m_next_sessionid++;
}
void on_close(connection_hdl hdl) {
connection_ptr con = m_server.get_con_from_hdl(hdl);
std::cout << "Closing connection " << con->name
<< " with sessionid " << con->sessionid << std::endl;
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_ptr con = m_server.get_con_from_hdl(hdl);
if (con->name == "") {
con->name = msg->get_payload();
std::cout << "Setting name of connection with sessionid "
<< con->sessionid << " to " << con->name << std::endl;
} else {
std::cout << "Got a message from connection " << con->name
<< " with sessionid " << con->sessionid << std::endl;
}
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
private:
int m_next_sessionid;
server m_server;
};
int main() {
print_server server;
server.run(9002);
}

View File

@@ -0,0 +1,42 @@
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::ref;
void custom_on_msg(server & s, connection_hdl hdl, server::message_ptr msg) {
std::cout << "Message sent to custom handler" << std::endl;
}
void default_on_msg(server & s, connection_hdl hdl, server::message_ptr msg) {
std::cout << "Message sent to default handler" << std::endl;
if (msg->get_payload() == "upgrade") {
// Upgrade our connection_hdl to a full connection_ptr
server::connection_ptr con = s.get_con_from_hdl(hdl);
// Change the on message handler for this connection only to
// custom_on_mesage
con->set_message_handler(bind(&custom_on_msg,ref(s),::_1,::_2));
std::cout << "Upgrading connection to custom handler" << std::endl;
}
}
int main() {
server s;
s.set_message_handler(bind(&default_on_msg,ref(s),::_1,::_2));
s.init_asio();
s.listen(9002);
s.start_accept();
s.run();
}

View File

@@ -0,0 +1,23 @@
## iostream server example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('iostream_server', ["iostream_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('iostream_server', ["iostream_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,93 @@
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
#include <fstream>
typedef websocketpp::server<websocketpp::config::core> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
s->get_alog().write(websocketpp::log::alevel::app,
"Text Message Received: "+msg->get_payload());
} else {
s->get_alog().write(websocketpp::log::alevel::app,
"Binary Message Received: "+websocketpp::utility::to_hex(msg->get_payload()));
}
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (const websocketpp::lib::error_code& e) {
s->get_alog().write(websocketpp::log::alevel::app,
"Echo Failed: "+e.message());
}
}
int main() {
server s;
std::ofstream log;
try {
// set up access channels to only log interesting things
s.clear_access_channels(websocketpp::log::alevel::all);
s.set_access_channels(websocketpp::log::alevel::connect);
s.set_access_channels(websocketpp::log::alevel::disconnect);
s.set_access_channels(websocketpp::log::alevel::app);
// Log to a file rather than stdout, as we are using stdout for real
// output
log.open("output.log");
s.get_alog().set_ostream(&log);
s.get_elog().set_ostream(&log);
// print all output to stdout
s.register_ostream(&std::cout);
// Register our message handler
s.set_message_handler(bind(&on_message,&s,::_1,::_2));
server::connection_ptr con = s.get_connection();
con->start();
// C++ iostream's don't support the idea of asynchronous i/o. As such
// there are two input strategies demonstrated here. Buffered I/O will
// read from stdin in chunks until EOF. This works very well for
// replaying canned connections as would be done in automated testing.
//
// If the server is being used live however, assuming input is being
// piped from elsewhere in realtime, this strategy will result in small
// messages being buffered forever. The non-buffered strategy below
// reads characters from stdin one at a time. This is inefficient and
// for more serious uses should be replaced with a platform specific
// asyncronous i/o technique like select, poll, IOCP, etc
bool buffered_io = false;
if (buffered_io) {
std::cin >> *con;
con->eof();
} else {
char a;
while(std::cin.get(a)) {
con->read_some(&a,1);
}
con->eof();
}
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
log.close();
}

View File

@@ -0,0 +1,10 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (print_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Print server example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('print_server', ["print_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('print_server', ["print_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,22 @@
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
void on_message(websocketpp::connection_hdl, server::message_ptr msg) {
std::cout << msg->get_payload() << std::endl;
}
int main() {
server print_server;
print_server.set_message_handler(&on_message);
print_server.init_asio();
print_server.listen(9002);
print_server.start_accept();
print_server.run();
}

View File

@@ -0,0 +1,51 @@
#include <set>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
class broadcast_server {
public:
broadcast_server() {
m_server.init_asio();
m_server.set_open_handler(bind(&broadcast_server::on_open,this,::_1));
m_server.set_close_handler(bind(&broadcast_server::on_close,this,::_1));
m_server.set_message_handler(bind(&broadcast_server::on_message,this,::_1,::_2));
}
void on_open(connection_hdl hdl) {
m_connections.insert(hdl);
}
void on_close(connection_hdl hdl) {
m_connections.erase(hdl);
}
void on_message(connection_hdl hdl, server::message_ptr msg) {
for (auto it : m_connections) {
m_server.send(it,msg);
}
}
void run(uint16_t port) {
m_server.listen(port);
m_server.start_accept();
m_server.run();
}
private:
typedef std::set<connection_hdl,std::owner_less<connection_hdl>> con_list;
server m_server;
con_list m_connections;
};
int main() {
broadcast_server server;
server.run(9002);
}

View File

@@ -0,0 +1,11 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (sip_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,22 @@
Checkout the project from git
At the top level, run cmake:
cmake -G 'Unix Makefiles' \
-D BUILD_EXAMPLES=ON \
-D WEBSOCKETPP_ROOT=/tmp/cm1 \
-D ENABLE_CPP11=OFF .
and then make the example:
make -C examples/sip_client
Now run it:
bin/sip_client ws://ws-server:80
It has been tested against the repro SIP proxy from reSIProcate
http://www.resiprocate.org/WebRTC_and_SIP_Over_WebSockets

View File

@@ -0,0 +1,23 @@
## SIP client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is avaliable build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,88 @@
#include <condition_variable>
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
#include <boost/thread/thread.hpp>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
// Create a server endpoint
client sip_client;
bool received;
void on_open(client* c, websocketpp::connection_hdl hdl) {
// now it is safe to use the connection
std::cout << "connection ready" << std::endl;
received=false;
// Send a SIP OPTIONS message to the server:
std::string SIP_msg="OPTIONS sip:carol@chicago.com SIP/2.0\r\nVia: SIP/2.0/WS df7jal23ls0d.invalid;rport;branch=z9hG4bKhjhs8ass877\r\nMax-Forwards: 70\r\nTo: <sip:carol@chicago.com>\r\nFrom: Alice <sip:alice@atlanta.com>;tag=1928301774\r\nCall-ID: a84b4c76e66710\r\nCSeq: 63104 OPTIONS\r\nContact: <sip:alice@pc33.atlanta.com>\r\nAccept: application/sdp\r\nContent-Length: 0\r\n\r\n";
sip_client.send(hdl, SIP_msg.c_str(), websocketpp::frame::opcode::text);
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
client::connection_ptr con = sip_client.get_con_from_hdl(hdl);
std::cout << "Received a reply:" << std::endl;
fwrite(msg->get_payload().c_str(), msg->get_payload().size(), 1, stdout);
received=true;
}
int main(int argc, char* argv[]) {
std::string uri = "ws://localhost:9001";
if (argc == 2) {
uri = argv[1];
}
try {
// We expect there to be a lot of errors, so suppress them
sip_client.clear_access_channels(websocketpp::log::alevel::all);
sip_client.clear_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
sip_client.init_asio();
// Register our handlers
sip_client.set_open_handler(bind(&on_open,&sip_client,::_1));
sip_client.set_message_handler(bind(&on_message,&sip_client,::_1,::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = sip_client.get_connection(uri, ec);
// Specify the SIP subprotocol:
con->add_subprotocol("sip");
sip_client.connect(con);
// Start the ASIO io_service run loop
sip_client.run();
while(!received) {
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
std::cout << "done" << std::endl;
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,23 @@
## Main development example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('subprotocol_server', ["subprotocol_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('subprotocol_server', ["subprotocol_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,52 @@
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> server;
using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
using websocketpp::lib::ref;
bool validate(server & s, connection_hdl hdl) {
server::connection_ptr con = s.get_con_from_hdl(hdl);
std::cout << "Cache-Control: " << con->get_request_header("Cache-Control") << std::endl;
const std::vector<std::string> & subp_requests = con->get_requested_subprotocols();
std::vector<std::string>::const_iterator it;
for (it = subp_requests.begin(); it != subp_requests.end(); ++it) {
std::cout << "Requested: " << *it << std::endl;
}
if (subp_requests.size() > 0) {
con->select_subprotocol(subp_requests[0]);
}
return true;
}
int main() {
try {
server s;
s.set_validate_handler(bind(&validate,ref(s),::_1));
s.init_asio();
s.listen(9005);
s.start_accept();
s.run();
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,10 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (telemetry_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Telemetry client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('telemetry_client', ["telemetry_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('telemetry_client', ["telemetry_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,156 @@
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
// This header pulls in the WebSocket++ abstracted thread support that will
// select between boost::thread and std::thread based on how the build system
// is configured.
#include <websocketpp/common/thread.hpp>
/**
* The telemetry client connects to a WebSocket server and sends a message every
* second containing an integer count. This example can be used as the basis for
* programs where a client connects and pushes data for logging, stress/load
* testing, etc.
*/
class telemetry_client {
public:
typedef websocketpp::client<websocketpp::config::asio_client> client;
typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
telemetry_client() : m_open(false),m_done(false) {
// set up access channels to only log interesting things
m_client.clear_access_channels(websocketpp::log::alevel::all);
m_client.set_access_channels(websocketpp::log::alevel::connect);
m_client.set_access_channels(websocketpp::log::alevel::disconnect);
m_client.set_access_channels(websocketpp::log::alevel::app);
// Initialize the Asio transport policy
m_client.init_asio();
// Bind the handlers we are using
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::bind;
m_client.set_open_handler(bind(&telemetry_client::on_open,this,::_1));
m_client.set_close_handler(bind(&telemetry_client::on_close,this,::_1));
m_client.set_fail_handler(bind(&telemetry_client::on_fail,this,::_1));
}
// This method will block until the connection is complete
void run(const std::string & uri) {
// Create a new connection to the given URI
websocketpp::lib::error_code ec;
client::connection_ptr con = m_client.get_connection(uri, ec);
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Get Connection Error: "+ec.message());
return;
}
// Grab a handle for this connection so we can talk to it in a thread
// safe manor after the event loop starts.
m_hdl = con->get_handle();
// Queue the connection. No DNS queries or network connections will be
// made until the io_service event loop is run.
m_client.connect(con);
// Create a thread to run the ASIO io_service event loop
websocketpp::lib::thread asio_thread(&client::run, &m_client);
// Create a thread to run the telemetry loop
websocketpp::lib::thread telemetry_thread(&telemetry_client::telemetry_loop,this);
asio_thread.join();
telemetry_thread.join();
}
// The open handler will signal that we are ready to start sending telemetry
void on_open(websocketpp::connection_hdl) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection opened, starting telemetry!");
scoped_lock guard(m_lock);
m_open = true;
}
// The close handler will signal that we should stop sending telemetry
void on_close(websocketpp::connection_hdl) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection closed, stopping telemetry!");
scoped_lock guard(m_lock);
m_done = true;
}
// The fail handler will signal that we should stop sending telemetry
void on_fail(websocketpp::connection_hdl) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Connection failed, stopping telemetry!");
scoped_lock guard(m_lock);
m_done = true;
}
void telemetry_loop() {
uint64_t count = 0;
std::stringstream val;
websocketpp::lib::error_code ec;
while(1) {
bool wait = false;
{
scoped_lock guard(m_lock);
// If the connection has been closed, stop generating telemetry
if (m_done) {break;}
// If the connection hasn't been opened yet wait a bit and retry
if (!m_open) {
wait = true;
}
}
if (wait) {
sleep(1);
continue;
}
val.str("");
val << "count is " << count++;
m_client.get_alog().write(websocketpp::log::alevel::app, val.str());
m_client.send(m_hdl,val.str(),websocketpp::frame::opcode::text,ec);
// The most likely error that we will get is that the connection is
// not in the right state. Usually this means we tried to send a
// message to a connection that was closed or in the process of
// closing. While many errors here can be easily recovered from,
// in this simple example, we'll stop the telemetry loop.
if (ec) {
m_client.get_alog().write(websocketpp::log::alevel::app,
"Send Error: "+ec.message());
break;
}
sleep(1);
}
}
private:
client m_client;
websocketpp::connection_hdl m_hdl;
websocketpp::lib::mutex m_lock;
bool m_open;
bool m_done;
};
int main(int argc, char* argv[]) {
telemetry_client c;
std::string uri = "ws://localhost:9002";
if (argc == 2) {
uri = argv[1];
}
c.run(uri);
}

View File

@@ -0,0 +1,11 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (testee_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Autobahn test client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('testee_client', ["testee_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('testee_client', ["testee_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,84 @@
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
int case_count = 0;
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
client::connection_ptr con = c->get_con_from_hdl(hdl);
if (con->get_resource() == "/getCaseCount") {
std::cout << "Detected " << msg->get_payload() << " test cases."
<< std::endl;
case_count = atoi(msg->get_payload().c_str());
} else {
c->send(hdl, msg->get_payload(), msg->get_opcode());
}
}
int main(int argc, char* argv[]) {
// Create a server endpoint
client c;
std::string uri = "ws://localhost:9001";
if (argc == 2) {
uri = argv[1];
}
try {
// We expect there to be a lot of errors, so suppress them
c.clear_access_channels(websocketpp::log::alevel::all);
c.clear_error_channels(websocketpp::log::elevel::all);
// Initialize ASIO
c.init_asio();
// Register our handlers
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri+"/getCaseCount", ec);
c.connect(con);
// Start the ASIO io_service run loop
c.run();
std::cout << "case count: " << case_count << std::endl;
for (int i = 1; i <= case_count; i++) {
c.reset();
std::stringstream url;
url << uri << "/runCase?case=" << i << "&agent="
<< websocketpp::user_agent;
con = c.get_connection(url.str(), ec);
c.connect(con);
c.run();
}
std::cout << "done" << std::endl;
} catch (const std::exception & e) {
std::cout << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,23 @@
## Autobahn Testee Server
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('testee_server', ["testee_server.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('testee_server', ["testee_server.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,142 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
struct testee_config : public websocketpp::config::asio {
// pull default settings from our core config
typedef websocketpp::config::asio core;
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
typedef core::rng_type rng_type;
typedef core::endpoint_base endpoint_base;
static bool const enable_multithreading = true;
struct transport_config : public core::transport_config {
typedef core::concurrency_type concurrency_type;
typedef core::elog_type elog_type;
typedef core::alog_type alog_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
static bool const enable_multithreading = true;
};
typedef websocketpp::transport::asio::endpoint<transport_config>
transport_type;
static const websocketpp::log::level elog_level =
websocketpp::log::elevel::none;
static const websocketpp::log::level alog_level =
websocketpp::log::alevel::none;
};
typedef websocketpp::server<testee_config> server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
// pull out the type of messages sent by our config
typedef server::message_ptr message_ptr;
// Define a callback to handle incoming messages
void on_message(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}
void on_socket_init(websocketpp::connection_hdl, boost::asio::ip::tcp::socket & s) {
boost::asio::ip::tcp::no_delay option(true);
s.set_option(option);
}
int main(int argc, char * argv[]) {
// Create a server endpoint
server testee_server;
short port = 9002;
size_t num_threads = 1;
if (argc == 3) {
port = atoi(argv[1]);
num_threads = atoi(argv[2]);
}
try {
// Total silence
testee_server.clear_access_channels(websocketpp::log::alevel::all);
testee_server.clear_error_channels(websocketpp::log::alevel::all);
// Initialize ASIO
testee_server.init_asio();
testee_server.set_reuse_addr(true);
// Register our message handler
testee_server.set_message_handler(bind(&on_message,&testee_server,::_1,::_2));
testee_server.set_socket_init_handler(bind(&on_socket_init,::_1,::_2));
// Listen on specified port with extended listen backlog
testee_server.set_listen_backlog(8192);
testee_server.listen(port);
// Start the server accept loop
testee_server.start_accept();
// Start the ASIO io_service run loop
if (num_threads == 1) {
testee_server.run();
} else {
typedef websocketpp::lib::shared_ptr<websocketpp::lib::thread> thread_ptr;
std::vector<thread_ptr> ts;
for (size_t i = 0; i < num_threads; i++) {
ts.push_back(websocketpp::lib::make_shared<websocketpp::lib::thread>(&server::run, &testee_server));
}
for (size_t i = 0; i < num_threads; i++) {
ts[i]->join();
}
}
} catch (const std::exception & e) {
std::cout << "exception: " << e.what() << std::endl;
} catch (websocketpp::lib::error_code e) {
std::cout << "error code: " << e.message() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}

View File

@@ -0,0 +1,11 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (utility_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()

View File

@@ -0,0 +1,23 @@
## Utility client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is available build using that, otherwise use boost
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
ALL_LIBS = boostlibs(['system'],env_cpp11) + [platform_libs] + [polyfill_libs]
prgs += env_cpp11.Program('utility_client', ["utility_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('utility_client', ["utility_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')

View File

@@ -0,0 +1,270 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// **NOTE:** This file is a snapshot of the WebSocket++ utility client tutorial.
// Additional related material can be found in the tutorials/utility_client
// directory of the WebSocket++ repository.
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/common/memory.hpp>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <sstream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
class connection_metadata {
public:
typedef websocketpp::lib::shared_ptr<connection_metadata> ptr;
connection_metadata(int id, websocketpp::connection_hdl hdl, std::string uri)
: m_id(id)
, m_hdl(hdl)
, m_status("Connecting")
, m_uri(uri)
, m_server("N/A")
{}
void on_open(client * c, websocketpp::connection_hdl hdl) {
m_status = "Open";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
}
void on_fail(client * c, websocketpp::connection_hdl hdl) {
m_status = "Failed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
m_server = con->get_response_header("Server");
m_error_reason = con->get_ec().message();
}
void on_close(client * c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
std::stringstream s;
s << "close code: " << con->get_remote_close_code() << " ("
<< websocketpp::close::status::get_string(con->get_remote_close_code())
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
websocketpp::connection_hdl get_hdl() const {
return m_hdl;
}
int get_id() const {
return m_id;
}
std::string get_status() const {
return m_status;
}
friend std::ostream & operator<< (std::ostream & out, connection_metadata const & data);
private:
int m_id;
websocketpp::connection_hdl m_hdl;
std::string m_status;
std::string m_uri;
std::string m_server;
std::string m_error_reason;
};
std::ostream & operator<< (std::ostream & out, connection_metadata const & data) {
out << "> URI: " << data.m_uri << "\n"
<< "> Status: " << data.m_status << "\n"
<< "> Remote Server: " << (data.m_server.empty() ? "None Specified" : data.m_server) << "\n"
<< "> Error/close reason: " << (data.m_error_reason.empty() ? "N/A" : data.m_error_reason);
return out;
}
class websocket_endpoint {
public:
websocket_endpoint () : m_next_id(0) {
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
m_endpoint.init_asio();
m_endpoint.start_perpetual();
m_thread = websocketpp::lib::make_shared<websocketpp::lib::thread>(&client::run, &m_endpoint);
}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
for (con_list::const_iterator it = m_connection_list.begin(); it != m_connection_list.end(); ++it) {
if (it->second->get_status() != "Open") {
// Only close open connections
continue;
}
std::cout << "> Closing connection " << it->second->get_id() << std::endl;
websocketpp::lib::error_code ec;
m_endpoint.close(it->second->get_hdl(), websocketpp::close::status::going_away, "", ec);
if (ec) {
std::cout << "> Error closing connection " << it->second->get_id() << ": "
<< ec.message() << std::endl;
}
}
m_thread->join();
}
int connect(std::string const & uri) {
websocketpp::lib::error_code ec;
client::connection_ptr con = m_endpoint.get_connection(uri, ec);
if (ec) {
std::cout << "> Connect initialization error: " << ec.message() << std::endl;
return -1;
}
int new_id = m_next_id++;
connection_metadata::ptr metadata_ptr = websocketpp::lib::make_shared<connection_metadata>(new_id, con->get_handle(), uri);
m_connection_list[new_id] = metadata_ptr;
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_fail_handler(websocketpp::lib::bind(
&connection_metadata::on_fail,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
con->set_close_handler(websocketpp::lib::bind(
&connection_metadata::on_close,
metadata_ptr,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
m_endpoint.connect(con);
return new_id;
}
void close(int id, websocketpp::close::status::value code, std::string reason) {
websocketpp::lib::error_code ec;
con_list::iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
std::cout << "> No connection found with id " << id << std::endl;
return;
}
m_endpoint.close(metadata_it->second->get_hdl(), code, reason, ec);
if (ec) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
connection_metadata::ptr get_metadata(int id) const {
con_list::const_iterator metadata_it = m_connection_list.find(id);
if (metadata_it == m_connection_list.end()) {
return connection_metadata::ptr();
} else {
return metadata_it->second;
}
}
private:
typedef std::map<int,connection_metadata::ptr> con_list;
client m_endpoint;
websocketpp::lib::shared_ptr<websocketpp::lib::thread> m_thread;
con_list m_connection_list;
int m_next_id;
};
int main() {
bool done = false;
std::string input;
websocket_endpoint endpoint;
while (!done) {
std::cout << "Enter Command: ";
std::getline(std::cin, input);
if (input == "quit") {
done = true;
} else if (input == "help") {
std::cout
<< "\nCommand List:\n"
<< "connect <ws uri>\n"
<< "close <connection id> [<close code:default=1000>] [<close reason>]\n"
<< "show <connection id>\n"
<< "help: Display this help text\n"
<< "quit: Exit the program\n"
<< std::endl;
} else if (input.substr(0,7) == "connect") {
int id = endpoint.connect(input.substr(8));
if (id != -1) {
std::cout << "> Created connection with id " << id << std::endl;
}
} else if (input.substr(0,5) == "close") {
std::stringstream ss(input);
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason = "";
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
endpoint.close(id, close_code, reason);
} else if (input.substr(0,4) == "show") {
int id = atoi(input.substr(5).c_str());
connection_metadata::ptr metadata = endpoint.get_metadata(id);
if (metadata) {
std::cout << *metadata << std::endl;
} else {
std::cout << "> Unknown connection id " << id << std::endl;
}
} else {
std::cout << "> Unrecognized Command" << std::endl;
}
}
return 0;
}

45
src/websocketpp/readme.md Normal file
View File

@@ -0,0 +1,45 @@
WebSocket++ (0.4.0)
==========================
WebSocket++ is a header only C++ library that implements RFC6455 The WebSocket
Protocol. It allows integrating WebSocket client and server functionality into
C++ programs. It uses interchangeable network transport modules including one
based on C++ iostreams and one based on Boost Asio.
Major Features
==============
* Full support for RFC6455
* Partial support for Hixie 76 / Hybi 00, 07-17 draft specs (server only)
* Message/event based interface
* Supports secure WebSockets (TLS), IPv6, and explicit proxies.
* Flexible dependency management (C++11 Standard Library or Boost)
* Interchangeable network transport modules (iostream and Boost Asio)
* Portable/cross platform (Posix/Windows, 32/64bit, Intel/ARM/PPC)
* Thread-safe
Get Involved
============
[![Build Status](https://travis-ci.org/zaphoyd/websocketpp.png)](https://travis-ci.org/zaphoyd/websocketpp)
**Project Website**
http://www.zaphoyd.com/websocketpp/
**User Manual**
http://www.zaphoyd.com/websocketpp/manual/
**GitHub Repository**
https://github.com/zaphoyd/websocketpp/
**Announcements Mailing List**
http://groups.google.com/group/websocketpp-announcements/
**IRC Channel**
#websocketpp (freenode)
**Discussion / Development / Support Mailing List / Forum**
http://groups.google.com/group/websocketpp/
Author
======
Peter Thorson - websocketpp@zaphoyd.com

View File

@@ -0,0 +1,43 @@
Complete & Tested:
- Server and client roles pass all Autobahn v0.5.9 test suite tests strictly
- Streaming UTF8 validation
- random number generation
- iostream based transport
- C++11 support
- LLVM/Clang support
- GCC support
- 64 bit support
- 32 bit support
- Logging
- Client role
- message_handler
- ping_handler
- pong_handler
- open_handler
- close_handler
- echo_server & echo_server_tls
- External io_service support
- TLS support
- exception/error handling
- Timeouts
- Subprotocol negotiation
- validate_handler
- Hybi 00/Hixie 76 legacy protocol support
- Outgoing Proxy Support
- socket_init_handler
- tls_init_handler
- tcp_init_handler
Ongoing work
- Performance tuning
- PowerPC support
- Visual Studio / Windows support
- CMake build/install support
- http_handler
Future feature roadmap
- Extension support
- permessage_compress extension
- Message buffer pool
- flow control
- tutorials & documentation

View File

@@ -0,0 +1,25 @@
## connection unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs]
objs = env.Object('connection_boost.o', ["connection.cpp"], LIBS = BOOST_LIBS)
objs = env.Object('connection_tu2_boost.o', ["connection_tu2.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_connection_boost', ["connection_boost.o","connection_tu2_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('connection_stl.o', ["connection.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('connection_tu2_stl.o', ["connection_tu2.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_connection_stl', ["connection_stl.o","connection_tu2_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,241 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE connection
#include <boost/test/unit_test.hpp>
#include "connection_tu2.hpp"
// NOTE: these tests currently test against hardcoded output values. I am not
// sure how problematic this will be. If issues arise like order of headers the
// output should be parsed by http::response and have values checked directly
BOOST_AUTO_TEST_CASE( basic_http_request ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 426 Upgrade Required\r\nServer: " +
std::string(websocketpp::user_agent)+"\r\n\r\n";
std::string o2 = run_server_test(input);
BOOST_CHECK(o2 == output);
}
struct connection_extension {
connection_extension() : extension_value(5) {}
int extension_method() {
return extension_value;
}
bool is_server() const {
return false;
}
int extension_value;
};
struct stub_config : public websocketpp::config::core {
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
typedef core::rng_type rng_type;
typedef core::transport_type transport_type;
typedef core::endpoint_base endpoint_base;
typedef connection_extension connection_base;
};
struct connection_setup {
connection_setup(bool p_is_server) : c(p_is_server, "", alog, elog, rng) {}
websocketpp::lib::error_code ec;
stub_config::alog_type alog;
stub_config::elog_type elog;
stub_config::rng_type rng;
websocketpp::connection<stub_config> c;
};
/*void echo_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}*/
void validate_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}
bool validate_set_ua(server* s, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
con->replace_header("Server","foo");
return true;
}
void http_func(server* s, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::string res = con->get_resource();
con->set_body(res);
con->set_status(websocketpp::http::status_code::ok);
}
BOOST_AUTO_TEST_CASE( connection_extensions ) {
connection_setup env(true);
BOOST_CHECK( env.c.extension_value == 5 );
BOOST_CHECK( env.c.extension_method() == 5 );
BOOST_CHECK( env.c.is_server() == true );
}
BOOST_AUTO_TEST_CASE( basic_websocket_request ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: ";
output+=websocketpp::user_agent;
output+="\r\nUpgrade: websocket\r\n\r\n";
server s;
s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
BOOST_CHECK(run_server_test(s,input) == output);
}
BOOST_AUTO_TEST_CASE( http_request ) {
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 200 OK\r\nContent-Length: 8\r\nServer: ";
output+=websocketpp::user_agent;
output+="\r\n\r\n/foo/bar";
server s;
s.set_http_handler(bind(&http_func,&s,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( request_no_server_header ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nUpgrade: websocket\r\n\r\n";
server s;
s.set_user_agent("");
s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( request_no_server_header_override ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
server s;
s.set_user_agent("");
s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
s.set_validate_handler(bind(&validate_set_ua,&s,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( basic_client_websocket ) {
std::string uri = "ws://localhost";
//std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
std::string ref = "GET / HTTP/1.1\r\nConnection: Upgrade\r\nFoo: Bar\r\nHost: localhost\r\nSec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\nSec-WebSocket-Version: 13\r\nUpgrade: websocket\r\nUser-Agent: foo\r\n\r\n";
std::stringstream output;
client e;
e.set_access_channels(websocketpp::log::alevel::none);
e.set_error_channels(websocketpp::log::elevel::none);
e.set_user_agent("foo");
e.register_ostream(&output);
client::connection_ptr con;
websocketpp::lib::error_code ec;
con = e.get_connection(uri, ec);
con->append_header("Foo","Bar");
e.connect(con);
BOOST_CHECK_EQUAL(ref, output.str());
}
BOOST_AUTO_TEST_CASE( set_max_message_size ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
// After the handshake, add a single frame with a message that is too long.
char frame0[10] = {char(0x82), char(0x83), 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01};
input.append(frame0, 10);
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: foo\r\nUpgrade: websocket\r\n\r\n";
// After the handshake, add a single frame with a close message with message too big
// error code.
char frame1[4] = {char(0x88), 0x19, 0x03, char(0xf1)};
output.append(frame1, 4);
output.append("A message was too large");
server s;
s.set_user_agent("");
s.set_validate_handler(bind(&validate_set_ua,&s,::_1));
s.set_max_message_size(2);
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
// TODO: set max message size in client endpoint test case
// TODO: set max message size mid connection test case
// TODO: [maybe] set max message size in open handler
/*
BOOST_AUTO_TEST_CASE( user_reject_origin ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example2.com\r\n\r\n";
std::string output = "HTTP/1.1 403 Forbidden\r\nServer: "+websocketpp::USER_AGENT+"\r\n\r\n";
BOOST_CHECK(run_server_test(input) == output);
}
BOOST_AUTO_TEST_CASE( basic_text_message ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
unsigned char frames[8] = {0x82,0x82,0xFF,0xFF,0xFF,0xFF,0xD5,0xD5};
input.append(reinterpret_cast<char*>(frames),8);
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: "+websocketpp::USER_AGENT+"\r\nUpgrade: websocket\r\n\r\n**";
BOOST_CHECK( run_server_test(input) == output);
}
*/

View File

@@ -0,0 +1,57 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "connection_tu2.hpp"
void echo_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}
std::string run_server_test(std::string input) {
server test_server;
return run_server_test(test_server,input);
}
std::string run_server_test(server & s, std::string input) {
server::connection_ptr con;
std::stringstream output;
s.clear_access_channels(websocketpp::log::alevel::all);
s.clear_error_channels(websocketpp::log::elevel::all);
s.register_ostream(&output);
con = s.get_connection();
con->start();
std::stringstream channel;
channel << input;
channel >> *con;
return output.str();
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <iostream>
#include <sstream>
// Test Environment:
// server, no TLS, no locks, iostream based transport
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
#include <websocketpp/client.hpp>
typedef websocketpp::server<websocketpp::config::core> server;
/// NOTE: the "server" config is being used for the client here because we don't
/// want to pull in the real RNG. A better way to do this might be a custom
/// client config with the RNG explicitly stubbed out.
typedef websocketpp::client<websocketpp::config::core> client;
typedef websocketpp::config::core::message_type::ptr message_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
void echo_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg);
std::string run_server_test(std::string input);
std::string run_server_test(server & s, std::string input);

View File

@@ -0,0 +1,24 @@
## endpoint unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs] + [tls_libs]
objs = env.Object('endpoint_boost.o', ["endpoint.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_endpoint_boost', ["endpoint_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
objs += env_cpp11.Object('endpoint_stl.o', ["endpoint.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_endpoint_stl', ["endpoint_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,101 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE endpoint
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <sstream>
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
BOOST_AUTO_TEST_CASE( construct_server_iostream ) {
websocketpp::server<websocketpp::config::core> s;
}
BOOST_AUTO_TEST_CASE( construct_server_asio_plain ) {
websocketpp::server<websocketpp::config::asio> s;
}
BOOST_AUTO_TEST_CASE( construct_server_asio_tls ) {
websocketpp::server<websocketpp::config::asio_tls> s;
}
BOOST_AUTO_TEST_CASE( initialize_server_asio ) {
websocketpp::server<websocketpp::config::asio> s;
s.init_asio();
}
BOOST_AUTO_TEST_CASE( initialize_server_asio_external ) {
websocketpp::server<websocketpp::config::asio> s;
boost::asio::io_service ios;
s.init_asio(&ios);
}
struct endpoint_extension {
endpoint_extension() : extension_value(5) {}
int extension_method() {
return extension_value;
}
bool is_server() const {
return false;
}
int extension_value;
};
struct stub_config : public websocketpp::config::core {
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
typedef core::rng_type rng_type;
typedef core::transport_type transport_type;
typedef endpoint_extension endpoint_base;
};
BOOST_AUTO_TEST_CASE( endpoint_extensions ) {
websocketpp::server<stub_config> s;
BOOST_CHECK( s.extension_value == 5 );
BOOST_CHECK( s.extension_method() == 5 );
BOOST_CHECK( s.is_server() == true );
}

View File

@@ -0,0 +1,27 @@
## http unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs] + ['z']
objs = env.Object('extension_boost.o', ["extension.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('permessage_deflate_boost.o', ["permessage_deflate.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_extension_boost', ["extension_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_permessage_deflate_boost', ["permessage_deflate_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs] + ['z']
objs += env_cpp11.Object('extension_stl.o', ["extension.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('permessage_deflate_stl.o', ["permessage_deflate.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_extension_stl', ["extension_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_permessage_deflate_stl', ["permessage_deflate_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,37 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE extension
#include <boost/test/unit_test.hpp>
#include <string>
#include <websocketpp/extensions/extension.hpp>
BOOST_AUTO_TEST_CASE( blank ) {
BOOST_CHECK( true );
}

View File

@@ -0,0 +1,543 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE permessage_deflate
#include <boost/test/unit_test.hpp>
#include <websocketpp/error.hpp>
#include <websocketpp/extensions/extension.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
#include <string>
#include <websocketpp/utilities.hpp>
#include <iostream>
class config {};
typedef websocketpp::extensions::permessage_deflate::enabled<config> enabled_type;
typedef websocketpp::extensions::permessage_deflate::disabled<config> disabled_type;
struct ext_vars {
enabled_type exts;
enabled_type extc;
websocketpp::lib::error_code ec;
websocketpp::err_str_pair esp;
websocketpp::http::attribute_list attr;
};
namespace pmde = websocketpp::extensions::permessage_deflate::error;
namespace pmd_mode = websocketpp::extensions::permessage_deflate::mode;
// Ensure the disabled extension behaves appropriately disabled
BOOST_AUTO_TEST_CASE( disabled_is_disabled ) {
disabled_type exts;
BOOST_CHECK( !exts.is_implemented() );
}
BOOST_AUTO_TEST_CASE( disabled_is_off ) {
disabled_type exts;
BOOST_CHECK( !exts.is_enabled() );
}
// Ensure the enabled version actually works
BOOST_AUTO_TEST_CASE( enabled_is_enabled ) {
ext_vars v;
BOOST_CHECK( v.exts.is_implemented() );
BOOST_CHECK( v.extc.is_implemented() );
}
BOOST_AUTO_TEST_CASE( enabled_starts_disabled ) {
ext_vars v;
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK( !v.extc.is_enabled() );
}
BOOST_AUTO_TEST_CASE( negotiation_empty_attr ) {
ext_vars v;
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
}
BOOST_AUTO_TEST_CASE( negotiation_invalid_attr ) {
ext_vars v;
v.attr["foo"] = "bar";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attributes) );
BOOST_CHECK_EQUAL( v.esp.second, "");
}
// Negotiate s2c_no_context_takeover
BOOST_AUTO_TEST_CASE( negotiate_s2c_no_context_takeover_invalid ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "foo";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
BOOST_CHECK_EQUAL( v.esp.second, "");
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_no_context_takeover ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover");
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_no_context_takeover_server_initiated ) {
ext_vars v;
v.exts.enable_s2c_no_context_takeover();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover");
}
// Negotiate c2s_no_context_takeover
BOOST_AUTO_TEST_CASE( negotiate_c2s_no_context_takeover_invalid ) {
ext_vars v;
v.attr["c2s_no_context_takeover"] = "foo";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
BOOST_CHECK_EQUAL( v.esp.second, "");
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_no_context_takeover ) {
ext_vars v;
v.attr["c2s_no_context_takeover"] = "";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_no_context_takeover");
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_no_context_takeover_server_initiated ) {
ext_vars v;
v.exts.enable_c2s_no_context_takeover();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_no_context_takeover");
}
// Negotiate s2c_max_window_bits
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_invalid ) {
ext_vars v;
std::vector<std::string> values;
values.push_back("");
values.push_back("foo");
values.push_back("7");
values.push_back("16");
std::vector<std::string>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
v.attr["s2c_max_window_bits"] = *it;
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
BOOST_CHECK_EQUAL( v.esp.second, "");
}
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_valid ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "8";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_max_window_bits=8");
v.attr["s2c_max_window_bits"] = "15";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
}
BOOST_AUTO_TEST_CASE( invalid_set_s2c_max_window_bits ) {
ext_vars v;
v.ec = v.exts.set_s2c_max_window_bits(7,pmd_mode::decline);
BOOST_CHECK_EQUAL(v.ec,pmde::make_error_code(pmde::invalid_max_window_bits));
v.ec = v.exts.set_s2c_max_window_bits(16,pmd_mode::decline);
BOOST_CHECK_EQUAL(v.ec,pmde::make_error_code(pmde::invalid_max_window_bits));
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_decline ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "8";
v.ec = v.exts.set_s2c_max_window_bits(15,pmd_mode::decline);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_accept ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "8";
v.ec = v.exts.set_s2c_max_window_bits(15,pmd_mode::accept);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_max_window_bits=8");
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_largest ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "8";
v.ec = v.exts.set_s2c_max_window_bits(15,pmd_mode::largest);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_max_window_bits=8");
}
BOOST_AUTO_TEST_CASE( negotiate_s2c_max_window_bits_smallest ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "8";
v.ec = v.exts.set_s2c_max_window_bits(15,pmd_mode::smallest);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_max_window_bits=8");
}
// Negotiate s2c_max_window_bits
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_invalid ) {
ext_vars v;
std::vector<std::string> values;
values.push_back("foo");
values.push_back("7");
values.push_back("16");
std::vector<std::string>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
v.attr["c2s_max_window_bits"] = *it;
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( !v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, pmde::make_error_code(pmde::invalid_attribute_value) );
BOOST_CHECK_EQUAL( v.esp.second, "");
}
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_valid ) {
ext_vars v;
v.attr["c2s_max_window_bits"] = "";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
v.attr["c2s_max_window_bits"] = "8";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_max_window_bits=8");
v.attr["c2s_max_window_bits"] = "15";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
}
BOOST_AUTO_TEST_CASE( invalid_set_c2s_max_window_bits ) {
ext_vars v;
v.ec = v.exts.set_c2s_max_window_bits(7,pmd_mode::decline);
BOOST_CHECK_EQUAL(v.ec,pmde::make_error_code(pmde::invalid_max_window_bits));
v.ec = v.exts.set_c2s_max_window_bits(16,pmd_mode::decline);
BOOST_CHECK_EQUAL(v.ec,pmde::make_error_code(pmde::invalid_max_window_bits));
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_decline ) {
ext_vars v;
v.attr["c2s_max_window_bits"] = "8";
v.ec = v.exts.set_c2s_max_window_bits(8,pmd_mode::decline);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate");
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_accept ) {
ext_vars v;
v.attr["c2s_max_window_bits"] = "8";
v.ec = v.exts.set_c2s_max_window_bits(15,pmd_mode::accept);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_max_window_bits=8");
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_largest ) {
ext_vars v;
v.attr["c2s_max_window_bits"] = "8";
v.ec = v.exts.set_c2s_max_window_bits(15,pmd_mode::largest);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_max_window_bits=8");
}
BOOST_AUTO_TEST_CASE( negotiate_c2s_max_window_bits_smallest ) {
ext_vars v;
v.attr["c2s_max_window_bits"] = "8";
v.ec = v.exts.set_c2s_max_window_bits(15,pmd_mode::smallest);
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_max_window_bits=8");
}
// Combinations with 2
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated1 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["c2s_no_context_takeover"] = "";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; c2s_no_context_takeover");
}
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated2 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; s2c_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated3 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated4 ) {
ext_vars v;
v.attr["c2s_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_no_context_takeover; s2c_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated5 ) {
ext_vars v;
v.attr["c2s_no_context_takeover"] = "";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_no_context_takeover; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated6 ) {
ext_vars v;
v.attr["s2c_max_window_bits"] = "10";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_max_window_bits=10; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated1 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["c2s_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; c2s_no_context_takeover; s2c_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated2 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["c2s_no_context_takeover"] = "";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; c2s_no_context_takeover; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated3 ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; s2c_max_window_bits=10; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated4 ) {
ext_vars v;
v.attr["c2s_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; c2s_no_context_takeover; s2c_max_window_bits=10; c2s_max_window_bits=10");
}
BOOST_AUTO_TEST_CASE( negotiate_four_client_initiated ) {
ext_vars v;
v.attr["s2c_no_context_takeover"] = "";
v.attr["c2s_no_context_takeover"] = "";
v.attr["s2c_max_window_bits"] = "10";
v.attr["c2s_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( v.esp.second, "permessage-deflate; s2c_no_context_takeover; c2s_no_context_takeover; s2c_max_window_bits=10; c2s_max_window_bits=10");
}
// Compression
/*
BOOST_AUTO_TEST_CASE( compress_data ) {
ext_vars v;
std::string in = "Hello";
std::string out;
std::string in2;
std::string out2;
v.exts.init();
v.ec = v.exts.compress(in,out);
std::cout << "in : " << websocketpp::utility::to_hex(in) << std::endl;
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
std::cout << "out: " << websocketpp::utility::to_hex(out) << std::endl;
in2 = out;
v.ec = v.exts.decompress(reinterpret_cast<const uint8_t *>(in2.data()),in2.size(),out2);
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
std::cout << "out: " << websocketpp::utility::to_hex(out2) << std::endl;
BOOST_CHECK_EQUAL( out, out2 );
}
BOOST_AUTO_TEST_CASE( decompress_data ) {
ext_vars v;
uint8_t in[12] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
std::string out;
v.exts.init();
v.ec = v.exts.decompress(in,12,out);
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
std::cout << "out: " << websocketpp::utility::to_hex(out) << std::endl;
BOOST_CHECK( false );
}
*/

View File

@@ -0,0 +1,23 @@
## http unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework'],env) + [platform_libs]
objs = env.Object('parser_boost.o', ["parser.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_http_boost', ["parser_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('parser_stl.o', ["parser.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_http_stl', ["parser_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

BIN
src/websocketpp/test/http/a.out Executable file

Binary file not shown.

View File

@@ -0,0 +1,968 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE http_parser
#include <boost/test/unit_test.hpp>
#include <string>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
BOOST_AUTO_TEST_CASE( is_token_char ) {
// Valid characters
// misc
BOOST_CHECK( websocketpp::http::is_token_char('!') );
BOOST_CHECK( websocketpp::http::is_token_char('#') );
BOOST_CHECK( websocketpp::http::is_token_char('$') );
BOOST_CHECK( websocketpp::http::is_token_char('%') );
BOOST_CHECK( websocketpp::http::is_token_char('&') );
BOOST_CHECK( websocketpp::http::is_token_char('\'') );
BOOST_CHECK( websocketpp::http::is_token_char('*') );
BOOST_CHECK( websocketpp::http::is_token_char('+') );
BOOST_CHECK( websocketpp::http::is_token_char('-') );
BOOST_CHECK( websocketpp::http::is_token_char('.') );
BOOST_CHECK( websocketpp::http::is_token_char('^') );
BOOST_CHECK( websocketpp::http::is_token_char('_') );
BOOST_CHECK( websocketpp::http::is_token_char('`') );
BOOST_CHECK( websocketpp::http::is_token_char('~') );
// numbers
for (int i = 0x30; i < 0x3a; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// upper
for (int i = 0x41; i < 0x5b; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// lower
for (int i = 0x61; i < 0x7b; i++) {
BOOST_CHECK( websocketpp::http::is_token_char((unsigned char)(i)) );
}
// invalid characters
// lower unprintable
for (int i = 0; i < 33; i++) {
BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
}
// misc
BOOST_CHECK( !websocketpp::http::is_token_char('(') );
BOOST_CHECK( !websocketpp::http::is_token_char(')') );
BOOST_CHECK( !websocketpp::http::is_token_char('<') );
BOOST_CHECK( !websocketpp::http::is_token_char('>') );
BOOST_CHECK( !websocketpp::http::is_token_char('@') );
BOOST_CHECK( !websocketpp::http::is_token_char(',') );
BOOST_CHECK( !websocketpp::http::is_token_char(';') );
BOOST_CHECK( !websocketpp::http::is_token_char(':') );
BOOST_CHECK( !websocketpp::http::is_token_char('\\') );
BOOST_CHECK( !websocketpp::http::is_token_char('"') );
BOOST_CHECK( !websocketpp::http::is_token_char('/') );
BOOST_CHECK( !websocketpp::http::is_token_char('[') );
BOOST_CHECK( !websocketpp::http::is_token_char(']') );
BOOST_CHECK( !websocketpp::http::is_token_char('?') );
BOOST_CHECK( !websocketpp::http::is_token_char('=') );
BOOST_CHECK( !websocketpp::http::is_token_char('{') );
BOOST_CHECK( !websocketpp::http::is_token_char('}') );
// upper unprintable and out of ascii range
for (int i = 127; i < 256; i++) {
BOOST_CHECK( !websocketpp::http::is_token_char((unsigned char)(i)) );
}
// is not
BOOST_CHECK( !websocketpp::http::is_not_token_char('!') );
BOOST_CHECK( websocketpp::http::is_not_token_char('(') );
}
BOOST_AUTO_TEST_CASE( extract_token ) {
std::string d1 = "foo";
std::string d2 = " foo ";
std::pair<std::string,std::string::const_iterator> ret;
ret = websocketpp::http::parser::extract_token(d1.begin(),d1.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d1.begin()+3 );
ret = websocketpp::http::parser::extract_token(d2.begin(),d2.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d2.begin()+0 );
ret = websocketpp::http::parser::extract_token(d2.begin()+1,d2.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d2.begin()+4 );
}
BOOST_AUTO_TEST_CASE( extract_quoted_string ) {
std::string d1 = "\"foo\"";
std::string d2 = "\"foo\\\"bar\\\"baz\"";
std::string d3 = "\"foo\" ";
std::string d4 = "";
std::string d5 = "foo";
std::pair<std::string,std::string::const_iterator> ret;
using websocketpp::http::parser::extract_quoted_string;
ret = extract_quoted_string(d1.begin(),d1.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d1.end() );
ret = extract_quoted_string(d2.begin(),d2.end());
BOOST_CHECK( ret.first == "foo\"bar\"baz" );
BOOST_CHECK( ret.second == d2.end() );
ret = extract_quoted_string(d3.begin(),d3.end());
BOOST_CHECK( ret.first == "foo" );
BOOST_CHECK( ret.second == d3.begin()+5 );
ret = extract_quoted_string(d4.begin(),d4.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d4.begin() );
ret = extract_quoted_string(d5.begin(),d5.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.second == d5.begin() );
}
BOOST_AUTO_TEST_CASE( extract_all_lws ) {
std::string d1 = " foo bar";
d1.append(1,char(9));
d1.append("baz\r\n d\r\n \r\n e\r\nf");
std::string::const_iterator ret;
ret = websocketpp::http::parser::extract_all_lws(d1.begin(),d1.end());
BOOST_CHECK( ret == d1.begin()+1 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+1,d1.end());
BOOST_CHECK( ret == d1.begin()+1 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+4,d1.end());
BOOST_CHECK( ret == d1.begin()+9 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+12,d1.end());
BOOST_CHECK( ret == d1.begin()+13 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+16,d1.end());
BOOST_CHECK( ret == d1.begin()+19 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+20,d1.end());
BOOST_CHECK( ret == d1.begin()+28 );
ret = websocketpp::http::parser::extract_all_lws(d1.begin()+29,d1.end());
BOOST_CHECK( ret == d1.begin()+29 );
}
BOOST_AUTO_TEST_CASE( extract_attributes_blank ) {
std::string s = "";
websocketpp::http::attribute_list a;
std::string::const_iterator it;
it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
BOOST_CHECK( it == s.begin() );
BOOST_CHECK_EQUAL( a.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extract_attributes_simple ) {
std::string s = "foo";
websocketpp::http::attribute_list a;
std::string::const_iterator it;
it = websocketpp::http::parser::extract_attributes(s.begin(),s.end(),a);
BOOST_CHECK( it == s.end() );
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("foo") != a.end() );
BOOST_CHECK_EQUAL( a.find("foo")->second, "" );
}
BOOST_AUTO_TEST_CASE( extract_parameters ) {
std::string s1 = "";
std::string s2 = "foo";
std::string s3 = " foo \r\nAbc";
std::string s4 = " \r\n foo ";
std::string s5 = "foo,bar";
std::string s6 = "foo;bar";
std::string s7 = "foo;baz,bar";
std::string s8 = "foo;bar;baz";
std::string s9 = "foo;bar=baz";
std::string s10 = "foo;bar=baz;boo";
std::string s11 = "foo;bar=baz;boo,bob";
std::string s12 = "foo;bar=\"a b c\"";
std::string s13 = "foo;bar=\"a \\\"b\\\" c\"";
std::string sx = "foo;bar=\"a \\\"b\\\" c\"";
websocketpp::http::parameter_list p;
websocketpp::http::attribute_list a;
std::string::const_iterator it;
using websocketpp::http::parser::extract_parameters;
it = extract_parameters(s1.begin(),s1.end(),p);
BOOST_CHECK( it == s1.begin() );
p.clear();
it = extract_parameters(s2.begin(),s2.end(),p);
BOOST_CHECK( it == s2.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
p.clear();
it = extract_parameters(s3.begin(),s3.end(),p);
BOOST_CHECK( it == s3.begin()+5 );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
p.clear();
it = extract_parameters(s4.begin(),s4.end(),p);
BOOST_CHECK( it == s4.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
p.clear();
it = extract_parameters(s5.begin(),s5.end(),p);
BOOST_CHECK( it == s5.end() );
BOOST_CHECK_EQUAL( p.size(), 2 );
BOOST_CHECK( p[0].first == "foo" );
BOOST_CHECK_EQUAL( p[0].second.size(), 0 );
BOOST_CHECK( p[1].first == "bar" );
BOOST_CHECK_EQUAL( p[1].second.size(), 0 );
p.clear();
it = extract_parameters(s6.begin(),s6.end(),p);
BOOST_CHECK( it == s6.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "" );
p.clear();
it = extract_parameters(s7.begin(),s7.end(),p);
BOOST_CHECK( it == s7.end() );
BOOST_CHECK_EQUAL( p.size(), 2 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("baz") != a.end() );
BOOST_CHECK_EQUAL( a.find("baz")->second, "" );
BOOST_CHECK( p[1].first == "bar" );
a = p[1].second;
BOOST_CHECK_EQUAL( a.size(), 0 );
p.clear();
it = extract_parameters(s8.begin(),s8.end(),p);
BOOST_CHECK( it == s8.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "" );
BOOST_CHECK( a.find("baz") != a.end() );
BOOST_CHECK_EQUAL( a.find("baz")->second, "" );
p.clear();
it = extract_parameters(s9.begin(),s9.end(),p);
BOOST_CHECK( it == s9.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
p.clear();
it = extract_parameters(s10.begin(),s10.end(),p);
BOOST_CHECK( it == s10.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
BOOST_CHECK( a.find("boo") != a.end() );
BOOST_CHECK_EQUAL( a.find("boo")->second, "" );
p.clear();
it = extract_parameters(s11.begin(),s11.end(),p);
BOOST_CHECK( it == s11.end() );
BOOST_CHECK_EQUAL( p.size(), 2 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 2 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "baz" );
BOOST_CHECK( a.find("boo") != a.end() );
BOOST_CHECK_EQUAL( a.find("boo")->second, "" );
a = p[1].second;
BOOST_CHECK_EQUAL( a.size(), 0 );
p.clear();
it = extract_parameters(s12.begin(),s12.end(),p);
BOOST_CHECK( it == s12.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "a b c" );
p.clear();
it = extract_parameters(s13.begin(),s13.end(),p);
BOOST_CHECK( it == s13.end() );
BOOST_CHECK_EQUAL( p.size(), 1 );
BOOST_CHECK( p[0].first == "foo" );
a = p[0].second;
BOOST_CHECK_EQUAL( a.size(), 1 );
BOOST_CHECK( a.find("bar") != a.end() );
BOOST_CHECK_EQUAL( a.find("bar")->second, "a \"b\" c" );
}
BOOST_AUTO_TEST_CASE( strip_lws ) {
std::string test1 = "foo";
std::string test2 = " foo ";
std::string test3 = "foo ";
std::string test4 = " foo";
std::string test5 = " foo ";
std::string test6 = " \r\n foo ";
std::string test7 = " \t foo ";
std::string test8 = " \t ";
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test1), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test2), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test3), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test4), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test5), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test6), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test7), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test8), "" );
}
BOOST_AUTO_TEST_CASE( case_insensitive_headers ) {
websocketpp::http::parser::parser r;
r.replace_header("foo","bar");
BOOST_CHECK_EQUAL( r.get_header("foo"), "bar" );
BOOST_CHECK_EQUAL( r.get_header("FOO"), "bar" );
BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar" );
}
BOOST_AUTO_TEST_CASE( case_insensitive_headers_overwrite ) {
websocketpp::http::parser::parser r;
r.replace_header("foo","bar");
BOOST_CHECK_EQUAL( r.get_header("foo"), "bar" );
BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar" );
r.replace_header("Foo","baz");
BOOST_CHECK_EQUAL( r.get_header("foo"), "baz" );
BOOST_CHECK_EQUAL( r.get_header("Foo"), "baz" );
r.remove_header("FoO");
BOOST_CHECK_EQUAL( r.get_header("foo"), "" );
BOOST_CHECK_EQUAL( r.get_header("Foo"), "" );
}
BOOST_AUTO_TEST_CASE( blank_consume ) {
websocketpp::http::parser::request r;
std::string raw = "";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( blank_request ) {
websocketpp::http::parser::request r;
std::string raw = "\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( bad_request_no_host ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
BOOST_CHECK( r.ready() == false );
}
BOOST_AUTO_TEST_CASE( basic_request ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( trailing_body_characters ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( basic_split1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\n";
std::string raw2 = "Host: www.example.com\r\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( basic_split2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r";
std::string raw2 = "\n\r\na";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "www.example.com" );
}
BOOST_AUTO_TEST_CASE( max_header_len ) {
websocketpp::http::parser::request r;
std::string raw(websocketpp::http::max_header_size+1,'*');
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (const websocketpp::http::exception& e) {
if (e.m_error_code == websocketpp::http::status_code::request_header_fields_too_large) {
exception = true;
}
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( max_header_len_split ) {
websocketpp::http::parser::request r;
std::string raw(websocketpp::http::max_header_size-1,'*');
std::string raw2(2,'*');
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
pos += r.consume(raw2.c_str(),raw2.size());
} catch (const websocketpp::http::exception& e) {
if (e.m_error_code == websocketpp::http::status_code::request_header_fields_too_large) {
exception = true;
}
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( firefox_full_request ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK( pos == 482 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK( r.get_version() == "HTTP/1.1" );
BOOST_CHECK( r.get_method() == "GET" );
BOOST_CHECK( r.get_uri() == "/" );
BOOST_CHECK( r.get_header("Host") == "localhost:5000" );
BOOST_CHECK( r.get_header("User-Agent") == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0" );
BOOST_CHECK( r.get_header("Accept") == "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" );
BOOST_CHECK( r.get_header("Accept-Language") == "en-us,en;q=0.5" );
BOOST_CHECK( r.get_header("Accept-Encoding") == "gzip, deflate" );
BOOST_CHECK( r.get_header("Connection") == "keep-alive, Upgrade" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Version") == "8" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Origin") == "http://zaphoyd.com" );
BOOST_CHECK( r.get_header("Sec-WebSocket-Key") == "pFik//FxwFk0riN4ZiPFjQ==" );
BOOST_CHECK( r.get_header("Pragma") == "no-cache" );
BOOST_CHECK( r.get_header("Cache-Control") == "no-cache" );
BOOST_CHECK( r.get_header("Upgrade") == "websocket" );
}
BOOST_AUTO_TEST_CASE( bad_method ) {
websocketpp::http::parser::request r;
std::string raw = "GE]T / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( bad_header_name ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHo]st: www.example.com\r\n\r\n";
bool exception = false;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.0\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 41 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.0" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
}
BOOST_AUTO_TEST_CASE( new_http_version1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 42 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.12" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
}
BOOST_AUTO_TEST_CASE( new_http_version2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/12.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 43 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/12.12" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
}
/* commented out due to not being implemented yet
BOOST_AUTO_TEST_CASE( new_http_version3 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTPS/12.12\r\nHost: www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == true );
}*/
BOOST_AUTO_TEST_CASE( header_whitespace1 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com \r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 43 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
}
BOOST_AUTO_TEST_CASE( header_whitespace2 ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost:www.example.com\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 40 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Host"), "www.example.com" );
}
BOOST_AUTO_TEST_CASE( header_aggregation ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\nFoo: bar\r\nFoo: bat\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos = r.consume(raw.c_str(),raw.size());
} catch (...) {
exception = true;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 61 );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_method(), "GET" );
BOOST_CHECK_EQUAL( r.get_uri(), "/" );
BOOST_CHECK_EQUAL( r.get_header("Foo"), "bar, bat" );
}
BOOST_AUTO_TEST_CASE( wikipedia_example_response ) {
websocketpp::http::parser::response r;
std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 159 );
BOOST_CHECK( r.headers_ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_status_code(), websocketpp::http::status_code::switching_protocols );
BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
}
BOOST_AUTO_TEST_CASE( response_with_non_standard_lws ) {
websocketpp::http::parser::response r;
std::string raw = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept:HSmrc0sMlYUkAGmm5OPpG2HaGWk=\r\nSec-WebSocket-Protocol: chat\r\n\r\n";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 158 );
BOOST_CHECK( r.headers_ready() );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_status_code(), websocketpp::http::status_code::switching_protocols );
BOOST_CHECK_EQUAL( r.get_status_msg(), "Switching Protocols" );
BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket" );
BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade" );
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Accept"), "HSmrc0sMlYUkAGmm5OPpG2HaGWk=" );
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "chat" );
}
BOOST_AUTO_TEST_CASE( plain_http_response ) {
websocketpp::http::parser::response r;
std::string raw = "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(raw.c_str(),raw.size());
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK( exception == false );
BOOST_CHECK_EQUAL( pos, 405 );
BOOST_CHECK( r.headers_ready() == true );
BOOST_CHECK( r.ready() == true );
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1" );
BOOST_CHECK_EQUAL( r.get_status_code(), websocketpp::http::status_code::ok );
BOOST_CHECK_EQUAL( r.get_status_msg(), "OK" );
BOOST_CHECK_EQUAL( r.get_header("Date"), "Thu, 10 May 2012 11:59:25 GMT" );
BOOST_CHECK_EQUAL( r.get_header("Server"), "Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch" );
BOOST_CHECK_EQUAL( r.get_header("Last-Modified"), "Tue, 30 Mar 2010 17:41:28 GMT" );
BOOST_CHECK_EQUAL( r.get_header("ETag"), "\"16799d-55-4830823a78200\"" );
BOOST_CHECK_EQUAL( r.get_header("Accept-Ranges"), "bytes" );
BOOST_CHECK_EQUAL( r.get_header("Content-Length"), "85" );
BOOST_CHECK_EQUAL( r.get_header("Vary"), "Accept-Encoding" );
BOOST_CHECK_EQUAL( r.get_header("Content-Type"), "text/html" );
BOOST_CHECK_EQUAL( r.get_body(), "<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>" );
}
BOOST_AUTO_TEST_CASE( parse_istream ) {
websocketpp::http::parser::response r;
std::stringstream s;
s << "HTTP/1.1 200 OK\r\nDate: Thu, 10 May 2012 11:59:25 GMT\r\nServer: Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 PHP/5.3.8 with Suhosin-Patch\r\nLast-Modified: Tue, 30 Mar 2010 17:41:28 GMT\r\nETag: \"16799d-55-4830823a78200\"\r\nAccept-Ranges: bytes\r\nContent-Length: 85\r\nVary: Accept-Encoding\r\nContent-Type: text/html\r\n\r\n<!doctype html>\n<html>\n<head>\n<title>Thor</title>\n</head>\n<body> \n<p>Thor</p>\n</body>";
bool exception = false;
size_t pos = 0;
try {
pos += r.consume(s);
} catch (std::exception &e) {
exception = true;
std::cout << e.what() << std::endl;
}
BOOST_CHECK_EQUAL( exception, false );
BOOST_CHECK_EQUAL( pos, 405 );
BOOST_CHECK_EQUAL( r.headers_ready(), true );
BOOST_CHECK_EQUAL( r.ready(), true );
}
BOOST_AUTO_TEST_CASE( write_request_basic ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\n\r\n";
r.set_version("HTTP/1.1");
r.set_method("GET");
r.set_uri("/");
BOOST_CHECK_EQUAL( r.raw(), raw );
}
BOOST_AUTO_TEST_CASE( write_request_with_header ) {
websocketpp::http::parser::request r;
std::string raw = "GET / HTTP/1.1\r\nHost: http://example.com\r\n\r\n";
r.set_version("HTTP/1.1");
r.set_method("GET");
r.set_uri("/");
r.replace_header("Host","http://example.com");
BOOST_CHECK_EQUAL( r.raw(), raw );
}
BOOST_AUTO_TEST_CASE( write_request_with_body ) {
websocketpp::http::parser::request r;
std::string raw = "POST / HTTP/1.1\r\nContent-Length: 48\r\nContent-Type: application/x-www-form-urlencoded\r\nHost: http://example.com\r\n\r\nlicenseID=string&content=string&paramsXML=string";
r.set_version("HTTP/1.1");
r.set_method("POST");
r.set_uri("/");
r.replace_header("Host","http://example.com");
r.replace_header("Content-Type","application/x-www-form-urlencoded");
r.set_body("licenseID=string&content=string&paramsXML=string");
BOOST_CHECK_EQUAL( r.raw(), raw );
}

View File

@@ -0,0 +1,141 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <websocketpp/http/parser.hpp>
#include <chrono>
class scoped_timer {
public:
scoped_timer(std::string i) : m_id(i),m_start(std::chrono::steady_clock::now()) {
std::cout << "Clock " << i << ": ";
}
~scoped_timer() {
std::chrono::nanoseconds time_taken = std::chrono::steady_clock::now()-m_start;
//nanoseconds_per_test
//tests_per_second
//1000000000.0/(double(time_taken.count())/1000.0)
std::cout << 1000000000.0/(double(time_taken.count())/1000.0) << std::endl;
//std::cout << (1.0/double(time_taken.count())) * double(1000000000*1000) << std::endl;
}
private:
std::string m_id;
std::chrono::steady_clock::time_point m_start;
};
int main() {
std::string raw = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
std::string firefox = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\nAccept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
std::string firefox1 = "GET / HTTP/1.1\r\nHost: localhost:5000\r\nUser-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0) Gecko/20100101 Firefox/10.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: en-us,en;q=0.5\r\n";
std::string firefox2 = "Accept-Encoding: gzip, deflate\r\nConnection: keep-alive, Upgrade\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Origin: http://zaphoyd.com\r\nSec-WebSocket-Key: pFik//FxwFk0riN4ZiPFjQ==\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n";
{
scoped_timer timer("Simplest 1 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(raw.c_str(),raw.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 1 chop, consume old");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume2(firefox.c_str(),firefox.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 1 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(firefox.c_str(),firefox.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
{
scoped_timer timer("FireFox, 2 chop");
for (int i = 0; i < 1000; i++) {
websocketpp::http::parser::request r;
try {
r.consume(firefox1.c_str(),firefox1.size());
r.consume(firefox2.c_str(),firefox2.size());
} catch (...) {
std::cout << "exception" << std::endl;
}
if (!r.ready()) {
std::cout << "error" << std::endl;
break;
}
}
}
return 0;
}

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,23 @@
## logger unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs]
objs = env.Object('logger_basic_boost.o', ["basic.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('logger_basic_boost', ["logger_basic_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('logger_basic_stl.o', ["basic.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('logger_basic_stl', ["logger_basic_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE basic_log
#include <boost/test/unit_test.hpp>
#include <string>
#include <websocketpp/logger/basic.hpp>
#include <websocketpp/concurrency/none.hpp>
#include <websocketpp/concurrency/basic.hpp>
BOOST_AUTO_TEST_CASE( is_token_char ) {
typedef websocketpp::log::basic<websocketpp::concurrency::none,websocketpp::log::elevel> error_log;
error_log elog;
BOOST_CHECK( elog.static_test(websocketpp::log::elevel::info ) == true );
BOOST_CHECK( elog.static_test(websocketpp::log::elevel::warn ) == true );
BOOST_CHECK( elog.static_test(websocketpp::log::elevel::rerror ) == true );
BOOST_CHECK( elog.static_test(websocketpp::log::elevel::fatal ) == true );
elog.set_channels(websocketpp::log::elevel::info);
elog.write(websocketpp::log::elevel::info,"Information");
elog.write(websocketpp::log::elevel::warn,"A warning");
elog.write(websocketpp::log::elevel::rerror,"A error");
elog.write(websocketpp::log::elevel::fatal,"A critical error");
}
BOOST_AUTO_TEST_CASE( access_clear ) {
typedef websocketpp::log::basic<websocketpp::concurrency::none,websocketpp::log::alevel> access_log;
std::stringstream out;
access_log logger(0xffffffff,&out);
// clear all channels
logger.clear_channels(0xffffffff);
// writes shouldn't happen
logger.write(websocketpp::log::alevel::devel,"devel");
//std::cout << "|" << out.str() << "|" << std::endl;
BOOST_CHECK( out.str().size() == 0 );
}
BOOST_AUTO_TEST_CASE( basic_concurrency ) {
typedef websocketpp::log::basic<websocketpp::concurrency::basic,websocketpp::log::alevel> access_log;
std::stringstream out;
access_log logger(0xffffffff,&out);
logger.set_channels(0xffffffff);
logger.write(websocketpp::log::alevel::devel,"devel");
//std::cout << "|" << out.str() << "|" << std::endl;
BOOST_CHECK( out.str().size() > 0 );
}

View File

@@ -0,0 +1,27 @@
## message_buffer unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs]
objs = env.Object('message_boost.o', ["message.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('alloc_boost.o', ["alloc.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_message_boost', ["message_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_alloc_boost', ["alloc_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('message_stl.o', ["message.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('alloc_stl.o', ["alloc.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_message_stl', ["message_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_alloc_stl', ["alloc_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,96 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE message_buffer_alloc
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/message_buffer/alloc.hpp>
template <template <class> class con_msg_manager>
struct stub {
typedef websocketpp::lib::shared_ptr<stub> ptr;
typedef con_msg_manager<stub> con_msg_man_type;
typedef typename con_msg_man_type::ptr con_msg_man_ptr;
typedef typename con_msg_man_type::weak_ptr con_msg_man_weak_ptr;
stub(con_msg_man_ptr manager, websocketpp::frame::opcode::value op, size_t size = 128)
: m_opcode(op)
, m_manager(manager)
, m_size(size) {}
bool recycle() {
con_msg_man_ptr shared = m_manager.lock();
if (shared) {
return shared->recycle(this);
} else {
return false;
}
}
websocketpp::frame::opcode::value m_opcode;
con_msg_man_weak_ptr m_manager;
size_t m_size;
};
BOOST_AUTO_TEST_CASE( basic_get_message ) {
typedef stub<websocketpp::message_buffer::alloc::con_msg_manager>
message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_man_type;
con_msg_man_type::ptr manager(new con_msg_man_type());
message_type::ptr msg = manager->get_message(websocketpp::frame::opcode::TEXT,512);
BOOST_CHECK(msg);
BOOST_CHECK(msg->m_opcode == websocketpp::frame::opcode::TEXT);
BOOST_CHECK(msg->m_manager.lock() == manager);
BOOST_CHECK(msg->m_size == 512);
}
BOOST_AUTO_TEST_CASE( basic_get_manager ) {
typedef stub<websocketpp::message_buffer::alloc::con_msg_manager>
message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_man_type;
typedef websocketpp::message_buffer::alloc::endpoint_msg_manager
<con_msg_man_type> endpoint_manager_type;
endpoint_manager_type em;
con_msg_man_type::ptr manager = em.get_manager();
message_type::ptr msg = manager->get_message(websocketpp::frame::opcode::TEXT,512);
BOOST_CHECK(msg);
BOOST_CHECK(msg->m_opcode == websocketpp::frame::opcode::TEXT);
BOOST_CHECK(msg->m_manager.lock() == manager);
BOOST_CHECK(msg->m_size == 512);
}

View File

@@ -0,0 +1,72 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE message
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/message_buffer/message.hpp>
template <typename message>
struct stub {
typedef websocketpp::lib::weak_ptr<stub> weak_ptr;
typedef websocketpp::lib::shared_ptr<stub> ptr;
stub() : recycled(false) {}
bool recycle(message *) {
this->recycled = true;
return false;
}
bool recycled;
};
BOOST_AUTO_TEST_CASE( basic_size_check ) {
typedef websocketpp::message_buffer::message<stub> message_type;
typedef stub<message_type> stub_type;
stub_type::ptr s(new stub_type());
message_type::ptr msg(new message_type(s,websocketpp::frame::opcode::TEXT,500));
BOOST_CHECK(msg->get_payload().capacity() >= 500);
}
BOOST_AUTO_TEST_CASE( recycle ) {
typedef websocketpp::message_buffer::message<stub> message_type;
typedef stub<message_type> stub_type;
stub_type::ptr s(new stub_type());
message_type::ptr msg(new message_type(s,websocketpp::frame::opcode::TEXT,500));
BOOST_CHECK(s->recycled == false);
BOOST_CHECK(msg->recycle() == false);
BOOST_CHECK(s->recycled == true);
}

View File

@@ -0,0 +1,156 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_00_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi00.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
BOOST_AUTO_TEST_CASE( exact_match ) {
websocketpp::http::parser::request r;
websocketpp::http::parser::response response;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nOrigin: http://example.com\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(p.validate_handshake(r));
websocketpp::uri_ptr u;
bool exception;
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == false);
BOOST_CHECK(u->get_secure() == false);
BOOST_CHECK(u->get_host() == "www.example.com");
BOOST_CHECK(u->get_resource() == "/");
BOOST_CHECK(u->get_port() == websocketpp::URI_DEFAULT_PORT);
p.process_handshake(r,response);
BOOST_CHECK(response.get_header("Connection") == "Upgrade");
BOOST_CHECK(response.get_header("Upgrade") == "websocket");
BOOST_CHECK(response.get_header("Sec-WebSocket-Origin") == "http://example.com");
BOOST_CHECK(response.get_header("Sec-WebSocket-Location") == "ws://www.example.com/");
BOOST_CHECK(response.get_header("Sec-WebSocket-Key3") == "n`9eBk9z$R8pOtVb");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
}
BOOST_AUTO_TEST_CASE( bad_host ) {
websocketpp::http::parser::request r;
websocketpp::processor::hybi00<websocketpp::http::parser::request,websocketpp::http::parser::response> p(false);
websocketpp::uri_ptr u;
bool exception = false;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
r.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
BOOST_CHECK(!p.validate_handshake(r));
try {
u = p.get_uri(r);
} catch (const websocketpp::uri_exception& e) {
exception = true;
}
BOOST_CHECK(exception == true);
}

View File

@@ -0,0 +1,47 @@
## processor unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system'],env) + [platform_libs] + ['z']
objs = env.Object('test_processor_boost.o', ["processor.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi13_boost.o', ["hybi13.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi08_boost.o', ["hybi08.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi07_boost.o', ["hybi07.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_hybi00_boost.o', ["hybi00.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('test_extension_permessage_compress_boost.o', ["extension_permessage_compress.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_processor_boost', ["test_processor_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi13_boost', ["test_hybi13_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi08_boost', ["test_hybi08_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi07_boost', ["test_hybi07_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_hybi00_boost', ["test_hybi00_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_extension_permessage_compress_boost', ["test_extension_permessage_compress_boost.o"], LIBS = BOOST_LIBS + ['z'])
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs] + ['z']
# no C++11 features are used in processor so there are no C++11 versions of
# these tests.
objs += env_cpp11.Object('test_processor_stl.o', ["processor.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi13_stl.o', ["hybi13.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi08_stl.o', ["hybi08.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi07_stl.o', ["hybi07.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_hybi00_stl.o', ["hybi00.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('test_extension_permessage_compress_stl.o', ["extension_permessage_compress.cpp"], LIBS = BOOST_LIBS_CPP11 + ['z'])
prgs += env_cpp11.Program('test_processor_stl', ["test_processor_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi13_stl', ["test_hybi13_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi08_stl', ["test_hybi08_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi07_stl', ["test_hybi07_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_hybi00_stl', ["test_hybi00_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_extension_permessage_compress_stl', ["test_extension_permessage_compress_stl.o"], LIBS = BOOST_LIBS_CPP11 + ['z'])
Return('prgs')

View File

@@ -0,0 +1,198 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE extension_permessage_deflate
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct config {
typedef websocketpp::http::parser::request request_type;
};
typedef websocketpp::extensions::permessage_deflate::enabled<config>
compressor_type;
using namespace websocketpp;
BOOST_AUTO_TEST_CASE( deflate_init ) {
/*compressor_type compressor;
websocketpp::http::parser::attribute_list attributes;
std::pair<lib::error_code,std::string> neg_ret;
neg_ret = compressor.negotiate(attributes);
BOOST_CHECK_EQUAL( neg_ret.first,
extensions::permessage_deflate::error::invalid_parameters );*/
/**
* Window size is primarily controlled by the writer. A stream can only be
* read by a window size equal to or greater than the one use to compress
* it initially. The default windows size is also the maximum window size.
* Thus:
*
* Outbound window size can be limited unilaterally under the assumption
* that the opposite end will be using the default (maximum size which can
* read anything)
*
* Inbound window size must be limited by asking the remote endpoint to
* do so and it agreeing.
*
* Context takeover is also primarily controlled by the writer. If the
* compressor does not clear its context between messages then the reader
* can't either.
*
* Outbound messages may clear context between messages unilaterally.
* Inbound messages must retain state unless the remote endpoint signals
* otherwise.
*
* Negotiation options:
* Client must choose from the following options:
* - whether or not to request an inbound window limit
* - whether or not to signal that it will honor an outbound window limit
* - whether or not to request that the server disallow context takeover
*
* Server must answer in the following ways
* - If client requested a window size limit, is the window size limit
* acceptable?
* - If client allows window limit requests, should we send one?
* - If client requested no context takeover, should we accept?
*
*
*
* All Defaults
* Req: permessage-compress; method=deflate
* Ans: permessage-compress; method=deflate
*
* # Client wants to limit the size of inbound windows from server
* permessage-compress; method="deflate; s2c_max_window_bits=8, deflate"
* Ans: permessage-compress; method="deflate; s2c_max_window_bits=8"
* OR
* Ans: permessage-compress; method=deflate
*
* # Server wants to limit the size of inbound windows from client
* Client:
* permessage-compress; method="deflate; c2s_max_window_bits, deflate"
*
* Server:
* permessage-compress; method="deflate; c2s_max_window_bits=8"
*
* # Client wants to
*
*
*
*
*
*
*/
/* processor::extensions::deflate_method d(true);
http::parser::attribute_list attributes;
lib::error_code ec;
attributes.push_back(http::parser::attribute("foo","bar"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::unknown_method_parameter);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","bar"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","7"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","16"));
ec = d.init(attributes);
BOOST_CHECK(ec == processor::extensions::error::invalid_algorithm_settings);
attributes.clear();
attributes.push_back(http::parser::attribute("s2c_max_window_bits","9"));
ec = d.init(attributes);
BOOST_CHECK( !ec);
attributes.clear();
ec = d.init(attributes);
BOOST_CHECK( !ec);
processor::extensions::deflate_engine de;
unsigned char test_in[] = "HelloHelloHelloHello";
unsigned char test_out[30];
uLongf test_out_size = 30;
int ret;
ret = compress(test_out, &test_out_size, test_in, 20);
std::cout << ret << std::endl
<< websocketpp::utility::to_hex(test_in,20) << std::endl
<< websocketpp::utility::to_hex(test_out,test_out_size) << std::endl;
std::string input = "Hello";
std::string output = "";
ec = de.compress(input,output);
BOOST_CHECK( ec == processor::extensions::error::uninitialized );
//std::cout << ec.message() << websocketpp::utility::to_hex(output) << std::endl;
ec = de.init(15,15,Z_DEFAULT_COMPRESSION,8,Z_FIXED);
//ec = de.init();
BOOST_CHECK( !ec );
ec = de.compress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
output = "";
ec = de.compress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
input = output;
output = "";
ec = de.decompress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
*/
}

View File

@@ -0,0 +1,274 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_00_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi00.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
static const size_t max_message_size = 16000000;
};
struct processor_setup {
processor_setup(bool server)
: msg_manager(new stub_config::con_msg_manager_type())
, p(false,server,msg_manager) {}
websocketpp::lib::error_code ec;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi00<stub_config> p;
};
typedef stub_config::message_type::ptr message_ptr;
BOOST_AUTO_TEST_CASE( exact_match ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nOrigin: http://example.com\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","WjN}|M(6");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
env.ec = env.p.validate_handshake(env.req);
BOOST_CHECK(!env.ec);
websocketpp::uri_ptr u;
BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
BOOST_CHECK_EQUAL(u->get_secure(), false);
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
env.p.process_handshake(env.req,"",env.res);
BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "Upgrade");
BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "WebSocket");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Origin"), "http://example.com");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Location"), "ws://www.example.com/");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Key3"), "n`9eBk9z$R8pOtVb");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
processor_setup env(true);
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
processor_setup env(true);
websocketpp::uri_ptr u;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nOrigin: http://example.com\r\nSec-WebSocket-Key1: 3e6b263 4 17 80\r\nSec-WebSocket-Key2: 17 9 G`ZD9 2 2b 7X 3 /r90\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
env.req.replace_header("Sec-WebSocket-Key3","janelle!");
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK( !env.p.validate_handshake(env.req) );
BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols ) {
processor_setup env(true);
std::vector<std::string> subps;
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( prepare_data_frame_null ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
message_ptr invalid;
// empty pointers arguements should return sane error
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
// test valid opcodes
// text (1) should be the only valid opcode
for (int i = 0; i < 0xF; i++) {
in->set_opcode(websocketpp::frame::opcode::value(i));
env.ec = env.p.prepare_data_frame(in,out);
if (i != 1) {
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
} else {
BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
}
}
/*
* TODO: tests for invalid UTF8
char buf[2] = {0x00, 0x00};
in->set_opcode(websocketpp::frame::opcode::text);
in->set_payload("foo");
env.ec = env.p.prepare_data_frame(in,out);
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_payload );
*/
}
BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
in->set_opcode(websocketpp::frame::opcode::text);
in->set_payload("foo");
env.ec = env.p.prepare_data_frame(in,out);
unsigned char raw_header[1] = {0x00};
unsigned char raw_payload[4] = {0x66,0x6f,0x6f,0xff};
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( out->get_header(), std::string(reinterpret_cast<char*>(raw_header),1) );
BOOST_CHECK_EQUAL( out->get_payload(), std::string(reinterpret_cast<char*>(raw_payload),4) );
}
BOOST_AUTO_TEST_CASE( empty_consume ) {
uint8_t frame[2] = {0x00,0x00};
processor_setup env(true);
size_t ret = env.p.consume(frame,0,env.ec);
BOOST_CHECK_EQUAL( ret, 0);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( empty_frame ) {
uint8_t frame[2] = {0x00, 0xff};
processor_setup env(true);
size_t ret = env.p.consume(frame,2,env.ec);
BOOST_CHECK_EQUAL( ret, 2);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "" );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( short_frame ) {
uint8_t frame[5] = {0x00, 0x66, 0x6f, 0x6f, 0xff};
processor_setup env(true);
size_t ret = env.p.consume(frame,5,env.ec);
BOOST_CHECK_EQUAL( ret, 5);
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "foo" );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}

View File

@@ -0,0 +1,193 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_07_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi07.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/random/none.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
static const size_t max_message_size = 16000000;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_compress_config
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK(!ec);
websocketpp::uri_ptr u;
u = p.get_uri(r);
BOOST_CHECK(u->get_valid());
BOOST_CHECK(!u->get_secure());
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
p.process_handshake(r,"",response);
BOOST_CHECK_EQUAL(response.get_header("Connection"), "upgrade");
BOOST_CHECK_EQUAL(response.get_header("Upgrade"), "websocket");
BOOST_CHECK_EQUAL(response.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi07<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( !ec );
BOOST_CHECK( !p.get_uri(r)->get_valid() );
}

View File

@@ -0,0 +1,197 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_08_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi08.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/random/none.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
static const size_t max_message_size = 16000000;
/// Extension related config
static const bool enable_extensions = false;
/// Extension specific config
/// permessage_deflate_config
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK(!ec);
websocketpp::uri_ptr u;
u = p.get_uri(r);
BOOST_CHECK(u->get_valid() == true);
BOOST_CHECK(u->get_secure() == false);
BOOST_CHECK(u->get_host() == "www.example.com");
BOOST_CHECK(u->get_resource() == "/");
BOOST_CHECK(u->get_port() == websocketpp::uri_default_port);
p.process_handshake(r,"",response);
BOOST_CHECK(response.get_header("Connection") == "upgrade");
BOOST_CHECK(response.get_header("Upgrade") == "websocket");
BOOST_CHECK(response.get_header("Sec-WebSocket-Accept") == "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::rng_type rng;
stub_config::con_msg_manager_type::ptr msg_manager;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( ec == websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
stub_config::request_type r;
stub_config::response_type response;
stub_config::con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
websocketpp::processor::hybi08<stub_config> p(false,true,msg_manager,rng);
websocketpp::uri_ptr u;
websocketpp::lib::error_code ec;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\nSec-WebSocket-Key: foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == p.get_version());
ec = p.validate_handshake(r);
BOOST_CHECK( !ec );
u = p.get_uri(r);
BOOST_CHECK( !u->get_valid() );
}

View File

@@ -0,0 +1,693 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_13_processor
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/hybi13.hpp>
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
#include <websocketpp/message_buffer/message.hpp>
#include <websocketpp/message_buffer/alloc.hpp>
#include <websocketpp/random/none.hpp>
#include <websocketpp/extensions/permessage_deflate/disabled.hpp>
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct stub_config {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
struct permessage_deflate_config {
typedef stub_config::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::disabled
<permessage_deflate_config> permessage_deflate_type;
static const size_t max_message_size = 16000000;
static const bool enable_extensions = false;
};
struct stub_config_ext {
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::message_buffer::message
<websocketpp::message_buffer::alloc::con_msg_manager> message_type;
typedef websocketpp::message_buffer::alloc::con_msg_manager<message_type>
con_msg_manager_type;
typedef websocketpp::random::none::int_generator<uint32_t> rng_type;
struct permessage_deflate_config {
typedef stub_config_ext::request_type request_type;
};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
static const size_t max_message_size = 16000000;
static const bool enable_extensions = true;
};
typedef stub_config::con_msg_manager_type con_msg_manager_type;
typedef stub_config::message_type::ptr message_ptr;
// Set up a structure that constructs new copies of all of the support structure
// for using connection processors
struct processor_setup {
processor_setup(bool server)
: msg_manager(new con_msg_manager_type())
, p(false,server,msg_manager,rng) {}
websocketpp::lib::error_code ec;
con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi13<stub_config> p;
};
struct processor_setup_ext {
processor_setup_ext(bool server)
: msg_manager(new con_msg_manager_type())
, p(false,server,msg_manager,rng) {}
websocketpp::lib::error_code ec;
con_msg_manager_type::ptr msg_manager;
stub_config::rng_type rng;
stub_config::request_type req;
stub_config::response_type res;
websocketpp::processor::hybi13<stub_config_ext> p;
};
BOOST_AUTO_TEST_CASE( exact_match ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK(!env.p.validate_handshake(env.req));
websocketpp::uri_ptr u;
BOOST_CHECK_NO_THROW( u = env.p.get_uri(env.req) );
BOOST_CHECK_EQUAL(u->get_secure(), false);
BOOST_CHECK_EQUAL(u->get_host(), "www.example.com");
BOOST_CHECK_EQUAL(u->get_resource(), "/");
BOOST_CHECK_EQUAL(u->get_port(), websocketpp::uri_default_port);
env.p.process_handshake(env.req,"",env.res);
BOOST_CHECK_EQUAL(env.res.get_header("Connection"), "upgrade");
BOOST_CHECK_EQUAL(env.res.get_header("Upgrade"), "websocket");
BOOST_CHECK_EQUAL(env.res.get_header("Sec-WebSocket-Accept"), "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=");
}
BOOST_AUTO_TEST_CASE( non_get_method ) {
processor_setup env(true);
std::string handshake = "POST / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK( env.p.validate_handshake(env.req) == websocketpp::processor::error::invalid_http_method );
}
BOOST_AUTO_TEST_CASE( old_http_version ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(env.req));
BOOST_CHECK_EQUAL(websocketpp::processor::get_websocket_version(env.req), env.p.get_version());
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::invalid_http_version );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key1 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( missing_handshake_key2 ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK_EQUAL( env.p.validate_handshake(env.req), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( bad_host ) {
processor_setup env(true);
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com:70000\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: foo\r\n\r\n";
env.req.consume(handshake.c_str(),handshake.size());
BOOST_CHECK( websocketpp::processor::is_websocket_handshake(env.req) );
BOOST_CHECK_EQUAL( websocketpp::processor::get_websocket_version(env.req), env.p.get_version() );
BOOST_CHECK( !env.p.validate_handshake(env.req) );
BOOST_CHECK( !env.p.get_uri(env.req)->get_valid() );
}
// FRAME TESTS TO DO
//
// unmasked, 0 length, binary
// 0x82 0x00
//
// masked, 0 length, binary
// 0x82 0x80
//
// unmasked, 0 length, text
// 0x81 0x00
//
// masked, 0 length, text
// 0x81 0x80
BOOST_AUTO_TEST_CASE( frame_empty_binary_unmasked ) {
uint8_t frame[2] = {0x82, 0x00};
// all in one chunk
processor_setup env1(false);
size_t ret1 = env1.p.consume(frame,2,env1.ec);
BOOST_CHECK_EQUAL( ret1, 2 );
BOOST_CHECK( !env1.ec );
BOOST_CHECK_EQUAL( env1.p.ready(), true );
// two separate chunks
processor_setup env2(false);
BOOST_CHECK_EQUAL( env2.p.consume(frame,1,env2.ec), 1 );
BOOST_CHECK( !env2.ec );
BOOST_CHECK_EQUAL( env2.p.ready(), false );
BOOST_CHECK_EQUAL( env2.p.consume(frame+1,1,env2.ec), 1 );
BOOST_CHECK( !env2.ec );
BOOST_CHECK_EQUAL( env2.p.ready(), true );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[4] = {0x82, 0x02, 0x2A, 0x2A};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,4,env.ec), 4 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( frame_extended_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
frame[0] = 0x82;
frame[1] = 0x7E;
frame[2] = 0x00;
frame[3] = 0x7E;
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
}
BOOST_AUTO_TEST_CASE( frame_jumbo_binary_unmasked ) {
processor_setup env(false);
uint8_t frame[130] = {0x82, 0x7E, 0x00, 0x7E};
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,130,env.ec), 130 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
message_ptr foo = env.p.get_message();
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( foo->get_payload().size(), 126 );
}
BOOST_AUTO_TEST_CASE( control_frame_too_large ) {
processor_setup env(false);
uint8_t frame[130] = {0x88, 0x7E, 0x00, 0x7E};
std::fill_n(frame+4,126,0x2A);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,130,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::control_too_big );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( rsv_bits_used ) {
uint8_t frame[3][2] = {{0x90, 0x00},
{0xA0, 0x00},
{0xC0, 0x00}};
for (int i = 0; i < 3; i++) {
processor_setup env(false);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_rsv_bit );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
}
BOOST_AUTO_TEST_CASE( reserved_opcode_used ) {
uint8_t frame[10][2] = {{0x83, 0x00},
{0x84, 0x00},
{0x85, 0x00},
{0x86, 0x00},
{0x87, 0x00},
{0x8B, 0x00},
{0x8C, 0x00},
{0x8D, 0x00},
{0x8E, 0x00},
{0x8F, 0x00}};
for (int i = 0; i < 10; i++) {
processor_setup env(false);
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame[i],2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
}
BOOST_AUTO_TEST_CASE( fragmented_control_message ) {
processor_setup env(false);
uint8_t frame[2] = {0x08, 0x00};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::fragmented_control );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( fragmented_binary_message ) {
processor_setup env0(false);
processor_setup env1(false);
uint8_t frame0[6] = {0x02, 0x01, 0x2A, 0x80, 0x01, 0x2A};
uint8_t frame1[8] = {0x02, 0x01, 0x2A, 0x89, 0x00, 0x80, 0x01, 0x2A};
// read fragmented message in one chunk
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame0,6,env0.ec), 6 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read fragmented message in two chunks
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame0,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), false );
BOOST_CHECK_EQUAL( env0.p.consume(frame0+3,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read fragmented message with control message in between
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env0.p.consume(frame1,8,env0.ec), 5 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_opcode(), websocketpp::frame::opcode::PING);
BOOST_CHECK_EQUAL( env0.p.consume(frame1+5,3,env0.ec), 3 );
BOOST_CHECK( !env0.ec );
BOOST_CHECK_EQUAL( env0.p.ready(), true );
BOOST_CHECK_EQUAL( env0.p.get_message()->get_payload(), "**" );
// read lone continuation frame
BOOST_CHECK_EQUAL( env0.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env0.p.consume(frame0+3,3,env0.ec), 0);
BOOST_CHECK_EQUAL( env0.ec, websocketpp::processor::error::invalid_continuation );
// read two start frames in a row
BOOST_CHECK_EQUAL( env1.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env1.p.consume(frame0,3,env1.ec), 3);
BOOST_CHECK( !env1.ec );
BOOST_CHECK_GT( env1.p.consume(frame0,3,env1.ec), 0);
BOOST_CHECK_EQUAL( env1.ec, websocketpp::processor::error::invalid_continuation );
}
BOOST_AUTO_TEST_CASE( unmasked_client_frame ) {
processor_setup env(true);
uint8_t frame[2] = {0x82, 0x00};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,2,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_required );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( masked_server_frame ) {
processor_setup env(false);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_GT( env.p.consume(frame,8,env.ec), 0 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::masking_forbidden );
BOOST_CHECK_EQUAL( env.p.ready(), false );
}
BOOST_AUTO_TEST_CASE( frame_small_binary_masked ) {
processor_setup env(true);
uint8_t frame[8] = {0x82, 0x82, 0xFF, 0xFF, 0xFF, 0xFF, 0xD5, 0xD5};
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame,8,env.ec), 8 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( masked_fragmented_binary_message ) {
processor_setup env(true);
uint8_t frame0[14] = {0x02, 0x81, 0xAB, 0x23, 0x98, 0x45, 0x81,
0x80, 0x81, 0xB8, 0x34, 0x12, 0xFF, 0x92};
// read fragmented message in one chunk
BOOST_CHECK_EQUAL( env.p.get_message(), message_ptr() );
BOOST_CHECK_EQUAL( env.p.consume(frame0,14,env.ec), 14 );
BOOST_CHECK( !env.ec );
BOOST_CHECK_EQUAL( env.p.ready(), true );
BOOST_CHECK_EQUAL( env.p.get_message()->get_payload(), "**" );
}
BOOST_AUTO_TEST_CASE( prepare_data_frame ) {
processor_setup env(true);
message_ptr in = env.msg_manager->get_message();
message_ptr out = env.msg_manager->get_message();
message_ptr invalid;
// empty pointers arguements should return sane error
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(in,invalid), websocketpp::processor::error::invalid_arguments );
BOOST_CHECK_EQUAL( env.p.prepare_data_frame(invalid,out), websocketpp::processor::error::invalid_arguments );
// test valid opcodes
// control opcodes should return an error, data ones shouldn't
for (int i = 0; i < 0xF; i++) {
in->set_opcode(websocketpp::frame::opcode::value(i));
env.ec = env.p.prepare_data_frame(in,out);
if (websocketpp::frame::opcode::is_control(in->get_opcode())) {
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::invalid_opcode );
} else {
BOOST_CHECK_NE( env.ec, websocketpp::processor::error::invalid_opcode );
}
}
//in.set_payload("foo");
//e = prepare_data_frame(in,out);
}
BOOST_AUTO_TEST_CASE( single_frame_message_too_large ) {
processor_setup env(true);
env.p.set_max_message_size(3);
uint8_t frame0[10] = {0x82, 0x84, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01};
// read message that is one byte too large
BOOST_CHECK_EQUAL( env.p.consume(frame0,10,env.ec), 6 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
}
BOOST_AUTO_TEST_CASE( multiple_frame_message_too_large ) {
processor_setup env(true);
env.p.set_max_message_size(4);
uint8_t frame0[8] = {0x02, 0x82, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01};
uint8_t frame1[9] = {0x80, 0x83, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01};
// read first message frame with size under the limit
BOOST_CHECK_EQUAL( env.p.consume(frame0,8,env.ec), 8 );
BOOST_CHECK( !env.ec );
// read second message frame that puts the size over the limit
BOOST_CHECK_EQUAL( env.p.consume(frame1,9,env.ec), 6 );
BOOST_CHECK_EQUAL( env.ec, websocketpp::processor::error::message_too_big );
}
BOOST_AUTO_TEST_CASE( client_handshake_request ) {
processor_setup env(false);
websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
env.p.client_handshake_request(env.req,u, std::vector<std::string>());
BOOST_CHECK_EQUAL( env.req.get_method(), "GET" );
BOOST_CHECK_EQUAL( env.req.get_version(), "HTTP/1.1");
BOOST_CHECK_EQUAL( env.req.get_uri(), "/");
BOOST_CHECK_EQUAL( env.req.get_header("Host"), "localhost");
BOOST_CHECK_EQUAL( env.req.get_header("Sec-WebSocket-Version"), "13");
BOOST_CHECK_EQUAL( env.req.get_header("Connection"), "Upgrade");
BOOST_CHECK_EQUAL( env.req.get_header("Upgrade"), "websocket");
}
// TODO:
// test cases
// - adding headers
// - adding Upgrade header
// - adding Connection header
// - adding Sec-WebSocket-Version, Sec-WebSocket-Key, or Host header
// - other Sec* headers?
// - User Agent header?
// Origin support
// Subprotocol requests
//websocketpp::uri_ptr u(new websocketpp::uri("ws://localhost/"));
//env.p.client_handshake_request(env.req,u);
BOOST_AUTO_TEST_CASE( client_handshake_response_404 ) {
processor_setup env(false);
std::string res = "HTTP/1.1 404 Not Found\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::invalid_http_status );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_upgrade ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_connection ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response_no_accept ) {
processor_setup env(false);
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK_EQUAL( env.p.validate_server_handshake_response(env.req,env.res), websocketpp::processor::error::missing_required_header );
}
BOOST_AUTO_TEST_CASE( client_handshake_response ) {
processor_setup env(false);
env.req.append_header("Sec-WebSocket-Key", "dGhlIHNhbXBsZSBub25jZQ==");
std::string res = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: foo, wEbsOckEt\r\nConnection: bar, UpGrAdE\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\n\r\n";
env.res.consume(res.data(),res.size());
BOOST_CHECK( !env.p.validate_server_handshake_response(env.req,env.res) );
}
BOOST_AUTO_TEST_CASE( extensions_disabled ) {
processor_setup env(true);
env.req.replace_header("Sec-WebSocket-Extensions","");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK_EQUAL( neg_results.first, websocketpp::processor::error::extensions_disabled );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_blank ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions","");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_unknown ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions","foo");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_empty ) {
processor_setup env(true);
std::vector<std::string> subps;
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_one ) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo");
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_REQUIRE_EQUAL( subps.size(), 1 );
BOOST_CHECK_EQUAL( subps[0], "foo" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_multiple ) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo,bar");
BOOST_CHECK( !env.p.extract_subprotocols(env.req,subps) );
BOOST_REQUIRE_EQUAL( subps.size(), 2 );
BOOST_CHECK_EQUAL( subps[0], "foo" );
BOOST_CHECK_EQUAL( subps[1], "bar" );
}
BOOST_AUTO_TEST_CASE( extract_subprotocols_invalid) {
processor_setup env(true);
std::vector<std::string> subps;
env.req.replace_header("Sec-WebSocket-Protocol","foo,bar,,,,");
BOOST_CHECK_EQUAL( env.p.extract_subprotocols(env.req,subps), websocketpp::processor::error::make_error_code(websocketpp::processor::error::subprotocol_parse_error) );
BOOST_CHECK_EQUAL( subps.size(), 0 );
}
BOOST_AUTO_TEST_CASE( extension_negotiation_permessage_deflate ) {
processor_setup_ext env(true);
env.req.replace_header("Sec-WebSocket-Extensions",
"permessage-deflate; c2s_max_window_bits");
std::pair<websocketpp::lib::error_code,std::string> neg_results;
neg_results = env.p.negotiate_extensions(env.req);
BOOST_CHECK( !neg_results.first );
BOOST_CHECK_EQUAL( neg_results.second, "permessage-deflate" );
}

View File

@@ -0,0 +1,135 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE processors
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <string>
#include <websocketpp/processors/processor.hpp>
#include <websocketpp/http/request.hpp>
BOOST_AUTO_TEST_CASE( exact_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\n\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(!websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( ci_exact_match ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: UpGrAde\r\nUpgrade: WebSocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_exact_match1 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade,foo\r\nUpgrade: websocket,foo\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( non_exact_match2 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: keep-alive,Upgrade,foo\r\nUpgrade: foo,websocket,bar\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::is_websocket_handshake(r));
}
BOOST_AUTO_TEST_CASE( version_blank ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 0);
}
BOOST_AUTO_TEST_CASE( version_7 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 7\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 7);
}
BOOST_AUTO_TEST_CASE( version_8 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 8\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 8);
}
BOOST_AUTO_TEST_CASE( version_13 ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == 13);
}
BOOST_AUTO_TEST_CASE( version_non_numeric ) {
websocketpp::http::parser::request r;
std::string handshake = "GET / HTTP/1.1\r\nHost: www.example.com\r\nUpgrade: websocket\r\nSec-WebSocket-Version: abc\r\n\r\n";
r.consume(handshake.c_str(),handshake.size());
BOOST_CHECK(websocketpp::processor::get_websocket_version(r) == -1);
}

View File

@@ -0,0 +1,27 @@
## random number generation unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','random','system'],env) + [platform_libs]
objs = env.Object('random_none_boost.o', ["none.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('random_device_boost.o', ["random_device.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_random_none_boost', ["random_none_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_random_device_boost', ["random_device_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('random_none_stl.o', ["none.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('random_device_stl.o', ["random_device.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_random_none_stl', ["random_none_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_random_device_stl', ["random_device_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,40 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE random_none
#include <boost/test/unit_test.hpp>
#include <websocketpp/common/stdint.hpp>
#include <websocketpp/random/none.hpp>
BOOST_AUTO_TEST_CASE( does_it_compile ) {
websocketpp::random::none::int_generator<int32_t> rng;
int32_t foo = rng();
BOOST_CHECK( foo == 0 );
}

View File

@@ -0,0 +1,50 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE random_device
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/common/stdint.hpp>
#include <websocketpp/random/random_device.hpp>
#include <websocketpp/concurrency/none.hpp>
BOOST_AUTO_TEST_CASE( compiles ) {
websocketpp::random::random_device::int_generator<int32_t,websocketpp::concurrency::none> rng;
bool e = false;
try {
int32_t foo = rng();
std::cout << foo << std::endl;
} catch (...) {
e = true;
}
BOOST_CHECK( e == false );
}

View File

@@ -0,0 +1,27 @@
## role unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system','random'],env) + [platform_libs]
objs = env.Object('client_boost.o', ["client.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('server_boost.o', ["server.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_client_boost', ["client_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_server_boost', ["server_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework'],env_cpp11) + [platform_libs] + [polyfill_libs]
objs += env_cpp11.Object('client_stl.o', ["client.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('server_stl.o', ["server.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_client_stl', ["client_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_server_stl', ["server_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,194 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE client
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/random/random_device.hpp>
#include <websocketpp/config/core.hpp>
#include <websocketpp/client.hpp>
#include <websocketpp/http/request.hpp>
struct stub_config : public websocketpp::config::core {
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
//typedef core::rng_type rng_type;
typedef websocketpp::random::random_device::int_generator<uint32_t,concurrency_type> rng_type;
typedef core::transport_type transport_type;
typedef core::endpoint_base endpoint_base;
static const websocketpp::log::level elog_level = websocketpp::log::elevel::none;
static const websocketpp::log::level alog_level = websocketpp::log::alevel::none;
};
typedef websocketpp::client<stub_config> client;
typedef client::connection_ptr connection_ptr;
BOOST_AUTO_TEST_CASE( invalid_uri ) {
client c;
websocketpp::lib::error_code ec;
connection_ptr con = c.get_connection("foo", ec);
BOOST_CHECK_EQUAL( ec , websocketpp::error::make_error_code(websocketpp::error::invalid_uri) );
}
BOOST_AUTO_TEST_CASE( unsecure_endpoint ) {
client c;
websocketpp::lib::error_code ec;
connection_ptr con = c.get_connection("wss://localhost/", ec);
BOOST_CHECK_EQUAL( ec , websocketpp::error::make_error_code(websocketpp::error::endpoint_not_secure) );
}
BOOST_AUTO_TEST_CASE( get_connection ) {
client c;
websocketpp::lib::error_code ec;
connection_ptr con = c.get_connection("ws://localhost/", ec);
BOOST_CHECK( con );
BOOST_CHECK_EQUAL( con->get_host() , "localhost" );
BOOST_CHECK_EQUAL( con->get_port() , 80 );
BOOST_CHECK_EQUAL( con->get_secure() , false );
BOOST_CHECK_EQUAL( con->get_resource() , "/" );
}
BOOST_AUTO_TEST_CASE( connect_con ) {
client c;
websocketpp::lib::error_code ec;
std::stringstream out;
std::string o;
c.register_ostream(&out);
connection_ptr con = c.get_connection("ws://localhost/", ec);
c.connect(con);
o = out.str();
websocketpp::http::parser::request r;
r.consume(o.data(),o.size());
BOOST_CHECK( r.ready() );
BOOST_CHECK_EQUAL( r.get_method(), "GET");
BOOST_CHECK_EQUAL( r.get_version(), "HTTP/1.1");
BOOST_CHECK_EQUAL( r.get_uri(), "/");
BOOST_CHECK_EQUAL( r.get_header("Host"), "localhost");
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Version"), "13");
BOOST_CHECK_EQUAL( r.get_header("Connection"), "Upgrade");
BOOST_CHECK_EQUAL( r.get_header("Upgrade"), "websocket");
// Key is randomly generated & User-Agent will change so just check that
// they are not empty.
BOOST_CHECK_NE( r.get_header("Sec-WebSocket-Key"), "");
BOOST_CHECK_NE( r.get_header("User-Agent"), "" );
// connection should have written out an opening handshake request and be in
// the read response internal state
// TODO: more tests related to reading the HTTP response
std::stringstream channel2;
channel2 << "e\r\n\r\n";
channel2 >> *con;
}
BOOST_AUTO_TEST_CASE( select_subprotocol ) {
client c;
websocketpp::lib::error_code ec;
using websocketpp::error::make_error_code;
connection_ptr con = c.get_connection("ws://localhost/", ec);
BOOST_CHECK( con );
con->select_subprotocol("foo",ec);
BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::server_only) );
BOOST_CHECK_THROW( con->select_subprotocol("foo") , websocketpp::exception );
}
BOOST_AUTO_TEST_CASE( add_subprotocols_invalid ) {
client c;
websocketpp::lib::error_code ec;
using websocketpp::error::make_error_code;
connection_ptr con = c.get_connection("ws://localhost/", ec);
BOOST_CHECK( con );
con->add_subprotocol("",ec);
BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) );
BOOST_CHECK_THROW( con->add_subprotocol("") , websocketpp::exception );
con->add_subprotocol("foo,bar",ec);
BOOST_CHECK_EQUAL( ec , make_error_code(websocketpp::error::invalid_subprotocol) );
BOOST_CHECK_THROW( con->add_subprotocol("foo,bar") , websocketpp::exception );
}
BOOST_AUTO_TEST_CASE( add_subprotocols ) {
client c;
websocketpp::lib::error_code ec;
std::stringstream out;
std::string o;
c.register_ostream(&out);
connection_ptr con = c.get_connection("ws://localhost/", ec);
BOOST_CHECK( con );
con->add_subprotocol("foo",ec);
BOOST_CHECK( !ec );
BOOST_CHECK_NO_THROW( con->add_subprotocol("bar") );
c.connect(con);
o = out.str();
websocketpp::http::parser::request r;
r.consume(o.data(),o.size());
BOOST_CHECK( r.ready() );
BOOST_CHECK_EQUAL( r.get_header("Sec-WebSocket-Protocol"), "foo, bar");
}

View File

@@ -0,0 +1,247 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE server
#include <boost/test/unit_test.hpp>
#include <iostream>
// Test Environment:
// server, no TLS, no locks, iostream based transport
#include <websocketpp/config/core.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::core> server;
typedef websocketpp::config::core::message_type::ptr message_ptr;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
/*struct stub_config : public websocketpp::config::core {
typedef core::concurrency_type concurrency_type;
typedef core::request_type request_type;
typedef core::response_type response_type;
typedef core::message_type message_type;
typedef core::con_msg_manager_type con_msg_manager_type;
typedef core::endpoint_msg_manager_type endpoint_msg_manager_type;
typedef core::alog_type alog_type;
typedef core::elog_type elog_type;
typedef core::rng_type rng_type;
typedef core::transport_type transport_type;
typedef core::endpoint_base endpoint_base;
};*/
/* Run server and return output test rig */
std::string run_server_test(server& s, std::string input) {
server::connection_ptr con;
std::stringstream output;
s.register_ostream(&output);
s.clear_access_channels(websocketpp::log::alevel::all);
s.clear_error_channels(websocketpp::log::elevel::all);
con = s.get_connection();
con->start();
std::stringstream channel;
channel << input;
channel >> *con;
return output.str();
}
/* handler library*/
void echo_func(server* s, websocketpp::connection_hdl hdl, message_ptr msg) {
s->send(hdl, msg->get_payload(), msg->get_opcode());
}
bool validate_func_subprotocol(server* s, std::string* out, std::string accept,
websocketpp::connection_hdl hdl)
{
server::connection_ptr con = s->get_con_from_hdl(hdl);
std::stringstream o;
const std::vector<std::string> & protocols = con->get_requested_subprotocols();
std::vector<std::string>::const_iterator it;
for (it = protocols.begin(); it != protocols.end(); ++it) {
o << *it << ",";
}
*out = o.str();
if (accept != "") {
con->select_subprotocol(accept);
}
return true;
}
void open_func_subprotocol(server* s, std::string* out, websocketpp::connection_hdl hdl) {
server::connection_ptr con = s->get_con_from_hdl(hdl);
*out = con->get_subprotocol();
}
/* Tests */
BOOST_AUTO_TEST_CASE( basic_websocket_request ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
server s;
s.set_user_agent("test");
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( invalid_websocket_version ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: a\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 400 Bad Request\r\nServer: test\r\n\r\n";
server s;
s.set_user_agent("test");
//s.set_message_handler(bind(&echo_func,&s,::_1,::_2));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( unimplemented_websocket_version ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 14\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\n\r\n";
std::string output = "HTTP/1.1 400 Bad Request\r\nSec-WebSocket-Version: 0,7,8,13\r\nServer: test\r\n\r\n";
server s;
s.set_user_agent("test");
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
}
BOOST_AUTO_TEST_CASE( list_subprotocol_empty ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
std::string subprotocol;
server s;
s.set_user_agent("test");
s.set_open_handler(bind(&open_func_subprotocol,&s,&subprotocol,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
BOOST_CHECK_EQUAL(subprotocol, "");
}
BOOST_AUTO_TEST_CASE( list_subprotocol_one ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
std::string validate;
std::string open;
server s;
s.set_user_agent("test");
s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"",::_1));
s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
BOOST_CHECK_EQUAL(validate, "foo,");
BOOST_CHECK_EQUAL(open, "");
}
BOOST_AUTO_TEST_CASE( accept_subprotocol_one ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: foo\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
std::string validate;
std::string open;
server s;
s.set_user_agent("test");
s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"foo",::_1));
s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
BOOST_CHECK_EQUAL(validate, "foo,");
BOOST_CHECK_EQUAL(open, "foo");
}
BOOST_AUTO_TEST_CASE( accept_subprotocol_invalid ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: foo\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
std::string validate;
std::string open;
server s;
s.set_user_agent("test");
s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"foo2",::_1));
s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
std::string o;
BOOST_CHECK_THROW(o = run_server_test(s,input), websocketpp::exception);
}
BOOST_AUTO_TEST_CASE( accept_subprotocol_two ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example.com\r\nSec-WebSocket-Protocol: foo, bar\r\n\r\n";
std::string output = "HTTP/1.1 101 Switching Protocols\r\nConnection: upgrade\r\nSec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=\r\nSec-WebSocket-Protocol: bar\r\nServer: test\r\nUpgrade: websocket\r\n\r\n";
std::string validate;
std::string open;
server s;
s.set_user_agent("test");
s.set_validate_handler(bind(&validate_func_subprotocol,&s,&validate,"bar",::_1));
s.set_open_handler(bind(&open_func_subprotocol,&s,&open,::_1));
BOOST_CHECK_EQUAL(run_server_test(s,input), output);
BOOST_CHECK_EQUAL(validate, "foo,bar,");
BOOST_CHECK_EQUAL(open, "bar");
}
/*BOOST_AUTO_TEST_CASE( user_reject_origin ) {
std::string input = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: upgrade\r\nUpgrade: websocket\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nOrigin: http://www.example2.com\r\n\r\n";
std::string output = "HTTP/1.1 403 Forbidden\r\nServer: test\r\n\r\n";
server s;
s.set_user_agent("test");
BOOST_CHECK(run_server_test(s,input) == output);
}*/

View File

@@ -0,0 +1,24 @@
## transport integration tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system','thread','random'],env) + [platform_libs] + [tls_libs]
objs = env.Object('boost_integration.o', ["integration.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_boost_integration', ["boost_integration.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
objs += env_cpp11.Object('stl_integration.o', ["integration.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_stl_integration', ["stl_integration.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,28 @@
## asio transport unit tests
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
Import('tls_libs')
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
BOOST_LIBS = boostlibs(['unit_test_framework','system','thread'],env) + [platform_libs] + [tls_libs]
objs = env.Object('base_boost.o', ["base.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('timers_boost.o', ["timers.cpp"], LIBS = BOOST_LIBS)
prgs = env.Program('test_base_boost', ["base_boost.o"], LIBS = BOOST_LIBS)
prgs += env.Program('test_timers_boost', ["timers_boost.o"], LIBS = BOOST_LIBS)
if env_cpp11.has_key('WSPP_CPP11_ENABLED'):
BOOST_LIBS_CPP11 = boostlibs(['unit_test_framework','system'],env_cpp11) + [platform_libs] + [polyfill_libs] + [tls_libs]
objs += env_cpp11.Object('base_stl.o', ["base.cpp"], LIBS = BOOST_LIBS_CPP11)
objs += env_cpp11.Object('timers_stl.o', ["timers.cpp"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_base_stl', ["base_stl.o"], LIBS = BOOST_LIBS_CPP11)
prgs += env_cpp11.Program('test_timers_stl', ["timers_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')

View File

@@ -0,0 +1,49 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_base
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <websocketpp/transport/asio/base.hpp>
BOOST_AUTO_TEST_CASE( blank_error ) {
websocketpp::lib::error_code ec;
BOOST_CHECK( !ec );
}
BOOST_AUTO_TEST_CASE( asio_error ) {
using websocketpp::transport::asio::error::make_error_code;
using websocketpp::transport::asio::error::general;
websocketpp::lib::error_code ec = make_error_code(general);
BOOST_CHECK( ec == general );
BOOST_CHECK( ec.value() == 1 );
}

View File

@@ -0,0 +1,187 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE transport_asio_timers
#include <boost/test/unit_test.hpp>
#include <exception>
#include <iostream>
#include <websocketpp/common/thread.hpp>
#include <websocketpp/transport/asio/endpoint.hpp>
#include <websocketpp/transport/asio/security/tls.hpp>
// Concurrency
#include <websocketpp/concurrency/none.hpp>
// HTTP
#include <websocketpp/http/request.hpp>
#include <websocketpp/http/response.hpp>
// Loggers
#include <websocketpp/logger/stub.hpp>
//#include <websocketpp/logger/basic.hpp>
#include <boost/asio.hpp>
// Accept a connection, read data, and discard until EOF
void run_dummy_server(int port) {
using boost::asio::ip::tcp;
try {
boost::asio::io_service io_service;
tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v6(), port));
tcp::socket socket(io_service);
acceptor.accept(socket);
for (;;) {
char data[512];
boost::system::error_code ec;
socket.read_some(boost::asio::buffer(data), ec);
if (ec == boost::asio::error::eof) {
break;
} else if (ec) {
// other error
throw ec;
}
}
} catch (std::exception & e) {
std::cout << e.what() << std::endl;
} catch (boost::system::error_code & ec) {
std::cout << ec.message() << std::endl;
}
}
// Wait for the specified time period then fail the test
void run_test_timer(long value) {
boost::asio::io_service ios;
boost::asio::deadline_timer t(ios,boost::posix_time::milliseconds(value));
boost::system::error_code ec;
t.wait(ec);
BOOST_FAIL( "Test timed out" );
}
struct config {
typedef websocketpp::concurrency::none concurrency_type;
//typedef websocketpp::log::basic<concurrency_type,websocketpp::log::alevel> alog_type;
typedef websocketpp::log::stub alog_type;
typedef websocketpp::log::stub elog_type;
typedef websocketpp::http::parser::request request_type;
typedef websocketpp::http::parser::response response_type;
typedef websocketpp::transport::asio::tls_socket::endpoint socket_type;
static const bool enable_multithreading = true;
static const long timeout_socket_pre_init = 1000;
static const long timeout_proxy = 1000;
static const long timeout_socket_post_init = 1000;
static const long timeout_dns_resolve = 1000;
static const long timeout_connect = 1000;
static const long timeout_socket_shutdown = 1000;
};
// Mock context that does no validation
typedef websocketpp::lib::shared_ptr<boost::asio::ssl::context> context_ptr;
context_ptr on_tls_init(websocketpp::connection_hdl) {
return context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::tlsv1));
}
// Mock connection
struct mock_con: public websocketpp::transport::asio::connection<config> {
typedef websocketpp::transport::asio::connection<config> base;
mock_con(bool a, config::alog_type& b, config::elog_type& c) : base(a,b,c) {}
void start() {
base::init(websocketpp::lib::bind(&mock_con::handle_start,this,
websocketpp::lib::placeholders::_1));
}
void handle_start(const websocketpp::lib::error_code& ec) {
using websocketpp::transport::asio::socket::make_error_code;
using websocketpp::transport::asio::socket::error::tls_handshake_timeout;
BOOST_CHECK_EQUAL( ec, make_error_code(tls_handshake_timeout) );
base::cancel_socket();
}
};
typedef websocketpp::transport::asio::connection<config> con_type;
typedef websocketpp::lib::shared_ptr<mock_con> connection_ptr;
struct mock_endpoint : public websocketpp::transport::asio::endpoint<config> {
typedef websocketpp::transport::asio::endpoint<config> base;
mock_endpoint() {
alog.set_channels(websocketpp::log::alevel::all);
base::init_logging(&alog,&elog);
init_asio();
}
void connect(std::string u) {
m_con.reset(new mock_con(false,alog,elog));
websocketpp::uri_ptr uri(new websocketpp::uri(u));
BOOST_CHECK( uri->get_valid() );
BOOST_CHECK_EQUAL( base::init(m_con), websocketpp::lib::error_code() );
base::async_connect(
m_con,
uri,
websocketpp::lib::bind(
&mock_endpoint::handle_connect,
this,
m_con,
websocketpp::lib::placeholders::_1
)
);
}
void handle_connect(connection_ptr con, websocketpp::lib::error_code const & ec)
{
BOOST_CHECK( !ec );
con->start();
}
connection_ptr m_con;
config::alog_type alog;
config::elog_type elog;
};
BOOST_AUTO_TEST_CASE( tls_handshake_timeout ) {
websocketpp::lib::thread dummy_server(websocketpp::lib::bind(&run_dummy_server,9005));
websocketpp::lib::thread timer(websocketpp::lib::bind(&run_test_timer,5000));
dummy_server.detach();
timer.detach();
mock_endpoint endpoint;
endpoint.set_tls_init_handler(&on_tls_init);
endpoint.connect("wss://localhost:9005");
endpoint.run();
}

View File

@@ -0,0 +1,98 @@
/*
* Copyright (c) 2011, Peter Thorson. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the WebSocket++ Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL PETER THORSON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
//#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE hybi_util
#include <boost/test/unit_test.hpp>
#include <iostream>
#include "../../src/processors/hybi_util.hpp"
#include "../../src/network_utilities.hpp"
BOOST_AUTO_TEST_CASE( circshift_0 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,0);
BOOST_CHECK( test == 0x0123456789abcdef);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,0);
BOOST_CHECK( test == 0x01234567);
}
}
BOOST_AUTO_TEST_CASE( circshift_1 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,1);
BOOST_CHECK( test == 0xef0123456789abcd);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,1);
BOOST_CHECK( test == 0x67012345);
}
}
BOOST_AUTO_TEST_CASE( circshift_2 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,2);
BOOST_CHECK( test == 0xcdef0123456789ab);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,2);
BOOST_CHECK( test == 0x45670123);
}
}
BOOST_AUTO_TEST_CASE( circshift_3 ) {
if (sizeof(size_t) == 8) {
size_t test = 0x0123456789abcdef;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,3);
BOOST_CHECK( test == 0xabcdef0123456789);
} else {
size_t test = 0x01234567;
test = websocketpp::processor::hybi_util::circshift_prepared_key(test,3);
BOOST_CHECK( test == 0x23456701);
}
}

Some files were not shown because too many files have changed in this diff Show More