jdk/test/sun/net/www/http/ChunkedOutputStream/Test.java
changeset 2 90ce3da70b43
child 88 b2fea49cad6b
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /**
       
    25  * @test
       
    26  * @bug 5026745
       
    27  * @run main/othervm/timeout=500 Test
       
    28  * @summary Cannot flush output stream when writing to an HttpUrlConnection
       
    29  */
       
    30 
       
    31 import java.io.*;
       
    32 import java.net.*;
       
    33 import com.sun.net.httpserver.*;
       
    34 
       
    35 public class Test implements HttpHandler {
       
    36 
       
    37     static int count = 0;
       
    38 
       
    39     static final String str1 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
       
    40                                 "1234567890abcdefkjsdlkjflkjsldkfjlsdkjflkj"+
       
    41                                 "1434567890abcdefkjsdlkjflkjsldkfjlsdkjflkj";
       
    42 
       
    43     static final String str2 = "Helloworld1234567890abcdefghijklmnopqrstuvwxyz"+
       
    44                                 "1234567890";
       
    45 
       
    46     public void handle(HttpExchange exchange) {
       
    47         String reqbody;
       
    48         try {
       
    49             switch (count) {
       
    50             case 0: /* test1 -- keeps conn alive */
       
    51             case 1: /* test2 -- closes conn */
       
    52                 printRequestURI(exchange);
       
    53                 reqbody = read(exchange.getRequestBody());
       
    54                 if (!reqbody.equals(str1)) {
       
    55                     exchange.sendResponseHeaders(500, 0);
       
    56                     break;
       
    57                 }
       
    58 
       
    59                 Headers headers = exchange.getRequestHeaders();
       
    60                 String chunk =  headers.getFirst("Transfer-encoding");
       
    61 
       
    62                 if (!"chunked".equals (chunk)) {
       
    63                     exchange.sendResponseHeaders(501, 0);
       
    64                     break;
       
    65                 }
       
    66 
       
    67                 exchange.sendResponseHeaders(200, reqbody.length());
       
    68                 write(exchange.getResponseBody(), reqbody);
       
    69 
       
    70                 if (count == 1) {
       
    71                     Headers resHeaders = exchange.getResponseHeaders() ;
       
    72                     resHeaders.set("Connection", "close");
       
    73                 }
       
    74                 break;
       
    75             case 2: /* test 3 */
       
    76                 printRequestURI(exchange);
       
    77                 reqbody = read(exchange.getRequestBody());
       
    78 
       
    79                 if (!reqbody.equals(str2)) {
       
    80                     exchange.sendResponseHeaders(500, 0);
       
    81                     break;
       
    82                 }
       
    83                 headers = exchange.getRequestHeaders();
       
    84                 int clen = Integer.parseInt( headers.getFirst("Content-length"));
       
    85 
       
    86                 if (clen != str2.length()) {
       
    87                     exchange.sendResponseHeaders(501, 0);
       
    88                     break;
       
    89                 }
       
    90                 Headers resHeaders = exchange.getResponseHeaders() ;
       
    91                 resHeaders.set("Connection", "close");
       
    92 
       
    93                 exchange.sendResponseHeaders(200, reqbody.length());
       
    94                 write(exchange.getResponseBody(), reqbody);
       
    95                 break;
       
    96             case 3: /* test 4 */
       
    97             case 4: /* test 5 */
       
    98                 printRequestURI(exchange);
       
    99                 break;
       
   100             case 5: /* test 6 */
       
   101                 printRequestURI(exchange);
       
   102                 resHeaders = exchange.getResponseHeaders() ;
       
   103                 resHeaders.set("Location", "http://foo.bar/");
       
   104                 resHeaders.set("Connection", "close");
       
   105                 exchange.sendResponseHeaders(307, 0);
       
   106                 break;
       
   107             case 6: /* test 7 */
       
   108             case 7: /* test 8 */
       
   109                 printRequestURI(exchange);
       
   110                 reqbody = read(exchange.getRequestBody());
       
   111                 if (reqbody != null && !"".equals(reqbody)) {
       
   112                     exchange.sendResponseHeaders(501, 0);
       
   113                     break;
       
   114                 }
       
   115                 resHeaders = exchange.getResponseHeaders() ;
       
   116                 resHeaders.set("Connection", "close");
       
   117                 exchange.sendResponseHeaders(200, 0);
       
   118                 break;
       
   119             case 8: /* test 9 */
       
   120                 printRequestURI(exchange);
       
   121                 reqbody = read(exchange.getRequestBody());
       
   122                 if (!reqbody.equals(str1)) {
       
   123                     exchange.sendResponseHeaders(500, 0);
       
   124                     break;
       
   125                 }
       
   126 
       
   127                 headers = exchange.getRequestHeaders();
       
   128                 chunk =  headers.getFirst("Transfer-encoding");
       
   129                 if (!"chunked".equals(chunk)) {
       
   130                     exchange.sendResponseHeaders(501, 0);
       
   131                     break;
       
   132                 }
       
   133 
       
   134                 exchange.sendResponseHeaders(200, reqbody.length());
       
   135                 write(exchange.getResponseBody(), reqbody);
       
   136                 break;
       
   137             case 9: /* test10 */
       
   138                 printRequestURI(exchange);
       
   139                 InputStream is = exchange.getRequestBody();
       
   140                 String s = read (is, str1.length());
       
   141 
       
   142                 boolean error = false;
       
   143                 for (int i=10; i< 200 * 1024; i++) {
       
   144                     byte c = (byte)is.read();
       
   145 
       
   146                     if (c != (byte)i) {
       
   147                         error = true;
       
   148                         System.out.println ("error at position " + i);
       
   149                     }
       
   150                 }
       
   151                 if (!s.equals(str1) ) {
       
   152                     System.out.println ("received string : " + s);
       
   153                     exchange.sendResponseHeaders(500, 0);
       
   154                 } else if (error) {
       
   155                     System.out.println ("error");
       
   156                     exchange.sendResponseHeaders(500, 0);
       
   157                 } else {
       
   158                     exchange.sendResponseHeaders(200, 0);
       
   159                 }
       
   160                 break;
       
   161             }
       
   162             exchange.close();
       
   163             count ++;
       
   164         } catch (IOException e) {
       
   165             e.printStackTrace();
       
   166         }
       
   167     }
       
   168 
       
   169     static void printRequestURI(HttpExchange exchange) {
       
   170         URI uri = exchange.getRequestURI();
       
   171         System.out.println("HttpServer: handle " + uri);
       
   172     }
       
   173 
       
   174 
       
   175     static String read (InputStream is, int len) {
       
   176         try {
       
   177             byte[] ba = new byte [len];
       
   178             int c;
       
   179             int l = 0;
       
   180             while ((c= is.read(ba, l, ba.length-l)) != -1 && l<len)  {
       
   181                 l += c;
       
   182             }
       
   183             return new String (ba, 0, l, "ISO8859-1");
       
   184         } catch (Exception e) {
       
   185             e.printStackTrace();
       
   186         }
       
   187         return null;
       
   188     }
       
   189 
       
   190     static String read(InputStream is) {
       
   191         try {
       
   192             byte[] ba = new byte [8096];
       
   193             int off = 0, c;
       
   194             while ((c= is.read(ba, off, ba.length)) != -1)  {
       
   195                 off += c;
       
   196             }
       
   197             return new String(ba, 0, off, "ISO8859-1");
       
   198         } catch (Exception e) {
       
   199             e.printStackTrace();
       
   200         }
       
   201         return null;
       
   202     }
       
   203 
       
   204     static void write(OutputStream os, String str) {
       
   205         try {
       
   206             byte[] ba = str.getBytes("ISO8859-1");
       
   207             os.write(ba);
       
   208         } catch (Exception e) {
       
   209             e.printStackTrace();
       
   210         }
       
   211     }
       
   212 
       
   213     static void readAndCompare(InputStream is, String cmp) throws IOException {
       
   214         int c;
       
   215         byte buf[] = new byte [1024];
       
   216         int off = 0;
       
   217         int len = 1024;
       
   218         while ((c=is.read(buf, off, len)) != -1) {
       
   219             off += c;
       
   220             len -= c;
       
   221         }
       
   222         String s1 = new String(buf, 0, off, "ISO8859_1");
       
   223         if (!cmp.equals(s1)) {
       
   224             throw new IOException("strings not same");
       
   225         }
       
   226     }
       
   227 
       
   228     /* basic chunked test (runs twice) */
       
   229 
       
   230     static void test1 (String u) throws Exception {
       
   231         URL url = new URL (u);
       
   232         System.out.println ("client opening connection to: " + u);
       
   233         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   234         urlc.setChunkedStreamingMode (20);
       
   235         urlc.setDoOutput(true);
       
   236         urlc.setRequestMethod ("POST");
       
   237         OutputStream os = urlc.getOutputStream ();
       
   238         os.write (str1.getBytes());
       
   239         os.close();
       
   240         InputStream is = urlc.getInputStream();
       
   241         readAndCompare (is, str1);
       
   242         is.close();
       
   243     }
       
   244 
       
   245     /* basic fixed length test */
       
   246 
       
   247     static void test3 (String u) throws Exception {
       
   248         URL url = new URL (u);
       
   249         System.out.println ("client opening connection to: " + u);
       
   250         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   251         urlc.setFixedLengthStreamingMode (str2.length());
       
   252         urlc.setDoOutput(true);
       
   253         urlc.setRequestMethod ("POST");
       
   254         OutputStream os = urlc.getOutputStream ();
       
   255         os.write (str2.getBytes());
       
   256         os.close();
       
   257         InputStream is = urlc.getInputStream();
       
   258         readAndCompare (is, str2);
       
   259         is.close();
       
   260     }
       
   261 
       
   262     /* write too few bytes */
       
   263 
       
   264     static void test4 (String u) throws Exception {
       
   265         URL url = new URL (u);
       
   266         System.out.println ("client opening connection to: " + u);
       
   267         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   268         urlc.setFixedLengthStreamingMode (str2.length()+1);
       
   269         urlc.setDoOutput(true);
       
   270         urlc.setRequestMethod ("POST");
       
   271         OutputStream os = urlc.getOutputStream ();
       
   272         os.write (str2.getBytes());
       
   273         try {
       
   274             os.close();
       
   275             throw new Exception ("should have thrown IOException");
       
   276         } catch (IOException e) {}
       
   277     }
       
   278 
       
   279     /* write too many bytes */
       
   280 
       
   281     static void test5 (String u) throws Exception {
       
   282         URL url = new URL (u);
       
   283         System.out.println ("client opening connection to: " + u);
       
   284         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   285         urlc.setFixedLengthStreamingMode (str2.length()-1);
       
   286         urlc.setDoOutput(true);
       
   287         urlc.setRequestMethod ("POST");
       
   288         OutputStream os = urlc.getOutputStream ();
       
   289         try {
       
   290             os.write (str2.getBytes());
       
   291             throw new Exception ("should have thrown IOException");
       
   292         } catch (IOException e) {}
       
   293     }
       
   294 
       
   295     /* check for HttpRetryException on redirection */
       
   296 
       
   297     static void test6 (String u) throws Exception {
       
   298         URL url = new URL (u);
       
   299         System.out.println ("client opening connection to: " + u);
       
   300         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   301         urlc.setChunkedStreamingMode (20);
       
   302         urlc.setDoOutput(true);
       
   303         urlc.setRequestMethod ("POST");
       
   304         OutputStream os = urlc.getOutputStream ();
       
   305         os.write (str1.getBytes());
       
   306         os.close();
       
   307         try {
       
   308             InputStream is = urlc.getInputStream();
       
   309             throw new Exception ("should have gotten HttpRetryException");
       
   310         } catch (HttpRetryException e) {
       
   311             if (e.responseCode() != 307) {
       
   312                 throw new Exception ("Wrong response code " + e.responseCode());
       
   313             }
       
   314             if (!e.getLocation().equals ("http://foo.bar/")) {
       
   315                 throw new Exception ("Wrong location " + e.getLocation());
       
   316             }
       
   317         }
       
   318     }
       
   319 
       
   320     /* next two tests send zero length posts */
       
   321 
       
   322     static void test7 (String u) throws Exception {
       
   323         URL url = new URL (u);
       
   324         System.out.println ("client opening connection to: " + u);
       
   325         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   326         urlc.setChunkedStreamingMode (20);
       
   327         urlc.setDoOutput(true);
       
   328         urlc.setRequestMethod ("POST");
       
   329         OutputStream os = urlc.getOutputStream ();
       
   330         os.close();
       
   331         int ret = urlc.getResponseCode();
       
   332         if (ret != 200) {
       
   333             throw new Exception ("Expected 200: got " + ret);
       
   334         }
       
   335     }
       
   336 
       
   337     static void test8 (String u) throws Exception {
       
   338         URL url = new URL (u);
       
   339         System.out.println ("client opening connection to: " + u);
       
   340         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   341         urlc.setFixedLengthStreamingMode (0);
       
   342         urlc.setDoOutput(true);
       
   343         urlc.setRequestMethod ("POST");
       
   344         OutputStream os = urlc.getOutputStream ();
       
   345         os.close();
       
   346         int ret = urlc.getResponseCode();
       
   347         if (ret != 200) {
       
   348             throw new Exception ("Expected 200: got " + ret);
       
   349         }
       
   350     }
       
   351 
       
   352     /* calling setChunkedStreamingMode with -1 should entail using
       
   353        the default chunk size */
       
   354    static void test9 (String u) throws Exception {
       
   355         URL url = new URL (u);
       
   356         System.out.println ("client opening connection to: " + u);
       
   357         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   358         urlc.setChunkedStreamingMode (-1);
       
   359         urlc.setDoOutput(true);
       
   360         urlc.setRequestMethod ("POST");
       
   361         OutputStream os = urlc.getOutputStream ();
       
   362         os.write (str1.getBytes());
       
   363         os.close();
       
   364         InputStream is = urlc.getInputStream();
       
   365         readAndCompare (is, str1);
       
   366         is.close();
       
   367     }
       
   368 
       
   369    static void test10 (String u) throws Exception {
       
   370         URL url = new URL (u);
       
   371         System.out.println ("client opening connection to: " + u);
       
   372         HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
       
   373         urlc.setChunkedStreamingMode (4 * 1024);
       
   374         urlc.setDoOutput(true);
       
   375         urlc.setRequestMethod ("POST");
       
   376         OutputStream os = urlc.getOutputStream ();
       
   377         byte[] buf = new byte [200 * 1024];
       
   378         for (int i=0; i< 200 * 1024; i++) {
       
   379             buf[i] = (byte) i;
       
   380         }
       
   381         /* write a small bit first, and then the large buffer */
       
   382         os.write (str1.getBytes());
       
   383         os.write (buf, 10, buf.length - 10); /* skip 10 bytes to test offset */
       
   384         os.close();
       
   385         InputStream is = urlc.getInputStream();
       
   386         is.close();
       
   387         int ret = urlc.getResponseCode();
       
   388         if (ret != 200) {
       
   389             throw new Exception ("Expected 200: got " + ret);
       
   390         }
       
   391     }
       
   392 
       
   393     static com.sun.net.httpserver.HttpServer httpserver;
       
   394 
       
   395     public static void main (String[] args) throws Exception {
       
   396         try {
       
   397             httpserver = com.sun.net.httpserver.HttpServer.create(new InetSocketAddress(0), 0);
       
   398             HttpContext ctx = httpserver.createContext("/test/", new Test() );
       
   399             httpserver.start();
       
   400 
       
   401             int port = httpserver.getAddress().getPort();
       
   402 
       
   403             System.out.println ("Server started: listening on port: " + port);
       
   404             test1("http://localhost:"+ port + "/test/test1");
       
   405             test1("http://localhost:"+ port + "/test/test2");
       
   406             test3("http://localhost:"+ port + "/test/test3");
       
   407             test4("http://localhost:"+ port + "/test/test4");
       
   408             test5("http://localhost:"+ port + "/test/test5");
       
   409             test6("http://localhost:"+ port + "/test/test6");
       
   410             test7("http://localhost:"+ port + "/test/test7");
       
   411             test8("http://localhost:"+ port + "/test/test8");
       
   412             test9("http://localhost:"+ port + "/test/test9");
       
   413             test10("http://localhost:"+ port + "/test/test10");
       
   414         } finally {
       
   415             if (httpserver != null)
       
   416                 httpserver.stop(0);
       
   417         }
       
   418     }
       
   419 
       
   420 }