forked from eclipse-birt/birt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow cookies to be set by javascript. Fixes eclipse-birt#32.
- Loading branch information
1 parent
f814ecf
commit 4c520bd
Showing
10 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
src/com/machinepublishers/jbrowserdriver/CookieStore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* jBrowserDriver (TM) | ||
* Copyright (C) 2014-2016 Machine Publishers, LLC | ||
* | ||
* Sales and support: ops@machinepublishers.com | ||
* Updates: https://github.com/MachinePublishers/jBrowserDriver | ||
* | ||
* 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. | ||
*/ | ||
package com.machinepublishers.jbrowserdriver; | ||
|
||
import java.io.IOException; | ||
import java.io.Serializable; | ||
import java.net.CookieHandler; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.apache.http.cookie.Cookie; | ||
import org.apache.http.cookie.CookieOrigin; | ||
import org.apache.http.cookie.CookieSpec; | ||
import org.apache.http.cookie.MalformedCookieException; | ||
import org.apache.http.impl.client.BasicCookieStore; | ||
import org.apache.http.impl.cookie.LaxCookieSpecProvider; | ||
import org.apache.http.message.BasicHeader; | ||
|
||
public class CookieStore extends CookieHandler implements org.apache.http.client.CookieStore, Serializable { | ||
|
||
private static final CookieSpec spec = new LaxCookieSpecProvider().create(null); | ||
private final org.apache.http.client.CookieStore store = new BasicCookieStore(); | ||
|
||
@Override | ||
public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException { | ||
final String reqHost = canonicalHost(uri.getHost()); | ||
final String reqPath = canonicalPath(uri.getPath()); | ||
final boolean reqSecure = isSecure(uri.getScheme()); | ||
final boolean reqJavascript = isJavascript(uri.getScheme()); | ||
StringBuilder builder = new StringBuilder(); | ||
if (reqJavascript) { | ||
List<Cookie> list; | ||
synchronized (store) { | ||
store.clearExpired(new Date()); | ||
list = store.getCookies(); | ||
} | ||
for (Cookie cookie : list) { | ||
if ((!cookie.isSecure() || reqSecure) | ||
&& reqHost.endsWith(canonicalHost(cookie.getDomain())) | ||
&& reqPath.startsWith(canonicalPath(cookie.getPath()))) { | ||
if (builder.length() > 0) { | ||
builder.append(';'); | ||
} | ||
builder.append(cookie.getName()); | ||
builder.append('='); | ||
builder.append(cookie.getValue()); | ||
} | ||
} | ||
} | ||
String cookies = builder.length() == 0 ? null : builder.toString(); | ||
Map<String, List<String>> map; | ||
if (cookies != null) { | ||
map = new HashMap<String, List<String>>(); | ||
map.put("Cookie", Arrays.asList(cookies)); | ||
} else { | ||
map = Collections.emptyMap(); | ||
} | ||
return map; | ||
} | ||
|
||
@Override | ||
public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException { | ||
if (isJavascript(uri.getScheme())) { | ||
for (Map.Entry<String, List<String>> entry : responseHeaders.entrySet()) { | ||
for (String value : entry.getValue()) { | ||
try { | ||
List<Cookie> cookies = spec.parse(new BasicHeader(entry.getKey(), value), | ||
new CookieOrigin(uri.getHost(), uri.getPort(), uri.getPath(), isSecure(uri.getScheme()))); | ||
for (Cookie cookie : cookies) { | ||
synchronized (store) { | ||
store.addCookie(cookie); | ||
} | ||
} | ||
} catch (MalformedCookieException e) { | ||
LogsServer.instance().warn( | ||
"Malformed cookie for cookie named " + entry.getKey() + ". " + e.getMessage()); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static boolean isSecure(String scheme) { | ||
return "https".equalsIgnoreCase(scheme) || "javascripts".equalsIgnoreCase(scheme); | ||
} | ||
|
||
private static boolean isJavascript(String scheme) { | ||
return "javascripts".equalsIgnoreCase(scheme) || "javascript".equalsIgnoreCase(scheme); | ||
} | ||
|
||
private static String canonicalHost(String host) { | ||
if (host == null || host.isEmpty()) { | ||
return ""; | ||
} | ||
return host.startsWith(".") ? host.toLowerCase() : "." + host.toLowerCase(); | ||
} | ||
|
||
private static String canonicalPath(String path) { | ||
if (path == null || path.isEmpty()) { | ||
return ""; | ||
} | ||
String canonical = path; | ||
canonical = canonical.startsWith("/") ? canonical : "/" + canonical; | ||
canonical = canonical.endsWith("/") ? canonical : canonical + "/"; | ||
return canonical.toLowerCase(); | ||
} | ||
|
||
@Override | ||
public void addCookie(Cookie cookie) { | ||
synchronized (store) { | ||
store.addCookie(cookie); | ||
} | ||
} | ||
|
||
@Override | ||
public List<Cookie> getCookies() { | ||
synchronized (store) { | ||
return store.getCookies(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean clearExpired(Date date) { | ||
synchronized (store) { | ||
return store.clearExpired(date); | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
synchronized (store) { | ||
store.clear(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters