コード例 #1
0
ファイル: MultipartForm.cs プロジェクト: Metapyziks/WebServer
        public MultipartFormField(HeaderCollection headers, Stream stream)
            : base(headers)
        {
            var headerDict = new Dictionary <String, FormFieldHeader>();
            var subFields  = new List <FormField>();

            var readlineBuffer = new byte[128];

            var line = ReadLine(stream, readlineBuffer);

            while (true)
            {
                if (line == null || !line.StartsWith(String.Format("--{0}", Boundary)))
                {
                    throw new HttpException(400, GetFormatExceptionMessage(0x10));
                }

                if (line.EndsWith("--"))
                {
                    break;
                }

                headerDict.Clear();

                String headerLine;
                while (!String.IsNullOrWhiteSpace(headerLine = ReadLine(stream, readlineBuffer)))
                {
                    var keyVal = FormFieldHeader.ParseKeyValue(headerLine);
                    if (keyVal.Key == null || keyVal.Value == null)
                    {
                        throw new HttpException(400, GetFormatExceptionMessage(0x11));
                    }

                    headerDict.Add(keyVal.Key, keyVal.Value);
                }

                var start = stream.Position;
                var end   = stream.Position;

                while ((line = ReadLine(stream, readlineBuffer)) != null)
                {
                    if (line.StartsWith(String.Format("--{0}", Boundary)))
                    {
                        break;
                    }

                    end = stream.Position;
                }

                stream.Seek(start, SeekOrigin.Begin);
                var field = Create(headerDict, new FrameStream(stream, start, end - start - 2));
                stream.Seek(end, SeekOrigin.Begin);

                ReadLine(stream, readlineBuffer);

                subFields.Add(field);
            }

            _subFields = subFields.ToArray();
        }
コード例 #2
0
ファイル: Extensions.cs プロジェクト: Metapyziks/WebServer
        public static MultipartFormField ReadMultipartForm(this HttpListenerRequest request)
        {
            if (request.ContentType == null)
            {
                throw new NullReferenceException("Request content type was null.");
            }

            var headers = request.Headers.AllKeys.ToDictionary(
                x => x, x => FormFieldHeader.Parse(request.Headers[x]));

            using (var copy = new MemoryStream()) {
                request.InputStream.CopyTo(copy);
                copy.Seek(0, SeekOrigin.Begin);
                return(new MultipartFormField(new FormField.HeaderCollection(headers), copy));
            }
        }