henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 2 | * Copyright 2011 The WebRTC Project Authors. All rights reserved. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 3 | * |
Donald E Curtis | a873644 | 2015-08-05 15:48:13 -0700 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |
| 12 | #define EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
| 14 | #ifdef WIN32 |
| 15 | #include <winsock2.h> |
| 16 | typedef int socklen_t; |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 17 | typedef SOCKET NativeSocket; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | #else |
| 19 | #include <netinet/in.h> |
| 20 | #include <sys/select.h> |
| 21 | #include <sys/socket.h> |
| 22 | #define closesocket close |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 23 | typedef int NativeSocket; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 24 | |
| 25 | #ifndef SOCKET_ERROR |
| 26 | #define SOCKET_ERROR (-1) |
| 27 | #endif |
| 28 | |
| 29 | #ifndef INVALID_SOCKET |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | #define INVALID_SOCKET static_cast<NativeSocket>(-1) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 31 | #endif |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | #include <string> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | |
| 36 | class SocketBase { |
| 37 | public: |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | SocketBase() : socket_(INVALID_SOCKET) {} |
| 39 | explicit SocketBase(NativeSocket socket) : socket_(socket) {} |
Philipp Hancke | c91c2f5 | 2021-07-22 13:51:21 +0200 | [diff] [blame] | 40 | SocketBase(SocketBase& other) = delete; |
| 41 | SocketBase& operator=(const SocketBase& other) = delete; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | ~SocketBase() { Close(); } |
| 43 | |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 44 | NativeSocket socket() const { return socket_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 45 | bool valid() const { return socket_ != INVALID_SOCKET; } |
| 46 | |
| 47 | bool Create(); |
| 48 | void Close(); |
| 49 | |
| 50 | protected: |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 51 | NativeSocket socket_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | // Represents an HTTP server socket. |
| 55 | class DataSocket : public SocketBase { |
| 56 | public: |
| 57 | enum RequestMethod { |
| 58 | INVALID, |
| 59 | GET, |
| 60 | POST, |
| 61 | OPTIONS, |
| 62 | }; |
| 63 | |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame] | 64 | explicit DataSocket(NativeSocket socket) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 65 | : SocketBase(socket), method_(INVALID), content_length_(0) {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 66 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 67 | ~DataSocket() {} |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | |
| 69 | static const char kCrossOriginAllowHeaders[]; |
| 70 | |
| 71 | bool headers_received() const { return method_ != INVALID; } |
| 72 | |
| 73 | RequestMethod method() const { return method_; } |
| 74 | |
| 75 | const std::string& request_path() const { return request_path_; } |
| 76 | std::string request_arguments() const; |
| 77 | |
| 78 | const std::string& data() const { return data_; } |
| 79 | |
| 80 | const std::string& content_type() const { return content_type_; } |
| 81 | |
| 82 | size_t content_length() const { return content_length_; } |
| 83 | |
| 84 | bool request_received() const { |
| 85 | return headers_received() && (method_ != POST || data_received()); |
| 86 | } |
| 87 | |
| 88 | bool data_received() const { |
| 89 | return method_ != POST || data_.length() >= content_length_; |
| 90 | } |
| 91 | |
| 92 | // Checks if the request path (minus arguments) matches a given path. |
| 93 | bool PathEquals(const char* path) const; |
| 94 | |
| 95 | // Called when we have received some data from clients. |
| 96 | // Returns false if an error occurred. |
| 97 | bool OnDataAvailable(bool* close_socket); |
| 98 | |
| 99 | // Send a raw buffer of bytes. |
| 100 | bool Send(const std::string& data) const; |
| 101 | |
Artem Titov | 36de9df | 2021-07-26 13:21:35 +0200 | [diff] [blame] | 102 | // Send an HTTP response. The `status` should start with a valid HTTP |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 103 | // response code, followed by a string. E.g. "200 OK". |
Artem Titov | 36de9df | 2021-07-26 13:21:35 +0200 | [diff] [blame] | 104 | // If `connection_close` is set to true, an extra "Connection: close" HTTP |
| 105 | // header will be included. `content_type` is the mime content type, not |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 106 | // including the "Content-Type: " string. |
Artem Titov | 36de9df | 2021-07-26 13:21:35 +0200 | [diff] [blame] | 107 | // `extra_headers` should be either empty or a list of headers where each |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 108 | // header terminates with "\r\n". |
Artem Titov | 36de9df | 2021-07-26 13:21:35 +0200 | [diff] [blame] | 109 | // `data` is the body of the message. It's length will be specified via |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 110 | // a "Content-Length" header. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 111 | bool Send(const std::string& status, |
| 112 | bool connection_close, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | const std::string& content_type, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | const std::string& extra_headers, |
| 115 | const std::string& data) const; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 116 | |
| 117 | // Clears all held state and prepares the socket for receiving a new request. |
| 118 | void Clear(); |
| 119 | |
| 120 | protected: |
| 121 | // A fairly relaxed HTTP header parser. Parses the method, path and |
| 122 | // content length (POST only) of a request. |
| 123 | // Returns true if a valid request was received and no errors occurred. |
| 124 | bool ParseHeaders(); |
| 125 | |
| 126 | // Figures out whether the request is a GET or POST and what path is |
| 127 | // being requested. |
| 128 | bool ParseMethodAndPath(const char* begin, size_t len); |
| 129 | |
| 130 | // Determines the length of the body and it's mime type. |
| 131 | bool ParseContentLengthAndType(const char* headers, size_t length); |
| 132 | |
| 133 | protected: |
| 134 | RequestMethod method_; |
| 135 | size_t content_length_; |
| 136 | std::string content_type_; |
| 137 | std::string request_path_; |
| 138 | std::string request_headers_; |
| 139 | std::string data_; |
| 140 | }; |
| 141 | |
| 142 | // The server socket. Accepts connections and generates DataSocket instances |
| 143 | // for each new connection. |
| 144 | class ListeningSocket : public SocketBase { |
| 145 | public: |
| 146 | ListeningSocket() {} |
| 147 | |
| 148 | bool Listen(unsigned short port); |
| 149 | DataSocket* Accept() const; |
| 150 | }; |
| 151 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 152 | #endif // EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |