/*
Copyright (c) 2002 Joe Gregorio
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Net;
using System.Xml;
using System.Collections;
using System.IO;
using System.Text;
using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;
namespace Bitworking{
// this keeps track of HTTP caching info such as etags and last-updated dates.
// this data is kept in the http.xml file
public class httpCacheManager {
private static httpCacheManager active ;
private Hashtable cacheInfo ;
private const string cacheDirName = "http" ;
static httpCacheManager() {
Directory.CreateDirectory(cacheDirName) ;
}
// singleton wrapper
public static httpCacheManager Current {
get {
if ( null == active ) {
lock(typeof(httpCacheManager)) {
if ( null == active )
active = new httpCacheManager() ;
}
}
return active ;
}
}
static internal string cachedFileName(string fileName) {
return cacheDirName + "/" + fileName ;
}
// ZIVC
static public void ObliterateFromCache( string fileName ) {
File.Delete( cachedFileName( fileName ) );
}
// ZIVC
static private void AddToCache( string fileName, Stream input ) {
const int cbBuff = 8192;
int cb = 0;
byte[] buff = new byte[cbBuff];
string fn = cachedFileName( fileName );
File.Delete( fn );
using ( FileStream fs = File.OpenWrite( fn ) ) {
do {
cb = input.Read ( buff, 0, cbBuff ) ;
fs.Write ( buff, 0, cb ) ;
} while ( cb > 0 ) ;
}
} // AddToCache
// Created a delegate to handle creating the WebRequest to decouple Aggie.cs from HttpCacheManager.cs.
public delegate WebRequest CreateRequestDelegate(string url);
// ZIVC
// Returns true if the resource pointed to by url_ has changed since last time.
static public bool DownloadToCache( string fileName, CreateRequestDelegate requestBuilder, string url ) {
url = Current.MovedUrl( url );
int cRedirect = 0 ;
WebResponse resp = null ;
do {
WebRequest wreq = requestBuilder( url );
if (null != wreq) {
Current.UpdateRequest( wreq );
// enable compression support
wreq.Headers["Accept-Encoding"] = "deflate, gzip" ;
}
resp = wreq.GetResponse();
url = Current.CheckResponse( wreq, resp );
cRedirect++ ;
} while ( url.Length > 0 && cRedirect < 5 ) ;
if ( cRedirect == 5 ) {
throw new Exception( "max redirects reached" );
// TODO: Do we want to suppress resetting filename_ to ""
// (what our caller does when we throw)?
}
HttpWebResponse hres = resp as HttpWebResponse;
if ( hres != null ) {
if ( hres.StatusCode == HttpStatusCode.NotModified ) {
return true;
}
}
Stream rs = null ;
try {
rs = resp.GetResponseStream() ;
// insert a decompressor if needed
if ( hres != null ) {
string contentEnc = hres.Headers["Content-Encoding"];
if ( contentEnc != null ) {
contentEnc = contentEnc.ToLower() ;
if ( contentEnc == "gzip" ) {
rs = new GZipInputStream(rs) ;
}
else if ( contentEnc == "deflate" ) {
rs = new InflaterInputStream(rs) ;
}
}
}
httpCacheManager.AddToCache( fileName, rs );
}
finally {
if ( rs != null )
((IDisposable)rs).Dispose() ;
}
return false;
} // DownloadToCache
public httpCacheManager() {
// load the existing http.xml if it exists
cacheInfo = new Hashtable() ;
httpCacheInfoReader rdr = new httpCacheInfoReader () ;
rdr.Load(this) ;
}
public string MovedUrl ( string url ) {
string returnValue = url;
if ( cacheInfo.ContainsKey ( url ) ) {
httpCacheInfo i = (httpCacheInfo)cacheInfo[url];
if (null != i) {
string movedTo = i.MovedTo;
if ( i.MovedTo.Length > 0 ) {
returnValue = i.MovedTo;
}
}
}
return returnValue;
}
public void UpdateRequest ( WebRequest req ) {
HttpWebRequest hreq = req as HttpWebRequest;
if ( cacheInfo.ContainsKey ( req.RequestUri.ToString() ) ) {
httpCacheInfo i = (httpCacheInfo)cacheInfo[req.RequestUri.ToString()] ;
if (null != i) {
if ( i.ETag.Length > 0 ) {
req.Headers["if-None-Match"] = i.ETag ;
}
if ( i.LastModified.Length > 0 ) {
if ( hreq != null ) {
hreq.IfModifiedSince = DateTime.Parse(i.LastModified) ;
}
}
}
}
// we need to turn this off, so we see a 301 moved status
if ( hreq != null ) {
hreq.AllowAutoRedirect = false ;
}
}
// returns true if the caller should re-submit the http request
public string CheckResponse ( WebRequest req, WebResponse res ) {
httpCacheInfo i = null ;
string keyUrl = req.RequestUri.ToString() ;
if ( cacheInfo.ContainsKey(keyUrl))
i = (httpCacheInfo)cacheInfo[res.ResponseUri.ToString()] ;
else
i = new httpCacheInfo() ;
string redirUrl = "" ;
Uri baseUrl = new Uri(keyUrl) ;
// (ZIVC) Fake status code if not HTTP response
HttpWebResponse hres = res as HttpWebResponse;
HttpStatusCode status = HttpStatusCode.OK;
if ( hres != null )
status = hres.StatusCode;
switch ( status ) {
case HttpStatusCode.Moved:
redirUrl = new Uri(baseUrl, res.Headers["location"]).ToString();
i.MovedTo =redirUrl ;
break ;
case HttpStatusCode.Redirect:
case HttpStatusCode.MultipleChoices:
redirUrl = new Uri(baseUrl, res.Headers["location"]).ToString();
break ;
case HttpStatusCode.OK:
if ( null != res.Headers["Etag"] ) {
i.ETag = res.Headers["Etag"] ;
}
if ( null != res.Headers["Last-Modified"] ) {
i.LastModified = res.Headers["Last-Modified"] ;
}
break ;
}
Hashtable.Synchronized(cacheInfo)[keyUrl] = i ;
return redirUrl ;
}
public void Add ( string url, httpCacheInfo i ) {
cacheInfo[url] = i ;
}
public void Save() {
httpCacheInfoWriter w = new httpCacheInfoWriter() ;
w.Save(cacheInfo) ;
}
}
internal class httpCacheConsts {
internal const string ROOT = "http" ;
internal const string ITEM = "item" ;
internal const string URL = "url" ;
internal const string ETAG = "etag" ;
internal const string LASTMOD = "lastModified" ;
internal const string MOVEDTO = "movedTo" ;
}
public class httpCacheInfoWriter {
public void Save(Hashtable cache) {
try
{
using ( FileStream fs = new FileStream ( httpCacheManager.cachedFileName("http.xml"), FileMode.Create ) )
{
XmlTextWriter w = new XmlTextWriter (fs, null) ;
w.Formatting = Formatting.Indented;
w.WriteStartDocument() ;
w.WriteStartElement ( httpCacheConsts.ROOT ) ;
foreach ( string url in cache.Keys )
{
w.WriteStartElement ( httpCacheConsts.ITEM ) ;
w.WriteElementString ( httpCacheConsts.URL, url ) ;
httpCacheInfo i = (httpCacheInfo)cache[url] ;
if (null != i) {
w.WriteElementString ( httpCacheConsts.ETAG, i.ETag ) ;
w.WriteElementString ( httpCacheConsts.LASTMOD, i.LastModified ) ;
w.WriteElementString ( httpCacheConsts.MOVEDTO, i.MovedTo ) ;
}
w.WriteEndElement() ;
}
w.WriteEndDocument() ;
w.Close() ;
}
}
catch (Exception e) {
Console.WriteLine("{0}", e.Message);
}
}
}
public class httpCacheInfoReader {
public void Load(httpCacheManager mgr) {
try {
using ( FileStream fs = new FileStream ( httpCacheManager.cachedFileName("http.xml"), FileMode.Open ) ) {
XmlTextReader rdr = new XmlTextReader(fs) ;
rdr.WhitespaceHandling = WhitespaceHandling.Significant ;
rdr.ReadStartElement(httpCacheConsts.ROOT) ;
string url, etag, lu, movedTo ;
while (true) {
rdr.ReadStartElement(httpCacheConsts.ITEM) ;
url = rdr.ReadElementString(httpCacheConsts.URL) ;
// these 3 elements are optional, but have to appear in this order
etag = readElementStringIfExists ( rdr, httpCacheConsts.ETAG, "" ) ;
lu = readElementStringIfExists ( rdr, httpCacheConsts.LASTMOD, "" ) ;
movedTo = readElementStringIfExists ( rdr, httpCacheConsts.MOVEDTO, "" ) ;
mgr.Add ( url, new httpCacheInfo ( etag, lu, movedTo ) ) ;
rdr.ReadEndElement() ;
if ( rdr.LocalName != httpCacheConsts.ITEM ) {
break ;
}
}
}
}
catch(Exception e) {
Console.WriteLine("{0}", e.Message);
}
}
private string readElementStringIfExists ( XmlTextReader rdr, string elemName, string defValue ) {
if ( rdr.LocalName == elemName )
return rdr.ReadElementString ( elemName ) ;
return defValue ;
}
}
public class httpCacheInfo {
private string etag_;
private string lastModified_;
private string movedTo_;
public httpCacheInfo() {
etag_ = "" ;
lastModified_ = "" ;
movedTo_ = "" ;
}
public httpCacheInfo(string etag, string lastModified, string movedTo) {
etag_ = (null == etag) ? String.Empty : etag;
lastModified_ = (null == lastModified) ? String.Empty : lastModified;
movedTo_ = (null == movedTo) ? String.Empty : movedTo;
}
public string ETag {
get { return etag_; }
set { etag_ = value ; }
}
public string LastModified {
get { return lastModified_; }
set { lastModified_ = value ; }
}
public string MovedTo {
get { return movedTo_; }
set { movedTo_ = value ; }
}
}
}
|