-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
carddav: PROPPATCH support for address books #147
Open
bitfehler
wants to merge
1
commit into
emersion:master
Choose a base branch
from
bitfehler:bitfehler/carddav-proppatch
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ type Backend interface { | |
ListAddressBooks(ctx context.Context) ([]AddressBook, error) | ||
GetAddressBook(ctx context.Context, path string) (*AddressBook, error) | ||
CreateAddressBook(ctx context.Context, addressBook *AddressBook) error | ||
UpdateAddressBook(ctx context.Context, path string, update *AddressBookUpdate) error | ||
DeleteAddressBook(ctx context.Context, path string) error | ||
GetAddressObject(ctx context.Context, path string, req *AddressDataRequest) (*AddressObject, error) | ||
ListAddressObjects(ctx context.Context, path string, req *AddressDataRequest) ([]AddressObject, error) | ||
|
@@ -284,7 +285,7 @@ func (b *backend) Options(r *http.Request) (caps []string, allow []string, err e | |
if b.resourceTypeAtPath(r.URL.Path) != resourceTypeAddressObject { | ||
// Note: some clients assume the address book is read-only when | ||
// DELETE/MKCOL are missing | ||
return caps, []string{http.MethodOptions, "PROPFIND", "REPORT", "DELETE", "MKCOL"}, nil | ||
return caps, []string{http.MethodOptions, "PROPFIND", "PROPPATCH", "REPORT", "DELETE", "MKCOL"}, nil | ||
} | ||
|
||
var dataReq AddressDataRequest | ||
|
@@ -302,6 +303,8 @@ func (b *backend) Options(r *http.Request) (caps []string, allow []string, err e | |
http.MethodPut, | ||
http.MethodDelete, | ||
"PROPFIND", | ||
// TODO PROPPATCH support for address objects | ||
//"PROPPATCH", | ||
}, nil | ||
} | ||
|
||
|
@@ -614,43 +617,115 @@ func (b *backend) propFindAllAddressObjects(ctx context.Context, propfind *inter | |
} | ||
|
||
func (b *backend) PropPatch(r *http.Request, update *internal.PropertyUpdate) (*internal.Response, error) { | ||
homeSetPath, err := b.Backend.AddressBookHomeSetPath(r.Context()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resType := b.resourceTypeAtPath(r.URL.Path) | ||
resp := internal.NewOKResponse(r.URL.Path) | ||
|
||
if r.URL.Path == homeSetPath { | ||
// TODO: support PROPPATCH for address books | ||
switch resType { | ||
case resourceTypeAddressBook: | ||
abUpdate, err := b.propPatchAddressBook(r.Context(), update, resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
err = b.Backend.UpdateAddressBook(r.Context(), r.URL.Path, &abUpdate) | ||
if err != nil { | ||
return nil, err | ||
} | ||
case resourceTypeAddressObject: | ||
// TODO: support PROPPATCH for address objects | ||
return nil, internal.HTTPErrorf(http.StatusNotImplemented, "PROPPATCH for address objects not yet implemented") | ||
default: | ||
for _, prop := range update.Remove { | ||
emptyVal := internal.NewRawXMLElement(prop.Prop.XMLName, nil, nil) | ||
if err := resp.EncodeProp(http.StatusNotImplemented, emptyVal); err != nil { | ||
return nil, err | ||
for _, raw := range prop.Prop.Raw { | ||
rxn, ok := raw.XMLName() | ||
if !ok { | ||
return nil, fmt.Errorf("failed to parse properties") | ||
} | ||
emptyVal := internal.NewRawXMLElement(rxn, nil, nil) | ||
if err := resp.EncodeProp(http.StatusMethodNotAllowed, emptyVal); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
for _, prop := range update.Set { | ||
emptyVal := internal.NewRawXMLElement(prop.Prop.XMLName, nil, nil) | ||
if err := resp.EncodeProp(http.StatusNotImplemented, emptyVal); err != nil { | ||
return nil, err | ||
for _, raw := range prop.Prop.Raw { | ||
rxn, ok := raw.XMLName() | ||
if !ok { | ||
return nil, fmt.Errorf("failed to parse properties") | ||
} | ||
emptyVal := internal.NewRawXMLElement(rxn, nil, nil) | ||
if err := resp.EncodeProp(http.StatusMethodNotAllowed, emptyVal); err != nil { | ||
return nil, err | ||
} | ||
} | ||
} | ||
} else { | ||
for _, prop := range update.Remove { | ||
emptyVal := internal.NewRawXMLElement(prop.Prop.XMLName, nil, nil) | ||
if err := resp.EncodeProp(http.StatusMethodNotAllowed, emptyVal); err != nil { | ||
return nil, err | ||
} | ||
return resp, nil | ||
} | ||
|
||
func (b *backend) propPatchAddressBook(ctx context.Context, update *internal.PropertyUpdate, resp *internal.Response) (AddressBookUpdate, error) { | ||
// TODO handle all properties | ||
var ( | ||
result AddressBookUpdate | ||
name internal.DisplayName | ||
desc addressbookDescription | ||
) | ||
for _, prop := range update.Remove { | ||
for _, raw := range prop.Prop.Raw { | ||
rxn, ok := raw.XMLName() | ||
if !ok { | ||
return result, fmt.Errorf("failed to parse properties") | ||
} | ||
switch rxn { | ||
case internal.DisplayNameName: | ||
result.Description = new(string) | ||
if err := resp.EncodeProp(http.StatusOK, internal.DisplayName{}); err != nil { | ||
return result, err | ||
} | ||
case addressBookDescriptionName: | ||
result.Description = new(string) | ||
if err := resp.EncodeProp(http.StatusOK, desc); err != nil { | ||
return result, err | ||
} | ||
default: | ||
emptyVal := internal.NewRawXMLElement(rxn, nil, nil) | ||
if err := resp.EncodeProp(http.StatusNotImplemented, emptyVal); err != nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would a 404 be more appropriate for the properties we don't know about? |
||
return result, err | ||
} | ||
} | ||
} | ||
for _, prop := range update.Set { | ||
emptyVal := internal.NewRawXMLElement(prop.Prop.XMLName, nil, nil) | ||
if err := resp.EncodeProp(http.StatusMethodNotAllowed, emptyVal); err != nil { | ||
return nil, err | ||
} | ||
for _, prop := range update.Set { | ||
for _, raw := range prop.Prop.Raw { | ||
rxn, ok := raw.XMLName() | ||
if !ok { | ||
return result, fmt.Errorf("failed to parse properties") | ||
} | ||
switch rxn { | ||
case internal.DisplayNameName: | ||
if err := raw.Decode(&name); err != nil { | ||
return result, err | ||
} | ||
result.Name = &name.Name | ||
if err := resp.EncodeProp(http.StatusOK, internal.DisplayName{}); err != nil { | ||
return result, err | ||
} | ||
case addressBookDescriptionName: | ||
if err := raw.Decode(&desc); err != nil { | ||
return result, err | ||
} | ||
result.Description = &desc.Description | ||
if err := resp.EncodeProp(http.StatusOK, desc); err != nil { | ||
return result, err | ||
} | ||
default: | ||
emptyVal := internal.NewRawXMLElement(rxn, nil, nil) | ||
if err := resp.EncodeProp(http.StatusNotImplemented, emptyVal); err != nil { | ||
return result, err | ||
} | ||
} | ||
} | ||
} | ||
|
||
return resp, nil | ||
return result, nil | ||
} | ||
|
||
func (b *backend) Put(r *http.Request) (*internal.Href, error) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have to write out a multi-status here? Can't we just reply with a toplevel HTTP status instead? Something like
internal.HTTPErrorf
?