예제 #1
0
			internal virtual void ApplyConditionals(URLConnection urlConnection)
			{
				if (lastModified != 0L)
				{
					urlConnection.SetIfModifiedSince(lastModified);
				}
				if (entityTags != null && entityTags.Length > 0)
				{
					urlConnection.AddRequestProperty("If-None-Match", entityTags);
				}
			}
예제 #2
0
		/// <summary>
		/// Decrypt an input stream from
		/// <see cref="Get(string, string)">Get(string, string)</see>
		/// .
		/// </summary>
		/// <param name="u">
		/// connection previously created by
		/// <see cref="Get(string, string)">Get(string, string)</see>
		/// }.
		/// </param>
		/// <returns>stream to read plain text from.</returns>
		/// <exception cref="System.IO.IOException">decryption could not be configured.</exception>
		public virtual InputStream Decrypt(URLConnection u)
		{
			return encryption.Decrypt(u.GetInputStream());
		}
예제 #3
0
			private long CalculateExpiry(URLConnection urlConnection, long request_time, UrlConnectionExpiryCalculator urlConnectionExpiryCalculator)
			{
				if ("no-cache".Equals(urlConnection.GetHeaderField("Pragma")))
				{
					return 0L;
				}
				string cacheControl = urlConnection.GetHeaderField("Cache-Control");
				if (cacheControl != null)
				{
					if (cacheControl.IndexOf("no-cache") != -1)
					{
						return 0L;
					}
					int max_age = GetMaxAge(cacheControl);
					if (-1 != max_age)
					{
						long response_time = Runtime.CurrentTimeMillis();
						long apparent_age = Math.Max(0, response_time - urlConnection.GetDate());
						long corrected_received_age = Math.Max(apparent_age, urlConnection.GetHeaderFieldInt("Age", 0) * 1000L);
						long response_delay = response_time - request_time;
						long corrected_initial_age = corrected_received_age + response_delay;
						long creation_time = response_time - corrected_initial_age;
						return max_age * 1000L + creation_time;
					}
				}
				long explicitExpiry = urlConnection.GetHeaderFieldDate("Expires", -1L);
				if (explicitExpiry != -1L)
				{
					return explicitExpiry;
				}
				return urlConnectionExpiryCalculator == null ? 0L : urlConnectionExpiryCalculator.CalculateExpiry(urlConnection);
			}
예제 #4
0
			private string GetEntityTags(URLConnection urlConnection)
			{
				IList<string> etags = urlConnection.GetHeaderFields().Get("ETag");
				if (etags == null || etags.IsEmpty())
				{
					return null;
				}
				StringBuilder b = new StringBuilder();
				IEnumerator<string> it = etags.GetEnumerator();
				b.Append(it.Next());
				while (it.HasNext())
				{
					b.Append(", ").Append(it.Next());
				}
				return b.ToString();
			}
예제 #5
0
			/// <exception cref="System.IO.IOException"></exception>
			internal virtual bool UpdateValidator(URLConnection urlConnection, long request_time, UrlConnectionExpiryCalculator urlConnectionExpiryCalculator)
			{
				bool isResourceChanged = IsResourceChanged(urlConnection);
				if (!isResourceChanged)
				{
					expiry = CalculateExpiry(urlConnection, request_time, urlConnectionExpiryCalculator);
				}
				return isResourceChanged;
			}
예제 #6
0
			/// <exception cref="System.IO.IOException"></exception>
			private bool IsResourceChanged(URLConnection urlConnection)
			{
				if (urlConnection is HttpURLConnection)
				{
					return ((HttpURLConnection)urlConnection).GetResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED;
				}
				return lastModified == urlConnection.GetLastModified();
			}
예제 #7
0
		/// <summary>
		/// Override if you want to get notified if the URL connection fails to
		/// close.
		/// </summary>
		/// <remarks>
		/// Override if you want to get notified if the URL connection fails to
		/// close. Does nothing by default.
		/// </remarks>
		/// <param name="urlConnection">the connection</param>
		/// <param name="cause">the cause it failed to close.</param>
		protected internal virtual void OnFailedClosingUrlConnection(URLConnection urlConnection, IOException cause)
		{
		}
예제 #8
0
			public URLValidator(Uri uri, URLConnection urlConnection, long request_time, UrlConnectionExpiryCalculator urlConnectionExpiryCalculator)
			{
				this.uri = uri;
				this.lastModified = urlConnection.GetLastModified();
				this.entityTags = GetEntityTags(urlConnection);
				expiry = CalculateExpiry(urlConnection, request_time, urlConnectionExpiryCalculator);
			}
예제 #9
0
		private void Close(URLConnection urlConnection)
		{
			try
			{
				urlConnection.GetInputStream().Close();
			}
			catch (IOException e)
			{
				OnFailedClosingUrlConnection(urlConnection, e);
			}
		}
예제 #10
0
		private object GetSecurityDomain(URLConnection urlConnection)
		{
			return urlConnectionSecurityDomainProvider == null ? null : urlConnectionSecurityDomainProvider.GetSecurityDomain(urlConnection);
		}
예제 #11
0
		private static string GetCharacterEncoding(URLConnection urlConnection)
		{
			ParsedContentType pct = new ParsedContentType(urlConnection.GetContentType());
			string encoding = pct.GetEncoding();
			if (encoding != null)
			{
				return encoding;
			}
			string contentType = pct.GetContentType();
			if (contentType != null && contentType.StartsWith("text/"))
			{
				return "8859_1";
			}
			else
			{
				return "utf-8";
			}
		}
예제 #12
0
		/// <exception cref="System.IO.IOException"></exception>
		private static TextReader GetReader(URLConnection urlConnection)
		{
			return new StreamReader(urlConnection.GetInputStream(), GetCharacterEncoding(urlConnection));
		}
		public virtual long CalculateExpiry(URLConnection urlConnection)
		{
			return Runtime.CurrentTimeMillis() + relativeExpiry;
		}