RTSP与UDP URL解析

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);
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值