>
*/
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 int
*/
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:
*
* Warning: 110 fred "Response is stale"
*
* then this method will return "\"Response is stale\""
* (surrounding quotes included).
* @return {@link String}
*/
public String getWarnText() { return warnText; }
/** Returns the date and time when this warning was added, or
* null
if a warning date was not supplied in the
* header.
* @return {@link Date}
*/
public Date getWarnDate() { return warnDate; }
/** Formats a WarningValue
as a {@link String}
* suitable for including in a header. For example, you can:
*
* WarningValue wv = ...;
* HttpResponse resp = ...;
* resp.addHeader("Warning", wv.toString());
*
* @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);
}
}
}