@NotNull public static List<Url> getUrls(@NotNull VirtualFile file, @NotNull Project project, @Nullable String currentAuthority) { if (currentAuthority != null && !compareAuthority(currentAuthority)) { return Collections.emptyList(); } String path = WebServerPathToFileManager.getInstance(project).getPath(file); if (path == null) { return Collections.emptyList(); } int effectiveBuiltInServerPort = BuiltInServerOptions.getInstance().getEffectiveBuiltInServerPort(); Url url = Urls.newHttpUrl(currentAuthority == null ? "localhost:" + effectiveBuiltInServerPort : currentAuthority, '/' + project.getName() + '/' + path); int defaultPort = BuiltInServerManager.getInstance().getPort(); if (currentAuthority != null || defaultPort == effectiveBuiltInServerPort) { return Collections.singletonList(url); } return Arrays.asList(url, Urls.newHttpUrl("localhost:" + defaultPort, '/' + project.getName() + '/' + path)); }
@NotNull @Override public Collection<Url> getUrlsToOpen(@NotNull OpenInBrowserRequest request, boolean preferLocalUrl) throws WebBrowserUrlProvider.BrowserException { boolean isHtmlOrXml = isHtmlOrXmlFile(request.getFile().getViewProvider().getBaseLanguage()); if (!preferLocalUrl || !isHtmlOrXml) { DumbService dumbService = DumbService.getInstance(request.getProject()); for (WebBrowserUrlProvider urlProvider : WebBrowserUrlProvider.EP_NAME.getExtensions()) { if ((!dumbService.isDumb() || DumbService.isDumbAware(urlProvider)) && urlProvider.canHandleElement(request)) { Collection<Url> urls = getUrls(urlProvider, request); if (!urls.isEmpty()) { return urls; } } } if (!isHtmlOrXml) { return Collections.emptyList(); } } VirtualFile file = request.getVirtualFile(); return file instanceof LightVirtualFile || !request.getFile().getViewProvider().isPhysical() ? Collections.<Url>emptyList() : Collections.singletonList(Urls.newFromVirtualFile(file)); }
public static boolean parseAndCheckIsLocalHost(String uri, boolean onlyAnyOrLoopback, boolean hostsOnly) { if (uri == null || uri.equals("about:blank")) { return true; } try { Url parsedUri = Urls.parse(uri, false); if (parsedUri == null) { return false; } String host = getHost(parsedUri); return host != null && (isTrustedChromeExtension(parsedUri) || isLocalHost(host, onlyAnyOrLoopback, hostsOnly)); } catch (Exception ignored) { } return false; }
@Override public Image get(Object key) { if (myManager == null || key == null) return null; PsiElement element = getElement(); if (element == null) return null; URL url = (URL)key; Image inMemory = myManager.getElementImage(element, url.toExternalForm()); if (inMemory != null) { return inMemory; } Url parsedUrl = Urls.parseEncoded(url.toExternalForm()); BuiltInServerManager builtInServerManager = BuiltInServerManager.getInstance(); if (parsedUrl != null && builtInServerManager.isOnBuiltInWebServer(parsedUrl)) { try { url = new URL(builtInServerManager.addAuthToken(parsedUrl).toExternalForm()); } catch (MalformedURLException e) { LOG.warn(e); } } return Toolkit.getDefaultToolkit().createImage(url); }
public SourceResolver(@NotNull List<String> sourceUrls, boolean trimFileScheme, @Nullable Url baseFileUrl, boolean baseUrlIsFile, @Nullable List<String> sourceContents) { rawSources = sourceUrls; this.sourceContents = sourceContents; canonicalizedSources = new Url[sourceUrls.size()]; canonicalizedSourcesMap = SystemInfo.isFileSystemCaseSensitive ? new ObjectIntHashMap<Url>(canonicalizedSources.length) : new ObjectIntHashMap<Url>(canonicalizedSources.length, Urls.getCaseInsensitiveUrlHashingStrategy()); for (int i = 0; i < sourceUrls.size(); i++) { String rawSource = sourceUrls.get(i); Url url = canonicalizeUrl(rawSource, baseFileUrl, trimFileScheme, i, baseUrlIsFile); canonicalizedSources[i] = url; canonicalizedSourcesMap.put(url, i); } }
protected Url canonicalizeUrl(@NotNull String url, @Nullable Url baseUrl, boolean trimFileScheme, int sourceIndex, boolean baseUrlIsFile) { if (trimFileScheme && url.startsWith(StandardFileSystems.FILE_PROTOCOL_PREFIX)) { return Urls.newLocalFileUrl(FileUtil.toCanonicalPath(VfsUtilCore.toIdeaUrl(url, true).substring(StandardFileSystems.FILE_PROTOCOL_PREFIX.length()), '/')); } else if (baseUrl == null || url.contains(URLUtil.SCHEME_SEPARATOR) || url.startsWith("data:") || url.startsWith("blob:") || url.startsWith("javascript:")) { return Urls.parseEncoded(url); } String path = canonicalizePath(url, baseUrl, baseUrlIsFile); if (baseUrl.getScheme() == null && baseUrl.isInLocalFileSystem()) { return Urls.newLocalFileUrl(path); } // browserify produces absolute path in the local filesystem if (isAbsolute(path)) { VirtualFile file = LocalFileFinder.findFile(path); if (file != null) { if (absoluteLocalPathToSourceIndex == null) { // must be linked, on iterate original path must be first absoluteLocalPathToSourceIndex = createStringIntMap(rawSources.size()); sourceIndexToAbsoluteLocalPath = new String[rawSources.size()]; } absoluteLocalPathToSourceIndex.put(path, sourceIndex); sourceIndexToAbsoluteLocalPath[sourceIndex] = path; String canonicalPath = file.getCanonicalPath(); if (canonicalPath != null && !canonicalPath.equals(path)) { absoluteLocalPathToSourceIndex.put(canonicalPath, sourceIndex); } return Urls.newLocalFileUrl(path); } } return new UrlImpl(baseUrl.getScheme(), baseUrl.getAuthority(), path, null); }
@Nullable private MappingList findByFile(@NotNull SourceMap sourceMap, @NotNull VirtualFile sourceFile) { MappingList mappings = null; if (absoluteLocalPathToSourceIndex != null && sourceFile.isInLocalFileSystem()) { mappings = getMappingsBySource(sourceMap, absoluteLocalPathToSourceIndex.get(sourceFile.getPath())); if (mappings == null) { String sourceFileCanonicalPath = sourceFile.getCanonicalPath(); if (sourceFileCanonicalPath != null) { mappings = getMappingsBySource(sourceMap, absoluteLocalPathToSourceIndex.get(sourceFileCanonicalPath)); } } } if (mappings == null) { int index = canonicalizedSourcesMap.get(Urls.newFromVirtualFile(sourceFile).trimParameters()); if (index != -1) { return sourceMap.sourceIndexToMappings[index]; } for (int i = 0; i < canonicalizedSources.length; i++) { Url url = canonicalizedSources[i]; if (Urls.equalsIgnoreParameters(url, sourceFile)) { return sourceMap.sourceIndexToMappings[i]; } VirtualFile canonicalFile = sourceFile.getCanonicalFile(); if (canonicalFile != null && !canonicalFile.equals(sourceFile) && Urls.equalsIgnoreParameters(url, canonicalFile)) { return sourceMap.sourceIndexToMappings[i]; } } } return mappings; }
@Nullable @Override protected Url getUrl(@NotNull OpenInBrowserRequest request, @NotNull VirtualFile file) throws BrowserException { if (file instanceof HttpVirtualFile) { return Urls.newFromVirtualFile(file); } else { return ContainerUtil.getFirstItem(getUrls(file, request.getProject(), null)); } }
@Override @NotNull public VirtualFile createChild(@NotNull VirtualFile parent, @NotNull String name, boolean isDirectory) { String parentPath = parent.getPath(); boolean hasEndSlash = parentPath.charAt(parentPath.length() - 1) == '/'; return getRemoteFileManager().getOrCreateFile((HttpVirtualFileImpl)parent, Urls.newFromIdea(parent.getUrl() + (hasEndSlash ? "" : '/') + name), parentPath + (hasEndSlash ? "" : '/') + name, isDirectory); }
@Nullable private static HostAndPort getHostAndPort(@NotNull String rawUrl) { URI url = Urls.parseAsJavaUriWithoutParameters(rawUrl); if (url == null) { return null; } int port = url.getPort(); if (port == -1) { port = "https".equals(url.getScheme()) ? 443 : 80; } return HostAndPort.fromParts(StringUtil.notNullize(url.getHost(), "127.0.0.1"), port); }
@NotNull @Override public Collection<Url> getUrlsToOpen(@NotNull OpenInBrowserRequest request, boolean preferLocalUrl) throws WebBrowserUrlProvider.BrowserException { VirtualFile virtualFile = request.getVirtualFile(); if(virtualFile instanceof HttpVirtualFile) { return Collections.singleton(Urls.newFromVirtualFile(virtualFile)); } if(!preferLocalUrl || !HtmlUtil.isHtmlFile(request.getFile())) { WebBrowserUrlProvider provider = getProvider(request); if(provider != null) { if(request.getResult() != null) { return request.getResult(); } try { Collection<Url> urls = provider.getUrls(request); if(!urls.isEmpty()) { return urls; } } catch(WebBrowserUrlProvider.BrowserException e) { if(!HtmlUtil.isHtmlFile(request.getFile())) { throw e; } } } } return virtualFile instanceof LightVirtualFile || !request.getFile().getViewProvider().isPhysical() ? Collections.<Url>emptySet() : Collections.singleton(Urls.newFromVirtualFile(virtualFile)); }
@NotNull protected Url rawUrlToOurUrl(@NotNull String url) { //noinspection ConstantConditions return Urls.parseEncoded(url); }
public VirtualFile findFileByPath(@NotNull String path, boolean isDirectory) { return getRemoteFileManager().getOrCreateFile(null, Urls.newFromIdea(VirtualFileManager.constructUrl(myProtocol, path)), path, isDirectory); }