⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                TraceTag(ttidSsdpParser, "Token %s does not match any SSDP "
                         "headers",token);
            }
        }
        // Get the next header
        if (!strncmp(pBeyondTokenEnd, szEndOfHeaders, END_HEADERS_SIZE))
        {
            return (pBeyondTokenEnd + END_HEADERS_SIZE);
        }
        else
        {
            token = strtok(NULL, "\r\n");
        }
    }

    // We should always have a "\r\n\r\n" in any legal message.
    TraceTag(ttidSsdpParser, "Received message does not contain \\r\\n\\r\\n. Ignored. ");
    FreeSsdpRequest(Result);
    Result->status = HTTP_STATUS_BAD_REQUEST;   
    return NULL;
}

VOID InitializeSsdpRequest(SSDP_REQUEST *pRequest)
{
    memset(pRequest, 0, sizeof(SSDP_REQUEST));
    pRequest->Method = SSDP_INVALID;

}


VOID FreeSsdpRequest(SSDP_REQUEST *pSsdpRequest)
{
    INT i = 0;

    if (pSsdpRequest->Content != NULL)
    {
        SsdpFree(pSsdpRequest->Content);
        pSsdpRequest->Content = NULL;
    }

    if (pSsdpRequest->ContentType != NULL)
    {
        SsdpFree(pSsdpRequest->ContentType);
        pSsdpRequest->ContentType = NULL;
    }

    if (pSsdpRequest->RequestUri != NULL)
    {
        SsdpFree(pSsdpRequest->RequestUri);
        pSsdpRequest->RequestUri = NULL;
    }

    for (i = 0; i < NUM_OF_HEADERS; i++)
    {
        if (pSsdpRequest->Headers[i] != NULL)
        {
            SsdpFree(pSsdpRequest->Headers[i]);
            pSsdpRequest->Headers[i] = NULL;
        }
    }
}
// Get rid of leading or trailing white space or tab.

VOID PrintSsdpRequest(const SSDP_REQUEST *pssdpRequest)
{
    INT i;

    for (i = 0; i < NUM_OF_HEADERS; i++)
    {
        if (pssdpRequest->Headers[i] == NULL)
        {
            TraceTag(ttidSsdpParser, "%s = (NULL) ",SsdpHeaderStr[i],
                     pssdpRequest->Headers[i]);
        }
        else
        {
            TraceTag(ttidSsdpParser, "%s = (%s) ",SsdpHeaderStr[i],
                     pssdpRequest->Headers[i]);
        }
    }
}

// Assume szValue does not have beginning or trailing spaces.

INT GetMaxAgeFromCacheControl(const CHAR *szValue)
{
    const CHAR * pEqual;
    _int64 Temp;

    if (szValue == NULL)
    {
        return -1;
    }

    pEqual = strchr(szValue, '=');
    if (pEqual == NULL)
    {
        return -1;
    }

    Temp = _atoi64(pEqual+1);

    if (Temp > UINT_MAX / 1000)
    {

        TraceTag(ttidSsdpAnnounce, "Life time exceeded the UINT limit. "
                 "Set to limit");

        Temp = UINT_MAX;
    }

    return (UINT)Temp;
}

BOOL ConvertToByebyeNotify(PSSDP_REQUEST pSsdpRequest)
{
    CHAR * szTemp = "ssdp:byebye";

    if(pSsdpRequest->Headers[SSDP_NTS])
        free(pSsdpRequest->Headers[SSDP_NTS]);
    pSsdpRequest->Headers[SSDP_NTS] = NULL;

    pSsdpRequest->Headers[SSDP_NTS] = _strdup(szTemp);

    if (pSsdpRequest->Headers[SSDP_NTS] == NULL)
    {
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

BOOL ConvertToAliveNotify(PSSDP_REQUEST pSsdpRequest) 
{
    CHAR * szTemp = "ssdp:alive"; 

    Assert(pSsdpRequest->Headers[SSDP_ST] != NULL); 
    Assert(pSsdpRequest->Headers[SSDP_NTS] == NULL); 
    pSsdpRequest->Headers[SSDP_NTS] = _strdup(szTemp); 

    if (pSsdpRequest->Headers[SSDP_NTS] == NULL)
    {
        return FALSE; 
    }
    else 
    {
        pSsdpRequest->Headers[SSDP_NT] = pSsdpRequest->Headers[SSDP_ST]; 
        pSsdpRequest->Headers[SSDP_ST] = NULL; 
        return TRUE; 
    }
}

BOOL CompareSsdpRequest(const PSSDP_REQUEST pRequestA, const PSSDP_REQUEST pRequestB)
{
    INT i;

    for (i = 0; i < NUM_OF_HEADERS; i++)
    {
        if ((pRequestA->Headers[i] == NULL && 
             pRequestB->Headers[i]!= NULL) ||
            (pRequestA->Headers[i] != NULL && 
             pRequestB->Headers[i]== NULL))
        {
            return FALSE; 
        } 
        else if (pRequestA->Headers[i] != NULL && 
                 pRequestB->Headers[i] != NULL)
        {
            if (strcmp(pRequestA->Headers[i], pRequestB->Headers[i]) != 0) 
            {
                TraceTag(ttidSsdpParser, "Different headers index %d",i); 
                return FALSE; 
            }
        }
    }

    Assert(pRequestA->Content == NULL && pRequestB->Content == NULL); 

    Assert(pRequestA->ContentType == NULL && pRequestB->ContentType == NULL); 

    // We ignore Request URI, as they should always be * for alive and byebye. 

    return TRUE;

}

// Deep copy

BOOL CopySsdpRequest(PSSDP_REQUEST Destination, const PSSDP_REQUEST Source)
{
    INT i;
    *Destination = *Source;

    for (i = 0; i < NUM_OF_HEADERS; i++)
    {
        if (Source->Headers[i] != NULL)
        {
            Destination->Headers[i] = (CHAR *) SsdpAlloc(
                strlen(Source->Headers[i]) + 1);
            if (Destination->Headers[i] == NULL)
            {
                goto cleanup;
            }
            else
            {
                strcpy(Destination->Headers[i], Source->Headers[i]);
            }
        }
    }

    if (Source->Content != NULL)
    {
        Destination->Content = (CHAR *) SsdpAlloc(
            strlen(Source->Content) + 1);
        if (Destination->Content == NULL)
        {
            goto cleanup;
        }
        else
        {
            strcpy(Destination->Content, Source->Content);
        }
    }

    if (Source->ContentType != NULL)
    {
        Destination->ContentType = (CHAR *) SsdpAlloc(
            strlen(Source->ContentType) + 1);
        if (Destination->ContentType == NULL)
        {
            goto cleanup;
        }
        else
        {
            strcpy(Destination->ContentType, Source->ContentType);
        }
    }

    if (Source->RequestUri != NULL)
    {
        Destination->RequestUri = (CHAR *) SsdpAlloc(
            strlen(Source->RequestUri) + 1);
        if (Destination->RequestUri == NULL)
        {
            goto cleanup;
        }
        else
        {
            strcpy(Destination->RequestUri, Source->RequestUri);
        }
    }

    return TRUE;

cleanup:

    FreeSsdpRequest(Destination);
    return FALSE;
}

BOOL IsStrDigits(LPSTR pszStr)
{
    int i = 0; 
    while (pszStr[i] != '\0')
    {
        if (isdigit(pszStr[i++]) == 0)
        {
            return FALSE; 
        }
    }

    return TRUE; 
}
VOID strtrim(CHAR ** pszStr)
{

    CHAR *end;
    CHAR *begin;

    // Empty string. Nothing to do.
    //
    if (!(**pszStr))
    {
        return;
    }

    begin = *pszStr;
    end = begin + strlen(*pszStr) - 1;

    while (*begin == ' ' || *begin == '\t')
    {
        begin++;
    }

    *pszStr = begin;

    while (*end == ' ' || *end == '\t')
    {
        end--;
    }

    *(end+1) = '\0';
}

CHAR* IsHeadersComplete(const CHAR *szHeaders)
{
    return (CHAR*)strstr(szHeaders, szHeaderEnd); 
}


// IsLinkLocal
__inline int
IsLinkLocal(const IPv6Addr *Addr)
{
    return ((Addr->s6_bytes[0] == 0xfe) &&
            ((Addr->s6_bytes[1] & 0xc0) == 0x80));
}

// FixURLAddressScopeId
BOOL FixURLAddressScopeId(LPCSTR pszURL, DWORD ScopeId, LPSTR pszBuffer, DWORD* pdwSize)
{
    Assert(pszURL != NULL);
    Assert(pdwSize != NULL);
    Assert(pszBuffer != NULL || *pdwSize == 0);
    
    char pszHost[INTERNET_MAX_HOST_NAME_LENGTH];
    char pszUrlPath[INTERNET_MAX_PATH_LENGTH];
    char pszUserName[INTERNET_MAX_USER_NAME_LENGTH];
    char pszPassword[INTERNET_MAX_PASSWORD_LENGTH];
    
    URL_COMPONENTSA urlComp = {0};
    
    urlComp.dwStructSize = sizeof(URL_COMPONENTS);
    
    // server
    urlComp.lpszHostName = pszHost;
    urlComp.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH;
    
    // URL Path
    urlComp.lpszUrlPath = pszUrlPath;
    urlComp.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH;

    // user
    urlComp.lpszUserName = pszUserName;
    urlComp.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH;

    // password
    urlComp.lpszPassword = pszPassword;
    urlComp.dwPasswordLength = INTERNET_MAX_PASSWORD_LENGTH;
    
    // break down URL
    if(!InternetCrackUrlA(pszURL, 0, ICU_DECODE, &urlComp))
    {
        TraceTag(ttidSsdpParser, "Error %d cracking URL %s.", GetLastError(), pszURL);
        return FALSE;
    }
        
    int             nSize;
    SOCKADDR_IN6    sockaddr;
    wchar_t         pwszHost[INTERNET_MAX_HOST_NAME_LENGTH];
    
    if(-1 == mbstowcs(pwszHost, pszHost, INTERNET_MAX_HOST_NAME_LENGTH))
    {
        TraceTag(ttidSsdpParser, "Error converting host %s to unicode.", pszHost);
        return FALSE;
    }
    
    // get IPv6 address from the URL
    if(ERROR_SUCCESS == WSAStringToAddress(pwszHost, AF_INET6, NULL, (LPSOCKADDR)&sockaddr, &(nSize = sizeof(sockaddr))))
        if(IsLinkLocal((IPv6Addr*)&sockaddr.sin6_addr))
        {
            DWORD dw;
            
            // set scope id for IPv6 address
            sockaddr.sin6_scope_id = ScopeId;
            
            // format address with scope id
            if(ERROR_SUCCESS != WSAAddressToString((LPSOCKADDR)&sockaddr, sizeof(sockaddr), NULL, pwszHost + 1, &(dw = INTERNET_MAX_HOST_NAME_LENGTH - 2)))
            {
                TraceTag(ttidSsdpParser, "Error %d converting address to string.", GetLastError());
                return FALSE;
            }
            
            pwszHost[0] = L'[';
            wcscat(pwszHost, L"]");
            
            urlComp.dwHostNameLength = wcstombs(pszHost, pwszHost, INTERNET_MAX_HOST_NAME_LENGTH);
            
            Assert(urlComp.dwHostNameLength > 0);
            
            // recreate the location URL, now including scope id
            return InternetCreateUrlA(&urlComp, 0, pszBuffer, pdwSize);
        }
        
    return TRUE;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -