jdk/src/share/classes/sun/net/httpserver/Request.java
author chegar
Fri, 16 Sep 2011 12:09:04 -0700
changeset 10596 39b3a979e600
parent 7668 d4a77089c587
child 11902 a94ba35d9c4a
permissions -rw-r--r--
7090158: Networking Libraries don't build with javac -Werror Summary: Minor changes to networking java files to remove warnings Reviewed-by: chegar, weijun, hawtin Contributed-by: kurchi.subhra.hazra@oracle.com, sasha_bu@hotmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
10596
39b3a979e600 7090158: Networking Libraries don't build with javac -Werror
chegar
parents: 7668
diff changeset
     2
 * Copyright (c) 2005, 2011, 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);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            hdrs.add (k,v);
7537
b7d2d2428f31 7005016: sqe test jhttp/HttpServer150013/HttpServer150013.java
michaelm
parents: 7271
diff changeset
   204
            len = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        return hdrs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * Implements blocking reading semantics on top of a non-blocking channel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    static class ReadStream extends InputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        ByteBuffer chanbuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        byte[] one;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   217
        private boolean closed = false, eof = false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        ByteBuffer markBuf; /* reads may be satisifed from this buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        boolean marked;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        boolean reset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        int readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        static long readTimeout;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        ServerImpl server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   224
        final static int BUFSIZE = 8 * 1024;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            this.channel = chan;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            this.server = server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   229
            chanbuf = ByteBuffer.allocate (BUFSIZE);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   230
            chanbuf.clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            one = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            closed = marked = reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        public synchronized int read (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            return read (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public synchronized int read () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            int result = read (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            if (result == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                return one[0] & 0xFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        public synchronized int read (byte[] b, int off, int srclen) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            int canreturn, willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                throw new IOException ("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            if (eof) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   259
            assert channel.isBlocking();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   260
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   261
            if (off < 0 || srclen < 0|| srclen > (b.length-off)) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   262
                throw new IndexOutOfBoundsException ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   263
            }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   264
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (reset) { /* satisfy from markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                canreturn = markBuf.remaining ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                willreturn = canreturn>srclen ? srclen : canreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                markBuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                if (canreturn == willreturn) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            } else { /* satisfy from channel */
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   273
                chanbuf.clear ();
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   274
                if (srclen <  BUFSIZE) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   275
                    chanbuf.limit (srclen);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   277
                do {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   278
                    willreturn = channel.read (chanbuf);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   279
                } while (willreturn == 0);
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   280
                if (willreturn == -1) {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   281
                    eof = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   284
                chanbuf.flip ();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                chanbuf.get(b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                if (marked) { /* copy into markBuf */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                        markBuf.put (b, off, willreturn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                    } catch (BufferOverflowException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                        marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return willreturn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   298
        public boolean markSupported () {
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   299
            return true;
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   300
        }
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   301
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   302
        /* Does not query the OS socket */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        public synchronized int available () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                throw new IOException ("Stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            if (eof)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            if (reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                return markBuf.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   313
            return chanbuf.remaining();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (closed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        public synchronized void mark (int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            this.readlimit = readlimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            markBuf = ByteBuffer.allocate (readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            marked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            reset = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        public synchronized void reset () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            if (closed )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if (!marked)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                throw new IOException ("Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            marked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            reset = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            markBuf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    static class WriteStream extends java.io.OutputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        SocketChannel channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        ByteBuffer buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        SelectionKey key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        boolean closed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        byte[] one;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        ServerImpl server;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            this.channel = channel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            this.server = server;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   355
            assert channel.isBlocking();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            closed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            one = new byte [1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            buf = ByteBuffer.allocate (4096);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        public synchronized void write (int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            one[0] = (byte)b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            write (one, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        public synchronized void write (byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            write (b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        public synchronized void write (byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            int l = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                throw new IOException ("stream is closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            int cap = buf.capacity();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (cap < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                int diff = len - cap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                buf = ByteBuffer.allocate (2*(cap+diff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            buf.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            buf.put (b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            buf.flip ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            while ((n = channel.write (buf)) < l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                l -= n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                if (l == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        public void close () throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
            if (closed)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                return;
7271
17d3fc18872d 6725892: Http server stability issues
michaelm
parents: 5506
diff changeset
   394
            //server.logStackTrace ("Request.OS.close: isOpen="+channel.isOpen());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            channel.close ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            closed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
}