summaryrefslogtreecommitdiff
path: root/java/com/android/voicemailomtp/src/org/apache/james/mime4j/MimeStreamParser.java
blob: a8aad5a38c8e5f0a83386ca0cac31f8a295743aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/

package org.apache.james.mime4j;

import org.apache.james.mime4j.decoder.Base64InputStream;
import org.apache.james.mime4j.decoder.QuotedPrintableInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.util.BitSet;
import java.util.LinkedList;

/**
 * <p>
 * Parses MIME (or RFC822) message streams of bytes or characters and reports
 * parsing events to a <code>ContentHandler</code> instance.
 * </p>
 * <p>
 * Typical usage:<br/>
 * <pre>
 *      ContentHandler handler = new MyHandler();
 *      MimeStreamParser parser = new MimeStreamParser();
 *      parser.setContentHandler(handler);
 *      parser.parse(new BufferedInputStream(new FileInputStream("mime.msg")));
 * </pre>
 * <strong>NOTE:</strong> All lines must end with CRLF
 * (<code>\r\n</code>). If you are unsure of the line endings in your stream
 * you should wrap it in a {@link org.apache.james.mime4j.EOLConvertingInputStream} instance.
 *
 *
 * @version $Id: MimeStreamParser.java,v 1.8 2005/02/11 10:12:02 ntherning Exp $
 */
public class MimeStreamParser {
    private static final Log log = LogFactory.getLog(MimeStreamParser.class);

    private static BitSet fieldChars = null;

    private RootInputStream rootStream = null;
    private LinkedList<BodyDescriptor> bodyDescriptors = new LinkedList<BodyDescriptor>();
    private ContentHandler handler = null;
    private boolean raw = false;
    private boolean prematureEof = false;

    static {
        fieldChars = new BitSet();
        for (int i = 0x21; i <= 0x39; i++) {
            fieldChars.set(i);
        }
        for (int i = 0x3b; i <= 0x7e; i++) {
            fieldChars.set(i);
        }
    }

    /**
     * Creates a new <code>MimeStreamParser</code> instance.
     */
    public MimeStreamParser() {
    }

    /**
     * Parses a stream of bytes containing a MIME message.
     *
     * @param is the stream to parse.
     * @throws IOException on I/O errors.
     */
    public void parse(InputStream is) throws IOException {
        rootStream = new RootInputStream(is);
        parseMessage(rootStream);
    }

    /**
     * Determines if this parser is currently in raw mode.
     *
     * @return <code>true</code> if in raw mode, <code>false</code>
     *         otherwise.
     * @see #setRaw(boolean)
     */
    public boolean isRaw() {
        return raw;
    }

    /**
     * Enables or disables raw mode. In raw mode all future entities
     * (messages or body parts) in the stream will be reported to the
     * {@link ContentHandler#raw(InputStream)} handler method only.
     * The stream will contain the entire unparsed entity contents
     * including header fields and whatever is in the body.
     *
     * @param raw <code>true</code> enables raw mode, <code>false</code>
     *        disables it.
     */
    public void setRaw(boolean raw) {
        this.raw = raw;
    }

    /**
     * Finishes the parsing and stops reading lines.
     * NOTE: No more lines will be parsed but the parser
     * will still call
     * {@link ContentHandler#endMultipart()},
     * {@link ContentHandler#endBodyPart()},
     * {@link ContentHandler#endMessage()}, etc to match previous calls
     * to
     * {@link ContentHandler#startMultipart(BodyDescriptor)},
     * {@link ContentHandler#startBodyPart()},
     * {@link ContentHandler#startMessage()}, etc.
     */
    public void stop() {
        rootStream.truncate();
    }

