• Avalanche.Core
Search Results for

    Show / Hide Table of Contents
    • Avalanche.Accessor
      • Introduction
      • IAccessor
        • IAccessor
        • IListAccessor
        • IMapAccessor
        • IRecordAccessor
        • IContentAccessor
        • IOneOfAccessor
        • IAnyAccessor
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • Dependency Injection
        • AccessorMessages
    • Avalanche.Binding
      • Introduction
    • Avalanche.Core
      • License
    • Avalanche.DataType
      • Introduction
      • DataType
        • IDataType
        • IListType
        • IMapType
        • IRecordType
        • IFieldType
        • IOneOfType
        • IAnyType
        • IStringType
        • IValueType
        • IIntegerType
        • IEnumerationType
        • IRealType
      • .Net
        • Introduction
        • IList<T>
        • IDictionary<K,V>
        • FieldInfo
        • Enum
        • OneOfAttribute
        • StructLayoutAttribute
        • Class
      • Protobuf
        • Introduction
      • Articles
        • DataTypeRequest
        • PrintTree
        • DataTypeMessages
    • Avalanche.Emit
      • Introduction
      • TypeBuilder
      • ConstructorBuilder
      • MethodBuilder
      • PropertyBuilder
      • FieldBuilder
      • Emit
      • Utilities
    • 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.Localization
      • Introduction
      • Localization
      • LocalizationFile
      • LocalizationFiles
      • LocalizationFileSystem
      • LocalizationFileFormat
      • LocalizationLine
      • LocalizationLines
      • TemplateFormat
      • CultureProvider
      • FallbackCultureProvider
      • ResourceManager
      • LocalizationError
      • Microsoft.Extensions
        • Introduction
        • DependencyInjection
        • FileProvider
        • Logging
        • ITextLocalizer
        • IFileLocalizer
        • Localization
      • Asp.Net
        • Introduction
        • Supplying localization
        • Inject to pages
        • Culture Assigned
        • Minimalistic Api
        • Diagnostics
      • Pluralization
        • Introduction
        • Multiple plural parameters
        • Custom PluralRules
        • Invariant Culture
        • Unit Prefix
        • IPluralRule
        • IPluralNumber
        • IPluralRules
        • CLDRs
        • Unicode.CLDR40
        • Unicode.CLDR41
        • Unicode.CLDR42
      • Articles
        • Alphabet localization
        • Benchmarks
        • Caching
        • Class Library
        • Demo
        • Diagnostics
        • Embedded resources
        • Emplacement
        • File localization
        • Text localization
        • Printing templates
    • Avalanche.Message
      • Introduction
      • IMessage
      • IMessageProvider
      • IMessageDescription
      • IMessageDescriptions
      • MessageLevel
      • Message printing
      • Messages and Exceptions
      • Microsoft.Extensions
        • DependencyInjection
      • Articles
        • Aggregate Messages
        • Localization
        • Logging
        • Validation
    • Avalanche.Service
      • Introduction
      • Service
        • Introduction
        • IService
        • IServiceDisposable
        • IServiceDecoration
        • IServiceCast
        • IServiceObservable
        • IServiceContainer
        • Construction
        • Query
        • CancellationToken
        • CachePolicy
        • Scope
      • Handler
        • Introduction
        • IHandler
        • IHandlerCast
        • IHandlerDecoration
        • IHandlerWithOrder
        • CancellationToken
        • Cyclicity
        • Delegates
        • Invokable
        • ExportAttribute
        • OrderAttribute
        • PrintTree
        • Recursion
      • Query
        • Introduction
        • IQuery
        • IQueryCast
        • IQueryDecoration
      • Entry
        • Introduction
        • IEntry
        • IEntryCast
        • IEntryDecoration
        • IEntryObservable
        • IEntryVisitable
        • EntryState
      • Request
        • Introduction
        • IRequest
        • IRequestFor
        • IRequestToBeCached
        • IRequestToBeDisposed
        • RequestAttribute
        • ContextParameterAttribute
        • Print Tree
      • Dependency Injection
        • Introduction
        • Asp.Net
        • ServiceRequest<T>
        • Decorating a service
        • Handler
        • CachePolicy
        • CancellationToken
        • QueryLogger
      • Examples
        • NodeCount
        • Expression
        • Mapper
      • Articles
        • Benchmarks
        • Error Handling
        • ServiceMessages
    • 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
      • HttpStatusCode
      • OpcUaStatusCode
    • Avalanche.Template
      • Introduction
      • TemplateFormats
      • ITemplatePrintable
      • ITemplateFormatPrintable
      • ITemplateText
      • ITemplateBreakdown
      • ITemplateFormat
      • ITemplateFormats
      • Extract Arguments
      • Emplacement
    • Avalanche.Tokenizer
      • Introduction
      • IToken
      • ITokenizer
      • Tokenizers
    • 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
        • ListCloner
        • DictionaryCloner
        • FieldCloner
        • PassthroughCloner
        • RecordCloner
        • ClonerProvider
      • Dispose
        • IDisposeAttachable
        • IDisposeBelatable
      • Provider
        • Introduction
        • ProviderBase
        • Delegate
        • Pipe
        • Cache
        • ResultCapture
        • AsReadOnly
        • AsService
        • IProviderEvent
      • 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.Writer
      • Introduction
      • ConstantWriter
      • Context
      • ConvertWriter
      • DefaultConstructor
      • DelegateWriter
      • PassthroughWriter
      • Referer
      • TypeCast
      • Writer
      • WriterPipe
      • WriterMessages

    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.

    In This Article
    Back to top Copyright © Toni Kalajainen