03_decorator.qbk 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [/-----------------------------------------------------------------------------]
  8. [section:decorator Decorator]
  9. For programs which need to modify either the outgoing WebSocket HTTP Upgrade
  10. request, the outgoing WebSocket HTTP Upgrade response, or both, the stream
  11. supports an optional property called a ['decorator]. This is a function
  12. pointer or callable object which is invoked before the implementation
  13. sends an HTTP message. The decorator receives a modifiable reference to
  14. the message, allowing for modifications. The interface to this system
  15. uses:
  16. [table WebSocket Decorator Interface
  17. [[Name][Description]]
  18. [[
  19. [link beast.ref.boost__beast__websocket__request_type `request_type`]
  20. ][
  21. This is the type of the object passed to the decorator to
  22. represent HTTP Upgrade requests.
  23. ]]
  24. [[
  25. [link beast.ref.boost__beast__websocket__response_type `response_type`]
  26. ][
  27. This is the type of the object passed to the decorator to
  28. represent HTTP Upgrade response.
  29. ]]
  30. [[
  31. [link beast.ref.boost__beast__websocket__stream_base__decorator `stream_base::decorator`]
  32. ][
  33. Objects of this type are used to hold a decorator to be
  34. set on the stream using `set_option`.
  35. ]]
  36. [[
  37. [link beast.ref.boost__beast__websocket__stream.set_option `stream::set_option`]
  38. ][
  39. This function is used to set a `stream_base::decorator` on the stream.
  40. ]]
  41. ]
  42. This declares a normal function which decorates outgoing HTTP requests:
  43. [code_websocket_3_1]
  44. When using a decorator, it must be set on the stream before any handshaking
  45. takes place. This sets the decorator on the stream, to be used for all
  46. subsequent calls to accept or handshake:
  47. [code_websocket_3_2]
  48. Alternatively, a function object may be used. Small function objects will
  49. not incur a memory allocation. The follow code declares and sets a function
  50. object as a decorator:
  51. [code_websocket_3_3]
  52. A lambda may be used in place of a named function object:
  53. [code_websocket_3_4]
  54. It also possible for a single decorator to handle both requests and
  55. responses, if it is overloaded for both types either as a generic
  56. lambda (C++14 and later) or as a class as shown here:
  57. [code_websocket_3_5]
  58. The implementation takes ownership by decay-copy of the invocable object
  59. used as the decorator. Move-only types are possible:
  60. [code_websocket_3_6]
  61. [important
  62. Undefined behavior results if the decorator modifies the fields
  63. specific to perform the WebSocket Upgrade, such as the Upgrade
  64. or Connection fields.
  65. ]
  66. [endsect]