Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/services/network/http_connection_impl.h" | 5 #include "mojo/services/network/http_connection_impl.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "base/bind_helpers.h" | |
| 10 #include "base/callback.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 #include "mojo/common/handle_watcher.h" | |
| 9 #include "mojo/services/network/http_server_impl.h" | 14 #include "mojo/services/network/http_server_impl.h" |
| 10 #include "mojo/services/network/net_adapters.h" | 15 #include "mojo/services/network/net_adapters.h" |
| 11 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/http/http_request_headers.h" | |
| 18 #include "net/http/http_status_code.h" | |
| 12 #include "net/server/http_server.h" | 19 #include "net/server/http_server.h" |
| 13 #include "net/server/http_server_request_info.h" | 20 #include "net/server/http_server_request_info.h" |
| 21 #include "net/server/http_server_response_info.h" | |
| 22 #include "third_party/mojo/src/mojo/public/cpp/bindings/type_converter.h" | |
| 23 #include "third_party/mojo/src/mojo/public/cpp/system/data_pipe.h" | |
| 14 | 24 |
| 15 namespace mojo { | 25 namespace mojo { |
| 16 | 26 |
| 27 // SimpleDataPipeReader reads till end-of-file, stores the data in a string and | |
| 28 // notifies completion. | |
| 29 class HttpConnectionImpl::SimpleDataPipeReader { | |
| 30 public: | |
| 31 using CompletionCallback = | |
| 32 base::Callback<void(SimpleDataPipeReader*, scoped_ptr<std::string>)>; | |
| 33 | |
| 34 SimpleDataPipeReader() {} | |
| 35 ~SimpleDataPipeReader() {} | |
| 36 | |
| 37 void Start(ScopedDataPipeConsumerHandle consumer, | |
| 38 const CompletionCallback& completion_callback) { | |
| 39 DCHECK(consumer.is_valid() && !consumer_.is_valid()); | |
| 40 consumer_ = consumer.Pass(); | |
| 41 completion_callback_ = completion_callback; | |
| 42 buffer_.reset(new std::string); | |
| 43 ReadMore(); | |
| 44 } | |
| 45 | |
| 46 private: | |
| 47 void ReadMore() { | |
| 48 const void* buf; | |
| 49 uint32_t buf_size; | |
| 50 MojoResult rv = BeginReadDataRaw(consumer_.get(), &buf, &buf_size, | |
| 51 MOJO_READ_DATA_FLAG_NONE); | |
| 52 if (rv == MOJO_RESULT_OK) { | |
| 53 buffer_->append(static_cast<const char*>(buf), buf_size); | |
| 54 EndReadDataRaw(consumer_.get(), buf_size); | |
| 55 WaitToReadMore(); | |
| 56 } else if (rv == MOJO_RESULT_SHOULD_WAIT) { | |
| 57 WaitToReadMore(); | |
| 58 } else if (rv == MOJO_RESULT_FAILED_PRECONDITION) { | |
| 59 // We reached end-of-file. | |
| 60 completion_callback_.Run(this, buffer_.Pass()); | |
| 61 // Note: This object may have been destroyed in the callback. | |
| 62 } else { | |
| 63 CHECK(false); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 void WaitToReadMore() { | |
| 68 watcher_.Start(consumer_.get(), MOJO_HANDLE_SIGNAL_READABLE, | |
| 69 MOJO_DEADLINE_INDEFINITE, | |
| 70 base::Bind(&SimpleDataPipeReader::OnHandleReady, | |
| 71 base::Unretained(this))); | |
| 72 } | |
| 73 | |
| 74 void OnHandleReady(MojoResult result) { ReadMore(); } | |
| 75 | |
| 76 ScopedDataPipeConsumerHandle consumer_; | |
| 77 common::HandleWatcher watcher_; | |
| 78 CompletionCallback completion_callback_; | |
| 79 scoped_ptr<std::string> buffer_; | |
| 80 | |
| 81 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); | |
| 82 }; | |
| 83 | |
| 84 template <> | |
| 85 struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> { | |
| 86 static URLRequestPtr Convert(const net::HttpServerRequestInfo& obj) { | |
| 87 URLRequestPtr request(URLRequest::New()); | |
| 88 request->url = obj.path; | |
| 89 request->method = obj.method; | |
| 90 request->headers.resize(obj.headers.size()); | |
| 91 size_t index = 0; | |
| 92 for (const auto& item : obj.headers) { | |
| 93 HTTPHeaderPtr header(HTTPHeader::New()); | |
| 94 header->name = item.first; | |
| 95 header->value = item.second; | |
| 96 request->headers[index++] = header.Pass(); | |
| 97 } | |
| 98 if (!obj.data.empty()) { | |
| 99 uint32_t num_bytes = static_cast<uint32_t>(obj.data.size()); | |
| 100 MojoCreateDataPipeOptions options; | |
| 101 options.struct_size = sizeof(MojoCreateDataPipeOptions); | |
| 102 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | |
| 103 options.element_num_bytes = 1; | |
| 104 options.capacity_num_bytes = num_bytes; | |
| 105 DataPipe data_pipe(options); | |
| 106 request->body.push_back(data_pipe.consumer_handle.Pass()); | |
| 107 MojoResult result = | |
| 108 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), | |
| 109 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | |
| 110 DCHECK_EQ(MOJO_RESULT_OK, result); | |
|
jam
2015/05/15 06:46:43
nit: perhaps CHECK so that we know if we have to h
yzshen1
2015/05/15 17:22:54
Done.
| |
| 111 } | |
| 112 return request.Pass(); | |
| 113 } | |
| 114 }; | |
| 115 | |
| 17 HttpConnectionImpl::HttpConnectionImpl(int connection_id, | 116 HttpConnectionImpl::HttpConnectionImpl(int connection_id, |
| 18 HttpServerImpl* owner, | 117 HttpServerImpl* owner, |
| 19 HttpConnectionDelegatePtr delegate, | 118 HttpConnectionDelegatePtr delegate, |
| 20 HttpConnectionPtr* connection) | 119 HttpConnectionPtr* connection) |
| 21 : connection_id_(connection_id), | 120 : connection_id_(connection_id), |
| 22 owner_(owner), | 121 owner_(owner), |
| 23 delegate_(delegate.Pass()), | 122 delegate_(delegate.Pass()), |
| 24 binding_(this, connection) { | 123 binding_(this, connection), |
| 124 encountered_connection_error_(false) { | |
| 125 | |
| 25 binding_.set_error_handler(this); | 126 binding_.set_error_handler(this); |
| 26 delegate_.set_error_handler(this); | 127 delegate_.set_error_handler(this); |
| 27 } | 128 } |
| 28 | 129 |
| 29 HttpConnectionImpl::~HttpConnectionImpl() {} | 130 HttpConnectionImpl::~HttpConnectionImpl() { |
| 131 for (const auto& reader : response_body_readers_) | |
|
jam
2015/05/15 06:46:43
nit: STLDeleteElements
yzshen1
2015/05/15 17:22:54
Done.
| |
| 132 delete reader; | |
| 133 } | |
| 30 | 134 |
| 31 void HttpConnectionImpl::OnReceivedHttpRequest( | 135 void HttpConnectionImpl::OnReceivedHttpRequest( |
| 32 const net::HttpServerRequestInfo& info) { | 136 const net::HttpServerRequestInfo& info) { |
| 33 // TODO(yzshen): implement it. | 137 if (!delegate_) |
| 138 return; | |
| 139 | |
| 140 delegate_->OnReceivedRequest( | |
| 141 URLRequest::From(info), [this](URLResponsePtr response) { | |
| 142 if (response->body.is_valid()) { | |
| 143 SimpleDataPipeReader* reader = new SimpleDataPipeReader; | |
| 144 response_body_readers_.insert(reader); | |
| 145 reader->Start( | |
| 146 response->body.Pass(), | |
| 147 base::Bind(&HttpConnectionImpl::OnFinishedReadingResponseBody, | |
| 148 base::Unretained(this), base::Passed(&response))); | |
| 149 } else { | |
| 150 OnFinishedReadingResponseBody(response.Pass(), nullptr, nullptr); | |
| 151 } | |
| 152 }); | |
| 34 } | 153 } |
| 35 | 154 |
| 36 void HttpConnectionImpl::OnReceivedWebSocketRequest( | 155 void HttpConnectionImpl::OnReceivedWebSocketRequest( |
| 37 const net::HttpServerRequestInfo& info) { | 156 const net::HttpServerRequestInfo& info) { |
| 38 // TODO(yzshen): implement it. | 157 // TODO(yzshen): implement it. |
| 39 } | 158 } |
| 40 | 159 |
| 41 void HttpConnectionImpl::OnReceivedWebSocketMessage(const std::string& data) { | 160 void HttpConnectionImpl::OnReceivedWebSocketMessage(const std::string& data) { |
| 42 // TODO(yzshen): implement it. | 161 // TODO(yzshen): implement it. |
| 43 } | 162 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 58 const SetReceiveBufferSizeCallback& callback) { | 177 const SetReceiveBufferSizeCallback& callback) { |
| 59 if (size > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) | 178 if (size > static_cast<uint32_t>(std::numeric_limits<int32_t>::max())) |
| 60 size = std::numeric_limits<int32_t>::max(); | 179 size = std::numeric_limits<int32_t>::max(); |
| 61 | 180 |
| 62 owner_->server()->SetReceiveBufferSize( | 181 owner_->server()->SetReceiveBufferSize( |
| 63 connection_id_, static_cast<int32_t>(size)); | 182 connection_id_, static_cast<int32_t>(size)); |
| 64 callback.Run(MakeNetworkError(net::OK)); | 183 callback.Run(MakeNetworkError(net::OK)); |
| 65 } | 184 } |
| 66 | 185 |
| 67 void HttpConnectionImpl::OnConnectionError() { | 186 void HttpConnectionImpl::OnConnectionError() { |
| 68 // The proxy side of |binding_| or the impl side of |delegate_| has closed the | 187 if (!encountered_connection_error_) { |
|
jam
2015/05/15 06:46:43
why is this boolean needed? perhaps document why i
yzshen1
2015/05/15 17:22:54
I realized that based on the current code it won't
| |
| 69 // pipe. The connection is not needed anymore. | 188 // The proxy side of |binding_| or the impl side of |delegate_| has closed |
| 70 owner_->server()->Close(connection_id_); | 189 // the pipe. |
| 190 encountered_connection_error_ = true; | |
| 191 binding_.Close(); | |
| 192 delegate_.reset(); | |
| 193 | |
| 194 // Don't close the connection until all pending responses are sent. | |
| 195 if (response_body_readers_.empty()) | |
| 196 owner_->server()->Close(connection_id_); | |
| 197 } | |
| 198 } | |
| 199 | |
| 200 void HttpConnectionImpl::OnFinishedReadingResponseBody( | |
| 201 URLResponsePtr response, | |
| 202 SimpleDataPipeReader* reader, | |
| 203 scoped_ptr<std::string> body) { | |
| 204 if (reader) { | |
| 205 delete reader; | |
| 206 response_body_readers_.erase(reader); | |
| 207 } | |
| 208 | |
| 209 net::HttpServerResponseInfo info( | |
| 210 static_cast<net::HttpStatusCode>(response->status_code)); | |
| 211 | |
| 212 std::string content_type; | |
| 213 for (size_t i = 0; i < response->headers.size(); ++i) { | |
| 214 const HTTPHeader& header = *(response->headers[i]); | |
| 215 | |
| 216 if (body) { | |
| 217 // net::HttpServerResponseInfo::SetBody() automatically sets | |
| 218 // Content-Length and Content-Types, so skip the two here. | |
| 219 // | |
| 220 // TODO(yzshen): Consider adding to net::HttpServerResponseInfo a simple | |
| 221 // setter for body which doesn't fiddle with headers. | |
| 222 if (base::strcasecmp(header.name.data(), | |
| 223 net::HttpRequestHeaders::kContentLength) == 0) { | |
| 224 continue; | |
| 225 } else if (base::strcasecmp(header.name.data(), | |
| 226 net::HttpRequestHeaders::kContentType) == 0) { | |
| 227 content_type = header.value; | |
| 228 continue; | |
| 229 } | |
| 230 } | |
| 231 info.AddHeader(header.name, header.value); | |
| 232 } | |
| 233 | |
| 234 if (body) | |
| 235 info.SetBody(*body, content_type); | |
| 236 | |
| 237 owner_->server()->SendResponse(connection_id_, info); | |
| 238 | |
| 239 if (response_body_readers_.empty() && encountered_connection_error_) | |
| 240 owner_->server()->Close(connection_id_); | |
| 71 } | 241 } |
| 72 | 242 |
| 73 } // namespace mojo | 243 } // namespace mojo |
| OLD | NEW |