DefaultRemoteContentProvider must explicitly set token in the headers (Open URL and...
authorVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Wed, 27 Apr 2016 13:54:01 +0000 (15:54 +0200)
committerVladimir Krivosheev <vladimir.krivosheev@jetbrains.com>
Wed, 27 Apr 2016 13:54:01 +0000 (15:54 +0200)
platform/built-in-server-api/src/org/jetbrains/ide/BuiltInServerManager.java
platform/built-in-server/src/org/jetbrains/builtInWebServer/BuiltInWebServer.kt
platform/built-in-server/src/org/jetbrains/ide/BuiltInServerManagerImpl.java
platform/platform-impl/src/com/intellij/openapi/vfs/impl/http/DefaultRemoteContentProvider.java
xml/impl/src/com/intellij/ide/browsers/BrowserLauncherImpl.java
xml/impl/xml.iml

index fff0a1564531c3e0f495470b67b4f634de1a5459..448ede483363198895982a4a0c6d6f4ffae8b2e7 100644 (file)
@@ -18,8 +18,12 @@ package org.jetbrains.ide;
 import com.intellij.openapi.Disposable;
 import com.intellij.openapi.application.ApplicationManager;
 import com.intellij.openapi.components.ApplicationComponent;
+import com.intellij.util.Url;
+import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
 
+import java.net.URLConnection;
+
 public abstract class BuiltInServerManager extends ApplicationComponent.Adapter {
   public static BuiltInServerManager getInstance() {
     return ApplicationManager.getApplication().getComponent(BuiltInServerManager.class);
@@ -31,4 +35,8 @@ public abstract class BuiltInServerManager extends ApplicationComponent.Adapter
 
   @Nullable
   public abstract Disposable getServerDisposable();
+  
+  public abstract boolean isOnBuiltInWebServer(@Nullable Url url);
+  
+  public abstract void configureRequestToWebServer(@NotNull URLConnection connection);
 }
\ No newline at end of file
index 308937b079c9084353aa2e3ddcaceedfd79f239b..334cf2ede98f9f08f2e5c98f15fa5ef4c70adb9d 100644 (file)
@@ -262,16 +262,18 @@ internal fun validateToken(request: HttpRequest, channel: Channel): HttpHeaders?
     return DefaultHttpHeaders().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode(STANDARD_COOKIE) + "; SameSite=strict")
   }
 
-  SwingUtilities.invokeAndWait {
-    ProjectUtil.focusProjectWindow(null, true)
-
-    if (MessageDialogBuilder
-        .yesNo("", "Page '" + StringUtil.trimMiddle(url, 50) + "' requested without authorization, " +
-            "\nyou can copy URL and open it in browser to trust it.")
-        .icon(Messages.getWarningIcon())
-        .yesText("Copy authorization URL to clipboard")
-        .show() == Messages.YES) {
-      CopyPasteManager.getInstance().setContents(StringSelection(url + "?" + TOKEN_PARAM_NAME + "=" + acquireToken()))
+  if (!urlDecoder.path().endsWith("/favicon.ico")) {
+    SwingUtilities.invokeAndWait {
+      ProjectUtil.focusProjectWindow(null, true)
+
+      if (MessageDialogBuilder
+          .yesNo("", "Page '" + StringUtil.trimMiddle(url, 50) + "' requested without authorization, " +
+              "\nyou can copy URL and open it in browser to trust it.")
+          .icon(Messages.getWarningIcon())
+          .yesText("Copy authorization URL to clipboard")
+          .show() == Messages.YES) {
+        CopyPasteManager.getInstance().setContents(StringSelection(url + "?" + TOKEN_PARAM_NAME + "=" + acquireToken()))
+      }
     }
   }
 
index 6e33b54cbe247232f6d157913060965170da9854..941a0cb6566bee15362ec7d842f169c413274728 100644 (file)
@@ -10,13 +10,21 @@ import com.intellij.openapi.application.ApplicationNamesInfo;
 import com.intellij.openapi.diagnostic.Logger;
 import com.intellij.openapi.util.Disposer;
 import com.intellij.openapi.util.NotNullLazyValue;
+import com.intellij.openapi.util.text.StringUtil;
+import com.intellij.util.Url;
+import com.intellij.util.net.NetUtils;
 import io.netty.channel.oio.OioEventLoopGroup;
 import org.jetbrains.annotations.NonNls;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
+import org.jetbrains.builtInWebServer.BuiltInServerOptions;
+import org.jetbrains.builtInWebServer.BuiltInWebServerKt;
 import org.jetbrains.io.BuiltInServer;
 import org.jetbrains.io.SubServer;
 
+import java.net.InetAddress;
+import java.net.URLConnection;
+import java.net.UnknownHostException;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
 import java.util.concurrent.atomic.AtomicBoolean;
@@ -116,6 +124,16 @@ public class BuiltInServerManagerImpl extends BuiltInServerManager {
     return server;
   }
 
+  @Override
+  public boolean isOnBuiltInWebServer(@Nullable Url url) {
+    return url != null && !StringUtil.isEmpty(url.getAuthority()) && isOnBuiltInWebServerByAuthority(url.getAuthority());
+  }
+
+  @Override
+  public void configureRequestToWebServer(@NotNull URLConnection connection) {
+    connection.setRequestProperty(BuiltInWebServerKt.TOKEN_HEADER_NAME, BuiltInWebServerKt.acquireToken());
+  }
+
   private static void bindCustomPorts(@NotNull BuiltInServer server) {
     if (ApplicationManager.getApplication().isUnitTestMode()) {
       return;
@@ -130,4 +148,41 @@ public class BuiltInServerManagerImpl extends BuiltInServerManager {
       }
     }
   }
+  
+  public static boolean isOnBuiltInWebServerByAuthority(@NotNull String authority) {
+    int portIndex = authority.indexOf(':');
+    if (portIndex < 0 || portIndex == authority.length() - 1) {
+      return false;
+    }
+
+    int port;
+    try {
+      port = Integer.parseInt(authority.substring(portIndex + 1));
+    }
+    catch (NumberFormatException ignored) {
+      return false;
+    }
+
+    if (BuiltInServerOptions.getInstance().builtInServerPort != port && BuiltInServerManager.getInstance().getPort() != port) {
+      return false;
+    }
+
+    String host = authority.substring(0, portIndex);
+    if (NetUtils.isLocalhost(host)) {
+      return true;
+    }
+
+    InetAddress inetAddress;
+    try {
+      inetAddress = InetAddress.getByName(host);
+    }
+    catch (UnknownHostException ignored) {
+      return false;
+    }
+
+    if (inetAddress == null) {
+      return false;
+    }
+    return inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress();
+  }
 }
\ No newline at end of file
index c74cecb41df77708a96f4737693309aa55fb0d76..4f0baf1bd92f6182c05511153b46ab14dccb2d7b 100644 (file)
@@ -29,6 +29,7 @@ import com.intellij.util.Url;
 import com.intellij.util.io.HttpRequests;
 import com.intellij.util.net.ssl.CertificateManager;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.ide.BuiltInServerManager;
 
 import java.io.File;
 import java.io.IOException;
@@ -60,6 +61,12 @@ public class DefaultRemoteContentProvider extends RemoteContentProvider {
         .connectTimeout(60 * 1000)
         .productNameAsUserAgent()
         .hostNameVerifier(CertificateManager.HOSTNAME_VERIFIER)
+        .tuner(connection -> {
+          BuiltInServerManager builtInServerManager = BuiltInServerManager.getInstance();
+          if (builtInServerManager.isOnBuiltInWebServer(url)) {
+            builtInServerManager.configureRequestToWebServer(connection);
+          }
+        })
         .connect(new HttpRequests.RequestProcessor<Object>() {
           @Override
           public Object process(@NotNull HttpRequests.Request request) throws IOException {
index 7a62abe0b33f35ea11a4237aaf097a4ad20cf437..4b509f7119a3b46f21f3c3f9ec97924784bbcb50 100644 (file)
@@ -30,70 +30,26 @@ import com.intellij.openapi.util.registry.Registry;
 import com.intellij.openapi.util.text.StringUtil;
 import com.intellij.ui.AppUIUtil;
 import com.intellij.util.ArrayUtil;
-import com.intellij.util.Url;
 import com.intellij.util.Urls;
-import com.intellij.util.net.NetUtils;
 import org.jetbrains.annotations.NotNull;
 import org.jetbrains.annotations.Nullable;
-import org.jetbrains.builtInWebServer.BuiltInServerOptions;
 import org.jetbrains.ide.BuiltInServerManager;
 
-import java.net.InetAddress;
 import java.net.URI;
-import java.net.UnknownHostException;
 import java.util.concurrent.Future;
 import java.util.concurrent.TimeUnit;
 
 public final class BrowserLauncherImpl extends BrowserLauncherAppless {
   @Override
   public void browse(@NotNull String url, @Nullable WebBrowser browser, @Nullable Project project) {
-    if (Registry.is("ide.built.in.web.server.activatable", false)) {
-      Url parsedUrl = Urls.parse(url, false);
-      if (parsedUrl != null && parsedUrl.getAuthority() != null && isOnBuiltInWebServerByAuthority(parsedUrl.getAuthority())) {
-        PropertiesComponent.getInstance().setValue("ide.built.in.web.server.active", true);
-      }
+    if (Registry.is("ide.built.in.web.server.activatable", false) &&
+        BuiltInServerManager.getInstance().isOnBuiltInWebServer(Urls.parse(url, false))) {
+      PropertiesComponent.getInstance().setValue("ide.built.in.web.server.active", true);
     }
     
     super.browse(url, browser, project);
   }
 
-  public static boolean isOnBuiltInWebServerByAuthority(@NotNull String authority) {
-    int portIndex = authority.indexOf(':');
-    if (portIndex < 0 || portIndex == authority.length() - 1) {
-      return false;
-    }
-
-    int port;
-    try {
-      port = Integer.parseInt(authority.substring(portIndex + 1));
-    }
-    catch (NumberFormatException ignored) {
-      return false;
-    }
-
-    if (BuiltInServerOptions.getInstance().builtInServerPort != port && BuiltInServerManager.getInstance().getPort() != port) {
-      return false;
-    }
-
-    String host = authority.substring(0, portIndex);
-    if (NetUtils.isLocalhost(host)) {
-      return true;
-    }
-
-    InetAddress inetAddress;
-    try {
-      inetAddress = InetAddress.getByName(host);
-    }
-    catch (UnknownHostException ignored) {
-      return false;
-    }
-
-    if (inetAddress == null) {
-      return false;
-    }
-    return inetAddress.isLoopbackAddress() || inetAddress.isAnyLocalAddress();
-  }
-
   @Override
   protected void browseUsingNotSystemDefaultBrowserPolicy(@NotNull URI uri, @NotNull GeneralSettings settings, @Nullable Project project) {
     WebBrowserManager browserManager = WebBrowserManager.getInstance();
index fb6c556fefcf622862904ef86b71347db8170f7b..447d4923d47e31d736b4a92d9e8155923ce8686b 100644 (file)
@@ -23,7 +23,7 @@
     <orderEntry type="module" module-name="xml-structure-view-impl" exported="" />
     <orderEntry type="library" name="Netty" level="project" />
     <orderEntry type="module" module-name="xdebugger-api" />
-    <orderEntry type="module" module-name="built-in-server" />
+    <orderEntry type="module" module-name="built-in-server-api" />
   </component>
   <component name="copyright">
     <Base>