public bool AssessCachedAcceptHeader(ContentTypeEnum assessedContentType) { ContentTypeEnum actualContentType = ContentTypeEnum.NotResolved; string key = GetCachingKey(); object acceptHeaderObj = cache[key]; string acceptHeader = string.Empty; if (acceptHeaderObj != null) { acceptHeader = acceptHeaderObj.ToString(); } if (!string.IsNullOrEmpty(acceptHeader)) { ChooseContentType(ref actualContentType, acceptHeader); TraceManager.PipelineComponent.TraceInfo("{0} - Used cached Accept header to determine that the response content type is {1}", CallToken, actualContentType.ToString()); } return(assessedContentType == actualContentType); }
public bool AssessContentType(ContentTypeEnum assessedContentType) { bool contentTypeSet = false; ContentTypeEnum actualContentType = ContentTypeEnum.NotResolved; string contentType = ExtractInboundHttpHeader("Content-Type"); if (!string.IsNullOrEmpty(contentType)) { ChooseContentType(ref actualContentType, contentType); contentTypeSet = true; TraceManager.PipelineComponent.TraceInfo("{0} - Used Content-Type HTTP header to determine that the request content type is {1}", CallToken, actualContentType.ToString()); } if (!contentTypeSet) { ReflectContentTypeBasedOnMessageContent(ref contentTypeSet, ref actualContentType); } if (!contentTypeSet) { TraceManager.PipelineComponent.TraceWarning("{0} - Could not determine the request content type, as Content-Type header was either blank or not provided, and request body appeared to be neither XML nor JSON", CallToken); } return(assessedContentType == actualContentType); }
private void ReflectContentTypeBasedOnMessageContent(ref bool contentTypeSet, ref ContentTypeEnum actualContentType) { TraceManager.PipelineComponent.TraceWarning("{0} - No Content-Type HTTP header was found so will have to resort to reading a portion of the stream to determine the request content type", CallToken); string firstChar = string.Empty; StreamReader reader = new StreamReader(InMsg.BodyPart.GetOriginalDataStream()); Pc.ResourceTracker.AddResource(reader); string firstLine = string.Empty; while (!reader.EndOfStream && (firstLine == string.Empty)) { firstLine = reader.ReadLine().Trim(); TraceManager.PipelineComponent.TraceInfo("{0} - Reading line from stream to try to determine the request content type", CallToken, firstChar, actualContentType.ToString()); } InMsg.BodyPart.Data.Position = 0; if (firstLine.Length > 0) { firstChar = firstLine.Substring(0, 1); switch (firstChar) { case "<": actualContentType = ContentTypeEnum.Xml; contentTypeSet = true; break; case "{": actualContentType = ContentTypeEnum.Json; contentTypeSet = true; break; case "[": actualContentType = ContentTypeEnum.Json; contentTypeSet = true; break; default: actualContentType = ContentTypeEnum.NotResolved; break; } } if (contentTypeSet) { TraceManager.PipelineComponent.TraceInfo("{0} - Used first character in request body {1} to determine that the request content type is {2}", CallToken, firstChar, actualContentType.ToString()); } }