jdk/test/com/sun/net/httpserver/FileServerHandler.java
author dfuchs
Thu, 08 Jun 2017 12:41:07 +0100
changeset 45531 fb3dbffad37b
parent 36131 379db4b2f95d
child 45535 4b19310ae4ee
permissions -rw-r--r--
8180044: java/net/httpclient/ManyRequests.java failed due to timeout Summary: Fixes several race conditions observed while testing. Reviewed-by: michaelm, msheppar, prappo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
     2
 * Copyright (c) 2005, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.concurrent.*;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    26
import java.util.logging.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import com.sun.net.httpserver.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Implements a basic static content HTTP server
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * which understands text/html, text/plain content types
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Must be given an abs pathname to the document root.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Directory listings together with text + html files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * can be served.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    40
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    41
 * File Server created on files sub-path
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    42
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    43
 * Echo server created on echo sub-path
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public class FileServerHandler implements HttpHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        public static void main (String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            if (args.length != 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                System.out.println ("usage: java FileServerHandler rootDir port logfilename");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    52
            Logger logger = Logger.getLogger("com.sun.net.httpserver");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    53
            ConsoleHandler ch = new ConsoleHandler();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    54
            logger.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    55
            ch.setLevel(Level.ALL);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    56
            logger.addHandler(ch);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            String rootDir = args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            int port = Integer.parseInt (args[1]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            String logfile = args[2];
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    61
            HttpServer server = HttpServer.create (new InetSocketAddress (port), 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            HttpHandler h = new FileServerHandler (rootDir);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    63
            HttpHandler h1 = new EchoHandler ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    65
            HttpContext c = server.createContext ("/files", h);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            c.getFilters().add (new LogFilter (new File (logfile)));
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    67
            HttpContext c1 = server.createContext ("/echo", h1);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    68
            c.getFilters().add (new LogFilter (new File (logfile)));
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    69
            c1.getFilters().add (new LogFilter (new File (logfile)));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            server.setExecutor (Executors.newCachedThreadPool());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            server.start ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        String docroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        FileServerHandler (String docroot) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            this.docroot = docroot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        int invocation = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        public void handle (HttpExchange t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            InputStream is = t.getRequestBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            Headers map = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            Headers rmap = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            URI uri = t.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            String path = uri.getPath();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    90
            int x = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
    91
            while (is.read () != -1) x++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            is.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            File f = new File (docroot, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            if (!f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                notfound (t, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            String fixedrequest = map.getFirst ("XFixed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            String method = t.getRequestMethod();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (method.equals ("HEAD")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                rmap.set ("Content-Length", Long.toString (f.length()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                t.sendResponseHeaders (200, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            } else if (!method.equals("GET")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                t.sendResponseHeaders (405, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (path.endsWith (".html") || path.endsWith (".htm")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                rmap.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                rmap.set ("Content-Type", "text/plain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if (f.isDirectory()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                if (!path.endsWith ("/")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                    moved (t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                rmap.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                t.sendResponseHeaders (200, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                String[] list = f.list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                PrintStream p = new PrintStream (os);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                p.println ("<h2>Directory listing for: " + path+ "</h2>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                p.println ("<ul>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                for (int i=0; i<list.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    p.println ("<li><a href=\""+list[i]+"\">"+list[i]+"</a></li>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                p.println ("</ul><p><hr>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                p.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                int clen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                if (fixedrequest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    clen = (int) f.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    clen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                t.sendResponseHeaders (200, clen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                FileInputStream fis = new FileInputStream (f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                byte[] buf = new byte [16 * 1024];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                while ((len=fis.read (buf)) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    os.write (buf, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    count += len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                fis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        void moved (HttpExchange t) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Headers req = t.getRequestHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            Headers map = t.getResponseHeaders();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            URI uri = t.getRequestURI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            String host = req.getFirst ("Host");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            String location = "http://"+host+uri.getPath() + "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            map.set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            map.set ("Location", location);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            t.sendResponseHeaders (301, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        void notfound (HttpExchange t, String p) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            t.getResponseHeaders().set ("Content-Type", "text/html");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            t.sendResponseHeaders (404, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            OutputStream os = t.getResponseBody();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            String s = "<h2>File not found</h2>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            s = s + p + "<p>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            os.write (s.getBytes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            t.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   183
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   184
class EchoHandler implements HttpHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   185
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   186
    byte[] read(InputStream is) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   187
        byte[] buf = new byte[1024];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   188
        byte[] result = new byte[0];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   189
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   190
        while (true) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   191
            int n = is.read(buf);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   192
            if (n > 0) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   193
                byte[] b1 = new byte[result.length + n];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   194
                System.arraycopy(result, 0, b1, 0, result.length);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   195
                System.arraycopy(buf, 0, b1, result.length, n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   196
                result = b1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   197
            } else if (n == -1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   198
                return result;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   199
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   200
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   201
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   202
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   203
    public void handle (HttpExchange t)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   204
        throws IOException
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   205
    {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   206
        InputStream is = t.getRequestBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   207
        Headers map = t.getRequestHeaders();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   208
        String fixedrequest = map.getFirst ("XFixed");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   209
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   210
        // return the number of bytes received (no echo)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   211
        String summary = map.getFirst ("XSummary");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   212
        if (fixedrequest != null && summary == null)  {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   213
            byte[] in = read(is);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   214
            t.sendResponseHeaders(200, in.length);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   215
            OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   216
            os.write(in);
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   217
            close(os);
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   218
            close(is);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   219
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   220
            OutputStream os = t.getResponseBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   221
            byte[] buf = new byte[64 * 1024];
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   222
            t.sendResponseHeaders(200, 0);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   223
            int n, count=0;;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   224
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   225
            while ((n = is.read(buf)) != -1) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   226
                if (summary == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   227
                    os.write(buf, 0, n);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   228
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   229
                count += n;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   230
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   231
            if (summary != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   232
                String s = Integer.toString(count);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   233
                os.write(s.getBytes());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   234
            }
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   235
            close(os);
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   236
            close(is);
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   237
        }
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   238
    }
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   239
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   240
    protected void close(OutputStream os) throws IOException {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   241
            os.close();
45531
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   242
    }
fb3dbffad37b 8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents: 36131
diff changeset
   243
    protected void close(InputStream is) throws IOException {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   244
            is.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   245
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents: 5506
diff changeset
   246
    }