File: HttpDateParse.cs

package info (click to toggle)
mono 4.6.2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 778,148 kB
  • ctags: 914,052
  • sloc: cs: 5,779,509; xml: 2,773,713; ansic: 432,645; sh: 14,749; makefile: 12,361; perl: 2,488; python: 1,434; cpp: 849; asm: 531; sql: 95; sed: 16; php: 1
file content (472 lines) | stat: -rw-r--r-- 16,804 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//------------------------------------------------------------
namespace System.ServiceModel.Web
{
    using System;
    using System.Globalization;

    // DO NOT EDIT THIS CODE. 
    //
    // All of the code from this class was taken from build 20717.00 
    // of System.Net.HttpDateParse.  If there is a bug with this code 
    // it should be fixed  in the original System.Net.HttpDateParse 
    // and then ported here. [[....]]

    internal static class HttpDateParse
    {
        private const int BASE_DEC = 10; // base 10

        //
        // Date indicies used to figure out what each entry is.
        //

        private const int DATE_INDEX_DAY_OF_WEEK = 0;

        private const int DATE_1123_INDEX_DAY = 1;
        private const int DATE_1123_INDEX_MONTH = 2;
        private const int DATE_1123_INDEX_YEAR = 3;
        private const int DATE_1123_INDEX_HRS = 4;
        private const int DATE_1123_INDEX_MINS = 5;
        private const int DATE_1123_INDEX_SECS = 6;

        private const int DATE_ANSI_INDEX_MONTH = 1;
        private const int DATE_ANSI_INDEX_DAY = 2;
        private const int DATE_ANSI_INDEX_HRS = 3;
        private const int DATE_ANSI_INDEX_MINS = 4;
        private const int DATE_ANSI_INDEX_SECS = 5;
        private const int DATE_ANSI_INDEX_YEAR = 6;

        private const int DATE_INDEX_TZ = 7;

        private const int DATE_INDEX_LAST = DATE_INDEX_TZ;
        private const int MAX_FIELD_DATE_ENTRIES = (DATE_INDEX_LAST + 1);

        //
        // DATE_TOKEN's DWORD values used to determine what day/month we're on
        //

        private const int DATE_TOKEN_JANUARY = 1;
        private const int DATE_TOKEN_FEBRUARY = 2;
        private const int DATE_TOKEN_MARCH = 3;
        private const int DATE_TOKEN_APRIL = 4;
        private const int DATE_TOKEN_MAY = 5;
        private const int DATE_TOKEN_JUNE = 6;
        private const int DATE_TOKEN_JULY = 7;
        private const int DATE_TOKEN_AUGUST = 8;
        private const int DATE_TOKEN_SEPTEMBER = 9;
        private const int DATE_TOKEN_OCTOBER = 10;
        private const int DATE_TOKEN_NOVEMBER = 11;
        private const int DATE_TOKEN_DECEMBER = 12;

        private const int DATE_TOKEN_LAST_MONTH = (DATE_TOKEN_DECEMBER + 1);

        private const int DATE_TOKEN_SUNDAY = 0;
        private const int DATE_TOKEN_MONDAY = 1;
        private const int DATE_TOKEN_TUESDAY = 2;
        private const int DATE_TOKEN_WEDNESDAY = 3;
        private const int DATE_TOKEN_THURSDAY = 4;
        private const int DATE_TOKEN_FRIDAY = 5;
        private const int DATE_TOKEN_SATURDAY = 6;

        private const int DATE_TOKEN_LAST_DAY = (DATE_TOKEN_SATURDAY + 1);

        private const int DATE_TOKEN_GMT = -1000;

        private const int DATE_TOKEN_LAST = DATE_TOKEN_GMT;

        private const int DATE_TOKEN_ERROR = (DATE_TOKEN_LAST + 1);

        //
        // MakeUpper - takes an assumed lower character and bit manipulates into a upper.
        //              (make sure the character is Lower case alpha char to begin,
        //               otherwise it corrupts)
        //
        static char MakeUpper(char c)
        {
            return (Char.ToUpper(c, CultureInfo.InvariantCulture));
        }

        // Routine Description:
        //
        //    Looks at the first three bytes of string to determine if we're looking
        //        at a Day of the Week, or Month, or "GMT" string.  Is inlined so that
        //        the compiler can optimize this code into the caller FInternalParseHttpDate.
        //
        // Arguments:
        //
        //    lpszDay - a string ptr to the first byte of the string in question.
        //
        // Return Value:
        //
        //    DWORD
        //    Success - The Correct date token, 0-6 for day of the week, 1-14 for month, etc
        //
        //    Failure - DATE_TOKEN_ERROR
        static int MapDayMonthToDword(char[] lpszDay, int index)
        {
            switch (MakeUpper(lpszDay[index]))
            { // make uppercase
                case 'A':
                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'P':
                            return DATE_TOKEN_APRIL;
                        case 'U':
                            return DATE_TOKEN_AUGUST;

                    }
                    return DATE_TOKEN_ERROR;

                case 'D':
                    return DATE_TOKEN_DECEMBER;

                case 'F':
                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'R':
                            return DATE_TOKEN_FRIDAY;
                        case 'E':
                            return DATE_TOKEN_FEBRUARY;
                    }

