Skip to content

Commit

Permalink
enhance xml error response
Browse files Browse the repository at this point in the history
  • Loading branch information
refs committed Nov 24, 2020
1 parent 24bf5c4 commit b8222bd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
50 changes: 50 additions & 0 deletions internal/http/services/owncloud/ocdav/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2018-2020 CERN
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// In applying this license, CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.

package ocdav

import (
"encoding/xml"
)

type code int

const (
SabredavMethodNotAllowed code = iota
)

var (
codesEnum = []string{
"Sabre\\DAV\\Exception\\MethodNotAllowed",
}
)

type exception struct {
code code
message string
}

func Marshal(e exception) ([]byte, error) {

return xml.Marshal(&errorXML{
Xmlnsd: "DAV",
Xmlnss: "http://sabredav.org/ns",
Exception: codesEnum[e.code],
Message: e.message,
})
}
19 changes: 17 additions & 2 deletions internal/http/services/owncloud/ocdav/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ func (s *svc) handlePropfind(w http.ResponseWriter, r *http.Request, ns string)
if r.URL.Path == "/" {
log.Debug().Str("path", r.URL.Path).Msg("method not allowed")
w.WriteHeader(http.StatusMethodNotAllowed)
b, err := Marshal(exception{
code: SabredavMethodNotAllowed,
message: "Listing members of this collection is disabled",
})
if err != nil {
log.Error().Msgf("error marshaling xml response: %s", b)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Write(b)
return
}

// see https://tools.ietf.org/html/rfc4918#section-10.2
Expand Down Expand Up @@ -647,8 +658,12 @@ type propertyXML struct {

// http://www.webdav.org/specs/rfc4918.html#ELEMENT_error
type errorXML struct {
XMLName xml.Name `xml:"d:error"`
InnerXML []byte `xml:",innerxml"`
XMLName xml.Name `xml:"d:error"`
Xmlnsd string `xml:"xmlns:d,attr"`
Xmlnss string `xml:"xmlns:s,attr"`
Exception string `xml:"s:exception"`
Message string `xml:"s:message"`
InnerXML []byte `xml:",innerxml"`
}

var errInvalidPropfind = errors.New("webdav: invalid propfind")

0 comments on commit b8222bd

Please sign in to comment.