[hyena] Added HttpStringDownloader, AcceptContentTypes



commit 58996ea761ea3350ed8aee42f831eb2ba409d55c
Author: Aaron Bockover <abockover novell com>
Date:   Tue Jul 13 12:45:28 2010 -0400

    Added HttpStringDownloader, AcceptContentTypes
    
    HttpStringDownloader allows for easy fetching of HTTP resources to be
    encoded as strings. Encoding is attempted to be detected automatically,
    and falls back to UTF8. An explicit Encoding can be provided as well. An
    example:
    
      new HttpStringDownloader () {
          Uri = new Uri ("http://banshee.fm/foo";),
          AcceptContentTypes = new [] { "text/foo" },
          Finished = d => {
              if (d.State.Success) {
                  PresentString (d.Content);
              }
          }
      }.Start ()
    
    On the base HttpDownloader, an AcceptContentTypes property was added
    that will cause an exception to be thrown when the HTTP response is
    delivered if its Content-Type does not match one of the
    AcceptContentTypes. The propery is optinal (an unset AcceptContentTypes
    will accept any/all Content-Type responses).

 Hyena/Hyena.Downloader/Buffer.cs               |    2 +-
 Hyena/Hyena.Downloader/HttpDownloader.cs       |   19 ++++++-
 Hyena/Hyena.Downloader/HttpDownloaderState.cs  |    2 +
 Hyena/Hyena.Downloader/HttpStringDownloader.cs |   67 ++++++++++++++++++++++++
 Hyena/Hyena.csproj                             |    9 +++-
 Hyena/Makefile.am                              |    1 +
 6 files changed, 96 insertions(+), 4 deletions(-)
---
diff --git a/Hyena/Hyena.Downloader/Buffer.cs b/Hyena/Hyena.Downloader/Buffer.cs
index 97792b3..f6a469f 100644
--- a/Hyena/Hyena.Downloader/Buffer.cs
+++ b/Hyena/Hyena.Downloader/Buffer.cs
@@ -30,7 +30,7 @@ namespace Hyena.Downloader
 {
     public class Buffer
     {
-        public long Length { get; set; }
+        public int Length { get; set; }
         public DateTime TimeStamp { get; set; }
         public byte [] Data { get; set; }
     }
diff --git a/Hyena/Hyena.Downloader/HttpDownloader.cs b/Hyena/Hyena.Downloader/HttpDownloader.cs
index 72a2572..a9026b7 100644
--- a/Hyena/Hyena.Downloader/HttpDownloader.cs
+++ b/Hyena/Hyena.Downloader/HttpDownloader.cs
@@ -47,7 +47,8 @@ namespace Hyena.Downloader
         public Uri Uri { get; set; }
         public TimeSpan ProgressEventRaiseLimit { get; set; }
         public HttpDownloaderState State { get; private set; }
-        
+        public string [] AcceptContentTypes { get; set; }
+
         private int buffer_size = 8192;
         public int BufferSize {
             get { return buffer_size; }
@@ -152,8 +153,24 @@ namespace Hyena.Downloader
                         State.Success = false;
                         raise = true;
                         return;
+                    } else if (AcceptContentTypes != null) {
+                        var accepted = false;
+                        foreach (var type in AcceptContentTypes) {
+                            if (type == response.ContentType) {
+                                accepted = true;
+                                break;
+                            }
+                        }
+                        if (!accepted) {
+                            throw new WebException ("Invalid content type: " +
+                                response.ContentType + "; expected one of: " +
+                                String.Join (", ", AcceptContentTypes));
+                        }
                     }
 
+                    State.ContentType = response.ContentType;
+                    State.CharacterSet = response.CharacterSet;
+
                     State.TotalBytesExpected = response.ContentLength;
 
                     response_stream = response.GetResponseStream ();
diff --git a/Hyena/Hyena.Downloader/HttpDownloaderState.cs b/Hyena/Hyena.Downloader/HttpDownloaderState.cs
index 0cc77b5..ec4e1d6 100644
--- a/Hyena/Hyena.Downloader/HttpDownloaderState.cs
+++ b/Hyena/Hyena.Downloader/HttpDownloaderState.cs
@@ -39,6 +39,8 @@ namespace Hyena.Downloader
         public long TotalBytesExpected { get; internal set; }
         public bool Success { get; internal set; }
         public bool Working { get; internal set; }
+        public string ContentType { get; internal set; }
+        public string CharacterSet { get; internal set; }
         public Exception FailureException { get; internal set; }
     }
 }
