liveMedia/RTSPCommon.cpp

Go to the documentation of this file.
00001 /**********
00002 This library is free software; you can redistribute it and/or modify it under
00003 the terms of the GNU Lesser General Public License as published by the
00004 Free Software Foundation; either version 2.1 of the License, or (at your
00005 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
00006 
00007 This library is distributed in the hope that it will be useful, but WITHOUT
00008 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00009 FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
00010 more details.
00011 
00012 You should have received a copy of the GNU Lesser General Public License
00013 along with this library; if not, write to the Free Software Foundation, Inc.,
00014 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015 **********/
00016 // "liveMedia"
00017 // Copyright (c) 1996-2008 Live Networks, Inc.  All rights reserved.
00018 // Common routines used by both RTSP clients and servers
00019 // Implementation
00020 
00021 #include "RTSPCommon.hh"
00022 #include "Locale.hh"
00023 #include <string.h>
00024 #include <stdio.h>
00025 
00026 Boolean parseRTSPRequestString(char const* reqStr,
00027                                unsigned reqStrSize,
00028                                char* resultCmdName,
00029                                unsigned resultCmdNameMaxSize,
00030                                char* resultURLPreSuffix,
00031                                unsigned resultURLPreSuffixMaxSize,
00032                                char* resultURLSuffix,
00033                                unsigned resultURLSuffixMaxSize,
00034                                char* resultCSeq,
00035                                unsigned resultCSeqMaxSize) {
00036   // This parser is currently rather dumb; it should be made smarter #####
00037 
00038   // Read everything up to the first space as the command name:
00039   Boolean parseSucceeded = False;
00040   unsigned i;
00041   for (i = 0; i < resultCmdNameMaxSize-1 && i < reqStrSize; ++i) {
00042     char c = reqStr[i];
00043     if (c == ' ' || c == '\t') {
00044       parseSucceeded = True;
00045       break;
00046     }
00047 
00048     resultCmdName[i] = c;
00049   }
00050   resultCmdName[i] = '\0';
00051   if (!parseSucceeded) return False;
00052 
00053   // Skip over the prefix of any "rtsp://" or "rtsp:/" URL that follows:
00054   unsigned j = i+1;
00055   while (j < reqStrSize && (reqStr[j] == ' ' || reqStr[j] == '\t')) ++j; // skip over any additional white space
00056   for (; (int)j < (int)(reqStrSize-8); ++j) {
00057     if ((reqStr[j] == 'r' || reqStr[j] == 'R')
00058         && (reqStr[j+1] == 't' || reqStr[j+1] == 'T')
00059         && (reqStr[j+2] == 's' || reqStr[j+2] == 'S')
00060         && (reqStr[j+3] == 'p' || reqStr[j+3] == 'P')
00061         && reqStr[j+4] == ':' && reqStr[j+5] == '/') {
00062       j += 6;
00063       if (reqStr[j] == '/') {
00064         // This is a "rtsp://" URL; skip over the host:port part that follows:
00065         ++j;
00066         while (j < reqStrSize && reqStr[j] != '/' && reqStr[j] != ' ') ++j;
00067       } else {
00068         // This is a "rtsp:/" URL; back up to the "/":
00069         --j;
00070       }
00071       i = j;
00072       break;
00073     }
00074   }
00075 
00076   // Look for the URL suffix (before the following "RTSP/"):
00077   parseSucceeded = False;
00078   for (unsigned k = i+1; (int)k < (int)(reqStrSize-5); ++k) {
00079     if (reqStr[k] == 'R' && reqStr[k+1] == 'T' &&
00080         reqStr[k+2] == 'S' && reqStr[k+3] == 'P' && reqStr[k+4] == '/') {
00081       while (--k >= i && reqStr[k] == ' ') {} // go back over all spaces before "RTSP/"
00082       unsigned k1 = k;
00083       while (k1 > i && reqStr[k1] != '/') --k1;
00084       // the URL suffix comes from [k1+1,k]
00085 
00086       // Copy "resultURLSuffix":
00087       if (k - k1 + 1 > resultURLSuffixMaxSize) return False; // there's no room
00088       unsigned n = 0, k2 = k1+1;
00089       while (k2 <= k) resultURLSuffix[n++] = reqStr[k2++];
00090       resultURLSuffix[n] = '\0';
00091 
00092       // Also look for the URL 'pre-suffix' before this:
00093       unsigned k3 = (k1 == 0) ? 0 : --k1;
00094       while (k3 > i && reqStr[k3] != '/') --k3;
00095       // the URL pre-suffix comes from [k3+1,k1]
00096 
00097       // Copy "resultURLPreSuffix":
00098       if (k1 - k3 + 1 > resultURLPreSuffixMaxSize) return False; // there's no room
00099       n = 0; k2 = k3+1;
00100       while (k2 <= k1) resultURLPreSuffix[n++] = reqStr[k2++];
00101       resultURLPreSuffix[n] = '\0';
00102 
00103       i = k + 7; // to go past " RTSP/"
00104       parseSucceeded = True;
00105       break;
00106     }
00107   }
00108   if (!parseSucceeded) return False;
00109 
00110   // Look for "CSeq:", skip whitespace,
00111   // then read everything up to the next \r or \n as 'CSeq':
00112   parseSucceeded = False;
00113   for (j = i; (int)j < (int)(reqStrSize-5); ++j) {
00114     if (reqStr[j] == 'C' && reqStr[j+1] == 'S' && reqStr[j+2] == 'e' &&
00115         reqStr[j+3] == 'q' && reqStr[j+4] == ':') {
00116       j += 5;
00117       unsigned n;
00118       while (j < reqStrSize && (reqStr[j] ==  ' ' || reqStr[j] == '\t')) ++j;
00119       for (n = 0; n < resultCSeqMaxSize-1 && j < reqStrSize; ++n,++j) {
00120         char c = reqStr[j];
00121         if (c == '\r' || c == '\n') {
00122           parseSucceeded = True;
00123           break;
00124         }
00125 
00126         resultCSeq[n] = c;
00127       }
00128       resultCSeq[n] = '\0';
00129       break;
00130     }
00131   }
00132   if (!parseSucceeded) return False;
00133 
00134   return True;
00135 }
00136 
00137 Boolean parseRangeHeader(char const* buf, double& rangeStart, double& rangeEnd) {
00138   // First, find "Range:"
00139   while (1) {
00140     if (*buf == '\0') return False; // not found
00141     if (_strncasecmp(buf, "Range: ", 7) == 0) break;
00142     ++buf;
00143   }
00144 
00145   // Then, run through each of the fields, looking for ones we handle:
00146   char const* fields = buf + 7;
00147   while (*fields == ' ') ++fields;
00148   double start, end;
00149   Locale("C", LC_NUMERIC);
00150   if (sscanf(fields, "npt = %lf - %lf", &start, &end) == 2) {
00151     rangeStart = start;
00152     rangeEnd = end;
00153   } else if (sscanf(fields, "npt = %lf -", &start) == 1) {
00154     rangeStart = start;
00155     rangeEnd = 0.0;
00156   } else {
00157     return False; // The header is malformed
00158   }
00159 
00160   return True;
00161 }

Generated on Fri Dec 19 21:58:19 2008 for live by  doxygen 1.5.2