summaryrefslogtreecommitdiffstats
path: root/mobile/android/thirdparty/ch/boye/httpclientandroidlib/impl/client/cache/WarningValue.java
blob: a3c2ce67df06b16c0cca4b29d0255f83b89d0085 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * ====================================================================
 * 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.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */
package ch.boye.httpclientandroidlib.impl.client.cache;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import ch.boye.httpclientandroidlib.Header;
import ch.boye.httpclientandroidlib.client.utils.DateUtils;

/** This class provides for parsing and understanding Warning headers. As
 * the Warning header can be multi-valued, but the values can contain
 * separators like commas inside quoted strings, we cannot use the regular
 * {@link Header#getElements()} call to access the values.
 */
class WarningValue {

    private int offs;
    private int init_offs;
    private final String src;
    private int warnCode;
    private String warnAgent;
    private String warnText;
    private Date warnDate;

    WarningValue(final String s) {
        this(s, 0);
    }

    WarningValue(final String s, final int offs) {
        this.offs = this.init_offs = offs;
        this.src = s;
        consumeWarnValue();
    }

    /** Returns an array of the parseable warning values contained
     * in the given header value, which is assumed to be a
     * Warning header. Improperly formatted warning values will be
     * skipped, in keeping with the philosophy of "ignore what you
     * cannot understand."
     * @param h Warning {@link Header} to parse
     * @return array of <code>WarnValue</code> objects
     */
    public static WarningValue[] getWarningValues(final Header h) {
        final List<WarningValue> out = new ArrayList<WarningValue>();
        final String src = h.getValue();
        int offs = 0;
        while(offs < src.length()) {
            try {
                final WarningValue wv = new WarningValue(src, offs);
                out.add(wv);
                offs = wv.offs;
            } catch (final IllegalArgumentException e) {
                final int nextComma = src.indexOf(',', offs);
                if (nextComma == -1) {
                    break;
                }
                offs = nextComma + 1;
            }
        }
        final WarningValue[] wvs = {};
        return out.toArray(wvs);
    }

    /*
     * LWS            = [CRLF] 1*( SP | HT )
     * CRLF           = CR LF
     */
    protected void consumeLinearWhitespace() {
        while(offs < src.length()) {
            switch(src.charAt(offs)) {
            case '\r':
                if (offs+2 >= src.length()
                    || src.charAt(offs+1) != '\n'
                    || (src.charAt(offs+2) != ' '
                        && src.charAt(offs+2) != '\t')) {
                    return;
                }
                offs += 2;
                break;
            case ' ':
            case '\t':
                break;
            default:
                return;
            }
            offs++;
        }
    }

    /*
     * CHAR           = <any US-ASCII character (octets 0 - 127)>
     */
    private boolean isChar(final char c) {
        final int i = c;
        return (i >= 0 && i <= 127);
    }

    /*
     * CTL            = <any US-ASCII control character
                        (octets 0 - 31) and DEL (127)>
     */
    private boolean isControl(final char c) {
        final int i = c;
        return (i == 127 || (i >=0 && i <= 31));
    }

    /*
     * separators     = "(" | ")" | "<" | ">" | "@"
     *                | "," | ";" | ":" | "\" | <">
     *                | "/" | "[" | "]" | "?" | "="
     *                | "{" | "}" | SP | HT
     */
    private boolean isSeparator(final char c) {
        return (c == '(' || c == ')' || c == '<' || c == '>'
                || c == '@' || c == ',' || c == ';' || c == ':'
                || c == '\\' || c == '\"' || c == '/'
                || c == '[' || c == ']' || c == '?' || c == '='
                || c == '{' || c == '}' || c == ' ' || c == '\t');
    }

    /*
     * token          = 1*<any CHAR except CTLs or separators>
     */
    protected void consumeToken() {
        if (!isTokenChar(src.charAt(offs))) {
            parseError();
        }
        while(offs < src.length()) {
            if (!isTokenChar(src.charAt(offs))) {
                break;
            }
            offs++;
        }
    }

    private boolean isTokenChar(final char c) {
        return (isChar(c) && !isControl(c) && !isSeparator(c));
    }

    private static final String TOPLABEL = "\\p{Alpha}([\\p{Alnum}-]*\\p{Alnum})?";
    private static final String DOMAINLABEL = "\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?";
    private static final String HOSTNAME = "(" + DOMAINLABEL + "\\.)*" + TOPLABEL + "\\.?";
    private static final String IPV4ADDRESS = "\\d+\\.\\d+\\.\\d+\\.\\d+";
    private static final String HOST = "(" + HOSTNAME + ")|(" + IPV4ADDRESS + ")";
    private static final String PORT = "\\d*";
    private static final String HOSTPORT = "(" + HOST + ")(\\:" + PORT + ")?";
    private static final Pattern HOSTPORT_PATTERN = Pattern.compile(HOSTPORT);

    protected void consumeHostPort() {
        final Matcher m = HOSTPORT_PATTERN.matcher(src.substring(offs));
        if (!m.find()) {
            parseError();
        }
        if (m.start() != 0) {
            parseError();
        }
        offs += m.end();
    }


    /*
     * warn-agent = ( host [ ":" port ] ) | pseudonym
     * pseudonym         = token
     */
    protected void consumeWarnAgent() {
        final int curr_offs = offs;
        try {
            consumeHostPort();
            warnAgent = src.substring(curr_offs, offs);
            consumeCharacter(' ');
            return;
        } catch (final IllegalArgumentException e) {
            offs = curr_offs;
        }
        consumeToken();
        warnAgent = src.substring(curr_offs, offs);
        consumeCharacter(' ');
    }

    /*
     * quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
     * qdtext         = <any TEXT except <">>
     */
    protected void consumeQuotedString() {
        if (src.charAt(offs) != '\"') {
            parseError();
        }
        offs++;
        boolean foundEnd = false;
        while(offs < src.length() && !foundEnd) {
            final char c = src.charAt(offs);
            if (offs + 1 < src.length() && c == '\\'
                && isChar(src.charAt(offs+1))) {
                offs += 2;    // consume quoted-pair
            } else if (c == '\"') {
                foundEnd = true;
                offs++;
            } else if (c != '\"' && !isControl(c)) {
                offs++;
            } else {
                parseError();
            }
        }
        if (!foundEnd) {
            parseError();
        }
    }

    /*
     * warn-text  = quoted-string
     */
    protected void consumeWarnText() {
        final int curr = offs;
        consumeQuotedString();
        warnText = src.substring(curr, offs);
    }

    private static final String MONTH = "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec";
    private static final String WEEKDAY = "Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday";
    private static final String WKDAY = "Mon|Tue|Wed|Thu|Fri|Sat|Sun";
    private static final String TIME = "\\d{2}:\\d{2}:\\d{2}";
    private static final String DATE3 = "(" + MONTH + ") ( |\\d)\\d";
    private static final String DATE2 = "\\d{2}-(" + MONTH + ")-\\d{2}";
    private static final String DATE1 = "\\d{2} (" + MONTH + ") \\d{4}";
    private static final String ASCTIME_DATE = "(" + WKDAY + ") (" + DATE3 + ") (" + TIME + ") \\d{4}";
    private static final String RFC850_DATE = "(" + WEEKDAY + "), (" + DATE2 + ") (" + TIME + ") GMT";
    private static final String RFC1123_DATE = "(" + WKDAY + "), (" + DATE1 + ") (" + TIME + ") GMT";
    private static final String HTTP_DATE = "(" + RFC1123_DATE + ")|(" + RFC850_DATE + ")|(" + ASCTIME_DATE + ")";
    private static final String WARN_DATE = "\"(" + HTTP_DATE + ")\"";
    private static final Pattern WARN_DATE_PATTERN = Pattern.compile(WARN_DATE);

    /*
     * warn-date  = <"> HTTP-date <">
     */
    protected void consumeWarnDate() {
        final int curr = offs;
        final Matcher m = WARN_DATE_PATTERN.matcher(src.substring(offs));
        if (!m.lookingAt()) {
            parseError();
        }
        offs += m.end();
        warnDate = DateUtils.parseDate(src.substring(curr+1,offs-1));
    }

    /*
     * warning-value = warn-code SP warn-agent SP warn-text [SP warn-date]
     */
    protected void consumeWarnValue() {
        consumeLinearWhitespace();
        consumeWarnCode();
        consumeWarnAgent();
        consumeWarnText();
        if (offs + 1 < src.length() && src.charAt(offs) == ' ' && src.charAt(offs+1) == '\"') {
            consumeCharacter(' ');
            consumeWarnDate();
        }
        consumeLinearWhitespace();
        if (offs != src.length()) {
            consumeCharacter(',');
        }
    }

    protected void consumeCharacter(final char c) {
        if (offs + 1 > src.length()
            || c != src.charAt(offs)) {
            parseError();
        }
        offs++;
    }

    /*
     * warn-code  = 3DIGIT
     */
    protected void consumeWarnCode() {
        if (offs + 4 > src.length()
            || !Character.isDigit(src.charAt(offs))
            || !Character.isDigit(src.charAt(offs + 1))
            || !Character.isDigit(src.charAt(offs + 2))
            || src.charAt(offs + 3) != ' ') {
            parseError();
        }
        warnCode = Integer.parseInt(src.substring(offs,offs+3));
        offs += 4;
    }

    private void parseError() {
        final String s = src.substring(init_offs);
        throw new IllegalArgumentException("Bad warn code \"" + s + "\"");
    }

    /** Returns the 3-digit code associated with this warning.
     * @return <code>int</code>
     */
    public int getWarnCode() { return warnCode; }

    /** Returns the "warn-agent" string associated with this warning,
     * which is either the name or pseudonym of the server that added
     * this particular Warning header.
     * @return {@link String}
     */
    public String getWarnAgent() { return warnAgent; }

    /** Returns the human-readable warning text for this warning. Note
     * that the original quoted-string is returned here, including
     * escaping for any contained characters. In other words, if the
     * header was:
     * <pre>
     *   Warning: 110 fred "Response is stale"
     * </pre>
     * then this method will return <code>"\"Response is stale\""</code>
     * (surrounding quotes included).
     * @return {@link String}
     */
    public String getWarnText() { return warnText; }

    /** Returns the date and time when this warning was added, or
     * <code>null</code> if a warning date was not supplied in the
     * header.
     * @return {@link Date}
     */
    public Date getWarnDate() { return warnDate; }

    /** Formats a <code>WarningValue</code> as a {@link String}
     * suitable for including in a header. For example, you can:
     * <pre>
     *   WarningValue wv = ...;
     *   HttpResponse resp = ...;
     *   resp.addHeader("Warning", wv.toString());
     * </pre>
     * @return {@link String}
     */
    @Override
    public String toString() {
        if (warnDate != null) {
            return String.format("%d %s %s \"%s\"", warnCode,
                    warnAgent, warnText, DateUtils.formatDate(warnDate));
        } else {
            return String.format("%d %s %s", warnCode, warnAgent, warnText);
        }
    }

}