src/server/public/webdav.h

changeset 59
ab25c0a231d0
child 91
fac51f87def0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/public/webdav.h	Mon May 06 13:44:27 2013 +0200
@@ -0,0 +1,319 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef WS_WEBDAV_H
+#define	WS_WEBDAV_H
+
+#include "nsapi.h"
+
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <inttypes.h>
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+typedef struct DavCollection          DavCollection;
+    
+typedef struct PropfindResponse       PropfindResponse;
+typedef struct PersistenceManager     PersistenceManager;
+
+typedef struct PropfindRequest        PropfindRequest;
+typedef struct ProppatchRequest       ProppatchRequest;
+typedef struct DavProperty            DavProperty;
+
+typedef struct Propstat               Propstat;
+
+typedef struct XmlNs                  XmlNs;
+typedef struct XmlNsMap               XmlNsMap;
+
+typedef struct XmlData                XmlData;
+typedef struct XmlElement             XmlElement;
+
+typedef uint8_t                       xmlch_t;
+
+typedef struct UcxDlist               List;
+typedef struct UcxMap                 Map;
+
+typedef struct _strbuf                Buffer;
+
+struct DavCollection {
+    PersistenceManager *mgr;
+    // callbacks
+};
+
+struct PropfindRequest {
+    Session            *sn;
+    Request            *rq;
+    
+    XmlNsMap           *nsmap;
+    
+    List               *properties; /* DavProperty list, requested props */
+    int8_t             allprop;
+    int8_t             propname;
+    
+    int8_t             prop;
+    int8_t             isdir;
+    List               *notFoundProps;
+    List               *forbiddenProps;
+    PersistenceManager *persistencemgr;
+    void               *mgrdata;
+
+    char               *path;
+    char               *uri;
+
+    Buffer             *out;
+};
+
+struct ProppatchRequest {
+    Session            *sn;
+    Request            *rq;
+    
+    List               *setProps;    /* XmlElement list, set props */
+    List               *removeProps; /* DavProperty list, remove props */
+    
+    Propstat           *propstat;
+    XmlNsMap           *nsmap;
+    
+    PersistenceManager *backend;
+    
+    char               *path;
+    
+    Buffer             *out;
+};
+
+struct Propstat {
+    List           *okprop; /* properties with status 200 OK */
+    Map            *map;    /* all other properties */
+    pool_handle_t  *pool;
+};
+
+struct DavProperty {
+    XmlNs *xmlns;
+    char  *name;
+};
+
+struct XmlData {
+    XmlNsMap   *nsmap;
+    XmlElement *elm;
+};
+
+struct XmlElement {
+    XmlNs  *xmlns;
+    char   *name;
+    void   *content; /* xmlch* or UcxDlist* */
+    size_t ctlen;    /* content string length. If 0, content is a UcxDlist */
+};
+
+struct XmlNs {
+    char *xmlns;
+    char *prefix;
+    int  nslen;
+    int  prelen;
+};
+
+struct XmlNsMap {
+    Map           *map;
+    pool_handle_t *pool;
+    int num;
+};
+
+typedef void(*dav_propfind_begin_f)(PersistenceManager*, PropfindRequest*);
+typedef void(*dav_propfind_end_f)(PersistenceManager*, PropfindRequest*);
+typedef void(*dav_propfind_f)(PersistenceManager*,PropfindRequest*,char*);
+typedef void(*dav_proppatch_f)(PersistenceManager*,ProppatchRequest*);
+struct PersistenceManager {
+    void(*propfind_begin)(PersistenceManager *mgr, PropfindRequest *rq);
+    void(*propfind_end)(PersistenceManager *mgr, PropfindRequest *rq);
+    
+    /*
+     * void propfind(PersistenceManager *mgr, PropfindRequest *rq, char *path)
+     * 
+     * Gets all requested properties for a WebDAV resource(file). This function
+     * should add properties with dav_propfind_add_str_prop or
+     * dav_prop_add_xml_prop. Unavailable properties should be added with
+     * dav_propfind_add_prop_error
+     * 
+     * 
+     * mgr:  WebDAV Persistence Manager
+     * rq:   current PropfindRequest object
+     * path: the webdav resource path
+     */
+    void(*propfind)(PersistenceManager *mgr, PropfindRequest *rq, char *path);
+    
+    /*
+     * void proppatch(PersistenceManager *mgr, ProppatchRequest *rq)
+     * 
+     * Sets properties for a WebDAV resource. It should add all properties to
+     * the propstat object (member of the ProppatchRequest).
+     * 
+     * mgr:  WebDAV backend
+     * path: current ProppatchRequest object
+     */
+    void(*proppatch)(PersistenceManager *mgr, ProppatchRequest *rq);
+    
+    /*
+     * bool
+     * if true, some properties are get from the VFS and not from this
+     * persistence manager
+     */
+    int vfs_props;
+};
+
+void webdav_add_persistence_manager(char *name, PersistenceManager *mgr);
+
+int webdav_service(pblock *pb, Session *sn, Request *rq);
+int webdav_put(pblock *pb, Session *sn, Request *rq);
+int webdav_delete(pblock *pb, Session *sn, Request *rq);
+int webdav_mkcol(pblock *pb, Session *sn, Request *rq);
+int webdav_copy(pblock *pb, Session *sn, Request *rq);
+int webdav_move(pblock *pb, Session *sn, Request *rq);
+int webdav_propfind(pblock *pb, Session *sn, Request *rq);
+int webdav_proppatch(pblock *pb, Session *sn, Request *rq);
+
+
+/*
+ * dav_propfind_add_str_prop
+ * 
+ * Adds a string property to the current WebDAV resource response
+ */
+void dav_propfind_add_str_prop(
+        PropfindRequest *rq,
+        DavProperty* prop,
+        char *str,
+        size_t len);
+
+//void dav_propfind_add_xml_prop();
+
+/*
+ * dav_propfind_add_prop_error
+ * 
+ * Adds a unavailable property to the response
+ * 
+ * rq:    propfind request object
+ * prop:  unavailable property
+ * error: HTTP status code
+ */
+void dav_propfind_add_prop_error(
+        PropfindRequest *rq,
+        DavProperty *prop,
+        int error);
+
+
+
+/*---------------------------------- utils ----------------------------------*/
+
+/*
+ * XmlNsMap
+ * 
+ * a map containing xml namespaces
+ * 
+ * key:    namespace uri
+ * value:  XmlNs object, containing namespace uri and the prefix
+ */
+
+XmlNsMap* xmlnsmap_create(pool_handle_t *pool);
+
+void xmlnsmap_free(XmlNsMap *map);
+
+/*
+ * Puts a namespace in the map. If the namespace is already in the map, the
+ * available XmlNs object is returned
+ */
+XmlNs* xmlnsmap_put(XmlNsMap *map, char *ns);
+
+/*
+ * Gets a namespace from the map. Returns NULL if the namespace is not in the
+ * map
+ */
+XmlNs* xmlnsmap_get(XmlNsMap *map, char *ns);
+
+
+
+/*
+ * XmlElement
+ * 
+ * representing a xml element
+ */
+
+/*
+ * adds a xml element to a parent element
+ */
+void xmlelm_add_child(XmlElement *parent, XmlElement *child);
+
+/*
+ * writes an xml element to an output buffer
+ * if wv is true, it writes the complete value of the element
+ */
+void xmlelm_write(XmlElement *elm, Buffer *out, int wv);
+
+
+/*
+ * PropstatMap
+ * 
+ * A map containing multiple propstat objects
+ * 
+ * key:    status code:  int
+ * value:  prop list:    UcxDlist* (list of XmlElement*)
+ */
+
+
+/*
+ * creates a new Propstat
+ */
+Propstat* propstat_create(pool_handle_t *pool);
+
+
+/*
+ * adds a property to the propstat map
+ * 
+ * propstat:  propstat object
+ * status:    http status code
+ * prop:      WebDAV property
+ */
+void propstat_add(Propstat *propstat, int status, XmlElement *prop);
+
+/*
+ * writes the propstat object to an output buffer
+ * if wv is true, it writes the values of the 'OK' properties
+ * 
+ * propstat:  propstat object
+ * out:       output buffer
+ * wv:        property output mode
+ */
+void propstat_write(Propstat *propstat, Buffer *out, int wv);
+
+
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif	/* WS_WEBDAV_H */
+

mercurial