96464896
  B   ˆl–Ü4ý( ©”u( 	•íÀà™S�ÿ�þZdY¹DGÃ Â�ÏÊ„�:çÇ_ŠI|¥�+Wé3ž¯ÁÅ ?÷     # Copyright (C) 2008, 2009, 2011, 2013 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""DAV test server.

This defines the TestingDAVRequestHandler and the DAVServer classes which
implements the DAV specification parts used by the webdav plugin.
"""


import errno
import os
import re
import shutil # FIXME: Can't we use breezy.osutils ?
import stat
import time
import urllib.parse # FIXME: Can't we use breezy.urlutils ?


from breezy import (
    trace,
    urlutils,
    )
from breezy.tests import http_server


class TestingDAVRequestHandler(http_server.TestingHTTPRequestHandler):
    """
    Subclass of TestingHTTPRequestHandler handling DAV requests.

    This is not a full implementation of a DAV server, only the parts
    really used by the plugin are.
    """

    _RANGE_HEADER_RE = re.compile(
        r'bytes (?P<begin>\d+)-(?P<end>\d+)/(?P<size>\d+|\*)')

    delete_success_code = 204
    move_default_overwrite = True


    def date_time_string(self, timestamp=None):
        """Return the current date and time formatted for a message header."""
        if timestamp is None:
            timestamp = time.time()
        year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp)
        s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
                self.weekdayname[wd],
                day, self.monthname[month], year,
                hh, mm, ss)
        return s

    def _read(self, length):
        """Read the client socket"""
        return self.rfile.read(length)

    def _readline(self):
        """Read a full line on the client socket"""
        return self.rfile.readline()

    def read_body(self):
        """Read the body either by chunk or as a whole."""
        content_length = self.headers.get('Content-Length')
        encoding = self.headers.get('Transfer-Encoding')
        if encoding is not None:
            assert encoding == 'chunked'
            body = []
            # We receive the content by chunk
            while True:
                length, data = self.read_chunk()
                if length == 0:
                    break
                body.append(data)
            body = ''.join(body)

        else:
            if content_length is not None:
                body = self._read(int(content_length))

        return body

    def read_chunk(self):
        """Read a chunk of data.

        A chunk consists of:
        - a line containing the length of the data in hexa,
        - the data.
        - a empty line.

        An empty chunk specifies a length of zero
        """
        length = int(self._readline(), 16)
        data = None
        if length != 0:
            data = self._read(length)
            # Eats the newline following the chunk
            self._readline()
        return length, data

    def send_head(self):
        """Specialized version of SimpleHttpServer.

        We *don't* want the apache behavior of permanently redirecting
        directories without trailing slashes to directories with trailing
        slashes. That's a waste and a severe penalty for clients with high
        latency.

        The installation documentation of the plugin should mention the
        DirectorySlash apache directive and insists on turning it *Off*.
        """
        path = self.translate_path(self.path)
        f = None
        if os.pat