Ejemplo n.º 1
0
        /// <summary>
        /// Parses a specific HTTP header field.
        /// </summary>
        /// <param name="KeyLower">Lower-case version of field name.</param>
        /// <param name="Key">Field name, as it appears in the header.</param>
        /// <param name="Value">Unparsed header field value</param>
        /// <returns>HTTP header field object, corresponding to the particular field.</returns>
        protected virtual HttpField ParseField(string KeyLower, string Key, string Value)
        {
            switch (KeyLower)
            {
            case "content-encoding": return(this.contentEncoding = new HttpFieldContentEncoding(Key, Value));

            case "content-language": return(this.contentLanguage = new HttpFieldContentLanguage(Key, Value));

            case "content-length": return(this.contentLength = new HttpFieldContentLength(Key, Value));

            case "content-location": return(this.contentLocation = new HttpFieldContentLocation(Key, Value));

            case "content-md5": return(this.contentMD5 = new HttpFieldContentMD5(Key, Value));

            case "content-range": return(this.contentRange = new HttpFieldContentRange(Key, Value));

            case "content-type": return(this.contentType = new HttpFieldContentType(Key, Value));

            case "transfer-encoding": return(this.transferEncoding = new HttpFieldTransferEncoding(Key, Value));

            case "via": return(this.via = new HttpFieldVia(Key, Value));

            default: return(new HttpField(Key, Value));
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> BinaryDataReceived(byte[] Data, int Offset, int NrRead)
        {
            if (this.dataStream is null)
            {
                HttpFieldTransferEncoding TransferEncoding = this.header.TransferEncoding;
                if (!(TransferEncoding is null))
                {
                    if (TransferEncoding.Value == "chunked")
                    {
                        this.dataStream       = new TemporaryStream();
                        this.transferEncoding = new ChunkedTransferEncoding(new BinaryOutputStream(this.dataStream), null);
                    }
                    else
                    {
                        await this.SendResponse(null, null, new HttpException(501, "Not Implemented", "Transfer encoding not implemented."), false);

                        return(true);
                    }
                }
                else
                {
                    HttpFieldContentLength ContentLength = this.header.ContentLength;
                    if (!(ContentLength is null))
                    {
                        long l = ContentLength.ContentLength;
                        if (l < 0)
                        {
                            await this.SendResponse(null, null, new HttpException(400, "Bad Request", "Negative content lengths invalid."), false);

                            return(true);
                        }

                        if (l <= MaxInmemoryMessageSize)
                        {
                            this.dataStream = new MemoryStream((int)l);
                        }
                        else
                        {
                            this.dataStream = new TemporaryStream();
                        }

                        this.transferEncoding = new ContentLengthEncoding(new BinaryOutputStream(this.dataStream), l, null);
                    }
Ejemplo n.º 3
0
        private bool BinaryDataReceived(byte[] Data, int Offset, int NrRead)
        {
            if (this.dataStream == null)
            {
                HttpFieldTransferEncoding TransferEncoding = this.header.TransferEncoding;
                if (TransferEncoding != null)
                {
                    if (TransferEncoding.Value == "chunked")
                    {
                        this.dataStream       = new TemporaryFile();
                        this.transferEncoding = new ChunkedTransferEncoding(this.dataStream, null);
                    }
                    else
                    {
                        this.SendResponse(null, 501, "Not Implemented", false);
                        return(true);
                    }
                }
                else
                {
                    HttpFieldContentLength ContentLength = this.header.ContentLength;
                    if (ContentLength != null)
                    {
                        long l = ContentLength.ContentLength;
                        if (l < 0)
                        {
                            this.SendResponse(null, 400, "Bad Request", false);
                            return(true);
                        }

                        if (l <= MaxInmemoryMessageSize)
                        {
                            this.dataStream = new MemoryStream((int)l);
                        }
                        else
                        {
                            this.dataStream = new TemporaryFile();
                        }

                        this.transferEncoding = new ContentLengthEncoding(this.dataStream, l, null);
                    }
                    else
                    {
                        this.SendResponse(null, 411, "Length Required", true);
                        return(false);
                    }
                }
            }

            bool Complete = this.transferEncoding.Decode(Data, Offset, NrRead, out int NrAccepted);

            if (this.HasSniffers)
            {
                if (Offset == 0 && NrAccepted == Data.Length)
                {
                    this.ReceiveBinary(Data);
                }
                else
                {
                    byte[] Data2 = new byte[NrAccepted];
                    Array.Copy(Data, Offset, Data2, 0, NrAccepted);
                    this.ReceiveBinary(Data2);
                }
            }

            if (Complete)
            {
                if (this.transferEncoding.InvalidEncoding)
                {
                    this.SendResponse(null, 400, "Bad Request", false);
                    return(true);
                }
                else
                {
                    Offset += NrAccepted;
                    NrRead -= NrAccepted;

                    if (!this.RequestReceived())
                    {
                        return(false);
                    }

                    if (NrRead > 0)
                    {
                        return(this.BinaryHeaderReceived(Data, Offset, NrRead));
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            else if (this.dataStream.Position > MaxEntitySize)
            {
                this.dataStream.Dispose();
                this.dataStream = null;

                this.SendResponse(null, 413, "Request Entity Too Large", true);
                return(false);
            }
            else
            {
                return(true);
            }
        }
Ejemplo n.º 4
0
        private async Task <bool> BinaryDataReceived(byte[] Data, int Offset, int NrRead)
        {
            if (this.dataStream is null)
            {
                HttpFieldTransferEncoding TransferEncoding = this.header.TransferEncoding;
                if (TransferEncoding != null)
                {
                    if (TransferEncoding.Value == "chunked")
                    {
                        this.dataStream       = new TemporaryFile();
                        this.transferEncoding = new ChunkedTransferEncoding(new BinaryOutputStream(this.dataStream), null);
                    }
                    else
                    {
                        await this.SendResponse(null, null, new HttpException(501, "Not Implemented", "Transfer encoding not implemented."), false);

                        return(true);
                    }
                }
                else
                {
                    HttpFieldContentLength ContentLength = this.header.ContentLength;
                    if (ContentLength != null)
                    {
                        long l = ContentLength.ContentLength;
                        if (l < 0)
                        {
                            await this.SendResponse(null, null, new HttpException(400, "Bad Request", "Negative content lengths invalid."), false);

                            return(true);
                        }

                        if (l <= MaxInmemoryMessageSize)
                        {
                            this.dataStream = new MemoryStream((int)l);
                        }
                        else
                        {
                            this.dataStream = new TemporaryFile();
                        }

                        this.transferEncoding = new ContentLengthEncoding(new BinaryOutputStream(this.dataStream), l, null);
                    }
                    else
                    {
                        await this.SendResponse(null, null, new HttpException(411, "Length Required", "Content Length required."), true);

                        return(false);
                    }
                }
            }

            ulong DecodingResponse = await this.transferEncoding.DecodeAsync(Data, Offset, NrRead);

            int  NrAccepted = (int)DecodingResponse;
            bool Complete   = (DecodingResponse & 0x100000000) != 0;

            if (this.HasSniffers)
            {
                if (Offset == 0 && NrAccepted == Data.Length)
                {
                    this.ReceiveBinary(Data);
                }
                else
                {
                    byte[] Data2 = new byte[NrAccepted];
                    Array.Copy(Data, Offset, Data2, 0, NrAccepted);
                    this.ReceiveBinary(Data2);
                }
            }

            if (Complete)
            {
                if (this.transferEncoding.InvalidEncoding)
                {
                    await this.SendResponse(null, null, new HttpException(400, "Bad Request", "Invalid transfer encoding."), false);

                    return(true);
                }
                else if (this.transferEncoding.TransferError)
                {
                    await this.SendResponse(null, null, new HttpException(500, "Internal Server Error", "Unable to transfer content to resource."), false);

                    return(true);
                }
                else
                {
                    Offset += NrAccepted;
                    NrRead -= NrAccepted;

                    if (!await this.RequestReceived())
                    {
                        return(false);
                    }

                    if (NrRead > 0)
                    {
                        return(await this.BinaryHeaderReceived(Data, Offset, NrRead));
                    }
                    else
                    {
                        return(true);
                    }
                }
            }
            else if (this.dataStream.Position > MaxEntitySize)
            {
                this.dataStream.Dispose();
                this.dataStream = null;

                await this.SendResponse(null, null, new HttpException(413, "Request Entity Too Large", "Maximum Entity Size: " + MaxEntitySize.ToString()), true);

                return(false);
            }
            else
            {
                return(true);
            }
        }