    /**
     * Parses an entity which consists of a header followed by a body containing
     * arbitrary data, body parts or an embedded message.
     *
     * @param is the stream to parse.
     * @throws IOException on I/O errors.
     */
    private void parseEntity(InputStream is) throws IOException {
        BodyDescriptor bd = parseHeader(is);

        if (bd.isMultipart()) {
            bodyDescriptors.addFirst(bd);

            handler.startMultipart(bd);

            MimeBoundaryInputStream tempIs =
                new MimeBoundaryInputStream(is, bd.getBoundary());
            handler.preamble(new CloseShieldInputStream(tempIs));
            tempIs.consume();

            while (tempIs.hasMoreParts()) {
                tempIs = new MimeBoundaryInputStream(is, bd.getBoundary());
                parseBodyPart(tempIs);
                tempIs.consume();
                if (tempIs.parentEOF()) {
                    prematureEof = true;
//                    if (log.isWarnEnabled()) {
//                        log.warn("Line " + rootStream.getLineNumber()
//                                + ": Body part ended prematurely. "
//                                + "Higher level boundary detected or "
//                                + "EOF reached.");
//                    }
                    break;
                }
            }

            handler.epilogue(new CloseShieldInputStream(is));

            handler.endMultipart();

            bodyDescriptors.removeFirst();

        } else if (bd.isMessage()) {
            if (bd.isBase64Encoded()) {
                log.warn("base64 encoded message/rfc822 detected");
                is = new EOLConvertingInputStream(
                        new Base64InputStream(is));
            } else if (bd.isQuotedPrintableEncoded()) {
                log.warn("quoted-printable encoded message/rfc822 detected");
                is = new EOLConvertingInputStream(
                        new QuotedPrintableInputStream(is));
            }
            bodyDescriptors.addFirst(bd);
            parseMessage(is);
            bodyDescriptors.removeFirst();
        } else {
            handler.body(bd, new CloseShieldInputStream(is));
        }

        /*
         * Make sure the stream has been consumed.
         */
        while (is.read() != -1) {
        }
    }

    private void parseMessage(InputStream is) throws IOException {
        if (raw) {
            handler.raw(new CloseShieldInputStream(is));
        } else {
            handler.startMessage();
            parseEntity(is);
            handler.endMessage();
        }
    }

    public boolean getPrematureEof() {
        return prematureEof;
    }

    private void parseBodyPart(InputStream is) throws IOException {
        if (raw) {
            handler.raw(new CloseShieldInputStream(is));
        } else {
            handler.startBodyPart();
            parseEntity(is);
            handler.endBodyPart();
        }
    }

    /**
     * Parses a header.
     *
     * @param is the stream to parse.
     * @return a <code>BodyDescriptor</code> describing the body following
     *         the header.
     */
    private BodyDescriptor parseHeader(InputStream is) throws IOException {
        BodyDescriptor bd = new BodyDescriptor(bodyDescriptors.isEmpty()
                        ? null : (BodyDescriptor) bodyDescriptors.getFirst());

        handler.startHeader();

        int lineNumber = rootStream.getLineNumber();

        StringBuffer sb = new StringBuffer();
        int curr = 0;
        int prev = 0;
        while ((curr = is.read()) != -1) {
            if (curr == '\n' && (prev == '\n' || prev == 0)) {
                /*
                 * [\r]\n[\r]\n or an immediate \r\n have been seen.
                 */
                sb.deleteCharAt(sb.length() - 1);
                break;
            }
            sb.append((char) curr);
            prev = curr == '\r' ? prev : curr;
        }

//        if (curr == -1 && log.isWarnEnabled()) {
//            log.warn("Line " + rootStream.getLineNumber()
//                    + ": Unexpected end of headers detected. "
//                    + "Boundary detected in header or EOF reached.");
//        }

        int start = 0;
        int pos = 0;
        int startLineNumber = lineNumber;
        while (pos < sb.length()) {
            while (pos < sb.length() && sb.charAt(pos) != '\r') {
                pos++;
            }
            if (pos < sb.length() - 1 && sb.charAt(pos + 1) != '\n') {
                pos++;
                continue;
            }

            if (pos >= sb.length() - 2 || fieldChars.get(sb.charAt(pos + 2))) {

                /*
                 * field should be the complete field data excluding the
                 * trailing \r\n.
                 */
                String field = sb.substring(start, pos);
                start = pos + 2;

                /*
                 * Check for a valid field.
                 */
                int index = field.indexOf(':');
                boolean valid = false;
                if (index != -1 && fieldChars.get(field.charAt(0))) {
                    valid = true;
                    String fieldName = field.substring(0, index).trim();
                    for (int i = 0; i < fieldName.length(); i++) {
                        if (!fieldChars.get(fieldName.charAt(i))) {
                            valid = false;
                            break;
                        }
                    }

                    if (valid) {
                        handler.field(field);
                        bd.addField(fieldName, field.substring(index + 1));
                    }
                }

                if (!valid && log.isWarnEnabled()) {
                    log.warn("Line " + startLineNumber
                            + ": Ignoring invalid field: '" + field.trim() + "'");
                }

                startLineNumber = lineNumber;
            }

            pos += 2;
            lineNumber++;
        }

        handler.endHeader();

        return bd;
    }

    /**
     * Sets the <code>ContentHandler</code> to use when reporting
     * parsing events.
     *
     * @param h the <code>ContentHandler</code>.
     */
    public void setContentHandler(ContentHandler h) {
        this.handler = h;
    }

}