[Htmlparser-cvs] htmlparser/src/org/htmlparser Parser.java,1.84,1.85
Brought to you by:
derrickoswald
From: <der...@pr...> - 2004-01-26 15:01:15
|
Update of /cvsroot/htmlparser/htmlparser/src/org/htmlparser In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19868 Modified Files: Parser.java Log Message: Fix RFE #817723 enhancement: add user-agent Add get/setDefaultRequestProperties() which is used when creating a new connection to condition the connection prior to connecting. Currently, the only request property is "User-Agent", which is set to "HTMLParser/1.4". Sophisticated users may set other properties to tailor the parser behaviour. Index: Parser.java =================================================================== RCS file: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/Parser.java,v retrieving revision 1.84 retrieving revision 1.85 diff -C2 -d -r1.84 -r1.85 *** Parser.java 19 Jan 2004 23:14:18 -0000 1.84 --- Parser.java 26 Jan 2004 00:27:34 -0000 1.85 *************** *** 33,36 **** --- 33,39 ---- import java.net.URL; import java.net.URLConnection; + import java.util.HashMap; + import java.util.Iterator; + import java.util.Map; import org.htmlparser.filters.TagNameFilter; *************** *** 98,101 **** --- 101,114 ---- /** + * Default Request header fields. + * So far this is just "User-Agent". + */ + protected static Map mDefaultRequestProperties = new HashMap (); + static + { + mDefaultRequestProperties.put ("User-Agent", "HTMLParser/" + VERSION_NUMBER); + } + + /** * Feedback object. */ *************** *** 161,164 **** --- 174,241 ---- } + /** + * Get the current default request header properties. + * A String-to-String map of header keys and values. + * These fields are set by the parser when creating a connection. + */ + public static Map getDefaultRequestProperties () + { + return (mDefaultRequestProperties); + } + + /** + * Set the default request header properties. + * A String-to-String map of header keys and values. + * These fields are set by the parser when creating a connection. + * Some of these can be set directly on a <code>URLConnection</code>, + * i.e. If-Modified-Since is set with setIfModifiedSince(long), + * but since the parser transparently opens the connection on behalf + * of the developer, these properties are not available before the + * connection is fetched. Setting these request header fields affects all + * subsequent connections opened by the parser. For more direct control + * create a <code>URLConnection</code> and set it on the parser.<p> + * From <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616 Hypertext Transfer Protocol -- HTTP/1.1</a>: + * <pre> + * 5.3 Request Header Fields + * + * The request-header fields allow the client to pass additional + * information about the request, and about the client itself, to the + * server. These fields act as request modifiers, with semantics + * equivalent to the parameters on a programming language method + * invocation. + * + * request-header = Accept ; Section 14.1 + * | Accept-Charset ; Section 14.2 + * | Accept-Encoding ; Section 14.3 + * | Accept-Language ; Section 14.4 + * | Authorization ; Section 14.8 + * | Expect ; Section 14.20 + * | From ; Section 14.22 + * | Host ; Section 14.23 + * | If-Match ; Section 14.24 + * | If-Modified-Since ; Section 14.25 + * | If-None-Match ; Section 14.26 + * | If-Range ; Section 14.27 + * | If-Unmodified-Since ; Section 14.28 + * | Max-Forwards ; Section 14.31 + * | Proxy-Authorization ; Section 14.34 + * | Range ; Section 14.35 + * | Referer ; Section 14.36 + * | TE ; Section 14.39 + * | User-Agent ; Section 14.43 + * + * Request-header field names can be extended reliably only in + * combination with a change in the protocol version. However, new or + * experimental header fields MAY be given the semantics of request- + * header fields if all parties in the communication recognize them to + * be request-header fields. Unrecognized header fields are treated as + * entity-header fields. + * </pre> + */ + public static void setDefaultRequestProperties (Map properties) + { + mDefaultRequestProperties = properties; + } + // // Constructors *************** *** 500,503 **** --- 577,583 ---- ParserException { + Map properties; + String key; + String value; URLConnection ret; *************** *** 505,508 **** --- 585,596 ---- { ret = url.openConnection (); + properties = getDefaultRequestProperties (); + if (null != properties) + for (Iterator iterator = properties.keySet ().iterator (); iterator.hasNext (); ) + { + key = (String)iterator.next (); + value = (String)properties.get (key); + ret.setRequestProperty (key, value); + } } catch (IOException ioe) |