src/jdk.httpserver/share/classes/sun/net/httpserver/Request.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 25859 jdk/src/jdk.httpserver/share/classes/sun/net/httpserver/Request.java@3317bb8137f4
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2005, 2013, 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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2624
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.net.httpserver;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.nio.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.channels.*;
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
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
class Request {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    final static int BUF_LEN = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    final static byte CR = 13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    final static byte LF = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private String startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private SocketChannel chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private InputStream is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private OutputStream os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    Request (InputStream rawInputStream, OutputStream rawout) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        is = rawInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        os = rawout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            startLine = readLine();
2612
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    51
            if (startLine == null) {
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    52
                return;
d7fb0809c7e4 6630639: lightweight HttpServer leaks file descriptors on no-data connections
michaelm
parents: 2
diff changeset
    53
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            /* skip blank lines */
1511
65ddd8f149f3 6756771: com.sun.net.httpserver.HttpServer should handle POSTs larger than 2Gig
chegar
parents: 2
diff changeset
    55
        } while (startLine == null ? false : startLine.equals (""));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    char[] buf = new char [BUF_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    StringBuffer lineBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public InputStream inputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return is;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    public OutputStream outputStream () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return os;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * read a line from the stream returning as a String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Not used for reading headers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public String readLine () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        boolean gotCR = false, gotLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        pos = 0; lineBuf = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        while (!gotLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            int c = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (gotCR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                if (c == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    gotLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    gotCR = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    consume (CR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                if (c == CR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    gotCR = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    consume (c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        lineBuf.append (buf, 0, pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return new String (lineBuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private void consume (int c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (pos == BUF_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            lineBuf.append (buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            pos = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        buf[pos++] = (char)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * returns the request line (first line of a request)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public String requestLine () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return startLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    Headers hdrs = null;
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   120
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    Headers headers () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (hdrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        hdrs = new Headers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        char s[] = new char[10];
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   128
        int len = 0;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   129
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        int firstc = is.read();
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   131
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   132
        // check for empty headers
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   133
        if (firstc == CR || firstc == LF) {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   134
            int c = is.read();
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   135
            if (c == CR || c == LF) {
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   136
                return hdrs;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   137
            }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   138
            s[0] = (char)firstc;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   139
            len = 1;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   140
            firstc = c;
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   141
        }
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   142
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        while (firstc != LF && firstc != CR && firstc >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            int keyend = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            boolean inKey = firstc > ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            s[len++] = (char) firstc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    parseloop:{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                while ((c = is.read()) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    switch (c) {
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
   151
                      /*fallthrough*/
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                      case ':':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                        if (inKey && len > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                            keyend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                      case '\t':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                      case ' ':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                        inKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                      case CR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                      case LF:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                        firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                        if (c == CR && firstc == LF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                            firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                            if (firstc == CR)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                firstc = is.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        if (firstc == LF || firstc == CR || firstc > ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                            break parseloop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        /* continuation */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        c = ' ';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (len >= s.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        char ns[] = new char[s.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                        System.arraycopy(s, 0, ns, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        s = ns;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    s[len++] = (char) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                firstc = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            while (len > 0 && s[len - 1] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            String k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (keyend <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                k = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                keyend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                k = String.copyValueOf(s, 0, keyend);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                if (keyend < len && s[keyend] == ':')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                while (keyend < len && s[keyend] <= ' ')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                    keyend++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            String v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            if (keyend >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                v = new String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                v = String.copyValueOf(s, keyend, len - keyend);
11901
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   203
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   204
            if (hdrs.size() >= ServerConfig.getMaxReqHeaders()) {
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   205
                throw new IOException("Maximum number of request headers (" +
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   206
                        "sun.net.httpserver.maxReqHeaders) exceeded, " +
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   207
                        ServerConfig.getMaxReqHeaders() + ".");
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   208
            }
d7f004398a91 7126960: Add property to limit number of request headers to the HTTP Server
chegar
parents: 7668
diff changeset
   209
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            hdrs.add (k,v);
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   211
            len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Implements blocking reading semantics on top of a non-blocking channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    static class ReadStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        ByteBuffer chanbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        byte[] one;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   224
        private boolean closed = false, eof = false;
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 14342
diff changeset
   225
        ByteBuffer markBuf; /* reads may be satisfied from this buffer */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        boolean marked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        boolean reset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        int readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        static long readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        ServerImpl server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   231
        final static int BUFSIZE = 8 * 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            this.channel = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            this.server = server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   236
            chanbuf = ByteBuffer.allocate (BUFSIZE);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   237
            chanbuf.clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            one = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            closed = marked = reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        public synchronized int read (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            return read (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            int result = read (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                return one[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        public synchronized int read (byte[] b, int off, int srclen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            int canreturn, willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                throw new IOException ("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   266
            assert channel.isBlocking();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   267
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   268
            if (off < 0 || srclen < 0|| srclen > (b.length-off)) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   269
                throw new IndexOutOfBoundsException ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   270
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   271
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (reset) { /* satisfy from markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                canreturn = markBuf.remaining ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                markBuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                if (canreturn == willreturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                    reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            } else { /* satisfy from channel */
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   280
                chanbuf.clear ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   281
                if (srclen <  BUFSIZE) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   282
                    chanbuf.limit (srclen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   284
                do {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   285
                    willreturn = channel.read (chanbuf);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   286
                } while (willreturn == 0);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   287
                if (willreturn == -1) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   288
                    eof = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   291
                chanbuf.flip ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                chanbuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                if (marked) { /* copy into markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                        markBuf.put (b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    } catch (BufferOverflowException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                        marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            return willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   305
        public boolean markSupported () {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   306
            return true;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   307
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   308
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   309
        /* Does not query the OS socket */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        public synchronized int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            if (eof)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return markBuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   320
            return chanbuf.remaining();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        public synchronized void mark (int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            this.readlimit = readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            markBuf = ByteBuffer.allocate (readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        public synchronized void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (closed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (!marked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                throw new IOException ("Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            reset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            markBuf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static class WriteStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            this.server = server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   362
            assert channel.isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            buf = ByteBuffer.allocate (4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        public synchronized void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            one[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            write (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        public synchronized void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        public synchronized void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            int l = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                throw new IOException ("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            int cap = buf.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            if (cap < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                int diff = len - cap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                buf = ByteBuffer.allocate (2*(cap+diff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            buf.put (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            buf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            while ((n = channel.write (buf)) < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                l -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                if (l == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                return;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   401
            //server.logStackTrace ("Request.OS.close: isOpen="+channel.isOpen());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
}