Example #1
0
//Parses the request
QTSS_Error HTTPRequest::Parse()
{
    Assert(fRequestHeader.Ptr != NULL);
    StringParser parser(&fRequestHeader);
    
    // Store the request line (used for logging) 
    // (ex: GET /index.html HTTP/1.0)
    StringParser requestLineParser(&fRequestHeader);
    requestLineParser.ConsumeUntil(&fRequestLine, StringParser::sEOLMask);
  
    // Parse request line returns an error if there is an error in the
    // request URI or the formatting of the request line. 
    // If the method or version are not found, they are set
    // to httpIllegalMethod or httpIllegalVersion respectively, 
    // and QTSS_NoErr is returned.
	// 解析第一行
    QTSS_Error err = ParseRequestLine(&parser);
    if (err != QTSS_NoErr)
		return err;
  
    // Parse headers and set values of headers into fFieldValues array
    err = ParseHeaders(&parser);
    if (err != QTSS_NoErr)
            return err;
  
    return QTSS_NoErr;
}
Example #2
0
size_t cHTTPRequest::ParseHeaders(const char * a_Data, size_t a_Size)
{
	if (!m_IsValid)
	{
		return AString::npos;
	}
	
	if (m_Method.empty())
	{
		// The first line hasn't been processed yet
		size_t res = ParseRequestLine(a_Data, a_Size);
		if ((res == AString::npos) || (res == a_Size))
		{
			return res;
		}
		size_t res2 = m_EnvelopeParser.Parse(a_Data + res, a_Size - res);
		if (res2 == AString::npos)
		{
			m_IsValid = false;
			return res2;
		}
		return res2 + res;
	}
	
	if (m_EnvelopeParser.IsInHeaders())
	{
		size_t res = m_EnvelopeParser.Parse(a_Data, a_Size);
		if (res == AString::npos)
		{
			m_IsValid = false;
		}
		return res;
	}
	return 0;
}
void RequestParser::Parse(const string& s) {
  if (start == 0) {
    // Request line.
    ParseRequestLine(s);
  } else if (s.find("Range") == 0) {
    int64_t start,end;
    if (ParseRange(s, start, end)) {
      rangeStart = start;
      rangeEnd = end;
      hasRange = true;
    }
  }
}
Example #4
0
/**
 * @brief SIP 헤더 문자열을 파싱하여 CSipMessage 클래스의 멤버 변수에 저장한다.
 * @param pszText		SIP 헤더의 값을 저장한 문자열
 * @param iTextLen	pszText 문자열의 길이
 * @returns 성공하면 파싱한 길이를 리턴하고 그렇지 않으면 -1 를 리턴한다.
 */
