OLD | NEW |
---|---|
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_CONNECTION_H_ | 5 #ifndef NET_SERVER_HTTP_CONNECTION_H_ |
6 #define NET_SERVER_HTTP_CONNECTION_H_ | 6 #define NET_SERVER_HTTP_CONNECTION_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | |
13 #include "net/base/io_buffer.h" | |
12 #include "net/http/http_status_code.h" | 14 #include "net/http/http_status_code.h" |
13 | 15 |
14 namespace net { | 16 namespace net { |
15 | 17 |
16 class HttpServer; | 18 class HttpServer; |
19 class HttpServerRequestInfo; | |
17 class HttpServerResponseInfo; | 20 class HttpServerResponseInfo; |
18 class StreamListenSocket; | 21 class StreamSocket; |
19 class WebSocket; | 22 class WebSocket; |
20 | 23 |
mmenke
2014/05/23 19:20:58
Could you please document these classes?
byungchul
2014/05/30 00:19:02
Done.
| |
21 class HttpConnection { | 24 class HttpConnection : public base::SupportsWeakPtr<HttpConnection> { |
mmenke
2014/05/23 19:20:58
WeakPtrs should generally not be exposed to extern
Ryan Sleevi
2014/05/28 01:36:26
+1
Note that WeakPtrFactory should be the *last*
byungchul
2014/05/30 00:19:02
Removed here, and changed in HttpServer.
| |
22 public: | 25 public: |
26 class Delegate { | |
27 public: | |
28 // Called when new data is available. Data read can be retrieved by | |
29 // conn->read_buf(). Whenever delegate consumes data, it must call | |
30 // conn->read_buf()->DidConsume(). | |
31 virtual void DidRead(HttpConnection* conn) = 0; | |
32 | |
33 // Called when underlying socket has been closed. | |
34 virtual void DidClose(HttpConnection* conn) = 0; | |
mmenke
2014/05/23 19:20:58
DISALLOW_COPY_AND_ASSIGN
mmenke
2014/05/23 19:20:58
Write out connection (Google style guide discourag
byungchul
2014/05/30 00:19:02
Removed.
| |
35 }; | |
36 | |
37 // IOBuffer for data read. It has 3 parts: | |
38 // 1) consumed data: [base_->StartOfBuffer(), data()] | |
39 // 2) unconsumed data: [data(), base_->data()] | |
40 // 3) not used buffer: [base_->data(), | |
41 // base_->data() + base_->RemainingCapacity()] | |
42 class ReadIOBuffer : public IOBuffer { | |
43 public: | |
44 // Returns the bytes of unconsumed data. | |
45 size_t GetUnconsumedSize() const; | |
mmenke
2014/05/23 19:20:58
IOBuffers use ints everything. I think mixing siz
byungchul
2014/05/30 00:19:02
Done.
| |
46 | |
47 // Changes |data_| so that |data_| always points to the first unconsumed | |
48 // byte. | |
49 void DidConsume(size_t bytes); | |
50 | |
51 private: | |
52 friend class HttpConnection; | |
53 | |
54 ReadIOBuffer(); | |
55 virtual ~ReadIOBuffer(); | |
56 | |
57 // Returns IOBuffer to append more data read. | |
58 IOBuffer* GetUnusedIOBuffer() const; | |
59 // Returns capacity not used yet. | |
60 size_t GetUnusedCapacity() const; | |
61 | |
62 // Capacity of |base_|. | |
63 size_t GetCapacity() const; | |
64 void SetCapacity(size_t capacity); | |
65 | |
66 // More data is appended. Refresh |data_| if base_->data() has changed. | |
67 void DidRead(size_t bytes); | |
68 | |
69 scoped_refptr<GrowableIOBuffer> base_; | |
70 // Offset of data_ from base_->data(). | |
71 size_t consumed_offset_; | |
mmenke
2014/05/23 19:20:58
DISALLOW_COPY_AND_ASSIGN
byungchul
2014/05/30 00:19:02
Done.
| |
72 }; | |
73 | |
74 HttpConnection(int id, scoped_ptr<StreamSocket> socket, Delegate* delegate); | |
23 ~HttpConnection(); | 75 ~HttpConnection(); |
24 | 76 |
25 void Send(const std::string& data); | 77 void Send(const std::string& data); |
26 void Send(const char* bytes, int len); | 78 void Send(const char* bytes, int len); |
27 void Send(const HttpServerResponseInfo& response); | 79 void Send(const HttpServerResponseInfo& response); |
28 | 80 |
29 void Shift(int num_bytes); | 81 void Close(); |
30 | 82 |
31 const std::string& recv_data() const { return recv_data_; } | 83 // Upgrades this http connection to web socket connection. |
84 void UpgradeToWebSocket(const HttpServerRequestInfo& request, size_t* pos); | |
85 | |
32 int id() const { return id_; } | 86 int id() const { return id_; } |
87 StreamSocket* socket() const { return socket_.get(); } | |
88 WebSocket* web_socket() const { return web_socket_.get(); } | |
89 scoped_refptr<ReadIOBuffer> read_buf() const { return read_buf_; } | |
33 | 90 |
34 private: | 91 private: |
35 friend class HttpServer; | 92 class PendingWriteIOBuffer; |
36 static int last_id_; | |
37 | 93 |
38 HttpConnection(HttpServer* server, scoped_ptr<StreamListenSocket> sock); | 94 void DoReadLoop(int rv); |
95 void OnReadCompleted(int rv); | |
96 int DidRead(int rv); | |
39 | 97 |
40 HttpServer* server_; | 98 void DoWriteLoop(int rv); |
41 scoped_ptr<StreamListenSocket> socket_; | 99 void OnWriteCompleted(int rv); |
100 int DidWrite(int rv); | |
101 | |
102 void CloseInNextRunLoop(); | |
103 | |
104 const int id_; | |
105 const scoped_ptr<StreamSocket> socket_; | |
106 Delegate* const delegate_; | |
107 | |
108 const scoped_refptr<ReadIOBuffer> read_buf_; | |
109 const scoped_refptr<PendingWriteIOBuffer> pending_write_buf_; | |
110 | |
42 scoped_ptr<WebSocket> web_socket_; | 111 scoped_ptr<WebSocket> web_socket_; |
43 std::string recv_data_; | 112 |
44 int id_; | |
45 DISALLOW_COPY_AND_ASSIGN(HttpConnection); | 113 DISALLOW_COPY_AND_ASSIGN(HttpConnection); |
46 }; | 114 }; |
47 | 115 |
48 } // namespace net | 116 } // namespace net |
49 | 117 |
50 #endif // NET_SERVER_HTTP_CONNECTION_H_ | 118 #endif // NET_SERVER_HTTP_CONNECTION_H_ |
OLD | NEW |