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.

168 lines
5.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_
  28. #define TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_
  29. #pragma once
  30. #ifdef WIN32
  31. #include <winsock2.h>
  32. typedef int socklen_t;
  33. #else
  34. #include <netinet/in.h>
  35. #include <sys/select.h>
  36. #include <sys/socket.h>
  37. #define closesocket close
  38. #endif
  39. #include <string>
  40. #ifndef SOCKET_ERROR
  41. #define SOCKET_ERROR (-1)
  42. #endif
  43. #ifndef INVALID_SOCKET
  44. #define INVALID_SOCKET static_cast<int>(~0)
  45. #endif
  46. class SocketBase {
  47. public:
  48. SocketBase() : socket_(INVALID_SOCKET) { }
  49. explicit SocketBase(int socket) : socket_(socket) { }
  50. ~SocketBase() { Close(); }
  51. int socket() const { return socket_; }
  52. bool valid() const { return socket_ != INVALID_SOCKET; }
  53. bool Create();
  54. void Close();
  55. protected:
  56. int socket_;
  57. };
  58. // Represents an HTTP server socket.
  59. class DataSocket : public SocketBase {
  60. public:
  61. enum RequestMethod {
  62. INVALID,
  63. GET,
  64. POST,
  65. OPTIONS,
  66. };
  67. explicit DataSocket(int socket)
  68. : SocketBase(socket),
  69. method_(INVALID),
  70. content_length_(0) {
  71. }
  72. ~DataSocket() {
  73. }
  74. static const char kCrossOriginAllowHeaders[];
  75. bool headers_received() const { return method_ != INVALID; }
  76. RequestMethod method() const { return method_; }
  77. const std::string& request_path() const { return request_path_; }
  78. std::string request_arguments() const;
  79. const std::string& data() const { return data_; }
  80. const std::string& content_type() const { return content_type_; }
  81. size_t content_length() const { return content_length_; }
  82. bool request_received() const {
  83. return headers_received() && (method_ != POST || data_received());
  84. }
  85. bool data_received() const {
  86. return method_ != POST || data_.length() >= content_length_;
  87. }
  88. // Checks if the request path (minus arguments) matches a given path.
  89. bool PathEquals(const char* path) const;
  90. // Called when we have received some data from clients.
  91. // Returns false if an error occurred.
  92. bool OnDataAvailable(bool* close_socket);
  93. // Send a raw buffer of bytes.
  94. bool Send(const std::string& data) const;
  95. // Send an HTTP response. The |status| should start with a valid HTTP
  96. // response code, followed by a string. E.g. "200 OK".
  97. // If |connection_close| is set to true, an extra "Connection: close" HTTP
  98. // header will be included. |content_type| is the mime content type, not
  99. // including the "Content-Type: " string.
  100. // |extra_headers| should be either empty or a list of headers where each
  101. // header terminates with "\r\n".
  102. // |data| is the body of the message. It's length will be specified via
  103. // a "Content-Length" header.
  104. bool Send(const std::string& status, bool connection_close,
  105. const std::string& content_type,
  106. const std::string& extra_headers, const std::string& data) const;
  107. // Clears all held state and prepares the socket for receiving a new request.
  108. void Clear();
  109. protected:
  110. // A fairly relaxed HTTP header parser. Parses the method, path and
  111. // content length (POST only) of a request.
  112. // Returns true if a valid request was received and no errors occurred.
  113. bool ParseHeaders();
  114. // Figures out whether the request is a GET or POST and what path is
  115. // being requested.
  116. bool ParseMethodAndPath(const char* begin, size_t len);
  117. // Determines the length of the body and it's mime type.
  118. bool ParseContentLengthAndType(const char* headers, size_t length);
  119. protected:
  120. RequestMethod method_;
  121. size_t content_length_;
  122. std::string content_type_;
  123. std::string request_path_;
  124. std::string request_headers_;
  125. std::string data_;
  126. };
  127. // The server socket. Accepts connections and generates DataSocket instances
  128. // for each new connection.
  129. class ListeningSocket : public SocketBase {
  130. public:
  131. ListeningSocket() {}
  132. bool Listen(unsigned short port);
  133. DataSocket* Accept() const;
  134. };
  135. #endif // TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_