24 */ |
24 */ |
25 |
25 |
26 package jdk.internal.net.http; |
26 package jdk.internal.net.http; |
27 |
27 |
28 import java.io.IOException; |
28 import java.io.IOException; |
29 import java.lang.System.Logger.Level; |
|
30 import java.nio.ByteBuffer; |
29 import java.nio.ByteBuffer; |
31 import java.util.ArrayList; |
30 import java.util.ArrayList; |
32 import java.util.Collections; |
31 import java.util.Collections; |
33 import java.util.List; |
32 import java.util.List; |
34 import java.util.function.Consumer; |
33 import java.util.function.Consumer; |
35 import java.net.http.HttpHeaders; |
34 import java.net.http.HttpHeaders; |
36 import java.net.http.HttpResponse; |
35 import java.net.http.HttpResponse; |
37 |
|
38 import jdk.internal.net.http.common.Logger; |
36 import jdk.internal.net.http.common.Logger; |
39 import jdk.internal.net.http.common.Utils; |
37 import jdk.internal.net.http.common.Utils; |
|
38 import static java.lang.String.format; |
40 |
39 |
41 /** |
40 /** |
42 * Implements chunked/fixed transfer encodings of HTTP/1.1 responses. |
41 * Implements chunked/fixed transfer encodings of HTTP/1.1 responses. |
43 * |
42 * |
44 * Call pushBody() to read the body (blocking). Data and errors are provided |
43 * Call pushBody() to read the body (blocking). Data and errors are provided |
93 return chunkedContent; |
92 return chunkedContent; |
94 } |
93 } |
95 |
94 |
96 interface BodyParser extends Consumer<ByteBuffer> { |
95 interface BodyParser extends Consumer<ByteBuffer> { |
97 void onSubscribe(AbstractSubscription sub); |
96 void onSubscribe(AbstractSubscription sub); |
|
97 // A current-state message suitable for inclusion in an exception |
|
98 // detail message. |
|
99 String currentStateMessage(); |
98 } |
100 } |
99 |
101 |
100 // Returns a parser that will take care of parsing the received byte |
102 // Returns a parser that will take care of parsing the received byte |
101 // buffers and forward them to the BodySubscriber. |
103 // buffers and forward them to the BodySubscriber. |
102 // When the parser is done, it will call onComplete. |
104 // When the parser is done, it will call onComplete. |
140 @Override |
142 @Override |
141 public void onSubscribe(AbstractSubscription sub) { |
143 public void onSubscribe(AbstractSubscription sub) { |
142 if (debug.on()) |
144 if (debug.on()) |
143 debug.log("onSubscribe: " + pusher.getClass().getName()); |
145 debug.log("onSubscribe: " + pusher.getClass().getName()); |
144 pusher.onSubscribe(this.sub = sub); |
146 pusher.onSubscribe(this.sub = sub); |
|
147 } |
|
148 |
|
149 @Override |
|
150 public String currentStateMessage() { |
|
151 return format("chunked transfer encoding, state: %s", state); |
145 } |
152 } |
146 |
153 |
147 @Override |
154 @Override |
148 public void accept(ByteBuffer b) { |
155 public void accept(ByteBuffer b) { |
149 if (closedExceptionally != null) { |
156 if (closedExceptionally != null) { |
423 } |
430 } |
424 } |
431 } |
425 } |
432 } |
426 |
433 |
427 @Override |
434 @Override |
|
435 public String currentStateMessage() { |
|
436 return format("fixed content-length: %d, bytes received: %d", |
|
437 contentLength, contentLength - remaining); |
|
438 } |
|
439 |
|
440 @Override |
428 public void accept(ByteBuffer b) { |
441 public void accept(ByteBuffer b) { |
429 if (closedExceptionally != null) { |
442 if (closedExceptionally != null) { |
430 if (debug.on()) |
443 if (debug.on()) |
431 debug.log("already closed: " + closedExceptionally); |
444 debug.log("already closed: " + closedExceptionally); |
432 return; |
445 return; |