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: add support for net requests to use the session cookie store #22808

Merged
merged 8 commits into from Mar 23, 2020
3 changes: 3 additions & 0 deletions docs/api/client-request.md
Expand Up @@ -22,6 +22,9 @@ which the request is associated.
with which the request is associated. Defaults to the empty string. The
`session` option prevails on `partition`. Thus if a `session` is explicitly
specified, `partition` is ignored.
* `useSessionCookies` Boolean (optional) - Whether to send cookies with this
request from the provided session. This will make the `net` request's
cookie behavior match a `fetch` request. Default is `false`.
* `protocol` String (optional) - The protocol scheme in the form 'scheme:'.
Currently supported values are 'http:' or 'https:'. Defaults to 'http:'.
* `host` String (optional) - The server host provided as a concatenation of
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/api/net.js
Expand Up @@ -234,7 +234,8 @@ function parseOptions (options) {
method: method,
url: urlStr,
redirectPolicy,
extraHeaders: options.headers || {}
extraHeaders: options.headers || {},
useSessionCookies: options.useSessionCookies || false
};
for (const [name, value] of Object.entries(urlLoaderOptions.extraHeaders)) {
if (!_isValidHeaderName(name)) {
Expand Down
8 changes: 8 additions & 0 deletions shell/browser/api/atom_api_url_loader.cc
Expand Up @@ -19,6 +19,7 @@
#include "mojo/public/cpp/system/data_pipe_producer.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "net/base/load_flags.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"
Expand Down Expand Up @@ -345,6 +346,7 @@ mate::WrappableBase* SimpleURLLoaderWrapper::New(mate::Arguments* args) {
return nullptr;
}
auto request = std::make_unique<network::ResourceRequest>();
request->attach_same_site_cookies = true;
opts.Get("method", &request->method);
opts.Get("url", &request->url);
std::map<std::string, std::string> extra_headers;
Expand All @@ -359,6 +361,12 @@ mate::WrappableBase* SimpleURLLoaderWrapper::New(mate::Arguments* args) {
}
}

bool use_session_cookies = false;
opts.Get("useSessionCookies", &use_session_cookies);
if (!use_session_cookies) {
request->load_flags |= net::LOAD_DO_NOT_SEND_COOKIES;
}

// Chromium filters headers using browser rules, while for net module we have
// every header passed.
request->report_raw_headers = true;
Expand Down