Statistics
| Branch: | Tag: | Revision:

root / pithos / api / compat.py @ a0177bc5

History | View | Annotate | Download (3.5 kB)

1 16526868 Antony Chazapis
# Copyright (c) Django Software Foundation and individual contributors.
2 16526868 Antony Chazapis
# All rights reserved.
3 16526868 Antony Chazapis
# 
4 16526868 Antony Chazapis
# Redistribution and use in source and binary forms, with or without modification,
5 16526868 Antony Chazapis
# are permitted provided that the following conditions are met:
6 16526868 Antony Chazapis
# 
7 16526868 Antony Chazapis
#     1. Redistributions of source code must retain the above copyright notice, 
8 16526868 Antony Chazapis
#        this list of conditions and the following disclaimer.
9 16526868 Antony Chazapis
#     
10 16526868 Antony Chazapis
#     2. Redistributions in binary form must reproduce the above copyright 
11 16526868 Antony Chazapis
#        notice, this list of conditions and the following disclaimer in the
12 16526868 Antony Chazapis
#        documentation and/or other materials provided with the distribution.
13 16526868 Antony Chazapis
# 
14 16526868 Antony Chazapis
#     3. Neither the name of Django nor the names of its contributors may be used
15 16526868 Antony Chazapis
#        to endorse or promote products derived from this software without
16 16526868 Antony Chazapis
#        specific prior written permission.
17 16526868 Antony Chazapis
# 
18 16526868 Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 16526868 Antony Chazapis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 16526868 Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 16526868 Antony Chazapis
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22 16526868 Antony Chazapis
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 16526868 Antony Chazapis
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 16526868 Antony Chazapis
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 16526868 Antony Chazapis
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 16526868 Antony Chazapis
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 16526868 Antony Chazapis
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 16526868 Antony Chazapis
29 16526868 Antony Chazapis
import re
30 16526868 Antony Chazapis
import datetime
31 16526868 Antony Chazapis
import calendar
32 16526868 Antony Chazapis
33 22dab079 Antony Chazapis
MONTHS = 'jan feb mar apr may jun jul aug sep oct nov dec'.split()
34 16526868 Antony Chazapis
__D = r'(?P<day>\d{2})'
35 16526868 Antony Chazapis
__D2 = r'(?P<day>[ \d]\d)'
36 16526868 Antony Chazapis
__M = r'(?P<mon>\w{3})'
37 16526868 Antony Chazapis
__Y = r'(?P<year>\d{4})'
38 16526868 Antony Chazapis
__Y2 = r'(?P<year>\d{2})'
39 16526868 Antony Chazapis
__T = r'(?P<hour>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})'
40 16526868 Antony Chazapis
RFC1123_DATE = re.compile(r'^\w{3}, %s %s %s %s GMT$' % (__D, __M, __Y, __T))
41 16526868 Antony Chazapis
RFC850_DATE = re.compile(r'^\w{6,9}, %s-%s-%s %s GMT$' % (__D, __M, __Y2, __T))
42 16526868 Antony Chazapis
ASCTIME_DATE = re.compile(r'^\w{3} %s %s %s %s$' % (__M, __D2, __T, __Y))
43 16526868 Antony Chazapis
44 16526868 Antony Chazapis
def parse_http_date(date):
45 16526868 Antony Chazapis
    """
46 16526868 Antony Chazapis
    Parses a date format as specified by HTTP RFC2616 section 3.3.1.
47 16526868 Antony Chazapis

48 16526868 Antony Chazapis
    The three formats allowed by the RFC are accepted, even if only the first
49 16526868 Antony Chazapis
    one is still in widespread use.
50 16526868 Antony Chazapis

51 16526868 Antony Chazapis
    Returns an floating point number expressed in seconds since the epoch, in
52 16526868 Antony Chazapis
    UTC.
53 16526868 Antony Chazapis
    """
54 16526868 Antony Chazapis
    # emails.Util.parsedate does the job for RFC1123 dates; unfortunately
55 16526868 Antony Chazapis
    # RFC2616 makes it mandatory to support RFC850 dates too. So we roll
56 16526868 Antony Chazapis
    # our own RFC-compliant parsing.
57 16526868 Antony Chazapis
    for regex in RFC1123_DATE, RFC850_DATE, ASCTIME_DATE:
58 16526868 Antony Chazapis
        m = regex.match(date)
59 16526868 Antony Chazapis
        if m is not None:
60 16526868 Antony Chazapis
            break
61 16526868 Antony Chazapis
    else:
62 16526868 Antony Chazapis
        raise ValueError("%r is not in a valid HTTP date format" % date)
63 16526868 Antony Chazapis
    try:
64 16526868 Antony Chazapis
        year = int(m.group('year'))
65 16526868 Antony Chazapis
        if year < 100:
66 16526868 Antony Chazapis
            if year < 70:
67 16526868 Antony Chazapis
                year += 2000
68 16526868 Antony Chazapis
            else:
69 16526868 Antony Chazapis
                year += 1900
70 16526868 Antony Chazapis
        month = MONTHS.index(m.group('mon').lower()) + 1
71 16526868 Antony Chazapis
        day = int(m.group('day'))
72 16526868 Antony Chazapis
        hour = int(m.group('hour'))
73 16526868 Antony Chazapis
        min = int(m.group('min'))
74 16526868 Antony Chazapis
        sec = int(m.group('sec'))
75 16526868 Antony Chazapis
        result = datetime.datetime(year, month, day, hour, min, sec)
76 16526868 Antony Chazapis
        return calendar.timegm(result.utctimetuple())
77 16526868 Antony Chazapis
    except Exception:
78 16526868 Antony Chazapis
        raise ValueError("%r is not a valid date" % date)
79 16526868 Antony Chazapis
80 16526868 Antony Chazapis
def parse_http_date_safe(date):
81 16526868 Antony Chazapis
    """
82 16526868 Antony Chazapis
    Same as parse_http_date, but returns None if the input is invalid.
83 16526868 Antony Chazapis
    """
84 16526868 Antony Chazapis
    try:
85 16526868 Antony Chazapis
        return parse_http_date(date)
86 16526868 Antony Chazapis
    except Exception:
87 16526868 Antony Chazapis
        pass