public HttpListenerWebResponse(HttpListenerWebRequest httpListenerRequest)
 {
     #if DEBUG
     if (HttpTraceHelper.InternalLog.TraceVerbose)
     {
         HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) + "::.ctor() keepAlive:" + httpListenerRequest.KeepAlive.ToString());
     }
     #endif
     m_HttpListenerWebRequest = httpListenerRequest;
     m_Date = DateTime.MinValue;
     m_Headers = new WebHeaderCollection();
     m_ProtocolVersion = new Version(1,1);
     m_SentHeaders = false;
     m_KeepAlive = httpListenerRequest.KeepAlive;
 }
        public HttpListenerWebResponse(HttpListenerWebRequest httpListenerRequest)
        {
#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("HttpListenerWebResponse#" + HttpTraceHelper.HashString(this) +
                                          "::.ctor() keepAlive:" + httpListenerRequest.KeepAlive);
            }
#endif
            Request         = httpListenerRequest;
            Date            = DateTime.MinValue;
            Headers         = new WebHeaderCollection();
            ProtocolVersion = new Version(1, 1);
            SentHeaders     = false;
            KeepAlive       = httpListenerRequest.KeepAlive;
        }
Ejemplo n.º 3
0
        public HttpListenerWebRequest GetNextRequest()
        {
            HttpListenerWebRequest httpListenerRequest = null;

            lock (_pendingRequests)
            {
#if DEBUG
                if (HttpTraceHelper.InternalLog.TraceVerbose)
                {
                    HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                              "::GetNextRequest() _pendingRequests.Count: " + _pendingRequests.Count);
                }
#endif

                if (_pendingRequests.Count > 0)
                {
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                                  "::GetNextRequest() calling Dequeue()");
                    }
#endif

                    httpListenerRequest = (HttpListenerWebRequest)_pendingRequests.Dequeue();

#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine("HttpWebListener#" + HttpTraceHelper.HashString(this) +
                                                  "::GetNextRequest() calling Reset()");
                    }
#endif

                    _requestReady.Reset();
                }
            }
            return(httpListenerRequest);
        }
        /// <summary>
        /// parse strictly, any exceptions will be treated as errors
        /// </summary>
        /// <returns></returns>
        public ParseState Parse()
        {
            string header;

#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine(
                    string.Format(
                        "ConnectionState#{0}::Parse() entering _parsedOffset:{1} _eofOffset:{2} _parserState:{3}",
                        HttpTraceHelper.HashString(this), ParsedOffset, EndOfOffset, ParserState));
            }
#endif

            while (ParsedOffset < EndOfOffset)
            {
                int first;
                int last;
                switch (ParserState)
                {
                    case ParseState.None:
                        Request = new HttpListenerWebRequest(this);
                        ParserState = ParseState.Method;
                        goto case ParseState.Method;
                    case ParseState.Method:
                        first = SkipWhite(ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindWhite(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        Request.Method = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() Method:{1}",
                                                                    HttpTraceHelper.HashString(this), Request.Method));
                        }
#endif
                        ParsedOffset = last;
                        ParserState = ParseState.Uri;
                        goto case ParseState.Uri;
                    case ParseState.Uri:
                        first = SkipWhite(ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindWhite(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        Request.RelativeUri = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() RelativeUri:{1}",
                                                                    HttpTraceHelper.HashString(this),
                                                                    Request.RelativeUri));
                        }
#endif
                        ParsedOffset = last;
                        ParserState = ParseState.Version;
                        goto case ParseState.Version;
                    case ParseState.Version:
                        first = SkipWhite(ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindCrLf(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        if (last - first < 8 || ConnectionBuffer[first] != (byte) 'H' ||
                            ConnectionBuffer[first + 1] != (byte) 'T' ||
                            ConnectionBuffer[first + 2] != (byte) 'T' || ConnectionBuffer[first + 3] != (byte) 'P' ||
                            ConnectionBuffer[first + 4] != (byte) '/' || ConnectionBuffer[first + 6] != (byte) '.')
                        {
                            return ParseState.Error;
                        }
                        Request.ProtocolVersion = new Version(((ConnectionBuffer[first + 5] - '0')),
                                                              ((ConnectionBuffer[first + 7] - '0')));
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() ProtocolVersion:{1}",
                                                                    HttpTraceHelper.HashString(this),
                                                                    Request.ProtocolVersion));
                        }
#endif
                        ParsedOffset = last + 2;
                        Request.Headers = new WebHeaderCollection();
                        ParserState = ParseState.Headers;
                        goto case ParseState.Headers;
                    case ParseState.Headers:
                        first = SkipWhite(ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindCrLf(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        header = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
                        var column = header.IndexOf(':');
                        if (column == -1)
                        {
                            ParserState = ParseState.Error;
                            return ParseState.Error;
                        }
                        var headerName = header.Substring(0, column).Trim();
                        var headerValue = header.Substring(column + 1).Trim();
                        Request.Headers.Add(headerName, headerValue);
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(
                                string.Format("ConnectionState#{0}::Parse() headerName:[{1}] headerValue:[{2}]",
                                              HttpTraceHelper.HashString(this), headerName, headerValue));
                        }
#endif
                        if (ConnectionBuffer[last + 2] == (byte) '\r' && ConnectionBuffer[last + 3] == (byte) '\n')
                        {
                            var valid = Request.InterpretHeaders();
#if DEBUG
                            if (HttpTraceHelper.InternalLog.TraceVerbose)
                            {
                                HttpTraceHelper.WriteLine(
                                    string.Format("ConnectionState#{0}::Parse() InterpretHeaders() returned:{1}",
                                                  HttpTraceHelper.HashString(this), valid));
                            }
#endif
                            if (!valid)
                            {
                                ParserState = ParseState.Error;
                                return ParseState.Error;
                            }

                            if (Listener.Auto100Continue)
                            {
                                Request.Send100Continue();
                            }

                            if (ParserState == ParseState.Headers)
                            {
                                ParserState = ParseState.Done;
                            }
                            ParsedOffset = last + 4;
#if DEBUG
                            if (HttpTraceHelper.InternalLog.TraceVerbose)
                            {
                                HttpTraceHelper.WriteLine(
                                    string.Format(
                                        "ConnectionState#{0}::Parse() returning ParseState.Done _parsedOffset:{1} _eofOffset:{2}",
                                        HttpTraceHelper.HashString(this), ParsedOffset, EndOfOffset));
                            }
#endif
                            return ParseState.Done;
                        }
                        ParsedOffset = last + 2;
                        break;
                    default:
                        ParserState = ParseState.Error;
                        return ParseState.Error;
                }
            }

            return ParseState.Continue;
        }
        public ParseState Parse()
        {
            //
            // parse strictly, any exceptions will be treated as errors
            //
            int first, last, column;
            string header, headerName, headerValue;

            #if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() entering m_ParsedOffset:" + m_ParsedOffset.ToString() + " m_EofOffset:" + m_EofOffset.ToString() + " m_ParserState:" + m_ParserState.ToString());
            }
            #endif

            while (m_ParsedOffset < m_EofOffset)
            {
                switch (m_ParserState)
                {
                    case ParseState.None:
                        m_HttpListenerWebRequest = new HttpListenerWebRequest(this);
                        m_ParserState = ParseState.Method;
                        goto case ParseState.Method;
                    case ParseState.Method:
                        first = SkipWhite(m_ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindWhite(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        m_HttpListenerWebRequest.Method = Encoding.ASCII.GetString(m_Buffer, first, last-first);
            #if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() Method:" + m_HttpListenerWebRequest.Method);
                        }
            #endif
                        m_ParsedOffset = last;
                        m_ParserState = ParseState.Uri;
                        goto case ParseState.Uri;
                    case ParseState.Uri:
                        first = SkipWhite(m_ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindWhite(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        m_HttpListenerWebRequest.RelativeUri = Encoding.ASCII.GetString(m_Buffer, first, last-first);
            #if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() RelativeUri:" + m_HttpListenerWebRequest.RelativeUri);
                        }
            #endif
                        m_ParsedOffset = last;
                        m_ParserState = ParseState.Version;
                        goto case ParseState.Version;
                    case ParseState.Version:
                        first = SkipWhite(m_ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindCrLf(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        if (last-first < 8 || m_Buffer[first] != (byte)'H' || m_Buffer[first+1] != (byte)'T' || m_Buffer[first+2] != (byte)'T' || m_Buffer[first+3] != (byte)'P' || m_Buffer[first+4] != (byte)'/' || m_Buffer[first+6] != (byte)'.')
                        {
                            return ParseState.Error;
                        }
                        m_HttpListenerWebRequest.ProtocolVersion = new Version(((int)(m_Buffer[first+5]-'0')), ((int)(m_Buffer[first+7]-'0')));
            #if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() ProtocolVersion:" + m_HttpListenerWebRequest.ProtocolVersion.ToString());
                        }
            #endif
                        m_ParsedOffset = last+2;
                        m_HttpListenerWebRequest.Headers = new WebHeaderCollection();
                        m_ParserState = ParseState.Headers;
                        goto case ParseState.Headers;
                    case ParseState.Headers:
                        first = SkipWhite(m_ParsedOffset);
                        if (first == -1)
                        {
                            return ParseState.Continue;
                        }
                        last = FindCrLf(first);
                        if (last == -1)
                        {
                            return ParseState.Continue;
                        }
                        header = Encoding.ASCII.GetString(m_Buffer, first, last-first);
                        column = header.IndexOf(':');
                        if (column == -1)
                        {
                            m_ParserState = ParseState.Error;
                            return ParseState.Error;
                        }
                        headerName = header.Substring(0, column).Trim();
                        headerValue = header.Substring(column+1).Trim();
                        m_HttpListenerWebRequest.Headers.Add(headerName, headerValue);
            #if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() headerName:[" + headerName + "] headerValue:[" + headerValue + "]");
                        }
            #endif
                        if (m_Buffer[last+2] == (byte)'\r' && m_Buffer[last+3] == (byte)'\n')
                        {
                            bool valid = m_HttpListenerWebRequest.InterpretHeaders();
            #if DEBUG
                            if (HttpTraceHelper.InternalLog.TraceVerbose)
                            {
                                HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() InterpretHeaders() returned:" + valid.ToString());
                            }
            #endif
                            if (!valid)
                            {
                                m_ParserState = ParseState.Error;
                                return ParseState.Error;
                            }

                            if (Listener.Auto100Continue)
                            {
                                m_HttpListenerWebRequest.Send100Continue();
                            }

                            if (m_ParserState == ParseState.Headers)
                            {
                                m_ParserState = ParseState.Done;
                            }
                            m_ParsedOffset = last+4;
            #if DEBUG
                            if (HttpTraceHelper.InternalLog.TraceVerbose)
                            {
                                HttpTraceHelper.WriteLine("ConnectionState#" + HttpTraceHelper.HashString(this) + "::Parse() returning ParseState.Done m_ParsedOffset:" + m_ParsedOffset.ToString() + " m_EofOffset:" + m_EofOffset.ToString());
                            }
            #endif
                            return ParseState.Done;
                        }
                        m_ParsedOffset = last+2;
                        break;
                    default:
                        m_ParserState = ParseState.Error;
                        return ParseState.Error;
                }
            }

            return ParseState.Continue;
        }
Ejemplo n.º 6
0
 public void Reset()
 {
     httpRequest = null;
     requestStream = null;
     httpResponse = null;
     readBytes = 0;
     buffer = new MemoryStream();
 }
Ejemplo n.º 7
0
        /// <summary>
        /// parse strictly, any exceptions will be treated as errors
        /// </summary>
        /// <returns></returns>
        public ParseState Parse()
        {
            string header;

#if DEBUG
            if (HttpTraceHelper.InternalLog.TraceVerbose)
            {
                HttpTraceHelper.WriteLine(
                    string.Format(
                        "ConnectionState#{0}::Parse() entering _parsedOffset:{1} _eofOffset:{2} _parserState:{3}",
                        HttpTraceHelper.HashString(this), ParsedOffset, EndOfOffset, ParserState));
            }
#endif

            while (ParsedOffset < EndOfOffset)
            {
                int first;
                int last;
                switch (ParserState)
                {
                case ParseState.None:
                    Request     = new HttpListenerWebRequest(this);
                    ParserState = ParseState.Method;
                    goto case ParseState.Method;

                case ParseState.Method:
                    first = SkipWhite(ParsedOffset);
                    if (first == -1)
                    {
                        return(ParseState.Continue);
                    }
                    last = FindWhite(first);
                    if (last == -1)
                    {
                        return(ParseState.Continue);
                    }
                    Request.Method = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() Method:{1}",
                                                                HttpTraceHelper.HashString(this), Request.Method));
                    }
#endif
                    ParsedOffset = last;
                    ParserState  = ParseState.Uri;
                    goto case ParseState.Uri;

                case ParseState.Uri:
                    first = SkipWhite(ParsedOffset);
                    if (first == -1)
                    {
                        return(ParseState.Continue);
                    }
                    last = FindWhite(first);
                    if (last == -1)
                    {
                        return(ParseState.Continue);
                    }
                    Request.RelativeUri = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() RelativeUri:{1}",
                                                                HttpTraceHelper.HashString(this),
                                                                Request.RelativeUri));
                    }
#endif
                    ParsedOffset = last;
                    ParserState  = ParseState.Version;
                    goto case ParseState.Version;

                case ParseState.Version:
                    first = SkipWhite(ParsedOffset);
                    if (first == -1)
                    {
                        return(ParseState.Continue);
                    }
                    last = FindCrLf(first);
                    if (last == -1)
                    {
                        return(ParseState.Continue);
                    }
                    if (last - first < 8 || ConnectionBuffer[first] != (byte)'H' ||
                        ConnectionBuffer[first + 1] != (byte)'T' ||
                        ConnectionBuffer[first + 2] != (byte)'T' || ConnectionBuffer[first + 3] != (byte)'P' ||
                        ConnectionBuffer[first + 4] != (byte)'/' || ConnectionBuffer[first + 6] != (byte)'.')
                    {
                        return(ParseState.Error);
                    }
                    Request.ProtocolVersion = new Version(((ConnectionBuffer[first + 5] - '0')),
                                                          ((ConnectionBuffer[first + 7] - '0')));
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(string.Format("ConnectionState#{0}::Parse() ProtocolVersion:{1}",
                                                                HttpTraceHelper.HashString(this),
                                                                Request.ProtocolVersion));
                    }
#endif
                    ParsedOffset    = last + 2;
                    Request.Headers = new WebHeaderCollection();
                    ParserState     = ParseState.Headers;
                    goto case ParseState.Headers;

                case ParseState.Headers:
                    first = SkipWhite(ParsedOffset);
                    if (first == -1)
                    {
                        return(ParseState.Continue);
                    }
                    last = FindCrLf(first);
                    if (last == -1)
                    {
                        return(ParseState.Continue);
                    }
                    header = Encoding.ASCII.GetString(ConnectionBuffer, first, last - first);
                    var column = header.IndexOf(':');
                    if (column == -1)
                    {
                        ParserState = ParseState.Error;
                        return(ParseState.Error);
                    }
                    var headerName  = header.Substring(0, column).Trim();
                    var headerValue = header.Substring(column + 1).Trim();
                    Request.Headers.Add(headerName, headerValue);
