nsresult nsHttpResponseHead::Parse(char *block) { LOG(("nsHttpResponseHead::Parse [this=%p]\n", this)); // this command works on a buffer as prepared by Flatten, as such it is // not very forgiving ;-) char *p = PL_strstr(block, "\r\n"); if (!p) return NS_ERROR_UNEXPECTED; *p = 0; ParseStatusLine(block); do { block = p + 2; if (*block == 0) break; p = PL_strstr(block, "\r\n"); if (!p) return NS_ERROR_UNEXPECTED; *p = 0; ParseHeaderLine(block); } while (1); return NS_OK; }
TEST(StatusLineTestGroup, StatusLineParseTest) { struct StatusLine *s = CreateStatusLine(0, (char *)""); char string[] = "SIP/2.0 180 Ringing"; ParseStatusLine(string, s); STRCMP_EQUAL("SIP/2.0", StatusLineGetSipVersion(s)); CHECK_EQUAL(180, StatusLineGetStatusCode(s)); STRCMP_EQUAL("Ringing", StatusLineGetReasonPhrase(s)); DestroyStatusLine(s); }
TEST(StatusLineTestGroup, StatusLine2StringTest) { struct StatusLine *s = CreateStatusLine(0, (char *)""); char string[] = "SIP/2.0 180 Ringing"; char result[64] = {0}; ParseStatusLine(string, s); StatusLine2String(result, s); STRCMP_EQUAL(string, result); DestroyStatusLine(s); }
static bool ParseStatus(WifiStatus &status, char *src) { status.Clear(); while (true) { char *eol = strchr(src, '\n'); if (eol != nullptr) *eol = 0; if (!ParseStatusLine(status, src)) break; if (eol == nullptr) break; src = eol + 1; } return true; }
/** * @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 }
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; }