📄 httpfields.java
字号:
if (fields == null) return; Enumeration e = fields.getFieldNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); Enumeration values = fields.getValues(name); while (values.hasMoreElements()) add(name, (String) values.nextElement()); } } /* ------------------------------------------------------------ */ /** * Get field value parameters. Some field values can have parameters. This method separates the * value from the parameters and optionally populates a map with the paramters. For example: * * <PRE> * * FieldName : Value ; param1=val1 ; param2=val2 * * </PRE> * * @param value The Field value, possibly with parameteres. * @param parameters A map to populate with the parameters, or null * @return The value. */ public static String valueParameters(String value, Map parameters) { if (value == null) return null; int i = value.indexOf(';'); if (i < 0) return value; if (parameters == null) return value.substring(0, i).trim(); StringTokenizer tok1 = new QuotedStringTokenizer(value.substring(i), ";", false, true); while (tok1.hasMoreTokens()) { String token = tok1.nextToken(); StringTokenizer tok2 = new QuotedStringTokenizer(token, "= "); if (tok2.hasMoreTokens()) { String paramName = tok2.nextToken(); String paramVal = null; if (tok2.hasMoreTokens()) paramVal = tok2.nextToken(); parameters.put(paramName, paramVal); } } return value.substring(0, i).trim(); } /* ------------------------------------------------------------ */ private static Float __one = new Float("1.0"); private static Float __zero = new Float("0.0"); private static StringMap __qualities = new StringMap(); static { __qualities.put(null, __one); __qualities.put("1.0", __one); __qualities.put("1", __one); __qualities.put("0.9", new Float("0.9")); __qualities.put("0.8", new Float("0.8")); __qualities.put("0.7", new Float("0.7")); __qualities.put("0.66", new Float("0.66")); __qualities.put("0.6", new Float("0.6")); __qualities.put("0.5", new Float("0.5")); __qualities.put("0.4", new Float("0.4")); __qualities.put("0.33", new Float("0.33")); __qualities.put("0.3", new Float("0.3")); __qualities.put("0.2", new Float("0.2")); __qualities.put("0.1", new Float("0.1")); __qualities.put("0", __zero); __qualities.put("0.0", __zero); } /* ------------------------------------------------------------ */ public static Float getQuality(String value) { if (value == null) return __zero; int qe = value.indexOf(";"); if (qe++ < 0 || qe == value.length()) return __one; if (value.charAt(qe++) == 'q') { qe++; Map.Entry entry = __qualities.getEntry(value, qe, value.length() - qe); if (entry != null) return (Float) entry.getValue(); } HashMap params = new HashMap(3); valueParameters(value, params); String qs = (String) params.get("q"); Float q = (Float) __qualities.get(qs); if (q == null) { try { q = new Float(qs); } catch (Exception e) { q = __one; } } return q; } /* ------------------------------------------------------------ */ /** * List values in quality order. * * @param enum Enumeration of values with quality parameters * @return values in quality order. */ public static List qualityList(Enumeration e) { if (e == null || !e.hasMoreElements()) return Collections.EMPTY_LIST; Object list = null; Object qual = null; // Assume list will be well ordered and just add nonzero while (e.hasMoreElements()) { String v = e.nextElement().toString(); Float q = getQuality(v); if (q.floatValue() >= 0.001) { list = LazyList.add(list, v); qual = LazyList.add(qual, q); } } List vl = LazyList.getList(list, false); if (vl.size() < 2) return vl; List ql = LazyList.getList(qual, false); // sort list with swaps Float last = __zero; for (int i = vl.size(); i-- > 0;) { Float q = (Float) ql.get(i); if (last.compareTo(q) > 0) { Object tmp = vl.get(i); vl.set(i, vl.get(i + 1)); vl.set(i + 1, tmp); ql.set(i, ql.get(i + 1)); ql.set(i + 1, q); last = __zero; i = vl.size(); continue; } last = q; } ql.clear(); return vl; } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ public static final class Field { private Buffer _name; private Buffer _value; private String _stringValue; private long _numValue; private Field _next; private Field _prev; private int _revision; /* ------------------------------------------------------------ */ private Field(Buffer name, Buffer value, long numValue, int revision) { _name = name.asImmutableBuffer(); _value = value.isImmutable() ? value : new View(value); _next = null; _prev = null; _revision = revision; _numValue = numValue; _stringValue=null; } /* ------------------------------------------------------------ */ private void clear() { _revision = -1; } /* ------------------------------------------------------------ */ private void destroy() { _name = null; _value = null; _next = null; _prev = null; _stringValue=null; } /* ------------------------------------------------------------ */ /** * Reassign a value to this field. Checks if the string value is the same as that in the char * array, if so then just reuse existing value. */ private void reset(Buffer value, long numValue, int revision) { _revision = revision; if (_value == null) { _value = value.isImmutable() ? value : new View(value); _numValue = numValue; _stringValue=null; } else if (value.isImmutable()) { _value = value; _numValue = numValue; _stringValue=null; } else { if (_value instanceof View) ((View) _value).update(value); else _value = new View(value); _numValue = numValue; // check to see if string value is still valid. if (_stringValue!=null) { if (_stringValue.length()!=value.length()) _stringValue=null; else { for (int i=value.length();i-->0;) { if (value.peek(value.getIndex()+i)!=_stringValue.charAt(i)) { _stringValue=null; break; } } } } } } /* ------------------------------------------------------------ */ public void put(Buffer buffer) throws IOException { int o=(_name instanceof CachedBuffer)?((CachedBuffer)_name).getOrdinal():-1; if (o>=0) buffer.put(_name); else { int s=_name.getIndex(); int e=_name.putIndex(); while (s<e) { byte b=_name.peek(s++); switch(b) { case '\r': case '\n': case ':' : continue; default: buffer.put(b); } } } buffer.put((byte) ':'); buffer.put((byte) ' '); o=(_value instanceof CachedBuffer)?((CachedBuffer)_value).getOrdinal():-1; if (o>=0 || _numValue>=0) buffer.put(_value); else { int s=_value.getIndex(); int e=_value.putIndex(); while (s<e) { byte b=_value.peek(s++); switch(b) { case '\r': case '\n': continue; default: buffer.put(b); } } } BufferUtil.putCRLF(buffer); } /* ------------------------------------------------------------ */ public String getName() { return BufferUtil.to8859_1_String(_name); } /* ------------------------------------------------------------ */ Buffer getNameBuffer() { return _name; } /* ------------------------------------------------------------ */ public int getNameOrdinal() { return HttpHeaders.CACHE.getOrdinal(_name); } /* ------------------------------------------------------------ */ public String getValue() { if (_stringValue==null) _stringValue=BufferUtil.to8859_1_String(_value); return _stringValue; } /* ------------------------------------------------------------ */ public Buffer getValueBuffer() { return _value; } /* ------------------------------------------------------------ */ public int getValueOrdinal() { return HttpHeaderValues.CACHE.getOrdinal(_value); } /* ------------------------------------------------------------ */ public int getIntValue() { return (int) getLongValue(); } /* ------------------------------------------------------------ */ public long getLongValue() { if (_numValue == -1) _numValue = BufferUtil.toLong(_value); return _numValue; } /* ------------------------------------------------------------ */ public String toString() { return ("[" + (_prev == null ? "" : "<-") + getName() + "="+_revision+"=" + _value + (_next == null ? "" : "->") + "]"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -