parseurl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2009 Andrej Stepanchuk
  3. * Copyright (C) 2009-2010 Howard Chu
  4. *
  5. * This file is part of librtmp.
  6. *
  7. * librtmp is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1,
  10. * or (at your option) any later version.
  11. *
  12. * librtmp is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with librtmp see the file COPYING. If not, write to
  19. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/lgpl.html
  22. */
  23. #include "rtmp_sys.h"
  24. #include "log.h"
  25. int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port,
  26. AVal *app)
  27. {
  28. char *p, *end, *col, /* *ques, */ *slash;
  29. RTMP_Log(RTMP_LOGDEBUG, "Parsing...");
  30. *protocol = RTMP_PROTOCOL_RTMP;
  31. *port = 0;
  32. app->av_len = 0;
  33. app->av_val = NULL;
  34. /* Old School Parsing */
  35. /* look for usual :// pattern */
  36. p = strstr(url, "://");
  37. if(!p)
  38. {
  39. RTMP_Log(RTMP_LOGERROR, "RTMP URL: No :// in url!");
  40. return FALSE;
  41. }
  42. {
  43. int len = (int)(p-url);
  44. if(len == 4 && strncasecmp(url, "rtmp", 4)==0)
  45. *protocol = RTMP_PROTOCOL_RTMP;
  46. else if(len == 5 && strncasecmp(url, "rtmpt", 5)==0)
  47. *protocol = RTMP_PROTOCOL_RTMPT;
  48. else if(len == 5 && strncasecmp(url, "rtmps", 5)==0)
  49. *protocol = RTMP_PROTOCOL_RTMPS;
  50. else if(len == 5 && strncasecmp(url, "rtmpe", 5)==0)
  51. *protocol = RTMP_PROTOCOL_RTMPE;
  52. else if(len == 5 && strncasecmp(url, "rtmfp", 5)==0)
  53. *protocol = RTMP_PROTOCOL_RTMFP;
  54. else if(len == 6 && strncasecmp(url, "rtmpte", 6)==0)
  55. *protocol = RTMP_PROTOCOL_RTMPTE;
  56. else if(len == 6 && strncasecmp(url, "rtmpts", 6)==0)
  57. *protocol = RTMP_PROTOCOL_RTMPTS;
  58. else
  59. {
  60. RTMP_Log(RTMP_LOGWARNING, "Unknown protocol!\n");
  61. goto parsehost;
  62. }
  63. }
  64. RTMP_Log(RTMP_LOGDEBUG, "Parsed protocol: %d", *protocol);
  65. parsehost:
  66. /* let's get the hostname */
  67. p+=3;
  68. /* check for sudden death */
  69. if(*p==0)
  70. {
  71. RTMP_Log(RTMP_LOGWARNING, "No hostname in URL!");
  72. return FALSE;
  73. }
  74. end = p + strlen(p);
  75. col = strchr(p, ':');
  76. // ques = strchr(p, '?');
  77. slash = strchr(p, '/');
  78. {
  79. int hostlen;
  80. if(slash)
  81. hostlen = slash - p;
  82. else
  83. hostlen = end - p;
  84. if(col && col -p < hostlen)
  85. hostlen = col - p;
  86. if(hostlen < 256)
  87. {
  88. host->av_val = p;
  89. host->av_len = hostlen;
  90. RTMP_Log(RTMP_LOGDEBUG, "Parsed host : %.*s", hostlen, host->av_val);
  91. }
  92. else
  93. {
  94. RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!");
  95. }
  96. p+=hostlen;
  97. }
  98. /* get the port number if available */
  99. if(*p == ':')
  100. {
  101. unsigned int p2;
  102. p++;
  103. p2 = atoi(p);
  104. if(p2 > 65535)
  105. {
  106. RTMP_Log(RTMP_LOGWARNING, "Invalid port number!");
  107. }
  108. else
  109. {
  110. *port = p2;
  111. }
  112. }
  113. if(!slash)
  114. {
  115. RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!");
  116. return TRUE;
  117. }
  118. p = slash+1;
  119. //just.. whatever.
  120. app->av_val = p;
  121. app->av_len = (int)strlen(p);
  122. if(app->av_len && p[app->av_len-1] == '/')
  123. app->av_len--;
  124. RTMP_Log(RTMP_LOGDEBUG, "Parsed app : %.*s", app->av_len, p);
  125. p += app->av_len;
  126. if (*p == '/')
  127. p++;
  128. return TRUE;
  129. }
  130. /*
  131. * Extracts playpath from RTMP URL. playpath is the file part of the
  132. * URL, i.e. the part that comes after rtmp://host:port/app/
  133. *
  134. * Returns the stream name in a format understood by FMS. The name is
  135. * the playpath part of the URL with formatting depending on the stream
  136. * type:
  137. *
  138. * mp4 streams: prepend "mp4:", remove extension
  139. * mp3 streams: prepend "mp3:", remove extension
  140. * flv streams: remove extension
  141. */
  142. void RTMP_ParsePlaypath(AVal *in, AVal *out)
  143. {
  144. int addMP4 = 0;
  145. int addMP3 = 0;
  146. int subExt = 0;
  147. const char *playpath = in->av_val;
  148. const char *temp, *q, *ext = NULL;
  149. const char *ppstart = playpath;
  150. char *streamname, *destptr, *p;
  151. int pplen = in->av_len;
  152. out->av_val = NULL;
  153. out->av_len = 0;
  154. if ((*ppstart == '?') &&
  155. (temp=strstr(ppstart, "slist=")) != 0)
  156. {
  157. ppstart = temp+6;
  158. pplen = (int)strlen(ppstart);
  159. temp = strchr(ppstart, '&');
  160. if (temp)
  161. {
  162. pplen = temp-ppstart;
  163. }
  164. }
  165. q = strchr(ppstart, '?');
  166. if (pplen >= 4)
  167. {
  168. if (q)
  169. ext = q-4;
  170. else
  171. ext = &ppstart[pplen-4];
  172. if ((strncmp(ext, ".f4v", 4) == 0) ||
  173. (strncmp(ext, ".mp4", 4) == 0))
  174. {
  175. addMP4 = 1;
  176. subExt = 1;
  177. /* Only remove .flv from rtmp URL, not slist params */
  178. }
  179. else if ((ppstart == playpath) &&
  180. (strncmp(ext, ".flv", 4) == 0))
  181. {
  182. subExt = 1;
  183. }
  184. else if (strncmp(ext, ".mp3", 4) == 0)
  185. {
  186. addMP3 = 1;
  187. subExt = 1;
  188. }
  189. }
  190. streamname = (char *)malloc((pplen+4+1)*sizeof(char));
  191. if (!streamname)
  192. return;
  193. destptr = streamname;
  194. if (addMP4)
  195. {
  196. if (strncmp(ppstart, "mp4:", 4))
  197. {
  198. strcpy(destptr, "mp4:");
  199. destptr += 4;
  200. }
  201. else
  202. {
  203. subExt = 0;
  204. }
  205. }
  206. else if (addMP3)
  207. {
  208. if (strncmp(ppstart, "mp3:", 4))
  209. {
  210. strcpy(destptr, "mp3:");
  211. destptr += 4;
  212. }
  213. else
  214. {
  215. subExt = 0;
  216. }
  217. }
  218. for (p=(char *)ppstart; pplen >0;)
  219. {
  220. /* skip extension */
  221. if (subExt && p == ext)
  222. {
  223. p += 4;
  224. pplen -= 4;
  225. continue;
  226. }
  227. if (*p == '%')
  228. {
  229. unsigned int c;
  230. sscanf(p+1, "%02x", &c);
  231. *destptr++ = c;
  232. pplen -= 3;
  233. p += 3;
  234. }
  235. else
  236. {
  237. *destptr++ = *p++;
  238. pplen--;
  239. }
  240. }
  241. *destptr = '\0';
  242. out->av_val = streamname;
  243. out->av_len = destptr - streamname;
  244. }