Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry pick PR #3108: Add back support for data URL's. #3141

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions cobalt/network/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ static_library("network") {
sources = [
"cookie_jar_impl.cc",
"cookie_jar_impl.h",
"custom/data_protocol_handler.cc",
"custom/data_protocol_handler.h",
"custom/url_fetcher.cc",
"custom/url_fetcher.h",
"custom/url_fetcher_core.cc",
Expand All @@ -39,6 +41,12 @@ static_library("network") {
"custom/url_fetcher_impl.h",
"custom/url_fetcher_response_writer.cc",
"custom/url_fetcher_response_writer.h",
"custom/url_range_request_job.cc",
"custom/url_range_request_job.h",
"custom/url_request_data_job.cc",
"custom/url_request_data_job.h",
"custom/url_request_simple_job.cc",
"custom/url_request_simple_job.h",
"custom/url_request_status.cc",
"custom/url_request_status.h",
"disk_cache/cobalt_backend_factory.cc",
Expand Down
36 changes: 36 additions & 0 deletions cobalt/network/custom/data_protocol_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright (c) 2012 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.

#include "cobalt/network/custom/data_protocol_handler.h"

#include "cobalt/network/custom/url_request_data_job.h"

namespace net {

DataProtocolHandler::DataProtocolHandler() = default;

std::unique_ptr<URLRequestJob> DataProtocolHandler::CreateJob(
URLRequest* request) const {
return std::make_unique<URLRequestDataJob>(request);
}

bool DataProtocolHandler::IsSafeRedirectTarget(const GURL& location) const {
return false;
}

} // namespace net
48 changes: 48 additions & 0 deletions cobalt/network/custom/data_protocol_handler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright (c) 2012 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.

#ifndef COBALT_NETWORK_CUSTOM_DATA_PROTOCOL_HANDLER_H_
#define COBALT_NETWORK_CUSTOM_DATA_PROTOCOL_HANDLER_H_

#include <memory>

#include "base/compiler_specific.h"
#include "base/macros.h"
#include "net/base/net_export.h"
#include "net/url_request/url_request_job_factory.h"

namespace net {

class URLRequestJob;

// Implements a ProtocolHandler for Data jobs.
class NET_EXPORT DataProtocolHandler
: public URLRequestJobFactory::ProtocolHandler {
public:
DataProtocolHandler();
DataProtocolHandler(const DataProtocolHandler&) = delete;
DataProtocolHandler& operator=(const DataProtocolHandler&) = delete;

std::unique_ptr<URLRequestJob> CreateJob(URLRequest* request) const override;

bool IsSafeRedirectTarget(const GURL& location) const override;
};

} // namespace net

#endif // COBALT_NETWORK_CUSTOM_DATA_PROTOCOL_HANDLER_H_
44 changes: 44 additions & 0 deletions cobalt/network/custom/url_range_request_job.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2014 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.

#include "cobalt/network/custom/url_range_request_job.h"

#include <string>

#include "net/base/net_errors.h"
#include "net/http/http_request_headers.h"
#include "net/http/http_util.h"

namespace net {

URLRangeRequestJob::URLRangeRequestJob(URLRequest* request)
: URLRequestJob(request), range_parse_result_(OK) {}

URLRangeRequestJob::~URLRangeRequestJob() = default;

void URLRangeRequestJob::SetExtraRequestHeaders(
const HttpRequestHeaders& headers) {
std::string range_header;
if (headers.GetHeader(HttpRequestHeaders::kRange, &range_header)) {
if (!HttpUtil::ParseRangeHeader(range_header, &ranges_)) {
range_parse_result_ = ERR_REQUEST_RANGE_NOT_SATISFIABLE;
}
}
}

} // namespace net
55 changes: 55 additions & 0 deletions cobalt/network/custom/url_range_request_job.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2014 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.

#ifndef COBALT_NETWORK_CUSTOM_URL_RANGE_REQUEST_JOB_H_
#define COBALT_NETWORK_CUSTOM_URL_RANGE_REQUEST_JOB_H_

#include <vector>

#include "net/base/net_export.h"
#include "net/http/http_byte_range.h"
#include "net/url_request/url_request_job.h"

namespace net {

class HttpRequestHeaders;

// URLRequestJob with support for parsing range requests.
// It is up to subclasses to handle the response
// and deal with an errors parsing the range request header.
// This must be done after Start() has been called.
class NET_EXPORT URLRangeRequestJob : public URLRequestJob {
public:
explicit URLRangeRequestJob(URLRequest* request);

void SetExtraRequestHeaders(const HttpRequestHeaders& headers) override;

const std::vector<HttpByteRange>& ranges() const { return ranges_; }
int range_parse_result() const { return range_parse_result_; }

protected:
~URLRangeRequestJob() override;

private:
std::vector<HttpByteRange> ranges_;
int range_parse_result_;
};

} // namespace net

#endif // COBALT_NETWORK_CUSTOM_URL_RANGE_REQUEST_JOB_H_
73 changes: 73 additions & 0 deletions cobalt/network/custom/url_request_data_job.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright (c) 2012 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.

// Simple implementation of a data: protocol handler.

#include "cobalt/network/custom/url_request_data_job.h"

#include "net/base/data_url.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "url/gurl.h"

namespace net {

int URLRequestDataJob::BuildResponse(const GURL& url, std::string* mime_type,
std::string* charset, std::string* data,
HttpResponseHeaders* headers) {
if (!DataURL::Parse(url, mime_type, charset, data)) return ERR_INVALID_URL;

// |mime_type| set by DataURL::Parse() is guaranteed to be in
// token "/" token
// form. |charset| can be an empty string.

DCHECK(!mime_type->empty());

if (headers) {
headers->ReplaceStatusLine("HTTP/1.1 200 OK");
// "charset" in the Content-Type header is specified explicitly to follow
// the "token" ABNF in the HTTP spec. When DataURL::Parse() call is
// successful, it's guaranteed that the string in |charset| follows the
// "token" ABNF.
std::string content_type_header = *mime_type;
if (!charset->empty()) content_type_header.append(";charset=" + *charset);
headers->AddHeader("Content-Type", content_type_header);
}

return OK;
}

URLRequestDataJob::URLRequestDataJob(URLRequest* request)
: URLRequestSimpleJob(request) {}

int URLRequestDataJob::GetData(std::string* mime_type, std::string* charset,
std::string* data,
CompletionOnceCallback callback) const {
// Check if data URL is valid. If not, don't bother to try to extract data.
// Otherwise, parse the data from the data URL.
const GURL& url = request_->url();
if (!url.is_valid()) return ERR_INVALID_URL;

// TODO(tyoshino): Get the headers and export via
// URLRequestJob::GetResponseInfo().
return BuildResponse(url, mime_type, charset, data, NULL);
}

URLRequestDataJob::~URLRequestDataJob() = default;

} // namespace net
58 changes: 58 additions & 0 deletions cobalt/network/custom/url_request_data_job.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2024 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright (c) 2012 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.

#ifndef COBALT_NETWORK_CUSTOM_URL_REQUEST_DATA_JOB_H_
#define COBALT_NETWORK_CUSTOM_URL_REQUEST_DATA_JOB_H_

#include <string>

#include "base/macros.h"
#include "cobalt/network/custom/url_request_simple_job.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_export.h"
#include "net/url_request/url_request.h"

class GURL;

namespace net {

class HttpResponseHeaders;
class URLRequest;

class NET_EXPORT URLRequestDataJob : public URLRequestSimpleJob {
public:
// Extracts info from a data scheme URL. Returns OK if successful. Returns
// ERR_INVALID_URL otherwise.
static int BuildResponse(const GURL& url, std::string* mime_type,
std::string* charset, std::string* data,
HttpResponseHeaders* headers);

explicit URLRequestDataJob(URLRequest* request);
~URLRequestDataJob() override;
URLRequestDataJob(const URLRequestDataJob&) = delete;
URLRequestDataJob& operator=(const URLRequestDataJob&) = delete;


// URLRequestSimpleJob
int GetData(std::string* mime_type, std::string* charset, std::string* data,
CompletionOnceCallback callback) const override;
};

} // namespace net

#endif // COBALT_NETWORK_CUSTOM_URL_REQUEST_DATA_JOB_H_
Loading
Loading