forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
101 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,102 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.Net; | ||
using System.Text; | ||
using System.Web; | ||
using ServiceStack.Common.Web; | ||
|
||
namespace ServiceStack.ServiceHost | ||
{ | ||
public class Cookies : ICookies | ||
{ | ||
readonly IHttpResponse httpRes; | ||
private static readonly DateTime Session = DateTime.MinValue; | ||
private static readonly DateTime Permanent = DateTime.UtcNow.AddYears(20); | ||
private const string RootPath = "/"; | ||
public class Cookies : ICookies | ||
{ | ||
readonly IHttpResponse httpRes; | ||
private static readonly DateTime Session = DateTime.MinValue; | ||
private static readonly DateTime Permanent = DateTime.UtcNow.AddYears(20); | ||
private const string RootPath = "/"; | ||
|
||
public Cookies(IHttpResponse httpRes) | ||
{ | ||
this.httpRes = httpRes; | ||
} | ||
public Cookies(IHttpResponse httpRes) | ||
{ | ||
this.httpRes = httpRes; | ||
} | ||
|
||
/// <summary> | ||
/// Sets a persistent cookie which never expires | ||
/// </summary> | ||
public void AddPermanentCookie(string cookieName, string cookieValue) | ||
{ | ||
AddCookie(new Cookie(cookieName, cookieValue, RootPath) { | ||
Expires = Permanent, | ||
}); | ||
} | ||
/// <summary> | ||
/// Sets a persistent cookie which never expires | ||
/// </summary> | ||
public void AddPermanentCookie(string cookieName, string cookieValue, bool? secureOnly = null) | ||
{ | ||
var cookie = new Cookie(cookieName, cookieValue, RootPath) { | ||
Expires = Permanent, | ||
}; | ||
if (secureOnly != null) | ||
{ | ||
cookie.Secure = secureOnly.Value; | ||
} | ||
AddCookie(cookie); | ||
} | ||
|
||
/// <summary> | ||
/// Sets a session cookie which expires after the browser session closes | ||
/// </summary> | ||
public void AddSessionCookie(string cookieName, string cookieValue) | ||
{ | ||
this.AddSessionCookie(cookieName, cookieValue, false); | ||
} | ||
/// <summary> | ||
/// Sets a session cookie which expires after the browser session closes | ||
/// </summary> | ||
public void AddSessionCookie(string cookieName, string cookieValue, bool secureCookieAsRequest) | ||
{ | ||
var __newCookie = new Cookie(cookieName, cookieValue, RootPath); | ||
__newCookie.Secure = secureCookieAsRequest; | ||
this.AddCookie(__newCookie); | ||
} | ||
/// <summary> | ||
/// Sets a session cookie which expires after the browser session closes | ||
/// </summary> | ||
public void AddSessionCookie(string cookieName, string cookieValue, bool? secureOnly = null) | ||
{ | ||
var cookie = new Cookie(cookieName, cookieValue, RootPath); | ||
if (secureOnly != null) | ||
{ | ||
cookie.Secure = secureOnly.Value; | ||
} | ||
this.AddCookie(cookie); | ||
} | ||
|
||
/// <summary> | ||
/// Deletes a specified cookie by setting its value to empty and expiration to -1 days | ||
/// </summary> | ||
public void DeleteCookie(string cookieName) | ||
{ | ||
var cookie = new Cookie(cookieName, string.Empty, "/") { | ||
Expires = DateTime.UtcNow.AddDays(-1) | ||
}; | ||
AddCookie(cookie); | ||
} | ||
/// <summary> | ||
/// Deletes a specified cookie by setting its value to empty and expiration to -1 days | ||
/// </summary> | ||
public void DeleteCookie(string cookieName) | ||
{ | ||
var cookie = new Cookie(cookieName, string.Empty, "/") { | ||
Expires = DateTime.UtcNow.AddDays(-1) | ||
}; | ||
AddCookie(cookie); | ||
} | ||
|
||
public HttpCookie ToHttpCookie(Cookie cookie) | ||
{ | ||
var httpCookie = new HttpCookie(cookie.Name, cookie.Value) { | ||
Path = cookie.Path, | ||
Expires = cookie.Expires, | ||
HttpOnly = true, | ||
Secure = cookie.Secure | ||
}; | ||
if (string.IsNullOrEmpty(httpCookie.Domain)) | ||
{ | ||
httpCookie.Domain = (string.IsNullOrEmpty(cookie.Domain) ? null : cookie.Domain); | ||
} | ||
return httpCookie; | ||
} | ||
public HttpCookie ToHttpCookie(Cookie cookie) | ||
{ | ||
var httpCookie = new HttpCookie(cookie.Name, cookie.Value) { | ||
Path = cookie.Path, | ||
Expires = cookie.Expires, | ||
HttpOnly = true, | ||
Secure = cookie.Secure | ||
}; | ||
if (string.IsNullOrEmpty(httpCookie.Domain)) | ||
{ | ||
httpCookie.Domain = (string.IsNullOrEmpty(cookie.Domain) ? null : cookie.Domain); | ||
} | ||
return httpCookie; | ||
} | ||
|
||
public string GetHeaderValue(Cookie cookie) | ||
{ | ||
return cookie.Expires == Session | ||
? String.Format("{0}={1};path=/", cookie.Name, cookie.Value) | ||
: String.Format("{0}={1};expires={2};path={3}", | ||
cookie.Name, cookie.Value, cookie.Expires.ToString("R"), cookie.Path ?? "/"); | ||
} | ||
public string GetHeaderValue(Cookie cookie) | ||
{ | ||
return cookie.Expires == Session | ||
? String.Format("{0}={1};path=/", cookie.Name, cookie.Value) | ||
: String.Format("{0}={1};expires={2};path={3}", | ||
cookie.Name, cookie.Value, cookie.Expires.ToString("R"), cookie.Path ?? "/"); | ||
} | ||
|
||
/// <summary> | ||
/// Sets a persistent cookie which expires after the given time | ||
/// </summary> | ||
public void AddCookie(Cookie cookie) | ||
{ | ||
var aspNet = this.httpRes.OriginalResponse as HttpResponse; | ||
if (aspNet != null) | ||
{ | ||
var httpCookie = ToHttpCookie(cookie); | ||
aspNet.SetCookie(httpCookie); | ||
return; | ||
} | ||
var httpListener = this.httpRes.OriginalResponse as HttpListenerResponse; | ||
if (httpListener != null) | ||
{ | ||
var cookieStr = GetHeaderValue(cookie); | ||
httpListener.Headers.Add(HttpHeaders.SetCookie, cookieStr); | ||
} | ||
} | ||
} | ||
/// <summary> | ||
/// Sets a persistent cookie which expires after the given time | ||
/// </summary> | ||
public void AddCookie(Cookie cookie) | ||
{ | ||
var aspNet = this.httpRes.OriginalResponse as HttpResponse; | ||
if (aspNet != null) | ||
{ | ||
var httpCookie = ToHttpCookie(cookie); | ||
aspNet.SetCookie(httpCookie); | ||
return; | ||
} | ||
var httpListener = this.httpRes.OriginalResponse as HttpListenerResponse; | ||
if (httpListener != null) | ||
{ | ||
var cookieStr = GetHeaderValue(cookie); | ||
httpListener.Headers.Add(HttpHeaders.SetCookie, cookieStr); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters