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

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: Addressed comments. Created 6 years, 4 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>
9 #include <map> 8 #include <map>
9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/macros.h"
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 15
16 namespace net { 16 namespace net {
17 17
18 class HttpConnection; 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 {
25 public base::RefCountedThreadSafe<HttpServer> {
26 public: 27 public:
28 // Delegate to handle http/websocket events. Beware that any of these
29 // functions are not safe for http server destruction.
mmenke 2014/08/14 16:36:03 nit: Maybe "It's not safe to destroy the HttpServ
byungchul 2014/08/14 18:44:03 Done.
27 class Delegate { 30 class Delegate {
28 public: 31 public:
29 virtual void OnHttpRequest(int connection_id, 32 virtual void OnHttpRequest(int connection_id,
30 const HttpServerRequestInfo& info) = 0; 33 const HttpServerRequestInfo& info) = 0;
31
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
35 virtual void OnWebSocketMessage(int connection_id, 36 virtual void OnWebSocketMessage(int connection_id,
36 const std::string& data) = 0; 37 const std::string& data) = 0;
37
38 virtual void OnClose(int connection_id) = 0; 38 virtual void OnClose(int connection_id) = 0;
39
40 protected:
41 virtual ~Delegate() {}
42 }; 39 };
43 40
44 HttpServer(const StreamListenSocketFactory& socket_factory, 41 HttpServer(scoped_ptr<ServerSocket> server_socket,
45 HttpServer::Delegate* delegate); 42 HttpServer::Delegate* delegate);
43 ~HttpServer();
46 44
47 void AcceptWebSocket(int connection_id, 45 void AcceptWebSocket(int connection_id,
48 const HttpServerRequestInfo& request); 46 const HttpServerRequestInfo& request);
49 void SendOverWebSocket(int connection_id, const std::string& data); 47 void SendOverWebSocket(int connection_id, const std::string& data);
50 // Sends the provided data directly to the given connection. No validation is 48 // Sends the provided data directly to the given connection. No validation is
51 // performed that data constitutes a valid HTTP response. A valid HTTP 49 // performed that data constitutes a valid HTTP response. A valid HTTP
52 // response may be split across multiple calls to SendRaw. 50 // response may be split across multiple calls to SendRaw.
53 void SendRaw(int connection_id, const std::string& data); 51 void SendRaw(int connection_id, const std::string& data);
52 // TODO(byungchul): Consider to replace function name with SendResponseInfo
mmenke 2014/08/14 16:36:02 nit: "to replace" -> "replacing"
byungchul 2014/08/14 18:44:03 Done.
54 void SendResponse(int connection_id, const HttpServerResponseInfo& response); 53 void SendResponse(int connection_id, const HttpServerResponseInfo& response);
55 void Send(int connection_id, 54 void Send(int connection_id,
56 HttpStatusCode status_code, 55 HttpStatusCode status_code,
57 const std::string& data, 56 const std::string& data,
58 const std::string& mime_type); 57 const std::string& mime_type);
59 void Send200(int connection_id, 58 void Send200(int connection_id,
60 const std::string& data, 59 const std::string& data,
61 const std::string& mime_type); 60 const std::string& mime_type);
62 void Send404(int connection_id); 61 void Send404(int connection_id);
63 void Send500(int connection_id, const std::string& message); 62 void Send500(int connection_id, const std::string& message);
64 63
65 void Close(int connection_id); 64 void Close(int connection_id);
66 65
66 void SetReceiveBufferSize(int connection_id, int32 size);
67 void SetSendBufferSize(int connection_id, int32 size);
mmenke 2014/08/14 16:36:03 Should include the header for int32 (Not sure what
byungchul 2014/08/14 18:44:03 Done. Defined in base/basictypes.h.
68
67 // Copies the local address to |address|. Returns a network error code. 69 // Copies the local address to |address|. Returns a network error code.
68 int GetLocalAddress(IPEndPoint* address); 70 int GetLocalAddress(IPEndPoint* address);
69 71
70 // ListenSocketDelegate 72 private:
71 virtual void DidAccept(StreamListenSocket* server, 73 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 74
78 protected: 75 typedef std::map<int, HttpConnection*> IdToConnectionMap;
79 virtual ~HttpServer();
80 76
81 private: 77 void DoAcceptLoop();
82 friend class base::RefCountedThreadSafe<HttpServer>; 78 void OnAcceptCompleted(int rv);
83 friend class HttpConnection; 79 int HandleAcceptResult(int rv);
80
81 void DoReadLoop(HttpConnection* connection);
82 void OnReadCompleted(int connection_id, int rv);
83 int HandleReadResult(HttpConnection* conn, int rv);
84
85 void DoWriteLoop(HttpConnection* connection);
86 void OnWriteCompleted(int connection_id, int rv);
87 int HandleWriteResult(HttpConnection* conn, int rv);
84 88
85 // Expects the raw data to be stored in recv_data_. If parsing is successful, 89 // 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 90 // will remove the data parsed from recv_data_, leaving only the unused
87 // recv data. 91 // recv data.
88 bool ParseHeaders(HttpConnection* connection, 92 bool ParseHeaders(const char* data,
93 size_t data_len,
89 HttpServerRequestInfo* info, 94 HttpServerRequestInfo* info,
90 size_t* pos); 95 size_t* pos);
91 96
92 HttpConnection* FindConnection(int connection_id); 97 HttpConnection* FindConnection(int connection_id);
93 HttpConnection* FindConnection(StreamListenSocket* socket);
94 98
95 HttpServer::Delegate* delegate_; 99 const scoped_ptr<ServerSocket> server_socket_;
96 scoped_ptr<StreamListenSocket> server_; 100 scoped_ptr<StreamSocket> accepted_socket_;
97 typedef std::map<int, HttpConnection*> IdToConnectionMap; 101 HttpServer::Delegate* const delegate_;
102
103 int last_id_;
98 IdToConnectionMap id_to_connection_; 104 IdToConnectionMap id_to_connection_;
99 typedef std::map<StreamListenSocket*, HttpConnection*> SocketToConnectionMap; 105
100 SocketToConnectionMap socket_to_connection_; 106 base::WeakPtrFactory<HttpServer> weak_ptr_factory_;
101 107
102 DISALLOW_COPY_AND_ASSIGN(HttpServer); 108 DISALLOW_COPY_AND_ASSIGN(HttpServer);
103 }; 109 };
104 110
105 } // namespace net 111 } // namespace net
106 112
107 #endif // NET_SERVER_HTTP_SERVER_H_ 113 #endif // NET_SERVER_HTTP_SERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698