diff --git a/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerPathHandler.kt b/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerPathHandler.kt index ae9ffc15411d..868d1f0b8393 100644 --- a/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerPathHandler.kt +++ b/platform/built-in-server/src/org/jetbrains/builtInWebServer/DefaultWebServerPathHandler.kt @@ -16,7 +16,9 @@ package org.jetbrains.builtInWebServer import com.intellij.openapi.project.Project -import com.intellij.openapi.util.io.parentPath +import com.intellij.openapi.util.io.endsWithName +import com.intellij.openapi.util.io.endsWithSlash +import com.intellij.openapi.util.io.getParentPath import com.intellij.openapi.vfs.LocalFileSystem import com.intellij.util.PathUtilRt import io.netty.channel.ChannelHandlerContext @@ -51,7 +53,7 @@ private class DefaultWebServerPathHandler : WebServerPathHandler() { var virtualFile = pathInfo.file val isDirectory = if (virtualFile == null) pathInfo.ioFile!!.isDirectory else virtualFile.isDirectory if (isDirectory) { - if (!WebServerPathHandler.endsWithSlash(decodedRawPath)) { + if (!endsWithSlash(decodedRawPath)) { WebServerPathHandler.redirectToDirectory(request, channel, if (isCustomHost) path else (projectName + '/' + path)) return true } @@ -71,13 +73,14 @@ private class DefaultWebServerPathHandler : WebServerPathHandler() { pathToFileManager.pathToInfoCache.put(path, pathInfo) } - else if (!path.endsWith(pathInfo.name)) { - if (WebServerPathHandler.endsWithSlash(decodedRawPath)) { + else if (!endsWithName(path, pathInfo.name)) { + if (endsWithSlash(decodedRawPath)) { indexUsed = true } else { // FallbackResource feature in action, /login requested, /index.php retrieved, we must not redirect /login to /login/ - if (path.endsWith(PathUtilRt.getFileName(pathInfo.path.parentPath!!))) { + val parentPath = getParentPath(pathInfo.path) + if (parentPath != null && endsWithName(path, PathUtilRt.getFileName(parentPath))) { WebServerPathHandler.redirectToDirectory(request, channel, if (isCustomHost) path else ("$projectName/$path")) return true } diff --git a/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathHandler.java b/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathHandler.java index 09f1b1f343c5..e8426be24ff7 100644 --- a/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathHandler.java +++ b/platform/built-in-server/src/org/jetbrains/builtInWebServer/WebServerPathHandler.java @@ -51,8 +51,4 @@ public abstract class WebServerPathHandler { response.headers().add(HttpHeaderNames.LOCATION, url.toASCIIString()); Responses.send(response, channel, request); } - - protected static boolean endsWithSlash(@NotNull String path) { - return path.charAt(path.length() - 1) == '/'; - } } \ No newline at end of file diff --git a/platform/configuration-store-impl/src/ImportSettingsAction.kt b/platform/configuration-store-impl/src/ImportSettingsAction.kt index a92a0d5e3c88..52a53d312f61 100644 --- a/platform/configuration-store-impl/src/ImportSettingsAction.kt +++ b/platform/configuration-store-impl/src/ImportSettingsAction.kt @@ -30,7 +30,7 @@ import com.intellij.openapi.ui.Messages import com.intellij.openapi.updateSettings.impl.UpdateSettings import com.intellij.openapi.util.io.FileUtil import com.intellij.openapi.util.io.FileUtilRt -import com.intellij.openapi.util.io.parentPath +import com.intellij.openapi.util.io.getParentPath import com.intellij.util.Consumer import gnu.trove.THashSet import java.io.File @@ -132,7 +132,7 @@ fun getPaths(input: InputStream): Set { var path = entry.name result.add(path) while (true) { - path = path.parentPath ?: break + path = getParentPath(path) ?: break result.add("$path/") } } diff --git a/platform/platform-impl/src/com/intellij/openapi/util/io/fileUtil.kt b/platform/platform-impl/src/com/intellij/openapi/util/io/fileUtil.kt index 19ca3fc6823b..ed9ee24dff54 100644 --- a/platform/platform-impl/src/com/intellij/openapi/util/io/fileUtil.kt +++ b/platform/platform-impl/src/com/intellij/openapi/util/io/fileUtil.kt @@ -15,6 +15,8 @@ */ package com.intellij.openapi.util.io +import com.intellij.openapi.util.text.StringUtil +import com.intellij.util.PathUtilRt import java.io.File val File.systemIndependentPath: String @@ -23,14 +25,9 @@ val File.systemIndependentPath: String val File.parentSystemIndependentPath: String get() = getParent().replace(File.separatorChar, '/') -val String.parentPath: String? - get() { - if (isEmpty()) { - return null - } - var end = Math.max(lastIndexOf('/'), lastIndexOf('\\')) - if (end == length() - 1) { - end = Math.max(lastIndexOf('/', end - 1), lastIndexOf('\\', end - 1)) - } - return if (end == -1) null else substring(0, end) - } \ No newline at end of file +// PathUtilRt.getParentPath returns empty string if no parent path, but in Kotlin "null" is better because elvis operator could be used +fun getParentPath(path: String) = StringUtil.nullize(PathUtilRt.getParentPath(path)) + +fun endsWithSlash(path: String) = path.getOrNull(path.length() - 1) == '/' + +fun endsWithName(path: String, name: String) = path.endsWith(name) && (path.length() == name.length() || path.getOrNull(path.length() - name.length() - 1) == '/') \ No newline at end of file diff --git a/platform/platform-tests/platform-tests.iml b/platform/platform-tests/platform-tests.iml index f86f03c172f9..0fe926210d3e 100644 --- a/platform/platform-tests/platform-tests.iml +++ b/platform/platform-tests/platform-tests.iml @@ -27,5 +27,6 @@ + \ No newline at end of file diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/util/io/FileUtilKtTest.kt b/platform/platform-tests/testSrc/com/intellij/openapi/util/io/FileUtilKtTest.kt new file mode 100644 index 000000000000..66987e2697ab --- /dev/null +++ b/platform/platform-tests/testSrc/com/intellij/openapi/util/io/FileUtilKtTest.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2000-2015 JetBrains s.r.o. + * + * 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.intellij.openapi.util.io + +import org.assertj.core.api.Assertions.assertThat +import org.junit.Test + +private class FileUtilKtTest { + @Test fun testEndsWithName() { + assertThat(endsWithName("foo", "bar")).isFalse() + assertThat(endsWithName("foo", "foo")).isTrue() + assertThat(endsWithName("foo/bar", "foo")).isFalse() + assertThat(endsWithName("foo/bar", "bar")).isTrue() + assertThat(endsWithName("/foo", "foo")).isTrue() + assertThat(endsWithName("fooBar", "Bar")).isFalse() + assertThat(endsWithName("/foo/bar_bar", "bar")).isFalse() + } +} \ No newline at end of file