예제 #1
0
bool ON_wString::UrlDecode()
{
  CopyArray();

  bool rc = true;
  wchar_t c;
  wchar_t* s0 = Array();
  if ( !s0 )
    return true;
  wchar_t* s1 = s0;
  //const wchar_t* debg = s1;
  int i;
  for (i = Length(); i > 0; i-- )
  {
    c = *s0++;
    if (0==c)
      break;
    if (i >= 3 && '%' == c && UrlDecodeHelper(s0) )
    {
      s0++;
      *s1++ = *s0++;
      i -= 2;
    }
    else
    {
      *s1++ = c;
      if (rc)
        rc = IsValidUrlChar(c);
    }
  }
  *s1 = 0;
  SetLength(s1 - Array());
  return rc;
}
/**
Parses URL from a token. Is used by SearchUrlL method and if a URL
was found it's appended to item array. Note that parsing for generic URIs 
is done with SearchGenericUriL -method.

@param aType  a Type of URL to seach, i.e.
                  www.
                  wap.
                  IP e.g.127.0.0.1
@param        aTokenPtr Pointer to token that will be parsed
@param        aTextOffset Offset of the token (start position in the whole text)
@leave KErrNone, if successful; otherwise one of the other system-wide error codes.
@return ETrue if the parameter for phone number is valid, else returns EFalse
*/
TBool CTulAddressStringTokenizer::ParseUrlL(const TDesC& aType, const TPtrC& aTokenPtr, TInt aTextOffset)
    {
    TBool wasValidUrl = EFalse;
    TLex url;
    
    TInt position = aTokenPtr.FindF( aType ); 
    if ( position != KErrNotFound )
        { // address start found
        url = aTokenPtr.Right( aTokenPtr.Length() - position );
        url.Inc( aType.Length() );

        while( IsValidUrlChar( url.Peek() ) && !(url.Eos()) )
            {
            if( url.Peek() == ':' )
                {
                url.Inc();
                if ( !url.Peek().IsDigit() )
                    {
                    url.UnGet();
                    break;
                    }
                }
            else
                url.Inc();
            }

        // If a period or question mark was followed by a whitespace remove it
        if ( url.Eos() ) // Can't be followed by white space if it's
            { // the last character at token
            url.UnGet();
            if ( url.Peek() != '.' && url.Peek() != '?' && url.Peek() != ',' )	// If it wasn't a period or question mark
                url.Inc();
            }
        
        url.Mark();
        wasValidUrl = ETrue;
        }

    if ( wasValidUrl && ( url.MarkedOffset() > aType.Length() ) )
        {
        AddItemL( aTextOffset - aTokenPtr.Length() + position, url.MarkedOffset(), EFindItemSearchURLBin );
        return ETrue;
        }

    return EFalse;
    }
