OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef NET_SERVER_HTTP_LISTEN_SOCKET_H_ | 5 #ifndef NET_SERVER_HTTP_LISTEN_SOCKET_H_ |
6 #define NET_SERVER_HTTP_LISTEN_SOCKET_H_ | 6 #define NET_SERVER_HTTP_LISTEN_SOCKET_H_ |
7 | 7 |
8 #include "net/base/listen_socket.h" | 8 #include "net/base/listen_socket.h" |
9 | 9 |
10 class HttpServerRequestInfo; | 10 class HttpServerRequestInfo; |
11 | 11 |
12 // Implements a simple HTTP listen socket on top of the raw socket interface. | 12 // Implements a simple HTTP listen socket on top of the raw socket interface. |
13 class HttpListenSocket : public ListenSocket, | 13 class HttpListenSocket : public ListenSocket, |
14 public ListenSocket::ListenSocketDelegate { | 14 public ListenSocket::ListenSocketDelegate { |
15 public: | 15 public: |
16 class Delegate { | 16 class Delegate { |
17 public: | 17 public: |
18 virtual void OnHttpRequest(HttpListenSocket* socket, | 18 virtual void OnHttpRequest(HttpListenSocket* socket, |
19 HttpServerRequestInfo* info) = 0; | 19 const HttpServerRequestInfo& info) = 0; |
20 | 20 |
21 virtual void OnWebSocketRequest(HttpListenSocket* socket, | 21 virtual void OnWebSocketRequest(HttpListenSocket* socket, |
22 HttpServerRequestInfo* info) = 0; | 22 const HttpServerRequestInfo& info) = 0; |
23 | 23 |
24 virtual void OnWebSocketMessage(HttpListenSocket* socket, | 24 virtual void OnWebSocketMessage(HttpListenSocket* socket, |
25 const std::string& data) = 0; | 25 const std::string& data) = 0; |
26 | 26 |
27 virtual void OnClose(HttpListenSocket* socket) = 0; | 27 virtual void OnClose(HttpListenSocket* socket) = 0; |
28 protected: | 28 protected: |
29 virtual ~Delegate() {} | 29 virtual ~Delegate() {} |
30 }; | 30 }; |
31 | 31 |
| 32 virtual void Accept(); |
| 33 |
32 static HttpListenSocket* Listen(const std::string& ip, | 34 static HttpListenSocket* Listen(const std::string& ip, |
33 int port, | 35 int port, |
34 HttpListenSocket::Delegate* delegate); | 36 HttpListenSocket::Delegate* delegate); |
35 | 37 |
36 void AcceptWebSocket(HttpServerRequestInfo* request); | 38 void AcceptWebSocket(const HttpServerRequestInfo& request); |
37 | 39 |
38 void SendOverWebSocket(const std::string& data); | 40 void SendOverWebSocket(const std::string& data); |
39 | 41 |
| 42 void Send200(const std::string& data, const std::string& mime_type); |
| 43 void Send404(); |
| 44 void Send500(const std::string& message); |
| 45 |
| 46 void Close() { ListenSocket::Close(); } |
40 void Listen() { ListenSocket::Listen(); } | 47 void Listen() { ListenSocket::Listen(); } |
41 virtual void Accept(); | |
42 | 48 |
43 // ListenSocketDelegate | 49 // ListenSocketDelegate |
44 virtual void DidAccept(ListenSocket* server, ListenSocket* connection); | 50 virtual void DidAccept(ListenSocket* server, ListenSocket* connection); |
45 virtual void DidRead(ListenSocket* connection, const char* data, int len); | 51 virtual void DidRead(ListenSocket* connection, const char* data, int len); |
46 virtual void DidClose(ListenSocket* sock); | 52 virtual void DidClose(ListenSocket* sock); |
47 | 53 |
48 private: | 54 private: |
49 static const int kReadBufSize = 16 * 1024; | 55 static const int kReadBufSize = 16 * 1024; |
50 HttpListenSocket(SOCKET s, HttpListenSocket::Delegate* del); | 56 HttpListenSocket(SOCKET s, HttpListenSocket::Delegate* del); |
51 virtual ~HttpListenSocket(); | 57 virtual ~HttpListenSocket(); |
52 | 58 |
53 // Expects the raw data to be stored in recv_data_. If parsing is successful, | 59 // Expects the raw data to be stored in recv_data_. If parsing is successful, |
54 // will remove the data parsed from recv_data_, leaving only the unused | 60 // will remove the data parsed from recv_data_, leaving only the unused |
55 // recv data. | 61 // recv data. |
56 HttpServerRequestInfo* ParseHeaders(); | 62 bool ParseHeaders(HttpServerRequestInfo* info); |
57 | 63 |
58 HttpListenSocket::Delegate* delegate_; | 64 HttpListenSocket::Delegate* delegate_; |
59 bool is_web_socket_; | 65 bool is_web_socket_; |
60 std::string recv_data_; | 66 std::string recv_data_; |
61 | 67 |
62 DISALLOW_COPY_AND_ASSIGN(HttpListenSocket); | 68 DISALLOW_COPY_AND_ASSIGN(HttpListenSocket); |
63 }; | 69 }; |
64 | 70 |
65 #endif // NET_SERVER_HTTP_LISTEN_SOCKET_H_ | 71 #endif // NET_SERVER_HTTP_LISTEN_SOCKET_H_ |
OLD | NEW |