public override ThenResponse Perform(EventInfo eventInfo) { if (eventInfo.Type == EventType.Message) { ThenResponse thenResult = ThenResponse.BreakRules; if (!eventInfo.Message.CheckedEntity(HttpMessage.EntityFlag)) { IVariable <byte[]> carryOverVar = eventInfo.Variables.GetOrDefault <byte[]>($"ThenDelimitHttp_CarryOverBytes") ?? eventInfo.Variables.Add <byte[]>($"ThenDelimitHttp_CarryOverBytes"); byte[] carryOverBytes = carryOverVar.Value ?? new byte[0]; carryOverVar.Value = new byte[0]; byte[] fullBytes = carryOverBytes.Combine(eventInfo.Message.RawBytes); List <EventInfo> childEvents = new List <EventInfo>(); Tuple <int, HttpMessage> messageInfo = null; IVariable <int> syncId = eventInfo.Variables.GetOrDefault <int>("ThenDelimitHttp_SyncId") ?? eventInfo.Variables.Add <int>("ThenDelimitHttp_SyncId"); do { HttpMessageParser parser = new HttpMessageParser() { TextEncoding = eventInfo.ProxyConnection.ProxyInfo.DefaultEncoding }; messageInfo = parser.Parse(fullBytes); if (messageInfo.Item2 != null) { carryOverBytes = new byte[0]; messageInfo.Item2.SyncId = syncId.Value++; childEvents.Add(eventInfo.Clone(message: messageInfo.Item2)); if (messageInfo.Item1 <= fullBytes.Length) { fullBytes = new Buffer <byte>(fullBytes, messageInfo.Item1, fullBytes.Length - messageInfo.Item1).GetBytes(); } else { fullBytes = new byte[0]; } } else { carryOverBytes = fullBytes; } }while (fullBytes.Length > 0 && messageInfo.Item2 != null); carryOverVar.Value = carryOverBytes; eventInfo.Engine.Queue.AddFirst(childEvents); eventInfo.Message.SetEntityFlag(HttpMessage.EntityFlag, false); } else if (eventInfo.Message is HttpMessage) { thenResult = ThenResponse.Continue; } return(thenResult); } else { return(ThenResponse.Continue); } }
public void SetValue(EventInfo eventInfo, MessageValue messageValue, MessageValueType valueType, VariableString identifier, string replacementText) { switch (messageValue) { case MessageValue.DataDirection: { DataDirection dataDirection; if (Enum.TryParse(replacementText, out dataDirection)) { eventInfo.Direction = dataDirection; } } break; case MessageValue.HttpBody: { HttpMessage httpMessage = eventInfo.Message as HttpMessage; if (httpMessage != null) { if (valueType != MessageValueType.Text && identifier != null) { switch (valueType) { case MessageValueType.Json: replacementText = httpMessage.Body.Text.SetJsonValue(identifier.GetText(eventInfo.Variables), replacementText); break; case MessageValueType.Xml: replacementText = httpMessage.Body.Text.SetXmlValue(identifier.GetText(eventInfo.Variables), replacementText); break; } } httpMessage.Body = new HttpBody() { Text = replacementText }; if (GeneralSettings.AutoUpdateContentLength) { if (httpMessage.Headers.Contains("Content-Length")) { httpMessage.Headers["Content-Length"] = httpMessage.Body.Text.Length.ToString(); } } } } break; case MessageValue.HttpHeader: { HttpMessage httpMessage = eventInfo.Message as HttpMessage; if (httpMessage != null && identifier != null) { httpMessage.Headers[identifier.GetText(eventInfo.Variables)] = replacementText; } } break; case MessageValue.HttpHeaders: { HttpMessage httpMessage = eventInfo.Message as HttpMessage; if (httpMessage != null) { _httpHeadersParser.Parse(eventInfo, replacementText, true); } } break; case MessageValue.HttpStatusLine: { HttpMessage httpMessage = eventInfo.Message as HttpMessage; if (httpMessage != null) { HttpStatusLine newHttpStatusLine = _httpStatusLineParser.Parse(replacementText); httpMessage.StatusLine = newHttpStatusLine; } } break; case MessageValue.HttpMethod: { HttpRequestStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpRequestStatusLine; if (httpStatusLine != null) { httpStatusLine.Method = replacementText; } } break; case MessageValue.HttpRequestUri: { HttpRequestStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpRequestStatusLine; if (httpStatusLine != null) { httpStatusLine.Uri = replacementText; } } break; case MessageValue.HttpStatusMessage: { HttpResponseStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpResponseStatusLine; if (httpStatusLine != null) { httpStatusLine.StatusMessage = replacementText; } } break; case MessageValue.HttpVersion: { HttpStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine; if (httpStatusLine != null) { httpStatusLine.Version = replacementText; } } break; case MessageValue.HttpStatusCode: { HttpResponseStatusLine httpStatusLine = (eventInfo.Message as HttpMessage)?.StatusLine as HttpResponseStatusLine; if (httpStatusLine != null) { int replacementVal; if (int.TryParse(replacementText, out replacementVal)) { httpStatusLine.StatusCode = replacementVal; } } } break; case MessageValue.Message: { if (eventInfo.Message is HttpMessage) { HttpMessage newMessage = _httpMessageParser.Parse(replacementText, true).Item2; if (newMessage != null) { eventInfo.Message = newMessage; } } else if (eventInfo.Message != null) { eventInfo.Message.ResetCheckedEntity(HttpMessage.EntityFlag); if (valueType != MessageValueType.Text && identifier != null) { switch (valueType) { case MessageValueType.Json: replacementText = eventInfo.Message.ToString().SetJsonValue(identifier.GetText(eventInfo.Variables), replacementText); break; case MessageValueType.Xml: replacementText = eventInfo.Message.ToString().SetXmlValue(identifier.GetText(eventInfo.Variables), replacementText); break; } } eventInfo.Message.RawText = replacementText + eventInfo.Message.Delimiter; } } break; } }