cgi_session.h | cgi_session.h | |||
---|---|---|---|---|
skipping to change at line 28 | skipping to change at line 28 | |||
// | // | |||
// http://johnwiggins.net | // http://johnwiggins.net | |||
// cgilib@johnwiggins.net | // cgilib@johnwiggins.net | |||
// | // | |||
// cgi_session.h: interface for the session class | // cgi_session.h: interface for the session class | |||
// | // | |||
#ifdef WIN32 | #ifdef WIN32 | |||
// stop VC++ from complaining about the length of a symbol name greater tha n 256 characters | // stop VC++ from complaining about the length of a symbol name greater tha n 256 characters | |||
#pragma warning (disable: 4786) | #pragma warning (disable: 4786) | |||
#else | ||||
#include <unistd.h> | ||||
#endif | #endif | |||
#ifndef __SESSION_H__ | #ifndef __CGI_SESSION_H__ | |||
#define __SESSION_H__ | #define __CGI_SESSION_H__ | |||
#include <fstream> | ||||
#include <sstream> | #include <sstream> | |||
#include <string> | #include <string> | |||
#include <vector> | #include <vector> | |||
#include <list> | ||||
#include <utility> | #include <utility> | |||
#include <ctime> | #include <ctime> | |||
#include <iterator> | ||||
#include <algorithm> | ||||
#include <fcntl.h> | ||||
#include "jwcgi.h" | #include "jwcgi.h" | |||
namespace jwcgi { | namespace jwcgi { | |||
using std::vector; | ||||
using std::list; | ||||
using std::string; | ||||
using std::ifstream; | ||||
using std::ofstream; | ||||
//////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////// | |||
//////////////////////////////////////////////////////////////// | //////////////////////////////////////////////////////////////// | |||
// Session Object class. | // Session Object class. | |||
// | // | |||
template <class T> class session | class session { | |||
{ | ||||
// helpful definitions, less typing later | // helpful definitions, less typing later | |||
typedef std::vector<std::pair<std::string, std::string> > vec_pair_str; | typedef vector<std::pair<string, string> > vec_pair_str; | |||
typedef std::vector<std::pair<std::string, std::string> >::iterator vec_ | typedef vector<std::pair<string, string> >::iterator vec_pair_str_iter; | |||
pair_str_iter; | typedef vector<std::pair<string, string> >::const_iterator const_vec_pai | |||
typedef std::vector<std::pair<std::string, std::string> >::const_iterato | r_str_iter; | |||
r vec_pair_str_c_iter; | ||||
typedef list<string>::iterator list_str_it; | ||||
typedef list<string>::const_iterator const_list_str_it; | ||||
typedef std::istreambuf_iterator<string> istr_str_it; | ||||
public: | public: | |||
session(const CGI& Cgi, const T& P, long Expires); | // per program sessions can be created in seperate files/folders by givi | |||
ng | ||||
// separate paths/filenames in this constructor. | ||||
// ## the cgi program must have create/write writes to the folder hol | ||||
ding ## | ||||
// ## the file or explicit writes to an already created file of this | ||||
name ## | ||||
session(const CGI& cgi, long Expires, const string& Sessionfile = ".htce | ||||
egisession"); | ||||
~session() { | ||||
if(lockfileFD > 0) { | ||||
close(lockfileFD); | ||||
} | ||||
} | ||||
// set the amount of time before the session expires. | // set the amount of time before the session expires. | |||
// e.g. to have the session expire in 1 day: | // e.g. to have the session expire in 1 day: | |||
// settimeout(60 * 60 * 24); | // settimeout(60 * 60 * 24); | |||
bool settimeout(long Expires); | void settimeout(long Expires); | |||
// add/change a session variable. | // add/change a session variable. | |||
bool set(const std::string& name, const std::string& value); | void set(const string& name, const string& value); | |||
// return the contents of a session variable. | // return the contents of a session variable. | |||
// returns the empty string if the variable does not exist. | // returns the empty string if the variable does not exist. | |||
std::string get(const std::string& name) const; | string get(const string& name); | |||
// remove a session variable. | // remove a session variable. | |||
void remove(const std::string& name); | void remove(const string& name); | |||
private: | private: | |||
struct Client { | ||||
const string ipaddress; | ||||
time_t last_access; | ||||
// vector of name/value pairs | ||||
vector<std::pair<string, string> >values; | ||||
Client(const string& IP, time_t LA):ipaddress(IP), last_access(LA) {} | ||||
}; | ||||
// populate the session. | // populate the session. | |||
void fillcontents(); | // This is the call does timeout checking | |||
bool getsession(); | ||||
// commit the sessions contents | // commit the sessions contents | |||
bool writesession(); | // no timeout check done, so we must call | |||
// getsession before a call to this function | ||||
bool commitsession(); | ||||
// test the session has not timed out. | ||||
bool checksessionOK(const_list_str_it lastaccess) const; | ||||
// lock the sessionfile. | ||||
bool lock(int type); | ||||
const CGI& cgi; | // read the session file into data | |||
const T& p; | // returns false if the operation cannot complete | |||
time_t expires; | bool readsessionfile (list<string>& data) const; | |||
vec_pair_str contents; | // writes each member of data to the sessionfile with a newline for each | |||
value | ||||
// truncates the file to zero before writing. | ||||
// returns false if the operation cannot complete | ||||
bool writesessionfile(const list<string>& data) const; | ||||
const string sessionfile; | ||||
Client client; | ||||
long timeout; | ||||
const int lockfileFD; | ||||
static const string ip; | ||||
static const string la; | ||||
static const string id; | ||||
}; | }; | |||
} // end namespace jwcgi | } // end namespace jwcgi | |||
//////////////////////////////////////////////////////////////// | #endif // !ifndef __CGI_SESSION_H__ | |||
//////////////// implementation | ||||
#include "cgi_session.cpp" | ||||
#endif // !ifndef __SESSION_H__ | ||||
End of changes. 18 change blocks. | ||||
21 lines changed or deleted | 78 lines changed or added | |||
xhtml.h | xhtml.h | |||
---|---|---|---|---|
skipping to change at line 67 | skipping to change at line 67 | |||
// std::cout << " Number 123 + 300 = " << 123 + 300; | // std::cout << " Number 123 + 300 = " << 123 + 300; | |||
// p.pend(); | // p.pend(); | |||
// | // | |||
xhtml(T& Output); | xhtml(T& Output); | |||
virtual ~xhtml(); | virtual ~xhtml(); | |||
// encapsulate the http headers needed for CGI | // encapsulate the http headers needed for CGI | |||
// This function must be called before any other | // This function must be called before any other | |||
// to ensure the http headers are closed correctly. | // to ensure the http headers are closed correctly. | |||
void pagebegin(const string& title = "untitled page", | void pagebegin(const string& title = "untitled page", | |||
const string& headtag = "", | const string& headtag = "", | |||
const string& cssfile = "", | const string& cssfile = "", | |||
const string& httpheaders = "", | const string& httpheaders = "", | |||
const string& contenttype = "text/html"); | const string& contenttype = "text/html"); | |||
// visible part of page. | // visible part of page. | |||
void body(const string& id = "", | void body(const string& id = "", | |||
const string& Class = "", | const string& Class = "", | |||
const string& onload = ""); | const string& onload = ""); | |||
// Encapsulate html form tags. | // Encapsulate html form tags. | |||
void form(const string& id = "", | void form(const string& id = "", | |||
const string& action = "", | const string& action = "", | |||
const string& onsubmit = "", | const string& onsubmit = "", | |||
skipping to change at line 135 | skipping to change at line 135 | |||
const string& maxlen = "", | const string& maxlen = "", | |||
const string& onselect = "") const; | const string& onselect = "") const; | |||
void inputcheck(const string& name, const string& value, | void inputcheck(const string& name, const string& value, | |||
bool checked = false, const string& onclick = "", | bool checked = false, const string& onclick = "", | |||
const string& Class = "") const; | const string& Class = "") const; | |||
void textarea(const string& name, | void textarea(const string& name, | |||
const vector<string>& text, | const vector<string>& text, | |||
const string& cols, | const string& cols, | |||
const string& rows = "", | const string& rows, | |||
const string& onblur = "", | const string& onblur = "", | |||
const string& onchange = "", | const string& onchange = "", | |||
const string& onfocus = "", | const string& onfocus = "", | |||
const string& onselect = "", | const string& onselect = "", | |||
bool readonly = false, | bool readonly = false, | |||
const string& tooltip = "") const; | const string& tooltip = "") const; | |||
void inputimage(const string& src, | void inputimage(const string& src, | |||
const string& name = "", | const string& name = "", | |||
const string& Class = "") const; | const string& Class = "") const; | |||
skipping to change at line 239 | skipping to change at line 239 | |||
// | // | |||
// SetCookie(...) must be called before beginpage() to work | // SetCookie(...) must be called before beginpage() to work | |||
// "value_name" is in the form "nameofcookie=value" | // "value_name" is in the form "nameofcookie=value" | |||
// e.g. orderno=1234567 | // e.g. orderno=1234567 | |||
// example setting a cookie to expire 3 days from now: | // example setting a cookie to expire 3 days from now: | |||
// | // | |||
// long t = 60 * 60 * 24 * 3; // 3 days | // long t = 60 * 60 * 24 * 3; // 3 days | |||
// | // | |||
// to delete a cookie send a negative time | // to delete a cookie send a negative time | |||
// i.e. t -= 60; // negative one minute | // i.e. t -= 60; // negative one minute | |||
// cgiobject.SetCookie("clientname", "1234567", t); | // xhtmlobject.SetCookie("clientname", "1234567", t); | |||
// | // | |||
bool SetCookie(const string& cookie_name, const string& value, | bool SetCookie(const string& cookie_name, const string& value, | |||
const long& expires, const string& path = "", | const long& expires, const string& path = "", | |||
const string& domain = "", bool secure = false) const; | const string& domain = "", bool secure = false) const; | |||
// returns a cookie value if it exists | // returns a cookie value if it exists | |||
bool GetCookie(const CGI& cgi, const string& name, string& value) const; | bool GetCookie(const CGI& cgi, const string& name, string& value) const; | |||
// Delete a cookie already set | // Delete a cookie already set | |||
void DeleteCookie(const string& cookiename, | void DeleteCookie(const string& cookiename, | |||
End of changes. 3 change blocks. | ||||
6 lines changed or deleted | 6 lines changed or added | |||