/**
Search fixed start URLs, i.e. URLs without schema (www., wap.).
Also finds IPv4 addresses (*.*.*.*).
As a special case, supports deprecated hardcoded schematic addresses finding 
(http://, https://, rtsp://) to make sure deprecated search cases work 
as they did previously.

@param aText Text that will be parsed
@param aFindFixedSchemas If true, will find old fixed schematic URLs also
@return ETrue if any URL are found else returns EFalse
@leave KErrNone, if successful; otherwise one of the other system-wide error codes.
@panic ETulPanicDescriptorLength in debug build if item's position and/or length is out of the document's range.
*/
TBool CTulAddressStringTokenizer::SearchUrlL( const TDesC& aText, const TBool aFindFixedSchemas )
    {
    TLex text = aText;
    while ( !text.Eos() )
        {
        while( !(text.Eos()) && !IsValidUrlChar( text.Peek() ) )
            text.Inc();

        text.Mark();
        while( !(text.Eos()) && IsValidUrlChar( text.Peek() ) )
            text.Inc();

        TPtrC tokenPtr = text.MarkedToken();
        TBool wasValidUrl = EFalse;

        if ( aFindFixedSchemas )	// Search for http://
            wasValidUrl = ParseUrlL( KHttpUrlAddress, tokenPtr, text.Offset() );
        
        if (aFindFixedSchemas && !wasValidUrl)	// Search for https://
            wasValidUrl = ParseUrlL( KHttpsUrlAddress, tokenPtr, text.Offset() );

        if (aFindFixedSchemas && !wasValidUrl) // Search for rtsp://
            wasValidUrl = ParseUrlL( KRtspUrlAddress, tokenPtr, text.Offset() );

        if ( !wasValidUrl )	// Search for www.
            wasValidUrl = ParseUrlL( KWwwUrlAddress, tokenPtr, text.Offset() );

        if ( !wasValidUrl )	// Search for wap.
            wasValidUrl = ParseUrlL( KWapUrlAddress, tokenPtr, text.Offset() );

        if ( !wasValidUrl )	// Search for IP-address (xxx.xxx.xxx.xxx)
            { 
            if ( tokenPtr.Match( KIPAddress ) != KErrNotFound )
                {
                TInt periods = 0;
                wasValidUrl = ETrue;
                TBool endWithPunctuation = EFalse;
                TBool betweenBrackets = EFalse;

                // First see if token ends with ",",".","!","?",";" or ":"
                TChar charac = tokenPtr[tokenPtr.Length() - 1];
                TChar charac0 = tokenPtr[0];
                if ( charac == ',' || charac == '.' ||
                     charac == '!' || charac == '?' ||
                     charac == ';' || charac == ':' )
                    {
                    endWithPunctuation = ETrue;
                    }
                // Or if it starts and ends with brackets or quotation marks
                else if ( ( charac0 == '(' && charac == ')' )
                       || ( charac0 == '"' && charac == '"' )
                       || ( charac0 == '[' && charac == ']' )
                       || ( charac0 == '<' && charac == '>' ) )
                    {
                    betweenBrackets = ETrue;
                    }

                TInt i = 0;
                TInt tokensEnd = tokenPtr.Length();
                if ( endWithPunctuation )
                    tokensEnd--;
                else if ( betweenBrackets )
                    {
                    i = 1;
                    tokensEnd--;
                    }

                // Take a closer look to see if a valid IP-address
                TBuf<3> ipPart;
                TInt numbers = 0;
                for ( ; i < tokensEnd; i++ )
                    {
                    if ( !( ((TChar)tokenPtr[i]).IsDigit() || tokenPtr[i] == '.' ) )
                        {
                        wasValidUrl = EFalse;
                        break;
                        }

                    if ( tokenPtr[i] == '.' )
                        periods++;
                    else
                        numbers++;

                    if ( numbers > KNumbersInIpAddress || periods > KDotsInIpAddress )
                        {
                        wasValidUrl = EFalse;
                        break;
                        }

                    if ( ((TChar)tokenPtr[i]).IsDigit() )
                        {
                        ipPart.Append( tokenPtr[i] );
                        TBool checkInt = EFalse;
                        if ( i + 1 < tokensEnd )
                            {
                            if ( tokenPtr[i+1] == '.' )
                                checkInt = ETrue;
                            }

                        if ( i == tokensEnd - 1 || checkInt )
                            {
                            TLex val = ipPart;
                            TInt numberInt;
                            TInt error = val.Val( numberInt );
                            if ( error != KErrNone || numberInt > 255 )
                                {
                                wasValidUrl = EFalse;
                                break;
                                }

                            numbers = 0;
                            ipPart.Delete( 0, ipPart.Length() );
                            }
                        }
                    }

                if ( wasValidUrl && periods == KDotsInIpAddress )
                    {
                    TInt startPos = text.Offset() - tokenPtr.Length();
                    TInt length = tokenPtr.Length();
                    // If there was a punctuation at the end or brackets, let's take it/them away
                    if ( endWithPunctuation || betweenBrackets)
                        {
                        length--;
                        if ( betweenBrackets )
                            {
                            startPos++;
                            length--;
                            }
                        }

                    __ASSERT_DEBUG( startPos + length <= aText.Length(), Panic(ETulPanicDescriptorLength) );
                    AddItemL( startPos, length, EFindItemSearchURLBin );
                    }
                }
            }
        }

    return (iFoundItems->Count() > 0);
    }