import com.intellij.openapi.project.ProjectManager
import com.intellij.openapi.util.SystemInfoRt
import com.intellij.openapi.util.io.FileUtil
+import com.intellij.openapi.util.io.FileUtilRt
import com.intellij.openapi.util.io.endsWithName
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.util.UriUtil
import io.netty.channel.ChannelHandlerContext
import io.netty.handler.codec.http.*
import org.jetbrains.ide.HttpRequestHandler
-import org.jetbrains.io.host
-import org.jetbrains.io.isLocalOrigin
-import org.jetbrains.io.send
+import org.jetbrains.io.*
import java.io.IOException
import java.net.InetAddress
import java.nio.file.Path
return true
}
+ if (request.origin == null && request.referrer == null && request.isRegularBrowser() && !canBeAccessedDirectly(path)) {
+ HttpResponseStatus.NOT_FOUND.send(context.channel(), request)
+ return true
+ }
+
for (pathHandler in WebServerPathHandler.EP_NAME.extensions) {
LOG.catchAndLog {
if (pathHandler.process(path, project, request, context, projectName, decodedPath, isCustomHost)) {
catch (ignored: IOException) {
return false
}
+}
+
+private fun canBeAccessedDirectly(path: String): Boolean {
+ for (fileHandler in WebServerFileHandler.EP_NAME.extensions) {
+ for (ext in fileHandler.pageFileExtensions) {
+ if (FileUtilRt.extensionEquals(path, ext)) {
+ return true
+ }
+ }
+ }
+ return false
}
\ No newline at end of file
import java.nio.file.Paths
private class StaticFileHandler : WebServerFileHandler() {
+ override val pageFileExtensions = arrayOf("html", "htm", "shtml")
+
private var ssiProcessor: SsiProcessor? = null
override fun process(pathInfo: PathInfo, canonicalPath: CharSequence, project: Project, request: FullHttpRequest, channel: Channel, projectNameIfNotCustomHost: String?): Boolean {
internal val EP_NAME = ExtensionPointName.create<WebServerFileHandler>("org.jetbrains.webServerFileHandler")
}
+ open val pageFileExtensions: Array<String>
+ get() = emptyArray()
+
/**
* canonicalRequestPath contains index file name (if not specified in the request)
*/
request: FullHttpRequest,
channel: Channel,
projectNameIfNotCustomHost: String?): Boolean
-
}
fun getRequestPath(canonicalPath: CharSequence, projectNameIfNotCustomHost: String?) = if (projectNameIfNotCustomHost == null) "/$canonicalPath" else "/$projectNameIfNotCustomHost/$canonicalPath"
\ No newline at end of file
return false
}
+fun HttpRequest.isRegularBrowser() = userAgent?.startsWith("Mozilla/5.0") ?: false
+
// forbid POST requests from browser without Origin
fun HttpRequest.isWriteFromBrowserWithoutOrigin(): Boolean {
val userAgent = userAgent ?: return false
val method = method()
- return origin.isNullOrEmpty() && userAgent.startsWith("Mozilla/5.0") && (method == HttpMethod.POST || method == HttpMethod.PATCH || method == HttpMethod.PUT || method == HttpMethod.DELETE)
+ return origin.isNullOrEmpty() && isRegularBrowser() && (method == HttpMethod.POST || method == HttpMethod.PATCH || method == HttpMethod.PUT || method == HttpMethod.DELETE)
}
\ No newline at end of file