You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.9 KiB

4 months ago
  1. /*
  2. * IXWebSocketHandshake.h
  3. * Author: Benjamin Sergeant
  4. * Copyright (c) 2019 Machine Zone, Inc. All rights reserved.
  5. */
  6. #pragma once
  7. #include "IXCancellationRequest.h"
  8. #include "IXSocket.h"
  9. #include "IXWebSocketHttpHeaders.h"
  10. #include "IXWebSocketInitResult.h"
  11. #include "IXWebSocketPerMessageDeflate.h"
  12. #include "IXWebSocketPerMessageDeflateOptions.h"
  13. #include <atomic>
  14. #include <chrono>
  15. #include <memory>
  16. #include <string>
  17. namespace ix
  18. {
  19. class WebSocketHandshake
  20. {
  21. public:
  22. WebSocketHandshake(std::atomic<bool>& requestInitCancellation,
  23. std::unique_ptr<Socket>& _socket,
  24. WebSocketPerMessageDeflatePtr& perMessageDeflate,
  25. WebSocketPerMessageDeflateOptions& perMessageDeflateOptions,
  26. std::atomic<bool>& enablePerMessageDeflate);
  27. WebSocketInitResult clientHandshake(const std::string& url,
  28. const WebSocketHttpHeaders& extraHeaders,
  29. const std::string& host,
  30. const std::string& path,
  31. int port,
  32. int timeoutSecs);
  33. WebSocketInitResult serverHandshake(int timeoutSecs, bool enablePerMessageDeflate);
  34. private:
  35. std::string genRandomString(const int len);
  36. // Parse HTTP headers
  37. WebSocketInitResult sendErrorResponse(int code, const std::string& reason);
  38. bool insensitiveStringCompare(const std::string& a, const std::string& b);
  39. std::atomic<bool>& _requestInitCancellation;
  40. std::unique_ptr<Socket>& _socket;
  41. WebSocketPerMessageDeflatePtr& _perMessageDeflate;
  42. WebSocketPerMessageDeflateOptions& _perMessageDeflateOptions;
  43. std::atomic<bool>& _enablePerMessageDeflate;
  44. };
  45. } // namespace ix