Beispiel #1
0
        internal static Datagram ParseBody(byte[] buffer, int offset, int length,
                                           bool isBodyPossible, HttpHeader header)
        {
            if (!isBodyPossible)
            {
                return(Empty);
            }

            HttpTransferEncodingField transferEncodingField = header.TransferEncoding;

            if (transferEncodingField != null &&
                transferEncodingField.TransferCodings != null &&
                transferEncodingField.TransferCodings.Any(coding => coding != "identity"))
            {
                return(ParseChunkedBody(buffer, offset, length));
            }

            HttpContentLengthField contentLengthField = header.ContentLength;

            if (contentLengthField != null)
            {
                uint?contentLength = contentLengthField.ContentLength;
                if (contentLength != null)
                {
                    return(new Datagram(buffer, offset, Math.Min((int)contentLength.Value, length)));
                }
            }

            HttpContentTypeField contentTypeField = header.ContentType;

            if (contentTypeField != null)
            {
                if (contentTypeField.MediaType == "multipart" &&
                    contentTypeField.MediaSubtype == "byteranges")
                {
                    string boundary = contentTypeField.Parameters["boundary"];
                    if (boundary != null)
                    {
                        byte[] lastBoundaryBuffer = Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "\r\n--{0}--", boundary));
                        int    lastBoundaryOffset = buffer.Find(offset, length, lastBoundaryBuffer);
                        int    lastBoundaryEnd    = lastBoundaryOffset + lastBoundaryBuffer.Length;
                        return(new Datagram(buffer, offset,
                                            Math.Min(lastBoundaryEnd - offset, length)));
                    }
                }
            }

            return(new Datagram(buffer, offset, length));
        }
        internal static Datagram ParseBody(byte[] buffer, int offset, int length, bool isBodyPossible, HttpHeader header)
        {
            if (!isBodyPossible)
            {
                return(Datagram.Empty);
            }
            HttpTransferEncodingField transferEncoding = header.TransferEncoding;

            if (transferEncoding != null && transferEncoding.TransferCodings != null && Enumerable.Any <string>((IEnumerable <string>)transferEncoding.TransferCodings, (Func <string, bool>)(coding => coding != "identity")))
            {
                return(HttpDatagram.ParseChunkedBody(buffer, offset, length));
            }
            HttpContentLengthField contentLength1 = header.ContentLength;

            if (contentLength1 != null)
            {
                uint?contentLength2 = contentLength1.ContentLength;
                if (contentLength2.HasValue)
                {
                    return(new Datagram(buffer, offset, Math.Min((int)contentLength2.Value, length)));
                }
            }
            HttpContentTypeField contentType = header.ContentType;

            if (contentType != null && contentType.MediaType == "multipart" && contentType.MediaSubtype == "byteranges")
            {
                string str = contentType.Parameters["boundary"];
                if (str != null)
                {
                    byte[] bytes = Encoding.ASCII.GetBytes(string.Format((IFormatProvider)CultureInfo.InvariantCulture, "\r\n--{0}--", new object[1]
                    {
                        (object)str
                    }));
                    int num = ByteArrayExtensions.Find(buffer, offset, length, bytes) + bytes.Length;
                    return(new Datagram(buffer, offset, Math.Min(num - offset, length)));
                }
            }
            return(new Datagram(buffer, offset, length));
        }