initial ? wtf
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>dev.relism</groupId>
|
||||
<artifactId>fastpathrouter-parent</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>fpr-netty</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-bom</artifactId>
|
||||
<version>${netty.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>dev.relism</groupId>
|
||||
<artifactId>fpr-core</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-buffer</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.netty</groupId>
|
||||
<artifactId>netty-codec-http</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
package dev.relism.fpr.netty;
|
||||
|
||||
import dev.relism.fpr.core.ByteView;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public final class NettyByteBufView implements ByteView {
|
||||
private final ByteBuf buffer;
|
||||
private final int offset;
|
||||
private final int length;
|
||||
|
||||
public NettyByteBufView(ByteBuf buffer, int offset, int length) {
|
||||
if (buffer == null) {
|
||||
throw new IllegalArgumentException("buffer must not be null");
|
||||
}
|
||||
if (offset < 0 || length < 0 || offset + length > buffer.capacity()) {
|
||||
throw new IllegalArgumentException("invalid offset/length");
|
||||
}
|
||||
this.buffer = buffer;
|
||||
this.offset = offset;
|
||||
this.length = length;
|
||||
}
|
||||
|
||||
public ByteBuf buffer() {
|
||||
return buffer;
|
||||
}
|
||||
|
||||
public int offset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int length() {
|
||||
return length;
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte byteAt(int index) {
|
||||
if (index < 0 || index >= length) {
|
||||
throw new IndexOutOfBoundsException("index: " + index);
|
||||
}
|
||||
return buffer.getByte(offset + index);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsLong() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long longAt(int index) {
|
||||
if (index < 0 || index + 8 > length) {
|
||||
throw new IndexOutOfBoundsException("index: " + index);
|
||||
}
|
||||
return buffer.getLongLE(offset + index);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package dev.relism.fpr.netty;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
|
||||
public final class NettyPathExtractor {
|
||||
private NettyPathExtractor() {
|
||||
}
|
||||
|
||||
public static PathSpan extractFromRequestLine(ByteBuf buffer, int offset, int length, PathSpan out) {
|
||||
if (out == null) {
|
||||
throw new IllegalArgumentException("out must not be null");
|
||||
}
|
||||
int end = offset + length;
|
||||
int i = offset;
|
||||
while (i < end && buffer.getByte(i) != ' ') {
|
||||
i++;
|
||||
}
|
||||
if (i >= end) {
|
||||
throw new IllegalArgumentException("invalid request line");
|
||||
}
|
||||
i++;
|
||||
int pathStart = i;
|
||||
while (i < end && buffer.getByte(i) != ' ') {
|
||||
i++;
|
||||
}
|
||||
int pathLen = i - pathStart;
|
||||
out.set(pathStart, pathLen);
|
||||
return out;
|
||||
}
|
||||
|
||||
public static PathSpan extractFromUri(CharSequence uri, PathSpan out) {
|
||||
if (out == null) {
|
||||
throw new IllegalArgumentException("out must not be null");
|
||||
}
|
||||
int len = uri.length();
|
||||
int end = len;
|
||||
for (int i = 0; i < len; i++) {
|
||||
if (uri.charAt(i) == '?') {
|
||||
end = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
out.set(0, end);
|
||||
return out;
|
||||
}
|
||||
|
||||
public static final class PathSpan {
|
||||
private int start;
|
||||
private int len;
|
||||
|
||||
public int start() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public int len() {
|
||||
return len;
|
||||
}
|
||||
|
||||
public void set(int start, int len) {
|
||||
this.start = start;
|
||||
this.len = len;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package dev.relism.fpr.netty;
|
||||
|
||||
import dev.relism.fpr.core.ByteView;
|
||||
import dev.relism.fpr.core.FastPathRouter;
|
||||
import dev.relism.fpr.core.MatchResult;
|
||||
import dev.relism.fpr.core.RouterBuilder;
|
||||
import dev.relism.fpr.core.dsl.StringRouteParser;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class NettyAdaptersTest {
|
||||
@Test
|
||||
void byteBufViewRespectsOffsetAndLength() {
|
||||
ByteBuf buf = Unpooled.wrappedBuffer("abcdef".getBytes(StandardCharsets.US_ASCII));
|
||||
NettyByteBufView view = new NettyByteBufView(buf, 1, 3);
|
||||
|
||||
assertThat(view.length()).isEqualTo(3);
|
||||
assertThat((char) view.byteAt(0)).isEqualTo('b');
|
||||
assertThat((char) view.byteAt(2)).isEqualTo('d');
|
||||
}
|
||||
|
||||
@Test
|
||||
void extractPathFromRequestLine() {
|
||||
ByteBuf buf = Unpooled.wrappedBuffer("GET /alpha/beta?q=1 HTTP/1.1".getBytes(StandardCharsets.US_ASCII));
|
||||
NettyPathExtractor.PathSpan out = new NettyPathExtractor.PathSpan();
|
||||
|
||||
NettyPathExtractor.extractFromRequestLine(buf, 0, buf.readableBytes(), out);
|
||||
|
||||
String path = buf.toString(out.start(), out.len(), StandardCharsets.US_ASCII);
|
||||
assertThat(path).isEqualTo("/alpha/beta?q=1");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nettyIntegrationMatch() {
|
||||
RouterBuilder<String> builder = new RouterBuilder<>();
|
||||
builder.add(StringRouteParser.parse("/user/{id}"), "USER");
|
||||
FastPathRouter<ByteView, String> router = builder.compile();
|
||||
|
||||
ByteBuf buf = Unpooled.wrappedBuffer("/user/42".getBytes(StandardCharsets.US_ASCII));
|
||||
NettyByteBufView view = new NettyByteBufView(buf, 0, buf.readableBytes());
|
||||
|
||||
MatchResult<String> out = new MatchResult<>(builder.maxParamCount());
|
||||
assertThat(router.match(view, out)).isNotEqualTo(FastPathRouter.NO_MATCH);
|
||||
assertThat(out.handler()).isEqualTo("USER");
|
||||
assertThat(out.paramCount()).isEqualTo(1);
|
||||
assertThat(span(view, out, 0)).isEqualTo("42");
|
||||
}
|
||||
|
||||
private static String span(ByteView view, MatchResult<String> out, int index) {
|
||||
int start = out.startAt(index);
|
||||
int len = out.lenAt(index);
|
||||
byte[] bytes = new byte[len];
|
||||
for (int i = 0; i < len; i++) {
|
||||
bytes[i] = view.byteAt(start + i);
|
||||
}
|
||||
return new String(bytes, StandardCharsets.US_ASCII);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user