Skip to content
Permalink
Browse files
8257401: Use switch expressions in jdk.internal.net.http and java.net…
….http

Reviewed-by: chegar, dfuchs, pconcannon
  • Loading branch information
amCap1712 committed Dec 4, 2020
1 parent 86b6575 commit ac54900849304761b8e8a49ed312103eeef92b9c
Showing with 147 additions and 269 deletions.
  1. +16 −43 src/java.net.http/share/classes/jdk/internal/net/http/Http1HeaderParser.java
  2. +6 −5 src/java.net.http/share/classes/jdk/internal/net/http/Http1Response.java
  3. +6 −14 src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java
  4. +4 −7 src/java.net.http/share/classes/jdk/internal/net/http/MultiExchange.java
  5. +18 −36 src/java.net.http/share/classes/jdk/internal/net/http/RedirectFilter.java
  6. +5 −12 src/java.net.http/share/classes/jdk/internal/net/http/Stream.java
  7. +4 −8 src/java.net.http/share/classes/jdk/internal/net/http/common/SSLFlowDelegate.java
  8. +6 −7 src/java.net.http/share/classes/jdk/internal/net/http/frame/DataFrame.java
  9. +14 −24 src/java.net.http/share/classes/jdk/internal/net/http/frame/FramesEncoder.java
  10. +6 −7 src/java.net.http/share/classes/jdk/internal/net/http/frame/HeaderFrame.java
  11. +7 −9 src/java.net.http/share/classes/jdk/internal/net/http/frame/HeadersFrame.java
  12. +14 −24 src/java.net.http/share/classes/jdk/internal/net/http/frame/Http2Frame.java
  13. +4 −5 src/java.net.http/share/classes/jdk/internal/net/http/frame/PingFrame.java
  14. +6 −7 src/java.net.http/share/classes/jdk/internal/net/http/frame/PushPromiseFrame.java
  15. +14 −20 src/java.net.http/share/classes/jdk/internal/net/http/frame/SettingsFrame.java
  16. +8 −20 src/java.net.http/share/classes/jdk/internal/net/http/hpack/Decoder.java
  17. +9 −21 src/java.net.http/share/classes/jdk/internal/net/http/websocket/StatusCodes.java
@@ -116,42 +116,17 @@ boolean parse(ByteBuffer input) throws ProtocolException {

while (canContinueParsing(input)) {
switch (state) {
case INITIAL:
state = State.STATUS_LINE;
break;
case STATUS_LINE:
readResumeStatusLine(input);
break;
// fallthrough
case STATUS_LINE_FOUND_CR:
case STATUS_LINE_FOUND_LF:
readStatusLineFeed(input);
break;
case STATUS_LINE_END:
maybeStartHeaders(input);
break;
// fallthrough
case STATUS_LINE_END_CR:
case STATUS_LINE_END_LF:
maybeEndHeaders(input);
break;
case HEADER:
readResumeHeader(input);
break;
// fallthrough
case HEADER_FOUND_CR:
case HEADER_FOUND_LF:
resumeOrLF(input);
break;
case HEADER_FOUND_CR_LF:
resumeOrSecondCR(input);
break;
case HEADER_FOUND_CR_LF_CR:
resumeOrEndHeaders(input);
break;
default:
throw new InternalError(
"Unexpected state: " + String.valueOf(state));
case INITIAL -> state = State.STATUS_LINE;
case STATUS_LINE -> readResumeStatusLine(input);
case STATUS_LINE_FOUND_CR, STATUS_LINE_FOUND_LF -> readStatusLineFeed(input);
case STATUS_LINE_END -> maybeStartHeaders(input);
case STATUS_LINE_END_CR, STATUS_LINE_END_LF -> maybeEndHeaders(input);
case HEADER -> readResumeHeader(input);
case HEADER_FOUND_CR, HEADER_FOUND_LF -> resumeOrLF(input);
case HEADER_FOUND_CR_LF -> resumeOrSecondCR(input);
case HEADER_FOUND_CR_LF_CR -> resumeOrEndHeaders(input);

default -> throw new InternalError("Unexpected state: " + state);
}
}

@@ -161,13 +136,11 @@ boolean parse(ByteBuffer input) throws ProtocolException {
private boolean canContinueParsing(ByteBuffer buffer) {
// some states don't require any input to transition
// to the next state.
switch (state) {
case FINISHED: return false;
case STATUS_LINE_FOUND_LF: return true;
case STATUS_LINE_END_LF: return true;
case HEADER_FOUND_LF: return true;
default: return buffer.hasRemaining();
}
return switch (state) {
case FINISHED -> false;
case STATUS_LINE_FOUND_LF, STATUS_LINE_END_LF, HEADER_FOUND_LF -> true;
default -> buffer.hasRemaining();
};
}

/**
@@ -576,11 +576,12 @@ private State advance(State previous) {
}

Receiver<?> receiver(State state) {
switch(state) {
case READING_HEADERS: return headersReader;
case READING_BODY: return bodyReader;
default: return null;
}
return switch (state) {
case READING_HEADERS -> headersReader;
case READING_BODY -> bodyReader;

default -> null;
};

}

@@ -866,20 +866,12 @@ private void handleConnectionFrame(Http2Frame frame)
throws IOException
{
switch (frame.type()) {
case SettingsFrame.TYPE:
handleSettings((SettingsFrame)frame);
break;
case PingFrame.TYPE:
handlePing((PingFrame)frame);
break;
case GoAwayFrame.TYPE:
handleGoAway((GoAwayFrame)frame);
break;
case WindowUpdateFrame.TYPE:
handleWindowUpdate((WindowUpdateFrame)frame);
break;
default:
protocolError(ErrorFrame.PROTOCOL_ERROR);
case SettingsFrame.TYPE -> handleSettings((SettingsFrame) frame);
case PingFrame.TYPE -> handlePing((PingFrame) frame);
case GoAwayFrame.TYPE -> handleGoAway((GoAwayFrame) frame);
case WindowUpdateFrame.TYPE -> handleWindowUpdate((WindowUpdateFrame) frame);

default -> protocolError(ErrorFrame.PROTOCOL_ERROR);
}
}

@@ -476,13 +476,10 @@ private static boolean disableRetryConnect() {
/** Returns true is given request has an idempotent method. */
private static boolean isIdempotentRequest(HttpRequest request) {
String method = request.method();
switch (method) {
case "GET" :
case "HEAD" :
return true;
default :
return false;
}
return switch (method) {
case "GET", "HEAD" -> true;
default -> false;
};
}

/** Returns true if the given request can be automatically retried. */
@@ -74,19 +74,13 @@ public synchronized HttpRequestImpl response(Response r) throws IOException {
}

private static String redirectedMethod(int statusCode, String orig) {
switch (statusCode) {
case 301:
case 302:
return orig.equals("POST") ? "GET" : orig;
case 303:
return "GET";
case 307:
case 308:
return orig;
default:
// unexpected but return orig
return orig;
}
return switch (statusCode) {
case 301, 302 -> orig.equals("POST") ? "GET" : orig;
case 303 -> "GET";
case 307, 308 -> orig;

default -> orig; // unexpected but return orig
};
}

private static boolean isRedirecting(int statusCode) {
@@ -95,23 +89,16 @@ private static boolean isRedirecting(int statusCode) {
// 309-399 Unassigned => don't follow
// > 399: not a redirect code
if (statusCode > 308) return false;
switch (statusCode) {

return switch (statusCode) {
// 300: MultipleChoice => don't follow
case 300:
return false;
// 304: Not Modified => don't follow
case 304:
return false;
// 305: Proxy Redirect => don't follow.
case 305:
return false;
// 306: Unused => don't follow
case 306:
return false;
case 300, 304, 305, 306 -> false;
// 301, 302, 303, 307, 308: OK to follow.
default:
return true;
}
default -> true;
};
}

/**
@@ -158,16 +145,11 @@ private URI getRedirectedURI(HttpHeaders headers) {
private boolean canRedirect(URI redir) {
String newScheme = redir.getScheme();
String oldScheme = uri.getScheme();
switch (policy) {
case ALWAYS:
return true;
case NEVER:
return false;
case NORMAL:
return newScheme.equalsIgnoreCase(oldScheme)
|| newScheme.equalsIgnoreCase("https");
default:
throw new InternalError();
}
return switch (policy) {
case ALWAYS -> true;
case NEVER -> false;
case NORMAL -> newScheme.equalsIgnoreCase(oldScheme)
|| newScheme.equalsIgnoreCase("https");
};
}
}
@@ -451,18 +451,11 @@ void incoming(Http2Frame frame) throws IOException {

void otherFrame(Http2Frame frame) throws IOException {
switch (frame.type()) {
case WindowUpdateFrame.TYPE:
incoming_windowUpdate((WindowUpdateFrame) frame);
break;
case ResetFrame.TYPE:
incoming_reset((ResetFrame) frame);
break;
case PriorityFrame.TYPE:
incoming_priority((PriorityFrame) frame);
break;
default:
String msg = "Unexpected frame: " + frame.toString();
throw new IOException(msg);
case WindowUpdateFrame.TYPE -> incoming_windowUpdate((WindowUpdateFrame) frame);
case ResetFrame.TYPE -> incoming_reset((ResetFrame) frame);
case PriorityFrame.TYPE -> incoming_priority((PriorityFrame) frame);

default -> throw new IOException("Unexpected frame: " + frame.toString());
}
}

@@ -1021,14 +1021,10 @@ private static String states(AtomicInteger state) {
StringBuilder sb = new StringBuilder();
int x = s & ~TASK_BITS;
switch (x) {
case NOT_HANDSHAKING:
sb.append(" NOT_HANDSHAKING ");
break;
case HANDSHAKING:
sb.append(" HANDSHAKING ");
break;
default:
throw new InternalError();
case NOT_HANDSHAKING -> sb.append(" NOT_HANDSHAKING ");
case HANDSHAKING -> sb.append(" HANDSHAKING ");

default -> throw new InternalError();
}
if ((s & DOING_TASKS) > 0)
sb.append("|DOING_TASKS");
@@ -71,13 +71,12 @@ int length() {

@Override
public String flagAsString(int flag) {
switch (flag) {
case END_STREAM:
return "END_STREAM";
case PADDED:
return "PADDED";
}
return super.flagAsString(flag);
return switch (flag) {
case END_STREAM -> "END_STREAM";
case PADDED -> "PADDED";

default -> super.flagAsString(flag);
};
}

public List<ByteBuffer> getData() {
@@ -59,30 +59,20 @@ public ByteBuffer encodeConnectionPreface(byte[] preface, SettingsFrame frame) {
}

public List<ByteBuffer> encodeFrame(Http2Frame frame) {
switch (frame.type()) {
case DataFrame.TYPE:
return encodeDataFrame((DataFrame) frame);
case HeadersFrame.TYPE:
return encodeHeadersFrame((HeadersFrame) frame);
case PriorityFrame.TYPE:
return encodePriorityFrame((PriorityFrame) frame);
case ResetFrame.TYPE:
return encodeResetFrame((ResetFrame) frame);
case SettingsFrame.TYPE:
return encodeSettingsFrame((SettingsFrame) frame);
case PushPromiseFrame.TYPE:
return encodePushPromiseFrame((PushPromiseFrame) frame);
case PingFrame.TYPE:
return encodePingFrame((PingFrame) frame);
case GoAwayFrame.TYPE:
return encodeGoAwayFrame((GoAwayFrame) frame);
case WindowUpdateFrame.TYPE:
return encodeWindowUpdateFrame((WindowUpdateFrame) frame);
case ContinuationFrame.TYPE:
return encodeContinuationFrame((ContinuationFrame) frame);
default:
throw new UnsupportedOperationException("Not supported frame "+frame.type()+" ("+frame.getClass().getName()+")");
}
return switch (frame.type()) {
case DataFrame.TYPE -> encodeDataFrame((DataFrame) frame);
case HeadersFrame.TYPE -> encodeHeadersFrame((HeadersFrame) frame);
case PriorityFrame.TYPE -> encodePriorityFrame((PriorityFrame) frame);
case ResetFrame.TYPE -> encodeResetFrame((ResetFrame) frame);
case SettingsFrame.TYPE -> encodeSettingsFrame((SettingsFrame) frame);
case PushPromiseFrame.TYPE -> encodePushPromiseFrame((PushPromiseFrame) frame);
case PingFrame.TYPE -> encodePingFrame((PingFrame) frame);
case GoAwayFrame.TYPE -> encodeGoAwayFrame((GoAwayFrame) frame);
case WindowUpdateFrame.TYPE -> encodeWindowUpdateFrame((WindowUpdateFrame) frame);
case ContinuationFrame.TYPE -> encodeContinuationFrame((ContinuationFrame) frame);

default -> throw new UnsupportedOperationException("Not supported frame " + frame.type() + " (" + frame.getClass().getName() + ")");
};
}

private static final int NO_FLAGS = 0;
@@ -49,13 +49,12 @@ public HeaderFrame(int streamid, int flags, List<ByteBuffer> headerBlocks) {

@Override
public String flagAsString(int flag) {
switch (flag) {
case END_HEADERS:
return "END_HEADERS";
case END_STREAM:
return "END_STREAM";
}
return super.flagAsString(flag);
return switch (flag) {
case END_HEADERS -> "END_HEADERS";
case END_STREAM -> "END_STREAM";

default -> super.flagAsString(flag);
};
}


@@ -72,15 +72,13 @@ int length() {

@Override
public String flagAsString(int flag) {
switch (flag) {
case END_STREAM:
return "END_STREAM";
case PADDED:
return "PADDED";
case PRIORITY:
return "PRIORITY";
}
return super.flagAsString(flag);
return switch (flag) {
case END_STREAM -> "END_STREAM";
case PADDED -> "PADDED";
case PRIORITY -> "PRIORITY";

default -> super.flagAsString(flag);
};
}

public void setPadLength(int padLength) {

1 comment on commit ac54900

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on ac54900 Dec 4, 2020

Please sign in to comment.