📄 httpfields.java
字号:
// ========================================================================// Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.// ------------------------------------------------------------------------// Licensed 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.mortbay.jetty;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.Collections;import java.util.Date;import java.util.Enumeration;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.NoSuchElementException;import java.util.StringTokenizer;import java.util.TimeZone;import javax.servlet.http.Cookie;import org.mortbay.io.Buffer;import org.mortbay.io.BufferCache;import org.mortbay.io.BufferDateCache;import org.mortbay.io.BufferUtil;import org.mortbay.io.ByteArrayBuffer;import org.mortbay.io.View;import org.mortbay.io.BufferCache.CachedBuffer;import org.mortbay.util.LazyList;import org.mortbay.util.QuotedStringTokenizer;import org.mortbay.util.StringMap;import org.mortbay.util.StringUtil;import org.mortbay.util.URIUtil;/* ------------------------------------------------------------ *//** * HTTP Fields. A collection of HTTP header and or Trailer fields. This class is not synchronized * and needs to be protected from concurrent access. * * This class is not synchronized as it is expected that modifications will only be performed by a * single thread. * * @author Greg Wilkins (gregw) */public class HttpFields{ /* ------------------------------------------------------------ */ public final static String __separators = ", \t"; /* ------------------------------------------------------------ */ private static String[] DAYS = { "Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; private static String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jan"}; /* ------------------------------------------------------------ */ /** * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for * cookies */ public static String formatDate(long date, boolean cookie) { StringBuffer buf = new StringBuffer(32); GregorianCalendar gc = new GregorianCalendar(__GMT); gc.setTimeInMillis(date); formatDate(buf, gc, cookie); return buf.toString(); } /* ------------------------------------------------------------ */ /** * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for * cookies */ public static String formatDate(Calendar calendar, boolean cookie) { StringBuffer buf = new StringBuffer(32); formatDate(buf, calendar, cookie); return buf.toString(); } /* ------------------------------------------------------------ */ /** * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for * cookies */ public static String formatDate(StringBuffer buf, long date, boolean cookie) { GregorianCalendar gc = new GregorianCalendar(__GMT); gc.setTimeInMillis(date); formatDate(buf, gc, cookie); return buf.toString(); } /* ------------------------------------------------------------ */ /** * Format HTTP date "EEE, dd MMM yyyy HH:mm:ss 'GMT'" or "EEE, dd-MMM-yy HH:mm:ss 'GMT'"for * cookies */ public static void formatDate(StringBuffer buf, Calendar calendar, boolean cookie) { // "EEE, dd MMM yyyy HH:mm:ss 'GMT'" // "EEE, dd-MMM-yy HH:mm:ss 'GMT'", cookie int day_of_week = calendar.get(Calendar.DAY_OF_WEEK); int day_of_month = calendar.get(Calendar.DAY_OF_MONTH); int month = calendar.get(Calendar.MONTH); int year = calendar.get(Calendar.YEAR); int century = year / 100; year = year % 100; int epoch = (int) ((calendar.getTimeInMillis() / 1000) % (60 * 60 * 24)); int seconds = epoch % 60; epoch = epoch / 60; int minutes = epoch % 60; int hours = epoch / 60; buf.append(DAYS[day_of_week]); buf.append(','); buf.append(' '); StringUtil.append2digits(buf, day_of_month); if (cookie) { buf.append('-'); buf.append(MONTHS[month]); buf.append('-'); StringUtil.append2digits(buf, year); } else { buf.append(' '); buf.append(MONTHS[month]); buf.append(' '); StringUtil.append2digits(buf, century); StringUtil.append2digits(buf, year); } buf.append(' '); StringUtil.append2digits(buf, hours); buf.append(':'); StringUtil.append2digits(buf, minutes); buf.append(':'); StringUtil.append2digits(buf, seconds); buf.append(" GMT"); } /* -------------------------------------------------------------- */ private static TimeZone __GMT = TimeZone.getTimeZone("GMT"); public final static BufferDateCache __dateCache = new BufferDateCache("EEE, dd MMM yyyy HH:mm:ss 'GMT'", Locale.US); /* ------------------------------------------------------------ */ private final static String __dateReceiveFmt[] = { "EEE, dd MMM yyyy HH:mm:ss zzz", "EEE, dd-MMM-yy HH:mm:ss", "EEE MMM dd HH:mm:ss yyyy", "EEE, dd MMM yyyy HH:mm:ss", "EEE dd MMM yyyy HH:mm:ss zzz", "EEE dd MMM yyyy HH:mm:ss", "EEE MMM dd yyyy HH:mm:ss zzz", "EEE MMM dd yyyy HH:mm:ss", "EEE MMM-dd-yyyy HH:mm:ss zzz", "EEE MMM-dd-yyyy HH:mm:ss", "dd MMM yyyy HH:mm:ss zzz", "dd MMM yyyy HH:mm:ss", "dd-MMM-yy HH:mm:ss zzz", "dd-MMM-yy HH:mm:ss", "MMM dd HH:mm:ss yyyy zzz", "MMM dd HH:mm:ss yyyy", "EEE MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy zzz", "EEE, MMM dd HH:mm:ss yyyy", "EEE, dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss zzz", "EEE dd-MMM-yy HH:mm:ss", }; private static int __dateReceiveInit=3; private static SimpleDateFormat __dateReceive[]; static { __GMT.setID("GMT"); __dateCache.setTimeZone(__GMT); __dateReceive = new SimpleDateFormat[__dateReceiveFmt.length]; // Initialize only the standard formats here. for (int i = 0; i < __dateReceiveInit; i++) { __dateReceive[i] = new SimpleDateFormat(__dateReceiveFmt[i], Locale.US); __dateReceive[i].setTimeZone(__GMT); } } public final static String __01Jan1970 = formatDate(0, false); public final static Buffer __01Jan1970_BUFFER = new ByteArrayBuffer(__01Jan1970); /* -------------------------------------------------------------- */ protected ArrayList _fields = new ArrayList(20); protected int _revision; protected HashMap _bufferMap = new HashMap(32); protected SimpleDateFormat _dateReceive[] = new SimpleDateFormat[__dateReceive.length]; private StringBuffer _dateBuffer; private Calendar _calendar; /* ------------------------------------------------------------ */ /** * Constructor. */ public HttpFields() { } /* -------------------------------------------------------------- */ /** * Get enumeration of header _names. Returns an enumeration of strings representing the header * _names for this request. */ public Enumeration getFieldNames() { final int revision=_revision; return new Enumeration() { int i = 0; Field field = null; public boolean hasMoreElements() { if (field != null) return true; while (i < _fields.size()) { Field f = (Field) _fields.get(i++); if (f != null && f._prev == null && f._revision == revision) { field = f; return true; } } return false; } public Object nextElement() throws NoSuchElementException { if (field != null || hasMoreElements()) { String n = BufferUtil.to8859_1_String(field._name); field = null; return n; } throw new NoSuchElementException(); } }; } /* -------------------------------------------------------------- */ /** * Get enumeration of Fields Returns an enumeration of Fields for this request. */ public Iterator getFields() { final int revision=_revision; return new Iterator() { int i = 0; Field field = null; public boolean hasNext() { if (field != null) return true; while (i < _fields.size()) { Field f = (Field) _fields.get(i++); if (f != null && f._revision == revision) { field = f; return true; } } return false; } public Object next() { if (field != null || hasNext()) { final Field f = field; field = null; return f; } throw new NoSuchElementException(); } public void remove() { throw new UnsupportedOperationException(); } }; } /* ------------------------------------------------------------ */ private Field getField(String name) { return (Field) _bufferMap.get(HttpHeaders.CACHE.lookup(name)); } /* ------------------------------------------------------------ */ private Field getField(Buffer name) { return (Field) _bufferMap.get(name); } /* ------------------------------------------------------------ */ public boolean containsKey(Buffer name) { Field f = getField(name); return (f != null && f._revision == _revision); } /* ------------------------------------------------------------ */ public boolean containsKey(String name) { Field f = getField(name); return (f != null && f._revision == _revision); } /* -------------------------------------------------------------- */ /** * @return the value of a field, or null if not found. For multiple fields of the same name, * only the first is returned. * @param name the case-insensitive field name */ public String getStringField(String name) { // TODO - really reuse strings from previous requests! Field field = getField(name); if (field != null && field._revision == _revision) return field.getValue(); return null; } /* -------------------------------------------------------------- */ /** * @return the value of a field, or null if not found. For multiple fields of the same name, * only the first is returned. * @param name the case-insensitive field name */ public String getStringField(Buffer name) { // TODO - really reuse strings from previous requests! Field field = getField(name); if (field != null && field._revision == _revision) return BufferUtil.to8859_1_String(field._value); return null; } /* -------------------------------------------------------------- */ /** * @return the value of a field, or null if not found. For multiple fields of the same name, * only the first is returned. * @param name the case-insensitive field name */ public Buffer get(Buffer name) { Field field = getField(name); if (field != null && field._revision == _revision) return field._value; return null; } /* -------------------------------------------------------------- */ /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -