From cf6b72ddf6af1841936191f9ef3fb6f08b581cac Mon Sep 17 00:00:00 2001 From: dahall Date: Fri, 29 Jul 2022 15:07:57 -0600 Subject: [PATCH] Work on HTTP_DATA_CHUNK --- PInvoke/HttpApi/http.Struct.cs | 206 ++++++++++++++++++++++++++++------------- 1 file changed, 142 insertions(+), 64 deletions(-) diff --git a/PInvoke/HttpApi/http.Struct.cs b/PInvoke/HttpApi/http.Struct.cs index 8a33d618..9f1946d5 100644 --- a/PInvoke/HttpApi/http.Struct.cs +++ b/PInvoke/HttpApi/http.Struct.cs @@ -292,105 +292,183 @@ public static partial class HttpApi [StructLayout(LayoutKind.Sequential)] public struct HTTP_DATA_CHUNK { + private HTTP_DATA_CHUNK(HTTP_DATA_CHUNK_TYPE type) { DataChunkType = type; union = default; } + + /// Initializes a new instance of the struct for HttpDataChunkFromMemory. + /// The memory. + public HTTP_DATA_CHUNK(SafeAllocatedMemoryHandleBase mem) : this(HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromMemory) + { + FromMemory = new(mem); + } + + /// Initializes a new instance of the struct for HttpDataChunkFromFileHandle. + /// The file handle. + /// The starting offset. + /// The length. + public HTTP_DATA_CHUNK(HFILE hFile, ulong startingOffset = 0, ulong length = HTTP_BYTE_RANGE_TO_EOF) : this(HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromFileHandle) + { + FromFileHandle = new() { FileHandle = hFile, ByteRange = new() { StartingOffset = startingOffset, Length = length } }; + } + + /// Initializes a new instance of the struct for HttpDataChunkFromFragmentCache. + /// Name of the fragment. + public HTTP_DATA_CHUNK(string fragmentName) : this(HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromFragmentCache) + { + FromFragmentCache = new(fragmentName); + } + + /// Initializes a new instance of the struct for HttpDataChunkFromFragmentCacheEx. + /// Name of the fragment. + /// The starting offset. + /// The length. + public HTTP_DATA_CHUNK(string fragmentName, ulong startingOffset = 0, ulong length = HTTP_BYTE_RANGE_TO_EOF) : this(HTTP_DATA_CHUNK_TYPE.HttpDataChunkFromFragmentCacheEx) + { + FromFragmentCacheEx = new() { pFragmentName = fragmentName, ByteRange = new() { StartingOffset = startingOffset, Length = length } }; + } + + /// Initializes a new instance of the struct for HttpDataChunkTrailers. + /// The headers. + public HTTP_DATA_CHUNK(SafeNativeArray headers) : this(HTTP_DATA_CHUNK_TYPE.HttpDataChunkTrailers) + { + Trailers = new() { TrailerCount = (ushort)(headers?.Count ?? 0), pTrailers = headers }; + } + /// Type of data store. This member can be one of the values from the HTTP_DATA_CHUNK_TYPE enumeration. public HTTP_DATA_CHUNK_TYPE DataChunkType; /// - public UNION union; + private UNION union; + + public ref FROMMEMORY FromMemory => ref union.FromMemory; + + /// + public ref FROMFILEHANDLE FromFileHandle => ref union.FromFileHandle; + + /// + public ref FROMFRAGMENTCACHE FromFragmentCache => ref union.FromFragmentCache; + + /// + public ref FROMFRAGMENTCACHEEX FromFragmentCacheEx => ref union.FromFragmentCacheEx; + + /// + public ref TRAILERS Trailers => ref union.Trailers; /// [StructLayout(LayoutKind.Explicit)] - public struct UNION + private struct UNION { /// [FieldOffset(0)] - public FROMMEMORY FromMemory; + private FROMMEMORY _FromMemory; /// [FieldOffset(0)] - public FROMFILEHANDLE FromFileHandle; + private FROMFILEHANDLE _FromFileHandle; /// [FieldOffset(0)] - public FROMFRAGMENTCACHE FromFragmentCache; + private FROMFRAGMENTCACHE _FromFragmentCache; /// [FieldOffset(0)] - public FROMFRAGMENTCACHEEX FromFragmentCacheEx; + private FROMFRAGMENTCACHEEX _FromFragmentCacheEx; /// [FieldOffset(0)] - public TRAILERS Trailers; + private TRAILERS _Trailers; /// - [StructLayout(LayoutKind.Sequential)] - public struct FROMMEMORY - { - /// Pointer to the starting memory address of the data block. - public IntPtr pBuffer; - - /// Length, in bytes, of the data block. - public uint BufferLength; - } + public ref FROMMEMORY FromMemory => ref _FromMemory; /// - [StructLayout(LayoutKind.Sequential)] - public struct FROMFILEHANDLE - { - /// - /// An HTTP_BYTE_RANGE structure that specifies all or part of the file. To specify the entire file, set the - /// StartingOffset member to zero and the Length member to HTTP_BYTE_RANGE_TO_EOF. - /// - public HTTP_BYTE_RANGE ByteRange; - - /// Open handle to the file in question. - public HFILE FileHandle; - } + public ref FROMFILEHANDLE FromFileHandle => ref _FromFileHandle; /// - [StructLayout(LayoutKind.Sequential)] - public struct FROMFRAGMENTCACHE - { - /// Length, in bytes, of the fragment name not including the terminating null character. - public ushort FragmentNameLength; - - /// - /// Pointer to a string that contains the fragment name assigned when the fragment was added to the response-fragment cache - /// using the HttpAddFragmentToCache function. - /// - [MarshalAs(UnmanagedType.LPWStr)] - public string pFragmentName; - } + public ref FROMFRAGMENTCACHE FromFragmentCache => ref _FromFragmentCache; /// - [StructLayout(LayoutKind.Sequential)] - public struct FROMFRAGMENTCACHEEX - { - /// An HTTP_BYTE_RANGE structure specifying the byte range in the cached fragment. - public HTTP_BYTE_RANGE ByteRange; - - /// - /// - /// Pointer to a string that contains the fragment name assigned when the fragment was added to the response-fragment cache - /// using the HttpAddFragmentToCache function. The length of the string cannot exceed 65532 bytes. - /// - /// Note This string must be NULL terminated. - /// - [MarshalAs(UnmanagedType.LPWStr)] - public string pFragmentName; - } + public ref FROMFRAGMENTCACHEEX FromFragmentCacheEx => ref _FromFragmentCacheEx; /// - [StructLayout(LayoutKind.Sequential)] - public struct TRAILERS - { - /// Count of the number of HTTP_UNKNOWN_HEADER structures in the array pointed to by pTrailers. - public ushort TrailerCount; + public ref TRAILERS Trailers => ref _Trailers; + } - /// Pointer to an array of structures containing the trailers. - public IntPtr pTrailers; + /// + [StructLayout(LayoutKind.Sequential)] + public struct FROMMEMORY + { + /// Pointer to the starting memory address of the data block. + public IntPtr pBuffer; + + /// Length, in bytes, of the data block. + public uint BufferLength; + + internal FROMMEMORY(SafeAllocatedMemoryHandleBase mem) { pBuffer = mem ?? default; BufferLength = mem?.Size ?? 0; } + } + + /// + [StructLayout(LayoutKind.Sequential)] + public struct FROMFILEHANDLE + { + /// + /// An HTTP_BYTE_RANGE structure that specifies all or part of the file. To specify the entire file, set the + /// StartingOffset member to zero and the Length member to HTTP_BYTE_RANGE_TO_EOF. + /// + public HTTP_BYTE_RANGE ByteRange; + + /// Open handle to the file in question. + public HFILE FileHandle; + } + + /// + [StructLayout(LayoutKind.Sequential)] + public struct FROMFRAGMENTCACHE + { + /// Length, in bytes, of the fragment name not including the terminating null character. + public ushort FragmentNameLength; + + /// + /// Pointer to a string that contains the fragment name assigned when the fragment was added to the response-fragment cache using + /// the HttpAddFragmentToCache function. + /// + [MarshalAs(UnmanagedType.LPWStr)] + public string pFragmentName; + + internal FROMFRAGMENTCACHE(string fragmentName) + { + pFragmentName = fragmentName; + FragmentNameLength = (ushort)(fragmentName?.Length ?? 0); } } + + /// + [StructLayout(LayoutKind.Sequential)] + public struct FROMFRAGMENTCACHEEX + { + /// An HTTP_BYTE_RANGE structure specifying the byte range in the cached fragment. + public HTTP_BYTE_RANGE ByteRange; + + /// + /// + /// Pointer to a string that contains the fragment name assigned when the fragment was added to the response-fragment cache using + /// the HttpAddFragmentToCache function. The length of the string cannot exceed 65532 bytes. + /// + /// Note This string must be NULL terminated. + /// + [MarshalAs(UnmanagedType.LPWStr)] + public string pFragmentName; + } + + /// + [StructLayout(LayoutKind.Sequential)] + public struct TRAILERS + { + /// Count of the number of HTTP_UNKNOWN_HEADER structures in the array pointed to by pTrailers. + public ushort TrailerCount; + + /// Pointer to an array of structures containing the trailers. + public IntPtr pTrailers; + } } /// Describes additional property information when delegating a request.