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


C++ StringTokenizer::hasNextToken方法代码示例

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


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

示例1: readCookies

void CookieJar::readCookies(HttpHeader* header, CookieOrigin origin)
{
    // parse cookies if appropriate cookie field exists
    const char* field = (origin == Server ? "Set-Cookie" : "Cookie");
    int count = header->getFieldCount(field);
    if(count > 0)
    {
        // Set-Cookie: cookie1_name=cookie1_value; max-age=0; path=/
        // Set-Cookie: c2=v2; expires=Thu, 21-Aug-2008 23:47:25 GMT; path=/
        // Cookie: cookie1_name=cookie1_value; cookie2_name=cookie2_value

        // parse cookies by semi-colons (cannot parse by commas because
        // the "expires" value may contain a comma and no one follows the
        // standard)
        string cookies;
        Cookie cookie(NULL);
        TimeZone gmt = TimeZone::getTimeZone("GMT");
        Date d;
        Date now;
        bool name;
        StringTokenizer pairs;
        for(int i = 0; i < count; ++i)
        {
            name = true;
            header->getField(field, cookies, i);
            pairs.tokenize(cookies.c_str(), ';');
            while(pairs.hasNextToken())
            {
                // get next token (name=value)
                const char* token = pairs.nextToken();

                // get name part of token
                size_t nameLength = strcspn(token, "=");
                char tmpName[nameLength + 1];
                strncpy(tmpName, token, nameLength);
                tmpName[nameLength] = 0;

                // trim whitespace from name
                char* namePtr = (tmpName + nameLength);
                for(; namePtr != tmpName && *namePtr == ' '; --namePtr)
                {
                    *namePtr = 0;
                }
                for(namePtr = tmpName; *namePtr != 0 && *namePtr == ' ';
                        ++namePtr);

                // get value part of token (ensure value length is at least 0 in
                // case bad parsing because of no equals sign after name)
                size_t valueLength = strlen(token);
                valueLength = (nameLength == valueLength ?
                               0 : valueLength - nameLength - 1);
                char tmpValue[valueLength + 1];
                if(valueLength > 0)
                {
                    strncpy(tmpValue, token + nameLength + 1, valueLength);
                }
                tmpValue[valueLength] = 0;

                if(origin == Client)
                {
                    // set cookie
                    setCookie(namePtr, tmpValue, 0, false, false);
                }
                else
                {
                    if(name)
                    {
                        // first token *must* be cookie name = cookie value
                        cookie = Cookie();
                        cookie["name"] = namePtr;
                        cookie["value"] = tmpValue;
                        name = false;
                    }
                    else
                    {
                        if(strcmp(namePtr, "expires") == 0)
                        {
                            // parse expiration time
                            if(d.parse(tmpValue, HttpHeader::sDateFormat, &gmt))
                            {
                                int64_t age = d.getSeconds() - now.getSeconds();
                                cookie["maxAge"] = (age <= 0 ? 0 : age);
                            }
                            else
                            {
                                // bad date format, use maxAge of 0
                                cookie["maxAge"] = 0;
                            }
                        }
                        else if(strcasecmp(namePtr, "secure") == 0)
                        {
                            // cookie is secure
                            cookie["secure"] = true;
                        }
                        else if(strcasecmp(namePtr, "HttpOnly") == 0)
                        {
                            // cookie is http-only (non-javascript)
                            cookie["httpOnly"] = true;
                        }
                        else
//.........这里部分代码省略.........
开发者ID:zengyuxing007,项目名称:monarch,代码行数:101,代码来源:CookieJar.cpp


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