public async Task ReadLineWhenReaderNoBuffer() { using (var ms = new MemoryStream(Encoding.Default.GetBytes(readerTestString))) { RebufferableStreamReader reader = new RebufferableStreamReader(ms, Encoding.Default); string line = await reader.ReadLineAsync(); string nextLine = await reader.ReadLineAsync(); Assert.AreEqual(line, "ni hao wo shi zhanghao"); Assert.AreEqual(nextLine, "this is test!"); } }
public async Task ParseAsync(string boundaryKey = null) { if (string.IsNullOrEmpty(boundaryKey)) { boundaryKey = await DetectBoundaryKey(reader); } boundary = string.Format("--{0}", boundaryKey); endBoundary = string.Format("--{0}--", boundaryKey); boundaryBytes = encoding.GetBytes(boundary); endBoundaryBytes = encoding.GetBytes(endBoundary); string line = await reader.ReadLineAsync(); while (!this.readEndBoundary) { string partHeader = string.Empty; while ((line = await reader.ReadLineAsync()) != string.Empty) { if (line != boundary) { partHeader += line; } } Dictionary <string, string> partHeaders; try { partHeaders = partHeader.Replace("\r\n", ";").Split(';') .Select(x => x.Split(new[] { ':', '=' })) .ToDictionary( x => x[0].Trim().Replace("\"", string.Empty).ToLower(), x => x[1].Trim().Replace("\"", string.Empty)); } catch { throw new HttpRequestParseException("http报文实体格式错误"); } if (!partHeaders.ContainsKey("filename")) { await ParseParameterPartAsync(reader, partHeaders); } else { await ParseFilePartAsync(reader, partHeaders); } } }
private async Task ParseParameterPartAsync(RebufferableStreamReader reader, Dictionary <string, string> partHeaders) { StringBuilder value = new StringBuilder(); string line = await reader.ReadLineAsync(); while (line != endBoundary && line != boundary) { value.Append(line); line = await reader.ReadLineAsync(); } if (line == endBoundary) { this.readEndBoundary = true; } Parameters[partHeaders["name"]] = value.ToString(); }
private async Task <string> DetectBoundaryKey(RebufferableStreamReader reader) { var line = await reader.ReadLineAsync(); var boundary = string.Concat(line.Skip(2)); reader.Rebuffer(encoding.GetBytes("--" + boundary + "\r\n")); return(boundary); }
public async Task ReadLineWhenReaderHaveBufferAndNoLineInBuffer() { using (var ms = new MemoryStream(Encoding.Default.GetBytes(readerTestString))) { RebufferableStreamReader reader = new RebufferableStreamReader(ms, Encoding.Default); reader.Rebuffer(Encoding.Default.GetBytes(noLineBufferTestString)); var line = await reader.ReadLineAsync(); var secondLine = await reader.ReadLineAsync(); var thirdLine = await reader.ReadLineAsync(); Assert.AreEqual(line, noLineBufferTestString + "ni hao wo shi zhanghao"); Assert.AreEqual(secondLine, "this is test!"); Assert.IsTrue(string.IsNullOrEmpty(thirdLine)); } }
public async Task ReadLineWhenLineInBuffer() { using (var ms = new MemoryStream(Encoding.Default.GetBytes(readerTestString))) { RebufferableStreamReader reader = new RebufferableStreamReader(ms, Encoding.Default); reader.Rebuffer(Encoding.Default.GetBytes(lineBufferTestString)); var line = await reader.ReadLineAsync(); var secondLine = await reader.ReadLineAsync(); var thirdLine = await reader.ReadLineAsync(); Assert.AreEqual(line, "hahaha"); Assert.AreEqual(secondLine, "hehe" + "ni hao wo shi zhanghao"); Assert.AreEqual(thirdLine, "this is test!"); reader.Rebuffer(Encoding.Default.GetBytes("hello world")); var fourLine = await reader.ReadLineAsync(); Assert.AreEqual(fourLine, "hello world"); } }