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

📄 wml_uafn.c

📁 是一个手机功能的模拟程序
💻 C
📖 第 1 页 / 共 5 页
字号:
int SDL_GetElementType (void *pElm)
{
	

    ELEMENTTYPE *pTheElement;
    pTheElement = (ELEMENTTYPE*)pElm;

    if (pTheElement!=NULL)
    {
      #ifdef WAE_DEBUG
        PrintChar("***ELEMENTTYPE***");
        PrintInt(pTheElement->iType);
      #endif
	  if ( isTypeWMLText(pTheElement->iType) )
		return Type_GenericText;
	  else
        return (pTheElement->iType);
    }
    return 0;
}


WCHAR* SDL_GetOptionText (void *pUser, void* pElement)
{
  WCHAR* pwchText=NULL;

  if (pElement != NULL)
  {
    pELEMENTTYPE pTempElm = pElement;
    UINT32 iLength=0;

    /* Calculate length */
    while (pTempElm != NULL)
    {
      /* Check if text */
      if (isTypeWMLText(pTempElm->iType))
      {
        /* Add length */
        iLength += WML_GetStringLength (pUser, pTempElm);
      }

      /* Get next element */
      pTempElm = WML_GetNextElement (pTempElm);
    }

    /* Allocate memory */
    pwchText = NEWARRAY (WCHAR,iLength+1);

    if (pwchText != NULL)
    {
      WCHAR* pwchTempText=NULL;
      pTempElm = pElement;

      /* Set termination character */
      *pwchText=0;

      while (pTempElm != NULL)
      {
        /* Check if text */
        if (isTypeWMLText(pTempElm->iType))
        {
          /* Get string */
          pwchTempText = WML_GetString (pUser, pTempElm);

          /* Concat string */
          if (pwchTempText != NULL)
          {
            CONCATSTRING (pwchText,pwchTempText);
            DEALLOC (&pwchTempText);
          }
        }

        /* Get next element */
        pTempElm = WML_GetNextElement (pTempElm);
      }
    }
  }

  return pwchText;
}


/*----------------------------------------------
some functions directly mapped from SDL, check
interface.h functions for further info.
------------------------------------------------*/
void* SDL_GetElementContent (void *TheElement) 
{
  return (void*)WML_GetContent((ELEMENTTYPE*)TheElement);
}

void *SDL_GetNextElement (void *pElementObj)
{
     return (void*)WML_GetNextElement ((ELEMENTTYPE *)pElementObj);
}

void* SDL_GetText(void *pUser,void* pTextElement)
{
   return (void*)WML_GetString((UA*)pUser, (ELEMENTTYPE*)pTextElement);
}

/*---------------------------------------------------
Returns a pointer to the card with pCardName. 
If pCardName is NULL then the first card is returned,
----------------------------------------------------*/
void *SDL_GetCard (void *pUser, void* pCardName)
{
   ELEMENTTYPE* pCard=NULL;
   WCHAR* pTheCardName=NULL;
   pTheCardName = wip_byte2wchar((BYTE*)pCardName);
   pCard = WML_GetCard ((UA*)pUser, pTheCardName);
   DEALLOC(& pTheCardName);
   return (void*)pCard; 
}


/*---------------------------------------------
Checks if the card contains any NEWCONTEXT, in
that case deletes both History and Variabletable
Return NewContext 1 (TRUE), 0 (FALSE)
----------------------------------------------*/

int SDL_HandleNewContext(void *pUser,void *pTheCard)
{
   if (pTheCard !=NULL)
   {

     if ( ( (CARDELEMENT*)(pTheCard) )->fNewContext)
     {
       SDL_ClearContext(pUser);
	   return 1;
     }
   }
   return 0;
}




/*---------------------------- Functionality to handle (WSP) responses ----------------------------------*/

/*----------------------------------------------
Checks the WSP header for additional information about
the contentbase (checks Content Location and 
Content Base). (see RFC 2068 14.11, 14.15)
Returns a copy of the new base URL (if any)
------------------------------------------------*/
void* GetResponseBaseURL(void* pHeaderData, void* pRequestURL )
{
  HEADERDEF *pHeaderList = NULL;
  HEADERELEMENT *pHeaderElement = NULL;
  BYTE* pbContentBase=NULL;
  BYTE* pbContentLocation=NULL;
  BYTE* pbResult=NULL;

  pHeaderList = (HEADERDEF*)pHeaderData;

  if (pHeaderList !=NULL && pRequestURL!=NULL)
  {
    pHeaderElement = WSP_GetHeaderWK( 1, Field_Content_Base, pHeaderList->pHeadList );
    pbContentBase = WSP_GetContentBase(pHeaderElement);

	/* if there is a contentbase use that */
	if (pbContentBase!=NULL)
    {
       /* if not absolute try to resolve */
       b_Resolve ((BYTE*)pRequestURL, pbContentBase, & pbResult);

       pHeaderElement = WSP_GetHeaderWK( 1, Field_Content_Location, pHeaderList->pHeadList );

	   /* then use content location to resolve */
       pbContentLocation = WSP_GetContentLocation (pHeaderElement);
	   if (pbContentLocation!=NULL)
	   {
         pbContentBase = pbResult;
         b_Resolve ((BYTE*)pbContentBase, pbContentLocation, & pbResult);
		 DEALLOC(&pbContentBase);
       }
	}
	else /* otherwise try any content location field (absolute or relative) */
    {
       pHeaderElement = WSP_GetHeaderWK( 1, Field_Content_Location, pHeaderList->pHeadList );
       pbContentLocation = WSP_GetContentLocation (pHeaderElement);
	   if (pbContentLocation!=NULL)
         b_Resolve ((BYTE*)pRequestURL, pbContentLocation, & pbResult);
	}
  }

 #ifdef WAE_DEBUG
    PrintChar("Response base URL:");
    PrintChar(pbResult);
  #endif

  return (void*)pbResult;
}

