/// <exception cref="InvalidDataException"></exception> public IHttpResponse Deserialize(ResponseInfo responseInfo, Stream dataStream) { var contentType = responseInfo.GetHeaderValueOrNull(HttpHelper.ContentTypeHeader); if (string.IsNullOrWhiteSpace(contentType)) { throw new InvalidDataException("Content-Type header missed"); } var boundary = BatchSerializeHelper.GetBoundaryStringOrThrow(contentType); var subresponses = new List <IHttpResponse>(_subresponseDeserializers.Length); using (var reader = new PeekableStreamReader(dataStream)) { if (reader.ReadUntilFirstNonEmptyLine()) { while (true) { var subrequest = ParseNextSubresponseFrom(reader, boundary); subresponses.Add(subrequest); if (reader.PeekLine() == BatchSerializeHelper.GetCloseBoundaryString(boundary)) { break; } } } } if (subresponses.Count > _subresponseDeserializers.Length) { throw new InvalidDataException($"Actual and items count is greater than expected. Actual: {subresponses.Count}, expected: {_subresponseDeserializers.Length}"); } return(new HttpResponse <IHttpResponse[]>(responseInfo, subresponses.ToArray())); }
public void WriteTo(Stream stream, Uri host) { foreach (var request in SubRequests) { var sb = new StringBuilder(); sb.AppendLine(BatchSerializeHelper.GetOpenBoundaryString(_boundary)); sb.AppendLine(HttpHelper.HttpRequestContentTypeHeaderString); sb.AppendLine(); sb.AppendLine($"{request.Method.Name} {request.QueryAbsolutePath} {HttpHelper.Http11VersionCaption}"); sb.AppendLine($"Host: {host.Authority}"); stream.WriteUtf8(sb.ToString()); if (request.Content != null) { stream.WriteUtf8($"{HttpHelper.ContentTypeHeader}: {request.Content.ContentType}\r\n\r\n"); request.Content.WriteTo(stream, host); } else { stream.WriteUtf8("\r\n"); } stream.WriteUtf8("\r\n"); } stream.WriteUtf8(BatchSerializeHelper.GetCloseBoundaryString(_boundary)); }