示例#1
0
 private static HttpOptions GetClientOptions(BotData data,
                                             RuriLib.Functions.Http.Options.HttpRequestOptions options) => new()
示例#2
0
        private static async Task LogHttpResponseData(BotData data, HttpResponse response, HttpRequest request,
                                                      RuriLib.Functions.Http.Options.HttpRequestOptions requestOptions)
        {
            // Try to read the raw source for Content-Length calculation
            try
            {
                data.RAWSOURCE = await response.Content.ReadAsByteArrayAsync(data.CancellationToken);
            }
            catch (NullReferenceException)
            {
                // Thrown when there is no content (204) or we decided to not read it
                data.RAWSOURCE = Array.Empty <byte>();
            }

            // Address
            var uri = response.Request.Uri;

            if (!uri.IsAbsoluteUri)
            {
                uri = new Uri(request.Uri, uri);
            }
            data.ADDRESS = response.Request.Uri.AbsoluteUri;
            data.Logger.Log($"Address: {data.ADDRESS}", LogColors.DodgerBlue);

            // Response code
            data.RESPONSECODE = (int)response.StatusCode;
            data.Logger.Log($"Response code: {data.RESPONSECODE}", LogColors.Citrine);

            // Headers
            data.HEADERS = response.Headers;
            if (response.Content != null)
            {
                foreach (var header in response.Content.Headers)
                {
                    data.HEADERS[header.Key] = header.Value.First();
                }
            }

            if (!data.HEADERS.ContainsKey("Content-Length"))
            {
                data.HEADERS["Content-Length"] = data.RAWSOURCE.Length.ToString();
            }

            data.Logger.Log("Received Headers:", LogColors.MediumPurple);
            data.Logger.Log(data.HEADERS.Select(h => $"{h.Key}: {h.Value}"), LogColors.Violet);

            // Cookies
            data.Logger.Log("Received Cookies:", LogColors.MikadoYellow);
            data.Logger.Log(data.COOKIES.Select(h => $"{h.Key}: {h.Value}"), LogColors.Khaki);

            // Unzip the GZipped content if still gzipped (after Content-Length calculation)
            if (data.RAWSOURCE.Length > 1 && data.RAWSOURCE[0] == 0x1F && data.RAWSOURCE[1] == 0x8B)
            {
                try
                {
                    data.RAWSOURCE = GZip.Unzip(data.RAWSOURCE);
                }
                catch
                {
                    data.Logger.Log("Tried to unzip but failed", LogColors.DarkOrange);
                }
            }

            // Source
            if (!string.IsNullOrWhiteSpace(requestOptions.CodePagesEncoding))
            {
                data.SOURCE = CodePagesEncodingProvider.Instance
                              .GetEncoding(requestOptions.CodePagesEncoding).GetString(data.RAWSOURCE);
            }
            else
            {
                data.SOURCE = Encoding.UTF8.GetString(data.RAWSOURCE);
            }

            data.Logger.Log("Received Payload:", LogColors.ForestGreen);
            data.Logger.Log(data.SOURCE, LogColors.GreenYellow, true);
        }