\ No newline at end of file
diff --git a/Hyena/Hyena.Downloader/HttpStringDownloader.cs b/Hyena/Hyena.Downloader/HttpStringDownloader.cs
new file mode 100644
index 0000000..e1903cf
--- /dev/null
+++ b/Hyena/Hyena.Downloader/HttpStringDownloader.cs
@@ -0,0 +1,67 @@
+// 
+// HttpStringDownloader.cs
+// 
+// Author:
+//   Aaron Bockover <abockover novell com>
+// 
+// Copyright 2010 Novell, Inc.
+// 
+// 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.Text;
+
+namespace Hyena.Downloader
+{
+    public class HttpStringDownloader : HttpDownloader
+    {
+        private Encoding detected_encoding;
+
+        public string Content { get; private set; }
+        public Encoding Encoding { get; set; }
+        public new Action<HttpStringDownloader> Finished { get; set; }
+
+        protected override void OnBufferUpdated ()
+        {
+            var default_encoding = Encoding.UTF8;
+
+            if (detected_encoding == null && !String.IsNullOrEmpty (State.CharacterSet)) {
+                try {
+                    detected_encoding = Encoding.GetEncoding (State.CharacterSet);
+                } catch {
+                }
+            }
+
+            if (detected_encoding == null) {
+                detected_encoding = default_encoding;
+            }
+
+            Content += (Encoding ?? detected_encoding).GetString (State.Buffer.Data, 0, State.Buffer.Length);
+        }
+
+        protected override void OnFinished ()
+        {
+            base.OnFinished ();
+            var handler = Finished;
+            if (handler != null) {
+                handler (this);
+            }
+        }
+    }
+}
diff --git a/Hyena/Hyena.csproj b/Hyena/Hyena.csproj
index a73acf4..b96a1ca 100644
--- a/Hyena/Hyena.csproj
+++ b/Hyena/Hyena.csproj
@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"; ToolsVersion="3.5">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -17,6 +17,8 @@
     <RootNamespace>Hyena</RootNamespace>
     <Optimize>true</Optimize>
     <WarningLevel>4</WarningLevel>
+    <ReleaseVersion>1.3</ReleaseVersion>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <OutputPath>..\..\bin\</OutputPath>
@@ -25,6 +27,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>true</Optimize>
+    <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Submodule|AnyCPU' ">
     <DefineConstants>NET_2_0</DefineConstants>
@@ -33,6 +36,7 @@
     <DebugSymbols>true</DebugSymbols>
     <DebugType>full</DebugType>
     <Optimize>true</Optimize>
+    <WarningLevel>4</WarningLevel>
   </PropertyGroup>
   <ItemGroup>
     <Compile Include="Hyena.Data\BaseListModel.cs" />
@@ -156,6 +160,7 @@
     <Compile Include="Hyena.Data\MemoryListModel.cs" />
     <Compile Include="Hyena.Query\EnumQueryValue.cs" />
     <Compile Include="Hyena\EventArgs.cs" />
+    <Compile Include="Hyena.Downloader\HttpStringDownloader.cs" />
   </ItemGroup>
   <ItemGroup>
     <Reference Include="System" />
@@ -179,7 +184,7 @@
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <ProjectExtensions>
     <MonoDevelop>
-      <Properties InternalTargetFrameworkVersion="3.5">
+      <Properties>
         <Deployment.LinuxDeployData generateScript="false" />
         <MonoDevelop.Autotools.MakefileInfo IntegrationEnabled="true" RelativeMakefileName="Makefile.am" BuildTargetName="" CleanTargetName="">
           <BuildFilesVar Sync="true" Name="SOURCES" />
diff --git a/Hyena/Makefile.am b/Hyena/Makefile.am
index ab950c3..6825feb 100644
--- a/Hyena/Makefile.am
+++ b/Hyena/Makefile.am
@@ -42,6 +42,7 @@ SOURCES =  \
 	Hyena.Downloader/HttpDownloader.cs \
 	Hyena.Downloader/HttpDownloaderState.cs \
 	Hyena.Downloader/HttpFileDownloader.cs \
+	Hyena.Downloader/HttpStringDownloader.cs \
 	Hyena.Jobs/Job.cs \
 	Hyena.Jobs/JobExtensions.cs \
 	Hyena.Jobs/PriorityHints.cs \



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]