UDP URL解析
//参数1:strURL UDP的URL
//参数1:strIP 解析出的UDP的IP
//参数1:nPort 解析出的UDP的端口
void ParseUdpUrl(string strURL, string &strIP, int &nPort)
{
// 小写转大写
transform(strURL.begin(), strURL.end(), strURL.begin(),::toupper);
if (strURL.find("UDP://@") != string::npos)
{
string strIpAndPort = strURL.substr(strlen("UDP://@"));
int nPos = strIpAndPort.find(":");
strIP = strIpAndPort.substr(0, nPos);
nPort = atoi(strIpAndPort.substr(nPos + 1).c_str());
}
else if(strURL.find("UDP://") != string::npos)
{
string strIpAndPort = strURL.substr(strlen("UDP://"));
int nPos = strIpAndPort.find(":");
strIP = strIpAndPort.substr(0, nPos);
nPort = atoi(strIpAndPort.substr(nPos + 1).c_str());
}
else
{
printf("Illegal URL! \n");
}
}
rtsp URL解析
//此段代码借鉴与live555中解析
//参数1:url rtsp URL
//参数1:username 从URL解析出的用户名,不包含为NULL
//参数1:password 从URL解析出的密码 ,不包含为NULL
//参数1:UnauthorizedPath 从URL解析出的不包含用户名密码的URL
static void parsingRTSPURL(const char * url, char* &username, char* &password, char* &UnauthorizedPath)
{
//解析rtsp连接的用户名与密码
do
{
// Parse the URL as "rtsp://[<username>[:<password>]@]<server-address-or-name>[:<port>][/<stream-name>]"
char const* prefix = "rtsp://";
unsigned const prefixLength = 7;
// if (_strncasecmp(rtspURL, prefix, prefixLength) != 0)
// {
// printf("URL is not of the form %s\n", prefix);
// break;
// }
unsigned const parseBufferSize = 100;
char parseBuffer[parseBufferSize];
char const* from = &url[prefixLength];
// Check whether "<username>[:<password>]@" occurs next.
// We do this by checking whether '@' appears before the end of the URL, or before the first '/'.
username = password = UnauthorizedPath = NULL; // default return values
char const* colonPasswordStart = NULL;
char const* p;
for (p = from; *p != '\0'; ++p)
{
if (*p == ':' && colonPasswordStart == NULL)
{
colonPasswordStart = p;
}
else if (*p == '@')
{
// We found <username> (and perhaps <password>). Copy them into newly-allocated result strings:
if (colonPasswordStart == NULL) colonPasswordStart = p;
char const* usernameStart = from;
unsigned usernameLen = colonPasswordStart - usernameStart;
username = new char[usernameLen + 1]; // allow for the trailing '\0'
int i = 0;
while (usernameLen > 0)
{
int nBefore = 0;
int nAfter = 0;
if (*usernameStart == '%' && usernameLen >= 3 && sscanf(usernameStart + 1, "%n%2hhx%n", &nBefore, username, &nAfter) == 1)
{
unsigned codeSize = nAfter - nBefore; // should be 1 or 2
++username;
usernameStart += (1 + codeSize);
usernameLen -= (1 + codeSize);
}
else
{
username[i] = *usernameStart++;
i++;
--usernameLen;
}
}
username[i] = '\0';
char const* passwordStart = colonPasswordStart;
if (passwordStart < p) ++passwordStart; // skip over the ':'
unsigned passwordLen = p - passwordStart;
password = new char[passwordLen + 1]; // allow for the trailing '\0'
i = 0;
while (passwordLen > 0)
{
int nBefore = 0;
int nAfter = 0;
if (*passwordStart == '%' && passwordLen >= 3 && sscanf(passwordStart + 1, "%n%2hhx%n", &nBefore, password, &nAfter) == 1)
{
unsigned codeSize = nAfter - nBefore; // should be 1 or 2
++password;
passwordStart += (1 + codeSize);
passwordLen -= (1 + codeSize);
}
else
{
password[i] = *passwordStart++;
i++;
--passwordLen;
}
}
password[i] = '\0';
from = p + 1; // skip over the '@'
UnauthorizedPath = new char[128];
for (i = 0; i < prefixLength; i++)
{
UnauthorizedPath[i] = prefix[i];
}
for (i = 0; i < strlen(from); i++)
{
UnauthorizedPath[7 + i] = from[i];
}
UnauthorizedPath[7 + i] = '\0';
}
}
} while (0);
}