From 9ca9bac0a099b4de2dabf5422690bd080954ba9c Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 8 Sep 2016 16:02:58 +0200 Subject: [PATCH] convert HttpRequestHandler, ByteBufUtf8Writer to kotlin --- .../io/netty/buffer/ByteBufUtf8Writer.java | 66 +++++++----------- .../org/jetbrains/ide/HttpRequestHandler.java | 67 ++++++++++--------- 2 files changed, 60 insertions(+), 73 deletions(-) diff --git a/platform/platform-impl/src/io/netty/buffer/ByteBufUtf8Writer.java b/platform/platform-impl/src/io/netty/buffer/ByteBufUtf8Writer.java index a6eaab4ad5eb..d8baec212e8b 100644 --- a/platform/platform-impl/src/io/netty/buffer/ByteBufUtf8Writer.java +++ b/platform/platform-impl/src/io/netty/buffer/ByteBufUtf8Writer.java @@ -13,68 +13,52 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.netty.buffer; +package io.netty.buffer -import com.intellij.util.text.CharArrayCharSequence; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.io.NettyKt; +import com.intellij.util.io.writeUtf8 +import com.intellij.util.text.CharArrayCharSequence +import java.io.InputStream +import java.io.Writer -import java.io.IOException; -import java.io.InputStream; -import java.io.Writer; - -public final class ByteBufUtf8Writer extends Writer { - private final ByteBuf buffer; - - public ByteBufUtf8Writer(@NotNull ByteBuf buffer) { - this.buffer = buffer; +class ByteBufUtf8Writer(private val buffer: ByteBuf) : Writer() { + fun write(inputStream: InputStream, length: Int) { + buffer.writeBytes(inputStream, length) } - public void write(@NotNull InputStream inputStream, int length) throws IOException { - buffer.writeBytes(inputStream, length); + fun ensureWritable(minWritableBytes: Int) { + buffer.ensureWritable(minWritableBytes) } - public void ensureWritable(int minWritableBytes) { - buffer.ensureWritable(minWritableBytes); + override fun write(chars: CharArray, off: Int, len: Int) { + buffer.writeUtf8(CharArrayCharSequence(chars, off, off + len)) } - @Override - public void write(char[] chars, int off, int len) { - NettyKt.writeUtf8(buffer, new CharArrayCharSequence(chars, off, off + len)); + override fun write(str: String) { + buffer.writeUtf8(str) } - @Override - public void write(String str) { - NettyKt.writeUtf8(buffer, str); + override fun write(str: String, off: Int, len: Int) { + ByteBufUtilEx.writeUtf8(buffer, str, off, off + len) } - @Override - public void write(String str, int off, int len) { - ByteBufUtilEx.writeUtf8(buffer, str, off, off + len); - } - - @Override - public Writer append(CharSequence csq) { + override fun append(csq: CharSequence?): Writer { if (csq == null) { - ByteBufUtil.writeAscii(buffer, "null"); + ByteBufUtil.writeAscii(buffer, "null") } else { - NettyKt.writeUtf8(buffer, csq); + buffer.writeUtf8(csq) } - return this; + return this } - @Override - public Writer append(CharSequence csq, int start, int end) { - ByteBufUtilEx.writeUtf8(buffer, csq, start, end); - return this; + override fun append(csq: CharSequence?, start: Int, end: Int): Writer { + ByteBufUtilEx.writeUtf8(buffer, csq, start, end) + return this } - @Override - public void flush() { + override fun flush() { } - @Override - public void close() { + override fun close() { } } diff --git a/platform/platform-impl/src/org/jetbrains/ide/HttpRequestHandler.java b/platform/platform-impl/src/org/jetbrains/ide/HttpRequestHandler.java index 075898fd0f47..8312293c5a65 100644 --- a/platform/platform-impl/src/org/jetbrains/ide/HttpRequestHandler.java +++ b/platform/platform-impl/src/org/jetbrains/ide/HttpRequestHandler.java @@ -13,54 +13,57 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.jetbrains.ide; +package org.jetbrains.ide -import com.intellij.openapi.extensions.ExtensionPointName; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.http.FullHttpRequest; -import io.netty.handler.codec.http.HttpMethod; -import io.netty.handler.codec.http.HttpRequest; -import io.netty.handler.codec.http.QueryStringDecoder; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.io.NettyKt; +import com.intellij.openapi.extensions.ExtensionPointName +import com.intellij.util.io.host +import com.intellij.util.io.isLocalOrigin +import com.intellij.util.io.parseAndCheckIsLocalHost +import io.netty.channel.ChannelHandlerContext +import io.netty.handler.codec.http.FullHttpRequest +import io.netty.handler.codec.http.HttpMethod +import io.netty.handler.codec.http.HttpRequest +import io.netty.handler.codec.http.QueryStringDecoder +import java.io.IOException -import java.io.IOException; +abstract class HttpRequestHandler { + companion object { + // Your handler will be instantiated on first user request + val EP_NAME = ExtensionPointName.create("com.intellij.httpRequestHandler")!! -public abstract class HttpRequestHandler { - // Your handler will be instantiated on first user request - public static final ExtensionPointName EP_NAME = ExtensionPointName.create("com.intellij.httpRequestHandler"); - - protected static boolean checkPrefix(@NotNull String uri, @NotNull String prefix) { - if (uri.length() > prefix.length() && uri.charAt(0) == '/' && uri.regionMatches(true, 1, prefix, 0, prefix.length())) { - if ((uri.length() - prefix.length()) == 1) { - return true; - } - else { - char c = uri.charAt(prefix.length() + 1); - return c == '/' || c == '?'; + @JvmStatic + protected fun checkPrefix(uri: String, prefix: String): Boolean { + if (uri.length > prefix.length && uri[0] == '/' && uri.regionMatches(1, prefix, 0, prefix.length, ignoreCase = true)) { + if (uri.length - prefix.length == 1) { + return true + } + else { + val c = uri.get(prefix.length + 1) + return c == '/' || c == '?' + } } + return false } - return false; } /** - * Write request from browser without Origin will be always blocked regardles of your implementation. + * Write request from browser without Origin will be always blocked regardless of your implementation. */ @SuppressWarnings("SpellCheckingInspection") - public boolean isAccessible(@NotNull HttpRequest request) { - String host = NettyKt.getHost(request); - // If attacker.com DNS rebound to 127.0.0.1 and user open site directly — no Origin or Referer headers. + open fun isAccessible(request: HttpRequest): Boolean { + val host = request.host + // If attacker.com DNS rebound to 127.0.0.1 and user open site directly — no Origin or Referrer headers. // So we should check Host header. - return host != null && NettyKt.isLocalOrigin(request) && NettyKt.parseAndCheckIsLocalHost("http://" + host); + return host != null && request.isLocalOrigin() && parseAndCheckIsLocalHost("http://$host") } - public boolean isSupported(@NotNull FullHttpRequest request) { - return request.method() == HttpMethod.GET || request.method() == HttpMethod.HEAD; + open fun isSupported(request: FullHttpRequest): Boolean { + return request.method() === HttpMethod.GET || request.method() === HttpMethod.HEAD } /** * @return true if processed successfully, false to pass processing to other handlers. */ - public abstract boolean process(@NotNull QueryStringDecoder urlDecoder, @NotNull FullHttpRequest request, @NotNull ChannelHandlerContext context) - throws IOException; + @Throws(IOException::class) + abstract fun process(urlDecoder: QueryStringDecoder, request: FullHttpRequest, context: ChannelHandlerContext): Boolean } \ No newline at end of file