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

feat: Support response headers in File protocol handler #16098

Merged
merged 3 commits into from Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 14 additions & 9 deletions atom/browser/net/url_request_async_asar_job.cc
Expand Up @@ -71,14 +71,23 @@ void URLRequestAsyncAsarJob::StartAsync(std::unique_ptr<base::Value> options,
}

std::string file_path;
response_headers_ = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
if (options->is_dict()) {
auto* path_value =
options->FindKeyOfType("path", base::Value::Type::STRING);
if (path_value)
file_path = path_value->GetString();
base::DictionaryValue* headersValue = nullptr;
base::DictionaryValue* dict =
static_cast<base::DictionaryValue*>(options.get());
dict->GetString("path", &file_path);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This introduces a minor regression of going back to deprecated API:

// DEPRECATED, use Value::FindPath(path) and Value::GetString() instead.
bool GetString(StringPiece path, std::string* out_value) const;

Could you keep the previous FindKeyOfType implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, hadn't noticed that was deprecated. Fixed!

dict->GetDictionary("headers", &headersValue);
if (headersValue) {
for (const auto& iter : headersValue->DictItems()) {
response_headers_->AddHeader(iter.first + ": " +
iter.second.GetString());
}
}
} else if (options->is_string()) {
file_path = options->GetString();
}
response_headers_->AddHeader(kCORSHeader);

if (file_path.empty()) {
NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
Expand All @@ -103,11 +112,7 @@ void URLRequestAsyncAsarJob::Kill() {
}

void URLRequestAsyncAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
std::string status("HTTP/1.1 200 OK");
auto* headers = new net::HttpResponseHeaders(status);

headers->AddHeader(kCORSHeader);
info->headers = headers;
info->headers = response_headers_;
}

} // namespace atom
1 change: 1 addition & 0 deletions atom/browser/net/url_request_async_asar_job.h
Expand Up @@ -26,6 +26,7 @@ class URLRequestAsyncAsarJob : public asar::URLRequestAsarJob, public JsAsker {
void Kill() override;

private:
scoped_refptr<net::HttpResponseHeaders> response_headers_;
base::WeakPtrFactory<URLRequestAsyncAsarJob> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(URLRequestAsyncAsarJob);
Expand Down
4 changes: 3 additions & 1 deletion docs/api/protocol.md
Expand Up @@ -99,7 +99,9 @@ going to be created with `scheme`. `completion` will be called with

To handle the `request`, the `callback` should be called with either the file's
path or an object that has a `path` property, e.g. `callback(filePath)` or
`callback({ path: filePath })`.
`callback({ path: filePath })`. The object may also have a `headers` property
which gives a list of strings for the response headers, e.g.
`callback({ path: filePath, headers: ["Content-Security-Policy: default-src 'none'"]})`.

When `callback` is called with nothing, a number, or an object that has an
`error` property, the `request` will fail with the `error` number you
Expand Down
22 changes: 22 additions & 0 deletions spec/api-protocol-spec.js
Expand Up @@ -320,6 +320,28 @@ describe('protocol module', () => {
})
})

it('sets custom headers', (done) => {
const handler = (request, callback) => callback({
path: filePath,
headers: { 'X-Great-Header': 'sogreat' }
})
protocol.registerFileProtocol(protocolName, handler, (error) => {
if (error) return done(error)
$.ajax({
url: protocolName + '://fake-host',
cache: false,
success: (data, status, request) => {
assert.strictEqual(data, String(fileContent))
assert.strictEqual(request.getResponseHeader('X-Great-Header'), 'sogreat')
done()
},
error: (xhr, errorType, error) => {
done(error)
}
})
})
})

it('sends object as response', (done) => {
const handler = (request, callback) => callback({ path: filePath })
protocol.registerFileProtocol(protocolName, handler, (error) => {
Expand Down