-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
633be8f
commit a632b07
Showing
15 changed files
with
1,136 additions
and
47 deletions.
There are no files selected for viewing
11 changes: 0 additions & 11 deletions
11
downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestFilter.java
This file was deleted.
Oops, something went wrong.
70 changes: 70 additions & 0 deletions
70
downgradetest/src/main/java/xyz/wagyourtail/downgradetest/TestHttpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package xyz.wagyourtail.downgradetest; | ||
|
||
import com.sun.net.httpserver.Filter; | ||
import com.sun.net.httpserver.HttpServer; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.net.InetSocketAddress; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.util.Objects; | ||
import java.util.concurrent.ForkJoinPool; | ||
|
||
public class TestHttpClient { | ||
|
||
public static int servermain() throws IOException, InterruptedException { | ||
HttpServer server = HttpServer.create(); | ||
server.bind(new InetSocketAddress(0), 0); | ||
server.setExecutor(ForkJoinPool.commonPool()); | ||
server.createContext("/", (e) -> { | ||
e.sendResponseHeaders(200, 0); | ||
// read body | ||
if (!Objects.equals(e.getRequestMethod(), "GET")) { | ||
byte[] b = e.getRequestBody().readAllBytes(); | ||
System.out.println(new String(b)); | ||
OutputStream os = e.getResponseBody(); | ||
os.write("response: ".getBytes()); | ||
os.write(b); | ||
} else { | ||
e.getResponseBody().write("response".getBytes()); | ||
} | ||
e.close(); | ||
}); | ||
Thread t = new Thread(server::start); | ||
t.setDaemon(true); | ||
t.start(); | ||
Thread.sleep(250); | ||
return server.getAddress().getPort(); | ||
} | ||
|
||
public static void main(String[] args) throws IOException, InterruptedException { | ||
int port = servermain(); | ||
System.out.println(Filter.afterHandler("test", (e) -> System.out.println("test")).description()); | ||
|
||
try (HttpClient client = HttpClient.newHttpClient()) { | ||
HttpRequest request = HttpRequest.newBuilder() | ||
.header("User-Agent", "JVMDG Test 1.0") | ||
.uri(URI.create("http://localhost:" + port)) | ||
.build(); | ||
|
||
HttpResponse<String> resp = client.send(request, HttpResponse.BodyHandlers.ofString()); | ||
System.out.println(resp.body()); | ||
|
||
HttpRequest request2 = HttpRequest.newBuilder() | ||
.header("User-Agent", "JVMDG Test 1.0") | ||
.uri(URI.create("http://localhost:" + port)) | ||
.header("Content-Type", "application/x-www-form-urlencoded") | ||
.POST(HttpRequest.BodyPublishers.ofString("test body")) | ||
.build(); | ||
|
||
HttpResponse<String> resp2 = client.send(request2, HttpResponse.BodyHandlers.ofString()); | ||
System.out.println(resp2.body()); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 117 additions & 0 deletions
117
java-api/src/java11/java/xyz/wagyourtail/jvmdg/j11/impl/http/HttpRequestBuilderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package xyz.wagyourtail.jvmdg.j11.impl.http; | ||
|
||
import xyz.wagyourtail.jvmdg.j11.stub.java_net_http.J_N_H_HttpClient; | ||
import xyz.wagyourtail.jvmdg.j11.stub.java_net_http.J_N_H_HttpRequest; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class HttpRequestBuilderImpl implements J_N_H_HttpRequest.Builder { | ||
URI uri; | ||
boolean expectContinue = false; | ||
J_N_H_HttpClient.Version version; | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
Duration timeout; | ||
String method = "GET"; | ||
J_N_H_HttpRequest.BodyPublisher publisher; | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder uri(URI uri) { | ||
this.uri = uri; | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder expectContinue(boolean expectContinue) { | ||
this.expectContinue = expectContinue; | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder version(J_N_H_HttpClient.Version version) { | ||
this.version = version; | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder header(String name, String value) { | ||
if (!headers.containsKey(name)) { | ||
headers.put(name, new ArrayList<>(List.of(value))); | ||
} else { | ||
headers.get(name).add(value); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder headers(String... headers) { | ||
for (int i = 0; i < headers.length; i += 2) { | ||
header(headers[i], headers[i + 1]); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder timeout(Duration duration) { | ||
this.timeout = duration; | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder setHeader(String name, String value) { | ||
headers.put(name, new ArrayList<>(List.of(value))); | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder GET() { | ||
return method("GET", null); | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder POST(J_N_H_HttpRequest.BodyPublisher publisher) { | ||
return method("POST", publisher); | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder PUT(J_N_H_HttpRequest.BodyPublisher publisher) { | ||
return method("PUT", publisher); | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder DELETE() { | ||
return method("DELETE", null); | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder method(String name, J_N_H_HttpRequest.BodyPublisher publisher) { | ||
this.method = name; | ||
this.publisher = publisher; | ||
return this; | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest build() { | ||
return new HttpRequestImpl(this); | ||
} | ||
|
||
@Override | ||
public J_N_H_HttpRequest.Builder copy() { | ||
HttpRequestBuilderImpl copy = new HttpRequestBuilderImpl(); | ||
copy.uri = uri; | ||
copy.expectContinue = expectContinue; | ||
copy.version = version; | ||
// deep copy headers | ||
for (Map.Entry<String, List<String>> entry : headers.entrySet()) { | ||
copy.headers.put(entry.getKey(), new ArrayList<>(entry.getValue())); | ||
} | ||
copy.timeout = timeout; | ||
copy.method = method; | ||
copy.publisher = publisher; | ||
return copy; | ||
} | ||
} |
Oops, something went wrong.