Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/Request.java
41159 views
/*1* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.net.httpserver;2627import java.nio.*;28import java.io.*;29import java.nio.channels.*;30import com.sun.net.httpserver.*;3132/**33*/34class Request {3536final static int BUF_LEN = 2048;37final static byte CR = 13;38final static byte LF = 10;3940private String startLine;41private SocketChannel chan;42private InputStream is;43private OutputStream os;4445Request (InputStream rawInputStream, OutputStream rawout) throws IOException {46is = rawInputStream;47os = rawout;48do {49startLine = readLine();50if (startLine == null) {51return;52}53/* skip blank lines */54} while (startLine == null ? false : startLine.equals (""));55}565758char[] buf = new char [BUF_LEN];59int pos;60StringBuffer lineBuf;6162public InputStream inputStream () {63return is;64}6566public OutputStream outputStream () {67return os;68}6970/**71* read a line from the stream returning as a String.72* Not used for reading headers.73*/7475public String readLine () throws IOException {76boolean gotCR = false, gotLF = false;77pos = 0; lineBuf = new StringBuffer();78while (!gotLF) {79int c = is.read();80if (c == -1) {81return null;82}83if (gotCR) {84if (c == LF) {85gotLF = true;86} else {87gotCR = false;88consume (CR);89consume (c);90}91} else {92if (c == CR) {93gotCR = true;94} else {95consume (c);96}97}98}99lineBuf.append (buf, 0, pos);100return new String (lineBuf);101}102103private void consume (int c) {104if (pos == BUF_LEN) {105lineBuf.append (buf);106pos = 0;107}108buf[pos++] = (char)c;109}110111/**112* returns the request line (first line of a request)113*/114public String requestLine () {115return startLine;116}117118Headers hdrs = null;119@SuppressWarnings("fallthrough")120Headers headers () throws IOException {121if (hdrs != null) {122return hdrs;123}124hdrs = new Headers();125126char s[] = new char[10];127int len = 0;128129int firstc = is.read();130131// check for empty headers132if (firstc == CR || firstc == LF) {133int c = is.read();134if (c == CR || c == LF) {135return hdrs;136}137s[0] = (char)firstc;138len = 1;139firstc = c;140}141142while (firstc != LF && firstc != CR && firstc >= 0) {143int keyend = -1;144int c;145boolean inKey = firstc > ' ';146s[len++] = (char) firstc;147parseloop:{148while ((c = is.read()) >= 0) {149switch (c) {150/*fallthrough*/151case ':':152if (inKey && len > 0)153keyend = len;154inKey = false;155break;156case '\t':157c = ' ';158case ' ':159inKey = false;160break;161case CR:162case LF:163firstc = is.read();164if (c == CR && firstc == LF) {165firstc = is.read();166if (firstc == CR)167firstc = is.read();168}169if (firstc == LF || firstc == CR || firstc > ' ')170break parseloop;171/* continuation */172c = ' ';173break;174}175if (len >= s.length) {176char ns[] = new char[s.length * 2];177System.arraycopy(s, 0, ns, 0, len);178s = ns;179}180s[len++] = (char) c;181}182firstc = -1;183}184while (len > 0 && s[len - 1] <= ' ')185len--;186String k;187if (keyend <= 0) {188k = null;189keyend = 0;190} else {191k = String.copyValueOf(s, 0, keyend);192if (keyend < len && s[keyend] == ':')193keyend++;194while (keyend < len && s[keyend] <= ' ')195keyend++;196}197String v;198if (keyend >= len)199v = new String();200else201v = String.copyValueOf(s, keyend, len - keyend);202203if (hdrs.size() >= ServerConfig.getMaxReqHeaders()) {204throw new IOException("Maximum number of request headers (" +205"sun.net.httpserver.maxReqHeaders) exceeded, " +206ServerConfig.getMaxReqHeaders() + ".");207}208209hdrs.add (k,v);210len = 0;211}212return hdrs;213}214215/**216* Implements blocking reading semantics on top of a non-blocking channel217*/218219static class ReadStream extends InputStream {220SocketChannel channel;221ByteBuffer chanbuf;222byte[] one;223private boolean closed = false, eof = false;224ByteBuffer markBuf; /* reads may be satisfied from this buffer */225boolean marked;226boolean reset;227int readlimit;228static long readTimeout;229ServerImpl server;230final static int BUFSIZE = 8 * 1024;231232public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {233this.channel = chan;234this.server = server;235chanbuf = ByteBuffer.allocate (BUFSIZE);236chanbuf.clear();237one = new byte[1];238closed = marked = reset = false;239}240241public synchronized int read (byte[] b) throws IOException {242return read (b, 0, b.length);243}244245public synchronized int read () throws IOException {246int result = read (one, 0, 1);247if (result == 1) {248return one[0] & 0xFF;249} else {250return -1;251}252}253254public synchronized int read (byte[] b, int off, int srclen) throws IOException {255256int canreturn, willreturn;257258if (closed)259throw new IOException ("Stream closed");260261if (eof) {262return -1;263}264265assert channel.isBlocking();266267if (off < 0 || srclen < 0|| srclen > (b.length-off)) {268throw new IndexOutOfBoundsException ();269}270271if (reset) { /* satisfy from markBuf */272canreturn = markBuf.remaining ();273willreturn = canreturn>srclen ? srclen : canreturn;274markBuf.get(b, off, willreturn);275if (canreturn == willreturn) {276reset = false;277}278} else { /* satisfy from channel */279chanbuf.clear ();280if (srclen < BUFSIZE) {281chanbuf.limit (srclen);282}283do {284willreturn = channel.read (chanbuf);285} while (willreturn == 0);286if (willreturn == -1) {287eof = true;288return -1;289}290chanbuf.flip ();291chanbuf.get(b, off, willreturn);292293if (marked) { /* copy into markBuf */294try {295markBuf.put (b, off, willreturn);296} catch (BufferOverflowException e) {297marked = false;298}299}300}301return willreturn;302}303304public boolean markSupported () {305return true;306}307308/* Does not query the OS socket */309public synchronized int available () throws IOException {310if (closed)311throw new IOException ("Stream is closed");312313if (eof)314return -1;315316if (reset)317return markBuf.remaining();318319return chanbuf.remaining();320}321322public void close () throws IOException {323if (closed) {324return;325}326channel.close ();327closed = true;328}329330public synchronized void mark (int readlimit) {331if (closed)332return;333this.readlimit = readlimit;334markBuf = ByteBuffer.allocate (readlimit);335marked = true;336reset = false;337}338339public synchronized void reset () throws IOException {340if (closed )341return;342if (!marked)343throw new IOException ("Stream not marked");344marked = false;345reset = true;346markBuf.flip ();347}348}349350static class WriteStream extends java.io.OutputStream {351SocketChannel channel;352ByteBuffer buf;353SelectionKey key;354boolean closed;355byte[] one;356ServerImpl server;357358public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {359this.channel = channel;360this.server = server;361assert channel.isBlocking();362closed = false;363one = new byte [1];364buf = ByteBuffer.allocate (4096);365}366367public synchronized void write (int b) throws IOException {368one[0] = (byte)b;369write (one, 0, 1);370}371372public synchronized void write (byte[] b) throws IOException {373write (b, 0, b.length);374}375376public synchronized void write (byte[] b, int off, int len) throws IOException {377int l = len;378if (closed)379throw new IOException ("stream is closed");380381int cap = buf.capacity();382if (cap < len) {383int diff = len - cap;384buf = ByteBuffer.allocate (2*(cap+diff));385}386buf.clear();387buf.put (b, off, len);388buf.flip ();389int n;390while ((n = channel.write (buf)) < l) {391l -= n;392if (l == 0)393return;394}395}396397public void close () throws IOException {398if (closed)399return;400//server.logStackTrace ("Request.OS.close: isOpen="+channel.isOpen());401channel.close ();402closed = true;403}404}405}406407408