use copy URLs because they can mutate (very ugly thing)

This commit is contained in:
Jörg Prante 2024-01-03 14:29:07 +01:00
parent fa3fa2e958
commit 5b46911bd8
6 changed files with 34 additions and 7 deletions

View file

@ -1,3 +1,3 @@
group = org.xbib group = org.xbib
name = net-http name = net-http
version = 4.0.9 version = 4.0.10

View file

@ -69,6 +69,7 @@ public class HttpRequest extends BaseHttpRequest {
.setVersion(httpRequest.getVersion()) .setVersion(httpRequest.getVersion())
.setURL(httpRequest.getURL()) .setURL(httpRequest.getURL())
.setHeaders(httpRequest.getHeaders()) .setHeaders(httpRequest.getHeaders())
.setParameterBuilder(httpRequest.getParameterBuilder())
.content(httpRequest.getBody()) .content(httpRequest.getBody())
.setResponseListener(httpRequest.getResponseListener()) .setResponseListener(httpRequest.getResponseListener())
.setTimeoutListener(httpRequest.getTimeoutListener(), httpRequest.getTimeoutMillis()) .setTimeoutListener(httpRequest.getTimeoutListener(), httpRequest.getTimeoutMillis())

View file

@ -0,0 +1,28 @@
package org.xbib.net.http.client.netty;
import org.junit.jupiter.api.Test;
import org.xbib.net.URL;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ParameterTest {
@Test
public void testGetParametersWithSharedUrl() {
URL url = URL.from("http://example.com");
Map<String, Object> map1 = Map.of("a", "b");
HttpRequest request1 = HttpRequest.get()
.setURL(url)
.setParameters(map1)
.build();
assertEquals("http://example.com?a=b", request1.getURL().toString());
Map<String, Object> map2 = Map.of("c", "d");
HttpRequest request2 = HttpRequest.get()
.setURL(url)
.setParameters(map2)
.build();
assertEquals("http://example.com?c=d", request2.getURL().toString());
}
}

View file

@ -33,8 +33,8 @@ public abstract class BaseHttpRequest implements HttpRequest {
// validate request // validate request
HttpHeaders validatedHeaders = HttpHeaders.of(builder.httpHeaders); HttpHeaders validatedHeaders = HttpHeaders.of(builder.httpHeaders);
if (builder.url != null) { if (builder.url != null) {
// add our URI parameters to the URL // create new URL object so we can safely add our URI parameters to the URL by mutating it
URLBuilder urlBuilder = builder.url.mutator(); URLBuilder urlBuilder = URL.from(builder.url).mutator();
if (builder.requestPath != null) { if (builder.requestPath != null) {
urlBuilder.path(builder.requestPath); urlBuilder.path(builder.requestPath);
} }

View file

@ -27,8 +27,6 @@ import org.xbib.net.http.cookie.Cookie;
public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder { public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder {
private static final URL DEFAULT_URL = URL.from("http://localhost");
private static final String DEFAULT_FORM_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=utf-8"; private static final String DEFAULT_FORM_CONTENT_TYPE = "application/x-www-form-urlencoded; charset=utf-8";
/** /**
@ -98,7 +96,7 @@ public abstract class BaseHttpRequestBuilder implements HttpRequestBuilder {
protected BaseHttpRequestBuilder() { protected BaseHttpRequestBuilder() {
this.httpMethod = HttpMethod.GET; this.httpMethod = HttpMethod.GET;
this.httpVersion = HttpVersion.HTTP_1_1; this.httpVersion = HttpVersion.HTTP_1_1;
this.url = DEFAULT_URL; this.url = URL.from("http://localhost"); // will mutate for get parameters, create new object
this.contentType = DEFAULT_FORM_CONTENT_TYPE; this.contentType = DEFAULT_FORM_CONTENT_TYPE;
this.userAgent = getUserAgent(); this.userAgent = getUserAgent();
this.isGzipEnabled = false; this.isGzipEnabled = false;

View file

@ -6,7 +6,7 @@ dependencyResolutionManagement {
version('netty', '4.1.104.Final') version('netty', '4.1.104.Final')
version('netty-tcnative', '2.0.62.Final') version('netty-tcnative', '2.0.62.Final')
version('datastructures', '5.0.6') version('datastructures', '5.0.6')
version('net', '4.0.3') version('net', '4.0.4')
library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty') library('netty-codec-http2', 'io.netty', 'netty-codec-http2').versionRef('netty')
library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty') library('netty-handler', 'io.netty', 'netty-handler').versionRef('netty')
library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty') library('netty-handler-proxy', 'io.netty', 'netty-handler-proxy').versionRef('netty')