test/sun/net/www/http/KeepAliveCache/B5045306.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5045306 6356004
  27  * @library ../../httptest/
  28  * @build HttpCallback HttpServer HttpTransaction
  29  * @run main/othervm B5045306
  30  * @summary Http keep-alive implementation is not efficient
  31  */
  32 
  33 import java.net.*;
  34 import java.io.*;
  35 import java.nio.channels.*;
  36 import java.lang.management.*;
  37 
  38 /* Part 1:
  39  * The http client makes a connection to a URL whos content contains a lot of
  40  * data, more than can fit in the socket buffer. The client only reads
  41  * 1 byte of the data from the InputStream leaving behind more data than can
  42  * fit in the socket buffer. The client then makes a second call to the http
  43  * server. If the connection port used by the client is the same as for the
  44  * first call then that means that the connection is being reused.
  45  *
  46  * Part 2:
  47  * Test buggy webserver that sends less data than it specifies in its
  48  * Content-length header.
  49  */
  50 
  51 public class B5045306
  52 {
  53     static SimpleHttpTransaction httpTrans;
  54     static HttpServer server;
  55 


 147     public void request(HttpTransaction trans) {
 148         try {
 149             String path = trans.getRequestURI().getPath();
 150             if (path.equals("/firstCall")) {
 151                 port1 = trans.channel().socket().getPort();
 152                 System.out.println("First connection on client port = " + port1);
 153 
 154                 byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
 155                 for (int i=0; i<responseBody.length; i++)
 156                     responseBody[i] = 0x41;
 157                 trans.setResponseEntityBody (responseBody, responseBody.length);
 158                 trans.sendResponse(200, "OK");
 159             } else if (path.equals("/secondCall")) {
 160                 int port2 = trans.channel().socket().getPort();
 161                 System.out.println("Second connection on client port = " + port2);
 162 
 163                 if (port1 != port2)
 164                     failed = true;
 165 
 166                 trans.setResponseHeader ("Content-length", Integer.toString(0));








 167                 trans.sendResponse(200, "OK");
 168             } else if(path.equals("/part2")) {
 169                 System.out.println("Call to /part2");
 170                 byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
 171                 for (int i=0; i<responseBody.length; i++)
 172                     responseBody[i] = 0x41;
 173                 trans.setResponseEntityBody (responseBody, responseBody.length);
 174 
 175                 // override the Content-length header to be greater than the actual response body
 176                 trans.setResponseHeader("Content-length", Integer.toString(responseBody.length+1));
 177                 trans.sendResponse(200, "OK");
 178 
 179                 // now close the socket
 180                 trans.channel().socket().close();
 181             }
 182         } catch (Exception e) {
 183             e.printStackTrace();
 184         }
 185     }
 186 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 5045306 6356004 6993490
  27  * @library ../../httptest/
  28  * @build HttpCallback HttpServer HttpTransaction
  29  * @run main/othervm B5045306
  30  * @summary Http keep-alive implementation is not efficient
  31  */
  32 
  33 import java.net.*;
  34 import java.io.*;

  35 import java.lang.management.*;
  36 
  37 /* Part 1:
  38  * The http client makes a connection to a URL whos content contains a lot of
  39  * data, more than can fit in the socket buffer. The client only reads
  40  * 1 byte of the data from the InputStream leaving behind more data than can
  41  * fit in the socket buffer. The client then makes a second call to the http
  42  * server. If the connection port used by the client is the same as for the
  43  * first call then that means that the connection is being reused.
  44  *
  45  * Part 2:
  46  * Test buggy webserver that sends less data than it specifies in its
  47  * Content-length header.
  48  */
  49 
  50 public class B5045306
  51 {
  52     static SimpleHttpTransaction httpTrans;
  53     static HttpServer server;
  54 


 146     public void request(HttpTransaction trans) {
 147         try {
 148             String path = trans.getRequestURI().getPath();
 149             if (path.equals("/firstCall")) {
 150                 port1 = trans.channel().socket().getPort();
 151                 System.out.println("First connection on client port = " + port1);
 152 
 153                 byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
 154                 for (int i=0; i<responseBody.length; i++)
 155                     responseBody[i] = 0x41;
 156                 trans.setResponseEntityBody (responseBody, responseBody.length);
 157                 trans.sendResponse(200, "OK");
 158             } else if (path.equals("/secondCall")) {
 159                 int port2 = trans.channel().socket().getPort();
 160                 System.out.println("Second connection on client port = " + port2);
 161 
 162                 if (port1 != port2)
 163                     failed = true;
 164 
 165                 trans.setResponseHeader ("Content-length", Integer.toString(0));
 166 
 167                  /* Force the server to not respond for more that the timeout
 168                   * set by the keepalive cleaner (5000 millis). This ensures the
 169                   * timeout is correctly resets the default read timeout,
 170                   * infinity. See 6993490. */
 171                 System.out.println("server sleeping...");
 172                 try {Thread.sleep(6000); } catch (InterruptedException e) {}
 173 
 174                 trans.sendResponse(200, "OK");
 175             } else if(path.equals("/part2")) {
 176                 System.out.println("Call to /part2");
 177                 byte[] responseBody = new byte[RESPONSE_DATA_LENGTH];
 178                 for (int i=0; i<responseBody.length; i++)
 179                     responseBody[i] = 0x41;
 180                 trans.setResponseEntityBody (responseBody, responseBody.length);
 181 
 182                 // override the Content-length header to be greater than the actual response body
 183                 trans.setResponseHeader("Content-length", Integer.toString(responseBody.length+1));
 184                 trans.sendResponse(200, "OK");
 185 
 186                 // now close the socket
 187                 trans.channel().socket().close();
 188             }
 189         } catch (Exception e) {
 190             e.printStackTrace();
 191         }
 192     }
 193 }