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 #include "net/server/http_connection.h" | 5 #include "net/server/http_connection.h" |
6 | 6 |
7 #include "base/strings/string_util.h" | |
8 #include "base/strings/stringprintf.h" | |
9 #include "net/server/http_server.h" | 7 #include "net/server/http_server.h" |
| 8 #include "net/server/http_server_response_info.h" |
10 #include "net/server/web_socket.h" | 9 #include "net/server/web_socket.h" |
11 #include "net/socket/stream_listen_socket.h" | 10 #include "net/socket/stream_listen_socket.h" |
12 | 11 |
13 namespace net { | 12 namespace net { |
14 | 13 |
15 int HttpConnection::last_id_ = 0; | 14 int HttpConnection::last_id_ = 0; |
16 | 15 |
17 void HttpConnection::Send(const std::string& data) { | 16 void HttpConnection::Send(const std::string& data) { |
18 if (!socket_.get()) | 17 if (!socket_.get()) |
19 return; | 18 return; |
20 socket_->Send(data); | 19 socket_->Send(data); |
21 } | 20 } |
22 | 21 |
23 void HttpConnection::Send(const char* bytes, int len) { | 22 void HttpConnection::Send(const char* bytes, int len) { |
24 if (!socket_.get()) | 23 if (!socket_.get()) |
25 return; | 24 return; |
26 socket_->Send(bytes, len); | 25 socket_->Send(bytes, len); |
27 } | 26 } |
28 | 27 |
29 void HttpConnection::Send(HttpStatusCode status_code, | 28 void HttpConnection::Send(const HttpServerResponseInfo& response) { |
30 const std::string& data, | 29 Send(response.Serialize()); |
31 const std::string& content_type) { | |
32 if (!socket_.get()) | |
33 return; | |
34 | |
35 socket_->Send(base::StringPrintf( | |
36 "HTTP/1.1 %d %s\r\n" | |
37 "Content-Type:%s\r\n" | |
38 "Content-Length:%d\r\n" | |
39 "\r\n", | |
40 status_code, | |
41 GetHttpReasonPhrase(status_code), | |
42 content_type.c_str(), | |
43 static_cast<int>(data.length()))); | |
44 socket_->Send(data); | |
45 } | 30 } |
46 | 31 |
47 HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock) | 32 HttpConnection::HttpConnection(HttpServer* server, StreamListenSocket* sock) |
48 : server_(server), | 33 : server_(server), |
49 socket_(sock) { | 34 socket_(sock) { |
50 id_ = last_id_++; | 35 id_ = last_id_++; |
51 } | 36 } |
52 | 37 |
53 HttpConnection::~HttpConnection() { | 38 HttpConnection::~HttpConnection() { |
54 DetachSocket(); | 39 DetachSocket(); |
55 server_->delegate_->OnClose(id_); | 40 server_->delegate_->OnClose(id_); |
56 } | 41 } |
57 | 42 |
58 void HttpConnection::DetachSocket() { | 43 void HttpConnection::DetachSocket() { |
59 socket_ = NULL; | 44 socket_ = NULL; |
60 } | 45 } |
61 | 46 |
62 void HttpConnection::Shift(int num_bytes) { | 47 void HttpConnection::Shift(int num_bytes) { |
63 recv_data_ = recv_data_.substr(num_bytes); | 48 recv_data_ = recv_data_.substr(num_bytes); |
64 } | 49 } |
65 | 50 |
66 } // namespace net | 51 } // namespace net |
OLD | NEW |