Commit 9e02055b authored by yzshen's avatar yzshen Committed by Commit bot

Introduce Http{Request,Response} mojom structs.

The reason to stop using URL{Request,Response} for HTTP server interfaces: some
fields of those structs belong to a higher level, e.g., |auto_follow_redirects|.
It seems cleaner to define Http{Request,Response} which match the HTTP
request/response definitions.

BUG=478249
TEST=None

Review URL: https://codereview.chromium.org/1129143005

Cr-Commit-Position: refs/heads/master@{#330238}
parent d1b5d559
...@@ -26,17 +26,17 @@ class HeaderFlattener : public blink::WebHTTPHeaderVisitor { ...@@ -26,17 +26,17 @@ class HeaderFlattener : public blink::WebHTTPHeaderVisitor {
if (LowerCaseEqualsASCII(name_latin1, "accept")) if (LowerCaseEqualsASCII(name_latin1, "accept"))
has_accept_header_ = true; has_accept_header_ = true;
HTTPHeaderPtr header = HTTPHeader::New(); HttpHeaderPtr header = HttpHeader::New();
header->name = name_latin1; header->name = name_latin1;
header->value = value_latin1; header->value = value_latin1;
buffer_.push_back(header.Pass()); buffer_.push_back(header.Pass());
} }
Array<HTTPHeaderPtr> GetBuffer() { Array<HttpHeaderPtr> GetBuffer() {
// In some cases, WebKit doesn't add an Accept header, but not having the // In some cases, WebKit doesn't add an Accept header, but not having the
// header confuses some web servers. See bug 808613. // header confuses some web servers. See bug 808613.
if (!has_accept_header_) { if (!has_accept_header_) {
HTTPHeaderPtr header = HTTPHeader::New(); HttpHeaderPtr header = HttpHeader::New();
header->name = "Accept"; header->name = "Accept";
header->value = "*/*"; header->value = "*/*";
buffer_.push_back(header.Pass()); buffer_.push_back(header.Pass());
...@@ -46,7 +46,7 @@ class HeaderFlattener : public blink::WebHTTPHeaderVisitor { ...@@ -46,7 +46,7 @@ class HeaderFlattener : public blink::WebHTTPHeaderVisitor {
} }
private: private:
Array<HTTPHeaderPtr> buffer_; Array<HttpHeaderPtr> buffer_;
bool has_accept_header_; bool has_accept_header_;
}; };
......
...@@ -83,15 +83,15 @@ class HttpConnectionImpl::SimpleDataPipeReader { ...@@ -83,15 +83,15 @@ class HttpConnectionImpl::SimpleDataPipeReader {
}; };
template <> template <>
struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> { struct TypeConverter<HttpRequestPtr, net::HttpServerRequestInfo> {
static URLRequestPtr Convert(const net::HttpServerRequestInfo& obj) { static HttpRequestPtr Convert(const net::HttpServerRequestInfo& obj) {
URLRequestPtr request(URLRequest::New()); HttpRequestPtr request(HttpRequest::New());
request->url = obj.path;
request->method = obj.method; request->method = obj.method;
request->url = obj.path;
request->headers.resize(obj.headers.size()); request->headers.resize(obj.headers.size());
size_t index = 0; size_t index = 0;
for (const auto& item : obj.headers) { for (const auto& item : obj.headers) {
HTTPHeaderPtr header(HTTPHeader::New()); HttpHeaderPtr header(HttpHeader::New());
header->name = item.first; header->name = item.first;
header->value = item.second; header->value = item.second;
request->headers[index++] = header.Pass(); request->headers[index++] = header.Pass();
...@@ -104,7 +104,7 @@ struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> { ...@@ -104,7 +104,7 @@ struct TypeConverter<URLRequestPtr, net::HttpServerRequestInfo> {
options.element_num_bytes = 1; options.element_num_bytes = 1;
options.capacity_num_bytes = num_bytes; options.capacity_num_bytes = num_bytes;
DataPipe data_pipe(options); DataPipe data_pipe(options);
request->body.push_back(data_pipe.consumer_handle.Pass()); request->body = data_pipe.consumer_handle.Pass();
MojoResult result = MojoResult result =
WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(), WriteDataRaw(data_pipe.producer_handle.get(), obj.data.data(),
&num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE); &num_bytes, MOJO_WRITE_DATA_FLAG_ALL_OR_NONE);
...@@ -137,7 +137,7 @@ void HttpConnectionImpl::OnReceivedHttpRequest( ...@@ -137,7 +137,7 @@ void HttpConnectionImpl::OnReceivedHttpRequest(
return; return;
delegate_->OnReceivedRequest( delegate_->OnReceivedRequest(
URLRequest::From(info), [this](URLResponsePtr response) { HttpRequest::From(info), [this](HttpResponsePtr response) {
if (response->body.is_valid()) { if (response->body.is_valid()) {
SimpleDataPipeReader* reader = new SimpleDataPipeReader; SimpleDataPipeReader* reader = new SimpleDataPipeReader;
response_body_readers_.insert(reader); response_body_readers_.insert(reader);
...@@ -198,7 +198,7 @@ void HttpConnectionImpl::OnConnectionError() { ...@@ -198,7 +198,7 @@ void HttpConnectionImpl::OnConnectionError() {
} }
void HttpConnectionImpl::OnFinishedReadingResponseBody( void HttpConnectionImpl::OnFinishedReadingResponseBody(
URLResponsePtr response, HttpResponsePtr response,
SimpleDataPipeReader* reader, SimpleDataPipeReader* reader,
scoped_ptr<std::string> body) { scoped_ptr<std::string> body) {
if (reader) { if (reader) {
...@@ -211,7 +211,7 @@ void HttpConnectionImpl::OnFinishedReadingResponseBody( ...@@ -211,7 +211,7 @@ void HttpConnectionImpl::OnFinishedReadingResponseBody(
std::string content_type; std::string content_type;
for (size_t i = 0; i < response->headers.size(); ++i) { for (size_t i = 0; i < response->headers.size(); ++i) {
const HTTPHeader& header = *(response->headers[i]); const HttpHeader& header = *(response->headers[i]);
if (body) { if (body) {
// net::HttpServerResponseInfo::SetBody() automatically sets // net::HttpServerResponseInfo::SetBody() automatically sets
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "mojo/services/network/public/interfaces/http_connection.mojom.h" #include "mojo/services/network/public/interfaces/http_connection.mojom.h"
#include "mojo/services/network/public/interfaces/url_loader.mojom.h" #include "mojo/services/network/public/interfaces/http_message.mojom.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h"
#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h"
...@@ -51,7 +51,7 @@ class HttpConnectionImpl : public HttpConnection, ...@@ -51,7 +51,7 @@ class HttpConnectionImpl : public HttpConnection,
// ErrorHandler implementation. // ErrorHandler implementation.
void OnConnectionError() override; void OnConnectionError() override;
void OnFinishedReadingResponseBody(URLResponsePtr response_ptr, void OnFinishedReadingResponseBody(HttpResponsePtr response_ptr,
SimpleDataPipeReader* reader, SimpleDataPipeReader* reader,
scoped_ptr<std::string> body); scoped_ptr<std::string> body);
......
...@@ -68,13 +68,13 @@ std::string MakeRequestMessage(const TestRequest& data) { ...@@ -68,13 +68,13 @@ std::string MakeRequestMessage(const TestRequest& data) {
return message; return message;
} }
URLResponsePtr MakeResponseStruct(const TestResponse& data) { HttpResponsePtr MakeResponseStruct(const TestResponse& data) {
URLResponsePtr response(URLResponse::New()); HttpResponsePtr response(HttpResponse::New());
response->status_code = data.status_code; response->status_code = data.status_code;
response->headers.resize(data.headers.size()); response->headers.resize(data.headers.size());
size_t index = 0; size_t index = 0;
for (const auto& item : data.headers) { for (const auto& item : data.headers) {
HTTPHeaderPtr header(HTTPHeader::New()); HttpHeaderPtr header(HttpHeader::New());
header->name = item.first; header->name = item.first;
header->value = item.second; header->value = item.second;
response->headers[index++] = header.Pass(); response->headers[index++] = header.Pass();
...@@ -99,7 +99,7 @@ URLResponsePtr MakeResponseStruct(const TestResponse& data) { ...@@ -99,7 +99,7 @@ URLResponsePtr MakeResponseStruct(const TestResponse& data) {
} }
void CheckHeaders(const TestHeaders& expected, void CheckHeaders(const TestHeaders& expected,
const Array<HTTPHeaderPtr>& headers) { const Array<HttpHeaderPtr>& headers) {
// The server impl fiddles with Content-Length and Content-Type. So we don't // The server impl fiddles with Content-Length and Content-Type. So we don't
// do a strict check here. // do a strict check here.
std::map<std::string, std::string> header_map; std::map<std::string, std::string> header_map;
...@@ -116,17 +116,17 @@ void CheckHeaders(const TestHeaders& expected, ...@@ -116,17 +116,17 @@ void CheckHeaders(const TestHeaders& expected,
} }
} }
void CheckRequest(const TestRequest& expected, URLRequestPtr request) { void CheckRequest(const TestRequest& expected, HttpRequestPtr request) {
EXPECT_EQ(expected.method, request->method); EXPECT_EQ(expected.method, request->method);
EXPECT_EQ(expected.url, request->url); EXPECT_EQ(expected.url, request->url);
CheckHeaders(expected.headers, request->headers); CheckHeaders(expected.headers, request->headers);
if (expected.body) { if (expected.body) {
EXPECT_EQ(1u, request->body.size()); EXPECT_TRUE(request->body.is_valid());
std::string body; std::string body;
common::BlockingCopyToString(request->body[0].Pass(), &body); common::BlockingCopyToString(request->body.Pass(), &body);
EXPECT_EQ(*expected.body, body); EXPECT_EQ(*expected.body, body);
} else { } else {
EXPECT_EQ(0u, request->body.size()); EXPECT_FALSE(request->body.is_valid());
} }
} }
...@@ -264,7 +264,7 @@ class TestHttpClient { ...@@ -264,7 +264,7 @@ class TestHttpClient {
class HttpConnectionDelegateImpl : public HttpConnectionDelegate { class HttpConnectionDelegateImpl : public HttpConnectionDelegate {
public: public:
struct PendingRequest { struct PendingRequest {
URLRequestPtr request; HttpRequestPtr request;
OnReceivedRequestCallback callback; OnReceivedRequestCallback callback;
}; };
...@@ -277,7 +277,7 @@ class HttpConnectionDelegateImpl : public HttpConnectionDelegate { ...@@ -277,7 +277,7 @@ class HttpConnectionDelegateImpl : public HttpConnectionDelegate {
~HttpConnectionDelegateImpl() override {} ~HttpConnectionDelegateImpl() override {}
// HttpConnectionDelegate implementation: // HttpConnectionDelegate implementation:
void OnReceivedRequest(URLRequestPtr request, void OnReceivedRequest(HttpRequestPtr request,
const OnReceivedRequestCallback& callback) override { const OnReceivedRequestCallback& callback) override {
linked_ptr<PendingRequest> pending_request(new PendingRequest); linked_ptr<PendingRequest> pending_request(new PendingRequest);
pending_request->request = request.Pass(); pending_request->request = request.Pass();
...@@ -290,12 +290,12 @@ class HttpConnectionDelegateImpl : public HttpConnectionDelegate { ...@@ -290,12 +290,12 @@ class HttpConnectionDelegateImpl : public HttpConnectionDelegate {
} }
void OnReceivedWebSocketRequest( void OnReceivedWebSocketRequest(
URLRequestPtr request, HttpRequestPtr request,
const OnReceivedWebSocketRequestCallback& callback) override { const OnReceivedWebSocketRequestCallback& callback) override {
NOTREACHED(); NOTREACHED();
} }
void SendResponse(URLResponsePtr response) { void SendResponse(HttpResponsePtr response) {
ASSERT_FALSE(pending_requests_.empty()); ASSERT_FALSE(pending_requests_.empty());
linked_ptr<PendingRequest> request = pending_requests_[0]; linked_ptr<PendingRequest> request = pending_requests_[0];
pending_requests_.erase(pending_requests_.begin()); pending_requests_.erase(pending_requests_.begin());
......
...@@ -9,6 +9,7 @@ mojom("interfaces") { ...@@ -9,6 +9,7 @@ mojom("interfaces") {
sources = [ sources = [
"cookie_store.mojom", "cookie_store.mojom",
"http_connection.mojom", "http_connection.mojom",
"http_message.mojom",
"http_server.mojom", "http_server.mojom",
"net_address.mojom", "net_address.mojom",
"network_error.mojom", "network_error.mojom",
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
module mojo; module mojo;
import "network/public/interfaces/url_loader.mojom"; import "network/public/interfaces/http_message.mojom";
import "network/public/interfaces/network_error.mojom"; import "network/public/interfaces/network_error.mojom";
import "network/public/interfaces/web_socket.mojom"; import "network/public/interfaces/web_socket.mojom";
...@@ -18,7 +18,7 @@ interface HttpConnection { ...@@ -18,7 +18,7 @@ interface HttpConnection {
interface HttpConnectionDelegate { interface HttpConnectionDelegate {
// Called when an HTTP request is received. // Called when an HTTP request is received.
OnReceivedRequest(URLRequest request) => (URLResponse response); OnReceivedRequest(HttpRequest request) => (HttpResponse response);
// Called when an WebSocket request is received. // Called when an WebSocket request is received.
// //
...@@ -28,7 +28,7 @@ interface HttpConnectionDelegate { ...@@ -28,7 +28,7 @@ interface HttpConnectionDelegate {
// WebSocket should be written to the producer end of the |send_stream|. // WebSocket should be written to the producer end of the |send_stream|.
// |web_socket| will be already connected. There is no need to call Connect() // |web_socket| will be already connected. There is no need to call Connect()
// on it. But |client| will still receive a DidConnect() notification. // on it. But |client| will still receive a DidConnect() notification.
OnReceivedWebSocketRequest(URLRequest request) OnReceivedWebSocketRequest(HttpRequest request)
=> (WebSocket&? web_socket, => (WebSocket&? web_socket,
handle<data_pipe_consumer>? send_stream, handle<data_pipe_consumer>? send_stream,
WebSocketClient? client); WebSocketClient? client);
......
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
module mojo;
struct HttpHeader {
string name;
string value;
};
struct HttpRequest {
string method = "GET";
string url;
array<HttpHeader>? headers;
handle<data_pipe_consumer>? body;
};
struct HttpResponse {
uint32 status_code = 200;
array<HttpHeader>? headers;
handle<data_pipe_consumer>? body;
};
...@@ -4,13 +4,9 @@ ...@@ -4,13 +4,9 @@
module mojo; module mojo;
import "network/public/interfaces/http_message.mojom";
import "network/public/interfaces/network_error.mojom"; import "network/public/interfaces/network_error.mojom";
struct HTTPHeader {
string name;
string value;
};
struct URLRequest { struct URLRequest {
// The URL to load. // The URL to load.
string url; string url;
...@@ -19,7 +15,7 @@ struct URLRequest { ...@@ -19,7 +15,7 @@ struct URLRequest {
string method = "GET"; string method = "GET";
// Additional HTTP request headers. // Additional HTTP request headers.
array<HTTPHeader>? headers; array<HttpHeader>? headers;
// The payload for the request body, represented as a concatenation of data // The payload for the request body, represented as a concatenation of data
// streams. For HTTP requests, the method must be set to "POST" or "PUT". // streams. For HTTP requests, the method must be set to "POST" or "PUT".
...@@ -59,7 +55,7 @@ struct URLResponse { ...@@ -59,7 +55,7 @@ struct URLResponse {
string? status_line; string? status_line;
// The HTTP response headers. // The HTTP response headers.
array<HTTPHeader>? headers; array<HttpHeader>? headers;
// The MIME type of the response body. // The MIME type of the response body.
string? mime_type; string? mime_type;
......
...@@ -31,12 +31,12 @@ URLResponsePtr MakeURLResponse(const net::URLRequest* url_request) { ...@@ -31,12 +31,12 @@ URLResponsePtr MakeURLResponse(const net::URLRequest* url_request) {
response->status_code = headers->response_code(); response->status_code = headers->response_code();
response->status_line = headers->GetStatusLine(); response->status_line = headers->GetStatusLine();
response->headers = Array<HTTPHeaderPtr>::New(0); response->headers = Array<HttpHeaderPtr>::New(0);
std::vector<String> header_lines; std::vector<String> header_lines;
void* iter = nullptr; void* iter = nullptr;
std::string name, value; std::string name, value;
while (headers->EnumerateHeaderLines(&iter, &name, &value)) { while (headers->EnumerateHeaderLines(&iter, &name, &value)) {
HTTPHeaderPtr header = HTTPHeader::New(); HttpHeaderPtr header = HttpHeader::New();
header->name = name; header->name = name;
header->value = value; header->value = value;
response->headers.push_back(header.Pass()); response->headers.push_back(header.Pass());
......
...@@ -53,8 +53,8 @@ URLResponsePtr LocalFetcher::AsURLResponse(base::TaskRunner* task_runner, ...@@ -53,8 +53,8 @@ URLResponsePtr LocalFetcher::AsURLResponse(base::TaskRunner* task_runner,
response->body = data_pipe.consumer_handle.Pass(); response->body = data_pipe.consumer_handle.Pass();
int64 file_size; int64 file_size;
if (base::GetFileSize(path_, &file_size)) { if (base::GetFileSize(path_, &file_size)) {
response->headers = Array<HTTPHeaderPtr>(1); response->headers = Array<HttpHeaderPtr>(1);
HTTPHeaderPtr header = HTTPHeader::New(); HttpHeaderPtr header = HttpHeader::New();
header->name = "Content-Length"; header->name = "Content-Length";
header->value = base::StringPrintf("%" PRId64, file_size); header->value = base::StringPrintf("%" PRId64, file_size);
response->headers[0] = header.Pass(); response->headers[0] = header.Pass();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment