test/jdk/java/net/httpclient/ShortResponseBody.java
branchhttp-client-branch
changeset 56812 a6180efe1d58
parent 56795 03ece2518428
child 56833 be0819373531
equal deleted inserted replaced
56811:7c86bd1e9a43 56812:a6180efe1d58
   262         }
   262         }
   263     }
   263     }
   264 
   264 
   265     // can be used to prolong request body publication
   265     // can be used to prolong request body publication
   266     static final class InfiniteInputStream extends InputStream {
   266     static final class InfiniteInputStream extends InputStream {
       
   267         int count = 0;
       
   268         int k16 = 0;
   267         @Override
   269         @Override
   268         public int read() throws IOException {
   270         public int read() throws IOException {
       
   271             if (++count == 1) {
       
   272                 System.out.println("Start sending 1 byte");
       
   273             }
       
   274             if (count > 16 * 1024) {
       
   275                 k16++;
       
   276                 System.out.println("... 16K sent.");
       
   277                 count = count % (16 * 1024);
       
   278             }
       
   279             if (k16 > 128) {
       
   280                 System.out.println("WARNING: InfiniteInputStream: " +
       
   281                         "more than 128 16k buffers generated: returning EOF");
       
   282                 return -1;
       
   283             }
   269             return 1;
   284             return 1;
   270         }
   285         }
   271 
   286 
   272         @Override
   287         @Override
   273         public int read(byte[] buf, int offset, int length) {
   288         public int read(byte[] buf, int offset, int length) {
   274             //int count = offset;
   289             //int count = offset;
   275             //length = Math.max(0, Math.min(buf.length - offset, length));
   290             length = Math.max(0, Math.min(buf.length - offset, length));
   276             //for (; count < length; count++)
   291             //for (; count < length; count++)
   277             //    buf[offset++] = 0x01;
   292             //    buf[offset++] = 0x01;
   278             //return count;
   293             //return count;
   279             return Math.max(0, Math.min(buf.length - offset, length));
   294             if (count == 0) {
       
   295                 System.out.println("Start sending " + length);
       
   296             } else if (count > 16 * 1024) {
       
   297                 k16++;
       
   298                 System.out.println("... 16K sent.");
       
   299                 count = count % (16 * 1024);
       
   300             }
       
   301             if (k16 > 128) {
       
   302                 System.out.println("WARNING: InfiniteInputStream: " +
       
   303                         "more than 128 16k buffers generated: returning EOF");
       
   304                 return -1;
       
   305             }
       
   306             count += length;
       
   307             return length;
   280         }
   308         }
   281     }
   309     }
   282 
   310 
   283     // POST tests are racy in what may be received before writing may cause a
   311     // POST tests are racy in what may be received before writing may cause a
   284     // broken pipe or reset exception, before all the received data can be read.
   312     // broken pipe or reset exception, before all the received data can be read.
   491                     InputStream is = s.getInputStream();
   519                     InputStream is = s.getInputStream();
   492                     URI requestMethod = readRequestMethod(is);
   520                     URI requestMethod = readRequestMethod(is);
   493                     out.print(requestMethod + " ");
   521                     out.print(requestMethod + " ");
   494                     URI uriPath = readRequestPath(is);
   522                     URI uriPath = readRequestPath(is);
   495                     out.println(uriPath);
   523                     out.println(uriPath);
   496                     readRequestHeaders(is);
   524                     String headers = readRequestHeaders(is);
   497 
   525 
   498                     String query = uriPath.getRawQuery();
   526                     String query = uriPath.getRawQuery();
   499                     assert query != null;
   527                     if (query == null) {
       
   528                         out.println("Request headers: [" + headers + "]");
       
   529                     }
       
   530                     assert query != null : "null query for uriPath: " + uriPath;
   500                     String qv = query.split("=")[1];
   531                     String qv = query.split("=")[1];
   501                     int len;
   532                     int len;
   502                     if (qv.equals("all")) {
   533                     if (qv.equals("all")) {
   503                         len = response().getBytes(US_ASCII).length;
   534                         len = response().getBytes(US_ASCII).length;
   504                     } else {
   535                     } else {
   540             }
   571             }
   541             return URI.create(sb.toString());
   572             return URI.create(sb.toString());
   542         }
   573         }
   543 
   574 
   544         // Read until the end of a HTTP request headers
   575         // Read until the end of a HTTP request headers
   545         static void readRequestHeaders(InputStream is) throws IOException {
   576         static String readRequestHeaders(InputStream is) throws IOException {
   546             int requestEndCount = 0, r;
   577             int requestEndCount = 0, r;
       
   578             StringBuilder sb = new StringBuilder();
   547             while ((r = is.read()) != -1) {
   579             while ((r = is.read()) != -1) {
       
   580                 sb.append((char) r);
   548                 if (r == requestEnd[requestEndCount]) {
   581                 if (r == requestEnd[requestEndCount]) {
   549                     requestEndCount++;
   582                     requestEndCount++;
   550                     if (requestEndCount == 4) {
   583                     if (requestEndCount == 4) {
   551                         break;
   584                         break;
   552                     }
   585                     }
   553                 } else {
   586                 } else {
   554                     requestEndCount = 0;
   587                     requestEndCount = 0;
   555                 }
   588                 }
   556             }
   589             }
       
   590             return sb.toString();
   557         }
   591         }
   558     }
   592     }
   559 
   593 
   560     /** A server that issues a, possibly-partial, chunked reply. */
   594     /** A server that issues a, possibly-partial, chunked reply. */
   561     static class PlainVariableLengthServer extends ReplyingServer {
   595     static class PlainVariableLengthServer extends ReplyingServer {