예제 #1
0
        public static async Task <string> ReadLineAsync(ISharedBufferStream stream, MemoryStream stringBuffer)
        {
            bool cr   = false;
            int  used = 0;
            ArraySegment <byte> segment = await stream.SharedBufferReadAsync(Int32.MaxValue);

            if (segment.Count == 0)
            {
                return(null);
            }
            while (!ReadLine(segment, stringBuffer, ref cr, out used))
            {
                segment = await stream.SharedBufferReadAsync(Int32.MaxValue);

                if (segment.Count == 0)
                {
                    return(null);
                }
            }
            if (used != segment.Count)
            {
                stream.SharedBufferRewind(segment.Count - used);
            }
            return(Encoding.UTF8.GetString(stringBuffer.GetBuffer(), 0, (int)stringBuffer.Length));
        }
 public MultipartReader(Stream stream, string boundary)
 {
     this.stream = stream as ISharedBufferStream;
     if (this.stream == null)
     {
         this.stream = new LengthLimitedStream(stream, Int64.MaxValue);
     }
     this.boundary = boundary;
 }
예제 #3
0
        public static async Task <bool> ReadHeadersAsync(ISharedBufferStream stream, Dictionary <string, string> headers)
        {
            bool   flushNeeded = false;
            string line        = null;

            while (true)
            {
                string line2 = await ReadLineAsync(stream);

                if (line2 == null)
                {
                    throw new Exception("Invalid headers");
                }
                else if (line2.StartsWith(" ") || line2.StartsWith("\t"))
                {
                    if (line == null)
                    {
                        line = line2;
                    }
                    else
                    {
                        line += line2;
                    }
                    line2 = null;
                }
                else
                {
                    flushNeeded = true;
                }

                // flush previous line
                if (flushNeeded)
                {
                    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 BoundaryStream(ISharedBufferStream stream, string boundary)
 {
     this.stream   = stream;
     boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary);
 }
예제 #5
0
 public static Task <string> ReadLineAsync(ISharedBufferStream stream)
 {
     return(ReadLineAsync(stream, new MemoryStream(128)));
 }