Skip to content

Commit

Permalink
fix versions, cleanups
Browse files Browse the repository at this point in the history
Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
  • Loading branch information
butonic committed Aug 16, 2019
1 parent 0180f35 commit a9eb0af
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"io"

storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha"
usershareproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/usershareprovider/v0alpha"
"github.com/cs3org/reva/cmd/revad/grpcserver"
"github.com/cs3org/reva/cmd/revad/svcs/grpcsvcs/status"
Expand Down Expand Up @@ -152,36 +151,12 @@ func (s *service) ListShares(ctx context.Context, req *usershareproviderv0alphap
shares := []*usershareproviderv0alphapb.Share{}
var err error

if req.Filters == nil {
shares, err = s.sm.ListShares(ctx, nil) // TODO(labkode): add filter to share manager
if err != nil {
log.Err(err).Msg("error listing shares")
return &usershareproviderv0alphapb.ListSharesResponse{
Status: status.NewInternal(ctx, "error listing shares"),
}, nil
}
} else {
for _, filter := range req.Filters {
if filter.Type == usershareproviderv0alphapb.ListSharesRequest_Filter_LIST_SHARES_REQUEST_FILTER_TYPE_RESOURCE_ID {
ref := &storageproviderv0alphapb.ResourceInfo{
Id: filter.GetResourceId(),
}
log.Debug().Str("ref", ref.String()).Msg("list shares by resource id")

shares, err = s.sm.ListShares(ctx, ref)
if err != nil {
log.Err(err).Msg("error listing shares")
return &usershareproviderv0alphapb.ListSharesResponse{
Status: status.NewInternal(ctx, "error listing shares"),
}, nil
}
} else {
log.Warn().Interface("filter", filter).Msg("fileter not implemented")
return &usershareproviderv0alphapb.ListSharesResponse{
Status: status.NewUnimplemented(ctx, "error listing shares"),
}, nil
}
}
shares, err = s.sm.ListShares(ctx, req.Filters) // TODO(labkode): add filter to share manager
if err != nil {
log.Err(err).Msg("error listing shares")
return &usershareproviderv0alphapb.ListSharesResponse{
Status: status.NewInternal(ctx, "error listing shares"),
}, nil
}

res := &usershareproviderv0alphapb.ListSharesResponse{
Expand Down
11 changes: 7 additions & 4 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/dav.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@ type DavHandler struct {

func (h *DavHandler) init(c *Config) error {
h.FilesHandler = new(FilesHandler)
h.FilesHandler.init(c)
if err := h.FilesHandler.init(c); err != nil {
return err
}
h.AvatarsHandler = new(AvatarsHandler)
h.AvatarsHandler.init(c)
if err := h.AvatarsHandler.init(c); err != nil {
return err
}
h.MetaHandler = new(MetaHandler)
h.MetaHandler.init(c)
return nil
return h.MetaHandler.init(c)
}

// Handler handles requests
Expand Down
2 changes: 1 addition & 1 deletion cmd/revad/svcs/httpsvcs/ocdavsvc/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (h *FilesHandler) Handler(s *svc) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

// webdav should be death: baseURI is encoded as part of the
// reponse payload in href field
// response payload in href field
baseURI := path.Join("/", s.Prefix(), "remote.php/dav/files")
ctx := context.WithValue(r.Context(), ctxKeyBaseURI, baseURI)
r = r.WithContext(ctx)
Expand Down
3 changes: 1 addition & 2 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ocdavsvc

import (
"fmt"
"io"
"net/http"
"time"
Expand Down Expand Up @@ -111,7 +110,7 @@ func (s *svc) doGet(w http.ResponseWriter, r *http.Request) {

w.Header().Set("Content-Type", info.MimeType)
w.Header().Set("ETag", info.Etag)
w.Header().Set("OC-FileId", fmt.Sprintf("%s:%s", info.Id.StorageId, info.Id.OpaqueId))
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
t := utils.TSToTime(info.Mtime)
lastModifiedString := t.Format(time.RFC1123)
Expand Down
3 changes: 1 addition & 2 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/head.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ocdavsvc

import (
"fmt"
"net/http"
"time"

Expand Down Expand Up @@ -61,7 +60,7 @@ func (s *svc) doHead(w http.ResponseWriter, r *http.Request) {
info := res.Info
w.Header().Set("Content-Type", info.MimeType)
w.Header().Set("ETag", info.Etag)
w.Header().Set("OC-FileId", fmt.Sprintf("%s:%s", info.Id.StorageId, info.Id.OpaqueId))
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
t := utils.TSToTime(info.Mtime)
lastModifiedString := t.Format(time.RFC1123)
Expand Down
13 changes: 3 additions & 10 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ocdavsvc

import (
"encoding/base64"
"net/http"

"github.com/cs3org/reva/cmd/revad/svcs/httpsvcs"
Expand All @@ -32,8 +31,7 @@ type MetaHandler struct {

func (h *MetaHandler) init(c *Config) error {
h.VersionsHandler = new(VersionsHandler)
h.VersionsHandler.init(c)
return nil
return h.VersionsHandler.init(c)
}

// Handler handles requests
Expand All @@ -47,18 +45,13 @@ func (h *MetaHandler) Handler(s *svc) http.Handler {
return
}

decodedID, err := base64.StdEncoding.DecodeString(id)
if err != nil {
http.Error(w, "400 Bad Request", http.StatusBadRequest)
return
}
did := unwrap(id)

var head string
head, r.URL.Path = httpsvcs.ShiftPath(r.URL.Path)

switch head {
case "v":
h.VersionsHandler.Handler(s, string(decodedID)).ServeHTTP(w, r)
h.VersionsHandler.Handler(s, did).ServeHTTP(w, r)
default:
w.WriteHeader(http.StatusNotFound)
}
Expand Down
3 changes: 1 addition & 2 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/move.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ocdavsvc

import (
"fmt"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -195,7 +194,7 @@ func (s *svc) doMove(w http.ResponseWriter, r *http.Request) {
info := dstStatRes.Info
w.Header().Set("Content-Type", info.MimeType)
w.Header().Set("ETag", info.Etag)
w.Header().Set("OC-FileId", fmt.Sprintf("%s:%s", info.Id.StorageId, info.Id.OpaqueId))
w.Header().Set("OC-FileId", wrapResourceID(info.Id))
w.Header().Set("OC-ETag", info.Etag)
w.WriteHeader(successCode)
}
35 changes: 33 additions & 2 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/ocdavsvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
package ocdavsvc

import (
"encoding/base64"
"fmt"
"net/http"
"os"
"strings"

storageproviderv0alphapb "github.com/cs3org/go-cs3apis/cs3/storageprovider/v0alpha"
"github.com/cs3org/reva/cmd/revad/httpserver"
Expand Down Expand Up @@ -49,10 +52,8 @@ type Config struct {

type svc struct {
c *Config
handler http.Handler
webDavHandler *WebDavHandler
davHandler *DavHandler
gatewaySvc string
}

// New returns a new ocdavsvc
Expand All @@ -76,6 +77,9 @@ func New(m map[string]interface{}) (httpsvcs.Service, error) {
davHandler: new(DavHandler),
}
// initialize handlers and set default configs
if err := s.webDavHandler.init(conf); err != nil {
return nil, err
}
if err := s.davHandler.init(conf); err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,3 +140,30 @@ func (s *svc) Handler() http.Handler {
func (s *svc) getClient() (storageproviderv0alphapb.StorageProviderServiceClient, error) {
return pool.GetStorageProviderServiceClient(s.c.GatewaySvc)
}

func wrapResourceID(r *storageproviderv0alphapb.ResourceId) string {
return wrap(r.StorageId, r.OpaqueId)
}

// The fileID must be encoded
// - XML safe, because it is going to be used in the profind result
// - url safe, because the id might be used in a url, eg. the /dav/meta nodes
// which is why we base62 encode it
func wrap(sid string, oid string) string {
return base64.URLEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", sid, oid)))
}

func unwrap(rid string) *storageproviderv0alphapb.ResourceId {
decodedID, err := base64.URLEncoding.DecodeString(rid)
if err != nil {
return nil
}
parts := strings.SplitN(string(decodedID), ":", 2)
if len(parts) != 2 {
return nil
}
return &storageproviderv0alphapb.ResourceId{
StorageId: parts[0],
OpaqueId: parts[1],
}
}
2 changes: 1 addition & 1 deletion cmd/revad/svcs/httpsvcs/ocdavsvc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (s *svc) doOptions(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Access-Control-Allow-Methods", allow)
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Depth, Ocs-Apirequest")
w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type, Depth, Ocs-Apirequest, If-None-Match, Destination")
w.Header().Set("Content-Type", "application/xml")
w.Header().Set("Allow", allow)
w.Header().Set("DAV", "1, 2")
Expand Down
16 changes: 2 additions & 14 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/propfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package ocdavsvc

import (
"context"
"encoding/base64"
"encoding/xml"
"fmt"
"io"
Expand Down Expand Up @@ -150,7 +149,7 @@ func readPropfind(r io.Reader) (pf propfindXML, status int, err error) {
}

func (s *svc) formatPropfind(ctx context.Context, mds []*storageproviderv0alphapb.ResourceInfo) (string, error) {
responses := []*responseXML{}
responses := make([]*responseXML, 0, len(mds))
for _, md := range mds {
res, err := s.mdToPropResponse(ctx, md)
if err != nil {
Expand Down Expand Up @@ -213,18 +212,7 @@ func (s *svc) mdToPropResponse(ctx context.Context, md *storageproviderv0alphapb
getLastModified := s.newProp("d:getlastmodified", lasModifiedString)
propList = append(propList, getLastModified)

// the fileID must be xml-escaped as there are cases like public links
// that contains a path as the file id. This path can contain &, for example,
// which if it is not encoded properly, will result in an empty view for the user
//var fileIDEscaped bytes.Buffer
//if err := xml.EscapeText(&fileIDEscaped, []byte(fmt.Sprintf("%s:%s", md.Id.StorageId, md.Id.OpaqueId))); err != nil {
// return nil, err
//}
//ocID := s.newProp("oc:fileid", fileIDEscaped.String())
// TODO(jfd) xmlencoding still contains slashes, but the fileid might be used in the url as well.
// for the versions endpoint we need to either also urlencode ... or just base64 encode ... it should be opaque anyway.
base64id := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", md.Id.StorageId, md.Id.OpaqueId)))
ocID := s.newProp("oc:fileid", base64id)
ocID := s.newProp("oc:fileid", wrapResourceID(md.Id))
propList = append(propList, ocID)

// PropStat, only HTTP/1.1 200 is sent.
Expand Down
3 changes: 1 addition & 2 deletions cmd/revad/svcs/httpsvcs/ocdavsvc/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package ocdavsvc

import (
"fmt"
"net/http"
"regexp"
"strconv"
Expand Down Expand Up @@ -242,7 +241,7 @@ func (s *svc) doPut(w http.ResponseWriter, r *http.Request) {

w.Header().Add("Content-Type", info2.MimeType)
w.Header().Set("ETag", info2.Etag)
w.Header().Set("OC-FileId", fmt.Sprintf("%s:%s", info2.Id.StorageId, info2.Id.OpaqueId))
w.Header().Set("OC-FileId", wrapResourceID(info2.Id))
w.Header().Set("OC-ETag", info2.Etag)
t := utils.TSToTime(info2.Mtime)
lastModifiedString := t.Format(time.RFC1123)
Expand Down
Loading

0 comments on commit a9eb0af

Please sign in to comment.