OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef NET_BASE_TOOLS_HTTP_LISTEN_SOCKET_H_ |
| 6 #define NET_BASE_TOOLS_HTTP_LISTEN_SOCKET_H_ |
| 7 |
| 8 #include "base/message_loop.h" |
| 9 #include "net/base/listen_socket.h" |
| 10 |
| 11 class HttpServerRequestInfo; |
| 12 class HttpServerResponseInfo; |
| 13 |
| 14 // Implements a simple HTTP listen socket on top of the raw socket interface. |
| 15 class HttpListenSocket : public ListenSocket, |
| 16 public ListenSocket::ListenSocketDelegate { |
| 17 public: |
| 18 class Delegate { |
| 19 public: |
| 20 virtual void OnRequest(HttpListenSocket* connection, |
| 21 HttpServerRequestInfo* info) = 0; |
| 22 }; |
| 23 |
| 24 static HttpListenSocket* Listen(const std::string& ip, int port, |
| 25 HttpListenSocket::Delegate* delegate); |
| 26 virtual ~HttpListenSocket(); |
| 27 |
| 28 void Listen() { ListenSocket::Listen(); } |
| 29 virtual void Accept(); |
| 30 |
| 31 // Send a server response. |
| 32 // TODO(mbelshe): make this capable of non-ascii data. |
| 33 void Respond(HttpServerResponseInfo* info, std::string& data); |
| 34 |
| 35 // ListenSocketDelegate |
| 36 virtual void DidAccept(ListenSocket* server, ListenSocket* connection); |
| 37 virtual void DidRead(ListenSocket* connection, const std::string& data); |
| 38 virtual void DidClose(ListenSocket* sock); |
| 39 |
| 40 private: |
| 41 static const int kReadBufSize = 16 * 1024; |
| 42 HttpListenSocket(SOCKET s, HttpListenSocket::Delegate* del); |
| 43 |
| 44 // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 45 // will remove the data parsed from recv_data_, leaving only the unused |
| 46 // recv data. |
| 47 HttpServerRequestInfo* ParseHeaders(); |
| 48 |
| 49 HttpListenSocket::Delegate* delegate_; |
| 50 std::string recv_data_; |
| 51 |
| 52 DISALLOW_EVIL_CONSTRUCTORS(HttpListenSocket); |
| 53 }; |
| 54 |
| 55 #endif // NET_BASE_TOOLS_HTTP_LISTEN_SOCKET_H_ |
OLD | NEW |