parseurl.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 ((*ppstart == '?') &&
  156. (temp=strstr(ppstart, "slist=")) != 0)
  157. {
  158. ppstart = temp+6;
  159. pplen = (int)strlen(ppstart);
  160. temp = strchr(ppstart, '&');
  161. if (temp)
  162. {
  163. pplen = temp-ppstart;
  164. }
  165. }
  166. q = strchr(ppstart, '?');
  167. if (pplen >= 4)
  168. {
  169. if (q)
  170. ext = q-4;
  171. else
  172. ext = &ppstart[pplen-4];
  173. if ((strncmp(ext, ".f4v", 4) == 0) ||
  174. (strncmp(ext, ".mp4", 4) == 0))
  175. {
  176. addMP4 = 1;
  177. subExt = 1;
  178. /* Only remove .flv from rtmp URL, not slist params */
  179. }
  180. else if ((ppstart == playpath) &&
  181. (strncmp(ext, ".flv", 4) == 0))
  182. {
  183. subExt = 1;
  184. }
  185. else if (strncmp(ext, ".mp3", 4) == 0)
  186. {
  187. addMP3 = 1;
  188. subExt = 1;
  189. }
  190. }
  191. streamname = (char *)malloc((pplen+4+1)*sizeof(char));
  192. if (!streamname)
  193. return;
  194. destptr = streamname;
  195. if (addMP4)
  196. {
  197. if (strncmp(ppstart, "mp4:", 4))
  198. {
  199. strcpy(destptr, "mp4:");
  200. destptr += 4;
  201. }
  202. else
  203. {
  204. subExt = 0;
  205. }
  206. }
  207. else if (addMP3)
  208. {
  209. if (strncmp(ppstart, "mp3:", 4))
  210. {
  211. strcpy(destptr, "mp3:");
  212. destptr += 4;
  213. }
  214. else
  215. {
  216. subExt = 0;
  217. }
  218. }
  219. for (p=(char *)ppstart; pplen >0;)
  220. {
  221. /* skip extension */
  222. if (subExt && p == ext)
  223. {
  224. p += 4;
  225. pplen -= 4;
  226. continue;
  227. }
  228. if (*p == '%')
  229. {
  230. unsigned int c;
  231. sscanf(p+1, "%02x", &c);
  232. *destptr++ = c;
  233. pplen -= 3;
  234. p += 3;
  235. }
  236. else
  237. {
  238. *destptr++ = *p++;
  239. pplen--;
  240. }
  241. }
  242. *destptr = '\0';
  243. out->av_val = streamname;
  244. out->av_len = destptr - streamname;
  245. }