• Avalanche
  • Avalanche.Core
  • Lexical

    Show / Hide Table of Contents
    • Avalanche.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
        • AspNetCore
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR41
        • Unicode.CLDR40
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Logging
      • Validation
      • Localization
    • Avalanche.StatusCode
      • Introduction
      • HResult
        • Introduction
        • HResult.Facilities
        • BasicMessages
        • RpcMessages
        • DispatchMessages
        • ItfMessages
        • Win32Messages
        • ClrMessages
      • System
        • Introduction
        • AccessControlMessages
        • AggregateMessages
        • AppDomainMessages
        • ArgumentMessages
        • ArgumentNullMessages
        • ArgumentOutOfRangeMessages
        • ArithmeticMessages
        • ArrayMessages
        • AssemblyMessages
        • BadImageFormatMessages
        • CodeContractMessages
        • CodePageMessages
        • CollectionsMessages
        • CompilerServiceMessages
        • CryptographyMessages
        • CultureMessages
        • DiagnosticsMessages
        • EventSourceMessages
        • ExecutionEngineMessages
        • FormatMessages
        • HostProtectionMessages
        • IOMessages
        • IndexOutOfRangeMessages
        • InteropServiceMessages
        • InvalidCastMessages
        • InvalidOperationMessages
        • IsolatedStorageMessages
        • LazyMessages
        • MarshalerMessages
        • MemoryMessages
        • MiscellaneousMessages
        • NotImplementedMessages
        • NotSupportedMessages
        • ObjectDisposedMessages
        • OperationCanceledMessages
        • OverflowMessages
        • PlatformMessages
        • PolicyMessages
        • PrincipalMessages
        • ProgramMessages
        • ReferenceMessages
        • ReflectionMessages
        • RegionMessages
        • RemotingMessages
        • ResourcesMessages
        • SecurityMessages
        • SerializationMessages
        • StackMessages
        • TaskMessages
        • TextMessages
        • ThreadingMessages
        • TimeZoneMessages
        • TypeMessages
        • XmlMessages
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • Avalanche.Service
      • Introduction
      • Service
        • IService
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Cast
        • Scope
        • Dispose
      • Handler
        • Introduction
        • IHandler
        • Cancel
        • Recursion
        • Cyclicity
        • Delegates
        • Casting
        • Invokable
        • Handlers
      • Request
        • Introduction
        • IRequest
        • [Request]
        • [ContextParameter]
        • Print Tree
      • Dependency Injection
        • Introduction
        • ServiceRequest<T>
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • Expression Example
        • Mapper Example
      • ServiceMessages
    • Avalanche.Writer
      • Introduction
      • Writer
      • Referer
      • TypeCast
      • DefaultConstructor
      • DelegateWriter
      • WriterPipe
      • ConstantWriter
      • PassthroughWriter
      • Context
      • WriterMessages
    • Avalanche.Accessor
      • Introduction
      • AccessorServices
      • List
        • ListAccessor
        • ListConstructor
        • ListDeconstructor
        • ListDefaultConstructor
        • ListGet
        • ListSet
        • ListAdd
        • ListInsert
        • ListClear
        • ListCount
        • ListIndexOf
        • ListRemoveAt
        • ListReferer
      • Map
        • MapAccessor
        • MapConstructor
        • MapDeconstructor
        • MapDefaultConstructor
        • MapSet
        • MapGet
        • MapRemove
        • MapClear
        • MapCount
        • MapReferer
      • Record
        • RecordAccessor
        • RecordConstructor
        • RecordDeconstructor
        • RecordDefaultConstructor
      • Field
        • FieldAccessor
        • FieldReader
        • FieldWriter
        • FieldReferer
      • Dependency Injection
        • Introduction
      • AccessorMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • Avalanche.DataType
      • Introduction
      • IDataTypeBase
      • IDataType
      • IRecordType
      • IFieldType
      • IUnionType
      • IListType
      • IMapType
      • IValueType
      • IEnumerationType
      • IIntegerType
      • IRealType
      • IStringType
      • IAnyType
      • DataTypeRequest
      • PrintTree
      • DataTypeMessages
    • Avalanche.FileSystem
      • Introduction
      • Abstractions
        • IFileSystem
          • IFileSystemBrowse
          • IFileSystemCreateDirectory
          • IFileSystemDelete
          • IFileSystemFileAttribute
          • IFileSystemMount
          • IFileSystemMove
          • IFileSystemObserve
          • IFileSystemOpen
        • IEvent
        • IEntry
        • IOption
        • IToken
      • FileSystem
      • VirtualFileSystem
      • MemoryFileSystem
      • EmbeddedFileSystem
      • HttpFileSystem
      • Decoration
      • IFileProvider
      • Events
      • Utilities
        • Dispose
        • File Scanner
        • Visit Tree
        • File Operation
    • Avalanche.Identity
      • Introduction
      • Identity
      • IdentityParts
      • IdentityInterner
      • IdentityComparer
      • Print Tree
      • IdentityAccessors
        • Introduction
        • TypeName
    • Avalanche.Utilities
      • Introduction
      • Collections
        • Tuples
        • StructList
        • ArrayList
        • BijectionMap
        • LocakableDictionary
        • LockableList
        • MapList
        • Pipe
        • RingQueue
        • EnumerableExtensions
        • TupleUtilities
        • ArrayUtilities
      • Comparers
        • IGraphComparer
        • IGraphComparable
        • AlphaNumericComparer
        • EnumerableComparer
        • EnumerableGraphComparer
        • ReferenceComparer
        • KeyValuePairComparer
        • DefaultComparerProvider
        • RecordComparer
      • Cloners
        • ICloner
        • IGraphCloner
        • IGraphCloneable
        • EnumerableCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Concatenation
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
      • Record
        • IRecordDescription
        • IFieldDescription
        • IConstructorDescription
        • IConstructionDescription
        • IParameterDescription
        • IRecordProviders
        • RecordDelegates
          • RecordCreate
          • RecordClone
          • RecordCopy
          • IRecordDelegates
        • FieldDelegates
          • FieldRead
          • FieldWrite
          • RecreateWith
          • IFieldDelegates
      • String
        • IEscaper
        • UnicodeString
      • Miscellaneous
        • IIdGenerator
        • Permutation
        • IReadOnly
        • IUserDataContainer
        • Void
    • Avalanche.Core
      • License
      • Order

    HttpFileSystem

    new HttpFileSystem(HttpClient, IOption) creates a new http based filesystem.

    IFileSystem fs = new HttpFileSystem(httpClient: default, option: default);
    

    HttpFileSystem.Instance is the default singleton instance.

    IFileSystem fs = HttpFileSystem.Instance;
    

    Opening a resource with FileMode.Open and FileAccess.Read parameters makes a GET request.

    using (var s = HttpFileSystem.Instance.Open("http://lexical.fi/", FileMode.Open, FileAccess.Read, FileShare.None))
    {
        byte[] data = StreamUtils.ReadFully(s);
        String str = UTF8Encoding.UTF8.GetString(data);
        Console.WriteLine(str);
    }
    

    Web resources can be used with generic extension methods such as .CopyFile().

    MemoryFileSystem ram = new MemoryFileSystem();
    HttpFileSystem.Instance.CopyFile("http://lexical.fi", ram, "document.txt");
    ram.PrintTreeTo(Console.Out);
    
    ""
    └── "document.txt"
    

    Opening a resource with FileMode.Create and FileAccess.Write makes a PUT request.

    byte[] data = new byte[1024];
    using (var s = HttpFileSystem.Instance.Open("http://lexical.fi/", FileMode.Create, FileAccess.Write, FileShare.None))
        s.Write(data);
    

    HttpFileSystem can be constructed with various options, such as SubPath and custom http header.

    // Create options
    List<KeyValuePair<string, IEnumerable<string>>> headers = new List<KeyValuePair<string, IEnumerable<string>>>();
    headers.Add(new KeyValuePair<string, IEnumerable<string>>("User-Agent", new String[] { "MyUserAgent" }));
    IOption option1 = new Token(headers, typeof(System.Net.Http.Headers.HttpHeaders).FullName);
    IOption? option2 = Option.SubPath("http://lexical.fi/");
    IOption? options = Option.Union(option1, option2);
    
    // Create FileSystem
    IFileSystem fs = new HttpFileSystem(option: options);
    
    // Read resource
    using (var s = fs.Open("index.html", FileMode.Open, FileAccess.Read, FileShare.None))
    {
        byte[] data = StreamUtils.ReadFully(s);
        String str = UTF8Encoding.UTF8.GetString(data);
        Console.WriteLine(str);
    }
    

    User authentication header AuthenticationHeaderValue can be wrapped in Token and passed to Open() method.

    AuthenticationHeaderValue authentication = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword")));
    IToken token = new Token(authentication, typeof(AuthenticationHeaderValue).FullName);
    using (var s = HttpFileSystem.Instance.Open("https://lexical.fi/FileSystem/private/document.txt", FileMode.Open, FileAccess.Read, FileShare.None, token))
    {
        byte[] data = new byte[4096];
        int c = s.Read(data, 0, 1024);
        String str = UTF8Encoding.UTF8.GetString(data, 0, c);
        Console.WriteLine(str);
    }
    

    Another way is to pass user authentication token at construction of HttpFileSystem. The token must be given glob patterns where the token applies, for example "http://avalanche.fi/FileSystem/private/**".

    // Authentication header
    AuthenticationHeaderValue authentication = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword")));
    
    // Token
    IToken token = new Token(
        authentication,
        typeof(AuthenticationHeaderValue).FullName,
        "https://lexical.fi/FileSystem/private/**",
        "https://www.lexical.fi/FileSystem/private/**"
    );
    
    // Create FileSystem
    IFileSystem fs = new HttpFileSystem(default, token);
    
    // Open
    using (var s = fs.Open("https://lexical.fi/FileSystem/private/document.txt", FileMode.Open, FileAccess.Read, FileShare.None))
    {
        byte[] data = new byte[4096];
        int c = s.Read(data, 0, 1024);
        String str = UTF8Encoding.UTF8.GetString(data, 0, c);
        Console.WriteLine(str);
    }
    

    Third way is to pass authentication token into a decoration.

    // Authentication header
    AuthenticationHeaderValue authentication = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword")));
    
    // Create token
    IToken token = new Token(authentication, typeof(AuthenticationHeaderValue).FullName, "https://lexical.fi/FileSystem/private/**");
    
    // Pass token into decorator
    IFileSystem decoration = HttpFileSystem.Instance.Decorate(token);
    
    // Open
    using (var s = decoration.Open("https://lexical.fi/FileSystem/private/document.txt", FileMode.Open, FileAccess.Read, FileShare.None))
    {
        byte[] data = new byte[4096];
        int c = s.Read(data, 0, 1024);
        String str = UTF8Encoding.UTF8.GetString(data, 0, c);
        Console.WriteLine(str);
    }
    

    .Delete(uri) sends DELETE http request.

    HttpFileSystem.Instance.Delete("https://lexical.fi/FileSystem/private/document.txt");
    

    .Browse(uri) reads html document and parses links that refer to immediate child files and directories.

    var authBlob = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword"));
    var authentication = new AuthenticationHeaderValue("Basic", authBlob);
    var token = new Token(authentication, typeof(AuthenticationHeaderValue).FullName, "https://lexical.fi/FileSystem/private/**");
    
    IEnumerable<IEntry> entries = HttpFileSystem.Instance.Browse("https://lexical.fi/FileSystem/private/", token);
    

    .GetEntry(uri) reads resource headers and returns entry.

    var authBlob = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword"));
    var authentication = new AuthenticationHeaderValue("Basic", authBlob);
    var token = new Token(authentication, typeof(AuthenticationHeaderValue).FullName, "https://lexical.fi/FileSystem/private/**");
    
    IEntry entry = HttpFileSystem.Instance.GetEntry("https://lexical.fi/FileSystem/private/document.txt", token)!;
    

    File system can be scanned with .VisitTree() and .PrintTreeTo() extension methods.

    var authBlob = Convert.ToBase64String(UTF8Encoding.UTF8.GetBytes($"webuser:webpassword"));
    var authentication = new AuthenticationHeaderValue("Basic", authBlob);
    var token = new Token(authentication, typeof(AuthenticationHeaderValue).FullName, "https://lexical.fi/FileSystem/private/**");
    
    HttpFileSystem.Instance.PrintTreeTo(Console.Out, "https://lexical.fi/FileSystem/private/", option: token);
    
    "private"
    ├── "Directory"
    │  └── "file.txt"
    ├── "Folder"
    │  └── "file.txt"
    └── "document.txt"
    

    On github too. Notice that, only directories are returned from "/tree/", as files are on different url branch "/blob/".

    HttpFileSystem.Instance.PrintTreeTo(Console.Out, "https://github.com/tagcode/Avalanche.FileSystem/tree/master/");
    
    "master"
    ├── "Avalanche.FileSystem"
    │  ├── "Decoration"
    │  ├── "Extensions"
    │  ├── "Internal"
    │  ├── "Package"
    │  └── "Utility"
    └── "Avalanche.FileSystem.Abstractions"
       ├── "Extensions"
       ├── "FileProvider"
       ├── "Internal"
       ├── "Option"
       ├── "Package"
       └── "Utility"
    

    CancellationToken can be passed to break-up operation.

    // Cancel token
    CancellationTokenSource cancelSrc = new CancellationTokenSource();
    IToken token = new Token(cancelSrc.Token, typeof(CancellationToken).FullName);
    
    // Set canceled
    cancelSrc.Cancel();
    
    // Read
    HttpFileSystem.Instance.Open("http://lexical.fi/", FileMode.Open, FileAccess.Read, FileShare.None, token);
    
    Note

    HttpFileSystem doesn't support stream seeking. It can only stream content.

    Back to top Copyright © 2022 Toni Kalajainen, contact@avalanche.fi