mws_common.h
来自「Vc.Net入门与提高源码」· C头文件 代码 · 共 455 行 · 第 1/2 页
H
455 行
// Flush the response to client
pT->m_HttpResponse.Flush();
return HTTP_SUCCESS_NO_PROCESS;
}
HTTP_CODE SMTPError()
{
// Return a SMTP error message to the client
T* pT = static_cast<T*>(this);
// Clear the response
pT->m_HttpResponse.ClearResponse();
// Respond with the error message
pT->m_HttpResponse << "<HTML><HEAD><TITLE>MantaWeb: SMTP Error</TITLE></HEAD><BODY>"
"There was an error attempting to send a message through the SMTP server.<BR><BR>"
"Please try again later.</BODY></HTML>";
// Flush the response to client
pT->m_HttpResponse.Flush();
return HTTP_SUCCESS_NO_PROCESS;
}
// This overload takes the integral types and converts them
BOOL WriteSessionCookie(LPCSTR lpszUser, LPCSTR lpszFirstName, LPCSTR lpszLastName, LONG lUserID, GUID& lSessionID)
{
// Convert the long to a string
CStringA strUserID;
strUserID.Format("%d", lUserID);
// Convert the guid to a string
LPOLESTR lpStrGuid;
StringFromCLSID(lSessionID, &lpStrGuid);
USES_CONVERSION;
// Call the other overloaded call with all strings
BOOL bRet = WriteSessionCookie(lpszUser, lpszFirstName, lpszLastName, strUserID, W2CA(lpStrGuid));
// Free the guid string
CoTaskMemFree(&lpStrGuid);
return bRet;
}
// This overload takes all strings and writes to the session cookie (the previous method relies on this method)
BOOL WriteSessionCookie(LPCSTR lpszUser, LPCSTR lpszFirstName, LPCSTR lpszLastName, LPCSTR lpszUserID, LPCSTR lpszSessionID)
{
T* pT = static_cast<T*>(this);
// Create the cookie as discardable, with / as the path
CCookie cookie;
cookie.SetDiscard(TRUE);
cookie.SetPath("/");
// Set the name to the session cookie name
cookie.SetName(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME);
// Add all the values to the cookie
cookie.AddValue("Login", lpszUser);
cookie.AddValue("FirstName", lpszFirstName);
cookie.AddValue("LastName", lpszLastName);
cookie.AddValue("UserID", lpszUserID);
cookie.AddValue("SessionID", lpszSessionID);
// Append the cookie to the response
return pT->m_HttpResponse.AppendCookie(cookie);
}
// This method writes to the persistent cookie instead of the session cookie
BOOL WritePersistCookie(LPCSTR lpszLogin, LPCSTR lpszFirstName, LPCSTR lpszRememberPass)
{
T* pT = static_cast<T*>(this);
SYSTEMTIME st;
// Get the current time, add a year to that (cookie will expire 1 year from today)
GetSystemTime(&st);
st.wYear++;
// Create the cookie that expires one year from today, set the path to '/'
CCookie cookie;
cookie.SetPath("/");
cookie.SetExpires(st);
// Set the name to the persistant cookie name
cookie.SetName(MantaWeb::MANTAWEB_PERSISTANT_COOKIE_NAME);
// Add all the values to the cookie
cookie.AddValue("Login", lpszLogin);
cookie.AddValue("FirstName", lpszFirstName);
cookie.AddValue("RememberPass", lpszRememberPass);
// Append the cookie to the response
return pT->m_HttpResponse.AppendCookie(cookie);
}
// This method gets the login from either the persistent cookie or the session cookie
LPCSTR GetLogin(bool bFromPersist = false)
{
T* pT = static_cast<T*>(this);
if (bFromPersist) // If from persistent cookie, return the login
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_PERSISTANT_COOKIE_NAME).Lookup("Login");
else // Otherwise, get the login from the session cookie
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Lookup("Login");
}
// This method gets the first name from either the persistent cookie or the session cookie
LPCSTR GetFirstName(bool bFromPersist = false)
{
T* pT = static_cast<T*>(this);
if (bFromPersist) // If from persistent cookie, return the first name
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_PERSISTANT_COOKIE_NAME).Lookup("FirstName");
else // Otherwise, get the first name from the session cookie
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Lookup("FirstName");
}
LPCSTR GetLastName()
{
// Return the last name from the session cookie
T* pT = static_cast<T*>(this);
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Lookup("LastName");
}
bool GetRememberPass(bool* bRememberPass)
{
T* pT = static_cast<T*>(this);
int iValue;
// Get the int value for remembering the password from the persistent cookie
// Note: the boolean overload of Exchange() is only used with HTML checkboxes.
if (pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_PERSISTANT_COOKIE_NAME).Exchange("RememberPass", &iValue) != VALIDATION_S_OK)
return false;
*bRememberPass = (iValue != 0) ? true : false;
return true;
}
LPCSTR GetRememberPass()
{
T* pT = static_cast<T*>(this);
// Get the string value representing the boolean value for remembering the password
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_PERSISTANT_COOKIE_NAME).Lookup("RememberPass");
}
bool GetUserID(long* lUserID)
{
T* pT = static_cast<T*>(this);
// Get the long value for the user id from the session cookie
if (pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Exchange("UserID", lUserID) != VALIDATION_S_OK)
return false;
return true;
}
LPCSTR GetUserID()
{
T* pT = static_cast<T*>(this);
// Get the string value representing the user id
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Lookup("UserID");
}
bool GetSessionID(GUID* guidSessionID)
{
T* pT = static_cast<T*>(this);
// Get the guid session id from the session cookie
if (pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Exchange("SessionID", guidSessionID) != VALIDATION_S_OK)
return false;
return true;
}
LPCSTR GetSessionID()
{
T* pT = static_cast<T*>(this);
// Get the string session id from the sessino cookie
return pT->m_HttpRequest.Cookies(MantaWeb::MANTAWEB_SESSION_COOKIE_NAME).Lookup("SessionID");
}
// This method makes sure that a string has no illegal characters
// Checks for <, >, and " which might be used to supply script code
bool ValidateString(CString& str)
{
if (str.Find("<", 0) != -1 ||
str.Find(">", 0) != -1 ||
str.Find("\"", 0) != -1)
return false;
return true;
}
// This method puts in HTML sequences to prevent script attacks
void HTMLPrepareString(CString& str)
{
str.Replace("<", "<");
str.Replace(">", ">");
str.Replace("\"", """);
}
// This method loads HTML content from resource into a CString
// This is useful if you want to return HTML files that aren't exposed through the web server
bool LoadHtmlFromResource(LPCTSTR lpName, CStringA& strHTML)
{
// Find the specified resource
HRSRC hResInfo = FindResource(_AtlBaseModule.GetResourceInstance(), lpName, RT_HTML);
if (hResInfo != NULL)
{
// Load the resource
HGLOBAL hRes = LoadResource(_AtlBaseModule.GetResourceInstance(), hResInfo);
if (hRes != NULL)
{
// Lock the resource and copy it
// The resource does not need to be unlocked and freed
strHTML = (LPSTR) LockResource(hRes);
return true;
}
}
// An error occured, print the message
char szName[MAX_PATH];
// Get the module file name
GetModuleFileName(_AtlBaseModule.GetResourceInstance(), szName, MAX_PATH);
if (hResInfo == NULL)
{
// FindResource failed, set the HTML as the error message
strHTML.Format("<HTML><TITLE>MantaWeb: Resource Failure</TITLE><BODY>"
"FindResource() failed.<BR>Module file name: %s<BR>LastError: %d</BODY></HTML>",
szName, ::GetLastError());
}
else
{
// LoadResource failed, set the HTML as the error message
strHTML.Format("<HTML><TITLE>MantaWeb: Resource Failure</TITLE><BODY>"
"LoadResource() failed.<BR>Module file name: %s<BR>LastError: %d</BODY></HTML>",
szName, ::GetLastError());
}
return false;
}
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?