1/*
2 * Copyright (C) 2009, 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include "config.h"
32#include "ThreadableWebSocketChannel.h"
33
34#include "ContentRuleListResults.h"
35#include "Document.h"
36#include "Page.h"
37#include "RuntimeEnabledFeatures.h"
38#include "ScriptExecutionContext.h"
39#include "SocketProvider.h"
40#include "ThreadableWebSocketChannelClientWrapper.h"
41#include "UserContentProvider.h"
42#include "WebSocketChannel.h"
43#include "WebSocketChannelClient.h"
44#include "WorkerGlobalScope.h"
45#include "WorkerRunLoop.h"
46#include "WorkerThread.h"
47#include "WorkerThreadableWebSocketChannel.h"
48
49namespace WebCore {
50
51Ref<ThreadableWebSocketChannel> ThreadableWebSocketChannel::create(ScriptExecutionContext& context, WebSocketChannelClient& client, SocketProvider& provider)
52{
53 if (is<WorkerGlobalScope>(context)) {
54 WorkerGlobalScope& workerGlobalScope = downcast<WorkerGlobalScope>(context);
55 WorkerRunLoop& runLoop = workerGlobalScope.thread().runLoop();
56 return WorkerThreadableWebSocketChannel::create(workerGlobalScope, client, makeString("webSocketChannelMode", runLoop.createUniqueId()), provider);
57 }
58
59 auto& document = downcast<Document>(context);
60
61#if HAVE(NSURLSESSION_WEBSOCKET)
62 if (RuntimeEnabledFeatures::sharedFeatures().isNSURLSessionWebSocketEnabled()) {
63 if (auto channel = provider.createWebSocketChannel(document, client))
64 return channel.releaseNonNull();
65 }
66#endif
67 return WebSocketChannel::create(document, client, provider);
68}
69
70Optional<ThreadableWebSocketChannel::ValidatedURL> ThreadableWebSocketChannel::validateURL(Document& document, const URL& requestedURL)
71{
72 ValidatedURL validatedURL { requestedURL, true };
73#if ENABLE(CONTENT_EXTENSIONS)
74 if (auto* page = document.page()) {
75 if (auto* documentLoader = document.loader()) {
76 auto results = page->userContentProvider().processContentRuleListsForLoad(validatedURL.url, ContentExtensions::ResourceType::Raw, *documentLoader);
77 if (results.summary.blockedLoad)
78 return { };
79 if (results.summary.madeHTTPS) {
80 ASSERT(validatedURL.url.protocolIs("ws"));
81 validatedURL.url.setProtocol("wss");
82 }
83 validatedURL.areCookiesAllowed = !results.summary.blockedCookies;
84 }
85 }
86#endif
87 return validatedURL;
88}
89
90Optional<ResourceRequest> ThreadableWebSocketChannel::webSocketConnectRequest(Document& document, const URL& url)
91{
92 auto validatedURL = validateURL(document, url);
93 if (!validatedURL)
94 return { };
95
96 ResourceRequest request { validatedURL->url };
97 request.setHTTPUserAgent(document.userAgent(validatedURL->url));
98 request.setDomainForCachePartition(document.domainForCachePartition());
99 request.setAllowCookies(validatedURL->areCookiesAllowed);
100
101 // Add no-cache headers to avoid compatibility issue.
102 // There are some proxies that rewrite "Connection: upgrade"
103 // to "Connection: close" in the response if a request doesn't contain
104 // these headers.
105 request.addHTTPHeaderField(HTTPHeaderName::Pragma, "no-cache");
106 request.addHTTPHeaderField(HTTPHeaderName::CacheControl, "no-cache");
107
108 return request;
109}
110
111} // namespace WebCore
112