Chromium Code Reviews| 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 HttpServer; | |
| 18 class HttpServerRequestInfo; | 19 class HttpServerRequestInfo; |
| 20 class WebSocket; | |
| 21 | |
| 22 class Connection { | |
| 23 public: | |
| 24 void Send(const std::string& data); | |
| 25 void Send(const char* bytes, int len); | |
| 26 void Send200(const std::string& data, const std::string& content_type); | |
| 27 void Send404(); | |
| 28 void Send500(const std::string& message); | |
| 29 | |
| 30 const std::string& RecvData() const { return recv_data_; } | |
|
eroman
2011/08/02 18:46:41
nit: recv_data() for accessor.
| |
| 31 const int Id() const { return id_; } | |
|
eroman
2011/08/02 18:46:41
id()
| |
| 32 void Shift(int num_bytes); | |
| 33 | |
| 34 private: | |
| 35 static int lastId_; | |
|
eroman
2011/08/02 18:46:41
last_id_
Also please explain what this is.
| |
| 36 friend class HttpServer; | |
| 37 | |
| 38 Connection(HttpServer* server, ListenSocket* sock); | |
| 39 ~Connection(); | |
| 40 | |
| 41 void DetachSocket(); | |
| 42 | |
| 43 | |
|
eroman
2011/08/02 18:46:41
extra space
| |
| 44 HttpServer* server_; | |
| 45 scoped_refptr<ListenSocket> socket_; | |
| 46 scoped_ptr<WebSocket> web_socket_; | |
| 47 std::string recv_data_; | |
| 48 int id_; | |
| 49 | |
| 50 DISALLOW_COPY_AND_ASSIGN(Connection); | |
| 51 }; | |
| 19 | 52 |
| 20 class HttpServer : public ListenSocket::ListenSocketDelegate, | 53 class HttpServer : public ListenSocket::ListenSocketDelegate, |
| 21 public base::RefCountedThreadSafe<HttpServer> { | 54 public base::RefCountedThreadSafe<HttpServer> { |
| 22 public: | 55 public: |
| 23 class Delegate { | 56 class Delegate { |
| 24 public: | 57 public: |
| 25 virtual void OnHttpRequest(int connection_id, | 58 virtual void OnHttpRequest(int connection_id, |
| 26 const HttpServerRequestInfo& info) = 0; | 59 const HttpServerRequestInfo& info) = 0; |
| 27 | 60 |
| 28 virtual void OnWebSocketRequest(int connection_id, | 61 virtual void OnWebSocketRequest(int connection_id, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 46 void Send(int connection_id, const char* bytes, int len); | 79 void Send(int connection_id, const char* bytes, int len); |
| 47 void Send200(int connection_id, | 80 void Send200(int connection_id, |
| 48 const std::string& data, | 81 const std::string& data, |
| 49 const std::string& mime_type); | 82 const std::string& mime_type); |
| 50 void Send404(int connection_id); | 83 void Send404(int connection_id); |
| 51 void Send500(int connection_id, const std::string& message); | 84 void Send500(int connection_id, const std::string& message); |
| 52 void Close(int connection_id); | 85 void Close(int connection_id); |
| 53 | 86 |
| 54 private: | 87 private: |
| 55 friend class base::RefCountedThreadSafe<HttpServer>; | 88 friend class base::RefCountedThreadSafe<HttpServer>; |
| 56 class Connection { | |
| 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; | 89 friend class Connection; |
| 77 | 90 |
| 91 void AcceptWebSocketHyBi10(Connection* connection, | |
| 92 const HttpServerRequestInfo& request); | |
| 93 void SendOverWebSocketHyBi10(Connection* connection, const std::string& data); | |
| 94 | |
| 95 bool DidReadWebSocketHyBi10(Connection* connection, | |
| 96 char* data, | |
| 97 size_t length, | |
| 98 std::string* message); | |
| 99 | |
| 100 void AcceptWebSocketHixie76(Connection* connection, | |
| 101 const HttpServerRequestInfo& request); | |
| 102 void SendOverWebSocketHixie76(Connection* connection, | |
| 103 const std::string& data); | |
| 78 | 104 |
| 79 // ListenSocketDelegate | 105 // ListenSocketDelegate |
| 80 virtual void DidAccept(ListenSocket* server, ListenSocket* socket); | 106 virtual void DidAccept(ListenSocket* server, ListenSocket* socket); |
| 81 virtual void DidRead(ListenSocket* socket, const char* data, int len); | 107 virtual void DidRead(ListenSocket* socket, const char* data, int len); |
| 82 virtual void DidClose(ListenSocket* socket); | 108 virtual void DidClose(ListenSocket* socket); |
| 83 | 109 |
| 84 // Expects the raw data to be stored in recv_data_. If parsing is successful, | 110 // 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 | 111 // will remove the data parsed from recv_data_, leaving only the unused |
| 86 // recv data. | 112 // recv data. |
| 87 bool ParseHeaders(Connection* connection, | 113 bool ParseHeaders(Connection* connection, |
| 88 HttpServerRequestInfo* info, | 114 HttpServerRequestInfo* info, |
| 89 int* ppos); | 115 size_t* pos); |
| 90 | 116 |
| 91 Connection* FindConnection(int connection_id); | 117 Connection* FindConnection(int connection_id); |
| 92 Connection* FindConnection(ListenSocket* socket); | 118 Connection* FindConnection(ListenSocket* socket); |
| 93 | 119 |
| 94 HttpServer::Delegate* delegate_; | 120 HttpServer::Delegate* delegate_; |
| 95 scoped_refptr<ListenSocket> server_; | 121 scoped_refptr<ListenSocket> server_; |
| 96 typedef std::map<int, Connection*> IdToConnectionMap; | 122 typedef std::map<int, Connection*> IdToConnectionMap; |
| 97 IdToConnectionMap id_to_connection_; | 123 IdToConnectionMap id_to_connection_; |
| 98 typedef std::map<ListenSocket*, Connection*> SocketToConnectionMap; | 124 typedef std::map<ListenSocket*, Connection*> SocketToConnectionMap; |
| 99 SocketToConnectionMap socket_to_connection_; | 125 SocketToConnectionMap socket_to_connection_; |
| 100 | 126 |
| 101 DISALLOW_COPY_AND_ASSIGN(HttpServer); | 127 DISALLOW_COPY_AND_ASSIGN(HttpServer); |
| 102 }; | 128 }; |
| 103 | 129 |
| 104 } // namespace net | 130 } // namespace net |
| 105 | 131 |
| 106 #endif // NET_SERVER_HTTP_SERVER_H_ | 132 #endif // NET_SERVER_HTTP_SERVER_H_ |
| OLD | NEW |