                    return DATE_TOKEN_ERROR;

                case 'G':
                    return DATE_TOKEN_GMT;

                case 'M':

                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'O':
                            return DATE_TOKEN_MONDAY;
                        case 'A':
                            switch (MakeUpper(lpszDay[index + 2]))
                            {
                                case 'R':
                                    return DATE_TOKEN_MARCH;
                                case 'Y':
                                    return DATE_TOKEN_MAY;
                            }

                            // fall through to error
                            break;
                    }

                    return DATE_TOKEN_ERROR;

                case 'N':
                    return DATE_TOKEN_NOVEMBER;

                case 'J':

                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'A':
                            return DATE_TOKEN_JANUARY;

                        case 'U':
                            switch (MakeUpper(lpszDay[index + 2]))
                            {
                                case 'N':
                                    return DATE_TOKEN_JUNE;
                                case 'L':
                                    return DATE_TOKEN_JULY;
                            }

                            // fall through to error
                            break;
                    }

                    return DATE_TOKEN_ERROR;

                case 'O':
                    return DATE_TOKEN_OCTOBER;

                case 'S':

                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'A':
                            return DATE_TOKEN_SATURDAY;
                        case 'U':
                            return DATE_TOKEN_SUNDAY;
                        case 'E':
                            return DATE_TOKEN_SEPTEMBER;
                    }

                    return DATE_TOKEN_ERROR;


                case 'T':
                    switch (MakeUpper(lpszDay[index + 1]))
                    {
                        case 'U':
                            return DATE_TOKEN_TUESDAY;
                        case 'H':
                            return DATE_TOKEN_THURSDAY;
                    }

                    return DATE_TOKEN_ERROR;

                case 'U':
                    return DATE_TOKEN_GMT;

                case 'W':
                    return DATE_TOKEN_WEDNESDAY;

            }

            return DATE_TOKEN_ERROR;
        }

        
        // Routine Description:
        //
        //    Parses through a ANSI, RFC850, or RFC1123 date format and covents it into
        //     a FILETIME/SYSTEMTIME time format.
        //
        //    Important this a time-critical function and should only be changed
        //     with the intention of optimizing or a critical need work item.
        //
        // Arguments:
        //
        //    lpft - Ptr to FILETIME structure.  Used to store converted result.
        //            Must be NULL if not intended to be used !!!
        //
        //    lpSysTime - Ptr to SYSTEMTIME struture. Used to return Systime if needed.
        //
        //    lpcszDateStr - Const Date string to parse.
        //
        // Return Value:
        //
        //    BOOL
        //    Success - TRUE
        //    Failure - FALSE
        internal static bool ParseHttpDate(String DateString, out DateTime dtOut)
        {
            int index = 0;
            int i = 0, iLastLettered = -1;
            bool fIsANSIDateFormat = false;
            int[] rgdwDateParseResults = new int[MAX_FIELD_DATE_ENTRIES];
            bool fRet = true;
            char[] lpInputBuffer = DateString.ToCharArray();

            dtOut = new DateTime();

            //
            // Date Parsing v2 (1 more to go), and here is how it works...
            //  We take a date string and churn through it once, converting
            //  integers to integers, Month,Day, and GMT strings into integers,
            //  and all is then placed IN order in a temp array.
            //
            // At the completetion of the parse stage, we simple look at
            //  the data, and then map the results into the correct
            //  places in the SYSTIME structure.  Simple, No allocations, and
            //  No dirting the data.
            //
            // The end of the function does something munging and pretting
            //  up of the results to handle the year 2000, and TZ offsets
            //  Note: do we need to fully handle TZs anymore?
            //

            while (index < DateString.Length && i < MAX_FIELD_DATE_ENTRIES)
            {
                if (lpInputBuffer[index] >= '0' && lpInputBuffer[index] <= '9')
                {
                    //
                    // we have a numerical entry, scan through it and convent to DWORD
                    //

                    rgdwDateParseResults[i] = 0;

                    do
                    {
                        rgdwDateParseResults[i] *= BASE_DEC;
                        rgdwDateParseResults[i] += (lpInputBuffer[index] - '0');
                        index++;
                    } while (index < DateString.Length &&
                             lpInputBuffer[index] >= '0' &&
                             lpInputBuffer[index] <= '9');

                    i++; // next token
                }
                else if ((lpInputBuffer[index] >= 'A' && lpInputBuffer[index] <= 'Z') ||
                         (lpInputBuffer[index] >= 'a' && lpInputBuffer[index] <= 'z'))
                {
                    //
                    // we have a string, should be a day, month, or GMT
                    //   lets skim to the end of the string
                    //

                    rgdwDateParseResults[i] =
                    MapDayMonthToDword(lpInputBuffer, index);

                    iLastLettered = i;

                    // We want to ignore the possibility of a time zone such as PST or EST in a non-standard
                    // date format such as "Thu Dec 17 16:01:28 PST 1998" (Notice that the year is _after_ the time zone
                    if ((rgdwDateParseResults[i] == DATE_TOKEN_ERROR)
                        &&
                        !(fIsANSIDateFormat && (i == DATE_ANSI_INDEX_YEAR)))
                    {
                        fRet = false;
                        goto quit;
                    }

                    //
                    // At this point if we have a vaild string
                    //  at this index, we know for sure that we're
                    //  looking at a ANSI type DATE format.
                    //

                    if (i == DATE_ANSI_INDEX_MONTH)
                    {
                        fIsANSIDateFormat = true;
                    }

                    //
                    // Read past the end of the current set of alpha characters,
                    //  as MapDayMonthToDword only peeks at a few characters
                    //

                    do
                    {
                        index++;
                    } while (index < DateString.Length &&
                             ((lpInputBuffer[index] >= 'A' && lpInputBuffer[index] <= 'Z') ||
                               (lpInputBuffer[index] >= 'a' && lpInputBuffer[index] <= 'z')));

                    i++; // next token
                }
                else
                {
                    //
                    // For the generic case its either a space, comma, semi-colon, etc.
                    //  the point is we really don't care, nor do we need to waste time
                    //  worring about it (the orginal code did).   The point is we
                    //  care about the actual date information, So we just advance to the
                    //  next lexume.
                    //

                    index++;
                }
            }

            //
            // We're finished parsing the string, now take the parsed tokens
            //  and turn them to the actual structured information we care about.
            //  So we build lpSysTime from the Array, using a local if none is passed in.
            //

            int year;
            int month;
            int day;
            int hour;
            int minute;
            int second;
            int millisecond;

            millisecond = 0;

            if (fIsANSIDateFormat)
            {
                day = rgdwDateParseResults[DATE_ANSI_INDEX_DAY];
                month = rgdwDateParseResults[DATE_ANSI_INDEX_MONTH];
                hour = rgdwDateParseResults[DATE_ANSI_INDEX_HRS];
                minute = rgdwDateParseResults[DATE_ANSI_INDEX_MINS];
                second = rgdwDateParseResults[DATE_ANSI_INDEX_SECS];
                if (iLastLettered != DATE_ANSI_INDEX_YEAR)
                {
                    year = rgdwDateParseResults[DATE_ANSI_INDEX_YEAR];
                }
                else
                {
                    // This is a fix to get around toString/toGMTstring (where the timezone is
                    // appended at the end. (See above)
                    year = rgdwDateParseResults[DATE_INDEX_TZ];
                }
            }
            else
            {
                day = rgdwDateParseResults[DATE_1123_INDEX_DAY];
                month = rgdwDateParseResults[DATE_1123_INDEX_MONTH];
                year = rgdwDateParseResults[DATE_1123_INDEX_YEAR];
                hour = rgdwDateParseResults[DATE_1123_INDEX_HRS];
                minute = rgdwDateParseResults[DATE_1123_INDEX_MINS];
                second = rgdwDateParseResults[DATE_1123_INDEX_SECS];
            }

            //
            // Normalize the year, 90 == 1990, handle the year 2000, 02 == 2002
            //  This is Year 2000 handling folks!!!  We get this wrong and
            //  we all look bad.
            //

            if (year < 100)
            {
                year += ((year < 80) ? 2000 : 1900);
            }

            //
            // if we got misformed time, then plug in the current time
            // !lpszHrs || !lpszMins || !lpszSec
            //

            if ((i < 4)
                || (day > 31)
                || (hour > 23)
                || (minute > 59)
                || (second > 59))
            {
                fRet = false;
                goto quit;
            }

            //
            // Now do the DateTime conversion
            //

            dtOut = new DateTime(year, month, day, hour, minute, second, millisecond);

            //
            // we want the system time to be accurate. This is _suhlow_
            // The time passed in is in the local time zone; we have to convert this into GMT.
            //

            if (iLastLettered == DATE_ANSI_INDEX_YEAR)
            {
                // this should be an unusual case.
                dtOut = dtOut.ToUniversalTime();
            }

            //
            // If we have an Offset to another Time Zone
            //   then convert to appropriate GMT time
            //

            if ((i > DATE_INDEX_TZ &&
                 rgdwDateParseResults[DATE_INDEX_TZ] != DATE_TOKEN_GMT))
            {

                //
                // if we received +/-nnnn as offset (hhmm), modify the output FILETIME
                //

                double offset;

                offset = (double)rgdwDateParseResults[DATE_INDEX_TZ];
                dtOut.AddHours(offset);
            }

            // In the end, we leave it all in LocalTime

            dtOut = dtOut.ToLocalTime();

        quit:

            return fRet;
        }
    }
}