src/server/webdav/webdav.h

Sat, 19 Jan 2013 20:13:07 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 19 Jan 2013 20:13:07 +0100
changeset 48
37a512d7b8f6
parent 44
3da1f7b6847f
child 59
ab25c0a231d0
permissions
-rw-r--r--

fixed some memory leaks

/*
 * 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 WEBDAV_H
#define	WEBDAV_H

#include "../public/nsapi.h"

#include <sys/file.h>
#include <sys/stat.h>

#include <inttypes.h>

#include "../ucx/map.h"
#include "../ucx/dlist.h"
#include "../util/strbuf.h"

#ifdef	__cplusplus
extern "C" {
#endif

#define DAV_FOREACH(elem, list) \
        for (UcxDlist *elem = list ; elem != NULL ; elem = elem->next)
    
typedef struct PropfindResponse     PropfindResponse;
typedef struct DAVPropertyBackend   DAVPropertyBackend;

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;


struct PropfindRequest {
    Session            *sn;
    Request            *rq;

    UcxDlist           *properties; /* DavProperty list, requested props */
    int8_t             allprop;
    int8_t             propname;
    
    int8_t             prop;
    UcxDlist           *notFoundProps;
    UcxDlist           *forbiddenProps;
    DAVPropertyBackend *propertyBackend;

    char               *path;
    char               *uri;

    sbuf_t             *out;
};

struct ProppatchRequest {
    Session            *sn;
    Request            *rq;
    
    UcxDlist           *setProps;    /* XmlElement list, set props */
    UcxDlist           *removeProps; /* DavProperty list, remove props */
    
    Propstat           *propstat;
    XmlNsMap           *nsmap;
    
    DAVPropertyBackend *backend;
    
    
    sbuf_t             *out;
};

struct Propstat {
    UcxDlist           *okprop; /* properties with status 200 OK */
    UcxMap             *map;    /* all other properties */
    pool_handle_t      *pool;
};

struct DavProperty {
    char *xmlns;
    char *name;
};

struct XmlData {
    XmlNsMap   *nsmap;
    XmlElement *elm;
};

struct XmlElement {
    XmlNs  *xmlns;
    sstr_t 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 {
    UcxMap *map;
    pool_handle_t *pool;
};

/*
 * dav_propfind_f
 * 
 * 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
 * 
 * 
 * arg0: property backend
 * arg1: current PropfindRequest object
 * arg2: the webdav resource path
 */
typedef void(*dav_propfind_f)(DAVPropertyBackend*,PropfindRequest*,char*);

/*
 * dav_proppatch_f
 * 
 * Sets properties for a WebDAV resource. It should add all properties to the
 * propstat object (member of the ProppatchRequest).
 * 
 * arg0: WebDAV backend
 * arg1: current ProppatchRequest object
 */
typedef void(*dav_proppatch_f)(DAVPropertyBackend*,ProppatchRequest*);

struct DAVPropertyBackend {
    dav_propfind_f     propfind;
    dav_proppatch_f    proppatch;
};

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);

void dav_resource_response(PropfindRequest *rq, sstr_t path, sstr_t uri);

/*
 * 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);



DAVPropertyBackend* create_property_backend();
void dav_rq_propfind(DAVPropertyBackend *b, PropfindRequest *rq, char *path);
void dav_rq_proppatch(DAVPropertyBackend *b, ProppatchRequest *rq);

/*---------------------------------- 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, sbuf_t *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, sbuf_t *out, int wv);

#ifdef	__cplusplus
}
#endif

#endif	/* WEBDAV_H */

mercurial