Skip to content

Commit

Permalink
100 continue triggered by content request - nima (helidon-io#5714)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Jan 31, 2023
1 parent cab9e0c commit 5989c97
Show file tree
Hide file tree
Showing 15 changed files with 839 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ public String sendAndReceive(String path,
*/
public String receive() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
BufferedReader br = socketReader;
StringBuilder sb = new StringBuilder();
String t;
boolean ending = false;
int contentLength = -1;
while ((t = br.readLine()) != null) {

LOGGER.log(System.Logger.Level.TRACE, "Received: " + t);
LOGGER.log(System.Logger.Level.TRACE, "< " + t);

if (t.toLowerCase().startsWith("content-length")) {
int k = t.indexOf(':');
Expand Down Expand Up @@ -353,6 +353,7 @@ public SocketHttpClient awaitResponse(String expectedStartsWith, String expected
} catch (IOException e) {
throw new RuntimeException(e);
}
LOGGER.log(System.Logger.Level.TRACE, "< " + t);
sb.append(t).append("\n");
if (sb.toString().startsWith(expectedStartsWith) && sb.toString().endsWith(expectedEndsWith)) {
break;
Expand Down Expand Up @@ -519,6 +520,12 @@ public SocketHttpClient manualRequest(String formatString, Object... args) throw
connect();
}
PrintWriter pw = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(), StandardCharsets.UTF_8));

LOGGER.log(System.Logger.Level.TRACE, () -> {
String msg = "\n> " + formatString.replaceAll("\\n", EOL + "> ");
return String.format(msg.substring(0, msg.length() - "> ".length()), args);
});

pw.printf(formatString.replaceAll("\\n", EOL), args);
pw.flush();
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,6 +21,7 @@
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;
import java.util.function.Function;

import io.helidon.common.GenericType;
Expand All @@ -38,6 +39,8 @@ public abstract class ReadableEntityBase implements ReadableEntity {
private final Runnable entityProcessedRunnable;

private final AtomicBoolean entityProcessed = new AtomicBoolean();
private final AtomicBoolean entityRequested = new AtomicBoolean();
private final Consumer<Boolean> entityRequestedCallback;
private InputStream inputStream;

/**
Expand All @@ -52,6 +55,17 @@ protected ReadableEntityBase(Function<InputStream, InputStream> decoder,
Function<Integer, BufferData> readEntityFunction,
Runnable entityProcessedRunnable) {
this.decoder = decoder;
this.entityRequestedCallback = d -> {};
this.readEntityFunction = readEntityFunction;
this.entityProcessedRunnable = new EntityProcessedRunnable(entityProcessedRunnable, entityProcessed);
}

protected ReadableEntityBase(Consumer<Boolean> entityRequestedCallback,
Function<InputStream, InputStream> decoder,
Function<Integer, BufferData> readEntityFunction,
Runnable entityProcessedRunnable) {
this.entityRequestedCallback = entityRequestedCallback;
this.decoder = decoder;
this.readEntityFunction = readEntityFunction;
this.entityProcessedRunnable = new EntityProcessedRunnable(entityProcessedRunnable, entityProcessed);
}
Expand All @@ -68,6 +82,10 @@ public static ReadableEntity empty() {
@Override
public InputStream inputStream() {
if (consumed.compareAndSet(false, true)) {
if (!entityRequested.getAndSet(true)) {
entityRequestedCallback.accept(false);
}

this.inputStream = decoder.apply(new RequestingInputStream(readEntityFunction, entityProcessedRunnable));
return inputStream;
} else {
Expand Down Expand Up @@ -108,6 +126,9 @@ public void consume() {
if (consumed()) {
return;
}
if (!entityRequested.getAndSet(true)) {
entityRequestedCallback.accept(true);
}
InputStream theStream;
if (inputStream == null) {
theStream = inputStream();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -89,6 +89,10 @@ static Http2ServerRequest create(ConnectionContext ctx,
return new Http2ServerRequest(ctx, security, httpPrologue, headers, decoder, streamId, entitySupplier);
}

@Override
public void reset() {
}

@Override
public boolean isSecure() {
return ctx.isSecure();
Expand Down
Loading

0 comments on commit 5989c97

Please sign in to comment.