Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.
This repository was archived by the owner on Mar 30, 2022. It is now read-only.

propFind method with empty properties returns 400 #17

@d70rr3s

Description

@d70rr3s

Hi, I'm using DAV Client as adapter with Gaufrette and when I make a PROPFIND request with no properties the server send a 400 response. This happens with functions like this one:

       private function isDir($directory) {
        if ('/' === $directory) {
            return true;
        }

        $data = $this->getClient()->propFind($directory, [], 0);

        return !empty($data) && $data['{DAV:}resourcetype']->is('{DAV:}collection');
    }

I've been testing the resulting request and because the prop tags are empty the server returns a 400 response. In the other hand if we replace the prop tag with allprop tag it goes as expected. I make a work around and patch the code in the mean time like this:

       public function propFind($url, array $properties, $depth = 0) {

        $dom = new \DOMDocument('1.0', 'UTF-8');
        $dom->formatOutput = true;
        $root = $dom->createElementNS('DAV:', 'd:propfind');

       // My code
      // -----------
        if (count($properties) > 0) {
            $prop = $dom->createElement('d:prop');

            foreach($properties as $property) {

                list(
                    $namespace,
                    $elementName
                    ) = XMLUtil::parseClarkNotation($property);

                if ($namespace === 'DAV:') {
                    $element = $dom->createElement('d:'.$elementName);
                } else {
                    $element = $dom->createElementNS($namespace, 'x:'.$elementName);
                }

                $prop->appendChild( $element );
            }
        } else {
            $prop = $dom->createElement('d:allprop');
        }
        // -------

        $dom->appendChild($root)->appendChild( $prop );
        $body = $dom->saveXML();

        $url = $this->getAbsoluteUrl($url);

        $request = new HTTP\Request('PROPFIND', $url, [
            'Depth' => $depth,
            'Content-Type' => 'application/xml'
        ], $body);

        $response = $this->send($request);

        if ((int)$response->getStatus() >= 400) {
            throw new Exception('HTTP error: ' . $response->getStatus());
        }

        $result = $this->parseMultiStatus($response->getBodyAsString());

        // If depth was 0, we only return the top item
        if ($depth===0) {
            reset($result);
            $result = current($result);
            return isset($result[200])?$result[200]:[];
        }

        $newResult = [];
        foreach($result as $href => $statusList) {

            $newResult[$href] = isset($statusList[200])?$statusList[200]:[];

        }

        return $newResult;

    }

I don't like this solution and looking a way round to solve this issue.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions