src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEParser.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/xml/internal/org/jvnet/mimepull/MIMEParser.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,502 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.xml.internal.org.jvnet.mimepull;
    1.30 +
    1.31 +import java.io.InputStream;
    1.32 +import java.io.IOException;
    1.33 +import java.util.*;
    1.34 +import java.util.logging.Logger;
    1.35 +import java.nio.ByteBuffer;
    1.36 +import java.util.logging.Level;
    1.37 +
    1.38 +/**
    1.39 + * Pull parser for the MIME messages. Applications can use pull API to continue
    1.40 + * the parsing MIME messages lazily.
    1.41 + *
    1.42 + * <pre>
    1.43 + * for e.g.:
    1.44 + * <p>
    1.45 + *
    1.46 + * MIMEParser parser = ...
    1.47 + * Iterator<MIMEEvent> it = parser.iterator();
    1.48 + * while(it.hasNext()) {
    1.49 + *   MIMEEvent event = it.next();
    1.50 + *   ...
    1.51 + * }
    1.52 + * </pre>
    1.53 + *
    1.54 + * @author Jitendra Kotamraju
    1.55 + */
    1.56 +class MIMEParser implements Iterable<MIMEEvent> {
    1.57 +
    1.58 +    private static final Logger LOGGER = Logger.getLogger(MIMEParser.class.getName());
    1.59 +
    1.60 +    private static final String HEADER_ENCODING = "ISO8859-1";
    1.61 +
    1.62 +    // Actually, the grammar doesn't support whitespace characters
    1.63 +    // after boundary. But the mail implementation checks for it.
    1.64 +    // We will only check for these many whitespace characters after boundary
    1.65 +    private static final int NO_LWSP = 1000;
    1.66 +    private enum STATE {START_MESSAGE, SKIP_PREAMBLE, START_PART, HEADERS, BODY, END_PART, END_MESSAGE}
    1.67 +    private STATE state = STATE.START_MESSAGE;
    1.68 +
    1.69 +    private final InputStream in;
    1.70 +    private final byte[] bndbytes;
    1.71 +    private final int bl;
    1.72 +    private final MIMEConfig config;
    1.73 +    private final int[] bcs = new int[128]; // BnM algo: Bad Character Shift table
    1.74 +    private final int[] gss;                // BnM algo : Good Suffix Shift table
    1.75 +
    1.76 +    /**
    1.77 +     * Have we parsed the data from our InputStream yet?
    1.78 +     */
    1.79 +    private boolean parsed;
    1.80 +
    1.81 +    /*
    1.82 +     * Read and process body partsList until we see the
    1.83 +     * terminating boundary line (or EOF).
    1.84 +     */
    1.85 +    private boolean done = false;
    1.86 +
    1.87 +    private boolean eof;
    1.88 +    private final int capacity;
    1.89 +    private byte[] buf;
    1.90 +    private int len;
    1.91 +    private boolean bol;        // beginning of the line
    1.92 +
    1.93 +    /*
    1.94 +     * Parses the MIME content. At the EOF, it also closes input stream
    1.95 +     */
    1.96 +    MIMEParser(InputStream in, String boundary, MIMEConfig config) {
    1.97 +        this.in = in;
    1.98 +        this.bndbytes = getBytes("--"+boundary);
    1.99 +        bl = bndbytes.length;
   1.100 +        this.config = config;
   1.101 +        gss = new int[bl];
   1.102 +        compileBoundaryPattern();
   1.103 +
   1.104 +        // \r\n + boundary + "--\r\n" + lots of LWSP
   1.105 +        capacity = config.chunkSize+2+bl+4+NO_LWSP;
   1.106 +        createBuf(capacity);
   1.107 +    }
   1.108 +
   1.109 +    /**
   1.110 +     * Returns iterator for the parsing events. Use the iterator to advance
   1.111 +     * the parsing.
   1.112 +     *
   1.113 +     * @return iterator for parsing events
   1.114 +     */
   1.115 +    @Override
   1.116 +    public Iterator<MIMEEvent> iterator() {
   1.117 +        return new MIMEEventIterator();
   1.118 +    }
   1.119 +
   1.120 +    class MIMEEventIterator implements Iterator<MIMEEvent> {
   1.121 +
   1.122 +        @Override
   1.123 +        public boolean hasNext() {
   1.124 +            return !parsed;
   1.125 +        }
   1.126 +
   1.127 +        @Override
   1.128 +        public MIMEEvent next() {
   1.129 +            switch(state) {
   1.130 +                case START_MESSAGE :
   1.131 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.START_MESSAGE);}
   1.132 +                    state = STATE.SKIP_PREAMBLE;
   1.133 +                    return MIMEEvent.START_MESSAGE;
   1.134 +
   1.135 +                case SKIP_PREAMBLE :
   1.136 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.SKIP_PREAMBLE);}
   1.137 +                    skipPreamble();
   1.138 +                    // fall through
   1.139 +                case START_PART :
   1.140 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.START_PART);}
   1.141 +                    state = STATE.HEADERS;
   1.142 +                    return MIMEEvent.START_PART;
   1.143 +
   1.144 +                case HEADERS :
   1.145 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.HEADERS);}
   1.146 +                    InternetHeaders ih = readHeaders();
   1.147 +                    state = STATE.BODY;
   1.148 +                    bol = true;
   1.149 +                    return new MIMEEvent.Headers(ih);
   1.150 +
   1.151 +                case BODY :
   1.152 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.BODY);}
   1.153 +                    ByteBuffer buf = readBody();
   1.154 +                    bol = false;
   1.155 +                    return new MIMEEvent.Content(buf);
   1.156 +
   1.157 +                case END_PART :
   1.158 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.END_PART);}
   1.159 +                    if (done) {
   1.160 +                        state = STATE.END_MESSAGE;
   1.161 +                    } else {
   1.162 +                        state = STATE.START_PART;
   1.163 +                    }
   1.164 +                    return MIMEEvent.END_PART;
   1.165 +
   1.166 +                case END_MESSAGE :
   1.167 +                    if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "MIMEParser state={0}", STATE.END_MESSAGE);}
   1.168 +                    parsed = true;
   1.169 +                    return MIMEEvent.END_MESSAGE;
   1.170 +
   1.171 +                default :
   1.172 +                    throw new MIMEParsingException("Unknown Parser state = "+state);
   1.173 +            }
   1.174 +        }
   1.175 +
   1.176 +        @Override
   1.177 +        public void remove() {
   1.178 +            throw new UnsupportedOperationException();
   1.179 +        }
   1.180 +    }
   1.181 +
   1.182 +    /**
   1.183 +     * Collects the headers for the current part by parsing mesage stream.
   1.184 +     *
   1.185 +     * @return headers for the current part
   1.186 +     */
   1.187 +    private InternetHeaders readHeaders() {
   1.188 +        if (!eof) {
   1.189 +            fillBuf();
   1.190 +        }
   1.191 +        return new InternetHeaders(new LineInputStream());
   1.192 +    }
   1.193 +
   1.194 +    /**
   1.195 +     * Reads and saves the part of the current attachment part's content.
   1.196 +     * At the end of this method, buf should have the remaining data
   1.197 +     * at index 0.
   1.198 +     *
   1.199 +     * @return a chunk of the part's content
   1.200 +     *
   1.201 +     */
   1.202 +    private ByteBuffer readBody() {
   1.203 +        if (!eof) {
   1.204 +            fillBuf();
   1.205 +        }
   1.206 +        int start = match(buf, 0, len);     // matches boundary
   1.207 +        if (start == -1) {
   1.208 +            // No boundary is found
   1.209 +            assert eof || len >= config.chunkSize;
   1.210 +            int chunkSize = eof ? len : config.chunkSize;
   1.211 +            if (eof) {
   1.212 +                done = true;
   1.213 +                throw new MIMEParsingException("Reached EOF, but there is no closing MIME boundary.");
   1.214 +            }
   1.215 +            return adjustBuf(chunkSize, len-chunkSize);
   1.216 +        }
   1.217 +        // Found boundary.
   1.218 +        // Is it at the start of a line ?
   1.219 +        int chunkLen = start;
   1.220 +        if (bol && start == 0) {
   1.221 +            // nothing to do
   1.222 +        } else if (start > 0 && (buf[start-1] == '\n' || buf[start-1] =='\r')) {
   1.223 +            --chunkLen;
   1.224 +            if (buf[start-1] == '\n' && start >1 && buf[start-2] == '\r') {
   1.225 +                --chunkLen;
   1.226 +            }
   1.227 +        } else {
   1.228 +           return adjustBuf(start+1, len-start-1);  // boundary is not at beginning of a line
   1.229 +        }
   1.230 +
   1.231 +        if (start+bl+1 < len && buf[start+bl] == '-' && buf[start+bl+1] == '-') {
   1.232 +            state = STATE.END_PART;
   1.233 +            done = true;
   1.234 +            return adjustBuf(chunkLen, 0);
   1.235 +        }
   1.236 +
   1.237 +        // Consider all the whitespace in boundary+whitespace+"\r\n"
   1.238 +        int lwsp = 0;
   1.239 +        for(int i=start+bl; i < len && (buf[i] == ' ' || buf[i] == '\t'); i++) {
   1.240 +            ++lwsp;
   1.241 +        }
   1.242 +
   1.243 +        // Check for \n or \r\n in boundary+whitespace+"\n" or boundary+whitespace+"\r\n"
   1.244 +        if (start+bl+lwsp < len && buf[start+bl+lwsp] == '\n') {
   1.245 +            state = STATE.END_PART;
   1.246 +            return adjustBuf(chunkLen, len-start-bl-lwsp-1);
   1.247 +        } else if (start+bl+lwsp+1 < len && buf[start+bl+lwsp] == '\r' && buf[start+bl+lwsp+1] == '\n') {
   1.248 +            state = STATE.END_PART;
   1.249 +            return adjustBuf(chunkLen, len-start-bl-lwsp-2);
   1.250 +        } else if (start+bl+lwsp+1 < len) {
   1.251 +            return adjustBuf(chunkLen+1, len-chunkLen-1);       // boundary string in a part data
   1.252 +        } else if (eof) {
   1.253 +            done = true;
   1.254 +            throw new MIMEParsingException("Reached EOF, but there is no closing MIME boundary.");
   1.255 +        }
   1.256 +
   1.257 +        // Some more data needed to determine if it is indeed a proper boundary
   1.258 +        return adjustBuf(chunkLen, len-chunkLen);
   1.259 +    }
   1.260 +
   1.261 +    /**
   1.262 +     * Returns a chunk from the original buffer. A new buffer is
   1.263 +     * created with the remaining bytes.
   1.264 +     *
   1.265 +     * @param chunkSize create a chunk with these many bytes
   1.266 +     * @param remaining bytes from the end of the buffer that need to be copied to
   1.267 +     *        the beginning of the new buffer
   1.268 +     * @return chunk
   1.269 +     */
   1.270 +    private ByteBuffer adjustBuf(int chunkSize, int remaining) {
   1.271 +        assert buf != null;
   1.272 +        assert chunkSize >= 0;
   1.273 +        assert remaining >= 0;
   1.274 +
   1.275 +        byte[] temp = buf;
   1.276 +        // create a new buf and adjust it without this chunk
   1.277 +        createBuf(remaining);
   1.278 +        System.arraycopy(temp, len-remaining, buf, 0, remaining);
   1.279 +        len = remaining;
   1.280 +
   1.281 +        return ByteBuffer.wrap(temp, 0, chunkSize);
   1.282 +    }
   1.283 +
   1.284 +    private void createBuf(int min) {
   1.285 +        buf = new byte[min < capacity ? capacity : min];
   1.286 +    }
   1.287 +
   1.288 +    /**
   1.289 +     * Skips the preamble to find the first attachment part
   1.290 +     */
   1.291 +    private void skipPreamble() {
   1.292 +
   1.293 +        while(true) {
   1.294 +            if (!eof) {
   1.295 +                fillBuf();
   1.296 +            }
   1.297 +            int start = match(buf, 0, len);     // matches boundary
   1.298 +            if (start == -1) {
   1.299 +                // No boundary is found
   1.300 +                if (eof) {
   1.301 +                    throw new MIMEParsingException("Missing start boundary");
   1.302 +                } else {
   1.303 +                    adjustBuf(len-bl+1, bl-1);
   1.304 +                    continue;
   1.305 +                }
   1.306 +            }
   1.307 +
   1.308 +            if (start > config.chunkSize) {
   1.309 +                adjustBuf(start, len-start);
   1.310 +                continue;
   1.311 +            }
   1.312 +            // Consider all the whitespace boundary+whitespace+"\r\n"
   1.313 +            int lwsp = 0;
   1.314 +            for(int i=start+bl; i < len && (buf[i] == ' ' || buf[i] == '\t'); i++) {
   1.315 +                ++lwsp;
   1.316 +            }
   1.317 +            // Check for \n or \r\n
   1.318 +            if (start+bl+lwsp < len && (buf[start+bl+lwsp] == '\n' || buf[start+bl+lwsp] == '\r') ) {
   1.319 +                if (buf[start+bl+lwsp] == '\n') {
   1.320 +                    adjustBuf(start+bl+lwsp+1, len-start-bl-lwsp-1);
   1.321 +                    break;
   1.322 +                } else if (start+bl+lwsp+1 < len && buf[start+bl+lwsp+1] == '\n') {
   1.323 +                    adjustBuf(start+bl+lwsp+2, len-start-bl-lwsp-2);
   1.324 +                    break;
   1.325 +                }
   1.326 +            }
   1.327 +            adjustBuf(start+1, len-start-1);
   1.328 +        }
   1.329 +        if (LOGGER.isLoggable(Level.FINE)) {LOGGER.log(Level.FINE, "Skipped the preamble. buffer len={0}", len);}
   1.330 +    }
   1.331 +
   1.332 +    private static byte[] getBytes(String s) {
   1.333 +        char [] chars= s.toCharArray();
   1.334 +        int size = chars.length;
   1.335 +        byte[] bytes = new byte[size];
   1.336 +
   1.337 +        for (int i = 0; i < size;) {
   1.338 +            bytes[i] = (byte) chars[i++];
   1.339 +        }
   1.340 +        return bytes;
   1.341 +    }
   1.342 +
   1.343 +        /**
   1.344 +     * Boyer-Moore search method. Copied from java.util.regex.Pattern.java
   1.345 +     *
   1.346 +     * Pre calculates arrays needed to generate the bad character
   1.347 +     * shift and the good suffix shift. Only the last seven bits
   1.348 +     * are used to see if chars match; This keeps the tables small
   1.349 +     * and covers the heavily used ASCII range, but occasionally
   1.350 +     * results in an aliased match for the bad character shift.
   1.351 +     */
   1.352 +    private void compileBoundaryPattern() {
   1.353 +        int i, j;
   1.354 +
   1.355 +        // Precalculate part of the bad character shift
   1.356 +        // It is a table for where in the pattern each
   1.357 +        // lower 7-bit value occurs
   1.358 +        for (i = 0; i < bndbytes.length; i++) {
   1.359 +            bcs[bndbytes[i]&0x7F] = i + 1;
   1.360 +        }
   1.361 +
   1.362 +        // Precalculate the good suffix shift
   1.363 +        // i is the shift amount being considered
   1.364 +NEXT:   for (i = bndbytes.length; i > 0; i--) {
   1.365 +            // j is the beginning index of suffix being considered
   1.366 +            for (j = bndbytes.length - 1; j >= i; j--) {
   1.367 +                // Testing for good suffix
   1.368 +                if (bndbytes[j] == bndbytes[j-i]) {
   1.369 +                    // src[j..len] is a good suffix
   1.370 +                    gss[j-1] = i;
   1.371 +                } else {
   1.372 +                    // No match. The array has already been
   1.373 +                    // filled up with correct values before.
   1.374 +                    continue NEXT;
   1.375 +                }
   1.376 +            }
   1.377 +            // This fills up the remaining of optoSft
   1.378 +            // any suffix can not have larger shift amount
   1.379 +            // then its sub-suffix. Why???
   1.380 +            while (j > 0) {
   1.381 +                gss[--j] = i;
   1.382 +            }
   1.383 +        }
   1.384 +        // Set the guard value because of unicode compression
   1.385 +        gss[bndbytes.length -1] = 1;
   1.386 +    }
   1.387 +
   1.388 +    /**
   1.389 +     * Finds the boundary in the given buffer using Boyer-Moore algo.
   1.390 +     * Copied from java.util.regex.Pattern.java
   1.391 +     *
   1.392 +     * @param mybuf boundary to be searched in this mybuf
   1.393 +     * @param off start index in mybuf
   1.394 +     * @param len number of bytes in mybuf
   1.395 +     *
   1.396 +     * @return -1 if there is no match or index where the match starts
   1.397 +     */
   1.398 +    private int match(byte[] mybuf, int off, int len) {
   1.399 +        int last = len - bndbytes.length;
   1.400 +
   1.401 +        // Loop over all possible match positions in text
   1.402 +NEXT:   while (off <= last) {
   1.403 +            // Loop over pattern from right to left
   1.404 +            for (int j = bndbytes.length - 1; j >= 0; j--) {
   1.405 +                byte ch = mybuf[off+j];
   1.406 +                if (ch != bndbytes[j]) {
   1.407 +                    // Shift search to the right by the maximum of the
   1.408 +                    // bad character shift and the good suffix shift
   1.409 +                    off += Math.max(j + 1 - bcs[ch&0x7F], gss[j]);
   1.410 +                    continue NEXT;
   1.411 +                }
   1.412 +            }
   1.413 +            // Entire pattern matched starting at off
   1.414 +            return off;
   1.415 +        }
   1.416 +        return -1;
   1.417 +    }
   1.418 +
   1.419 +    /**
   1.420 +     * Fills the remaining buf to the full capacity
   1.421 +     */
   1.422 +    private void fillBuf() {
   1.423 +        if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "Before fillBuf() buffer len={0}", len);}
   1.424 +        assert !eof;
   1.425 +        while(len < buf.length) {
   1.426 +            int read;
   1.427 +            try {
   1.428 +                read = in.read(buf, len, buf.length-len);
   1.429 +            } catch(IOException ioe) {
   1.430 +                throw new MIMEParsingException(ioe);
   1.431 +            }
   1.432 +            if (read == -1) {
   1.433 +                eof = true;
   1.434 +                try {
   1.435 +                    if (LOGGER.isLoggable(Level.FINE)) {LOGGER.fine("Closing the input stream.");}
   1.436 +                    in.close();
   1.437 +                } catch(IOException ioe) {
   1.438 +                    throw new MIMEParsingException(ioe);
   1.439 +                }
   1.440 +                break;
   1.441 +            } else {
   1.442 +                len += read;
   1.443 +            }
   1.444 +        }
   1.445 +        if (LOGGER.isLoggable(Level.FINER)) {LOGGER.log(Level.FINER, "After fillBuf() buffer len={0}", len);}
   1.446 +    }
   1.447 +
   1.448 +    private void doubleBuf() {
   1.449 +        byte[] temp = new byte[2*len];
   1.450 +        System.arraycopy(buf, 0, temp, 0, len);
   1.451 +        buf = temp;
   1.452 +        if (!eof) {
   1.453 +            fillBuf();
   1.454 +        }
   1.455 +    }
   1.456 +
   1.457 +    class LineInputStream {
   1.458 +        private int offset;
   1.459 +
   1.460 +        /*
   1.461 +         * Read a line containing only ASCII characters from the input
   1.462 +         * stream. A line is terminated by a CR or NL or CR-NL sequence.
   1.463 +         * A common error is a CR-CR-NL sequence, which will also terminate
   1.464 +         * a line.
   1.465 +         * The line terminator is not returned as part of the returned
   1.466 +         * String. Returns null if no data is available. <p>
   1.467 +         *
   1.468 +         * This class is similar to the deprecated
   1.469 +         * <code>DataInputStream.readLine()</code>
   1.470 +         */
   1.471 +        public String readLine() throws IOException {
   1.472 +
   1.473 +            int hdrLen = 0;
   1.474 +            int lwsp = 0;
   1.475 +            while(offset+hdrLen < len) {
   1.476 +                if (buf[offset+hdrLen] == '\n') {
   1.477 +                    lwsp = 1;
   1.478 +                    break;
   1.479 +                }
   1.480 +                if (offset+hdrLen+1 == len) {
   1.481 +                    doubleBuf();
   1.482 +                }
   1.483 +                if (offset+hdrLen+1 >= len) {   // No more data in the stream
   1.484 +                    assert eof;
   1.485 +                    return null;
   1.486 +                }
   1.487 +                if (buf[offset+hdrLen] == '\r' && buf[offset+hdrLen+1] == '\n') {
   1.488 +                    lwsp = 2;
   1.489 +                    break;
   1.490 +                }
   1.491 +                ++hdrLen;
   1.492 +            }
   1.493 +            if (hdrLen == 0) {
   1.494 +                adjustBuf(offset+lwsp, len-offset-lwsp);
   1.495 +                return null;
   1.496 +            }
   1.497 +
   1.498 +            String hdr = new String(buf, offset, hdrLen, HEADER_ENCODING);
   1.499 +            offset += hdrLen+lwsp;
   1.500 +            return hdr;
   1.501 +        }
   1.502 +
   1.503 +    }
   1.504 +
   1.505 +}

mercurial