| 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" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 75 |
| 76 ScopedDataPipeConsumerHandle consumer_; | 76 ScopedDataPipeConsumerHandle consumer_; |
| 77 common::HandleWatcher watcher_; | 77 common::HandleWatcher watcher_; |
| 78 CompletionCallback completion_callback_; | 78 CompletionCallback completion_callback_; |
| 79 scoped_ptr<std::string> buffer_; | 79 scoped_ptr<std::string> buffer_; |
| 80 | 80 |
| 81 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); | 81 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 template <> | 84 template <> |
| 85 struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> { | 85 struct TypeConverter<HttpRequestPtr, net::HttpServerRequestInfo> { |
| 86 static URLRequestPtr Convert(const net::HttpServerRequestInfo& obj) { | 86 static HttpRequestPtr Convert(const net::HttpServerRequestInfo& obj) { |
| 87 URLRequestPtr request(URLRequest::New()); | 87 HttpRequestPtr request(HttpRequest::New()); |
| 88 request->method = obj.method; |
| 88 request->url = obj.path; | 89 request->url = obj.path; |
| 89 request->method = obj.method; | |
| 90 request->headers.resize(obj.headers.size()); | 90 request->headers.resize(obj.headers.size()); |
| 91 size_t index = 0; | 91 size_t index = 0; |
| 92 for (const auto& item : obj.headers) { | 92 for (const auto& item : obj.headers) { |
| 93 HTTPHeaderPtr header(HTTPHeader::New()); | 93 HTTPHeaderPtr header(HTTPHeader::New()); |
| 94 header->name = item.first; | 94 header->name = item.first; |
| 95 header->value = item.second; | 95 header->value = item.second; |
| 96 request->headers[index++] = header.Pass(); | 96 request->headers[index++] = header.Pass(); |
| 97 } | 97 } |
| 98 if (!obj.data.empty()) { | 98 if (!obj.data.empty()) { |
| 99 uint32_t num_bytes = static_cast<uint32_t>(obj.data.size()); | 99 uint32_t num_bytes = static_cast<uint32_t>(obj.data.size()); |
| 100 MojoCreateDataPipeOptions options; | 100 MojoCreateDataPipeOptions options; |
| 101 options.struct_size = sizeof(MojoCreateDataPipeOptions); | 101 options.struct_size = sizeof(MojoCreateDataPipeOptions); |
| 102 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; | 102 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; |
| 103 options.element_num_bytes = 1; | 103 options.element_num_bytes = 1; |
| 104 options.capacity_num_bytes = num_bytes; | 104 options.capacity_num_bytes = num_bytes; |
| 105 DataPipe data_pipe(options); | 105 DataPipe data_pipe(options); |
| 106 request->body.push_back(data_pipe.consumer_handle.Pass()); | 106 request->body = data_pipe.consumer_handle.Pass(); |
| 107 MojoResult result = | 107 MojoResult result = |
| 108 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), | 108 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), |
| 109 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); | 109 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); |
| 110 DCHECK_EQ(MOJO_RESULT_OK, result); | 110 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 111 } | 111 } |
| 112 return request.Pass(); | 112 return request.Pass(); |
| 113 } | 113 } |
| 114 }; | 114 }; |
| 115 | 115 |
| 116 HttpConnectionImpl::HttpConnectionImpl(int connection_id, | 116 HttpConnectionImpl::HttpConnectionImpl(int connection_id, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 131 for (const auto& reader : response_body_readers_) | 131 for (const auto& reader : response_body_readers_) |
| 132 delete reader; | 132 delete reader; |
| 133 } | 133 } |
| 134 | 134 |
| 135 void HttpConnectionImpl::OnReceivedHttpRequest( | 135 void HttpConnectionImpl::OnReceivedHttpRequest( |
| 136 const net::HttpServerRequestInfo& info) { | 136 const net::HttpServerRequestInfo& info) { |
| 137 if (!delegate_) | 137 if (!delegate_) |
| 138 return; | 138 return; |
| 139 | 139 |
| 140 delegate_->OnReceivedRequest( | 140 delegate_->OnReceivedRequest( |
| 141 URLRequest::From(info), [this](URLResponsePtr response) { | 141 HttpRequest::From(info), [this](HttpResponsePtr response) { |
| 142 if (response->body.is_valid()) { | 142 if (response->body.is_valid()) { |
| 143 SimpleDataPipeReader* reader = new SimpleDataPipeReader; | 143 SimpleDataPipeReader* reader = new SimpleDataPipeReader; |
| 144 response_body_readers_.insert(reader); | 144 response_body_readers_.insert(reader); |
| 145 reader->Start( | 145 reader->Start( |
| 146 response->body.Pass(), | 146 response->body.Pass(), |
| 147 base::Bind(&HttpConnectionImpl::OnFinishedReadingResponseBody, | 147 base::Bind(&HttpConnectionImpl::OnFinishedReadingResponseBody, |
| 148 base::Unretained(this), base::Passed(&response))); | 148 base::Unretained(this), base::Passed(&response))); |
| 149 } else { | 149 } else { |
| 150 OnFinishedReadingResponseBody(response.Pass(), nullptr, nullptr); | 150 OnFinishedReadingResponseBody(response.Pass(), nullptr, nullptr); |
| 151 } | 151 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 binding_.Close(); | 191 binding_.Close(); |
| 192 delegate_.reset(); | 192 delegate_.reset(); |
| 193 | 193 |
| 194 // Don't close the connection until all pending responses are sent. | 194 // Don't close the connection until all pending responses are sent. |
| 195 if (response_body_readers_.empty()) | 195 if (response_body_readers_.empty()) |
| 196 owner_->server()->Close(connection_id_); | 196 owner_->server()->Close(connection_id_); |
| 197 } | 197 } |
| 198 } | 198 } |
| 199 | 199 |
| 200 void HttpConnectionImpl::OnFinishedReadingResponseBody( | 200 void HttpConnectionImpl::OnFinishedReadingResponseBody( |
| 201 URLResponsePtr response, | 201 HttpResponsePtr response, |
| 202 SimpleDataPipeReader* reader, | 202 SimpleDataPipeReader* reader, |
| 203 scoped_ptr<std::string> body) { | 203 scoped_ptr<std::string> body) { |
| 204 if (reader) { | 204 if (reader) { |
| 205 delete reader; | 205 delete reader; |
| 206 response_body_readers_.erase(reader); | 206 response_body_readers_.erase(reader); |
| 207 } | 207 } |
| 208 | 208 |
| 209 net::HttpServerResponseInfo info( | 209 net::HttpServerResponseInfo info( |
| 210 static_cast<net::HttpStatusCode>(response->status_code)); | 210 static_cast<net::HttpStatusCode>(response->status_code)); |
| 211 | 211 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 234 if (body) | 234 if (body) |
| 235 info.SetBody(*body, content_type); | 235 info.SetBody(*body, content_type); |
| 236 | 236 |
| 237 owner_->server()->SendResponse(connection_id_, info); | 237 owner_->server()->SendResponse(connection_id_, info); |
| 238 | 238 |
| 239 if (response_body_readers_.empty() && encountered_connection_error_) | 239 if (response_body_readers_.empty() && encountered_connection_error_) |
| 240 owner_->server()->Close(connection_id_); | 240 owner_->server()->Close(connection_id_); |
| 241 } | 241 } |
| 242 | 242 |
| 243 } // namespace mojo | 243 } // namespace mojo |
| OLD | NEW |