changeset 1153:1f4da56abd4f

change http cookies interface
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Feb 2018 20:01:23 -0700
parents 21d157b153fe
children bbad9a21277c
files src/luan/modules/http/Http.luan src/luan/modules/http/Http_test.luan src/luan/modules/http/jetty/HttpServicer.java
diffstat 3 files changed, 33 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
diff -r 21d157b153fe -r 1f4da56abd4f src/luan/modules/http/Http.luan
--- a/src/luan/modules/http/Http.luan	Sun Feb 04 19:25:12 2018 -0700
+++ b/src/luan/modules/http/Http.luan	Sun Feb 04 20:01:23 2018 -0700
@@ -51,7 +51,7 @@
 	this.scheme = "http"  -- default
 	this.port = 80  -- default
 	this.parameters = {}
-	this.cookie = {}
+	this.cookies = {}
 
 	function this.query_string()
 		local string_uri = Io.uri "string:"
@@ -96,12 +96,20 @@
 		this.send_redirect = this.java.sendRedirect
 		this.send_error = this.java.sendError
 
-		function this.set_cookie(name,value,is_persistent,domain)
-			HttpServicer.setCookie(Http.request.java,this.java,name,value,is_persistent,domain)
+		function this.set_cookie(name,value,attributes)
+			HttpServicer.setCookie(Http.request.java,this.java,name,value,attributes)
 		end
 
+		function this.set_persistent_cookie(name,value,attributes) {
+			attributes = attributes or {}
+			attributes["Max-Age"] = "10000000"
+			this.set_cookie(name,value,attributes)
+		}
+
 		function this.remove_cookie(name,domain)
-			HttpServicer.removeCookie(Http.request.java,this.java,name,domain)
+			attributes = attributes or {}
+			attributes["Max-Age"] = "0"
+			this.set_cookie(name,value,attributes)
 		end
 
 		function this.set()
diff -r 21d157b153fe -r 1f4da56abd4f src/luan/modules/http/Http_test.luan
--- a/src/luan/modules/http/Http_test.luan	Sun Feb 04 19:25:12 2018 -0700
+++ b/src/luan/modules/http/Http_test.luan	Sun Feb 04 20:01:23 2018 -0700
@@ -12,7 +12,7 @@
 local Http_test = {}
 
 Http_test.welcome_file = "index.html"
-Http_test.cookie = {}
+Http_test.cookies = {}
 
 function Http_test.get_page(path)
 	Http.request.path = path
@@ -41,7 +41,7 @@
 
 function Http_test.init()
 	Http.request = Http.new_request{}
-	Http.request.cookie = Http_test.cookie
+	Http.request.cookies = Http_test.cookies
 
 	Http.response = Http.new_response{
 
@@ -53,11 +53,15 @@
 		end
 
 		set_cookie = function(name,value)
-			Http_test.cookie[name] = value
+			Http_test.cookies[name] = value
+		end
+
+		set_persistent_cookie = function(name,value)
+			Http_test.cookies[name] = value
 		end
 
 		remove_cookie = function(name)
-			Http_test.cookie[name] = nil
+			Http_test.cookies[name] = nil
 		end
 
 		send_redirect = function(url)
diff -r 21d157b153fe -r 1f4da56abd4f src/luan/modules/http/jetty/HttpServicer.java
--- a/src/luan/modules/http/jetty/HttpServicer.java	Sun Feb 04 19:25:12 2018 -0700
+++ b/src/luan/modules/http/jetty/HttpServicer.java	Sun Feb 04 20:01:23 2018 -0700
@@ -156,7 +156,7 @@
 			}
 		}
 
-		LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookie");
+		LuanTable cookieTbl = (LuanTable)requestTbl.rawGet("cookies");
 		for( Cookie cookie : request.getCookies() ) {
 			cookieTbl.rawPut( cookie.getName(), unescape(cookie.getValue()) );
 		}
@@ -228,35 +228,25 @@
 		return null;
 	}
 
-	public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,boolean isPersistent, String domain) {
+	public static void setCookie(HttpServletRequest request,HttpServletResponse response,String name,String value,LuanTable attributes) {
 		Cookie cookie = getCookie(request,name);
 		if( cookie==null || !cookie.getValue().equals(value) ) {
 			cookie = new Cookie(name, escape(value));
-			cookie.setPath("/");
-			if (domain != null && domain.length() > 0)
-				cookie.setDomain(domain);
-			if( isPersistent )
-				cookie.setMaxAge(10000000);
+			if( attributes != null ) {
+				String path = (String)attributes.rawGet("Path");
+				if( path != null )
+					cookie.setPath(path);
+				String domain = (String)attributes.rawGet("Domain");
+				if (domain != null && domain.length() > 0)
+					cookie.setDomain(domain);
+				String maxAge = (String)attributes.rawGet("Max-Age");
+				if( maxAge != null )
+					cookie.setMaxAge(Integer.parseInt(maxAge));
+			}
 			response.addCookie(cookie);
 		}
 	}
 
-	public static void removeCookie(HttpServletRequest request,
-									HttpServletResponse response,
-									String name,
-									String domain
-	) {
-		Cookie cookie = getCookie(request, name);
-		if(cookie != null) {
-			Cookie delCookie = new Cookie(name, "delete");
-			delCookie.setPath("/");
-			delCookie.setMaxAge(0);
-			if (domain != null && domain.length() > 0)
-				delCookie.setDomain(domain);
-			response.addCookie(delCookie);
-		}
-	}
-
 
 
 	private static String RUN_LATER_KEY = "Http.run_later";