henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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 | |
| 28 | #ifndef TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |
| 29 | #define TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |
| 30 | #pragma once |
| 31 | |
| 32 | #ifdef WIN32 |
| 33 | #include <winsock2.h> |
| 34 | typedef int socklen_t; |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 35 | typedef SOCKET NativeSocket; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 36 | #else |
| 37 | #include <netinet/in.h> |
| 38 | #include <sys/select.h> |
| 39 | #include <sys/socket.h> |
| 40 | #define closesocket close |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 41 | typedef int NativeSocket; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
| 43 | #ifndef SOCKET_ERROR |
| 44 | #define SOCKET_ERROR (-1) |
| 45 | #endif |
| 46 | |
| 47 | #ifndef INVALID_SOCKET |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 48 | #define INVALID_SOCKET static_cast<NativeSocket>(-1) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 49 | #endif |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 50 | #endif |
| 51 | |
| 52 | #include <string> |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 53 | |
| 54 | class SocketBase { |
| 55 | public: |
| 56 | SocketBase() : socket_(INVALID_SOCKET) { } |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 57 | explicit SocketBase(NativeSocket socket) : socket_(socket) { } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | ~SocketBase() { Close(); } |
| 59 | |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 60 | NativeSocket socket() const { return socket_; } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 61 | bool valid() const { return socket_ != INVALID_SOCKET; } |
| 62 | |
| 63 | bool Create(); |
| 64 | void Close(); |
| 65 | |
| 66 | protected: |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 67 | NativeSocket socket_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Represents an HTTP server socket. |
| 71 | class DataSocket : public SocketBase { |
| 72 | public: |
| 73 | enum RequestMethod { |
| 74 | INVALID, |
| 75 | GET, |
| 76 | POST, |
| 77 | OPTIONS, |
| 78 | }; |
| 79 | |
tommi@webrtc.org | 2c13f65 | 2014-11-28 10:37:31 +0000 | [diff] [blame^] | 80 | explicit DataSocket(NativeSocket socket) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | : SocketBase(socket), |
| 82 | method_(INVALID), |
| 83 | content_length_(0) { |
| 84 | } |
| 85 | |
| 86 | ~DataSocket() { |
| 87 | } |
| 88 | |
| 89 | static const char kCrossOriginAllowHeaders[]; |
| 90 | |
| 91 | bool headers_received() const { return method_ != INVALID; } |
| 92 | |
| 93 | RequestMethod method() const { return method_; } |
| 94 | |
| 95 | const std::string& request_path() const { return request_path_; } |
| 96 | std::string request_arguments() const; |
| 97 | |
| 98 | const std::string& data() const { return data_; } |
| 99 | |
| 100 | const std::string& content_type() const { return content_type_; } |
| 101 | |
| 102 | size_t content_length() const { return content_length_; } |
| 103 | |
| 104 | bool request_received() const { |
| 105 | return headers_received() && (method_ != POST || data_received()); |
| 106 | } |
| 107 | |
| 108 | bool data_received() const { |
| 109 | return method_ != POST || data_.length() >= content_length_; |
| 110 | } |
| 111 | |
| 112 | // Checks if the request path (minus arguments) matches a given path. |
| 113 | bool PathEquals(const char* path) const; |
| 114 | |
| 115 | // Called when we have received some data from clients. |
| 116 | // Returns false if an error occurred. |
| 117 | bool OnDataAvailable(bool* close_socket); |
| 118 | |
| 119 | // Send a raw buffer of bytes. |
| 120 | bool Send(const std::string& data) const; |
| 121 | |
| 122 | // Send an HTTP response. The |status| should start with a valid HTTP |
| 123 | // response code, followed by a string. E.g. "200 OK". |
| 124 | // If |connection_close| is set to true, an extra "Connection: close" HTTP |
| 125 | // header will be included. |content_type| is the mime content type, not |
| 126 | // including the "Content-Type: " string. |
| 127 | // |extra_headers| should be either empty or a list of headers where each |
| 128 | // header terminates with "\r\n". |
| 129 | // |data| is the body of the message. It's length will be specified via |
| 130 | // a "Content-Length" header. |
| 131 | bool Send(const std::string& status, bool connection_close, |
| 132 | const std::string& content_type, |
| 133 | const std::string& extra_headers, const std::string& data) const; |
| 134 | |
| 135 | // Clears all held state and prepares the socket for receiving a new request. |
| 136 | void Clear(); |
| 137 | |
| 138 | protected: |
| 139 | // A fairly relaxed HTTP header parser. Parses the method, path and |
| 140 | // content length (POST only) of a request. |
| 141 | // Returns true if a valid request was received and no errors occurred. |
| 142 | bool ParseHeaders(); |
| 143 | |
| 144 | // Figures out whether the request is a GET or POST and what path is |
| 145 | // being requested. |
| 146 | bool ParseMethodAndPath(const char* begin, size_t len); |
| 147 | |
| 148 | // Determines the length of the body and it's mime type. |
| 149 | bool ParseContentLengthAndType(const char* headers, size_t length); |
| 150 | |
| 151 | protected: |
| 152 | RequestMethod method_; |
| 153 | size_t content_length_; |
| 154 | std::string content_type_; |
| 155 | std::string request_path_; |
| 156 | std::string request_headers_; |
| 157 | std::string data_; |
| 158 | }; |
| 159 | |
| 160 | // The server socket. Accepts connections and generates DataSocket instances |
| 161 | // for each new connection. |
| 162 | class ListeningSocket : public SocketBase { |
| 163 | public: |
| 164 | ListeningSocket() {} |
| 165 | |
| 166 | bool Listen(unsigned short port); |
| 167 | DataSocket* Accept() const; |
| 168 | }; |
| 169 | |
| 170 | #endif // TALK_EXAMPLES_PEERCONNECTION_SERVER_DATA_SOCKET_H_ |