#if DEBUG
                    if (HttpTraceHelper.InternalLog.TraceVerbose)
                    {
                        HttpTraceHelper.WriteLine(
                            string.Format("ConnectionState#{0}::Parse() headerName:[{1}] headerValue:[{2}]",
                                          HttpTraceHelper.HashString(this), headerName, headerValue));
                    }
#endif
                    if (ConnectionBuffer[last + 2] == (byte)'\r' && ConnectionBuffer[last + 3] == (byte)'\n')
                    {
                        var valid = Request.InterpretHeaders();
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(
                                string.Format("ConnectionState#{0}::Parse() InterpretHeaders() returned:{1}",
                                              HttpTraceHelper.HashString(this), valid));
                        }
#endif
                        if (!valid)
                        {
                            ParserState = ParseState.Error;
                            return(ParseState.Error);
                        }

                        if (Listener.Auto100Continue)
                        {
                            Request.Send100Continue();
                        }

                        if (ParserState == ParseState.Headers)
                        {
                            ParserState = ParseState.Done;
                        }
                        ParsedOffset = last + 4;
#if DEBUG
                        if (HttpTraceHelper.InternalLog.TraceVerbose)
                        {
                            HttpTraceHelper.WriteLine(
                                string.Format(
                                    "ConnectionState#{0}::Parse() returning ParseState.Done _parsedOffset:{1} _eofOffset:{2}",
                                    HttpTraceHelper.HashString(this), ParsedOffset, EndOfOffset));
                        }
#endif
                        return(ParseState.Done);
                    }
                    ParsedOffset = last + 2;
                    break;

                default:
                    ParserState = ParseState.Error;
                    return(ParseState.Error);
                }
            }

            return(ParseState.Continue);
        }