Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(45)

Side by Side Diff: net/server/http_server.h

Issue 296053012: Replace StreamListenSocket with StreamSocket in HttpServer. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 7
8 #include <list> 8 #include <list>
mmenke 2014/05/23 19:20:58 I don't believe this is used? (And wasn't before,
byungchul 2014/05/30 00:19:02 Done.
9 #include <map> 9 #include <map>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
mmenke 2014/05/23 19:20:58 Should use macros.h instead (To make sure we get b
byungchul 2014/05/30 00:19:02 Done.
12 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
13 #include "net/http/http_status_code.h" 14 #include "net/http/http_status_code.h"
14 #include "net/socket/stream_listen_socket.h" 15 #include "net/server/http_connection.h"
15 16
16 namespace net { 17 namespace net {
17 18
18 class HttpConnection;
19 class HttpServerRequestInfo; 19 class HttpServerRequestInfo;
20 class HttpServerResponseInfo; 20 class HttpServerResponseInfo;
21 class IPEndPoint; 21 class IPEndPoint;
22 class ServerSocket;
23 class StreamSocket;
22 class WebSocket; 24 class WebSocket;
23 25
24 class HttpServer : public StreamListenSocket::Delegate, 26 class HttpServer : public HttpConnection::Delegate,
25 public base::RefCountedThreadSafe<HttpServer> { 27 public base::SupportsWeakPtr<HttpServer> {
mmenke 2014/05/23 19:20:58 WeakPtrs shouldn't be exposed to external classes
mmenke 2014/05/23 19:20:58 This class should be declared with NEXT_EXPORT, sh
byungchul 2014/05/28 01:19:35 NEXT_EXPORT is only for shared lib. Not sure which
Ryan Sleevi 2014/05/28 01:36:26 All of our platforms do component builds on the wa
byungchul 2014/05/30 00:19:02 Done.
byungchul 2014/06/08 17:24:27 Turned out that http_server is built as a static l
26 public: 28 public:
27 class Delegate { 29 class Delegate {
28 public: 30 public:
29 virtual void OnHttpRequest(int connection_id, 31 virtual void OnHttpRequest(int connection_id,
30 const HttpServerRequestInfo& info) = 0; 32 const HttpServerRequestInfo& info) = 0;
31 33
32 virtual void OnWebSocketRequest(int connection_id, 34 virtual void OnWebSocketRequest(int connection_id,
33 const HttpServerRequestInfo& info) = 0; 35 const HttpServerRequestInfo& info) = 0;
34 36
35 virtual void OnWebSocketMessage(int connection_id, 37 virtual void OnWebSocketMessage(int connection_id,
36 const std::string& data) = 0; 38 const std::string& data) = 0;
37 39
38 virtual void OnClose(int connection_id) = 0; 40 virtual void OnClose(int connection_id) = 0;
39
40 protected:
41 virtual ~Delegate() {}
42 }; 41 };
43 42
44 HttpServer(const StreamListenSocketFactory& socket_factory, 43 HttpServer(scoped_ptr<ServerSocket> server_socket,
45 HttpServer::Delegate* delegate); 44 HttpServer::Delegate* delegate);
45 virtual ~HttpServer();
46 46
47 void AcceptWebSocket(int connection_id, 47 void AcceptWebSocket(int connection_id,
48 const HttpServerRequestInfo& request); 48 const HttpServerRequestInfo& request);
49 void SendOverWebSocket(int connection_id, const std::string& data); 49 void SendOverWebSocket(int connection_id, const std::string& data);
50 // Sends the provided data directly to the given connection. No validation is 50 // Sends the provided data directly to the given connection. No validation is
51 // performed that data constitutes a valid HTTP response. A valid HTTP 51 // performed that data constitutes a valid HTTP response. A valid HTTP
52 // response may be split across multiple calls to SendRaw. 52 // response may be split across multiple calls to SendRaw.
53 void SendRaw(int connection_id, const std::string& data); 53 void SendRaw(int connection_id, const std::string& data);
54 void SendResponse(int connection_id, const HttpServerResponseInfo& response); 54 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
55 void Send(int connection_id, 55 void Send(int connection_id,
56 HttpStatusCode status_code, 56 HttpStatusCode status_code,
57 const std::string& data, 57 const std::string& data,
58 const std::string& mime_type); 58 const std::string& mime_type);
59 void Send200(int connection_id, 59 void Send200(int connection_id,
60 const std::string& data, 60 const std::string& data,
61 const std::string& mime_type); 61 const std::string& mime_type);
62 void Send404(int connection_id); 62 void Send404(int connection_id);
63 void Send500(int connection_id, const std::string& message); 63 void Send500(int connection_id, const std::string& message);
64 64
65 void Close(int connection_id); 65 void Close(int connection_id);
66 66
67 // Copies the local address to |address|. Returns a network error code. 67 // Copies the local address to |address|. Returns a network error code.
68 int GetLocalAddress(IPEndPoint* address); 68 int GetLocalAddress(IPEndPoint* address);
69 69
70 // ListenSocketDelegate 70 private:
71 virtual void DidAccept(StreamListenSocket* server, 71 friend class HttpServerTest;
72 scoped_ptr<StreamListenSocket> socket) OVERRIDE;
73 virtual void DidRead(StreamListenSocket* socket,
74 const char* data,
75 int len) OVERRIDE;
76 virtual void DidClose(StreamListenSocket* socket) OVERRIDE;
77 72
78 protected: 73 typedef std::map<int, HttpConnection*> IdToConnectionMap;
79 virtual ~HttpServer();
80 74
81 private: 75 void DoAcceptLoop(int rv);
82 friend class base::RefCountedThreadSafe<HttpServer>; 76 void OnAcceptCompleted(int rv);
83 friend class HttpConnection; 77 int DidAccept(int rv);
78
79 // HttpConnection::Delegate
80 virtual void DidRead(HttpConnection* conn) OVERRIDE;
81 virtual void DidClose(HttpConnection* conn) OVERRIDE;
84 82
85 // Expects the raw data to be stored in recv_data_. If parsing is successful, 83 // Expects the raw data to be stored in recv_data_. If parsing is successful,
86 // will remove the data parsed from recv_data_, leaving only the unused 84 // will remove the data parsed from recv_data_, leaving only the unused
87 // recv data. 85 // recv data.
88 bool ParseHeaders(HttpConnection* connection, 86 bool ParseHeaders(const char* data,
87 size_t data_len,
89 HttpServerRequestInfo* info, 88 HttpServerRequestInfo* info,
90 size_t* pos); 89 size_t* pos);
91 90
92 HttpConnection* FindConnection(int connection_id); 91 HttpConnection* FindConnection(int connection_id);
93 HttpConnection* FindConnection(StreamListenSocket* socket);
94 92
95 HttpServer::Delegate* delegate_; 93 const scoped_ptr<ServerSocket> server_socket_;
96 scoped_ptr<StreamListenSocket> server_; 94 scoped_ptr<StreamSocket> accepted_socket_;
97 typedef std::map<int, HttpConnection*> IdToConnectionMap; 95 HttpServer::Delegate* const delegate_;
96
97 int last_id_;
98 IdToConnectionMap id_to_connection_; 98 IdToConnectionMap id_to_connection_;
99 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap;
100 SocketToConnectionMap socket_to_connection_;
101 99
102 DISALLOW_COPY_AND_ASSIGN(HttpServer); 100 DISALLOW_COPY_AND_ASSIGN(HttpServer);
103 }; 101 };
104 102
105 } // namespace net 103 } // namespace net
106 104
107 #endif // NET_SERVER_HTTP_SERVER_H_ 105 #endif // NET_SERVER_HTTP_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698