140 lines
4.1 KiB
Go
140 lines
4.1 KiB
Go
package calendar
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"encoding/base64"
|
|
"strings"
|
|
"unicode"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
func DAVFilename(c Calendar, uid string) string {
|
|
if name := c.DAVNames[uid]; name != "" {
|
|
return name
|
|
}
|
|
return base64.RawURLEncoding.EncodeToString([]byte(uid)) + ".ics"
|
|
}
|
|
|
|
func ValidDAVFilename(name string) bool {
|
|
if len(name) < 5 || len(name) > 700 || !strings.HasSuffix(name, ".ics") {
|
|
return false
|
|
}
|
|
for _, ch := range name {
|
|
if !(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z' || ch >= '0' && ch <= '9' || ch == '-' || ch == '_' || ch == '.') {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func DAVUID(c Calendar, name string) (string, error) {
|
|
if !ValidDAVFilename(name) {
|
|
return "", ErrMissing
|
|
}
|
|
for uid, n := range c.DAVNames {
|
|
if n == name {
|
|
return uid, nil
|
|
}
|
|
}
|
|
b, err := base64.RawURLEncoding.DecodeString(strings.TrimSuffix(name, ".ics"))
|
|
if err != nil || len(b) == 0 || len(b) > 512 || !utf8.Valid(b) || strings.IndexFunc(string(b), unicode.IsControl) >= 0 {
|
|
return "", ErrMissing
|
|
}
|
|
return string(b), nil
|
|
}
|
|
|
|
// DAVSnapshot is complete or fails; a truncated listing can cause client deletes.
|
|
// The metadata lock keeps href mappings and event generations in one snapshot.
|
|
func (r *Repository) DAVSnapshot(ctx context.Context, domain, id, actor string, withData bool) (result Calendar, itemsResult []Event, resultErr error) {
|
|
defer func() { resultErr = r.failure(resultErr) }()
|
|
c, _, unlock, err := r.locked(ctx, domain, id, actor)
|
|
if err != nil {
|
|
return c, nil, err
|
|
}
|
|
defer unlock()
|
|
cols := "uid,etag,''"
|
|
if withData {
|
|
cols = "uid,etag,data"
|
|
}
|
|
rows, err := r.DB.QueryContext(ctx, `SELECT `+cols+` FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` ORDER BY uid LIMIT 10001`, domain, id)
|
|
if err != nil {
|
|
return c, nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Event{}
|
|
size := 0
|
|
for rows.Next() {
|
|
var item Event
|
|
if err = rows.Scan(&item.UID, &item.ETag, &item.Data); err != nil {
|
|
return c, nil, err
|
|
}
|
|
size += len(item.Data) + len(item.UID) + len(item.ETag)
|
|
if len(items) >= 10000 || size > 32<<20 {
|
|
return c, nil, ErrLimit
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
return c, items, rows.Err()
|
|
}
|
|
|
|
// ChangeDAV reserves resource identities in the same manifest commit as the
|
|
// monthly snapshots. Existing resources still require an exact strong ETag.
|
|
func (r *Repository) ChangeDAV(ctx context.Context, domain, id, actor, name, match string, item *Event, authorize func() error) (eventResult Event, created bool, resultErr error) {
|
|
defer func() { resultErr = r.failure(resultErr) }()
|
|
if !ValidDAVFilename(name) {
|
|
return Event{}, false, ErrInvalid
|
|
}
|
|
c, dir, unlock, err := r.lockedWithGuard(ctx, domain, id, actor, func(c Calendar) bool { return c.CanWrite(actor) })
|
|
if err != nil {
|
|
return Event{}, false, err
|
|
}
|
|
defer unlock()
|
|
if authorize != nil {
|
|
if err = authorize(); err != nil {
|
|
return Event{}, false, ErrForbidden
|
|
}
|
|
}
|
|
uid, _ := DAVUID(c, name)
|
|
var exists bool
|
|
if uid != "" {
|
|
if err = r.DB.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3)+`)`, domain, id, uid).Scan(&exists); err != nil {
|
|
return Event{}, false, err
|
|
}
|
|
}
|
|
if exists {
|
|
if match == "" {
|
|
return Event{}, false, ErrPreconditionRequired
|
|
}
|
|
if item != nil && item.UID != uid {
|
|
return Event{}, false, ErrConflict
|
|
}
|
|
} else {
|
|
if item == nil {
|
|
return Event{}, false, ErrMissing
|
|
}
|
|
if match != "" && match != "*" {
|
|
return Event{}, false, ErrConflict
|
|
}
|
|
uid = item.UID
|
|
var tag string
|
|
err = r.DB.QueryRowContext(ctx, `SELECT etag FROM calendar_event_cache WHERE domain=`+r.p(1)+` AND calendar=`+r.p(2)+` AND uid=`+r.p(3), domain, id, uid).Scan(&tag)
|
|
if err == nil {
|
|
return Event{}, false, ErrConflict
|
|
}
|
|
if err != sql.ErrNoRows {
|
|
return Event{}, false, err
|
|
}
|
|
if len(c.DAVNames) >= 10000 {
|
|
return Event{}, false, ErrLimit
|
|
}
|
|
if c.DAVNames == nil {
|
|
c.DAVNames = map[string]string{}
|
|
}
|
|
c.DAVNames[uid] = name
|
|
match = "*"
|
|
}
|
|
result, err := r.changeLocked(ctx, domain, actor, uid, match, item, c, dir)
|
|
return result, !exists, err
|
|
}
|