Esempio n. 1
0
        private async Task <byte[]> GetBytesFromMultipart(HttpListenerRequest request)
        {
            var multiparts = await MultipartFormData.ParseAsync(request);

            var multipart = multiparts.FirstOrDefault(t =>
            {
                if (t.Headers.ContainsKey("Content-Disposition") == false)
                {
                    return(false);
                }

                var contentDispositionText  = t.Headers["Content-Disposition"];
                var contentDispositionParts = contentDispositionText.Split(new[] { "; ", ";" }, StringSplitOptions.RemoveEmptyEntries);

                var fileNameMatches = contentDispositionParts.Select(s =>
                                                                     Regex.Match(s, @"^filename=[""']?(?<fileName>[^""']+)[""']?$"));
                return(fileNameMatches.Any(s => s.Success));
            });

            if (multipart == null)
            {
                return(null);
            }

            return(multipart.Data);
        }
        public static async Task<MultipartFormData[]> ParseAsync(HttpListenerRequest request)
        {
            var contentTypeParts = request.ContentType.Split(new[] { "; ", ";" }, StringSplitOptions.None);

            var boundaryMatch = contentTypeParts
                .Select(t => Regex.Match(t, "^boundary=(?<boundary>.+)$", RegexOptions.IgnoreCase))
                .First(t => t.Success);
            var boundaryText = boundaryMatch.Groups["boundary"].Value;

            var contentEncoding = request.ContentEncoding ?? Encoding.UTF8;
            var boundayBytes = contentEncoding.GetBytes(boundaryText);

            var contentBytes = await request.InputStream.ReadToEndAsync();
            var partsBytes = Split(contentBytes, boundayBytes);

            return partsBytes.Select(partBytes =>
            {
                var headerEndBytes = contentEncoding.GetBytes("\r\n\r\n");
                var headerEndIndex = GetSplitIndexes(partBytes, headerEndBytes).FirstOrDefault();

                if (headerEndIndex <= 0) return null;

                var multipartFormData = new MultipartFormData { Headers = new Dictionary<string, string>() };

                var headerContentBytes = new byte[headerEndIndex];
                Array.Copy(partBytes, headerContentBytes, headerEndIndex);

                using (var headerStream = new MemoryStream(headerContentBytes))
                using (var headerReader = new StreamReader(headerStream, contentEncoding))
                {
                    while (true)
                    {
                        var headerLine = headerReader.ReadLine();
                        if (string.IsNullOrEmpty(headerLine)) break;

                        var headerMatch = Regex.Match(headerLine, "^(?<key>[^:]+):(?<value>.+)$");
                        if (headerMatch.Success)
                        {
                            var headerKey = headerMatch.Groups["key"].Value;
                            var headerValue = headerMatch.Groups["value"].Value;
                            multipartFormData.Headers.Add(headerKey, headerValue);
                        }
                    }
                }

                var dataStartIndex = headerEndIndex + headerEndBytes.Length;
                var dataLength = partBytes.Length - dataStartIndex;

                multipartFormData.Data = new byte[dataLength];
                Array.Copy(partBytes, dataStartIndex, multipartFormData.Data, 0, dataLength);

                return multipartFormData;
            })
            .Where(t => t != null).ToArray();
        }
        public static async Task <MultipartFormData[]> ParseAsync(HttpListenerRequest request)
        {
            var contentTypeParts = request.ContentType.Split(new[] { "; ", ";" }, StringSplitOptions.None);

            var boundaryMatch = contentTypeParts
                                .Select(t => Regex.Match(t, "^boundary=(?<boundary>.+)$", RegexOptions.IgnoreCase))
                                .First(t => t.Success);
            var boundaryText = boundaryMatch.Groups["boundary"].Value;

            var contentEncoding = request.ContentEncoding ?? Encoding.UTF8;
            var boundayBytes    = contentEncoding.GetBytes(boundaryText);

            var contentBytes = await request.InputStream.ReadToEndAsync();

            var partsBytes = Split(contentBytes, boundayBytes);

            return(partsBytes.Select(partBytes =>
            {
                var headerEndBytes = contentEncoding.GetBytes("\r\n\r\n");
                var headerEndIndex = GetSplitIndexes(partBytes, headerEndBytes).FirstOrDefault();

                if (headerEndIndex <= 0)
                {
                    return null;
                }

                var multipartFormData = new MultipartFormData {
                    Headers = new Dictionary <string, string>()
                };

                var headerContentBytes = new byte[headerEndIndex];
                Array.Copy(partBytes, headerContentBytes, headerEndIndex);

                using (var headerStream = new MemoryStream(headerContentBytes))
                    using (var headerReader = new StreamReader(headerStream, contentEncoding))
                    {
                        while (true)
                        {
                            var headerLine = headerReader.ReadLine();
                            if (string.IsNullOrEmpty(headerLine))
                            {
                                break;
                            }

                            var headerMatch = Regex.Match(headerLine, "^(?<key>[^:]+):(?<value>.+)$");
                            if (headerMatch.Success)
                            {
                                var headerKey = headerMatch.Groups["key"].Value;
                                var headerValue = headerMatch.Groups["value"].Value;
                                multipartFormData.Headers.Add(headerKey, headerValue);
                            }
                        }
                    }

                var dataStartIndex = headerEndIndex + headerEndBytes.Length;
                var dataLength = partBytes.Length - dataStartIndex;

                multipartFormData.Data = new byte[dataLength];
                Array.Copy(partBytes, dataStartIndex, multipartFormData.Data, 0, dataLength);

                return multipartFormData;
            })
                   .Where(t => t != null).ToArray());
        }