NCBI C++ ToolKit
cgi_session.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: cgi_session.cpp 95063 2021-10-04 12:51:38Z grichenk $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Maxim Didenko
27  *
28  * ===========================================================================
29  */
30 
31 #include <ncbi_pch.hpp>
32 #include <corelib/ncbitime.hpp>
33 
34 #include <cgi/cgi_session.hpp>
35 #include <cgi/ncbicgi.hpp>
36 #include <cgi/cgi_exception.hpp>
37 
39 
40 const char* CCgiSession::kDefaultSessionIdName = "ncbi_session_data";
41 const char* CCgiSession::kDefaultSessionCookieDomain = ".nih.gov";
43 
46  EOwnership impl_owner,
47  ECookieSupport cookie_sup)
48  : m_Request(request), m_Impl(impl), m_CookieSupport(cookie_sup),
49  m_SessionIdName(kDefaultSessionIdName),
50  m_SessionCookieDomain(kDefaultSessionCookieDomain),
51  m_SessionCookiePath(kDefaultSessionCookiePath)
52 {
53  if (impl_owner == eTakeOwnership)
54  m_ImplGuard.reset(m_Impl);
56 }
57 
59 {
60  if (Exists()) {
61  try {
62  m_Impl->Reset();
63  }
64  catch (std::exception& e) {
65  ERR_POST("Session implementation clean-up error: " << e.what());
66  }
67  catch (...) {
68  ERR_POST("Session implementation clean-up error has occurred");
69  }
70  }
71 }
72 
73 const string& CCgiSession::GetId() const
74 {
75  if (m_SessionId.empty()) {
76  const_cast<CCgiSession*>(this)->m_SessionId = RetrieveSessionId();
77  if (m_SessionId.empty())
78  NCBI_THROW(CCgiSessionException, eSessionId,
79  "SessionId can not be retrieved from the cgi request");
80  }
81  return m_SessionId;
82 }
83 
84 void CCgiSession::SetId(const string& id)
85 {
86  if (m_SessionId == id)
87  return;
88  if (Exists()) {
89  m_Impl->Reset();
91  }
92  m_SessionId = id;
93  //GetDiagContext().SetProperty(
94  // CDiagContext::kProperty_SessionID, m_SessionId);
95 }
96 
97 void CCgiSession::ModifyId(const string& new_session_id)
98 {
99  if (m_SessionId == new_session_id)
100  return;
101  if (!m_Impl)
103  "The session implementation is not set");
104  if (!Exists())
105  NCBI_THROW(CCgiSessionException, eSessionId,
106  "The session must be loaded");
107  m_Impl->ModifySessionId(new_session_id);
108  m_SessionId = new_session_id;
109  //GetDiagContext().SetProperty(
110  // CDiagContext::kProperty_SessionID, m_SessionId);
111 }
112 
114 {
115  if (Exists())
116  return;
117  if (!m_Impl)
119  "The session implementation is not set");
120  if (m_Status == eDeleted)
122  "Cannot load deleted session");
123  if (m_Impl->LoadSession(GetId()))
124  m_Status = eLoaded;
125  else m_Status = eNotLoaded;
126 }
127 
129 {
130  if (Exists())
131  m_Impl->Reset();
132  if (!m_Impl)
134  "The session implementation is not set");
136  //GetDiagContext().SetProperty(
137  // CDiagContext::kProperty_SessionID, m_SessionId);
138  m_Status = eNew;
139 }
140 
142 {
143  x_Load();
144  return m_Impl->GetAttributeNames();
145 }
146 
148  size_t* size)
149 {
150  Load();
151  return m_Impl->GetAttrIStream(name, size);
152 }
153 
155 {
156  Load();
157  return m_Impl->GetAttrOStream(name);
158 }
159 
160 void CCgiSession::SetAttribute(const string& name, const string& value)
161 {
162  Load();
163  m_Impl->SetAttribute(name,value);
164 }
165 
166 string CCgiSession::GetAttribute(const string& name) const
167 {
168  x_Load();
169  return m_Impl->GetAttribute(name);
170 }
171 
172 void CCgiSession::RemoveAttribute(const string& name)
173 {
174  Load();
175  m_Impl->RemoveAttribute(name);
176 }
177 
179 {
180  if (m_SessionId.empty()) {
182  if (m_SessionId.empty())
183  return;
184  }
185  Load();
187  //GetDiagContext().SetProperty(
188  // CDiagContext::kProperty_SessionID, kEmptyStr);;
189  m_Status = eDeleted;
190 }
191 
192 
194 {
195  if (m_CookieSupport == eNoCookie ||
196  (!Exists() && m_Status != eDeleted))
197  return NULL;
198 
199  if (!m_SessionCookie.get()) {
200  const_cast<CCgiSession*>(this)->
202  m_SessionId,
205  if (m_Status == eDeleted) {
207  exp.AddYear(-10);
208  const_cast<CCgiSession*>(this)->
209  m_SessionCookie->SetExpTime(exp);
210  } else {
212  const_cast<CCgiSession*>(this)->
214  }
215 
216  }
217  return m_SessionCookie.get();
218 }
219 
221 {
222  if (m_CookieSupport == eUseCookie) {
223  const CCgiCookies& cookies = m_Request.GetCookies();
224  const CCgiCookie* cookie = cookies.Find(m_SessionIdName, kEmptyStr, kEmptyStr);
225 
226  if (cookie) {
227  return cookie->GetValue();
228  }
229  }
230  bool is_found = false;
231  const CCgiEntry& entry = m_Request.GetEntry(m_SessionIdName, &is_found);
232  if (is_found) {
233  return entry.GetValue();
234  }
235  return kEmptyStr;
236 }
237 
239 {
240  const_cast<CCgiSession*>(this)->Load();
241 }
242 
243 ///////////////////////////////////////////////////////////
245 {
246 }
247 
Exception classes used by the NCBI CGI framework.
API to store CGI session data between Web requests.
CCgiCookie::
Definition: ncbicgi.hpp:67
CCgiCookies::
Definition: ncbicgi.hpp:219
CCgiRequest::
Definition: ncbicgi.hpp:685
CCgiSessionException –.
CCgiSession –.
Definition: cgi_session.hpp:62
CTime –.
Definition: ncbitime.hpp:296
ICgiSessionStorage –.
@ eTakeOwnership
An object can take ownership of another.
Definition: ncbi_types.h:136
CCgiCookie * Find(const string &name, const string &domain, const string &path)
Return NULL if cannot find this exact cookie.
Definition: ncbicgi.cpp:690
const string & GetValue(void) const
All "const string& GetXXX(...)" methods beneath return reference to "NcbiEmptyString" if the requeste...
Definition: ncbicgi.hpp:1034
const string & GetValue() const
Get the value as a string, (necessarily) prefetching it all if applicable; the result remains availab...
Definition: ncbicgi.hpp:470
const CCgiCookies & GetCookies(void) const
Retrieve the request cookies.
Definition: ncbicgi.hpp:1173
const CCgiEntry & GetEntry(const string &name, bool *is_found=0) const
Get entry value by name.
Definition: ncbicgi.cpp:1449
string GetAttribute(const string &name) const
Get attribute data as string.
CTime m_SessionCookieExpTime
static const char * kDefaultSessionCookieDomain
Definition: cgi_session.hpp:67
static const char * kDefaultSessionIdName
Definition: cgi_session.hpp:66
bool Exists(void) const
Check if this session object is valid.
void SetId(const string &session_id)
Set session ID.
Definition: cgi_session.cpp:84
CCgiSession(const CCgiRequest &request, ICgiSessionStorage *impl, EOwnership impl_ownership=eTakeOwnership, ECookieSupport cookie_support=eUseCookie)
Definition: cgi_session.cpp:44
virtual ~ICgiSessionStorage()
void DeleteSession(void)
Delete current session.
EStatus m_Status
ECookieSupport
Specifies if a client session cookie can be used to transfer session id between requests.
Definition: cgi_session.hpp:82
unique_ptr< CCgiCookie > m_SessionCookie
virtual void Reset()=0
Reset the session.
TNames GetAttributeNames(void) const
Retrieve names of all attributes attached to this session.
ECookieSupport m_CookieSupport
void CreateNewSession(void)
Create new session.
static const char * kDefaultSessionCookiePath
Definition: cgi_session.hpp:68
virtual void ModifySessionId(const string &new_id)=0
Modify session id.
void Load(void)
Load the session.
list< string > TNames
Definition: cgi_session.hpp:64
virtual bool LoadSession(const string &sessionid)=0
Load the session The implementations must handle binary data correctly (e.g.
void RemoveAttribute(const string &name)
Remove attribute from the session.
virtual CNcbiIstream & GetAttrIStream(const string &name, size_t *size=0)=0
Get input stream to read an attribute's data from.
virtual string GetAttribute(const string &name) const =0
Get attribute data as string.
void x_Load() const
virtual string CreateNewSession()=0
Create a new empty session.
virtual void RemoveAttribute(const string &name)=0
Remove attribute from the session.
virtual void DeleteSession()=0
Delete current session.
void ModifyId(const string &new_session_id)
Modify session ID.
Definition: cgi_session.cpp:97
virtual TNames GetAttributeNames(void) const =0
Retrieve names of all attributes attached to this session.
unique_ptr< ICgiSessionStorage > m_ImplGuard
string m_SessionCookiePath
const CCgiRequest & m_Request
const CCgiCookie * GetSessionCookie(void) const
Get a cookie pertaining to the session.
CNcbiIstream & GetAttrIStream(const string &name, size_t *size=NULL)
Get input stream to read an attribute's data from.
const string & GetId(void) const
Get session ID.
Definition: cgi_session.cpp:73
string m_SessionId
ICgiSessionStorage * m_Impl
string m_SessionIdName
virtual CNcbiOstream & GetAttrOStream(const string &name)=0
Get output stream to write an attribute's data to.
CNcbiOstream & GetAttrOStream(const string &name)
Get output stream to write an attribute's data to.
string m_SessionCookieDomain
void SetAttribute(const string &name, const string &value)
Set attribute data as a string.
string RetrieveSessionId() const
Retrieve a session id from a query string or a session cookie.
virtual void SetAttribute(const string &name, const string &value)=0
Set attribute data as a string.
@ eUseCookie
A session cookie will be added to the response.
Definition: cgi_session.hpp:83
@ eNoCookie
A session cookie will not be added to the response.
Definition: cgi_session.hpp:84
@ eImplNotSet
The CGI application didn't set the session implementation.
Definition: cgi_session.hpp:76
@ eLoaded
The session is loaded.
Definition: cgi_session.hpp:73
@ eDeleted
The session is deleted.
Definition: cgi_session.hpp:75
@ eNew
The session has just been created.
Definition: cgi_session.hpp:72
@ eNotLoaded
The session has not been loaded yet.
Definition: cgi_session.hpp:74
#define NULL
Definition: ncbistd.hpp:225
#define ERR_POST(message)
Error posting with file, line number information but without error codes.
Definition: ncbidiag.hpp:186
#define NCBI_THROW(exception_class, err_code, message)
Generic macro to throw an exception, given the exception class, error code and message string.
Definition: ncbiexpt.hpp:704
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
#define kEmptyStr
Definition: ncbistr.hpp:123
bool IsEmpty(void) const
Is time object empty (date and time)?
Definition: ncbitime.hpp:2378
CTime & AddYear(int years=1, EDaylight adl=eDaylightDefault)
Add specified years and adjust for daylight saving time.
Definition: ncbitime.hpp:2293
@ eCurrent
Use current time. See also CCurrentTime.
Definition: ncbitime.hpp:300
@ eGmt
GMT (Greenwich Mean Time)
Definition: ncbitime.hpp:308
enum ENcbiOwnership EOwnership
Ownership relations between objects.
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Defines: CTimeFormat - storage class for time format.
Modified on Fri Apr 12 17:15:47 2024 by modify_doxy.py rev. 669887