public override int ReadByte() { if (length - position <= 0) { return(-1); } if (bufferContext.Count == 0) { bufferContext.Fill().Wait(); } if (bufferContext.Count == 0) { return(-1); } else { position++; bufferContext.Count--; return(bufferContext.Buffer[bufferContext.Offset++]); } }
public static async Task <bool> ReadHeadersAsync(BufferContext bufferContext, MemoryStream stringBuffer, Dictionary <string, string> headers) { bool cr = false; string line = null; while (true) { // if needed, fill the buffer if (bufferContext.Count <= 0) { await bufferContext.Fill(); } if (bufferContext.Count <= 0) { return(false); } // read a line if (ReadLine(bufferContext, stringBuffer, ref cr)) { string line2 = Encoding.UTF8.GetString(stringBuffer.GetBuffer(), 0, (int)stringBuffer.Length); stringBuffer.SetLength(0); // multi lines header if ((line != null) && (line2.StartsWith(" ") || line2.StartsWith("\t"))) { line += line2; } // new header else { // flush previous line if (line != null) { int pos = line.IndexOf(':'); if ((pos == -1) || (pos == 0)) { throw new Exception("Invalid mime header line. Missing ':'"); } string key = line.Substring(0, pos).ToLower(); string content = line.Substring(pos + 2, line.Length - (pos + 2)); headers[key] = HttpUtility.QuotedPrintableDecode(content); } line = line2; if (line == String.Empty) { return(true); } } } } }
public static async Task <string> ReadLineAsync(BufferContext bufferContext, MemoryStream stringBuffer) { bool cr = false; while (!ReadLine(bufferContext, stringBuffer, ref cr)) { await bufferContext.Fill(); if (bufferContext.Count == 0) { return(null); } } return(Encoding.UTF8.GetString(stringBuffer.GetBuffer(), 0, (int)stringBuffer.Length)); }
public override int ReadByte() { if (end) { return(-1); } while (true) { if (chunkPos < chunkLength) { if (bufferContext.Count == 0) { bufferContext.Fill().Wait(); } if (bufferContext.Count == 0) { end = true; return(-1); } bufferContext.Offset++; bufferContext.Count--; position++; chunkPos++; return(bufferContext.Buffer[bufferContext.Offset - 1]); } // load a new chunk else { stringBuffer.SetLength(0); Task <string> task = HttpUtility.ReadLineAsync(bufferContext, stringBuffer); task.Wait(); string chunkLine = task.Result; if (!firstChunk) { if (chunkLine != String.Empty) { throw new Exception("Invalid chunked Stream"); } stringBuffer.SetLength(0); task = HttpUtility.ReadLineAsync(bufferContext, stringBuffer); task.Wait(); chunkLine = task.Result; } else { firstChunk = false; } if (chunkLine == null) { throw new Exception("Invalid chunked Stream"); } chunkPos = 0; chunkLength = Int32.Parse(chunkLine, System.Globalization.NumberStyles.HexNumber); if (chunkLength == 0) { end = true; return(-1); } } } }