protected virtual ValueTask <bool> LoadContent(WebProgressTask task, NetworkReader reader)
        {
            if (!task.Request.HeaderParameter.TryGetValue("Content-Length", out string?strLength))
            {
                return(new ValueTask <bool>(true));
            }

            if (!int.TryParse(strLength, out int length) || length < 0)
            {
                WebServerLog.Add(ServerLogType.Error, GetType(), "Header", "Bad Request, invalid content length");
                task.Response.StatusCode = HttpStateCode.BadRequest;
                task.NextStage           = ServerStage.CreateResponse;
                return(new ValueTask <bool>(false));
            }

            var content = new IO.ContentStream(reader, length);

            task.Request.Post.SetPost(
                task,
                content,
                task.Request.HeaderParameter.TryGetValue("Content-Type", out string?contentType)
                    ? contentType : null
                );

            return(new ValueTask <bool>(true));
        }
Пример #2
0
        public async Task SetAsync(WebProgressTask task, IO.ContentStream content, string options)
        {
            var      match    = charsetRegex.Match(options);
            Encoding?encoding = null;

            if (match.Success)
            {
                try
                {
                    encoding = Encoding.GetEncoding(match.Groups["charset"].Value);
                }
                catch (Exception e)
                {
                    WebServerLog.Add(ServerLogType.Error, GetType(), "SetPost",
                                     $"invalid encoding {match.Groups["charset"].Value}: {e}");
                }
            }
            encoding ??= Encoding.UTF8;
            var buffer = new byte[content.UnreadData];
            await content.ReadAsync(buffer, 0, buffer.Length).ConfigureAwait(false);

            Set(encoding.GetString(buffer), options);
        }
Пример #3
0
        public async Task SetAsync(WebProgressTask task, IO.ContentStream content, string options)
        {
            var match    = boundaryRegex.Match(options);
            var boundary = match.Success ? match.Groups["name"].Value : "";

            boundary = $"--{boundary}";
            ReadOnlyMemory <byte> rawBoundary = Encoding.UTF8.GetBytes(boundary);

            Entries.Clear();
            using var reader = new NetworkReader(content, null, true);

            // parse the content
            while (true)
            {
                // expect boundary
                if (reader.ReadLine() != boundary)
                {
                    break;
                }

                // read headers until an empty line is found
                var    dict = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase);
                string?line;
                while (!string.IsNullOrWhiteSpace(line = reader.ReadLine()))
                {
                    var header = headerSplit.Match(line);
                    if (!header.Success)
                    {
                        break;
                    }
                    dict.Add(header.Groups["name"].Value, header.Groups["value"].Value);
                }

                var entry = GetEntry(dict);

                var storeInTemp = (AlwaysStoreFiles && entry is FormDataFile) ||
                                  (MaximumCacheSize >= 0 && content.FullLength > MaximumCacheSize);

                // read the content of these entries
                if (storeInTemp)
                {
                    var name = Path.GetTempFileName();
                    using var file = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Write,
                                                    FileShare.None
                                                    );
                    using var stream = StorageMapper?.Invoke(task, file) ?? file;
                    await reader.ReadUntilAsync(rawBoundary, stream).ConfigureAwait(false);

                    entry.Set(new FileInfo(name));
                }
                else
                {
                    entry.Set(await reader.ReadUntilAsync(rawBoundary).ConfigureAwait(false));
                }

                // add new entry
                Entries.Add(entry);
            }

            // there should nothing left but to be sure just discard the rest
            content.Discard();
        }
Пример #4
0
 public Task SetAsync(WebProgressTask task, IO.ContentStream content, string options)
 {
     Data = content;
     return(Task.CompletedTask);
 }
Пример #5
0
 public UnknownPostData(IO.ContentStream data, string?mime)
 {
     MimeType = mime ?? WebServer.MimeType.ApplicationOctetStream;
     Data     = data;
 }