| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SERVER_H_ | 5 #ifndef NET_SERVER_HTTP_SERVER_H_ |
| 6 #define NET_SERVER_HTTP_SERVER_H_ | 6 #define NET_SERVER_HTTP_SERVER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "net/base/listen_socket.h" | 14 #include "net/base/listen_socket.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 class HttpConnection; |
| 18 class HttpServerRequestInfo; | 19 class HttpServerRequestInfo; |
| 20 class WebSocket; |
| 19 | 21 |
| 20 class HttpServer : public ListenSocket::ListenSocketDelegate, | 22 class HttpServer : public ListenSocket::ListenSocketDelegate, |
| 21 public base::RefCountedThreadSafe<HttpServer> { | 23 public base::RefCountedThreadSafe<HttpServer> { |
| 22 public: | 24 public: |
| 23 class Delegate { | 25 class Delegate { |
| 24 public: | 26 public: |
| 25 virtual void OnHttpRequest(int connection_id, | 27 virtual void OnHttpRequest(int connection_id, |
| 26 const HttpServerRequestInfo& info) = 0; | 28 const HttpServerRequestInfo& info) = 0; |
| 27 | 29 |
| 28 virtual void OnWebSocketRequest(int connection_id, | 30 virtual void OnWebSocketRequest(int connection_id, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 void Send(int connection_id, const char* bytes, int len); | 48 void Send(int connection_id, const char* bytes, int len); |
| 47 void Send200(int connection_id, | 49 void Send200(int connection_id, |
| 48 const std::string& data, | 50 const std::string& data, |
| 49 const std::string& mime_type); | 51 const std::string& mime_type); |
| 50 void Send404(int connection_id); | 52 void Send404(int connection_id); |
| 51 void Send500(int connection_id, const std::string& message); | 53 void Send500(int connection_id, const std::string& message); |
| 52 void Close(int connection_id); | 54 void Close(int connection_id); |
| 53 | 55 |
| 54 private: | 56 private: |
| 55 friend class base::RefCountedThreadSafe<HttpServer>; | 57 friend class base::RefCountedThreadSafe<HttpServer>; |
| 56 class Connection { | 58 friend class HttpConnection; |
| 57 private: | |
| 58 static int lastId_; | |
| 59 friend class HttpServer; | |
| 60 | |
| 61 Connection(HttpServer* server, ListenSocket* sock); | |
| 62 ~Connection(); | |
| 63 | |
| 64 void DetachSocket(); | |
| 65 | |
| 66 void Shift(int num_bytes); | |
| 67 | |
| 68 HttpServer* server_; | |
| 69 scoped_refptr<ListenSocket> socket_; | |
| 70 bool is_web_socket_; | |
| 71 std::string recv_data_; | |
| 72 int id_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(Connection); | |
| 75 }; | |
| 76 friend class Connection; | |
| 77 | |
| 78 | 59 |
| 79 // ListenSocketDelegate | 60 // ListenSocketDelegate |
| 80 virtual void DidAccept(ListenSocket* server, ListenSocket* socket); | 61 virtual void DidAccept(ListenSocket* server, ListenSocket* socket); |
| 81 virtual void DidRead(ListenSocket* socket, const char* data, int len); | 62 virtual void DidRead(ListenSocket* socket, const char* data, int len); |
| 82 virtual void DidClose(ListenSocket* socket); | 63 virtual void DidClose(ListenSocket* socket); |
| 83 | 64 |
| 84 // Expects the raw data to be stored in recv_data_. If parsing is successful, | 65 // Expects the raw data to be stored in recv_data_. If parsing is successful, |
| 85 // will remove the data parsed from recv_data_, leaving only the unused | 66 // will remove the data parsed from recv_data_, leaving only the unused |
| 86 // recv data. | 67 // recv data. |
| 87 bool ParseHeaders(Connection* connection, | 68 bool ParseHeaders(HttpConnection* connection, |
| 88 HttpServerRequestInfo* info, | 69 HttpServerRequestInfo* info, |
| 89 int* ppos); | 70 size_t* pos); |
| 90 | 71 |
| 91 Connection* FindConnection(int connection_id); | 72 HttpConnection* FindConnection(int connection_id); |
| 92 Connection* FindConnection(ListenSocket* socket); | 73 HttpConnection* FindConnection(ListenSocket* socket); |
| 93 | 74 |
| 94 HttpServer::Delegate* delegate_; | 75 HttpServer::Delegate* delegate_; |
| 95 scoped_refptr<ListenSocket> server_; | 76 scoped_refptr<ListenSocket> server_; |
| 96 typedef std::map<int, Connection*> IdToConnectionMap; | 77 typedef std::map<int, HttpConnection*> IdToConnectionMap; |
| 97 IdToConnectionMap id_to_connection_; | 78 IdToConnectionMap id_to_connection_; |
| 98 typedef std::map<ListenSocket*, Connection*> SocketToConnectionMap; | 79 typedef std::map<ListenSocket*, HttpConnection*> SocketToConnectionMap; |
| 99 SocketToConnectionMap socket_to_connection_; | 80 SocketToConnectionMap socket_to_connection_; |
| 100 | 81 |
| 101 DISALLOW_COPY_AND_ASSIGN(HttpServer); | 82 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 102 }; | 83 }; |
| 103 | 84 |
| 104 } // namespace net | 85 } // namespace net |
| 105 | 86 |
| 106 #endif // NET_SERVER_HTTP_SERVER_H_ | 87 #endif // NET_SERVER_HTTP_SERVER_H_ |
| OLD | NEW |