1 /*
2  * Copyright (c) 2007, 2019, Oracle and/or its affiliates. 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 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 5052093
27  * @modules java.base/sun.net.www java.base/sun.net.www.protocol.file
28  * @library ../../../sun/net/www/httptest/
29  * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction
30  * @run main B5052093
31  * @summary URLConnection doesn't support large files
32  */
33 import java.net.*;
34 import java.io.*;
35 import sun.net.www.protocol.file.FileURLConnection;
36 import static java.net.Proxy.NO_PROXY;
37 
38 public class B5052093 implements HttpCallback {
39     private static TestHttpServer server;
40     private static long testSize = ((long) (Integer.MAX_VALUE)) + 2;
41 
42     public static class LargeFile extends File {
LargeFile()43         public LargeFile() {
44             super("/dev/zero");
45         }
46 
length()47         public long length() {
48             return testSize;
49         }
50     }
51 
52     public static class LargeFileURLConnection extends FileURLConnection {
LargeFileURLConnection(LargeFile f)53         public LargeFileURLConnection(LargeFile f) throws IOException {
54                 super(new URL("file:///dev/zero"), f);
55         }
56     }
57 
request(HttpTransaction req)58     public void request(HttpTransaction req) {
59         try {
60             req.setResponseHeader("content-length", Long.toString(testSize));
61             req.sendResponse(200, "OK");
62         } catch (IOException e) {
63             e.printStackTrace();
64         }
65     }
66 
main(String[] args)67     public static void main(String[] args) throws Exception {
68         InetAddress loopback = InetAddress.getLoopbackAddress();
69         server = new TestHttpServer(new B5052093(), 1, 10, loopback, 0);
70         try {
71             URL url = new URL("http://" + server.getAuthority() + "/foo");
72             URLConnection conn = url.openConnection(NO_PROXY);
73             int i = conn.getContentLength();
74             long l = conn.getContentLengthLong();
75             if (i != -1 || l != testSize) {
76                 System.out.println("conn.getContentLength = " + i);
77                 System.out.println("conn.getContentLengthLong = " + l);
78                 System.out.println("testSize = " + testSize);
79                 throw new RuntimeException("Wrong content-length from http");
80             }
81 
82             URLConnection fu = new LargeFileURLConnection(new LargeFile());
83             i = fu.getContentLength();
84             l = fu.getContentLengthLong();
85             if (i != -1 || l != testSize) {
86                 System.out.println("fu.getContentLength = " + i);
87                 System.out.println("fu.getContentLengthLong = " + l);
88                 System.out.println("testSize = " + testSize);
89                 throw new RuntimeException("Wrong content-length from file");
90             }
91         } finally {
92             server.terminate();
93         }
94     }
95 }
96