fix for uri trouble

This commit is contained in:
Jörg Prante 2019-05-08 15:45:24 +02:00
parent ef0c59d3f5
commit 7f14fce6fd
5 changed files with 62 additions and 15 deletions

View file

@ -2,7 +2,7 @@ import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter import java.time.format.DateTimeFormatter
plugins { plugins {
id "com.github.spotbugs" version "1.6.9" id "com.github.spotbugs" version "1.7.1"
id "org.sonarqube" version "2.6.1" id "org.sonarqube" version "2.6.1"
id "io.codearte.nexus-staging" version "0.11.0" id "io.codearte.nexus-staging" version "0.11.0"
id "org.xbib.gradle.plugin.asciidoctor" version "1.5.6.0.1" id "org.xbib.gradle.plugin.asciidoctor" version "1.5.6.0.1"
@ -148,7 +148,7 @@ subprojects {
scmDeveloperConnection = 'scm:git:git://github.com/' + user + '/' + name + '.git' scmDeveloperConnection = 'scm:git:git://github.com/' + user + '/' + name + '.git'
} }
task xbibUpload(type: Upload) { /*task xbibUpload(type: Upload) {
group = 'publish' group = 'publish'
configuration = configurations.archives configuration = configurations.archives
uploadDescriptor = true uploadDescriptor = true
@ -162,7 +162,7 @@ subprojects {
} }
} }
} }
} }*/
task sonaTypeUpload(type: Upload) { task sonaTypeUpload(type: Upload) {
group = 'publish' group = 'publish'

View file

@ -1,6 +1,6 @@
group = org.xbib group = org.xbib
name = netty-http name = netty-http
version = 4.1.35.1 version = 4.1.35.2
# main packages # main packages
netty.version = 4.1.35.Final netty.version = 4.1.35.Final

View file

@ -285,6 +285,7 @@ public class RequestBuilder {
if (url.getHost() == null) { if (url.getHost() == null) {
throw new IllegalStateException("host in URL not defined: " + url); throw new IllegalStateException("host in URL not defined: " + url);
} }
// add path from uri()
if (uri != null) { if (uri != null) {
try { try {
url = URL.base(url).resolve(uri); url = URL.base(url).resolve(uri);
@ -292,9 +293,7 @@ public class RequestBuilder {
throw new IllegalArgumentException(e); throw new IllegalArgumentException(e);
} }
} }
// add explicit parameters to URL // let Netty's query string decoder/encoder work over the URL to add parameters given implicitly in url()
queryParameters.forEach(param -> url.getQueryParams().add(param));
// let Netty's query string decoder/encoder work over the URL to add paramters given implicitly in url()
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(URI.create(url.toString()), StandardCharsets.UTF_8); QueryStringDecoder queryStringDecoder = new QueryStringDecoder(URI.create(url.toString()), StandardCharsets.UTF_8);
QueryStringEncoder queryStringEncoder = new QueryStringEncoder(queryStringDecoder.path()); QueryStringEncoder queryStringEncoder = new QueryStringEncoder(queryStringDecoder.path());
for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) { for (Map.Entry<String, List<String>> entry : queryStringDecoder.parameters().entrySet()) {
@ -302,15 +301,25 @@ public class RequestBuilder {
queryStringEncoder.addParam(entry.getKey(), value); queryStringEncoder.addParam(entry.getKey(), value);
} }
} }
// attach user query parameters
queryParameters.forEach(param -> queryStringEncoder.addParam(param.getFirst(), param.getSecond()));
// build uri from QueryStringDecoder // build uri from QueryStringDecoder
StringBuilder sb = new StringBuilder();
String pathAndQuery = queryStringEncoder.toString(); String pathAndQuery = queryStringEncoder.toString();
StringBuilder sb = new StringBuilder();
sb.append(pathAndQuery.isEmpty() ? "/" : pathAndQuery); sb.append(pathAndQuery.isEmpty() ? "/" : pathAndQuery);
String ref = url.getFragment(); String ref = url.getFragment();
if (ref != null && !ref.isEmpty()) { if (ref != null && !ref.isEmpty()) {
sb.append('#').append(ref); sb.append('#').append(ref);
} }
String uri = sb.toString(); String uri = sb.toString();
// resolve again
if (!uri.equals("/")) {
try {
url = uri.startsWith("/") ? URL.base(url).resolve(uri) : URL.base(url).resolve("/" + uri) ;
} catch (URLSyntaxException | MalformedInputException | UnmappableCharacterException e) {
throw new IllegalArgumentException(e);
}
}
DefaultHttpHeaders validatedHeaders = new DefaultHttpHeaders(true); DefaultHttpHeaders validatedHeaders = new DefaultHttpHeaders(true);
validatedHeaders.set(headers); validatedHeaders.set(headers);
String scheme = url.getScheme(); String scheme = url.getScheme();

View file

@ -4,16 +4,54 @@ import io.netty.handler.codec.http.HttpMethod;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.xbib.netty.http.client.Request; import org.xbib.netty.http.client.Request;
import java.util.logging.Level; import java.nio.charset.StandardCharsets;
import java.util.logging.Logger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class RequestBuilderTest { class RequestBuilderTest {
private static final Logger logger = Logger.getLogger(RequestBuilderTest.class.getName());
@Test @Test
void testSimpleRequest() { void testSimpleRequest() {
Request request = Request.builder(HttpMethod.GET).content("Hello", "text/plain").build(); Request request = Request.builder(HttpMethod.GET)
logger.log(Level.INFO, request.toString()); .content("Hello", "text/plain")
.build();
assertEquals("localhost", request.url().getHost());
assertNull(request.url().getPort());
assertEquals("Hello", request.content().toString(StandardCharsets.UTF_8));
}
@Test
void testGetRequest() {
Request request = Request.builder(HttpMethod.GET)
.url("http://xbib.org")
.addParameter("param1", "value1")
.addParameter("param2", "value2")
.build();
assertEquals("?param1=value1&param2=value2", request.relativeUri());
assertEquals("http://xbib.org/?param1=value1&param2=value2", request.url().toString());
}
@Test
void testPostRequest() {
Request request = Request.builder(HttpMethod.POST)
.url("http://xbib.org")
.addParameter("param1", "value1")
.addParameter("param2", "value2")
.content("Hello", "text/plain")
.build();
assertEquals("?param1=value1&param2=value2", request.relativeUri());
assertEquals("http://xbib.org/?param1=value1&param2=value2", request.url().toString());
assertEquals("Hello", request.content().toString(StandardCharsets.UTF_8));
}
@Test
void testRequest() {
Request request = Request.get()
.url("https://google.com")
.setVersion("HTTP/1.1")
.build();
assertEquals("google.com", request.url().getHost());
} }
} }

View file

@ -21,7 +21,7 @@ class URITest {
} }
@Test @Test
void testRequestURIs() { void testRelativeUri() {
RequestBuilder httpRequestBuilder = Request.get(); RequestBuilder httpRequestBuilder = Request.get();
httpRequestBuilder.url("https://localhost").uri("/path"); httpRequestBuilder.url("https://localhost").uri("/path");
assertEquals("/path", httpRequestBuilder.build().relativeUri()); assertEquals("/path", httpRequestBuilder.build().relativeUri());