CPPSERV


Home Projects Jobs Clientele Contact

cppserv


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Add new test functionality for session servlet



From: Sergey Jukov <sergey@total-knowledge.com>

---
 ChangeLog           |    5 +++++
 session/session.cpp |   32 +++++++++++++++++++++++++++++++-
 session/session.h   |   11 +++++++++--
 3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cde6de9..b25c28c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Sergey Jukov <sergey@total-knowledge.com>            Wed, 20 Sep 2006 14:40:00 -0800
+- Add testing getId(), getCreationTime(), getLastAccessedTime(),
+  setMaxInactiveInterval(), getMaxInactiveInterval(), isNew() and
+  invalidate() functions for session servlet.
+
 Sergey Jukov <sergey@total-knowledge.com>            Tue, 19 Sep 2006 15:50:00 -0800
 - Add testing reset() for headers
 
diff --git a/session/session.cpp b/session/session.cpp
index 4e380b6..7abf069 100644
--- a/session/session.cpp
+++ b/session/session.cpp
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2004 by Ilya A. Volynets-Evenbakh                       *
+ *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
  *   ilya@total-knowledge.com                                              *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -24,6 +24,10 @@ void SessionServlet::service(servlet::Ht
 {
 	std::ostream &out=resp.getOutputStream();
 	servlet::HttpSession *s=req.getSession();
+	std::string inval=req.getParameter("inval");
+	if(inval=="true"){
+		s->invalidate();
+	}
 	typedef boost::shared_ptr<int> ctr_t;
 	ctr_t counter;
 	if((counter=boost::static_pointer_cast<int>(s->getAttribute("count")))){
@@ -33,7 +37,33 @@ void SessionServlet::service(servlet::Ht
 		s->setAttribute("count",counter);
 	}
 	renderHeader(out);
+	out<<"<PRE>";
+	if(s->isNew()){
+		out<<"New session!\n";
+	} else {
+		out<<"Old session\n";
+	}
 	out<<"You hit this page "<<*counter<<" times during this session";
+	out<<"\n\nSessionID: "<<s->getId();
+	long sCreationTime=s->getCreationTime();
+	struct tm sout;
+	gmtime_r(&sCreationTime, &sout);
+	char str[32];
+	strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", &sout);
+	out<<"\nCreation time: "<<str;
+	long sLastAccessedTime=s->getLastAccessedTime();
+	gmtime_r(&sLastAccessedTime, &sout);
+	strftime(str, sizeof(str), "%a, %d %b %Y %H:%M:%S GMT", &sout);
+	out<<"\nLast Accessed time: "<<str;
+	s->setMaxInactiveInterval(30);
+	int sMaxInactiveInterval=s->getMaxInactiveInterval();
+	out<<"\nMaximum time interval: "<<sMaxInactiveInterval<<" seconds";
+	if(inval=="true"){
+		out<<"\n\n<a href=\""<<req.getRequestURI()<<"\">Start over</a>\n";
+	} else {
+		out<<"\n\n<a href=\""<<req.getRequestURI()<<"?inval=true\">Invalidate this session</a>\n";
+	}
+	out<<"<\PRE>";
 	renderFooter(out);
 }
 
diff --git a/session/session.h b/session/session.h
index 0022658..dd81ae3 100644
--- a/session/session.h
+++ b/session/session.h
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2004 by Ilya A. Volynets-Evenbakh                       *
+ *   Copyright (C) 2004-2006 by Ilya A. Volynets-Evenbakh                  *
  *   ilya@total-knowledge.com                                              *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -28,7 +28,14 @@ #include <iostream>
 /**
 This servlet demonstrates how to use sessions.
 It displays a page with number of hits within current session.
-This servlet also remonstrates use of CPPServObjectWrapper template.
+This servlet also demonstrates use of CPPServObjectWrapper template.
+It shows session id, session creation time and the last time when
+client sent a request associated with this session.
+It sets time (30 seconds in this example) between client requests
+before the session will be invalidated and returns maximum time
+interval, in seconds, that the servlet container will keep this session
+open between client accesses.
+It tests work of invalidate() function.
 
 @author Ilya A. Volynets-Evenbakh
 */
-- 
1.4.2


Authoright © Total Knowledge: 2001-2008