00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "RTSPCommon.hh"
00022 #include "Locale.hh"
00023 #include <string.h>
00024 #include <stdio.h>
00025 #include <time.h>
00026
00027 Boolean parseRTSPRequestString(char const* reqStr,
00028 unsigned reqStrSize,
00029 char* resultCmdName,
00030 unsigned resultCmdNameMaxSize,
00031 char* resultURLPreSuffix,
00032 unsigned resultURLPreSuffixMaxSize,
00033 char* resultURLSuffix,
00034 unsigned resultURLSuffixMaxSize,
00035 char* resultCSeq,
00036 unsigned resultCSeqMaxSize,
00037 unsigned& contentLength) {
00038
00039 contentLength = 0;
00040
00041
00042 Boolean parseSucceeded = False;
00043 unsigned i;
00044 for (i = 0; i < resultCmdNameMaxSize-1 && i < reqStrSize; ++i) {
00045 char c = reqStr[i];
00046 if (c == ' ' || c == '\t') {
00047 parseSucceeded = True;
00048 break;
00049 }
00050
00051 resultCmdName[i] = c;
00052 }
00053 resultCmdName[i] = '\0';
00054 if (!parseSucceeded) return False;
00055
00056
00057 unsigned j = i+1;
00058 while (j < reqStrSize && (reqStr[j] == ' ' || reqStr[j] == '\t')) ++j;
00059 for (; (int)j < (int)(reqStrSize-8); ++j) {
00060 if ((reqStr[j] == 'r' || reqStr[j] == 'R')
00061 && (reqStr[j+1] == 't' || reqStr[j+1] == 'T')
00062 && (reqStr[j+2] == 's' || reqStr[j+2] == 'S')
00063 && (reqStr[j+3] == 'p' || reqStr[j+3] == 'P')
00064 && reqStr[j+4] == ':' && reqStr[j+5] == '/') {
00065 j += 6;
00066 if (reqStr[j] == '/') {
00067
00068 ++j;
00069 while (j < reqStrSize && reqStr[j] != '/' && reqStr[j] != ' ') ++j;
00070 } else {
00071
00072 --j;
00073 }
00074 i = j;
00075 break;
00076 }
00077 }
00078
00079
00080 parseSucceeded = False;
00081 for (unsigned k = i+1; (int)k < (int)(reqStrSize-5); ++k) {
00082 if (reqStr[k] == 'R' && reqStr[k+1] == 'T' &&
00083 reqStr[k+2] == 'S' && reqStr[k+3] == 'P' && reqStr[k+4] == '/') {
00084 while (--k >= i && reqStr[k] == ' ') {}
00085 unsigned k1 = k;
00086 while (k1 > i && reqStr[k1] != '/') --k1;
00087
00088
00089
00090 if (k - k1 + 1 > resultURLSuffixMaxSize) return False;
00091 unsigned n = 0, k2 = k1+1;
00092 while (k2 <= k) resultURLSuffix[n++] = reqStr[k2++];
00093 resultURLSuffix[n] = '\0';
00094
00095
00096
00097 if (k1 - i > resultURLPreSuffixMaxSize) return False;
00098 n = 0; k2 = i + 1;
00099 while (k2 <= k1 - 1) resultURLPreSuffix[n++] = reqStr[k2++];
00100 resultURLPreSuffix[n] = '\0';
00101
00102 i = k + 7;
00103 parseSucceeded = True;
00104 break;
00105 }
00106 }
00107 if (!parseSucceeded) return False;
00108
00109
00110
00111 parseSucceeded = False;
00112 for (j = i; (int)j < (int)(reqStrSize-5); ++j) {
00113 if (reqStr[j] == 'C' && reqStr[j+1] == 'S' && reqStr[j+2] == 'e' &&
00114 reqStr[j+3] == 'q' && reqStr[j+4] == ':') {
00115 j += 5;
00116 while (j < reqStrSize && (reqStr[j] == ' ' || reqStr[j] == '\t')) ++j;
00117 unsigned n;
00118 for (n = 0; n < resultCSeqMaxSize-1 && j < reqStrSize; ++n,++j) {
00119 char c = reqStr[j];
00120 if (c == '\r' || c == '\n') {
00121 parseSucceeded = True;
00122 break;
00123 }
00124
00125 resultCSeq[n] = c;
00126 }
00127 resultCSeq[n] = '\0';
00128 break;
00129 }
00130 }
00131 if (!parseSucceeded) return False;
00132
00133
00134 for (j = i; (int)j < (int)(reqStrSize-15); ++j) {
00135 if (reqStr[j] == 'C' && reqStr[j+1] == 'o' && reqStr[j+2] == 'n' && reqStr[j+3] == 't' && reqStr[j+4] == 'e' &&
00136 reqStr[j+5] == 'n' && reqStr[j+6] == 't' && reqStr[j+7] == '-' && (reqStr[j+8] == 'L' || reqStr[j+8] == 'l') &&
00137 reqStr[j+9] == 'e' && reqStr[j+10] == 'n' && reqStr[j+11] == 'g' && reqStr[j+12] == 't' && reqStr[j+13] == 'h' &&
00138 reqStr[j+14] == ':') {
00139 j += 15;
00140 while (j < reqStrSize && (reqStr[j] == ' ' || reqStr[j] == '\t')) ++j;
00141 unsigned num;
00142 if (sscanf(&reqStr[j], "%u", &num) == 1) {
00143 contentLength = num;
00144 }
00145 }
00146 }
00147 return True;
00148 }
00149
00150 Boolean parseRangeParam(char const* paramStr, double& rangeStart, double& rangeEnd) {
00151 double start, end;
00152 int numCharsMatched = 0;
00153 Locale l("C", Numeric);
00154 if (sscanf(paramStr, "npt = %lf - %lf", &start, &end) == 2) {
00155 rangeStart = start;
00156 rangeEnd = end;
00157 } else if (sscanf(paramStr, "npt = %lf -", &start) == 1) {
00158 rangeStart = start;
00159 rangeEnd = 0.0;
00160 } else if (strcmp(paramStr, "npt=now-") == 0) {
00161 rangeStart = 0.0;
00162 rangeEnd = 0.0;
00163 } else if (sscanf(paramStr, "clock = %*s%n", &numCharsMatched) == 0 && numCharsMatched > 0) {
00164
00165 } else if (sscanf(paramStr, "smtpe = %*s%n", &numCharsMatched) == 0 && numCharsMatched > 0) {
00166
00167 } else {
00168 return False;
00169 }
00170
00171 return True;
00172 }
00173
00174 Boolean parseRangeHeader(char const* buf, double& rangeStart, double& rangeEnd) {
00175
00176 while (1) {
00177 if (*buf == '\0') return False;
00178 if (_strncasecmp(buf, "Range: ", 7) == 0) break;
00179 ++buf;
00180 }
00181
00182
00183 char const* fields = buf + 7;
00184 while (*fields == ' ') ++fields;
00185 return parseRangeParam(fields, rangeStart, rangeEnd);
00186 }
00187
00188 char const* dateHeader() {
00189 static char buf[200];
00190 #if !defined(_WIN32_WCE)
00191 time_t tt = time(NULL);
00192 strftime(buf, sizeof buf, "Date: %a, %b %d %Y %H:%M:%S GMT\r\n", gmtime(&tt));
00193 #else
00194
00195
00196
00197
00198 SYSTEMTIME SystemTime;
00199 GetSystemTime(&SystemTime);
00200 WCHAR dateFormat[] = L"ddd, MMM dd yyyy";
00201 WCHAR timeFormat[] = L"HH:mm:ss GMT\r\n";
00202 WCHAR inBuf[200];
00203 DWORD locale = LOCALE_NEUTRAL;
00204
00205 int ret = GetDateFormat(locale, 0, &SystemTime,
00206 (LPTSTR)dateFormat, (LPTSTR)inBuf, sizeof inBuf);
00207 inBuf[ret - 1] = ' ';
00208 ret = GetTimeFormat(locale, 0, &SystemTime,
00209 (LPTSTR)timeFormat,
00210 (LPTSTR)inBuf + ret, (sizeof inBuf) - ret);
00211 wcstombs(buf, inBuf, wcslen(inBuf));
00212 #endif
00213 return buf;
00214 }