parseurl.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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, *v6;
  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. v6 = strchr(p, ']');
  76. // ques = strchr(p, '?');
  77. slash = strchr(p, '/');
  78. col = strchr((v6 && v6 < slash) ? v6 : p, ':');
  79. {
  80. int hostlen;
  81. if(slash)
  82. hostlen = slash - p;
  83. else
  84. hostlen = end - p;
  85. if(col && col -p < hostlen)
  86. hostlen = col - p;
  87. if(hostlen < 256)
  88. {
  89. host->av_val = p;
  90. host->av_len = hostlen;
  91. RTMP_Log(RTMP_LOGDEBUG, "Parsed host : %.*s", hostlen, host->av_val);
  92. }
  93. else
  94. {
  95. RTMP_Log(RTMP_LOGWARNING, "Hostname exceeds 255 characters!");
  96. }
  97. p+=hostlen;
  98. }
  99. /* get the port number if available */
  100. if(*p == ':')
  101. {
  102. unsigned int p2;
  103. p++;
  104. p2 = atoi(p);
  105. if(p2 > 65535)
  106. {
  107. RTMP_Log(RTMP_LOGWARNING, "Invalid port number!");
  108. }
  109. else
  110. {
  111. *port = p2;
  112. }
  113. }
  114. if(!slash)
  115. {
  116. RTMP_Log(RTMP_LOGWARNING, "No application or playpath in URL!");
  117. return TRUE;
  118. }
  119. p = slash+1;
  120. //just.. whatever.
  121. app->av_val = p;
  122. app->av_len = (int)strlen(p);
  123. if(app->av_len && p[app->av_len-1] == '/')
  124. app->av_len--;
  125. RTMP_Log(RTMP_LOGDEBUG, "Parsed app : %.*s", app->av_len, p);
  126. p += app->av_len;
  127. if (*p == '/')
  128. p++;
  129. return TRUE;
  130. }
  131. /*
  132. * Extracts playpath from RTMP URL. playpath is the file part of the
  133. * URL, i.e. the part that comes after rtmp://host:port/app/
  134. *
  135. * Returns the stream name in a format understood by FMS. The name is
  136. * the playpath part of the URL with formatting depending on the stream
  137. * type:
  138. *
  139. * mp4 streams: prepend "mp4:", remove extension
  140. * mp3 streams: prepend "mp3:", remove extension
  141. * flv streams: remove extension
  142. */
  143. void RTMP_ParsePlaypath(AVal *in, AVal *out)
  144. {
  145. int addMP4 = 0;
  146. int addMP3 = 0;
  147. int subExt = 0;
  148. const char *playpath = in->av_val;
  149. const char *temp, *q, *ext = NULL;
  150. const char *ppstart = playpath;
  151. char *streamname, *destptr, *p;
  152. int pplen = in->av_len;
  153. out->av_val = NULL;
  154. out->av_len = 0;
  155. if (!playpath)
  156. return;
  157. if ((*ppstart == '?') &&
  158. (temp=strstr(ppstart, "slist=")) != 0)
  159. {
  160. ppstart = temp+6;
  161. pplen = (int)strlen(ppstart);
  162. temp = strchr(ppstart, '&');
  163. if (temp)
  164. {
  165. pplen = temp-ppstart;
  166. }
  167. }
  168. q = strchr(ppstart, '?');
  169. if (pplen >= 4)
  170. {
  171. if (q)
  172. ext = q-4;
  173. else
  174. ext = &ppstart[pplen-4];
  175. if ((strncmp(ext, ".f4v", 4) == 0) ||
  176. (strncmp(ext, ".mp4", 4) == 0))
  177. {
  178. addMP4 = 1;
  179. subExt = 1;
  180. /* Only remove .flv from rtmp URL, not slist params */
  181. }
  182. else if ((ppstart == playpath) &&
  183. (strncmp(ext, ".flv", 4) == 0))
  184. {
  185. subExt = 1;
  186. }
  187. else if (strncmp(ext, ".mp3", 4) == 0)
  188. {
  189. addMP3 = 1;
  190. subExt = 1;
  191. }
  192. }
  193. streamname = (char *)malloc((pplen+4+1)*sizeof(char));
  194. if (!streamname)
  195. return;
  196. destptr = streamname;
  197. if (addMP4)
  198. {
  199. if (strncmp(ppstart, "mp4:", 4))
  200. {
  201. strcpy(destptr, "mp4:");
  202. destptr += 4;
  203. }
  204. else
  205. {
  206. subExt = 0;
  207. }
  208. }
  209. else if (addMP3)
  210. {
  211. if (strncmp(ppstart, "mp3:", 4))
  212. {
  213. strcpy(destptr, "mp3:");
  214. destptr += 4;
  215. }
  216. else
  217. {
  218. subExt = 0;
  219. }
  220. }
  221. for (p=(char *)ppstart; pplen >0;)
  222. {
  223. /* skip extension */
  224. if (subExt && p == ext)
  225. {
  226. p += 4;
  227. pplen -= 4;
  228. continue;
  229. }
  230. if (*p == '%')
  231. {
  232. unsigned int c;
  233. sscanf(p+1, "%02x", &c);
  234. *destptr++ = c;
  235. pplen -= 3;
  236. p += 3;
  237. }
  238. else
  239. {
  240. *destptr++ = *p++;
  241. pplen--;
  242. }
  243. }
  244. *destptr = '\0';
  245. out->av_val = streamname;
  246. out->av_len = destptr - streamname;
  247. }