Merge branch 'cecil-light'
[mono.git] / mcs / class / System.Web / System.Web.SessionState_2.0 / SessionIDManager.cs
1 //
2 // System.Web.Compilation.SessionStateItemCollection
3 //
4 // Authors:
5 //   Marek Habersack (grendello@gmail.com)
6 //
7 // (C) 2006 Marek Habersack
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 #if NET_2_0
31 using System.Web;
32 using System.Web.Configuration;
33 using System.Web.Util;
34
35 namespace System.Web.SessionState 
36 {
37         public class SessionIDManager : ISessionIDManager
38         {
39                 SessionStateSection config;
40                 
41                 public SessionIDManager ()
42                 {
43                 }
44
45                 public static int SessionIDMaxLength {
46                         get { return 80; }
47                 }
48
49                 // Todo: find use for the context parameter?
50                 public virtual string CreateSessionID (HttpContext context)
51                 {
52                         return SessionId.Create ();
53                 }
54
55                 public virtual string Decode (string id)
56                 {
57                         return HttpUtility.UrlDecode (id);
58                 }
59
60                 public virtual string Encode (string id)
61                 {
62                         return HttpUtility.UrlEncode (id);
63                 }
64                 
65                 public string GetSessionID (HttpContext context)
66                 {
67                         string ret = null;
68
69                         if (SessionStateModule.IsCookieLess (context, config)) {
70                                 string tmp = context.Request.Headers [SessionStateModule.HeaderName];
71                                 if (tmp != null)
72                                         ret = Decode (tmp);
73                         } else {
74                                 HttpCookie cookie = context.Request.Cookies [config.CookieName];
75                                 if (cookie != null)
76                                         ret = Decode (cookie.Value);
77                         }
78                         
79                         if (ret != null && ret.Length > SessionIDMaxLength)
80                                 throw new HttpException ("The length of the session-identifier value retrieved from the HTTP request exceeds the SessionIDMaxLength value.");
81                         if (!Validate (ret))
82                                 throw new HttpException ("Invalid session ID");
83                         
84                         return ret;
85                 }
86
87                 public void Initialize ()
88                 {
89                         config = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
90                 }
91
92                 public bool InitializeRequest (HttpContext context, bool suppressAutoDetectRedirect, out bool supportSessionIDReissue)
93                 {
94                         // TODO: Implement AutoDetect handling
95                         if (config.CookieLess) {
96                                 supportSessionIDReissue = true;
97                                 return false;
98                         } else {
99                                 supportSessionIDReissue = false;
100                                 return false;
101                         }
102                 }
103
104                 public void RemoveSessionID (HttpContext context)
105                 {
106                         context.Response.Cookies.Remove(config.CookieName);
107                 }
108
109                 // TODO: add code to check whether the response has already been sent
110                 public void SaveSessionID (HttpContext context, string id, out bool redirected, out bool cookieAdded)
111                 {
112                         if (!Validate (id))
113                                 throw new HttpException ("Invalid session ID");
114
115                         HttpRequest request = context.Request;
116                         if (!SessionStateModule.IsCookieLess (context, config)) {
117                                 HttpCookie cookie = new HttpCookie (config.CookieName, id);
118                                 cookie.Path = request.ApplicationPath;
119                                 context.Response.AppendCookie (cookie);
120                                 cookieAdded = true;
121                                 redirected = false;
122                         } else {
123                                 request.SetHeader (SessionStateModule.HeaderName, id);
124                                 cookieAdded = false;
125                                 redirected = true;
126                                 UriBuilder newUri = new UriBuilder (request.Url);
127                                 newUri.Path = UrlUtils.InsertSessionId (id, request.FilePath);
128                                 context.Response.Redirect (newUri.Uri.PathAndQuery, false);
129                         }
130                 }
131
132                 public virtual bool Validate (string id)
133                 {
134                         return true;
135                 }
136         }
137 }
138 #endif