/// <summary>
        /// Determines whether the HTTP content has a valid length in bytes.
        /// </summary>
        /// <returns>
        /// Returns <see cref="T:System.Boolean"/>.true if <paramref name="length"/> is a valid length; otherwise, false.
        /// </returns>
        /// <param name="length">The length in bytes of the HTTP content.</param>
        public bool TryComputeLength(out long length)
        {
            long result = 0;

            foreach (var content in _contents)
            {
                result += Boundary.Length + 4;
                result += HttpHeaderContent.ComputeLength(content.Headers);
                long contentLength;
                if (!content.TryComputeLength(out contentLength))
                {
                    length = 0;
                    return(false);
                }

                result += contentLength;
                result += 2;
            }

            result += Boundary.Length + 6;
            length  = result;
            return(true);
        }
Ejemplo n.º 2
0
        private async Task WriteTo(Stream stream, bool withHeaders)
        {
            if (withHeaders)
            {
                await HttpHeaderContent.WriteTo(Headers, stream);
            }
            var boundaryStart = Encoding.UTF8.GetBytes($"--{Boundary}\r\n");
            var boundaryEnd   = Encoding.UTF8.GetBytes($"--{Boundary}--\r\n");
            var lineBreak     = Encoding.UTF8.GetBytes("\r\n");

            foreach (var content in _contents)
            {
                await stream.WriteAsync(boundaryStart, 0, boundaryStart.Length);

                await HttpHeaderContent.WriteTo(content.Headers, stream);

                await content.CopyToAsync(stream);

                // ensure line break
                await stream.WriteAsync(lineBreak, 0, lineBreak.Length);
            }

            await stream.WriteAsync(boundaryEnd, 0, boundaryEnd.Length);
        }