Esempio n. 1
0
 private void Initialize()
 {
     RequestLine.Clear();
     HeaderList.Clear();
     MessageBody = null;
     c           = TextStore.ReadChar();
 }
 protected bool ParseQuotedString()
 {
     if (c == '\"')
     {
         sb.Clear();
         sb.Append('\"');
         c = TextStore.ReadChar();
     }
     else
     {
         return(false);
     }
     while (c != '\"')
     {
         if (ParseQuotedPair())
         {
             continue;
         }
         else if (ParseText())
         {
             continue;
         }
         else
         {
             throw new Exception(ERROR_MALFORMED_QUOTEDSTRING);
         }
     }
     sb.Append('\"');
     c = TextStore.ReadChar();
     return(true);
 }
 private void ParseChunkTrailer()
 {
     while (true)
     {
         var name = ParseFieldName();
         if (name == null)
         {
             return;
         }
         if (c == ':')
         {
             c = TextStore.ReadChar();
         }
         else
         {
             throw new Exception(string.Format(ERROR_MALFORMED_HEADER, Name));
         }
         try
         {
             var value = ParseFieldValue();
             ParseCRLF();
             ((HttpChunkedMessageBody)MessageBody).Trailer.Add(name, value.Trim());
         }
         catch (Exception ex)
         {
             throw new Exception(string.Format(ERROR_MALFORMED_HEADER, Name), ex);
         }
     }
 }
        protected bool ParseHeader()
        {
            var name = ParseFieldName();

            if (name == null)
            {
                return(false);
            }
            if (c == ':')
            {
                c = TextStore.ReadChar();
            }
            else
            {
                throw new Exception(string.Format(ERROR_MALFORMED_HEADER, Name));
            }
            try
            {
                var value = ParseFieldValue();
                ParseCRLF();
                var h = HeaderList.Add(name, value.Trim());
                if (h.Name == HttpHeaderContentType.NAME)
                {
                    CheckEncoding(((HttpHeaderContentType)h).CharSet);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(ERROR_MALFORMED_HEADER, Name), ex);
            }
            return(true);
        }
Esempio n. 5
0
 private void Initialize(HttpMethod method)
 {
     Method = method;
     StatusLine.Clear();
     HeaderList.Clear();
     MessageBody = null;
     c           = TextStore.ReadChar();
 }
Esempio n. 6
0
 private void ParseReasonPhrase()
 {
     sb.Clear();
     while (c != -1 && !HttpChar.IsControl(c))
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     StatusLine.ReasonPhrase = sb.ToString();
 }
 protected void ParseSP()
 {
     if (c == ' ')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_SP);
     }
 }
Esempio n. 8
0
 private void ParseRequestUri()
 {
     if (c == '*')
     {
         RequestLine.RequestUri = "*";
         c = TextStore.ReadChar();
     }
     else if (ParseString())
     {
         RequestLine.RequestUri = sb.ToString();
     }
     else
     {
         throw new Exception(ERROR_MISSING_REQUESTURI);
     }
 }
 protected bool ParseText()
 {
     if (ParseLinearWhiteSpace())
     {
         return(true);
     }
     else if (HttpChar.IsControl(c))
     {
         return(false);
     }
     else
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
         return(true);
     }
 }
 protected bool ParseString()
 {
     if (HttpChar.IsChar(c) && !HttpChar.IsControl(c) && c != HttpChar.SP)
     {
         sb.Clear();
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     else
     {
         return(false);
     }
     while (HttpChar.IsChar(c) && !HttpChar.IsControl(c) && c != HttpChar.SP)
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     return(true);
 }
 protected bool ParseToken()
 {
     if (HttpChar.IsChar(c) && !HttpChar.IsControl(c) && !HttpChar.IsSeparator(c))
     {
         sb.Clear();
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     else
     {
         return(false);
     }
     while (HttpChar.IsChar(c) && !HttpChar.IsControl(c) && !HttpChar.IsSeparator(c))
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     return(true);
 }
 private void ParseChunkExtension()
 {
     while (c == ';')
     {
         c = TextStore.ReadChar();
         string name = ParseChunkExtensionName();
         string value;
         if (c == '=')
         {
             c     = TextStore.ReadChar();
             value = ParseChunkExtensionValue();
         }
         else
         {
             value = null;
         }
         ((HttpChunkedMessageBody)MessageBody).Current.AddParameter(name, value);
     }
 }
 protected bool ParseHexNumber()
 {
     if (HttpChar.IsHexDigit(c))
     {
         sb.Clear();
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     else
     {
         return(false);
     }
     while (HttpChar.IsHexDigit(c))
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
     }
     return(true);
 }
 protected bool ParseLinearWhiteSpace()
 {
     if (c == HttpChar.SP || c == HttpChar.HT)
     {
         sb.Append((char)c);
         c = TextStore.ReadChar();
         return(true);
     }
     else if (c == HttpChar.CR)
     {
         c = TextStore.ReadChar();
         if (c == HttpChar.LF)
         {
             c = TextStore.ReadChar();
         }
         else
         {
             TextStore.UnreadChar(c);
             c = HttpChar.CR;
             return(false);
         }
         if (c == HttpChar.SP || c == HttpChar.HT)
         {
             sb.Append('\r');
             sb.Append('\n');
             sb.Append((char)c);
             c = TextStore.ReadChar();
             return(true);
         }
         else
         {
             TextStore.UnreadChars(HttpChar.LF, c);
             c = HttpChar.CR;
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
 protected void ParseCRLF(bool bReadNext)
 {
     if (c == '\r')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_CRLF);
     }
     if (c == '\n')
     {
         if (bReadNext)
         {
             c = TextStore.ReadChar();
         }
     }
     else
     {
         throw new Exception(ERROR_MISSING_LF);
     }
 }
 protected bool ParseQuotedPair()
 {
     if (c == '\\')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         return(false);
     }
     if (HttpChar.IsChar(c))
     {
         sb.Append('\\');
         sb.Append((char)c);
         c = TextStore.ReadChar();
         return(true);
     }
     else
     {
         throw new Exception(ERROR_MALFORMED_QUOTEDPAIR);
     }
 }
 protected string ParseFieldValue()
 {
     sb.Clear();
     while (true)
     {
         if (c == '\"')
         {
             sb.Append('\"');
             c = TextStore.ReadChar();
             while (c != '\"')
             {
                 if (ParseQuotedPair())
                 {
                     continue;
                 }
                 else if (ParseText())
                 {
                     continue;
                 }
                 else
                 {
                     throw new Exception(ERROR_MALFORMED_QUOTEDSTRING);
                 }
             }
             sb.Append('\"');
             c = TextStore.ReadChar();
         }
         else if (ParseText())
         {
             continue;
         }
         else
         {
             break;
         }
     }
     return(sb.ToString());
 }
 private void ParseChunkedBody()
 {
     MessageBody = new HttpChunkedMessageBody();
     c           = TextStore.ReadChar();
     while (true)
     {
         var chunkSize = ParseChunkSize();
         ParseChunkExtension();
         ParseChunkCRLF(false);
         if (chunkSize == 0)
         {
             OnReceiveChunk?.Invoke(((HttpChunkedMessageBody)MessageBody).Current);
             break;
         }
         ParseChunkData(chunkSize);
         c = TextStore.ReadChar();
         ParseChunkCRLF(true);
         OnReceiveChunk?.Invoke(((HttpChunkedMessageBody)MessageBody).Current);
     }
     c = TextStore.ReadChar();
     ParseChunkTrailer();
     ParseChunkCRLF(false);
     OnReceiveChunkTrailer?.Invoke(((HttpChunkedMessageBody)MessageBody).Trailer);
 }
 protected void ParseHttpVersion()
 {
     if (c == 'H')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_HTTPVERSION);
     }
     if (c == 'T')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_HTTPVERSION);
     }
     if (c == 'T')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_HTTPVERSION);
     }
     if (c == 'P')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MISSING_HTTPVERSION);
     }
     if (c == '/')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MALFORMED_HTTPVERSION);
     }
     if (ParseNumber())
     {
         ProtocolVersion.Major = int.Parse(sb.ToString());
     }
     else
     {
         throw new Exception(ERROR_MALFORMED_HTTPVERSION);
     }
     if (c == '.')
     {
         c = TextStore.ReadChar();
     }
     else
     {
         throw new Exception(ERROR_MALFORMED_HTTPVERSION);
     }
     if (ParseNumber())
     {
         ProtocolVersion.Minor = int.Parse(sb.ToString());
     }
     else
     {
         throw new Exception(ERROR_MALFORMED_HTTPVERSION);
     }
 }