davx

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2026 License: MIT Imports: 12 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package davx is Frostmail's WebDAV client for CardDAV (RFC 6352) and CalDAV (RFC 4791): discovery, collections, sync-collection (RFC 6578), multiget, and conditional writes of raw objects (ADR-0017). Objects are bytes with ETags; parsing them is the caller's business (ADR-0018).

A Client talks to one origin, the home set's. Credentials go only there, to origins Options.Trusted accepts, and never over plain HTTP unless Options.AllowHTTP is set.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnauthorized means the server refused the credentials (401).
	ErrUnauthorized = errors.New("davx: the server refused the credentials")
	// ErrPrecondition means a conditional write failed (412): the object
	// changed on the server, or one exists where a new one was to go.
	ErrPrecondition = errors.New("davx: the object on the server is not the one expected")
	// ErrInvalidToken means the server no longer accepts a sync token; the
	// collection must be compared in full.
	ErrInvalidToken = errors.New("davx: the sync token is no longer valid")
	// ErrNotFound means the resource does not exist (404, 410).
	ErrNotFound = errors.New("davx: not found")
	// ErrUntrusted means the server sent the client to an origin that
	// Options.Trusted does not accept, or to plain HTTP.
	ErrUntrusted = errors.New("davx: the server points to an untrusted location")
)

Functions

func Discover

func Discover(ctx context.Context, start string, kind Kind, opts Options) (string, error)

Discover finds the home set of kind for the user that opts signs in as. It asks start for the user's principal (first at /.well-known/carddav or caldav when start has no path), then the principal for its home set, following redirects. start's origin is trusted; every other origin it is sent to must pass opts.Trusted. It returns the home set's absolute URL.

Types

type Change

type Change struct {
	Href string
	ETag string // "" when the server did not say
}

Change is an object's href and its current ETag.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client talks to one DAV origin.

func New

func New(base string, opts Options) (*Client, error)

New returns a client for base, an absolute URL whose origin every request goes to (normally a home set from Discover).

func (*Client) Collections

func (c *Client) Collections(ctx context.Context, kind Kind) ([]Collection, error)

Collections lists the address books or calendars in the client's home set.

func (*Client) DefaultCalendar

func (c *Client) DefaultCalendar(ctx context.Context) (string, error)

DefaultCalendar asks the server for the user's default calendar (RFC 6638 §9.2): from the calendar home this client is for, its principal, the principal's scheduling inbox, and the inbox's schedule-default-calendar-URL. It returns that calendar's path, or "" when the server names none or names one on another origin.

func (*Client) Delete

func (c *Client) Delete(ctx context.Context, href, ifMatch string) error

Delete removes an object if it still has the ETag ifMatch (any ETag when ifMatch is empty). An object already gone is not an error.

func (*Client) Get

func (c *Client) Get(ctx context.Context, href string) (Object, error)

Get fetches one object.

func (*Client) List

func (c *Client) List(ctx context.Context, coll string) ([]Change, error)

List returns every object of a collection with its ETag (PROPFIND at depth 1), for servers without sync-collection and for verify.

func (*Client) Multiget

func (c *Client) Multiget(ctx context.Context, kind Kind, coll string, hrefList []string) (objs []Object, missing []string, err error)

Multiget fetches objects of a collection by href (addressbook-multiget or calendar-multiget). Objects come back under the hrefs the caller gave; hrefs the server does not have are returned in missing. An XML parser turns CRLF into LF (XML 1.0 §2.11), so data without any CR gets its CRLFs back.

func (*Client) Put

func (c *Client) Put(ctx context.Context, href string, data []byte, kind Kind, ifMatch string) (Written, error)

Put writes an object. ifMatch is the ETag the change was made on; an empty ifMatch creates the object and fails with ErrPrecondition if one exists.

func (*Client) Sync

func (c *Client) Sync(ctx context.Context, coll, token string) (Delta, error)

Sync asks a collection what changed since token (sync-collection, RFC 6578); an empty token lists every object. A token the server no longer accepts gives ErrInvalidToken.

func (*Client) URL

func (c *Client) URL() string

URL is the client's base URL.

type Collection

type Collection struct {
	Href        string // its path, as the server wrote it
	Name        string
	Description string
	Color       string   // #rrggbb, or "" when the server has none
	Components  []string // a calendar's component types (VEVENT, VTODO); nil when the server does not say
	SyncToken   string
	CTag        string
	ReadOnly    bool // the user may not change its objects
	// Sync is whether the collection answers sync-collection: it says so in
	// supported-report-set or, when it lists no reports, has a sync token.
	Sync bool
}

Collection is an address book or calendar.

type Delta

type Delta struct {
	Changed []Change // new or changed objects
	Deleted []string // hrefs of removed objects
	Token   string   // the token to ask from next time
	// More means the server cut the answer short (507): ask again from
	// Token for the rest.
	More bool
}

Delta is what changed in a collection since a sync token.

type Kind

type Kind int

Kind is the kind of collection a client works with.

const (
	// AddressBooks are CardDAV address books.
	AddressBooks Kind = iota + 1
	// Calendars are CalDAV calendars.
	Calendars
)

func (Kind) ContentType

func (k Kind) ContentType() string

ContentType is the media type of the kind's objects.

func (Kind) String

func (k Kind) String() string

type Object

type Object struct {
	Href string // as the caller named it, or as the server wrote it
	ETag string
	Data []byte
}

Object is a contact or calendar object as the server holds it.

type Options

type Options struct {
	// Authorization returns the Authorization header for a request, such
	// as "Basic …" or "Bearer …"; nil sends none.
	Authorization func(ctx context.Context) (string, error)
	// HTTP sends the requests; nil uses one with a 60-second timeout. The
	// client never lets it follow redirects.
	HTTP *http.Client
	// Trusted reports whether credentials may go to u, an origin other
	// than the one a client or discovery started from (a redirect, a
	// principal or a home set on another host). Nil trusts none.
	Trusted func(u *url.URL) bool
	// AllowHTTP permits plain http:// URLs, for local test servers only.
	AllowHTTP bool
	// UserAgent is sent with every request.
	UserAgent string
}

Options configure a client.

type StatusError

type StatusError struct {
	Method, URL string
	Code        int
	Body        string // the start of the response body
}

StatusError is an HTTP status the client did not expect.

func (*StatusError) Error

func (e *StatusError) Error() string

type Written

type Written struct {
	// Href is where the object is: the href written, or for a new object
	// a server named itself (Google's CalDAV names it from its UID), the
	// Location it answered with.
	Href string
	// ETag is the new ETag, or "" when the server sent none (the caller
	// then fetches the object).
	ETag string
}

Written is what a PUT left on the server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL