Example #1
0
        /// <summary>
        /// Clear everything in the request
        /// </summary>
        public void Clear()
        {
            _body.Dispose();
            _body          = new MemoryStream();
            _contentLength = 0;
            _method        = string.Empty;
            _uri           = null;
            _queryString   = HttpInput.Empty;
            _bodyBytesLeft = 0;
            _headers.Clear();
            _connection = ConnectionType.Close;
            _param.SetForm(HttpInput.Empty);
            _param.SetQueryString(HttpInput.Empty);
            _form.Clear();

            AcceptTypes = null;
            Cookies     = null;
            IsAjax      = false;
            UriParts    = null;
        }
Example #2
0
 ///<summary>
 /// Cookies
 ///</summary>
 ///<param name="cookies">the cookies</param>
 public void SetCookies(RequestCookies cookies)
 {
     Cookies = cookies;
 }
        private void ProcessRequest(IHttpClientContext context, IHttpRequest request)
        {
            IHttpResponse response = request.CreateResponse(context);

            try
            {
                foreach (IRule rule in _rules)
                {
                    if (!rule.Process(request, response))
                    {
                        continue;
                    }
                    response.Send();
                    return;
                }

                // load cookies if the exist.
                RequestCookies cookies = request.Headers["cookie"] != null
                                                                                         ? new RequestCookies(request.Headers["cookie"])
                                                                                         : new RequestCookies(string.Empty);

                request.SetCookies(cookies);

                IHttpSession session;
                if (cookies[_sessionCookieName] != null)
                {
                    string sessionCookie = cookies[_sessionCookieName].Value;

                    // there's a bug somewhere which f***s up headers which can render the session cookie useless.
                    // therefore let's consider the session cookie as not set if that have happened.
                    if (sessionCookie.Length > 40)
                    {
                        LogWriter.Write(this, LogPrio.Error, "Session cookie is invalid: " + sessionCookie);
                        cookies.Remove(_sessionCookieName);
                        _sessionStore.Remove(sessionCookie);                         // free the session cookie (and thus generating a new one).
                        session = _sessionStore.Create();
                    }
                    else
                    {
                        session = _sessionStore.Load(sessionCookie) ??
                                  _sessionStore.Create(sessionCookie);
                    }
                }
                else
                {
                    session = _sessionStore.Create();
                }

                HandleRequest(context, request, response, session);
            }
            catch (Exception err)
            {
                if (_exceptionHandler == null)
#if DEBUG
                { throw; }
#else
                {
                    WriteLog(LogPrio.Fatal, err.Message);
                    return;
                }
#endif
                _exceptionHandler(this, err);

                Exception e = err;
                while (e != null)
                {
                    if (e is SocketException)
                    {
                        return;
                    }

                    e = e.InnerException;
                }

                try
                {
#if DEBUG
                    context.Respond("HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error", err.ToString(), "text/plain");
#else
                    context.Respond("HTTP/1.0", HttpStatusCode.InternalServerError, "Internal server error");
#endif
                }
                catch (Exception err2)
                {
                    LogWriter.Write(this, LogPrio.Fatal, "Failed to respond on message with Internal Server Error: " + err2);
                }
            }
        }