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

Side by Side Diff: mojo/services/network/http_connection_impl.cc

Issue 1129143005: Introduce Http{Request,Response} mojom structs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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 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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 76
77 ScopedDataPipeConsumerHandle consumer_; 77 ScopedDataPipeConsumerHandle consumer_;
78 common::HandleWatcher watcher_; 78 common::HandleWatcher watcher_;
79 CompletionCallback completion_callback_; 79 CompletionCallback completion_callback_;
80 scoped_ptr<std::string> buffer_; 80 scoped_ptr<std::string> buffer_;
81 81
82 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader); 82 DISALLOW_COPY_AND_ASSIGN(SimpleDataPipeReader);
83 }; 83 };
84 84
85 template <> 85 template <>
86 struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> { 86 struct TypeConverter<HttpRequestPtr, net::HttpServerRequestInfo> {
87 static URLRequestPtr Convert(const net::HttpServerRequestInfo& obj) { 87 static HttpRequestPtr Convert(const net::HttpServerRequestInfo& obj) {
88 URLRequestPtr request(URLRequest::New()); 88 HttpRequestPtr request(HttpRequest::New());
89 request->method = obj.method;
89 request->url = obj.path; 90 request->url = obj.path;
90 request->method = obj.method;
91 request->headers.resize(obj.headers.size()); 91 request->headers.resize(obj.headers.size());
92 size_t index = 0; 92 size_t index = 0;
93 for (const auto& item : obj.headers) { 93 for (const auto& item : obj.headers) {
94 HTTPHeaderPtr header(HTTPHeader::New()); 94 HttpHeaderPtr header(HttpHeader::New());
95 header->name = item.first; 95 header->name = item.first;
96 header->value = item.second; 96 header->value = item.second;
97 request->headers[index++] = header.Pass(); 97 request->headers[index++] = header.Pass();
98 } 98 }
99 if (!obj.data.empty()) { 99 if (!obj.data.empty()) {
100 uint32_t num_bytes = static_cast<uint32_t>(obj.data.size()); 100 uint32_t num_bytes = static_cast<uint32_t>(obj.data.size());
101 MojoCreateDataPipeOptions options; 101 MojoCreateDataPipeOptions options;
102 options.struct_size = sizeof(MojoCreateDataPipeOptions); 102 options.struct_size = sizeof(MojoCreateDataPipeOptions);
103 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE; 103 options.flags = MOJO_CREATE_DATA_PIPE_OPTIONS_FLAG_NONE;
104 options.element_num_bytes = 1; 104 options.element_num_bytes = 1;
105 options.capacity_num_bytes = num_bytes; 105 options.capacity_num_bytes = num_bytes;
106 DataPipe data_pipe(options); 106 DataPipe data_pipe(options);
107 request->body.push_back(data_pipe.consumer_handle.Pass()); 107 request->body = data_pipe.consumer_handle.Pass();
108 MojoResult result = 108 MojoResult result =
109 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), 109 WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(),
110 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); 110 &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
111 CHECK_EQ(MOJO_RESULT_OK, result); 111 CHECK_EQ(MOJO_RESULT_OK, result);
112 } 112 }
113 return request.Pass(); 113 return request.Pass();
114 } 114 }
115 }; 115 };
116 116
117 HttpConnectionImpl::HttpConnectionImpl(int connection_id, 117 HttpConnectionImpl::HttpConnectionImpl(int connection_id,
(...skipping 12 matching lines...) Expand all
130 HttpConnectionImpl::~HttpConnectionImpl() { 130 HttpConnectionImpl::~HttpConnectionImpl() {
131 STLDeleteElements(&response_body_readers_); 131 STLDeleteElements(&response_body_readers_);
132 } 132 }
133 133
134 void HttpConnectionImpl::OnReceivedHttpRequest( 134 void HttpConnectionImpl::OnReceivedHttpRequest(
135 const net::HttpServerRequestInfo& info) { 135 const net::HttpServerRequestInfo& info) {
136 if (EncounteredConnectionError()) 136 if (EncounteredConnectionError())
137 return; 137 return;
138 138
139 delegate_->OnReceivedRequest( 139 delegate_->OnReceivedRequest(
140 URLRequest::From(info), [this](URLResponsePtr response) { 140 HttpRequest::From(info), [this](HttpResponsePtr response) {
141 if (response->body.is_valid()) { 141 if (response->body.is_valid()) {
142 SimpleDataPipeReader* reader = new SimpleDataPipeReader; 142 SimpleDataPipeReader* reader = new SimpleDataPipeReader;
143 response_body_readers_.insert(reader); 143 response_body_readers_.insert(reader);
144 reader->Start( 144 reader->Start(
145 response->body.Pass(), 145 response->body.Pass(),
146 base::Bind(&HttpConnectionImpl::OnFinishedReadingResponseBody, 146 base::Bind(&HttpConnectionImpl::OnFinishedReadingResponseBody,
147 base::Unretained(this), base::Passed(&response))); 147 base::Unretained(this), base::Passed(&response)));
148 } else { 148 } else {
149 OnFinishedReadingResponseBody(response.Pass(), nullptr, nullptr); 149 OnFinishedReadingResponseBody(response.Pass(), nullptr, nullptr);
150 } 150 }
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 191
192 binding_.Close(); 192 binding_.Close();
193 delegate_.reset(); 193 delegate_.reset();
194 194
195 // Don't close the connection until all pending responses are sent. 195 // Don't close the connection until all pending responses are sent.
196 if (response_body_readers_.empty()) 196 if (response_body_readers_.empty())
197 owner_->server()->Close(connection_id_); 197 owner_->server()->Close(connection_id_);
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
212 std::string content_type; 212 std::string content_type;
213 for (size_t i = 0; i < response->headers.size(); ++i) { 213 for (size_t i = 0; i < response->headers.size(); ++i) {
214 const HTTPHeader& header = *(response->headers[i]); 214 const HttpHeader& header = *(response->headers[i]);
215 215
216 if (body) { 216 if (body) {
217 // net::HttpServerResponseInfo::SetBody() automatically sets 217 // net::HttpServerResponseInfo::SetBody() automatically sets
218 // Content-Length and Content-Types, so skip the two here. 218 // Content-Length and Content-Types, so skip the two here.
219 // 219 //
220 // TODO(yzshen): Consider adding to net::HttpServerResponseInfo a simple 220 // TODO(yzshen): Consider adding to net::HttpServerResponseInfo a simple
221 // setter for body which doesn't fiddle with headers. 221 // setter for body which doesn't fiddle with headers.
222 if (base::strcasecmp(header.name.data(), 222 if (base::strcasecmp(header.name.data(),
223 net::HttpRequestHeaders::kContentLength) == 0) { 223 net::HttpRequestHeaders::kContentLength) == 0) {
224 continue; 224 continue;
225 } else if (base::strcasecmp(header.name.data(), 225 } else if (base::strcasecmp(header.name.data(),
226 net::HttpRequestHeaders::kContentType) == 0) { 226 net::HttpRequestHeaders::kContentType) == 0) {
227 content_type = header.value; 227 content_type = header.value;
228 continue; 228 continue;
229 } 229 }
230 } 230 }
231 info.AddHeader(header.name, header.value); 231 info.AddHeader(header.name, header.value);
232 } 232 }
233 233
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() && EncounteredConnectionError()) 239 if (response_body_readers_.empty() && EncounteredConnectionError())
240 owner_->server()->Close(connection_id_); 240 owner_->server()->Close(connection_id_);
241 } 241 }
242 242
243 } // namespace mojo 243 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/network/http_connection_impl.h ('k') | mojo/services/network/http_server_apptest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698