Normal
 ?÷     # Copyright (C) 2006-2009, 2011, 2012, 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

"""Implementation of WebDAV for http transports.

A Transport which complement http transport by implementing
partially the WebDAV protocol to push files.
This should enable remote push operations.
"""

from io import StringIO
import os
import random
import sys
import time
import xml.sax
import xml.sax.handler


from breezy import (
    errors,
    osutils,
    trace,
    transport,
    )
from breezy.transport.http import (
    urllib,
    )


class DavResponseHandler(xml.sax.handler.ContentHandler):
    """Handle a multi-status DAV response."""

    def __init__(self):
        self.url = None
        self.elt_stack = None
        self.chars = None
        self.chars_wanted = False
        self.expected_content_handled = False

    def set_url(self, url):
        """Set the url used for error reporting when handling a response."""
        self.url = url

    def startDocument(self):
        self.elt_stack = []
        self.chars = None
        self.expected_content_handled = False

    def endDocument(self):
        self._validate_handling()
        if not self.expected_content_handled:
            raise errors.InvalidHttpResponse(self.url,
                                             msg='Unknown xml response')

    def startElement(self, name, attrs):
        self.elt_stack.append(self._strip_ns(name))
        # The following is incorrect in the general case where elements are
        # intermixed with chars in a higher level element. That's not the case
        # here (otherwise the chars_wanted will have to be stacked too).
        if self.chars_wanted:
            self.chars = ''
        else:
            self.chars = None

    def endElement(self, name):
        self.chars = None
        self.chars_wanted = False
        self.elt_stack.pop()

    def characters(self, chrs):
        if self.chars_wanted:
            self.chars += chrs

    def _current_element(self):
        return self.elt_stack[-1]

    def _strip_ns(self, name):
        """Strip the leading namespace from name.

        We don't have namespaces clashes in our context, stripping it makes the
        code simpler.
        """
        where = name.find(':')
        if where == -1:
            return name
        else:
            return name[where + 1:]


class DavStatHandler(DavResponseHandler):
    """Handle a PROPPFIND DAV response for a file or directory.

    The expected content is:
    - a multi-status element containing
      - a single response element containing
        - a href element
        - a propstat element containing
          - a status element (ignored)
          - a prop element containing at least (other are ignored)
            - a getcontentlength element (for files only)
            - an executable element (for files only)
            - a resourcetype element containing
              - a collection element (for directories only)
    """

    def __init__(self):
        DavResponseHandler.__init__(self)
        # Flags defining the context for the actions
        self._response_seen = False
        self._init_response_attrs()

    def _init_response_attrs(self):
        self.href = None
        self.length = -1
        self.executable = None
        self.is_dir = False

    def _validate_handling(self):
        if self.href is not None:
            self.expected_