/*--------------------------------------------------------------------------
 Updates the baseurl of the current card (with either the received response 
 url or the sent request url) and also copies the fragment 
----------------------------------------------------------------------------*/
void SDL_UpdateBaseURL(void* pUserAgent, void* pRequestURL ,void* pHeaderData)
{
  BYTE* pNewBase=NULL;
  BYTE* pFrag=NULL;    
  UA* pUser=NULL;
  int iBaseLength =0; 
  ELEMENTTYPE* pFirstCard=NULL;

  pUser = (UA*)pUserAgent;

  DEALLOC(&(pUser->pbURLBase));  /* remove any old base */
	
  if (pHeaderData!=NULL)  /* this was the result of a network request */
  {
     pNewBase = (BYTE*)GetResponseBaseURL(pHeaderData, pRequestURL );  /* retrieve the new url */
  }

  if (pNewBase==NULL) 
  {
     #ifdef WAE_DEBUG
       PrintChar("Using request URL:");
       PrintChar(pRequestURL);
     #endif

	 pNewBase = (BYTE*)GenCopyString(1, pRequestURL);   /* no info in header use pRequestURL instead */
  }

  if (pNewBase)
  {
	 b_GetFragment ((BYTE*)pNewBase, & pFrag);
  
	 /* no frag present e.g it was the first card */
     if (pFrag==NULL)
	 {
        pFirstCard= WML_GetCard (pUser, NULL); 
        if (pFirstCard!=NULL)
		{
			/* TODOjens ->pElm) */
           pFrag = (BYTE*)GenCopyString(2,((CARDELEMENT*)(pFirstCard))->pchId);  /*cast to avoid warnings, this is actually WCHAR */
           SDL_wchar2byte((void*)& pFrag);   /* transform to byte */
		}
  	    if (pFrag)
		{
           iBaseLength = B_STRINGLENGTH((const char *)pNewBase);
           pUser->pbURLBase = NEWARRAY(BYTE,B_STRINGLENGTH((const char *)pFrag)+iBaseLength+2);
		   if (pUser->pbURLBase)
           {
             B_COPYSTRING((CHAR*)pUser->pbURLBase,(CHAR*)pNewBase);
             B_COPYSTRING((CHAR*)(&(pUser->pbURLBase[iBaseLength])),(CHAR*)"#");
             B_COPYSTRING((CHAR*)(&(pUser->pbURLBase[iBaseLength+1])),(CHAR*)pFrag);
             DEALLOC(&pNewBase);
           }
		}
		else
		{
			pUser->pbURLBase = pNewBase;
		}

	 }
	 else  /* there is a frag, check if valid, if not change to first card */
	 {
	   ELEMENTTYPE* pCard=NULL;
	   WCHAR* pTheCardName=NULL;
   
	   BYTE* pNewURL=NULL;
	   BYTE* pbFragAncor=NULL;

	   BOOL overflow=FALSE;

	   pTheCardName = wip_byte2wchar(pFrag);
	   pCard = WML_GetCard (pUser, pTheCardName);

	   pUser->pbURLBase = pNewBase;

	   if (pCard && ( ((CARDELEMENT*)(pCard))->pchId ))
	   {
		   if ( COMPARESTRING(pTheCardName, ( ((CARDELEMENT*)(pCard))->pchId )  ) !=0 )
		   {
			   BYTE* pCardFrag=wip_wchar2byte(( ((CARDELEMENT*)(pCard))->pchId ), &overflow);

			   if (pCardFrag!=NULL && !overflow)
			   {
				   pbFragAncor=NEWARRAY(BYTE, B_STRINGLENGTH((const char*)pCardFrag)+2);
				   *pbFragAncor='#';
				   B_COPYSTRING( (char*)(pbFragAncor+1), (const char*)pCardFrag );

				   if (b_Resolve(pNewBase, pbFragAncor, &pNewURL))
				   {
					   DEALLOC(&pNewBase);
	    			   pUser->pbURLBase=pNewURL;
				   }
				   DEALLOC(&pbFragAncor);

			   }
			   DEALLOC(&pCardFrag);

		   }
	   }
	   DEALLOC(& pTheCardName);

	 }

     DEALLOC(&pFrag);
  }

  #ifdef WAE_DEBUG
    PrintChar("Set new base URL:");
    PrintChar(pUser->pbURLBase);
  #endif
}

/*--------------------------------------------------------------------
Returns IANA code if any and the content-type as a reference string 
-----------------------------------------------------------------------*/

void* SDL_GetDetailedContentType(void* pTheHeaderData)
{
    HEADERDEF *pHeaderList = NULL;
    HEADERELEMENT *pHeaderElement = NULL;
    BYTE* pbContentTypeText=NULL;
    BOOL fError=TRUE;
	WSPPARAMETERS* pWSPParam = NULL;

    pHeaderList = (HEADERDEF*)pTheHeaderData;

    if (pHeaderList !=NULL)
    {
       pHeaderElement = WSP_GetHeaderWK ( 1, Field_Content_Type, pHeaderList->pHeadList );
       if (pHeaderElement !=NULL)
       {
          pbContentTypeText= WSP_GetContentTypeString (pHeaderElement, &fError, &pWSPParam);
		  if (pWSPParam)
	         WSP_DeleteParameters (&pWSPParam);
	   }
    }
	return pbContentTypeText;
}


CONTENTINFOSTRUCT* SDL_CreateResponseInfo(void)
{
    CONTENTINFOSTRUCT* pCStruct=NULL;
	pCStruct = NEWSTRUCT(CONTENTINFOSTRUCT);
	
	if (pCStruct)
    {
		pCStruct->pContentTypeAsText = NULL;
		pCStruct->iIANACharID = IANA_CHARSET_INVALID;
		pCStruct->iPIVersion =1;  /* 1=unknown */
		pCStruct->iContentTypeAsInt =0;	
		pCStruct->fCacheControl =0;	
	}
	return pCStruct;
}

