当前位置: 首页>>代码示例>>C++>>正文


C++ HTTPServerRequest::getCookies方法代码示例

本文整理汇总了C++中HTTPServerRequest::getCookies方法的典型用法代码示例。如果您正苦于以下问题:C++ HTTPServerRequest::getCookies方法的具体用法?C++ HTTPServerRequest::getCookies怎么用?C++ HTTPServerRequest::getCookies使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HTTPServerRequest的用法示例。


在下文中一共展示了HTTPServerRequest::getCookies方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: isValidRequest

//------------------------------------------------------------------------------
bool ofxWebServerBaseRouteHandler::isValidRequest(const Settings& settings,
                                                  HTTPServerRequest& request,
                                                  HTTPServerResponse& response) {
    
    string sessionId = "";
    
    // extract cookie from request
    NameValueCollection cookies;
    request.getCookies(cookies);
    NameValueCollection::ConstIterator it = cookies.find(settings.sessionCookieName);
    if (it != cookies.end()) {
        sessionId = it->second;
    } else {
        sessionId = ofxWebServerSessionManager::generateSessionKey(request);
        HTTPCookie cookie(settings.sessionCookieName,sessionId);
        cookie.setPath("/");
        // set no age, so it expires @ end of session
        response.addCookie(cookie);
    }
    
    // TODO: update session manager
    
    URI uri(request.getURI());
    
    const string path = uri.getPath(); // just get the path

    if(settings.requireAuthentication) {
        if(request.hasCredentials()) {
            HTTPBasicCredentials credentials(request);
            const string& user = credentials.getUsername();
            const string& pwd = credentials.getPassword();
            
            if(settings.username == credentials.getUsername() &&
               settings.password == credentials.getPassword()) {
                // add an authentication cookie?
                return true;
            } else {
                response.setStatusAndReason(HTTPResponse::HTTP_UNAUTHORIZED);
                sendErrorResponse(response);
                return false;
            }
        } else {
            response.requireAuthentication(settings.realm);
            response.setContentLength(0);
            response.send();
            return false;
        }
    } else {
        return true;
    }
}
开发者ID:bakercp,项目名称:ofxWebServer,代码行数:52,代码来源:ofxWebServerBaseRouteHandler.cpp

示例2: handleRequest

	void handleRequest(HTTPServerRequest& request, HTTPServerResponse& response)
	{
		HTMLForm form(request, request.stream());

		response.setContentType("text/html");
		response.setChunkedTransferEncoding(true);
        
		std::ostream& ostr = response.send();
        
        URI requestURI(request.getURI());
        
		ostr << "<html><head><title>Echo HTTP Headers and HTML Form Information</title>"
		        "<link rel=\"stylesheet\" href=\"css/styles.css\" type=\"text/css\"/></head><body>"
		        "<div class=\"header\">"
				"<h1 class=\"category\">Open Service Platform</h1>"
				"<h1 class=\"title\">Echo HTTP Headers and HTML Form Information</H1>"
				"</div>"
		        "<div class=\"body\">";
        
		// echo the items available by method
		ostr << "<h2>Request</h2>"
		     << "<ul>"
		     << "<li><b>Method:</b> " << request.getMethod() << "</li>"
             << "<li><b>URI Path:</b> " << requestURI.getPath() << "</li>"
             << "<li><b>URI Query:</b> " << requestURI.getQuery() << "</li>"
             << "<li><b>HTTP Version:</b> " << request.getVersion() << "</li>"
             << "<li><b>Host:</b> " << request.getHost() << "</li>"
             << "<li><b>Content Type:</b> " << request.getContentType() << "</li>"
             << "<li><b>Chunked:</b> " << (request.getChunkedTransferEncoding() ? "true" : "false") << "</li>"
             << "<li><b>Keep Alive:</b> " << (request.getKeepAlive() ? "true" : "false") << "</li>"
             << "<li><b>Transfer Encoding:</b> " << request.getTransferEncoding() << "</li>"
             << "<li><b>Client Address:</b> " << request.clientAddress().toString() << "</li>";

        if (request.hasContentLength())
        {
            ostr << "<li><b>Content Length:</b> " << request.getContentLength64() << "</li>";
        }

		ostr << "</ul>";

        // echo the request headers
        {
            ostr << "<hr>"
                    "<h2>Request Headers</h2><ul>\n";

            NameValueCollection headers;
            NameValueCollection::ConstIterator itr(request.begin());
            NameValueCollection::ConstIterator itrEnd(request.end());
            
            for (; itr != itrEnd; ++itr)
            {
                ostr << "<li><b>" << htmlize(itr->first) << "</b>: " <<  htmlize(itr->second) << "</li>";
            }
            ostr << "</ul>";
        }

        // echo any cookies
        {
            NameValueCollection cookies;
            request.getCookies(cookies);

            if (!cookies.empty())
            {
                ostr << "<hr>";
                ostr << "<h2>Cookies</h2><ul>\n";

                NameValueCollection::ConstIterator itr(cookies.begin());
                NameValueCollection::ConstIterator itrEnd(cookies.end());
                
                for (; itr != itrEnd; ++itr)
                {
                    ostr << "<li><b>" << htmlize(itr->first) << "</b>: " <<  htmlize(itr->second) << "</li>";
                }
                ostr << "</ul>";
            }
        }
        
        // echo any form data (GETs or POSTs)
		if (!form.empty())
		{
            ostr << "<hr>"
			        "<h2>Form Data</h2><ul>\n";

			NameValueCollection::ConstIterator itr(form.begin());
			NameValueCollection::ConstIterator itrEnd(form.end());
			for (; itr != itrEnd; ++itr)
			{
				ostr << "<li><b>" << htmlize(itr->first) << "</b>: " << htmlize(itr->second) << "</li>\n";
			}
			ostr << "</ul>";
		}
        
        ostr << "<hr>"
             << "<h2>Response</h2>"
             << "<ul>"
		     << "<li><b>Status:</b> "  << response.getStatus()  << "</li>"
             << "<li><b>Reason:</b> "  << response.getReason()  << "</li>"
            << "</ul>";
        
        // echo the response headers
//.........这里部分代码省略.........
开发者ID:macchina-io,项目名称:macchina.io,代码行数:101,代码来源:WebEcho.cpp


注:本文中的HTTPServerRequest::getCookies方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。