Пример #1
0
 public WebSessionContainer <TSession> GetSessionContainer(WebRequest request, WebResponse response)
 {
     lock (SessionsSyncRoot)
     {
         // Periodically purge expired sessions.
         DateTime now = DateTime.Now;
         if (--_PurgeCountdown <= 0)
         {
             PurgeExpiredSessions(now);
             _PurgeCountdown = _PurgeCountdownMax;
         }
         // Look for an existing session.
         HttpCookie sessionCookie = request.Cookies[SessionCookieName];
         WebSessionContainer <TSession> sessionContainer;
         if (sessionCookie != null)
         {
             if (_Sessions.TryGetValue(sessionCookie.Value, out sessionContainer))
             {
                 if (SessionIsValid(response, now, sessionCookie, sessionContainer))
                 {
                     return(sessionContainer);
                 }
             }
         }
         // Start a new session.
         string sessionCookieValue = MakeSessionKey(response);
         sessionContainer = new WebSessionContainer <TSession>(response.Con.RemoteAddress);
         _Sessions[sessionCookieValue] = sessionContainer;
         sessionCookie = new HttpCookie(SessionCookieName, sessionCookieValue);
         response.Cookies.Add(sessionCookie);
         return(sessionContainer);
     }
 }
Пример #2
0
        public void ProcessRequest(HttpContext aspNetContext)
        {
            Connection con = new Connection(aspNetContext.Response.OutputStream,
                                            aspNetContext.Request.UserHostAddress, 0);

            // Construct a copy of the HttpRequest.Headers collection, because the one created by ASP.NET
            // is automagically modified whenever the response collection created by ASP.NET is modified.
            // There is no good way to mimic this behavior when running in our own web server, so to
            // maintain compatibility with our own server we use a copy of the ASP.NET request headers
            // so they won't be modified when we add to the response headers.
            NameValueCollection requestHeaders       = new NameValueCollection();
            NameValueCollection aspNetRequestHeaders = aspNetContext.Request.Headers;

            foreach (string headerName in aspNetRequestHeaders)
            {
                foreach (string value in aspNetRequestHeaders.GetValues(headerName))
                {
                    requestHeaders.Add(headerName, value);
                }
            }
            WebRequest request = new WebRequest(aspNetContext.Request.RequestType, requestHeaders,
                                                aspNetContext.Request.RawUrl, aspNetContext.Request.Path,
                                                aspNetContext.Request.QueryString, con.RemoteAddress);

            for (int fileIndex = 0; fileIndex < aspNetContext.Request.Files.Count; fileIndex++)
            {
                string          fieldName  = aspNetContext.Request.Files.AllKeys[fileIndex];
                HttpPostedFile  aspNetFile = aspNetContext.Request.Files[fileIndex];
                WebUploadedFile file       = new WebUploadedFile(aspNetFile.FileName, aspNetFile.InputStream,
                                                                 aspNetFile.ContentLength, aspNetFile.ContentType);
                request.UploadedFiles.Add(fieldName, file);
            }

            WebResponse response = new WebResponse(con, aspNetContext.Response.Headers,
                                                   aspNetContext.Response.Cookies, false);
            TSiteData siteData = (TSiteData)aspNetContext.Application[
                AspNetHttpModule <TSiteData, TSession> .SiteDataKey];
            WebSessionContainer <TSession>   sessionContainer = siteData.GetSessionContainer(request, response);
            WebContext <TSiteData, TSession> webContext       = new WebContext <TSiteData, TSession>(
                request, response, new ServerUtilities(new NullDiagOutput()), new SiteUtilities(aspNetContext.Server),
                siteData, sessionContainer.Session);

            WebPage <TSiteData, TSession> page = PageFactory.GetInstance(webContext);

            page.Process(webContext);
        }
Пример #3
0
        public void ExecuteRequest(WebRequest request, WebResponse response, WebPortListener server)
        {
            WebSessionContainer <TSession> sessionContainer = _SiteData.GetSessionContainer(request, response);
            TSession session = sessionContainer.Session;
            WebContext <TSiteData, TSession> context = new WebContext <TSiteData, TSession>(request, response, server,
                                                                                            new SiteUtilities(_SiteRoot), _SiteData, session);

            server.WriteDiagMessage(context.Request.Verb + " " + context.Request.RequestURI);
            IWebPageFactory <TSiteData, TSession> matchingHandler = null;

            foreach (IWebPageHandler <TSiteData, TSession> handler in _WebPageHandlers)
            {
                if (handler.IsMatch(context.Request.AbsoluteUriPath))
                {
                    matchingHandler = handler.Factory;
                    break;
                }
            }
            if (matchingHandler == null)
            {
                throw new WebExceptions.ResourceNotFound(request.RequestURI);
            }
            switch (context.Request.Verb)
            {
            case HttpVerbs.Get:
                WebPage <TSiteData, TSession> getPage = matchingHandler.GetInstance(context);
                getPage.Process(context);
                response.FlushBody(server);
                break;

            case HttpVerbs.Head:
                WebPage <TSiteData, TSession> headPage = matchingHandler.GetInstance(context);
                headPage.Process(context);
                break;

            case HttpVerbs.Post:
                WebPage <TSiteData, TSession> postPage = matchingHandler.GetInstance(context);
                postPage.Process(context);
                response.FlushBody(server);
                break;

            default:
                throw new WebExceptions.NotImplemented("Verb " + context.Request.Verb + " not supported");
            }
        }
Пример #4
0
 private bool SessionIsValid(WebResponse response, DateTime now, HttpCookie sessionCookie,
                             WebSessionContainer <TSession> sessionContainer)
 {
     // Has session expired?
     if (now.Subtract(sessionContainer.LastAccess).CompareTo(_ExpiresAfter) > 0)
     {
         // Remove it and say to create a new session.
         _Sessions.Remove(sessionCookie.Value);
     }
     else
     {
         // Make sure this connection is from the same remote address
         // as was used to create the session.
         if (sessionContainer.RemoteAddress == response.Con.RemoteAddress)
         {
             sessionContainer.LastAccess = now;
             return(true);
         }
     }
     return(false);
 }