void SDL_DeleteResponseInfo(void** ppTheInfo)
{
	if (ppTheInfo && *ppTheInfo)
    {
	  DEALLOC( & ((CONTENTINFOSTRUCT*)(*ppTheInfo))->pContentTypeAsText);
      DEALLOC(ppTheInfo);
	}
}

BOOL SDL_GetResponseInformation( int iScheme,  void* pTheHeaderData, void** ppResult, void** ppContentType)
{
    HEADERDEF *pHeaderList = NULL;
    HEADERELEMENT *pHeaderElement = NULL;
    BOOL fError=TRUE;
    WSPPARAMETERS* pWSPParam = NULL;
    CONTENTINFOSTRUCT* pCStruct=NULL;

    pHeaderList = (HEADERDEF*)pTheHeaderData;

    pCStruct = SDL_CreateResponseInfo();

	if (!pCStruct)
		return FALSE;
	
    SDL_DeleteResponseInfo(ppResult);

	*ppResult = pCStruct;

    switch (iScheme)
	{ 
		case Scheme_http:
		case Scheme_https:
		case Scheme_wapdevice:

	        if (!pHeaderList)     /* do not proceed use default instead */
		       return TRUE;			

			/* get the first contenttypefield, there should only be one*/
			pHeaderElement = WSP_GetHeaderWK ( 1, Field_Content_Type, pHeaderList->pHeadList );
			if (pHeaderElement !=NULL)
			{
			  /* get contenttypevalue, represented as a byte */
			  pCStruct->iContentTypeAsInt = WSP_GetContentTypeByte (pHeaderElement, &fError, & pWSPParam); 
              
			  if (pWSPParam)
			  {
			     pCStruct->iIANACharID = pWSPParam->iCharset;
				 pCStruct->iPIVersion = pWSPParam->iLevel;
	             WSP_DeleteParameters (&pWSPParam);
			  }

			  /* do the conversion to a stringrepr. */
			  if (fError!=TRUE)
			  {
				 WSP_ConvertContentStringByte(&pCStruct->iContentTypeAsInt, & pCStruct->pContentTypeAsText);
			  }
			}

		  
		/*	  if (#SDL(ContentTypeAsText)== NULL && #SDL(ResponseData).Status == 0x20 )
				SDL_OutputError( (UA*)#SDL(UA), ERR_WAE_UA_WSP_RESPONSE_INVALID, ERRTYPE_INFORMATION ); */ 
			  
			pCStruct->fCacheControl = SDL_GetCacheControl(pHeaderList);
 			break;

		case Scheme_file:
		case Scheme_function:
	        if (!*ppContentType)     /* do not proceed use default instead */
		       return TRUE;	

			pCStruct->pContentTypeAsText = *ppContentType;
			*ppContentType = NULL;
			pCStruct->iIANACharID = cfg_wae_ua_fileCharEncoding;
			pCStruct->fCacheControl = 0;
			pCStruct->iContentTypeAsInt = 0;
			WSP_ConvertContentStringByte(&pCStruct->iContentTypeAsInt, & pCStruct->pContentTypeAsText);
			break;
	}

	*ppResult = pCStruct;

	return TRUE;

}

  /*--------------------------------------------------------------
Cleanup function to be used when the response header is
ready to be removed
----------------------------------------------------------------*/
void DeleteWSPHeader(void** ppTheHeaderData)
{
  HEADERDEF** ppHeaderList;
  ppHeaderList = (HEADERDEF**)ppTheHeaderData;
  if (*ppTheHeaderData != NULL)
  {
    DeleteHEADERDEF(*ppHeaderList);
    *ppHeaderList=NULL;
  }
}


void SDL_DeleteResponseHeaderInfo( int iScheme, void** ppHeaderHandle, void** ppResponseInfo)
{
  if (iScheme == Scheme_https || iScheme == Scheme_http || iScheme == Scheme_wapdevice)
    DeleteWSPHeader( ppHeaderHandle );


  SDL_DeleteResponseInfo(ppResponseInfo);
}



/*--------------------------------------------------------------------

⌨️ 快捷键说明

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