// // for cookie purpose, the effective uri should only be http://host // the path will be taken into account when path-match algorithm applied // private URI GetEffectiveURI(URI uri) { URI effectiveURI = null; try { effectiveURI = new URI("http", uri.Host, null, null, null); // fragment component - query component - path component } catch (URISyntaxException) { effectiveURI = uri; } return(effectiveURI); }
/// <summary> /// Remove a cookie from store /// </summary> public virtual bool Remove(URI uri, HttpCookie ck) { // argument can't be null if (ck == null) { throw new NullPointerException("cookie is null"); } bool modified = false; @lock.@lock(); try { modified = CookieJar.Remove(ck); } finally { @lock.Unlock(); } return(modified); }
/// <summary> /// Add one cookie into cookie store. /// </summary> public virtual void Add(URI uri, HttpCookie cookie) { // pre-condition : argument can't be null if (cookie == null) { throw new NullPointerException("cookie is null"); } @lock.@lock(); try { // remove the ole cookie if there has had one CookieJar.Remove(cookie); // add new cookie if it has a non-zero max-age if (cookie.MaxAge != 0) { CookieJar.Add(cookie); // and add it to domain index if (cookie.Domain != null) { AddIndex(DomainIndex, cookie.Domain, cookie); } if (uri != null) { // add it to uri index, too AddIndex(UriIndex, GetEffectiveURI(uri), cookie); } } } finally { @lock.Unlock(); } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public void put(URI uri, java.util.Map<String, java.util.List<String>> responseHeaders) throws java.io.IOException public override void Put(URI uri, IDictionary <String, IList <String> > responseHeaders) { // pre-condition check if (uri == null || responseHeaders == null) { throw new IllegalArgumentException("Argument is null"); } // if there's no default CookieStore, no need to remember any cookie if (CookieJar == null) { return; } PlatformLogger logger = PlatformLogger.getLogger("java.net.CookieManager"); foreach (String headerKey in responseHeaders.Keys) { // RFC 2965 3.2.2, key must be 'Set-Cookie2' // we also accept 'Set-Cookie' here for backward compatibility if (headerKey == null || !(headerKey.EqualsIgnoreCase("Set-Cookie2") || headerKey.EqualsIgnoreCase("Set-Cookie"))) { continue; } foreach (String headerValue in responseHeaders[headerKey]) { try { IList <HttpCookie> cookies; try { cookies = HttpCookie.Parse(headerValue); } catch (IllegalArgumentException) { // Bogus header, make an empty list and log the error cookies = Collections.EmptyList(); if (logger.isLoggable(PlatformLogger.Level.SEVERE)) { logger.severe("Invalid cookie for " + uri + ": " + headerValue); } } foreach (HttpCookie cookie in cookies) { if (cookie.Path == null) { // If no path is specified, then by default // the path is the directory of the page/doc String path = uri.Path; if (!path.EndsWith("/")) { int i = path.LastIndexOf("/"); if (i > 0) { path = path.Substring(0, i + 1); } else { path = "/"; } } cookie.Path = path; } // As per RFC 2965, section 3.3.1: // Domain Defaults to the effective request-host. (Note that because // there is no dot at the beginning of effective request-host, // the default Domain can only domain-match itself.) if (cookie.Domain == null) { String host = uri.Host; if (host != null && !host.Contains(".")) { host += ".local"; } cookie.Domain = host; } String ports = cookie.Portlist; if (ports != null) { int port = uri.Port; if (port == -1) { port = "https".Equals(uri.Scheme) ? 443 : 80; } if (ports.Empty) { // Empty port list means this should be restricted // to the incoming URI port cookie.Portlist = "" + port; if (ShouldAcceptInternal(uri, cookie)) { CookieJar.Add(uri, cookie); } } else { // Only store cookies with a port list // IF the URI port is in that list, as per // RFC 2965 section 3.3.2 if (IsInPortList(ports, port) && ShouldAcceptInternal(uri, cookie)) { CookieJar.Add(uri, cookie); } } } else { if (ShouldAcceptInternal(uri, cookie)) { CookieJar.Add(uri, cookie); } } } } catch (IllegalArgumentException) { // invalid set-cookie header string // no-op } } } }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public java.util.Map<String, java.util.List<String>> get(URI uri, java.util.Map<String, java.util.List<String>> requestHeaders) throws java.io.IOException public override IDictionary <String, IList <String> > Get(URI uri, IDictionary <String, IList <String> > requestHeaders) { // pre-condition check if (uri == null || requestHeaders == null) { throw new IllegalArgumentException("Argument is null"); } IDictionary <String, IList <String> > cookieMap = new Dictionary <String, IList <String> >(); // if there's no default CookieStore, no way for us to get any cookie if (CookieJar == null) { return(Collections.UnmodifiableMap(cookieMap)); } bool secureLink = "https".Equals(uri.Scheme, StringComparison.CurrentCultureIgnoreCase); IList <HttpCookie> cookies = new List <HttpCookie>(); String path = uri.Path; if (path == null || path.Empty) { path = "/"; } foreach (HttpCookie cookie in CookieJar.Get(uri)) { // apply path-matches rule (RFC 2965 sec. 3.3.4) // and check for the possible "secure" tag (i.e. don't send // 'secure' cookies over unsecure links) if (PathMatches(path, cookie.Path) && (secureLink || !cookie.Secure)) { // Enforce httponly attribute if (cookie.HttpOnly) { String s = uri.Scheme; if (!"http".Equals(s, StringComparison.CurrentCultureIgnoreCase) && !"https".Equals(s, StringComparison.CurrentCultureIgnoreCase)) { continue; } } // Let's check the authorize port list if it exists String ports = cookie.Portlist; if (ports != null && !ports.Empty) { int port = uri.Port; if (port == -1) { port = "https".Equals(uri.Scheme) ? 443 : 80; } if (IsInPortList(ports, port)) { cookies.Add(cookie); } } else { cookies.Add(cookie); } } } // apply sort rule (RFC 2965 sec. 3.3.4) IList <String> cookieHeader = SortByPath(cookies); cookieMap["Cookie"] = cookieHeader; return(Collections.UnmodifiableMap(cookieMap)); }
/// <summary> /// Set the base URI of the resource being written to this destination /// </summary> /// <param name="uri">the base URI to be used</param> public void setDestinationBaseURI(java.net.URI uri) { destination.setDestinationBaseURI(uri); }
/// <summary> /// Called to indicate that a connection could not be established /// to a proxy/socks server. An implementation of this method can /// temporarily remove the proxies or reorder the sequence of /// proxies returned by <seealso cref="#select(URI)"/>, using the address /// and the IOException caught when trying to connect. /// </summary> /// <param name="uri"> /// The URI that the proxy at sa failed to serve. </param> /// <param name="sa"> /// The socket address of the proxy/SOCKS server /// </param> /// <param name="ioe"> /// The I/O exception thrown when the connect failed. </param> /// <exception cref="IllegalArgumentException"> if either argument is null </exception> public abstract void ConnectFailed(URI uri, SocketAddress sa, IOException ioe);
/// <summary> /// Selects all the applicable proxies based on the protocol to /// access the resource with and a destination address to access /// the resource at. /// The format of the URI is defined as follow: /// <UL> /// <LI>http URI for http connections</LI> /// <LI>https URI for https connections /// <LI>{@code socket://host:port}<br> /// for tcp client sockets connections</LI> /// </UL> /// </summary> /// <param name="uri"> /// The URI that a connection is required to /// </param> /// <returns> a List of Proxies. Each element in the /// the List is of type /// <seealso cref="java.net.Proxy Proxy"/>; /// when no proxy is available, the list will /// contain one element of type /// <seealso cref="java.net.Proxy Proxy"/> /// that represents a direct connection. </returns> /// <exception cref="IllegalArgumentException"> if the argument is null </exception> public abstract IList <Proxy> Select(URI uri);
/// <summary> /// The protocol handler calls this method after a resource has /// been retrieved, and the ResponseCache must decide whether or /// not to store the resource in its cache. If the resource is to /// be cached, then put() must return a CacheRequest object which /// contains an OutputStream that the protocol handler will /// use to write the resource into the cache. If the resource is /// not to be cached, then put must return null. /// </summary> /// <param name="uri"> a {@code URI} used to reference the requested /// network resource </param> /// <param name="conn"> - a URLConnection instance that is used to fetch /// the response to be cached </param> /// <returns> a {@code CacheRequest} for recording the /// response to be cached. Null return indicates that /// the caller does not intend to cache the response. </returns> /// <exception cref="IOException"> if an I/O error occurs </exception> /// <exception cref="IllegalArgumentException"> if any one of the arguments is /// null </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public abstract CacheRequest put(URI uri, URLConnection conn) throws java.io.IOException; public abstract CacheRequest Put(URI uri, URLConnection conn);
/// <summary> /// Retrieve the cached response based on the requesting uri, /// request method and request headers. Typically this method is /// called by the protocol handler before it sends out the request /// to get the network resource. If a cached response is returned, /// that resource is used instead. /// </summary> /// <param name="uri"> a {@code URI} used to reference the requested /// network resource </param> /// <param name="rqstMethod"> a {@code String} representing the request /// method </param> /// <param name="rqstHeaders"> - a Map from request header /// field names to lists of field values representing /// the current request headers </param> /// <returns> a {@code CacheResponse} instance if available /// from cache, or null otherwise </returns> /// <exception cref="IOException"> if an I/O error occurs </exception> /// <exception cref="IllegalArgumentException"> if any one of the arguments is null /// </exception> /// <seealso cref= java.net.URLConnection#setUseCaches(boolean) </seealso> /// <seealso cref= java.net.URLConnection#getUseCaches() </seealso> /// <seealso cref= java.net.URLConnection#setDefaultUseCaches(boolean) </seealso> /// <seealso cref= java.net.URLConnection#getDefaultUseCaches() </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public abstract CacheResponse get(URI uri, String rqstMethod, java.util.Map<String, java.util.List<String>> rqstHeaders) throws java.io.IOException; public abstract CacheResponse Get(URI uri, String rqstMethod, IDictionary <String, IList <String> > rqstHeaders);
/// <summary> /// Creates a new <tt>File</tt> instance by converting the given /// <tt>file:</tt> URI into an abstract pathname. /// </summary> public File(URI @uri) { }
/// <summary> /// Sets all the applicable cookies, examples are response header /// fields that are named Set-Cookie2, present in the response /// headers into a cookie cache. /// </summary> /// <param name="uri"> a {@code URI} where the cookies come from </param> /// <param name="responseHeaders"> an immutable map from field names to /// lists of field values representing the response /// header fields returned </param> /// <exception cref="IOException"> if an I/O error occurs </exception> /// <exception cref="IllegalArgumentException"> if either argument is null </exception> /// <seealso cref= #get(URI, Map) </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public abstract void put(URI uri, java.util.Map<String, java.util.List<String>> responseHeaders) throws java.io.IOException; public abstract void Put(URI uri, IDictionary <String, IList <String> > responseHeaders);
/// <summary> /// Gets all the applicable cookies from a cookie cache for the /// specified uri in the request header. /// /// <P>The {@code URI} passed as an argument specifies the intended use for /// the cookies. In particular the scheme should reflect whether the cookies /// will be sent over http, https or used in another context like javascript. /// The host part should reflect either the destination of the cookies or /// their origin in the case of javascript.</P> /// <P>It is up to the implementation to take into account the {@code URI} and /// the cookies attributes and security settings to determine which ones /// should be returned.</P> /// /// <P>HTTP protocol implementers should make sure that this method is /// called after all request headers related to choosing cookies /// are added, and before the request is sent.</P> /// </summary> /// <param name="uri"> a {@code URI} representing the intended use for the /// cookies </param> /// <param name="requestHeaders"> - a Map from request header /// field names to lists of field values representing /// the current request headers </param> /// <returns> an immutable map from state management headers, with /// field names "Cookie" or "Cookie2" to a list of /// cookies containing state information /// </returns> /// <exception cref="IOException"> if an I/O error occurs </exception> /// <exception cref="IllegalArgumentException"> if either argument is null </exception> /// <seealso cref= #put(URI, Map) </seealso> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public abstract java.util.Map<String, java.util.List<String>> get(URI uri, java.util.Map<String, java.util.List<String>> requestHeaders) throws java.io.IOException; public abstract IDictionary <String, IList <String> > Get(URI uri, IDictionary <String, IList <String> > requestHeaders);