public static InMemoryFile ToInMemoryFile(HarEntry entry) { try { var fileUri = new Uri(entry.Request.Url); var filename = $"{fileUri.Host}{fileUri.LocalPath}"; if (!string.IsNullOrEmpty(fileUri.Query)) { var path = Path.GetExtension(fileUri.LocalPath); filename += $"{fileUri.Query}{path}"; } var content = Unpack(entry.Response.Content); var memfile = new InMemoryFile() { FileName = Friendly(filename), Content = content }; return(memfile); } catch (Exception ex) { Console.WriteLine("Unable to create in-memory file from entry: "); Console.WriteLine(JsonSerializer.Serialize(entry)); Console.WriteLine(ex.Message); Environment.Exit(3); } return(null); }
private void AddHarEntry(HarEntry harEntry, long internalId, string url) { #region Argument Check if (harEntry == null) { throw new ArgumentNullException(nameof(harEntry)); } if (string.IsNullOrWhiteSpace(url)) { throw new ArgumentException( @"The value can be neither empty nor whitespace-only string nor null.", nameof(url)); } #endregion _harEntries.EnsureNotNull().Add(harEntry); _internalIdToHarEntryMap.EnsureNotNull().Add(internalId, harEntry); _urlToOpenRequestHarEntryMap.EnsureNotNull().Add(url, harEntry); }
private void FetchRequestHeaders( long connectTimeInMilliseconds, IPEndPoint sourceEndpoint, IPEndPoint targetEndpoint) { FetchLine(); var match = _line.MatchAgainst(ParsingHelper.RequestHeadersMarkerRegex); if (!match.Success) { throw new InvalidOperationException($"Request header was expected at line {_lineIndex}."); } var url = match.GetSucceededGroupValue(ParsingHelper.UrlGroupName); var size = match.GetSucceededGroupValue(ParsingHelper.SizeGroupName).ParseLong(); var frameId = match.GetSucceededGroupValue(ParsingHelper.FrameIdGroupName).ParseNullableLong(); var internalId = match.GetSucceededGroupValue(ParsingHelper.InternalIdGroupName).ParseLong(); var startTime = GetTime(match); if (frameId.HasValue || _harPage == null) { _harPage = new HarPage { Id = $"page_{++_pageUniqueId}", Title = url, StartedDateTime = startTime }; _harPages.EnsureNotNull().Add(_harPage); } var harRequest = new HarRequest { HeadersSize = size, Url = url, BodySize = 0 }; var harEntryTimings = new HarEntryTimings { Blocked = HarConstants.NotApplicableTiming, Dns = HarConstants.NotApplicableTiming, Connect = connectTimeInMilliseconds, Ssl = HarConstants.NotApplicableTiming, Send = 0 //// Currently, it's technically impossible to determine the 'send' timing value }; var harEntry = new HarEntry { PageRef = _harPage.Id, ConnectionId = sourceEndpoint.Port.ToString(CultureInfo.InvariantCulture), ServerIPAddress = targetEndpoint.Address.ToString(), StartedDateTime = startTime, Request = harRequest, Response = new HarResponse { BodySize = 0, Content = new HarContent() }, Timings = harEntryTimings }; AddHarEntry(harEntry, internalId, url); var multilineString = FetchMultipleLines(); var requestLineString = multilineString.Lines.FirstOrDefault(); var httpRequestLineMatch = requestLineString.MatchAgainst(ParsingHelper.HttpRequestLineRegex); if (!httpRequestLineMatch.Success) { throw new InvalidOperationException( $"An HTTP Request-Line was expected at line {multilineString.LineIndexRange.Lower}."); } harRequest.Method = httpRequestLineMatch.GetSucceededGroupValue(ParsingHelper.HttpMethodGroupName); harRequest.HttpVersion = httpRequestLineMatch.GetSucceededGroupValue(ParsingHelper.HttpVersionGroupName); var harHeaders = ParseHttpHeaders(multilineString); harRequest.Headers = harHeaders; }