Path: blob/master/src/jdk.httpserver/share/classes/sun/net/httpserver/Request.java
66646 views
/*1* Copyright (c) 2005, 2021, 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}208if (k == null) { // Headers disallows null keys, use empty string209k = ""; // instead to represent invalid key210}211hdrs.add (k,v);212len = 0;213}214return hdrs;215}216217/**218* Implements blocking reading semantics on top of a non-blocking channel219*/220221static class ReadStream extends InputStream {222SocketChannel channel;223ByteBuffer chanbuf;224byte[] one;225private boolean closed = false, eof = false;226ByteBuffer markBuf; /* reads may be satisfied from this buffer */227boolean marked;228boolean reset;229int readlimit;230static long readTimeout;231ServerImpl server;232final static int BUFSIZE = 8 * 1024;233234public ReadStream (ServerImpl server, SocketChannel chan) throws IOException {235this.channel = chan;236this.server = server;237chanbuf = ByteBuffer.allocate (BUFSIZE);238chanbuf.clear();239one = new byte[1];240closed = marked = reset = false;241}242243public synchronized int read (byte[] b) throws IOException {244return read (b, 0, b.length);245}246247public synchronized int read () throws IOException {248int result = read (one, 0, 1);249if (result == 1) {250return one[0] & 0xFF;251} else {252return -1;253}254}255256public synchronized int read (byte[] b, int off, int srclen) throws IOException {257258int canreturn, willreturn;259260if (closed)261throw new IOException ("Stream closed");262263if (eof) {264return -1;265}266267assert channel.isBlocking();268269if (off < 0 || srclen < 0|| srclen > (b.length-off)) {270throw new IndexOutOfBoundsException ();271}272273if (reset) { /* satisfy from markBuf */274canreturn = markBuf.remaining ();275willreturn = canreturn>srclen ? srclen : canreturn;276markBuf.get(b, off, willreturn);277if (canreturn == willreturn) {278reset = false;279}280} else { /* satisfy from channel */281chanbuf.clear ();282if (srclen < BUFSIZE) {283chanbuf.limit (srclen);284}285do {286willreturn = channel.read (chanbuf);287} while (willreturn == 0);288if (willreturn == -1) {289eof = true;290return -1;291}292chanbuf.flip ();293chanbuf.get(b, off, willreturn);294295if (marked) { /* copy into markBuf */296try {297markBuf.put (b, off, willreturn);298} catch (BufferOverflowException e) {299marked = false;300}301}302}303return willreturn;304}305306public boolean markSupported () {307return true;308}309310/* Does not query the OS socket */311public synchronized int available () throws IOException {312if (closed)313throw new IOException ("Stream is closed");314315if (eof)316return -1;317318if (reset)319return markBuf.remaining();320321return chanbuf.remaining();322}323324public void close () throws IOException {325if (closed) {326return;327}328channel.close ();329closed = true;330}331332public synchronized void mark (int readlimit) {333if (closed)334return;335this.readlimit = readlimit;336markBuf = ByteBuffer.allocate (readlimit);337marked = true;338reset = false;339}340341public synchronized void reset () throws IOException {342if (closed )343return;344if (!marked)345throw new IOException ("Stream not marked");346marked = false;347reset = true;348markBuf.flip ();349}350}351352static class WriteStream extends java.io.OutputStream {353SocketChannel channel;354ByteBuffer buf;355SelectionKey key;356boolean closed;357byte[] one;358ServerImpl server;359360public WriteStream (ServerImpl server, SocketChannel channel) throws IOException {361this.channel = channel;362this.server = server;363assert channel.isBlocking();364closed = false;365one = new byte [1];366buf = ByteBuffer.allocate (4096);367}368369public synchronized void write (int b) throws IOException {370one[0] = (byte)b;371write (one, 0, 1);372}373374public synchronized void write (byte[] b) throws IOException {375write (b, 0, b.length);376}377378public synchronized void write (byte[] b, int off, int len) throws IOException {379int l = len;380if (closed)381throw new IOException ("stream is closed");382383int cap = buf.capacity();384if (cap < len) {385int diff = len - cap;386buf = ByteBuffer.allocate (2*(cap+diff));387}388buf.clear();389buf.put (b, off, len);390buf.flip ();391int n;392while ((n = channel.write (buf)) < l) {393l -= n;394if (l == 0)395return;396}397}398399public void close () throws IOException {400if (closed)401return;402//server.logStackTrace ("Request.OS.close: isOpen="+channel.isOpen());403channel.close ();404closed = true;405}406}407}408409410