Compare commits

..

83 Commits

Author SHA1 Message Date
Peter Thorson 378437aecd Merge branch 'develop' 2016-02-22 08:30:10 -05:00
Peter Thorson cc7d74d8d0 Set version flags for release 2016-02-22 08:28:27 -05:00
Peter Thorson b154ac5907 Add FAQ entry 2016-02-22 08:28:05 -05:00
Peter Thorson 0348510c8d Fix minor issue in hybi00 subprotocol support patch. Update changelog for recent PRs 2016-02-21 18:30:58 -05:00
Peter Thorson a495c8c529 Merge branch 'develop' of https://github.com/zaphoyd/websocketpp into develop 2016-02-21 18:25:14 -05:00
Peter Thorson fabf8bf1ca clarified debugging output in connection read_handshake 2016-02-21 18:24:57 -05:00
Peter Thorson 3f913f97ef update tls version used in tls timer tests 2016-02-21 18:24:37 -05:00
Peter Thorson ffaeba2ec7 switch comment style in connection tests 2016-02-21 18:24:19 -05:00
Peter Thorson db304bfec4 Remove the use of cached read and write handlers in the asio transport. references #490 #525
There isn't a clean way to implement this performance optimization without adding global state/locking, which performs worse. This should fix
2016-02-21 18:23:58 -05:00
Peter Thorson d3bec51d25 Merge pull request #518 from lobermann/hixie76-hybi00-subprotocol-support
added subprotocol support for hybi00 / hixie76
2016-02-21 13:07:24 -05:00
Peter Thorson 8b672f7372 Merge pull request #524 from xavigibert/fix-uri
Fixed uri validator by checking its length.
2016-02-21 13:04:18 -05:00
Xavier Gibert Serra a4a79f6bf6 Fixed uri validator. This bug caused asan error heap-buffer-overflow when passing a short address. 2016-02-18 08:45:20 -08:00
Peter Thorson 9dc013a3aa Add an FAQ entry explaining how to use permessage-deflate in 0.6.0 and 0.7.0 2016-02-18 09:11:19 -05:00
Peter Thorson 931ad40b5f documentation & friend updates 2016-02-18 08:46:15 -05:00
Peter Thorson 842f745e24 Add echo_client example 2016-02-18 08:45:31 -05:00
Lukas Obermann e6f8766707 added subprotocol support for hybi00 / hixie76
This was missing but it is described in the RFC here
https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.9
2016-02-10 17:07:02 +01:00
Peter Thorson 17a2647454 Apply tentative fix for explicitly destroying objects that use io_service before it is deleted 2016-02-08 07:32:57 -05:00
Peter Thorson 323dec4efc Merge branch 'strand-fix-1' into develop 2016-02-08 07:31:41 -05:00
Peter Thorson ee343fb282 Adjust use of strand to work better with composite asio operations. references #459
particularly affected TLS servers and connection close behavior
2016-02-08 07:31:06 -05:00
Peter Thorson b76422aa7a explicitly delete work and resolver object before deleting io_service references #510 2016-02-07 11:58:04 -05:00
Peter Thorson 563fcc6b45 wrap TLS async_shutdown in a strand references #459 #466 2016-02-07 11:26:00 -05:00
Peter Thorson b6014cc79b FAQ & Change log minor update 2016-02-02 09:39:12 -05:00
Peter Thorson d917f0924b Remove execute permission from sha1.hpp fixes #489 2016-01-31 15:14:44 -05:00
Peter Thorson e036a5aeab Also fix this use of auto_ptr 2016-01-31 15:11:33 -05:00
Peter Thorson ed335a37b5 Replace deprecated use of auto_ptr when unique_ptr is available 2016-01-31 11:07:36 -05:00
Peter Thorson bb4cbf3d03 Merge branch 'develop' of https://github.com/zaphoyd/websocketpp into develop 2016-01-19 09:34:43 -05:00
Peter Thorson a388817606 Initial work on utility_server tutorial 2016-01-19 09:33:18 -05:00
Peter Thorson b373f1ea6b Merge pull request #509 from mgeier/no-get-uri
telemetry_server: Remove unnecessary call to get_uri()
2016-01-19 09:30:23 -05:00
Peter Thorson 28448c77a5 Merge pull request #507 from mgeier/unneeded-typedef
Remove unneeded typedef from telemetry_server example
2016-01-19 09:29:07 -05:00
Peter Thorson ae097d6a93 Merge pull request #508 from mgeier/lock-guard
broadcast_server: replace explicit unlock() by scoped unlock
2016-01-19 09:28:56 -05:00
Matthias Geier 02f0026a00 telemetry_server: Remove unnecessary call to get_uri() 2016-01-19 13:22:34 +01:00
Matthias Geier aac65bdfd0 broadcast_server: replace explicit unlock() by scoped unlock
Also: Replace some instances of unique_lock with lock_guard.
2016-01-19 12:31:23 +01:00
Matthias Geier 7b368e67ed Remove unneeded typedef from telemetry_server example 2016-01-19 10:29:37 +01:00
Peter Thorson d0802994d8 Merge branch 'pr/502' into develop 2016-01-14 09:12:02 -05:00
Peter Thorson 20cc197967 Fix a few other misc test related things 2016-01-12 10:51:59 -05:00
Gregor Jasny 89df55c577 Register tests to CTest 2016-01-05 09:51:15 +01:00
Peter Thorson 9713455e94 Fix TLS+Asio Standalone builds 2015-12-20 09:36:46 -05:00
Peter Thorson 044eda9cc4 Merge pull request #493 from jchampio/dev/cancel_socket
Always log errors when cancelling sockets
2015-12-19 11:52:24 -05:00
Peter Thorson fc6ec59ab8 more travis updates 2015-12-17 08:11:23 -05:00
Peter Thorson a01cce3b1a travis update 2015-12-17 07:32:26 -05:00
Peter Thorson e1b39037f7 travis config update 2015-12-17 07:30:00 -05:00
Peter Thorson f54fb0669b more docs 2015-12-17 07:29:51 -05:00
Peter Thorson 935c8a3264 Begin update to migrate manual content into the git repository
The goal here is to get all the documentation in one place including
the class reference, text based manual, tutorials, etc.
2015-12-16 10:03:14 -05:00
Peter Thorson 1e12e7aff8 use c++11 thread support on recent visual studio versions (really) 2015-12-15 19:17:11 -05:00
Peter Thorson bda1bd14cb use c++11 thread support on recent visual studio versions 2015-12-15 19:13:37 -05:00
Jacob Champion 5f52127cf7 Always log errors during socket cancel
cancel_socket_checked(), which logs any received errors, is now called
consistently instead of the lower-level cancel_socket().
2015-12-15 15:06:01 -08:00
Jacob Champion 999add0c4b Improve socket cancellation error messages
log_err() includes information about the error code.
2015-12-15 15:04:41 -08:00
Jacob Champion f7448bb1b0 Write to the error log for socket cancel failures
Previously an elevel entry was being written to an alog (possible
copy-paste error?).
2015-12-15 14:40:10 -08:00
Peter Thorson ed45dcc617 Fix an issue where the wrong type of strand was being created. 2015-11-24 10:22:54 -05:00
Peter Thorson 7f19b6ea95 Merge pull request #485 from Meoo/patch-1
Fix make_shared using the wrong strand type
2015-11-24 10:04:03 -05:00
Bastien Brunnenstein 5b99feef7f Fix make_shared using the wrong strand type
Fix compilation with standalone ASIO (#462), where asio::strand and asio::io_service::strand are separate types.
This is the correct type, as m_strand is a ``lib::shared_ptr<lib::asio::io_service::strand>``
2015-11-24 15:24:52 +01:00
Peter Thorson b85affddf4 Test how some doxygen compatible markup behaves on github 2015-11-05 08:29:04 -05:00
Peter Thorson 5500aef4ce Allow accessing the local endpoint when using the Asio transport. fixes #458 2015-11-05 06:33:15 -05:00
Peter Thorson 4e2fb75d07 Removes non-standards compliant masking behavior. fixes #469, fixes #395 2015-10-30 09:26:28 -04:00
Peter Thorson 6a94162264 Fix error handling in debug_client fixes #473 2015-10-30 08:45:32 -04:00
Peter Thorson 536db87c53 Merge branch 'pr/468' into develop 2015-09-30 09:59:11 -04:00
Peter Thorson 39c5f3ae3d Update changelog 2015-09-30 09:56:57 -04:00
vladon cd4af122c0 avoid unnecessary empty strings ("") in favor of clear() and empty() (performance) 2015-09-29 21:36:41 +03:00
Peter Thorson b6817e4c7e Actually have the response status getters work; also tests 2015-09-19 10:54:50 -04:00
Peter Thorson 72e55a76b5 remove alias for better compatibility with older compilers 2015-09-17 10:09:28 -04:00
Peter Thorson 93b75ccc7d Add get_status and get_response methods to connection fixes #465 2015-09-17 10:09:07 -04:00
Peter Thorson 3e8dbf767d Add example demonstrating external io_service 2015-09-17 08:11:48 -04:00
Peter Thorson 3c77ce3cc7 Fix race condition resulting in corruption of responses sent from non-blocking HTTP handers
The HTTP request processing code was expecting that the http response
state would be `deferred` after a call to an HTTP handler that deferred
the response. If, in addition to deferring, you also sent the response
the HTTP state advanced past `deferred` to `response_written`. This
doesn't easily show up in unit tests because the bug requires that the
async write still be in flight and the connection still be open when
the HTTP handler returns.

An asynchronous network transport (like Asio) combined with a
sufficiently large message that Asio yields control back to WebSocket++
before the response is fully written will trigger a second write on the
same connection while the first is in flight. This is not allowed per
the Asio spec and results in undefined behavior.

The HTTP request processing code now checks if the http state is init,
i.e. not deferred and no response has been started rather than just if
it was deferred.
2015-09-09 10:14:37 -04:00
Peter Thorson 54ce641d02 remove tabs 2015-08-25 09:30:00 -04:00
Peter Thorson e0bd3f7e95 Address an issue where an exception could be thrown when a socket gets canceled on Windows XP #460
This fix switches to using the non-exception variant of socket::cancel
and logging the error instead of throwing it. There is a chance that
something else will need to be addressed as a result of this change as
some async operations might not be cancelled? I believe everything in
question has timeouts that will trigger operation cancellation at some
point anyways.
2015-08-25 09:03:00 -04:00
Peter Thorson fb829e0895 Misc changes related to translation of asio transport errors #448 2015-08-20 10:14:55 -04:00
Peter Thorson f9dbc0fd9d Merge branch 'pr/456' into develop 2015-08-06 09:12:05 -04:00
Peter Thorson 0b2b77e4ad update changelog 2015-08-06 09:11:50 -04:00
Eli Fidler 15c78afbf4 fix crash in strip_lws()
Before this fix, a malicious client could cause a websocketpp server to create an invalid std::string.
2015-08-04 12:01:14 -04:00
Peter Thorson 4b18214eda Add preliminary support for C++11 features in Visual Studio 2015 2015-08-01 12:38:18 -04:00
Peter Thorson e80673278f Merge branch 'pr/454' into develop 2015-07-29 09:29:37 -04:00
Peter Thorson 95551e7e94 code style & change log update for PR 2015-07-29 09:28:22 -04:00
Peter Thorson 22918a3992 Merge branch 'permessage-deflate' into develop
Conflicts:
	Doxyfile
	examples/utility_client/CMakeLists.txt
	websocketpp/version.hpp
2015-07-29 09:12:21 -04:00
Peter Thorson 9b09b0b481 Merge branch 'pr/449' into develop 2015-07-29 08:59:14 -04:00
Peter Thorson ef380f7fff Add CMake build support for examples and tests, zlib dependencies 2015-07-29 08:57:56 -04:00
Mark Grimes 1f504001d2 Use smart pointer to hold new io_service temporarily in case of errors 2015-07-29 12:36:22 +01:00
Peter Thorson c54f2abaab utility client build scripts 2015-07-23 07:53:51 -04:00
Peter Thorson 3580530902 Miscellaneous changes
- Updates code style
- Adds some tags for future exception free work
- Fix utility client build scripts
2015-07-23 07:53:31 -04:00
Thijs Wenker bc7ad6550e fixed typo 2015-07-16 12:18:32 +02:00
Thijs Wenker d3fe51fa39 Added missing CMake files, organized the CMake structure between test and example directories using the global variable USE_FOLDERS 2015-07-16 12:17:59 +02:00
Thijs Wenker 7514c82c92 CMake: properly include examples and tests with macro (added unit_test_framework dependency for unit tests) 2015-07-16 11:38:38 +02:00
Peter Thorson dabf32a668 Better std::chrono autodetection for Visual Studio 2015-06-05 08:49:07 -04:00
Peter Thorson 39497aaa61 reset dev branch for work on next version 2015-06-02 08:59:10 -04:00
109 changed files with 3937 additions and 1510 deletions
+4
View File
@@ -88,3 +88,7 @@ examples/wsperf/wsperf_client
*.vcxproj.filters
*.user
install
Makefile
bin
Testing/Temporary/CTestCostData.txt
+3 -5
View File
@@ -2,18 +2,16 @@ language: cpp
compiler:
- gcc
before_install:
- sudo apt-get install libboost-chrono1.48-dev libboost-regex1.48-dev libboost-system1.48-dev libboost-thread1.48-dev libboost-test1.48-dev libboost-random1.48-dev -y
#- sudo apt-get install libboost-chrono1.48-dev libboost-regex1.48-dev libboost-system1.48-dev libboost-thread1.48-dev libboost-test1.48-dev libboost-random1.48-dev -y
- sudo add-apt-repository -y ppa:boost-latest/ppa && sudo apt-get update -q && sudo apt-get install -y libboost-chrono1.55-dev libboost-random1.55-dev libboost-regex1.55-dev libboost-system1.55-dev libboost-thread1.55-dev libboost-test1.55-dev
env:
global:
- BOOST_INCLUDES=/usr/include
- BOOST_LIBS=/usr/lib
- BOOST_LIBS=/usr/lib/x86_64-linux-gnu
script: scons -j 2 && scons test
branches:
only:
- master
- permessage-deflate
- experimental
- 0.3.x-cmake
- develop
notifications:
recipients:
+20 -3
View File
@@ -9,10 +9,12 @@ project (websocketpp)
cmake_minimum_required (VERSION 2.6)
set (WEBSOCKETPP_MAJOR_VERSION 0)
set (WEBSOCKETPP_MINOR_VERSION 6)
set (WEBSOCKETPP_MINOR_VERSION 7)
set (WEBSOCKETPP_PATCH_VERSION 0)
set (WEBSOCKETPP_VERSION ${WEBSOCKETPP_MAJOR_VERSION}.${WEBSOCKETPP_MINOR_VERSION}.${WEBSOCKETPP_PATCH_VERSION})
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files")
if (WIN32 AND NOT CYGWIN)
set (DEF_INSTALL_CMAKE_DIR cmake)
@@ -67,6 +69,8 @@ option (BUILD_TESTS "Build websocketpp tests." FALSE)
if (BUILD_TESTS OR BUILD_EXAMPLES)
enable_testing ()
############ Compiler specific setup
set (WEBSOCKETPP_PLATFORM_LIBS "")
@@ -150,6 +154,10 @@ if (BUILD_TESTS OR BUILD_EXAMPLES)
list (APPEND WEBSOCKETPP_BOOST_LIBS random)
endif()
if (BUILD_TESTS)
list (APPEND WEBSOCKETPP_BOOST_LIBS unit_test_framework)
endif()
############ Dependencies
# Set BOOST_ROOT env variable or pass with cmake -DBOOST_ROOT=path.
@@ -180,6 +188,14 @@ if (BUILD_TESTS OR BUILD_EXAMPLES)
set (Boost_USE_STATIC_LIBS FALSE)
endif ()
if (BOOST_STATIC)
set (Boost_USE_STATIC_LIBS TRUE)
endif ()
if (NOT Boost_USE_STATIC_LIBS)
add_definitions (/DBOOST_TEST_DYN_LINK)
endif ()
set (Boost_FIND_REQUIRED TRUE)
set (Boost_FIND_QUIETLY TRUE)
set (Boost_DEBUG FALSE)
@@ -212,6 +228,7 @@ if (BUILD_TESTS OR BUILD_EXAMPLES)
endif ()
find_package(OpenSSL)
find_package(ZLIB)
endif()
############ Add projects
@@ -221,12 +238,12 @@ add_subdirectory (websocketpp)
# Add examples
if (BUILD_EXAMPLES)
add_subdirectory (examples)
include_subdirs ("examples")
endif ()
# Add tests
if (BUILD_TESTS)
add_subdirectory (test)
include_subdirs ("test")
endif ()
print_used_build_config()
+1501 -1018
View File
File diff suppressed because it is too large Load Diff
+12 -2
View File
@@ -34,6 +34,11 @@ elif os.environ.has_key('BOOST_INCLUDES') and os.environ.has_key('BOOST_LIBS'):
else:
raise SCons.Errors.UserError, "Neither BOOST_ROOT, nor BOOST_INCLUDES + BOOST_LIBS was set!"
## Custom OpenSSL
if os.environ.has_key('OPENSSL_PATH'):
env.Append(CPPPATH = os.path.join(os.environ['OPENSSL_PATH'], 'include'))
env.Append(LIBPATH = os.environ['OPENSSL_PATH'])
if os.environ.has_key('WSPP_ENABLE_CPP11'):
env['WSPP_ENABLE_CPP11'] = True
else:
@@ -93,8 +98,7 @@ if env['PLATFORM'].startswith('win'):
#env['LIBPATH'] = env['BOOST_LIBS']
pass
else:
env['LIBPATH'] = ['/usr/lib',
'/usr/local/lib'] #, env['BOOST_LIBS']
env.Append(LIBPATH = ['/usr/lib', '/usr/local/lib'])
# Compiler specific warning flags
if env['CXX'].startswith('g++'):
@@ -227,6 +231,9 @@ if not env['PLATFORM'].startswith('win'):
# echo_server
echo_server = SConscript('#/examples/echo_server/SConscript',variant_dir = builddir + 'echo_server',duplicate = 0)
# echo_client
echo_client = SConscript('#/examples/echo_client/SConscript',variant_dir = builddir + 'echo_client',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)
@@ -260,6 +267,9 @@ subprotocol_server = SConscript('#/examples/subprotocol_server/SConscript',varia
# telemetry_server
telemetry_server = SConscript('#/examples/telemetry_server/SConscript',variant_dir = builddir + 'telemetry_server',duplicate = 0)
# external_io_service
external_io_service = SConscript('#/examples/external_io_service/SConscript',variant_dir = builddir + 'external_io_service',duplicate = 0)
if not env['PLATFORM'].startswith('win'):
# iostream_server
iostream_server = SConscript('#/examples/iostream_server/SConscript',variant_dir = builddir + 'iostream_server',duplicate = 0)
+64 -1
View File
@@ -1,6 +1,69 @@
HEAD
0.6.0
0.7.0 - 2016-02-22
- MINOR BREAKING SOCKET POLICY CHANGE: Asio transport socket policy method
`cancel_socket` will now return `lib::asio::error_code` instead of `void`.
Custom Asio transport socket policies will need to be updated accordingly.
This does not affect anyone using the bundled socket policies.
- Feature: Basic support for the permessage-deflate extension. #344
- Feature: Allow accessing the local endpoint when using the Asio transport.
This allows inspection of the address and port in cases where they are chosen
by the operating system rather than the user. Thank you Andreas Weis and
Muzahid Hussain for reporting and related code. #458
- Feature: Add support for subprotocols in Hybi00. Thank you Lukas Obermann
for reporting and a patch. #518
- Improvement: Better automatic std::chrono feature detection for Visual Studio
- Improvement: Major refactoring to bundled CMake build system. CMake can now be
used to build all of the examples and the test suite. Thank you Thijs Wenker
for a significant portion of this code. #378, #435, #449
- Improvement: In build environments where `lib::error_code` and
`lib::asio::error_code` match (such as using `boost::asio` with
`boost::system_error` or standalone asio with `std::system_error`, transport
errors are passed through natively rather than being reported as a translated
`pass_through` error type.
- Improvement: Add a `get_transport_error` method to Asio transport connections
to allow retrieving a machine readable native transport error.
- Improvement: Add `connection::get_response`, `connection::get_response_code`,
and `connection::get_response_msg` methods to allow accessing additional
information about the HTTP responses that WebSocket++ sends. #465 Thank you
Flow86 for reporting.
- Improvement: Removes use of empty strings ("") in favor of `string::clear()`
and `string::empty()`. This avoids generating unnecessary temporary objects.
#468 Thank you Vladislav Yaroslavlev for reporting and a patch.
- Documentation: Adds an example demonstrating the use of external `io_service`
- Documentation: Adds a simple echo_client example.
- Documentation: Begins migration of the web based user manual into Doxygen.
- Bug: Fix memory leak when init_asio produces an error. #454 Thank you Mark
Grimes for reporting and fixing.
- Bug: Fix crash when processing a specially crafted HTTP header. Thank you Eli
Fidler for reporting, test cases, and a patch. #456
- Bug: Fix an issue where standalone Asio builds that use TLS would not compile
due to lingering boost code. #448 Thank you mjsp for reporting
- Bug: Fix an issue where canceling a socket could throw an exception on some
older Windows XP platforms. It now prints an appropriate set of log messages
instead. Thank you Thijs Wenker for reporting and researching solutions. #460
- Bug: Fix an issue where deferred HTTP connections that start sending a very
long response before their HTTP handler ends would result in a second set of
HTTP headers being injected into the output. Thank you Kevin Smith for
reporting and providing test case details. #443
- Bug: Fix an issue where the wrong type of strand was being created. Thank you
Bastien Brunnenstein for reporting and a patch. #462
- Bug: Fix an issue where TLS includes were broken for Asio Standalone builds.
Thank you giachi and Bastien Brunnenstein for reporting. #491
- Bug: Remove the use of cached read and write handlers in the Asio transport.
This feature caused memory leaks when the io_service the connection was
running on was abruptly stopped. There isn't a clean and safe way of using
this optimization without global state and the associated locks. The locks
perform worse. Thank you Xavier Gibert for reporting, test cases, and code.
Fixes #490.
- Bug: Fix a heap buffer overflow when checking very short URIs. Thank you
Xavier Gibert for reporting and a patch #524
- Compatibility: Fixes a number of build & config issues on Visual Studio 2015
- Compatibility: Removes non-standards compliant masking behavior. #395, #469
- Compatibility: Replace deprecated use of auto_ptr on systems where unique_ptr
is available.
0.6.0 - 2015-06-02
- MINOR BREAKING TRANSPORT POLICY CHANGE: Custom transport policies will now be
required to include a new method `void set_uri(uri_ptr u)`. An implementation
is not required. The stub transport policy includes an example stub method
+32 -1
View File
@@ -13,7 +13,12 @@ macro (print_used_build_config)
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 (STATUS "WEBSOCKETPP_PLATFORM_TLS_LIBS = ${WEBSOCKETPP_PLATFORM_TLS_LIBS}")
message ("")
message (STATUS "OPENSSL_FOUND = ${OPENSSL_FOUND}")
message (STATUS "OPENSSL_INCLUDE_DIR = ${OPENSSL_INCLUDE_DIR}")
message (STATUS "OPENSSL_LIBRARIES = ${OPENSSL_LIBRARIES}")
message (STATUS "OPENSSL_VERSION = ${OPENSSL_VERSION}")
message ("")
endmacro ()
@@ -49,10 +54,23 @@ macro (build_executable TARGET_NAME)
include_directories (${WEBSOCKETPP_ROOT} ${WEBSOCKETPP_INCLUDE})
target_link_libraries(${TARGET_NAME} ${WEBSOCKETPP_PLATFORM_LIBS})
set_target_properties (${TARGET_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${WEBSOCKETPP_BIN})
set_target_properties (${TARGET_NAME} PROPERTIES DEBUG_POSTFIX d)
endmacro ()
# Build executable and register as test
macro (build_test TARGET_NAME)
build_executable (${TARGET_NAME} ${ARGN})
if (${CMAKE_VERSION} VERSION_LESS 3)
message(WARNING "CMake too old to register ${TARGET_NAME} as a test")
else ()
add_test(NAME ${TARGET_NAME} COMMAND $<TARGET_FILE:${TARGET_NAME}>)
endif ()
endmacro ()
# Finalize target for all types
macro (final_target)
if ("${TARGET_LIB_TYPE}" STREQUAL "EXECUTABLE")
@@ -76,3 +94,16 @@ endmacro ()
macro (link_openssl)
target_link_libraries (${TARGET_NAME} ${OPENSSL_SSL_LIBRARY} ${OPENSSL_CRYPTO_LIBRARY})
endmacro ()
macro (link_zlib)
target_link_libraries (${TARGET_NAME} ${ZLIB_LIBRARIES})
endmacro ()
macro (include_subdirs PARENT)
file (GLOB SDIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "${PARENT}/*")
foreach (SUBDIR ${SDIRS})
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt")
add_subdirectory ("${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}")
endif ()
endforeach ()
endmacro()
+86
View File
@@ -0,0 +1,86 @@
/** \page faq FAQ
## Dependency Management
### Can WebSocket++ be used without Boost?
Yes. WebSocket++ only uses Boost features as polyfills for C++11 language features and libraries. If you have a C++11 compiler and standard library you can use WebSocket++ without Boost. In most cases setting your build environment to use the C++11 (or later) language dialect is sufficient to enable this mode of use.
With less common compilers (and sometimes very recently release compilers) there may be specific issues with certain libraries that aren't automatically detected by the library. For these situations there are additional defines available to fine tune which C++11 libraries and features are used. TODO: more details about them.
For the iostream/raw transport the C++11 standard library is sufficient. For the Asio based transports, there is no C++11 library that provides the networking capabilaties that Asio does. As such even with a C++11 build system, you will need a standalone copy of Asio to use if Boost Asio is not available.
### Can WebSocket++ be used with standalone Asio
Yes. The process is the same as used with standalone Asio itself. Define `ASIO_STANDALONE` before including Asio or WebSocket++ headers. You will need to download a copy of the Asio headers separately (http://www.think-async.com) and make sure they are in your build system's include path.
### Can WebSocket++ be used without TLS or OpenSSL?
Yes. When using the iostream/raw transport, there are no TLS features and OpenSSL is not required. When using the Asio transport TLS features are optional. You only need OpenSSL if you want to use TLS. You can only make or recieve encrypted connections (https/wss) if you have enabled TLS features.
Whether an Asio endpoint uses TLS or not is determined by its config template parameter. The default bundled `websocketpp::config::asio` and `websocketpp::config::asio_client` configs do not support TLS, the `websocketpp::config::asio_tls` and `websocketpp::config::asio_tls_client` do.
The `<websocketpp/config/asio.hpp>` and `<websocketpp/config/asio_client.hpp>` headers will include both the TLS and non-TLS varients of their respective configs and require the presence of OpenSSL. The `<websocketpp/config/asio_no_tls.hpp>` and `<websocketpp/config/asio_no_tls_client.hpp>` headers will include only the non-TLS configs and do not require OpenSSL.
## Compression
### How do I use permessage-deflate in version 0.6.0-permessagedeflate and 0.7.0?
These versions of the library require a custom config to use the permessage-deflate extension. Here is a minimal example of such a custom config. You can also integrate these lines into an existing custom config.
Note that in these versions there is no fine grained control over which connections are compressed or not. Clients will request compression with the default settings and use it if the server supports it. Servers will accept whatever parameters clients request.
Outgoing messages by default will be compressed if compression was auto-negotiated during the handshake. There is an option to force a specific message to be sent uncompressed even if compression was negotiated. This may be useful for sending data that you know to be compressed already (images, zip files, etc).
__Server Example__
```
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct deflate_server_config : public websocketpp::config::asio {
// ... additional custom config if you need it for other things
/// permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
};
typedef websocketpp::server<deflate_server_config> server_endpoint_type;
```
__Client Example__
```
#include <websocketpp/extensions/permessage_deflate/enabled.hpp>
struct deflate_client_config : public websocketpp::config::asio_client {
// ... additional custom config if you need it for other things
/// permessage_compress extension
struct permessage_deflate_config {};
typedef websocketpp::extensions::permessage_deflate::enabled
<permessage_deflate_config> permessage_deflate_type;
};
typedef websocketpp::client<deflate_client_config> client_endpoint_type;
```
## Security
### Is it possible to terminate a malicious connection quickly, without tying up resources performing clean close steps,
Yes. The library will automatically detect and terminate connections that violate the WebSocket protocol. In cases where the library believes the remote endpoint to be malicious or sufficiently broken to be unlikely to understand or process the closing handshake, it will be omited.
If your application detects conditions above the protocol level that you believe to be malicious, for example, if you recognize an IP from a known denial of service attack, you can close the connection with two different levels of urgency. Use the standard `websocketpp::endpoint::close` or `websocketpp::connection::close` methods with one of the following special close codes:
- `websocketpp::close::status::omit_handshake`: Omits the closing handshake, but cleanly closes the TCP connection.
- `websocketpp::close::status::force_tcp_drop`: Forcibly drop the TCP connection.
Please note that usage of these disconnect methods results in a violation of the WebSocket protocol and may have negative reprocusions for the remote endpoint with respect to network timeouts. Please use caution when using them.
## Build Issues
### Getting compile errors related to `std::chrono`, `boost::chrono`, `waitable_timer`, or `steady_clock`
Your build system may be confused about whether it is supposed to be using `boost::chrono` or `std::chrono`. Boost automatically detects this setup on some compilers but not others. Defining `BOOST_ASIO_HAS_STD_CHRONO` can help. See http://www.boost.org/doc/libs/1_60_0/doc/html/boost_asio/overview/cpp2011/chrono.html for more details.
*/
+27
View File
@@ -0,0 +1,27 @@
/** \page getting_started Getting Started
WebSocket++ code is available on github at https://github.com/zaphoyd/websocketpp
The official project homepage lives at http://www.zaphoyd.com/websocketpp
The git repository is organized into several directories:
- **docs**: This documentation
- **examples**: Example programs that demonstrate how to build basic versions of some commonly used patterns for WebSocket clients and servers.
- **test**: Unit tests that confirm that the code you have works properly and help detect platform specific issues.
- **tutorials**: Detailed walkthroughs of a select set of the example programs.
- **websocketpp**: All of the library code and default configuration files.
WebSocket++ is a header only library. You can start using it by including the websocketpp source directory in your project's include path and including the appropriate WebSocket++ headers in your program. You may also need to include and/or link to appropriate Boost/system libraries. TODO: More information: Building a program with WebSocket++, Walkthroughs of the example programs
WebSocket++ includes cmake and scons scripts for building the examples and unit tests. Neither system is needed unless you want to build tests or examples in an automated fashion.
__Usage questions__ should be posted to the project mailing list at http://groups.google.com/group/websocketpp/ or the IRC channel (\#websocketpp on freenode).
__Bugs and issues__ should be posted to the project GitHub issues queue: https://github.com/zaphoyd/websocketpp/issues.
__Pull requests__ on GitHub are welcome. Please make them against the `develop` branch.
WebSocket++ is written and maintained by Peter Thorson. You can contact me via GitHub messaging, IRC, or via email at websocket@zaphoyd.com.
*/
+165
View File
@@ -0,0 +1,165 @@
/** \page reference.handlers Handler Reference
Handlers allow WebSocket++ programs to receive notifications about events
that happen in relation to their connections. Some handlers also behave as
hooks that give the program a chance to modify state or adjust settings before
the connection continues.
Handlers are registered by calling the appropriate `set_*_handler` method on either an
endpoint or connection. The * refers to the name of the handler (as
specified in the signature field below). For example, to set the open handler,
call `set_open_handler(...)`.
Setting handlers on an endpoint will result in them being copied as the default
handler to all new connections created by that endpoint. Changing an endpoint's
handlers will not affect connections that are already in progress. This includes
connections that are in the listening state. As such, it is important to set any
endpoint handlers before you call `endpoint::start_accept` or else the handlers
will not be attached to your first connection.
Setting handlers on a connection will result in the handler being changed for
that connection only, starting at the next time that handler is called. This can
be used to change the handler during a connection.
Connection Handlers
-------------------
These handlers will be called at most once per connection in the order specified below.
### Socket Init Handler
| Event | Signature | Availability |
| --------------------- | ----------------------------------------------------- | -------------------- |
| Socket initialization | `socket_init(connection_hdl, asio::ip::tcp::socket&)` | 0.3.0 Asio Transport |
This hook is triggered after the socket has been initialized but before a connection is established.
It allows setting arbitrary socket options before connections are sent/recieved.
### TCP Pre-init Handler
| Event | Signature | Availability |
| ----------------------------- | ------------------------------ | -------------------- |
| TCP established, no data sent | `tcp_pre_init(connection_hdl)` | 0.3.0 Asio Transport |
This hook is triggered after the TCP connection is established, but before any pre-WebSocket-handshake
operations have been run. Common pre-handshake operations include TLS handshakes and proxy connections.
### TCP Post-init Handler
| Event | Signature | Availability |
| ----------------------- | ------------------------------------------ | ----------------------------- |
| Request for TLS context | `tls_context_ptr tls_init(connection_hdl)` | 0.3.0 Asio Transport with TLS |
This hook is triggered before the TLS handshake to request the TLS context to use. You must
return a pointer to a configured TLS conext to continue. This provides the opportuinity to
set up the TLS settings, certificates, etc.
### Validate Handler
| Event | Signature | Availability |
| ------------------------------------- | ------------------------------- | ---------------------------- |
| Hook to accept or reject a connection | `bool validate(connection_hdl)` | 0.3.0 Core, Server role only |
This hook is triggered for servers during the opening handshake after the request has been
processed but before the response has been sent. It gives a program the opportunity to inspect
headers and other connection details and either accept or reject the connection. Validate happens
before the open or fail handler.
Return true to accept the connection, false to reject. If no validate handler is registered,
all connections will be accepted.
### Open Connection Handler
| Event | Signature | Availability |
| ------------------------- | ---------------------- | ------------ |
| Successful new connection | `open(connection_hdl)` | 0.3.0 Core |
Either open or fail will be called for each connection. Never both. All
connections that begin with an open handler call will also have a matching
close handler call when the connection ends.
### Fail Connection Handler
| Event | Signature | Availability |
| ----------------------------------- | ---------------------- | ------------ |
| Connection failed (before opening) | `fail(connection_hdl)` | 0.3.0 Core |
Either open or fail will be called for each connection. Never both. Connections
that fail will never have a close handler called.
### Close Connection Handler
| Event | Signature | Availability |
| --------------------------------- | ----------------------- | ------------ |
| Connection closed (after opening) | `close(connection_hdl)` | 0.3.0 Core |
Close will be called exactly once for every connection that open was called for.
Close is not called for failed connections.
Message Handlers
----------------
These handers are called in response to incoming messages or message like events. They only will be called while the connection is in the open state.
### Message Handler
| Event | Signature | Availability |
| --------------------- | -------------------------------------- | ------------ |
| Data message recieved | `message(connection_hdl, message_ptr)` | 0.3.0 Core |
Applies to all non-control messages, including both text and binary opcodes. The
`message_ptr` type and its API depends on your endpoint type and its config.
### Ping Handler
| Event | Signature | Availability |
| ------------- | ---------------------------------------- | ------------ |
| Ping recieved | `bool ping(connection_hdl, std::string)` | 0.3.0 Core |
Second (string) argument is the binary ping payload. Handler return value
indicates whether or not to respond to the ping with a pong. If no ping handler
is set, WebSocket++ will respond with a pong containing the same binary data as
the ping (Per requirements in RFC6455).
### Pong Handler
| Event | Signature | Availability |
| ------------- | ----------------------------------- | ------------ |
| Pong recieved | `pong(connection_hdl, std::string)` | 0.3.0 Core |
Second (string) argument is the binary pong payload.
### Pong Timeout Handler
| Event | Signature | Availability |
| ---------------------------------- | ------------------------------------------- | ---------------------------------------- |
| Timed out while waiting for a pong | `pong_timeout(connection_hdl, std::string)` | 0.3.0 Core, transport with timer support |
Triggered if there is no response to a ping after the configured duration. The second
(string) argument is the binary payload of the unanswered ping.
### HTTP Handler
| Event | Signature | Availability |
| --------------------- | --------------------- | ---------------------------- |
| HTTP request recieved | `http(connection_hdl` | 0.3.0 Core, Server role only |
Called when HTTP requests that are not WebSocket handshake upgrade requests are
recieved. Allows responding to regular HTTP requests. If no handler is registered
a 426/Upgrade Required error is returned.
### Interrupt Handler
| Event | Signature | Availability |
| ----------------------------------- | --------------------------- | ------------ |
| Connection was manually interrupted | `interrupt(connection_hdl)` | 0.3.0 Core |
Interrupt events can be triggered by calling `endpoint::interrupt` or `connection::interrupt`.
Interrupt is similar to a timer event with duration zero but with lower overhead. It is useful
for single threaded programs to allow breaking up a very long handler into multiple parts and
for multi threaded programs as a way for worker threads to signale to the main/network thread
that an event is ready.
todo: write low and high watermark handlers
*/
+22
View File
@@ -0,0 +1,22 @@
.tabs, .tabs2, .tabs3, .navpath ul {
background-image: none;
background-color: #333;
border: none;
border-bottom: 1px solid #575757;
}
.tablist li, .navpath li {
background-image: none;
background-color: #333;
}
.tablist a, .navpath li.navelem a {
color: #ccc;
text-shadow: 0px 1px 1px black;
}
.tablist a:hover, .navpath li.navelem a:hover {
background-image: none;
background-color: #444;
color: #ccc;
}
+21
View File
@@ -0,0 +1,21 @@
/** \mainpage
WebSocket++ is a C++ library that can be used to implement WebSocket functionality. The goals of the project are to provide a WebSocket implementation that is portable, flexible, lightweight, low level, and high performance.
WebSocket++ does not intend to be used alone as a web application framework or full featured web services platform. As such the components, examples, and performance tuning are geared towards operation as a WebSocket client or server. There are some minimal convenience features that stray from this (for example the ability to respond to HTTP requests other than WebSocket Upgrades) but these are not the focus of the project. In particular WebSocket++ does not intend to implement any non-WebSocket related fallback options (ajax / long polling / comet / etc).
In order to remain compact and improve portability, the WebSocket++ project strives to reduce or eliminate external dependencies where possible and appropriate. WebSocket++ core has no dependencies other than the C++11 standard library. For non-C++11 compilers the Boost libraries provide drop in polyfills for the C++11 functionality used.
WebSocket++ implements a pluggable data transport component. The default component allows reduced functionality by using STL iostream or raw byte shuffling via reading and writing char buffers. This component has no non-STL dependencies and can be used in a C++11 environment without Boost. Also included is an Asio based transport component that provides full featured network client/server functionality. This component requires either Boost Asio or a C++11 compiler and standalone Asio. As an advanced option, WebSocket++ supports custom transport layers if you want to provide your own using another library.
In order to accommodate the wide variety of use cases WebSocket++ has collected, the library is built in a way that most of the major components are loosely coupled and can be swapped out and replaced. WebSocket++ will attempt to track the future development of the WebSocket protocol and any extensions as they are developed.
- \subpage getting_started "Getting Started"
- \subpage faq "FAQ"
- \subpage tutorials "Tutorials"
- \subpage md_changelog "Change Log / Version History"
- Reference
- \subpage reference.handlers "Handler Reference"
*/
+10
View File
@@ -0,0 +1,10 @@
/** \page tutorials Tutorials
These tutorials are works in progress, some are more complete than others.
- \subpage md_tutorials_utility_client_utility_client
- \subpage md_tutorials_utility_server_utility_server
- \subpage md_tutorials_broadcast_tutorial_broadcast_tutorial
- \subpage md_tutorials_chat_tutorial_chat_tutorial
*/
-6
View File
@@ -1,6 +0,0 @@
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 ()
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (associative_storage)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
@@ -30,7 +30,7 @@ public:
connection_data data;
data.sessionid = m_next_sessionid++;
data.name = "";
data.name.clear();
m_connections[hdl] = data;
}
@@ -47,7 +47,7 @@ public:
void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_data& data = get_data_from_hdl(hdl);
if (data.name == "") {
if (data.name.empty()) {
data.name = msg->get_payload();
std::cout << "Setting name of connection with sessionid "
<< data.sessionid << " to " << data.name << std::endl;
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (broadcast_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+19 -15
View File
@@ -19,6 +19,7 @@ using websocketpp::lib::bind;
using websocketpp::lib::thread;
using websocketpp::lib::mutex;
using websocketpp::lib::lock_guard;
using websocketpp::lib::unique_lock;
using websocketpp::lib::condition_variable;
@@ -71,27 +72,30 @@ public:
}
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();
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_open" << std::endl;
m_actions.push(action(SUBSCRIBE,hdl));
}
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();
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_close" << std::endl;
m_actions.push(action(UNSUBSCRIBE,hdl));
}
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();
{
lock_guard<mutex> guard(m_action_lock);
//std::cout << "on_message" << std::endl;
m_actions.push(action(MESSAGE,hdl,msg));
}
m_action_cond.notify_one();
}
@@ -109,13 +113,13 @@ public:
lock.unlock();
if (a.type == SUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
lock_guard<mutex> guard(m_connection_lock);
m_connections.insert(a.hdl);
} else if (a.type == UNSUBSCRIBE) {
unique_lock<mutex> con_lock(m_connection_lock);
lock_guard<mutex> guard(m_connection_lock);
m_connections.erase(a.hdl);
} else if (a.type == MESSAGE) {
unique_lock<mutex> con_lock(m_connection_lock);
lock_guard<mutex> guard(m_connection_lock);
con_list::iterator it;
for (it = m_connections.begin(); it != m_connections.end(); ++it) {
+17
View File
@@ -0,0 +1,17 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (OPENSSL_FOUND)
init_target (debug_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
endif()
+1
View File
@@ -76,6 +76,7 @@ public:
if (ec) {
m_endpoint.get_alog().write(websocketpp::log::alevel::app,ec.message());
return;
}
//con->set_proxy("http://humupdates.uchicago.edu:8443");
+2
View File
@@ -8,3 +8,5 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
#init_target (dev)
#build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
#link_boost ()
#final_target ()
#set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (echo_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+23
View File
@@ -0,0 +1,23 @@
## echo_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] + ['z']
prgs += env_cpp11.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system','random'],env) + [platform_libs] + [polyfill_libs] + ['z']
prgs += env.Program('echo_client', ["echo_client.cpp"], LIBS = ALL_LIBS)
Return('prgs')
+97
View File
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2016, 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_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;
// This message handler will be invoked once for each incoming message. It
// prints the message and then sends a copy of the message back to the server.
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
websocketpp::lib::error_code ec;
c->send(hdl, msg->get_payload(), msg->get_opcode(), ec);
if (ec) {
std::cout << "Echo failed because: " << ec.message() << std::endl;
}
}
int main(int argc, char* argv[]) {
// Create a client endpoint
client c;
std::string uri = "ws://localhost:9002";
if (argc == 2) {
uri = argv[1];
}
try {
// Set logging to be pretty verbose (everything except message payloads)
c.set_access_channels(websocketpp::log::alevel::all);
c.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
c.init_asio();
// Register our message handler
c.set_message_handler(bind(&on_message,&c,::_1,::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection(uri, ec);
if (ec) {
std::cout << "could not create connection because: " << ec.message() << std::endl;
return 0;
}
// Note that connect here only requests a connection. No network messages are
// exchanged until the event loop starts running in the next line.
c.connect(con);
// Start the ASIO io_service run loop
// this will cause a single connection to be made to the server. c.run()
// will exit when this connection is closed.
c.run();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
}
}
+2
View File
@@ -8,3 +8,5 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+1 -1
View File
@@ -43,7 +43,7 @@ int main() {
echo_server.set_access_channels(websocketpp::log::alevel::all);
echo_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize ASIO
// Initialize Asio
echo_server.init_asio();
// Register our message handler
+3
View File
@@ -12,4 +12,7 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
endif()
+3
View File
@@ -12,4 +12,7 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
endif()
+51 -12
View File
@@ -1,3 +1,39 @@
/*
* Copyright (c) 2015, 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.
*
*/
/**
* NOTES
*
* This example uses a number of standard classes through the websocketpp::lib
* namespace. This is to allow easy switching between Boost, the C++11 STL, and
* the standalone Asio library. Your program need not use these namespaces if
* you do not need this sort of flexibility.
*/
#include <websocketpp/config/asio.hpp>
#include <websocketpp/server.hpp>
@@ -12,7 +48,7 @@ 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;
typedef websocketpp::lib::shared_ptr<websocketpp::lib::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()
@@ -46,27 +82,30 @@ enum tls_mode {
};
context_ptr on_tls_init(tls_mode mode, websocketpp::connection_hdl hdl) {
namespace asio = websocketpp::lib::asio;
std::cout << "on_tls_init called with hdl: " << hdl.lock().get() << std::endl;
std::cout << "using TLS mode: " << (mode == MOZILLA_MODERN ? "Mozilla Modern" : "Mozilla Intermediate") << std::endl;
context_ptr ctx = websocketpp::lib::make_shared<boost::asio::ssl::context>(boost::asio::ssl::context::sslv23);
context_ptr ctx = websocketpp::lib::make_shared<asio::ssl::context>(asio::ssl::context::sslv23);
try {
if (mode == MOZILLA_MODERN) {
// Modern disables TLSv1
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3 |
boost::asio::ssl::context::no_tlsv1 |
boost::asio::ssl::context::single_dh_use);
ctx->set_options(asio::ssl::context::default_workarounds |
asio::ssl::context::no_sslv2 |
asio::ssl::context::no_sslv3 |
asio::ssl::context::no_tlsv1 |
asio::ssl::context::single_dh_use);
} else {
ctx->set_options(boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::no_sslv2 |
boost::asio::ssl::context::no_sslv3 |
boost::asio::ssl::context::single_dh_use);
ctx->set_options(asio::ssl::context::default_workarounds |
asio::ssl::context::no_sslv2 |
asio::ssl::context::no_sslv3 |
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);
ctx->use_private_key_file("server.pem", asio::ssl::context::pem);
// Example method of generating this file:
// `openssl dhparam -out dh.pem 2048`
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (enriched_storage)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
@@ -61,7 +61,7 @@ public:
void on_message(connection_hdl hdl, server::message_ptr msg) {
connection_ptr con = m_server.get_con_from_hdl(hdl);
if (con->name == "") {
if (con->name.empty()) {
con->name = msg->get_payload();
std::cout << "Setting name of connection with sessionid "
<< con->sessionid << " to " << con->name << std::endl;
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (external_io_service)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+23
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('external_io_service', ["external_io_service.cpp"], LIBS = ALL_LIBS)
else:
ALL_LIBS = boostlibs(['system'],env) + [platform_libs] + [polyfill_libs]
prgs += env.Program('external_io_service', ["external_io_service.cpp"], LIBS = ALL_LIBS)
Return('prgs')
@@ -0,0 +1,85 @@
/*
* Copyright (c) 2015, 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 "tcp_echo_server.hpp"
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <iostream>
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::server<websocketpp::config::asio> ws_echo_server;
// Define a callback to handle incoming messages
void on_message(ws_echo_server* s, websocketpp::connection_hdl hdl, ws_echo_server::message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
// check for a special command to instruct the server to stop listening so
// it can be cleanly exited.
if (msg->get_payload() == "stop-listening") {
s->stop_listening();
return;
}
try {
s->send(hdl, msg->get_payload(), msg->get_opcode());
} catch (websocketpp::lib::error_code const & e) {
std::cout << "Echo failed because: " << e
<< "(" << e.message() << ")" << std::endl;
}
}
int main() {
asio::io_service service;
// Add a TCP echo server on port 9003
tcp_echo_server custom_http_server(service, 9003);
// Add a WebSocket echo server on port 9002
ws_echo_server ws_server;
ws_server.set_access_channels(websocketpp::log::alevel::all);
ws_server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// The only difference in this code between an internal and external
// io_service is the different constructor to init_asio
ws_server.init_asio(&service);
// Register our message handler
ws_server.set_message_handler(bind(&on_message,&ws_server,::_1,::_2));
ws_server.listen(9002);
ws_server.start_accept();
// TODO: add a timer?
// Start the Asio io_service run loop for all
service.run();
}
@@ -0,0 +1,97 @@
/*
* Copyright (c) 2015, 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.
*/
/**
* TCP Echo Server
*
* This file defines a simple TCP Echo Server. It is adapted from the Asio
* example: cpp03/echo/async_tcp_echo_server.cpp
*/
#include <websocketpp/common/asio.hpp>
#include <websocketpp/common/memory.hpp>
#include <websocketpp/common/functional.hpp>
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
namespace asio = websocketpp::lib::asio;
struct tcp_echo_session : websocketpp::lib::enable_shared_from_this<tcp_echo_session> {
typedef websocketpp::lib::shared_ptr<tcp_echo_session> ptr;
tcp_echo_session(asio::io_service & service) : m_socket(service) {}
void start() {
m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)),
websocketpp::lib::bind(
&tcp_echo_session::handle_read, shared_from_this(), _1, _2));
}
void handle_read(const asio::error_code & ec, size_t transferred) {
if (!ec) {
asio::async_write(m_socket,
asio::buffer(m_buffer, transferred),
bind(&tcp_echo_session::handle_write, shared_from_this(), _1));
}
}
void handle_write(const asio::error_code & ec) {
if (!ec) {
m_socket.async_read_some(asio::buffer(m_buffer, sizeof(m_buffer)),
bind(&tcp_echo_session::handle_read, shared_from_this(), _1, _2));
}
}
asio::ip::tcp::socket m_socket;
char m_buffer[1024];
};
struct tcp_echo_server {
tcp_echo_server(asio::io_service & service, short port)
: m_service(service)
, m_acceptor(service, asio::ip::tcp::endpoint(asio::ip::tcp::v6(), port))
{
this->start_accept();
}
void start_accept() {
tcp_echo_session::ptr new_session(new tcp_echo_session(m_service));
m_acceptor.async_accept(new_session->m_socket,
bind(&tcp_echo_server::handle_accept, this, new_session, _1));
}
void handle_accept(tcp_echo_session::ptr new_session, const asio::error_code & ec) {
if (!ec) {
new_session->start();
}
start_accept();
}
asio::io_service & m_service;
asio::ip::tcp::acceptor m_acceptor;
};
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (handler_switch)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (iostream_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+2
View File
@@ -8,3 +8,5 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+1 -1
View File
@@ -246,7 +246,7 @@ int main() {
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason = "";
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (simple_broadcast_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+1
View File
@@ -9,3 +9,4 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+1 -1
View File
@@ -12,7 +12,7 @@ env_cpp11 = env_cpp11.Clone ()
prgs = []
# if a C++11 environment is avaliable build using that, otherwise use boost
# 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('sip_client', ["sip_client.cpp"], LIBS = ALL_LIBS)
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (subprotocol_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+2
View File
@@ -8,3 +8,5 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
+2
View File
@@ -8,3 +8,5 @@ build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
@@ -32,7 +32,6 @@ class telemetry_server {
public:
typedef websocketpp::connection_hdl connection_hdl;
typedef websocketpp::server<websocketpp::config::asio> server;
typedef websocketpp::lib::lock_guard<websocketpp::lib::mutex> scoped_lock;
telemetry_server() : m_count(0) {
// set up access channels to only log interesting things
@@ -112,7 +111,7 @@ public:
server::connection_ptr con = m_endpoint.get_con_from_hdl(hdl);
std::ifstream file;
std::string filename = con->get_uri()->get_resource();
std::string filename = con->get_resource();
std::string response;
m_endpoint.get_alog().write(websocketpp::log::alevel::app,
@@ -201,4 +200,4 @@ int main(int argc, char* argv[]) {
s.run(docroot, port);
return 0;
}
}
+6
View File
@@ -2,10 +2,16 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (ZLIB_FOUND)
init_target (testee_client)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_zlib()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
endif()
+17
View File
@@ -0,0 +1,17 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (ZLIB_FOUND)
init_target (testee_server)
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_zlib()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
endif()
+8 -18
View File
@@ -1,23 +1,13 @@
## Utility client example
##
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
init_target (utility_client)
prgs = []
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
# 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)
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "examples")
Return('prgs')
+18 -6
View File
@@ -1,11 +1,23 @@
## Utility client example
##
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
Import('env')
Import('env_cpp11')
Import('boostlibs')
Import('platform_libs')
Import('polyfill_libs')
init_target (utility_client)
env = env.Clone ()
env_cpp11 = env_cpp11.Clone ()
build_executable (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
prgs = []
link_boost ()
final_target ()
# 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')
+2 -2
View File
@@ -289,7 +289,7 @@ int main() {
std::string cmd;
int id;
std::string message = "";
std::string message;
ss >> cmd >> id;
std::getline(ss,message);
@@ -301,7 +301,7 @@ int main() {
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason = "";
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
+1 -1
View File
@@ -1,4 +1,4 @@
WebSocket++ (0.6.0)
WebSocket++ (0.7.0)
==========================
WebSocket++ is a header only C++ library that implements RFC6455 The WebSocket
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (test_connection)
build_test (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+40 -35
View File
@@ -159,12 +159,17 @@ bool validate_set_ua(server* s, websocketpp::connection_hdl hdl) {
}
void http_func(server* s, websocketpp::connection_hdl hdl) {
using namespace websocketpp::http;
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);
con->set_status(status_code::ok);
BOOST_CHECK_EQUAL(con->get_response_code(), status_code::ok);
BOOST_CHECK_EQUAL(con->get_response_msg(), status_code::get_string(status_code::ok));
}
void defer_http_func(server* s, bool * deferred, websocketpp::connection_hdl hdl) {
@@ -376,31 +381,31 @@ BOOST_AUTO_TEST_CASE( websocket_fail_unsupported_version ) {
BOOST_CHECK(called);
}
/*BOOST_AUTO_TEST_CASE( websocket_fail_invalid_uri ) {
std::string input = "GET http://345.123.123.123/foo 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";
// BOOST_AUTO_TEST_CASE( websocket_fail_invalid_uri ) {
// std::string input = "GET http://345.123.123.123/foo 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";
server s;
websocketpp::lib::error_code ec = make_error_code(websocketpp::error::unsupported_version);
bool called = false;
s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
s.set_open_handler(bind(&on_open_print,&s,::_1));
// server s;
// websocketpp::lib::error_code ec = make_error_code(websocketpp::error::unsupported_version);
// bool called = false;
// s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
// s.set_open_handler(bind(&on_open_print,&s,::_1));
std::cout << run_server_test(s,input,true) << std::endl;
BOOST_CHECK(called);
}
// std::cout << run_server_test(s,input,true) << std::endl;
// BOOST_CHECK(called);
// }
BOOST_AUTO_TEST_CASE( websocket_fail_invalid_uri_http ) {
std::string input = "GET http://345.123.123.123/foo HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
// BOOST_AUTO_TEST_CASE( websocket_fail_invalid_uri_http ) {
// std::string input = "GET http://345.123.123.123/foo HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
server s;
websocketpp::lib::error_code ec = make_error_code(websocketpp::error::unsupported_version);
bool called = false;
s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
s.set_open_handler(bind(&on_open_print,&s,::_1));
// server s;
// websocketpp::lib::error_code ec = make_error_code(websocketpp::error::unsupported_version);
// bool called = false;
// s.set_fail_handler(bind(&check_on_fail,&s,ec,websocketpp::lib::ref(called),::_1));
// s.set_open_handler(bind(&on_open_print,&s,::_1));
std::cout << run_server_test(s,input,true) << std::endl;
BOOST_CHECK(called);
}*/
// std::cout << run_server_test(s,input,true) << std::endl;
// BOOST_CHECK(called);
// }
BOOST_AUTO_TEST_CASE( websocket_fail_upgrade_required ) {
std::string input = "GET /foo/bar HTTP/1.1\r\nHost: www.example.com\r\nOrigin: http://www.example.com\r\n\r\n";
@@ -418,26 +423,26 @@ BOOST_AUTO_TEST_CASE( websocket_fail_upgrade_required ) {
// 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( 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_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";
// BOOST_CHECK(run_server_test(input) == output);
// }
unsigned char frames[8] = {0x82,0x82,0xFF,0xFF,0xFF,0xFF,0xD5,0xD5};
input.append(reinterpret_cast<char*>(frames),8);
// 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";
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**";
// 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);
// }
BOOST_CHECK( run_server_test(input) == output);
}
*/
+17
View File
@@ -0,0 +1,17 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
if (OPENSSL_FOUND)
init_target (test_endpoint)
build_test (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
link_openssl ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
endif()
+22
View File
@@ -0,0 +1,22 @@
# Extension Tests
file (GLOB SOURCE extension.cpp)
init_target (test_extension)
build_executable (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
if ( ZLIB_FOUND )
# Permessage-deflate tests
file (GLOB SOURCE permessage_deflate.cpp)
init_target (test_permessage_deflate)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_zlib()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
endif ( ZLIB_FOUND )
+23 -23
View File
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE( negotiate_server_no_context_takeover_invalid ) {
BOOST_AUTO_TEST_CASE( negotiate_server_no_context_takeover ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
@@ -144,7 +144,7 @@ BOOST_AUTO_TEST_CASE( negotiate_client_no_context_takeover_invalid ) {
BOOST_AUTO_TEST_CASE( negotiate_client_no_context_takeover ) {
ext_vars v;
v.attr["client_no_context_takeover"] = "";
v.attr["client_no_context_takeover"].clear();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
@@ -282,7 +282,7 @@ BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_invalid ) {
BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_valid ) {
ext_vars v;
v.attr["client_max_window_bits"] = "";
v.attr["client_max_window_bits"].clear();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
BOOST_CHECK_EQUAL( v.esp.first, websocketpp::lib::error_code() );
@@ -364,8 +364,8 @@ BOOST_AUTO_TEST_CASE( negotiate_client_max_window_bits_smallest ) {
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated1 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["client_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["client_no_context_takeover"].clear();
v.esp = v.exts.negotiate(v.attr);
BOOST_CHECK( v.exts.is_enabled() );
@@ -376,7 +376,7 @@ BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated1 ) {
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated2 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -388,7 +388,7 @@ BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated2 ) {
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated3 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["client_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -400,7 +400,7 @@ BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated3 ) {
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated4 ) {
ext_vars v;
v.attr["client_no_context_takeover"] = "";
v.attr["client_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -412,7 +412,7 @@ BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated4 ) {
BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated5 ) {
ext_vars v;
v.attr["client_no_context_takeover"] = "";
v.attr["client_no_context_takeover"].clear();
v.attr["client_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -436,8 +436,8 @@ BOOST_AUTO_TEST_CASE( negotiate_two_client_initiated6 ) {
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated1 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["client_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["client_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -449,8 +449,8 @@ BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated1 ) {
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated2 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["client_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["client_no_context_takeover"].clear();
v.attr["client_max_window_bits"] = "10";
v.esp = v.exts.negotiate(v.attr);
@@ -462,7 +462,7 @@ BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated2 ) {
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated3 ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.attr["client_max_window_bits"] = "10";
@@ -475,7 +475,7 @@ BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated3 ) {
BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated4 ) {
ext_vars v;
v.attr["client_no_context_takeover"] = "";
v.attr["client_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.attr["client_max_window_bits"] = "10";
@@ -488,8 +488,8 @@ BOOST_AUTO_TEST_CASE( negotiate_three_client_initiated4 ) {
BOOST_AUTO_TEST_CASE( negotiate_four_client_initiated ) {
ext_vars v;
v.attr["server_no_context_takeover"] = "";
v.attr["client_no_context_takeover"] = "";
v.attr["server_no_context_takeover"].clear();
v.attr["client_no_context_takeover"].clear();
v.attr["server_max_window_bits"] = "10";
v.attr["client_max_window_bits"] = "10";
@@ -569,7 +569,7 @@ BOOST_AUTO_TEST_CASE( compress_data_no_context_takeover ) {
websocketpp::http::attribute_list alist;
alist["server_no_context_takeover"] = "";
alist["server_no_context_takeover"].clear();
v.exts.enable_server_no_context_takeover();
v.exts.negotiate(alist);
@@ -586,7 +586,7 @@ BOOST_AUTO_TEST_CASE( compress_data_no_context_takeover ) {
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
BOOST_CHECK_EQUAL( compress_in, decompress_out );
decompress_out = "";
decompress_out.clear();
v.ec = v.exts.compress(compress_in,compress_out2);
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
@@ -605,7 +605,7 @@ BOOST_AUTO_TEST_CASE( compress_data_no_context_takeover ) {
BOOST_AUTO_TEST_CASE( compress_empty ) {
ext_vars v;
std::string compress_in = "";
std::string compress_in;
std::string compress_out;
std::string decompress_out;
@@ -616,8 +616,8 @@ BOOST_AUTO_TEST_CASE( compress_empty ) {
v.ec = v.exts.decompress(reinterpret_cast<const uint8_t *>(compress_out.data()),compress_out.size(),decompress_out);
compress_out = "";
decompress_out = "";
compress_out.clear();
decompress_out.clear();
v.ec = v.exts.compress(compress_in,compress_out);
BOOST_CHECK_EQUAL( v.ec, websocketpp::lib::error_code() );
@@ -637,7 +637,7 @@ BOOST_AUTO_TEST_CASE( decompress_data ) {
ext_vars v;
uint8_t in[11] = {0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x07, 0x00, 0x00, 0x00, 0xff, 0xff};
std::string out = "";
std::string out;
std::string reference = "Hello";
v.exts.init(true);
+11
View File
@@ -0,0 +1,11 @@
file (GLOB SOURCE_FILES parser.cpp)
init_target (test_http)
build_test (${TARGET_NAME} ${SOURCE_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+9 -7
View File
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE( extract_token ) {
BOOST_CHECK( ret.second == d1.begin()+3 );
ret = websocketpp::http::parser::extract_token(d2.begin(),d2.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.first.empty() );
BOOST_CHECK( ret.second == d2.begin()+0 );
ret = websocketpp::http::parser::extract_token(d2.begin()+1,d2.end());
@@ -127,7 +127,7 @@ 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 d4;
std::string d5 = "foo";
std::pair<std::string,std::string::const_iterator> ret;
@@ -147,11 +147,11 @@ BOOST_AUTO_TEST_CASE( extract_quoted_string ) {
BOOST_CHECK( ret.second == d3.begin()+5 );
ret = extract_quoted_string(d4.begin(),d4.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.first.empty() );
BOOST_CHECK( ret.second == d4.begin() );
ret = extract_quoted_string(d5.begin(),d5.end());
BOOST_CHECK( ret.first == "" );
BOOST_CHECK( ret.first.empty() );
BOOST_CHECK( ret.second == d5.begin() );
}
@@ -185,7 +185,7 @@ BOOST_AUTO_TEST_CASE( extract_all_lws ) {
}
BOOST_AUTO_TEST_CASE( extract_attributes_blank ) {
std::string s = "";
std::string s;
websocketpp::http::attribute_list a;
std::string::const_iterator it;
@@ -209,7 +209,7 @@ BOOST_AUTO_TEST_CASE( extract_attributes_simple ) {
}
BOOST_AUTO_TEST_CASE( extract_parameters ) {
std::string s1 = "";
std::string s1;
std::string s2 = "foo";
std::string s3 = " foo \r\nAbc";
std::string s4 = " \r\n foo ";
@@ -365,6 +365,7 @@ BOOST_AUTO_TEST_CASE( strip_lws ) {
std::string test6 = " \r\n foo ";
std::string test7 = " \t foo ";
std::string test8 = " \t ";
std::string test9 = " \n\r";
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test1), "foo" );
BOOST_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test2), "foo" );
@@ -374,6 +375,7 @@ BOOST_AUTO_TEST_CASE( strip_lws ) {
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_CHECK_EQUAL( websocketpp::http::parser::strip_lws(test9), "" );
}
BOOST_AUTO_TEST_CASE( case_insensitive_headers ) {
@@ -408,7 +410,7 @@ BOOST_AUTO_TEST_CASE( case_insensitive_headers_overwrite ) {
BOOST_AUTO_TEST_CASE( blank_consume ) {
websocketpp::http::parser::request r;
std::string raw = "";
std::string raw;
bool exception = false;
+12
View File
@@ -0,0 +1,12 @@
file (GLOB SOURCE_FILES *.cpp)
file (GLOB HEADER_FILES *.hpp)
init_target (test_logger)
build_test (${TARGET_NAME} ${SOURCE_FILES} ${HEADER_FILES})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+17
View File
@@ -0,0 +1,17 @@
# Test alloc message buffer strategy
file (GLOB SOURCE alloc.cpp)
init_target (test_message_alloc)
build_executable (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test message buffers
file (GLOB SOURCE message.cpp)
init_target (test_message_buffer)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+59
View File
@@ -0,0 +1,59 @@
# Generic processor tests
file (GLOB SOURCE processor.cpp)
init_target (test_processor)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Hybi00 processor tests
file (GLOB SOURCE hybi00.cpp)
init_target (test_processor_hybi00)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Hybi07 processor tests
file (GLOB SOURCE hybi07.cpp)
init_target (test_processor_hybi07)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Hybi08 processor tests
file (GLOB SOURCE hybi08.cpp)
init_target (test_processor_hybi08)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
if (ZLIB_FOUND)
# Hybi13 processor tests
file (GLOB SOURCE hybi13.cpp)
init_target (test_processor_hybi13)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_zlib()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Permessage compression extension processor tests
file (GLOB SOURCE extension_permessage_compress.cpp)
init_target (test_processor_extension_permessage_compress)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_zlib()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
endif ( ZLIB_FOUND )
@@ -165,7 +165,7 @@ BOOST_AUTO_TEST_CASE( deflate_init ) {
<< websocketpp::utility::to_hex(test_out,test_out_size) << std::endl;
std::string input = "Hello";
std::string output = "";
std::string output;
ec = de.compress(input,output);
BOOST_CHECK( ec == processor::extensions::error::uninitialized );
@@ -181,7 +181,7 @@ BOOST_AUTO_TEST_CASE( deflate_init ) {
<< websocketpp::utility::to_hex(input) << std::endl
<< websocketpp::utility::to_hex(output) << std::endl;
output = "";
output.clear();
ec = de.compress(input,output);
std::cout << ec.message() << std::endl
@@ -189,7 +189,7 @@ BOOST_AUTO_TEST_CASE( deflate_init ) {
<< websocketpp::utility::to_hex(output) << std::endl;
input = output;
output = "";
output.clear();
ec = de.decompress(input,output);
std::cout << ec.message() << std::endl
<< websocketpp::utility::to_hex(input) << std::endl
+17
View File
@@ -0,0 +1,17 @@
# Test RNG policy none
file (GLOB SOURCE none.cpp)
init_target (test_random_none)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test RNG policy random_device
file (GLOB SOURCE random_device.cpp)
init_target (test_random_random_device)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+17
View File
@@ -0,0 +1,17 @@
# Test client role
file (GLOB SOURCE client.cpp)
init_target (test_roles_client)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test server role
file (GLOB SOURCE server.cpp)
init_target (test_roles_server)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+1 -1
View File
@@ -103,7 +103,7 @@ bool validate_func_subprotocol(server* s, std::string* out, std::string accept,
*out = o.str();
if (accept != "") {
if (!accept.empty()) {
con->select_subprotocol(accept);
}
+71
View File
@@ -0,0 +1,71 @@
if (OPENSSL_FOUND)
# Test transport integration
file (GLOB SOURCE integration.cpp)
init_target (test_transport)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio timers
file (GLOB SOURCE asio/timers.cpp)
init_target (test_transport_asio_timers)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio security
file (GLOB SOURCE asio/security.cpp)
init_target (test_transport_asio_security)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
link_openssl()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
endif()
# Test transport iostream base
file (GLOB SOURCE iostream/base.cpp)
init_target (test_transport_iostream_base)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport iostream endpoint
file (GLOB SOURCE iostream/endpoint.cpp)
init_target (test_transport_iostream_endpoint)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport iostream connection
file (GLOB SOURCE iostream/connection.cpp)
init_target (test_transport_iostream_connection)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test transport asio base
file (GLOB SOURCE asio/base.cpp)
init_target (test_transport_asio_base)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+4
View File
@@ -15,14 +15,18 @@ BOOST_LIBS = boostlibs(['unit_test_framework','system','thread','chrono'],env) +
objs = env.Object('base_boost.o', ["base.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('timers_boost.o', ["timers.cpp"], LIBS = BOOST_LIBS)
objs += env.Object('security_boost.o', ["security.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)
prgs += env.Program('test_security_boost', ["security_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)
objs += env_cpp11.Object('security_stl.o', ["security.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)
prgs += env_cpp11.Program('test_security_stl', ["security_stl.o"], LIBS = BOOST_LIBS_CPP11)
Return('prgs')
+69
View File
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2015, 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/common/type_traits.hpp>
#include <websocketpp/transport/asio/security/none.hpp>
#include <websocketpp/transport/asio/security/tls.hpp>
template <typename base>
struct dummy_con : public base {
websocketpp::lib::error_code test() {
return this->translate_ec(websocketpp::lib::asio::error_code());
}
};
BOOST_AUTO_TEST_CASE( translated_ec_none ) {
dummy_con<websocketpp::transport::asio::basic_socket::connection> tscon;
// If the current configuration settings result in the library error type and the asio
// error type being the same, then the code should pass through natively. Otherwise
// we should get a generic pass through error.
if(websocketpp::lib::is_same<websocketpp::lib::error_code,websocketpp::lib::asio::error_code>::value) {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::lib::error_code() );
} else {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::transport::error::make_error_code(websocketpp::transport::error::pass_through) );
}
}
BOOST_AUTO_TEST_CASE( translated_ec_tls ) {
dummy_con<websocketpp::transport::asio::tls_socket::connection> tscon;
// If the current configuration settings result in the library error type and the asio
// error type being the same, then the code should pass through natively. Otherwise
// we should get a generic pass through error.
if(websocketpp::lib::is_same<websocketpp::lib::error_code,websocketpp::lib::asio::error_code>::value) {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::lib::error_code() );
} else {
BOOST_CHECK_EQUAL( tscon.test(), websocketpp::transport::error::make_error_code(websocketpp::transport::error::pass_through) );
}
}
+1 -1
View File
@@ -108,7 +108,7 @@ struct config {
// 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));
return context_ptr(new boost::asio::ssl::context(boost::asio::ssl::context::sslv23));
}
// Mock connection
+53
View File
@@ -0,0 +1,53 @@
# Test close utilities
file (GLOB SOURCE close.cpp)
init_target (test_close)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test error utilities
file (GLOB SOURCE error.cpp)
init_target (test_error)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test frame utilities
file (GLOB SOURCE frame.cpp)
init_target (test_frame)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test sha1 utilities
file (GLOB SOURCE sha1.cpp)
init_target (test_sha1)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test uri utilities
file (GLOB SOURCE uri.cpp)
init_target (test_uri)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
# Test misc utilities
file (GLOB SOURCE utilities.cpp)
init_target (test_utilities)
build_test (${TARGET_NAME} ${SOURCE})
link_boost ()
final_target ()
set_target_properties(${TARGET_NAME} PROPERTIES FOLDER "test")
+4 -4
View File
@@ -88,7 +88,7 @@ BOOST_AUTO_TEST_CASE( value_extraction ) {
BOOST_AUTO_TEST_CASE( extract_empty ) {
lib::error_code ec;
std::string payload = "";
std::string payload;
BOOST_CHECK( close::extract_code(payload,ec) == close::status::no_status );
BOOST_CHECK( !ec );
@@ -109,12 +109,12 @@ BOOST_AUTO_TEST_CASE( extract_reason ) {
BOOST_CHECK( close::extract_reason(payload,ec) == "Foo" );
BOOST_CHECK( !ec );
payload = "";
BOOST_CHECK( close::extract_reason(payload,ec) == "" );
payload.clear();
BOOST_CHECK( close::extract_reason(payload,ec).empty() );
BOOST_CHECK( !ec );
payload = "00";
BOOST_CHECK( close::extract_reason(payload,ec) == "" );
BOOST_CHECK( close::extract_reason(payload,ec).empty() );
BOOST_CHECK( !ec );
payload = "000";
+1 -1
View File
@@ -246,7 +246,7 @@ int main() {
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason = "";
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
+2 -2
View File
@@ -289,7 +289,7 @@ int main() {
std::string cmd;
int id;
std::string message = "";
std::string message;
ss >> cmd >> id;
std::getline(ss,message);
@@ -301,7 +301,7 @@ int main() {
std::string cmd;
int id;
int close_code = websocketpp::close::status::normal;
std::string reason = "";
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
+56 -57
View File
@@ -1,5 +1,5 @@
Utility Client Example Application
==================================
Utility Client Example Application Tutorial
===========================================
Chapter 1: Initial Setup & Basics
---------------------------------
@@ -17,7 +17,7 @@ A basic program loop that prompts the user for a command and then processes it.
*note* A code snapshot for each step is present next to this tutorial file in the git repository.
```cpp
~~~{.cpp}
#include <iostream>
#include <string>
@@ -44,7 +44,7 @@ int main() {
return 0;
}
```
~~~
### Step 2
@@ -61,7 +61,7 @@ Connections do not maintain a link back to their associated endpoint. Endpoints
WebSocket++ endpoints are built by combining an endpoint role with an endpoint config. There are two different types of endpoint roles, one each for the client and server roles in a WebSocket session. This is a client tutorial so we will use the client role `websocketpp::client` which is provided by the `<websocketpp/client.hpp>` header.
> ###### Terminology: Endpoint Config
> ##### Terminology: Endpoint Config
> WebSocket++ endpoints have a group of settings that may be configured at compile time via the `config` template parameter. A config is a struct that contains types and static constants that are used to produce an endpoint with specific properties. Depending on which config is being used the endpoint will have different methods available and may have additional third party dependencies.
The endpoint role takes a template parameter called `config` that is used to configure the behavior of endpoint at compile time. For this example we are going to use a default config provided by the library called `asio_client`, provided by `<websocketpp/config/asio_no_tls_client.hpp>`. This is a client config that uses boost::asio to provide network transport and does not support TLS based security. Later on we will discuss how to introduce TLS based security into a WebSocket++ application, more about the other stock configs, and how to build your own custom configs.
@@ -78,7 +78,7 @@ In addition to the new headers, boost::asio depends on the `boost_system` shared
`clang++ step2.cpp -lboost_system`
#### Code so far
```cpp
~~~{.cpp}
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
@@ -110,8 +110,7 @@ int main() {
return 0;
}
```
~~~
### Step 3
@@ -119,7 +118,7 @@ _Create endpoint wrapper object that handles initialization and setting up the b
In order to process user input while network processing occurs in the background we are going to use a separate thread for the WebSocket++ processing loop. This leaves the main thread free to process foreground user input. In order to enable simple RAII style resource management for our thread and endpoint we will use a wrapper object that configures them both in its constructor.
> ###### Terminology: websocketpp::lib namespace
> ##### Terminology: websocketpp::lib namespace
> WebSocket++ is designed to be used with a C++11 standard library. As this is not universally available in popular build systems the Boost libraries may be used as polyfills for the C++11 standard library in C++98 build environments. The `websocketpp::lib` namespace is used by the library and its associated examples to abstract away the distinctions between the two. `websocketpp::lib::shared_ptr` will evaluate to `std::shared_ptr` in a C++11 environment and `boost::shared_ptr` otherwise.
>
> This tutorial uses the `websocketpp::lib` wrappers because it doesn't know what the build environment of the reader is. For your applications, unless you are interested in similar portability, are free to use the boost or std versions of these types directly.
@@ -129,21 +128,21 @@ In order to process user input while network processing occurs in the background
Within the `websocket_endpoint` constructor several things happen:
First, we set the endpoint logging behavior to silent by clearing all of the access and error logging channels. [TODO: link to more information about logging]
```cpp
~~~{.cpp}
m_endpoint.clear_access_channels(websocketpp::log::alevel::all);
m_endpoint.clear_error_channels(websocketpp::log::elevel::all);
```
~~~
Next, we initialize the transport system underlying the endpoint and set it to perpetual mode. In perpetual mode the endpoint's processing loop will not exit automatically when it has no connections. This is important because we want this endpoint to remain active while our application is running and process requests for new WebSocket connections on demand as we need them. Both of these methods are specific to the asio transport. They will not be necessary or present in endpoints that use a non-asio config.
```cpp
~~~{.cpp}
m_endpoint.init_asio();
m_endpoint.start_perpetual();
```
~~~
Finally, we launch a thread to run the `run` method of our client endpoint. While the endpoint is running it will process connection tasks (read and deliver incoming messages, frame and send outgoing messages, etc). Because it is running in perpetual mode, when there are no connections active it will wait for a new connection.
```cpp
~~~{.cpp}
m_thread.reset(new websocketpp::lib::thread(&client::run, &m_endpoint));
```
~~~
#### Build
@@ -163,7 +162,7 @@ Now that our client endpoint template is actually instantiated a few more linker
#### Code so far
```cpp
~~~{.cpp}
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
@@ -215,7 +214,7 @@ int main() {
return 0;
}
```
~~~
### Step 4
@@ -234,14 +233,14 @@ The `websocket_endpoint` object has gained some new data members and methods. It
#### The connect method
A new WebSocket connection is initiated via a three step process. First, a connection request is created by `endpoint::get_connection(uri)`. Next, the connection request is configured. Lastly, the connection request is submitted back to the endpoint via `endpoint::connect()` which adds it to the queue of new connections to make.
> ###### Terminology `connection_ptr`
> ##### Terminology `connection_ptr`
> WebSocket++ keeps track of connection related resources using a reference counted shared pointer. The type of this pointer is `endpoint::connection_ptr`. A `connection_ptr` allows direct access to information about the connection and allows changing connection settings. Because of this direct access and their internal resource management role within the library it is not safe for end applications to use `connection_ptr` except in the specific circumstances detailed below.
>
> **When is it safe to use `connection_ptr`?**
> - After `endpoint::get_connection(...)` and before `endpoint::connect()`: `get_connection` returns a `connection_ptr`. It is safe to use this pointer to configure your new connection. Once you submit the connection to `connect` you may no longer use the `connection_ptr` and should discard it immediately for optimal memory management.
> - During a handler: WebSocket++ allows you to register hooks / callbacks / event handlers for specific events that happen during a connection's lifetime. During the invocation of one of these handlers the library guarantees that it is safe to use a `connection_ptr` for the connection associated with the currently running handler.
> ###### Terminology `connection_hdl`
> ##### Terminology `connection_hdl`
> Because of the limited thread safety of the `connection_ptr` the library also provides a more flexible connection identifier, the `connection_hdl`. The `connection_hdl` has type `websocketpp::connection_hdl` and it is defined in `<websocketpp/common/connection_hdl.hpp>`. Note that unlike `connection_ptr` this is not dependent on the type or config of the endpoint. Code that simply stores or transmits `connection_hdl` but does not use them can include only the header above and can treat its hdls like values.
>
> Connection handles are not used directly. They are used by endpoint methods to identify the target of the desired action. For example, the endpoint method that sends a new message will take as a parameter the hdl of the connection to send the message to.
@@ -267,15 +266,15 @@ A new WebSocket connection is initiated via a three step process. First, a conne
If connection creation succeeds, the next sequential connection ID is generated and a `connection_metadata` object is inserted into the connection list under that ID. Initially the metadata object stores the connection ID, the `connection_hdl`, and the URI the connection was opened to.
```cpp
~~~{.cpp}
int new_id = m_next_id++;
metadata_ptr metadata(new connection_metadata(new_id, con->get_handle(), uri));
m_connection_list[new_id] = metadata;
```
~~~
Next, the connection request is configured. For this step the only configuration we will do is setting up a few default handlers. Later on we will return and demonstrate some more detailed configuration that can happen here (setting user agents, origin, proxies, custom headers, subprotocols, etc).
> ###### Terminology: Registering handlers
> ##### Terminology: Registering handlers
> WebSocket++ provides a number of execution points where you can register to have a handler run. Which of these points are available to your endpoint will depend on its config. TLS handlers will not exist on non-TLS endpoints for example. A complete list of handlers can be found at http://www.zaphoyd.com/websocketpp/manual/reference/handler-list.
>
> Handlers can be registered at the endpoint level and at the connection level. Endpoint handlers are copied into new connections as they are created. Changing an endpoint handler will affect only future connections. Handlers registered at the connection level will be bound to that specific connection only.
@@ -292,14 +291,14 @@ In this example we are going to set connection specific handlers that are bound
Lets look at the parameters being sent to bind in detail:
```cpp
~~~{.cpp}
con->set_open_handler(websocketpp::lib::bind(
&connection_metadata::on_open,
metadata,
&m_endpoint,
websocketpp::lib::placeholders::_1
));
```
~~~
`&connection_metadata::on_open` is the address of the `on_open` member function of the `connection_metadata` class. `metadata_ptr` is a pointer to the `connection_metadata` object associated with this class. It will be used as the object on which the `on_open` member function will be called. `&m_endpoint` is the address of the endpoint in use. This parameter will be passed as-is to the `on_open` method. Lastly, `websocketpp::lib::placeholders::_1` is a placeholder indicating that the bound function should take one additional argument to be filled in at a later time. WebSocket++ will fill in this placeholder with the `connection_hdl` when it invokes the handler.
@@ -315,7 +314,7 @@ The fail handler we registered, `connection_metadata::on_fail`, sets the status
Two new commands have been set up. "connect [uri]" will pass the URI to the `websocket_endpoint` connect method and report an error or the connection ID of the new connection. "show [connection id]" will retrieve and print out the metadata associated with that connection. The help text has been updated accordingly.
```cpp
~~~{.cpp}
} else if (input.substr(0,7) == "connect") {
int id = endpoint.connect(input.substr(8));
if (id != -1) {
@@ -331,7 +330,7 @@ Two new commands have been set up. "connect [uri]" will pass the URI to the `web
std::cout << "> Unknown connection id " << id << std::endl;
}
}
```
~~~
#### Build
@@ -339,7 +338,7 @@ There are no changes to the build instructions from step 3
#### Run
```
~~~
Enter Command: connect not a websocket uri
> Connect initialization error: invalid uri
Enter Command: show 0
@@ -358,11 +357,11 @@ Enter Command: show 1
> Status: Failed
> Remote Server: Apache
> Error/close reason: Invalid HTTP status.
```
~~~
#### Code so far
```cpp
~~~{.cpp}
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
@@ -525,7 +524,7 @@ int main() {
return 0;
}
```
~~~
### Step 5
@@ -535,7 +534,7 @@ This step adds a command that allows you to close a WebSocket connection and adj
#### Getting connection close information out of WebSocket++
> ###### Terminology: WebSocket close codes & reasons
> ##### Terminology: WebSocket close codes & reasons
> The WebSocket close handshake involves an exchange of optional machine readable close codes and human readable reason strings. Each endpoint sends independent close details. The codes are short integers. The reasons are UTF8 text strings of at most 125 characters. More details about valid close code ranges and the meaning of each code can be found at https://tools.ietf.org/html/rfc6455#section-7.4
The `websocketpp::close::status` namespace contains named constants for all of the IANA defined close codes. It also includes free functions to determine whether a value is reserved or invalid and to convert a code to a human readable text representation.
@@ -554,7 +553,7 @@ During the close handler call WebSocket++ connections offer the following method
The `connection_metadata::on_close` method is added. This method retrieves the close code and reason from the closing handshake and stores it in the local error reason field.
```cpp
~~~{.cpp}
void on_close(client * c, websocketpp::connection_hdl hdl) {
m_status = "Closed";
client::connection_ptr con = c->get_con_from_hdl(hdl);
@@ -564,7 +563,7 @@ void on_close(client * c, websocketpp::connection_hdl hdl) {
<< "), close reason: " << con->get_remote_close_reason();
m_error_reason = s.str();
}
```
~~~
Similarly to `on_open` and `on_fail`, `websocket_endpoint::connect` registers this close handler when a new connection is made.
@@ -572,7 +571,7 @@ Similarly to `on_open` and `on_fail`, `websocket_endpoint::connect` registers th
This method starts by looking up the given connection ID in the connection list. Next a close request is sent to the connection's handle with the specified WebSocket close code. This is done by calling `endpoint::close`. This is a thread safe method that is used to asynchronously dispatch a close signal to the connection with the given handle. When the operation is complete the connection's close handler will be triggered.
```cpp
~~~{.cpp}
void close(int id, websocketpp::close::status::value code) {
websocketpp::lib::error_code ec;
@@ -587,7 +586,7 @@ void close(int id, websocketpp::close::status::value code) {
std::cout << "> Error initiating close: " << ec.message() << std::endl;
}
}
```
~~~
#### Add close option to the command loop and help message
@@ -595,21 +594,21 @@ A close option is added to the command loop. It takes a connection ID and option
An entry is also added to the help system to describe how the new command may be used.
```cpp
~~~{.cpp}
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 = "";
std::string reason;
ss >> cmd >> id >> close_code;
std::getline(ss,reason);
endpoint.close(id, close_code, reason);
}
```
~~~
#### Close all outstanding connections in `websocket_endpoint` destructor
@@ -617,7 +616,7 @@ Until now quitting the program left outstanding connections and the WebSocket++
The destructor for `websocket_endpoint` now stops perpetual mode (so the run thread exits after the last connection is closed) and iterates through the list of open connections and requests a clean close for each. Finally, the run thread is joined which causes the program to wait until those connection closes complete.
```cpp
~~~{.cpp}
~websocket_endpoint() {
m_endpoint.stop_perpetual();
@@ -639,7 +638,7 @@ The destructor for `websocket_endpoint` now stops perpetual mode (so the run thr
m_thread->join();
}
```
~~~
#### Build
@@ -647,7 +646,7 @@ There are no changes to the build instructions from step 4
#### Run
```
~~~
Enter Command: connect ws://localhost:9002
> Created connection with id 0
Enter Command: close 0 1001 example message
@@ -662,7 +661,7 @@ Enter Command: close 1 1006
> Error initiating close: Invalid close code used
Enter Command: quit
> Closing connection 1
```
~~~
### Step 6
@@ -670,7 +669,7 @@ _Sending and receiving messages_
This step adds a command to send a message on a given connection and updates the show command to print a transcript of all sent and received messages for that connection.
> ###### Terminology: WebSocket message types (opcodes)
> ##### Terminology: WebSocket message types (opcodes)
> WebSocket messages have types indicated by their opcode. The protocol currently specifies two different opcodes for data messages, text and binary. Text messages represent UTF8 text and will be validated as such. Binary messages represent raw binary bytes and are passed through directly with no validation.
>
> WebSocket++ provides the values `websocketpp::frame::opcode::text` and `websocketpp::frame::opcode::binary` that can be used to direct how outgoing messages should be sent and to check how incoming messages are formatted.
@@ -698,7 +697,7 @@ The third overload, `connection_hdl hdl, message_ptr msg`, takes a WebSocket++ `
Like the close method, send will start by looking up the given connection ID in the connection list. Next a send request is sent to the connection's handle with the specified WebSocket message and the text opcode. Finally, we record the sent message with our connection metadata object so later our show connection command can print a list of messages sent.
```cpp
~~~{.cpp}
void send(int id, std::string message) {
websocketpp::lib::error_code ec;
@@ -716,13 +715,13 @@ void send(int id, std::string message) {
metadata_it->second->record_sent_message(message);
}
```
~~~
#### Add send option to the command loop and help message
A send option is added to the command loop. It takes a connection ID and a text message to send. An entry is also added to the help system to describe how the new command may be used.
```cpp
~~~{.cpp}
else if (input.substr(0,4) == "send") {
std::stringstream ss(input);
@@ -735,28 +734,28 @@ else if (input.substr(0,4) == "send") {
endpoint.send(id, message);
}
```
~~~
#### Add glue to `connection_metadata` for storing sent messages
In order to store messages sent on this connection some code is added to `connection_metadata`. This includes a new data member `std::vector<std::string> m_messages` to keep track of all messages sent and received as well as a method for adding a sent message in that list:
```cpp
~~~{.cpp}
void record_sent_message(std::string message) {
m_messages.push_back(">> " + message);
}
```
~~~
Finally the connection metadata output operator is updated to also print a list of processed messages:
```cpp
~~~{.cpp}
out << "> Messages Processed: (" << data.m_messages.size() << ") \n";
std::vector<std::string>::const_iterator it;
for (it = data.m_messages.begin(); it != data.m_messages.end(); ++it) {
out << *it << "\n";
}
```
~~~
#### Receiving Messages
@@ -766,7 +765,7 @@ Messages are received by registering a message handler. This handler will be cal
The message receiving behave that we are implementing will be to collect all messages sent and received and to print them in order when the show connection command is run. The sent messages are already being added to that list. Now we add a message handler that pushes received messages to the list as well. Text messages are pushed as-is. Binary messages are first converted to printable hexadecimal format.
```cpp
~~~{.cpp}
void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
if (msg->get_opcode() == websocketpp::frame::opcode::text) {
m_messages.push_back(msg->get_payload());
@@ -774,18 +773,18 @@ void on_message(websocketpp::connection_hdl hdl, client::message_ptr msg) {
m_messages.push_back(websocketpp::utility::to_hex(msg->get_payload()));
}
}
```
~~~
In order to have this handler called when new messages are received we also register it with our connection. Note that unlike most other handlers, the message handler has two parameters and thus needs two placeholders.
```cpp
~~~{.cpp}
con->set_message_handler(websocketpp::lib::bind(
&connection_metadata::on_message,
metadata_ptr,
websocketpp::lib::placeholders::_1,
websocketpp::lib::placeholders::_2
));
```
~~~
#### Build
@@ -795,7 +794,7 @@ There are no changes to the build instructions from step 5
In this example run we are connecting to the WebSocket++ example echo_server. This server will repeat any message we send back to it. You can also try testing this with the echo server at `ws://echo.websocket.org` with similar results.
```
~~~
Enter Command: connect ws://localhost:9002
> Created connection with id 0
Enter Command: send 0 example message
@@ -807,7 +806,7 @@ Enter Command: show 0
> Messages Processed: (2)
>> example message
<< example message
```
~~~
### Step 7
+71
View File
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2015, 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 server tutorial.
// Additional related material can be found in the tutorials/utility_server
// directory of the WebSocket++ repository.
// The ASIO_STANDALONE define is necessary to use the standalone version of Asio.
// Remove if you are using Boost Asio.
#define ASIO_STANDALONE
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <functional>
typedef websocketpp::server<websocketpp::config::asio> server;
class utility_server {
public:
utility_server() {
// Set logging settings
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
m_endpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);
// Initialize Asio
m_endpoint.init_asio();
}
void run() {
// Listen on port 9002
m_endpoint.listen(9002);
// Queues a connection accept operation
m_endpoint.start_accept();
// Start the Asio io_service run loop
m_endpoint.run();
}
private:
server m_endpoint;
};
int main() {
utility_server s;
s.run();
return 0;
}
+82
View File
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2015, 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 server tutorial.
// Additional related material can be found in the tutorials/utility_server
// directory of the WebSocket++ repository.
// The ASIO_STANDALONE define is necessary to use the standalone version of Asio.
// Remove if you are using Boost Asio.
#define ASIO_STANDALONE
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <functional>
typedef websocketpp::server<websocketpp::config::asio> server;
class utility_server {
public:
utility_server() {
// Set logging settings
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
m_endpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);
// Initialize Asio
m_endpoint.init_asio();
// Set the default message handler to the echo handler
m_endpoint.set_message_handler(std::bind(
&utility_server::echo_handler, this,
std::placeholders::_1, std::placeholders::_2
));
}
void echo_handler(websocketpp::connection_hdl hdl, server::message_ptr msg) {
// write a new message
m_endpoint.send(hdl, msg->get_payload(), msg->get_opcode());
}
void run() {
// Listen on port 9002
m_endpoint.listen(9002);
// Queues a connection accept operation
m_endpoint.start_accept();
// Start the Asio io_service run loop
m_endpoint.run();
}
private:
server m_endpoint;
};
int main() {
utility_server s;
s.run();
return 0;
}
+181
View File
@@ -0,0 +1,181 @@
Utility Server Example Application Tutorial
===========================================
Introduction
------------
This tutorial provides a step by step discussion of building a basic WebSocket++ server. The final product of this tutorial is the utility_server example application from the example section. This server demonstrates the following features:
- Use Asio Transport for networking
- Accept multiple WebSocket connections at once
- Read incoming messages and perform a few basic actions (echo, broadcast, telemetry, server commands) based on the path
- Use validate handler to reject connections to invalid paths
- Serve basic HTTP responses with the http handler
- Gracefully exit the server
- Encrypt connections with TLS
This tutorial is current as of the 0.6.x version of the library.
Chapter 1: Initial Setup & Basics
---------------------------------
### Step 1
_Add WebSocket++ includes and set up a a server endpoint type._
WebSocket++ includes two major object types. The endpoint and the connection. The
endpoint creates and launches new connections and maintains default settings for
those connections. Endpoints also manage any shared network resources.
The connection stores information specific to each WebSocket session.
> **Note:** Once a connection is launched, there is no link between the endpoint and the connection. All default settings are copied into the new connection by the endpoint. Changing default settings on an endpoint will only affect future connections.
Connections do not maintain a link back to their associated endpoint. Endpoints do not maintain a list of outstanding connections. If your application needs to iterate over all connections it will need to maintain a list of them itself.
WebSocket++ endpoints are built by combining an endpoint role with an endpoint config. There are two different types of endpoint roles, one each for the client and server roles in a WebSocket session. This is a server tutorial so we will use the server role `websocketpp::server` which is provided by the `<websocketpp/server.hpp>` header.
> #### Terminology: Endpoint Config
> WebSocket++ endpoints have a group of settings that may be configured at compile time via the `config` template parameter. A config is a struct that contains types and static constants that are used to produce an endpoint with specific properties. Depending on which config is being used the endpoint will have different methods available and may have additional third party dependencies.
The endpoint role takes a template parameter called `config` that is used to configure the behavior of endpoint at compile time. For this example we are going to use a default config provided by the library called `asio`, provided by `<websocketpp/config/asio_no_tls.hpp>`. This is a server config that uses the Asio library to provide network transport and does not support TLS based security. Later on we will discuss how to introduce TLS based security into a WebSocket++ application, more about the other stock configs, and how to build your own custom configs.
Combine a config with an endpoint role to produce a fully configured endpoint. This type will be used frequently so I would recommend a typedef here.
`typedef websocketpp::server<websocketpp::config::asio> server`
#### `utility_server` constructor
This endpoint type will be the base of the utility_server object that will keep track of the state of the server. Within the `utility_server` constructor several things happen:
First, we adjust the endpoint logging behavior to include all error logging channels and all access logging channels except the frame payload, which is particularly noisy and generally useful only for debugging. [TODO: link to more information about logging]
~~~{.cpp}
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
m_endpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);
~~~
Next, we initialize the transport system underlying the endpoint. This method is specific to the Asio transport not WebSocket++ core. It will not be necessary or present in endpoints that use a non-asio config.
> **Note:** This example uses an internal Asio `io_service` that is managed by the endpoint itself. This is a simple arrangement suitable for programs where WebSocket++ is the only code using Asio. If you have an existing program that already manages an `io_service` object or want to build a new program where WebSocket++ handlers share an io_service with other handlers you can pass the `io_service` you want WebSocket++ to register its handlers on to the `init_asio()` method and it will use it instead of generating and managing its own. [TODO: FAQ link instead?]
~~~{.cpp}
m_endpoint.init_asio();
~~~
#### `utility_server::run` method
In addition to the constructor, we also add a run method that sets up the listening socket, begins accepting connections, starts the Asio io_service event loop.
~~~{.cpp}
// Listen on port 9002
m_endpoint.listen(9002);
// Queues a connection accept operation
m_endpoint.start_accept();
// Start the Asio io_service run loop
m_endpoint.run();
~~~
The final line, `m_endpoint.run();`, will block until the endpoint is instructed to stop listening for new connections. While running it will listen for and process new connections as well as accept and process new data and control messages for existing connections. WebSocket++ uses Asio in an asyncronous mode where multiple connections can be similtaneously serviced efficiently within a single thread.
#### Build
Adding WebSocket++ has added a few dependencies to our program that must be addressed in the build system. Firstly, the WebSocket++ library headers need must be in the include search path of your build system. How exactly this is done depends on where you have the WebSocket++ headers installed what build system you are using.
For the rest of this tutorial we are going to assume a C++11 build environment. WebSocket++ will work with pre-C++11 systems if your build system has access to a recent version of the Boost library headers.
Finally, to use the Asio transport config we need to bring in the Asio library. There are two options here. If you have access to a C++11 build environment the standalone version from http://think-async.com is a good option. This header only library does not bring in any special dependencies and ensures you have the latest version of Asio. If you do not have a C++11 build environment or already have brought in the Boost libraries you can also use the version of Asio bundled with Boost.
To use standalone Asio, make sure the Asio headers are in your include path and define ASIO_STANDALONE. To use Boost Asio, make sure the Boost headers are in your include path and that you are linking to the boost_system library.
`c++ -std=c++11 step1.cpp` (Asio Standalone)
OR
`c++ -std=c++11 step1.cpp -lboost_system` (Boost Asio)
#### Code so far
```cpp
// The ASIO_STANDALONE define is necessary to use the standalone version of Asio.
// Remove if you are using Boost Asio.
#define ASIO_STANDALONE
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <functional>
typedef websocketpp::server<websocketpp::config::asio> server;
class utility_server {
public:
utility_server() {
// Set logging settings
m_endpoint.set_error_channels(websocketpp::log::elevel::all);
m_endpoint.set_access_channels(websocketpp::log::alevel::all ^ websocketpp::log::alevel::frame_payload);
// Initialize Asio
m_endpoint.init_asio();
}
void run() {
// Listen on port 9002
m_endpoint.listen(9002);
// Queues a connection accept operation
m_endpoint.start_accept();
// Start the Asio io_service run loop
m_endpoint.run();
}
private:
server m_endpoint;
};
int main() {
utility_server s;
s.run();
return 0;
}
```
### Step 2
_Set up a message handler to echo all replies back to the original user_
#### Setting a message handler
> ###### Terminology: Registering handlers
> WebSocket++ provides a number of execution points where you can register to have a handler run. Which of these points are available to your endpoint will depend on its config. TLS handlers will not exist on non-TLS endpoints for example. A complete list of handlers can be found at http://www.zaphoyd.com/websocketpp/manual/reference/handler-list.
>
> Handlers can be registered at the endpoint level and at the connection level. Endpoint handlers are copied into new connections as they are created. Changing an endpoint handler will affect only future connections. Handlers registered at the connection level will be bound to that specific connection only.
>
> The signature of handler binding methods is the same for endpoints and connections. The format is: `set_*_handler(...)`. Where * is the name of the handler. For example, `set_open_handler(...)` will set the handler to be called when a new connection is open. `set_fail_handler(...)` will set the handler to be called when a connection fails to connect.
>
> All handlers take one argument, a callable type that can be converted to a `std::function` with the correct count and type of arguments. You can pass free functions, functors, and Lambdas with matching argument lists as handlers. In addition, you can use `std::bind` (or `boost::bind`) to register functions with non-matching argument lists. This is useful for passing additional parameters not present in the handler signature or member functions that need to carry a 'this' pointer.
>
> The function signature of each handler can be looked up in the list above in the manual. In general, all handlers include the `connection_hdl` identifying which connection this even is associated with as the first parameter. Some handlers (such as the message handler) include additional parameters. Most handlers have a void return value but some (`validate`, `ping`, `tls_init`) do not. The specific meanings of the return values are documented in the handler list linked above.
### Step 3
_error handling_
### Step 4
_Set up open and close handlers and a connection data structure_
### Step 5
_Change the message handler for connections based on URI and add a validate handler to reject invalid URIs_
### Step 6
_Add some Admin commands (report total clients, cleanly shut down server)_
### Step 7
_Add some Broadcast commands_
### Step 8
_Add TLS_
+1 -1
View File
@@ -322,7 +322,7 @@ inline status::value extract_code(std::string const & payload, lib::error_code
inline std::string extract_reason(std::string const & payload, lib::error_code
& ec)
{
std::string reason = "";
std::string reason;
ec = lib::error_code();
if (payload.size() > 2) {
+3 -1
View File
@@ -28,8 +28,10 @@
#ifndef WEBSOCKETPP_COMMON_ASIO_SSL_HPP
#define WEBSOCKETPP_COMMON_ASIO_SSL_HPP
// NOTE: This file must be included before common/asio.hpp
#ifdef ASIO_STANDALONE
#include <asio/asio/ssl.hpp>
#include <asio/ssl.hpp>
#else
#include <boost/asio/ssl.hpp>
#endif
+9 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, Peter Thorson. All rights reserved.
* Copyright (c) 2015, 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:
@@ -39,6 +39,14 @@
#endif
#endif
// If we're on Visual Studio 2012 or higher and haven't explicitly disabled
// the use of C++11 chrono header then prefer it to boost.
#if defined(_MSC_VER) && _MSC_VER >= 1700 && !defined _WEBSOCKETPP_NO_CPP11_CHRONO_
#ifndef _WEBSOCKETPP_CPP11_CHRONO_
#define _WEBSOCKETPP_CPP11_CHRONO_
#endif
#endif
#ifdef _WEBSOCKETPP_CPP11_CHRONO_
#include <chrono>
#else
+7 -1
View File
@@ -105,6 +105,9 @@
#if __has_feature(cxx_noexcept)
// clang feature detect says we have noexcept
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_ noexcept
#elif defined(_MSC_VER) && _MSC_VER >= 1900
// Visual Studio 2015+ has noexcept
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_ noexcept
#else
// assume we don't have noexcept
#define _WEBSOCKETPP_NOEXCEPT_TOKEN_
@@ -121,6 +124,9 @@
#if __has_feature(cxx_constexpr)
// clang feature detect says we have constexpr
#define _WEBSOCKETPP_CONSTEXPR_TOKEN_ constexpr
#elif defined(_MSC_VER) && _MSC_VER >= 1900
// Visual Studio 2015+ has constexpr
#define _WEBSOCKETPP_CONSTEXPR_TOKEN_ constexpr
#else
// assume we don't have constexpr
#define _WEBSOCKETPP_CONSTEXPR_TOKEN_
@@ -142,7 +148,7 @@
#if __has_feature(cxx_nullptr)
// clang feature detect says we have nullptr
#define _WEBSOCKETPP_NULLPTR_TOKEN_ nullptr
#elif _MSC_VER >= 1600
#elif defined(_MSC_VER) &&_MSC_VER >= 1600
// Visual Studio version that has nullptr
#define _WEBSOCKETPP_NULLPTR_TOKEN_ nullptr
#else
+3
View File
@@ -65,14 +65,17 @@ namespace lib {
#ifdef _WEBSOCKETPP_CPP11_MEMORY_
using std::shared_ptr;
using std::weak_ptr;
using std::auto_ptr;
using std::enable_shared_from_this;
using std::static_pointer_cast;
using std::make_shared;
using std::unique_ptr;
typedef std::unique_ptr<unsigned char[]> unique_ptr_uchar_array;
#else
using boost::shared_ptr;
using boost::weak_ptr;
using std::auto_ptr;
using boost::enable_shared_from_this;
using boost::static_pointer_cast;
using boost::make_shared;
+8
View File
@@ -43,6 +43,14 @@
#endif
#endif
// If we're on Visual Studio 2013 or higher and haven't explicitly disabled
// the use of C++11 thread header then prefer it to boost.
#if defined(_MSC_VER) && _MSC_VER >= 1800 && !defined _WEBSOCKETPP_NO_CPP11_THREAD_
#ifndef _WEBSOCKETPP_CPP11_THREAD_
#define _WEBSOCKETPP_CPP11_THREAD_
#endif
#endif
#ifdef _WEBSOCKETPP_CPP11_THREAD_
#include <thread>
#include <mutex>
+2
View File
@@ -53,8 +53,10 @@ namespace lib {
#ifdef _WEBSOCKETPP_CPP11_TYPE_TRAITS_
using std::aligned_storage;
using std::is_same;
#else
using boost::aligned_storage;
using boost::is_same;
#endif
} // namespace lib
+79 -24
View File
@@ -628,7 +628,10 @@ public:
*/
size_t get_buffered_amount() const;
/// DEPRECATED: use get_buffered_amount instead
/// Get the size of the outgoing write buffer (in payload bytes)
/**
* @deprecated use `get_buffered_amount` instead
*/
size_t buffered_amount() const {
return get_buffered_amount();
}
@@ -969,6 +972,30 @@ public:
*/
std::string const & get_response_header(std::string const & key) const;
/// Get response HTTP status code
/**
* Gets the response status code
*
* @since 0.7.0
*
* @return The response status code sent
*/
http::status_code::value get_response_code() const {
return m_response.get_status_code();
}
/// Get response HTTP status message
/**
* Gets the response status message
*
* @since 0.7.0
*
* @return The response status message sent
*/
std::string const & get_response_msg() const {
return m_response.get_status_msg();
}
/// Set response status code and message
/**
* Sets the response status code to `code` and looks up the corresponding
@@ -1076,6 +1103,25 @@ public:
return m_request;
}
/// Get response object
/**
* Direct access to the HTTP response sent or received as a part of the
* opening handshake. This can be used to call methods of the response
* object that are not part of the standard request API that connection
* wraps.
*
* Note use of this method involves using behavior specific to the
* configured HTTP policy. Such behavior may not work with alternate HTTP
* policies.
*
* @since 0.7.0
*
* @return A const reference to the raw response object
*/
response_type const & get_response() const {
return m_response;
}
/// Defer HTTP Response until later (Exception free)
/**
* Used in the http handler to defer the HTTP response for this connection
@@ -1209,26 +1255,6 @@ public:
return m_ec;
}
////////////////////////////////////////////////////////////////////////
// The remaining public member functions are for internal/policy use //
// only. Do not call from application code unless you understand what //
// you are doing. //
////////////////////////////////////////////////////////////////////////
/// Set Connection Handle
/**
* The connection handle is a token that can be shared outside the
* WebSocket++ core for the purposes of identifying a connection and
* sending it messages.
*
* @param hdl A connection_hdl that the connection will use to refer
* to itself.
*/
void set_handle(connection_hdl hdl) {
m_connection_hdl = hdl;
transport_con_type::set_handle(hdl);
}
/// Get a message buffer
/**
* Warning: The API related to directly sending message buffers may change
@@ -1254,7 +1280,13 @@ public:
return m_msg_manager->get_message(op, size);
}
void start();
////////////////////////////////////////////////////////////////////////
// The remaining public member functions are for internal/policy use //
// only. Do not call from application code unless you understand what //
// you are doing. //
////////////////////////////////////////////////////////////////////////
void read_handshake(size_t num_bytes);
@@ -1304,6 +1336,27 @@ public:
* non-zero otherwise.
*/
void handle_write_frame(lib::error_code const & ec);
// protected:
// This set of methods would really like to be protected, but doing so
// requires that the endpoint be able to friend the connection. This is
// allowed with C++11, but not prior versions
/// Start the connection state machine
void start();
/// Set Connection Handle
/**
* The connection handle is a token that can be shared outside the
* WebSocket++ core for the purposes of identifying a connection and
* sending it messages.
*
* @param hdl A connection_hdl that the connection will use to refer
* to itself.
*/
void set_handle(connection_hdl hdl) {
m_connection_hdl = hdl;
transport_con_type::set_handle(hdl);
}
protected:
void handle_transport_init(lib::error_code const & ec);
@@ -1315,6 +1368,8 @@ protected:
/// set m_response and return an error code indicating status.
lib::error_code process_handshake_request();
private:
/// Completes m_response, serializes it, and sends it out on the wire.
void write_http_response(lib::error_code const & ec);
@@ -1342,7 +1397,7 @@ private:
* @return A status code, zero on success, non-zero otherwise
*/
lib::error_code send_close_ack(close::status::value code =
close::status::blank, std::string const & reason = "");
close::status::blank, std::string const & reason = std::string());
/// Send close frame
/**
@@ -1360,7 +1415,7 @@ private:
* @return A status code, zero on success, non-zero otherwise
*/
lib::error_code send_close_frame(close::status::value code =
close::status::blank, std::string const & reason = "", bool ack = false,
close::status::blank, std::string const & reason = std::string(), bool ack = false,
bool terminal = false);
/// Get a pointer to a new WebSocket protocol processor for a given version
+3
View File
@@ -85,6 +85,9 @@ public:
// TODO: organize these
typedef typename connection_type::termination_handler termination_handler;
// This would be ideal. Requires C++11 though
//friend connection;
explicit endpoint(bool p_is_server)
: m_alog(config::alog_level, log::channel_type_hint::access)
, m_elog(config::elog_level, log::channel_type_hint::error)
+1 -1
View File
@@ -78,7 +78,7 @@ public:
}
};
inline const lib::error_category& get_category() {
inline lib::error_category const & get_category() {
static category instance;
return instance;
}
+3 -3
View File
@@ -59,7 +59,7 @@ namespace http {
static char const header_separator[] = ":";
/// Literal value of an empty header
static std::string const empty_header = "";
static std::string const empty_header;
/// Maximum size in bytes before rejecting an HTTP header as too big.
size_t const max_header_size = 16000;
@@ -284,8 +284,8 @@ namespace http {
public:
exception(const std::string& log_msg,
status_code::value error_code,
const std::string& error_msg = "",
const std::string& body = "")
const std::string& error_msg = std::string(),
const std::string& body = std::string())
: m_msg(log_msg)
, m_error_msg(error_msg)
, m_body(body)
+3 -3
View File
@@ -71,7 +71,7 @@ inline void parser::append_header(std::string const & key, std::string const &
throw exception("Invalid header name",status_code::bad_request);
}
if (this->get_header(key) == "") {
if (this->get_header(key).empty()) {
m_headers[key] = val;
} else {
m_headers[key] += ", " + val;
@@ -91,7 +91,7 @@ inline void parser::remove_header(std::string const & key) {
inline void parser::set_body(std::string const & value) {
if (value.size() == 0) {
remove_header("Content-Length");
m_body = "";
m_body.clear();
return;
}
@@ -117,7 +117,7 @@ inline bool parser::parse_parameter_list(std::string const & in,
}
inline bool parser::prepare_body() {
if (get_header("Content-Length") != "") {
if (!get_header("Content-Length").empty()) {
std::string const & cl_header = get_header("Content-Length");
char * end;
+1 -1
View File
@@ -88,7 +88,7 @@ inline size_t request::consume(char const * buf, size_t len) {
//the range [begin,end) now represents a line to be processed.
if (end-begin == 0) {
// we got a blank line
if (m_method.empty() || get_header("Host") == "") {
if (m_method.empty() || get_header("Host").empty()) {
throw exception("Incomplete Request",status_code::bad_request);
}
+1 -1
View File
@@ -94,7 +94,7 @@ inline size_t response::consume(char const * buf, size_t len) {
// TODO: grab content-length
std::string length = get_header("Content-Length");
if (length == "") {
if (length.empty()) {
// no content length found, read indefinitely
m_read = 0;
} else {
+10 -6
View File
@@ -230,7 +230,7 @@ InputIterator extract_attributes(InputIterator begin, InputIterator end,
cursor = http::parser::extract_all_lws(cursor,end);
ret = http::parser::extract_token(cursor,end);
if (ret.first == "") {
if (ret.first.empty()) {
// error: expected a token
return begin;
} else {
@@ -242,7 +242,7 @@ InputIterator extract_attributes(InputIterator begin, InputIterator end,
if (cursor == end || *cursor != '=') {
// if there is an equals sign, read the attribute value. Otherwise
// record a blank value and continue
attributes[name] = "";
attributes[name].clear();
continue;
}
@@ -263,7 +263,7 @@ InputIterator extract_attributes(InputIterator begin, InputIterator end,
}
ret = http::parser::extract_token(cursor,end);
if (ret.first == "") {
if (ret.first.empty()) {
// error : expected token or quoted string
return begin;
} else {
@@ -321,7 +321,7 @@ InputIterator extract_parameters(InputIterator begin, InputIterator end,
ret = http::parser::extract_token(cursor,end);
if (ret.first == "") {
if (ret.first.empty()) {
// error: expected a token
return begin;
} else {
@@ -381,9 +381,13 @@ inline std::string strip_lws(std::string const & input) {
if (begin == input.end()) {
return std::string();
}
std::string::const_reverse_iterator end = extract_all_lws(input.rbegin(),input.rend());
return std::string(begin,end.base());
std::string::const_reverse_iterator rbegin = extract_all_lws(input.rbegin(),input.rend());
if (rbegin == input.rend()) {
return std::string();
}
return std::string(begin,rbegin.base());
}
/// Base HTTP parser
+30 -16
View File
@@ -540,6 +540,7 @@ connection<config>::get_response_header(std::string const & key) const {
return m_response.get_header(key);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_status(http::status_code::value code)
{
@@ -549,6 +550,8 @@ void connection<config>::set_status(http::status_code::value code)
}
m_response.set_status(code);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_status(http::status_code::value code,
std::string const & msg)
@@ -560,6 +563,8 @@ void connection<config>::set_status(http::status_code::value code,
m_response.set_status(code,msg);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::set_body(std::string const & value) {
if (m_internal_state != istate::PROCESS_HTTP_REQUEST) {
@@ -570,6 +575,7 @@ void connection<config>::set_body(std::string const & value) {
m_response.set_body(value);
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::append_header(std::string const & key,
std::string const & val)
@@ -592,6 +598,8 @@ void connection<config>::append_header(std::string const & key,
}
}
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::replace_header(std::string const & key,
std::string const & val)
@@ -614,6 +622,8 @@ void connection<config>::replace_header(std::string const & key,
}
}
}
// TODO: EXCEPTION_FREE
template <typename config>
void connection<config>::remove_header(std::string const & key)
{
@@ -762,7 +772,7 @@ void connection<config>::handle_transport_init(lib::error_code const & ec) {
template <typename config>
void connection<config>::read_handshake(size_t num_bytes) {
m_alog.write(log::alevel::devel,"connection read");
m_alog.write(log::alevel::devel,"connection read_handshake");
if (m_open_handshake_timeout_dur > 0) {
m_handshake_timer = transport_con_type::set_timer(
@@ -890,7 +900,7 @@ void connection<config>::handle_read_handshake(lib::error_code const & ec,
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,m_request.raw());
if (m_request.get_header("Sec-WebSocket-Key3") != "") {
if (!m_request.get_header("Sec-WebSocket-Key3").empty()) {
m_alog.write(log::alevel::devel,
utility::to_hex(m_request.get_header("Sec-WebSocket-Key3")));
}
@@ -907,7 +917,11 @@ void connection<config>::handle_read_handshake(lib::error_code const & ec,
// We have the complete request. Process it.
lib::error_code handshake_ec = this->process_handshake_request();
if (!m_is_http || m_http_state != session::http_state::deferred) {
// Write a response if this is a websocket connection or if it is an
// HTTP connection for which the response has not been deferred or
// started yet by a different system (i.e. still in init state).
if (!m_is_http || m_http_state == session::http_state::init) {
this->write_http_response(handshake_ec);
}
} else {
@@ -1146,7 +1160,7 @@ lib::error_code connection<config>::initialize_processor() {
m_response.set_status(http::status_code::bad_request);
std::stringstream ss;
std::string sep = "";
std::string sep;
std::vector<int>::const_iterator it;
for (it = versions_supported.begin(); it != versions_supported.end(); it++)
{
@@ -1294,7 +1308,7 @@ void connection<config>::write_http_response(lib::error_code const & ec) {
m_response.set_version("HTTP/1.1");
// Set server header based on the user agent settings
if (m_response.get_header("Server") == "") {
if (m_response.get_header("Server").empty()) {
if (!m_user_agent.empty()) {
m_response.replace_header("Server",m_user_agent);
} else {
@@ -1312,7 +1326,7 @@ void connection<config>::write_http_response(lib::error_code const & ec) {
if (m_alog.static_test(log::alevel::devel)) {
m_alog.write(log::alevel::devel,"Raw Handshake response:\n"+m_handshake_buffer);
if (m_response.get_header("Sec-WebSocket-Key3") != "") {
if (!m_response.get_header("Sec-WebSocket-Key3").empty()) {
m_alog.write(log::alevel::devel,
utility::to_hex(m_response.get_header("Sec-WebSocket-Key3")));
}
@@ -1436,7 +1450,7 @@ void connection<config>::send_http_request() {
}
// Unless the user has overridden the user agent, send generic WS++ UA.
if (m_request.get_header("User-Agent") == "") {
if (m_request.get_header("User-Agent").empty()) {
if (!m_user_agent.empty()) {
m_request.replace_header("User-Agent",m_user_agent);
} else {
@@ -2061,7 +2075,7 @@ lib::error_code connection<config>::send_close_frame(close::status::value code,
if (config::silent_close) {
m_alog.write(log::alevel::devel,"closing silently");
m_local_close_code = close::status::no_status;
m_local_close_reason = "";
m_local_close_reason.clear();
} else if (code != close::status::blank) {
m_alog.write(log::alevel::devel,"closing with specified codes");
m_local_close_code = code;
@@ -2069,12 +2083,12 @@ lib::error_code connection<config>::send_close_frame(close::status::value code,
} else if (!ack) {
m_alog.write(log::alevel::devel,"closing with no status code");
m_local_close_code = close::status::no_status;
m_local_close_reason = "";
m_local_close_reason.clear();
} else if (m_remote_close_code == close::status::no_status) {
m_alog.write(log::alevel::devel,
"acknowledging a no-status close with normal code");
m_local_close_code = close::status::normal;
m_local_close_reason = "";
m_local_close_reason.clear();
} else {
m_alog.write(log::alevel::devel,"acknowledging with remote codes");
m_local_close_code = m_remote_close_code;
@@ -2255,7 +2269,7 @@ void connection<config>::log_open_result()
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua == "") {
if (ua.empty()) {
s << "\"\" ";
} else {
// check if there are any quotes in the user agent
@@ -2278,9 +2292,9 @@ void connection<config>::log_close_result()
s << "Disconnect "
<< "close local:[" << m_local_close_code
<< (m_local_close_reason == "" ? "" : ","+m_local_close_reason)
<< (m_local_close_reason.empty() ? "" : ","+m_local_close_reason)
<< "] remote:[" << m_remote_close_code
<< (m_remote_close_reason == "" ? "" : ","+m_remote_close_reason) << "]";
<< (m_remote_close_reason.empty() ? "" : ","+m_remote_close_reason) << "]";
m_alog.write(log::alevel::disconnect,s.str());
}
@@ -2305,7 +2319,7 @@ void connection<config>::log_fail_result()
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua == "") {
if (ua.empty()) {
s << " \"\" ";
} else {
// check if there are any quotes in the user agent
@@ -2334,7 +2348,7 @@ void connection<config>::log_http_result() {
}
// Connection Type
s << (m_request.get_header("host") == "" ? "-" : m_request.get_header("host"))
s << (m_request.get_header("host").empty() ? "-" : m_request.get_header("host"))
<< " " << transport_con_type::get_remote_endpoint()
<< " \"" << m_request.get_method()
<< " " << (m_uri ? m_uri->get_resource() : "-")
@@ -2343,7 +2357,7 @@ void connection<config>::log_http_result() {
// User Agent
std::string ua = m_request.get_header("User-Agent");
if (ua == "") {
if (ua.empty()) {
s << " \"\" ";
} else {
// check if there are any quotes in the user agent
+26 -10
View File
@@ -29,6 +29,8 @@
#define WEBSOCKETPP_PROCESSOR_HYBI00_HPP
#include <websocketpp/frame.hpp>
#include <websocketpp/http/constants.hpp>
#include <websocketpp/utf8_validator.hpp>
#include <websocketpp/common/network.hpp>
#include <websocketpp/common/md5.hpp>
@@ -85,9 +87,9 @@ public:
// Host is required by HTTP/1.1
// Connection is required by is_websocket_handshake
// Upgrade is required by is_websocket_handshake
if (r.get_header("Sec-WebSocket-Key1") == "" ||
r.get_header("Sec-WebSocket-Key2") == "" ||
r.get_header("Sec-WebSocket-Key3") == "")
if (r.get_header("Sec-WebSocket-Key1").empty() ||
r.get_header("Sec-WebSocket-Key2").empty() ||
r.get_header("Sec-WebSocket-Key3").empty())
{
return make_error_code(error::missing_required_header);
}
@@ -126,18 +128,18 @@ public:
// Echo back client's origin unless our local application set a
// more restrictive one.
if (res.get_header("Sec-WebSocket-Origin") == "") {
if (res.get_header("Sec-WebSocket-Origin").empty()) {
res.append_header("Sec-WebSocket-Origin",req.get_header("Origin"));
}
// Echo back the client's request host unless our local application
// set a different one.
if (res.get_header("Sec-WebSocket-Location") == "") {
if (res.get_header("Sec-WebSocket-Location").empty()) {
uri_ptr uri = get_uri(req);
res.append_header("Sec-WebSocket-Location",uri->str());
}
if (subprotocol != "") {
if (!subprotocol.empty()) {
res.replace_header("Sec-WebSocket-Protocol",subprotocol);
}
@@ -186,15 +188,29 @@ public:
/// Extracts requested subprotocols from a handshake request
/**
* hybi00 doesn't support subprotocols so there never will be any requested
* hybi00 does support subprotocols
* https://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00#section-1.9
*
* @param [in] req The request to extract from
* @param [out] subprotocol_list A reference to a vector of strings to store
* the results in.
*/
lib::error_code extract_subprotocols(request_type const &,
std::vector<std::string> &)
lib::error_code extract_subprotocols(request_type const & req,
std::vector<std::string> & subprotocol_list)
{
if (!req.get_header("Sec-WebSocket-Protocol").empty()) {
http::parameter_list p;
if (!req.get_header_as_plist("Sec-WebSocket-Protocol",p)) {
http::parameter_list::const_iterator it;
for (it = p.begin(); it != p.end(); ++it) {
subprotocol_list.push_back(it->first);
}
} else {
return error::make_error_code(error::subprotocol_parse_error);
}
}
return lib::error_code();
}
@@ -400,7 +416,7 @@ public:
private:
void decode_client_key(std::string const & key, char * result) const {
unsigned int spaces = 0;
std::string digits = "";
std::string digits;
uint32_t num;
// key2
+6 -37
View File
@@ -173,7 +173,7 @@ public:
// Host is required by HTTP/1.1
// Connection is required by is_websocket_handshake
// Upgrade is required by is_websocket_handshake
if (r.get_header("Sec-WebSocket-Key") == "") {
if (r.get_header("Sec-WebSocket-Key").empty()) {
return make_error_code(error::missing_required_header);
}
@@ -574,19 +574,6 @@ public:
* Performs validation, masking, compression, etc. will return an error if
* there was an error, otherwise msg will be ready to be written
*
* By default WebSocket++ performs block masking/unmasking in a manner that
* makes assumptions about the nature of the machine and STL library used.
* In particular the assumption is either a 32 or 64 bit word size and an
* STL with std::string::data returning a contiguous char array.
*
* This method improves masking performance by 3-8x depending on the ratio
* of small to large messages and the availability of a 64 bit processor.
*
* To disable this optimization (for use with alternative STL
* implementations or processors) define WEBSOCKETPP_STRICT_MASKING when
* compiling the library. This will force the library to perform masking in
* single byte chunks.
*
* TODO: tests
*
* @param in An unprepared message to prepare
@@ -791,19 +778,9 @@ protected:
{
// unmask if masked
if (frame::get_masked(m_basic_header)) {
#ifdef WEBSOCKETPP_STRICT_MASKING
m_current_msg->prepared_key = frame::byte_mask_circ(
buf,
len,
m_current_msg->prepared_key
);
#else
m_current_msg->prepared_key = frame::word_mask_circ(
buf,
len,
m_current_msg->prepared_key
);
#endif
m_current_msg->prepared_key = frame::byte_mask_circ(
buf, len, m_current_msg->prepared_key);
// TODO: SIMD masking
}
std::string & out = m_current_msg->msg_ptr->get_raw_payload();
@@ -962,16 +939,8 @@ protected:
void masked_copy (std::string const & i, std::string & o,
frame::masking_key_type key) const
{
#ifdef WEBSOCKETPP_STRICT_MASKING
frame::byte_mask(i.begin(),i.end(),o.begin(),key);
#else
websocketpp::frame::word_mask_exact(
reinterpret_cast<uint8_t *>(const_cast<char *>(i.data())),
reinterpret_cast<uint8_t *>(const_cast<char *>(o.data())),
i.size(),
key
);
#endif
frame::byte_mask(i.begin(),i.end(),o.begin(),key);
// TODO: SIMD masking
}
/// Generic prepare control frame with opcode and payload.
+1 -1
View File
@@ -109,7 +109,7 @@ int get_websocket_version(request_type& r) {
return -2;
}
if (r.get_header("Sec-WebSocket-Version") == "") {
if (r.get_header("Sec-WebSocket-Version").empty()) {
return 0;
}
+2
View File
@@ -67,6 +67,8 @@ public:
/// Type of the endpoint component of this server
typedef endpoint<connection_type,config> endpoint_type;
friend class connection<config>;
explicit client() : endpoint_type(false)
{
endpoint_type::m_alog.write(log::alevel::devel, "client constructor");
+2
View File
@@ -64,6 +64,8 @@ public:
/// Type of the endpoint component of this server
typedef endpoint<connection_type,config> endpoint_type;
friend class connection<config>;
explicit server() : endpoint_type(true)
{
endpoint_type::m_alog.write(log::alevel::devel, "server constructor");

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