ExtendedHttpWebRequest.cs :  » GUI » SharpVectorGraphics » SharpVectors » Net » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » SharpVectorGraphics 
SharpVectorGraphics » SharpVectors » Net » ExtendedHttpWebRequest.cs
using System;
using System.Diagnostics;
using System.Net;
using System.Xml;
using System.IO;

using ICSharpCode.SharpZipLib.GZip;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace SharpVectors.Net{
  public class ExtendedHttpWebRequestCreator : System.Net.IWebRequestCreate
  {
    public ExtendedHttpWebRequestCreator(){}
    public WebRequest Create(Uri uri){return new ExtendedHttpWebRequest(uri);}
  }

  public class ExtendedHttpWebRequest : WebRequest
  {
    #region CacheManager
    private static ICacheManager cacheManager = new NoCacheManager();
    public static ICacheManager CacheManager
    {
      get{return cacheManager;}
      set{cacheManager = value;}
    }
    #endregion

    #region Registration
    public static bool Register()
    {
      return WebRequest.RegisterPrefix("http://", new ExtendedHttpWebRequestCreator());
    }
    #endregion

    #region Constructors
    internal ExtendedHttpWebRequest(Uri uri)
    {
      requestUri = uri;
    }
    #endregion

    #region RequestUri
    private Uri requestUri;
    public override Uri RequestUri
    {
      get
      {
        return requestUri;
      }
    }
    #endregion

    private WebRequest getRequest(CacheInfo cacheInfo)
    {
      WebRequest request;
      if(cacheInfo != null && 
        cacheInfo.CachedUri != null && 
        cacheInfo.Expires > DateTime.Now)
      {
        request = WebRequest.Create(cacheInfo.CachedUri);
      }
      else
      {
        request = WebRequest.CreateDefault(RequestUri);
      }

      HttpWebRequest hRequest = request as HttpWebRequest;
      if(hRequest != null && cacheInfo != null && cacheInfo.CachedUri != null)
      {
        if(cacheInfo.ETag != null)
        {
          hRequest.Headers["If-None-Match"] = cacheInfo.ETag;
        }
        if(cacheInfo.LastModified != DateTime.MinValue)
        {
          hRequest.IfModifiedSince = cacheInfo.LastModified;
        }

        hRequest.Headers["Accept-Encoding"] = "deflate, gzip" ;
      }

      return request;
    }

    private WebResponse getResponse(WebRequest request, CacheInfo cacheInfo)
    {
      WebResponse response = null;
      try
      {
        response = request.GetResponse();
      }
      catch(WebException webEx)
      {
        HttpWebResponse hresp2 = webEx.Response as HttpWebResponse;
        if(hresp2 != null)
        {
          if(hresp2.StatusCode == HttpStatusCode.NotModified)
          {
            
            if(cacheInfo != null && cacheInfo.CachedUri != null)
            {
              request = WebRequest.Create(cacheInfo.CachedUri);
            }
            else
            {
              request = WebRequest.Create(RequestUri);
            }
            response = request.GetResponse();
          }
        }
      }

      return response;
    }

    private CacheInfo processResponse(WebResponse response)
    {
      HttpWebResponse hResponse = response as HttpWebResponse;
      CacheInfo cacheInfo = null;

      if(hResponse != null)
      {
        DateTime expires;
        if(hResponse.Headers["Expires"] != null)
        {
          expires = DateTime.Parse(hResponse.Headers["Expires"]);
        }
        else
        {
          expires = DateTime.MinValue;
        }
      
        cacheInfo = new CacheInfo(expires, hResponse.Headers["Etag"], hResponse.LastModified, null, hResponse.ContentType);
      }

      return cacheInfo;
    }

    private Stream processResponseStream(WebResponse response)
    {
      Stream respStream = response.GetResponseStream();

      string contentEnc = response.Headers["Content-Encoding"];
      if ( contentEnc != null) 
      {
        contentEnc = contentEnc.ToLower() ;
        if ( contentEnc == "gzip" ) 
        {
          respStream = new GZipInputStream(respStream) ;
        }
        else if ( contentEnc == "deflate" ) 
        {
          respStream = new InflaterInputStream(respStream) ;
        }
      }
      else if(requestUri.ToString().EndsWith(".svgz"))
      {
        // TODO: this is an ugly hack for .svgz files. Fix later!
        respStream = new GZipInputStream(respStream) ;
      }
        
      Stream stream = new MemoryStream();
      int count = 0;
      byte[] buffer = new byte[4096];
      while((count = respStream.Read(buffer, 0, 4096)) > 0) stream.Write(buffer, 0, count);

      stream.Position = 0;
      
      ((IDisposable)respStream).Dispose();

      return stream;
    }

    public override WebResponse GetResponse()
    {
      CacheInfo cacheInfo = CacheManager.GetCacheInfo(RequestUri);

      WebRequest request = getRequest(cacheInfo);
      WebResponse response = getResponse(request, cacheInfo);

      Stream stream = processResponseStream(response);

      if(response is HttpWebResponse)
      {
        CacheInfo respCacheInfo = processResponse(response);

        CacheManager.SetCacheInfo(RequestUri, respCacheInfo, stream);
      }

      return new ExtendedHttpWebResponse(RequestUri, response, stream, cacheInfo);
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.