int CSipMessage::Parse( const char * pszText, int iTextLen )
{
	if( pszText == NULL || iTextLen <= 4 ) return -1;

	int iPos, iCurPos, iNameLen, iValueLen;
	const char * pszName, * pszValue;
	CSipHeader	clsHeader;

#ifdef PARSE_FAST
	bool bNotFound;
#endif

	if( !strncmp( pszText, "SIP/", 4 ) )
	{
		iCurPos = ParseStatusLine( pszText, iTextLen );
	}
	else
	{
		iCurPos = ParseRequestLine( pszText, iTextLen );
	}

	if( iCurPos == -1 ) return -1;

	while( iCurPos < iTextLen )
	{
		iPos = clsHeader.Parse( pszText + iCurPos, iTextLen - iCurPos );
		if( iPos == -1 ) return -1;
		iCurPos += iPos;

		iNameLen = (int)clsHeader.m_strName.length();
		if( iNameLen == 0 ) break;

		pszName = clsHeader.m_strName.c_str();
		pszValue = clsHeader.m_strValue.c_str();
		iValueLen = (int)clsHeader.m_strValue.length();

#ifdef PARSE_FAST
		bNotFound = false;

		if( iNameLen == 1 )
		{
			if( pszName[0] == 'v' )
			{
				if( ParseSipVia( m_clsViaList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 'f' )
			{
				if( m_clsFrom.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 't' )
			{
				if( m_clsTo.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 'i' )
			{
				if( m_clsCallId.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 'm' )
			{
				if( ParseSipFrom( m_clsContactList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 'c' )
			{
				if( m_clsContentType.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( pszName[0] == 'l' )
			{
				m_iContentLength = atoi( pszValue );
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 2 )
		{
			if( !strcasecmp( pszName, "To" ) )
			{
				if( m_clsTo.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 3 )
		{
			if( !strcasecmp( pszName, "Via" ) )
			{
				if( ParseSipVia( m_clsViaList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 4 )
		{
			if( !strcasecmp( pszName, "From" ) )
			{
				if( m_clsFrom.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( !strcasecmp( pszName, "CSeq" ) )
			{
				if( m_clsCSeq.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 5 )
		{
			if( !strcasecmp( pszName, "Route" ) )
			{
				if( ParseSipFrom( m_clsRouteList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
#ifdef USE_ACCEPT_HEADER
		else if( iNameLen == 6 )
		{
			if( !strcasecmp( pszName, "Accept" ) )
			{
				if( ParseSipContentType( m_clsAcceptList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
#endif
		else if( iNameLen == 7 )
		{
			if( !strcasecmp( pszName, "Call-ID" ) )
			{
				if( m_clsCallId.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( !strcasecmp( pszName, "Contact" ) )
			{
				if( ParseSipFrom( m_clsContactList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( !strcasecmp( pszName, "Expires" ) )
			{
				m_iExpires = atoi( pszValue );
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 10 )
		{
			if( !strcasecmp( pszName, "User-Agent" ) )
			{
				m_strUserAgent = clsHeader.m_strValue;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 12 )
		{
			if( !strcasecmp( pszName, "Max-Forwards" ) )
			{
				m_iMaxForwards = atoi( pszValue );
			}
			else if( !strcasecmp( pszName, "Record-Route" ) )
			{
				if( ParseSipFrom( m_clsRecordRouteList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( !strcasecmp( pszName, "Content-Type" ) )
			{
				if( m_clsContentType.Parse( pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 13 )
		{
			if( !strcasecmp( pszName, "Authorization" ) )
			{
				if( ParseSipCredential( m_clsAuthorizationList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 14 )
		{
			if( !strcasecmp( pszName, "Content-Length" ) )
			{
				m_iContentLength = atoi( pszValue );
			}
			else
			{
				bNotFound = true;
			}
		}
#ifdef USE_ACCEPT_HEADER
		else if( iNameLen == 15 )
		{
			else if( !strcasecmp( pszName, "Accept-Encoding" ) )
			{
				if( ParseSipAcceptData( m_clsAcceptEncodingList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else if( !strcasecmp( pszName, "Accept-Language" ) )
			{
				if( ParseSipAcceptData( m_clsAcceptLanguageList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
#endif
		else if( iNameLen == 16 )
		{
			if( !strcasecmp( pszName, "WWW-Authenticate" ) )
			{
				if( ParseSipChallenge( m_clsWwwAuthenticateList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 18 )
		{
			if( !strcasecmp( pszName, "Proxy-Authenticate" ) )
			{
				if( ParseSipChallenge( m_clsProxyAuthenticateList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else if( iNameLen == 19 )
		{
			if( !strcasecmp( pszName, "Proxy-Authorization" ) )
			{
				if( ParseSipCredential( m_clsProxyAuthorizationList, pszValue, iValueLen ) == -1 ) return -1;
			}
			else
			{
				bNotFound = true;
			}
		}
		else
		{
			bNotFound = true;
		}

		if( bNotFound )
		{
			m_clsHeaderList.push_back( clsHeader );
		}
#else
		if( !strcasecmp( pszName, "Via" ) || !strcasecmp( pszName, "v" ) )
		{
			if( ParseSipVia( m_clsViaList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Max-Forwards" ) )
		{
			m_iMaxForwards = atoi( pszValue );
		}
		else if( !strcasecmp( pszName, "From" ) || !strcasecmp( pszName, "f" ) )
		{
			if( m_clsFrom.Parse( pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "To" ) || !strcasecmp( pszName, "t" ) )
		{
			if( m_clsTo.Parse( pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "CSeq" ) )
		{
			if( m_clsCSeq.Parse( pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Call-ID" ) || !strcasecmp( pszName, "i" ) )
		{
			if( m_clsCallId.Parse( pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Contact" ) || !strcasecmp( pszName, "m" ) )
		{
			if( ParseSipFrom( m_clsContactList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Record-Route" ) )
		{
			if( ParseSipFrom( m_clsRecordRouteList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Route" ) )
		{
			if( ParseSipFrom( m_clsRouteList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Authorization" ) )
		{
			if( ParseSipCredential( m_clsAuthorizationList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "WWW-Authenticate" ) )
		{
			if( ParseSipChallenge( m_clsWwwAuthenticateList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Proxy-Authorization" ) )
		{
			if( ParseSipCredential( m_clsProxyAuthorizationList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Proxy-Authenticate" ) )
		{
			if( ParseSipChallenge( m_clsProxyAuthenticateList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Content-Type" ) || !strcasecmp( pszName, "c" ) )
		{
			if( m_clsContentType.Parse( pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Content-Length" ) || !strcasecmp( pszName, "l" ) )
		{
			m_iContentLength = atoi( pszValue );
		}
#ifdef USE_ACCEPT_HEADER
		else if( !strcasecmp( pszName, "Accept-Encoding" ) )
		{
			if( ParseSipAcceptData( m_clsAcceptEncodingList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Accept-Language" ) )
		{
			if( ParseSipAcceptData( m_clsAcceptLanguageList, pszValue, iValueLen ) == -1 ) return -1;
		}
		else if( !strcasecmp( pszName, "Accept" ) )
		{
			if( ParseSipContentType( m_clsAcceptList, pszValue, iValueLen ) == -1 ) return -1;
		}
#endif
		else if( !strcasecmp( pszName, "Expires" ) )
		{
			m_iExpires = atoi( pszValue );
		}
		else if( !strcasecmp( pszName, "User-Agent" ) )
		{
			m_strUserAgent = clsHeader.m_strValue;
		}
		else
		{
			m_clsHeaderList.push_back( clsHeader );
		}
#endif
	}
Example #5
0
int RtspRequest::ParseRtspPackage(char Buf[], int Size) //bool bReq
{
    int ret = 0;
    StringArray_t saLine;
    StringPair_t spHeader;
    string Delim=CRLF;
    string sRequest(Buf, Size);
    U32 theHeader = 0;
    StringUtil::Trim(sRequest);
    StringUtil::Split(sRequest, Delim, saLine);
    //Request Line
    
    if(strncmp(saLine[0].c_str(), RTSPProtocol::GetVersionString(RTSPProtocol::k10Version).Ptr, RTSPProtocol::GetVersionString(RTSPProtocol::k10Version).Len) != 0) //bReq
    {    
        m_bIsRequest = true;
        ret = ParseRequestLine(saLine[0]);
        if(ret < 0)
        {
            return -1;
        }
    }
    else
    {
        m_bIsRequest = false;
        ret = ParseStatusLine(saLine[0]);
        if(ret < 0)
        {
            return -1;
        }    
    }
    //Parse Header
    for(int i=1; i<saLine.size(); i++)
    {           
        StringUtil::Split(saLine[i], ':', spHeader);
        theHeader = RTSPProtocol::GetRequestHeader(StrPtrLen((char*)spHeader.first.c_str()));
        if(qtssIllegalHeader != theHeader)
        {
            StringUtil::Trim(spHeader.second);
            m_HeaderDict[theHeader] = spHeader.second;
        }

        //some headers require some special processing. If this code begins
        //to get out of control, we made need to come up with a function pointer table
        switch (theHeader)
        {
            case qtssSessionHeader:             ParseSessionHeader(); break;
            case qtssCSeqHeader:                ParseCSeqHeader(); break;
            case qtssTransportHeader:           ParseTransportHeader(); break;
            case qtssRangeHeader:               ParseRangeHeader();     break;
            case qtssContentLengthHeader:       ParseContentLengthHeader();break;
            case qtssSpeedHeader:               ParseSpeedHeader();     break;
            case qtssScaleHeader:               ParseScaleHeader();     break;
            case qtssRequireHeader:             ParseRequireHeader();   break;
            case ngodNoticeHeader:              ParseNoticeHeader();    break;
            case ngodOnDemandSessionIdHeader:   ParseOnDemandSessionIdHeader(); break;
            case ngodReasonHeader:              ParseReasonHeader(); break;
            case ngodSessionGroupHeader:        ParseSessionGroupHeader(); break;
            default:    break;
        }

    }
    return 0;
}