Skip to main content

NativeHttp

A class in TavernLib, TavernLib.Backend.

A blocking HTTP GET client on Windows' WinHTTP, for mod downloads during reconcile, before Unity's TLS is up. One instance is one session; don't use it on the main thread.

Constructors​

MemberWhat it is
NativeHttp()Created by TavernLib; mods don't make one.

Fields​

8 private and internal fields
MemberWhat it is
private IntPtr _sessionThe WinHTTP session handle, opened on first use and closed by Dispose.
private const string UserAgentThe user agent sent, "TavernLib".
private const uint WinHttpAccessTypeAutomaticProxyWinHTTP access type 4, the automatic system proxy.
private const uint WinHttpAccessTypeDefaultProxyWinHTTP access type 0, the WinHTTP-configured proxy.
private const uint WinHttpFlagSecureWinHTTP flag that makes a request use HTTPS.
private const uint WinHttpQueryContentLengthWinHTTP header query for the content length.
private const uint WinHttpQueryFlagNumberWinHTTP flag that returns a header as a number.
private const uint WinHttpQueryStatusCodeWinHTTP header query for the status code.

Methods​

MemberWhat it is
void Dispose()Closes the WinHTTP session.
void Download(string url, Stream destination, DateTime deadlineUtc, TimeSpan stallTimeout, long maxBytes)Writes a URL's body into a stream. Throws on a non-2xx status, a body over maxBytes, a stalled step or the deadline passing.
string GetString(string url, TimeSpan timeout, long maxBytes)Downloads a URL and returns its body as UTF-8 text, within the timeout and size limit.
14 private and internal methods
MemberWhat it is
private static void Check(bool ok, string call)Throws an IOException naming the WinHTTP call if it failed.
private static string Describe(int code)Turns a WinHTTP error code into readable text.
private static int RemainingMs(DateTime deadlineUtc, string url)Milliseconds left until the deadline; throws a TimeoutException once it has passed.
private IntPtr Session()Returns the WinHTTP session, opening it with the automatic proxy first and the default proxy as a fallback.
private static IOException TooLarge(Uri uri, long maxBytes)Builds the error for a response over the size limit.
private static bool WinHttpCloseHandle(IntPtr handle)P/Invoke into winhttp.dll that closes a handle.
private static IntPtr WinHttpConnect(IntPtr session, string server, ushort port, uint reserved)P/Invoke into winhttp.dll that connects to a server and port.
private static IntPtr WinHttpOpen(string agent, uint accessType, string proxyName, string proxyBypass, uint flags)P/Invoke into winhttp.dll that opens a session.
private static IntPtr WinHttpOpenRequest(IntPtr connection, string verb, string objectName, string version, string referrer, IntPtr acceptTypes, uint flags)P/Invoke into winhttp.dll that opens a request.
private static bool WinHttpQueryHeaders(IntPtr request, uint infoLevel, IntPtr name, ref uint buffer, ref uint bufferLength, IntPtr index)P/Invoke into winhttp.dll that reads a response header.
private static bool WinHttpReadData(IntPtr request, byte[] buffer, uint bytesToRead, out uint bytesRead)P/Invoke into winhttp.dll that reads response body bytes.
private static bool WinHttpReceiveResponse(IntPtr request, IntPtr reserved)P/Invoke into winhttp.dll that waits for the response.
private static bool WinHttpSendRequest(IntPtr request, IntPtr headers, uint headersLength, IntPtr optional, uint optionalLength, uint totalLength, IntPtr context)P/Invoke into winhttp.dll that sends a request.
private static bool WinHttpSetTimeouts(IntPtr handle, int resolveMs, int connectMs, int sendMs, int receiveMs)P/Invoke into winhttp.dll that sets the resolve, connect, send and receive timeouts.

Used in​

Members of other types that take, return or hold a NativeHttp: