libidav/webdav.h

Fri, 29 May 2015 12:29:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 May 2015 12:29:01 +0200
changeset 121
eea36bf5ffe2
parent 100
f4127c4d1018
child 124
41939c8f3f9c
permissions
-rw-r--r--

fixed statement type for logical expressions without operator + prepared OOM handling

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2015 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 <inttypes.h>
#include <ucx/map.h>
#include <ucx/mempool.h>
#include <ucx/list.h>
#include <ucx/buffer.h>
#include <curl/curl.h>
#include <libxml/tree.h>

#ifdef	__cplusplus
extern "C" {
#endif

typedef struct DavContext    DavContext;
typedef struct DavProxy      DavProxy;
typedef struct DavSession    DavSession;
typedef struct DavResource   DavResource;
typedef struct DavRequest    DavRequest;
typedef struct DavNamespace  DavNamespace;
typedef struct DavProperty   DavProperty;
typedef struct DavPropName   DavPropName;
typedef struct DavKey        DavKey;

#include "davql.h"

typedef size_t(*dav_read_func)(void*, size_t, size_t, void*);
typedef size_t(*dav_write_func)(const void*, size_t, size_t, void*);

enum DavError {
    DAV_OK = 0,
    DAV_ERROR,
    DAV_NOT_FOUND,
    DAV_UNAUTHORIZED,
    DAV_FORBIDDEN,
    DAV_METHOD_NOT_ALLOWED,
    DAV_CONFLICT,
    DAV_UNSUPPORTED_PROTOCOL,
    DAV_COULDNT_RESOLVE_PROXY,
    DAV_COULDNT_RESOLVE_HOST,
    DAV_COULDNT_CONNECT,
    DAV_TIMEOUT,
    DAV_SSL_ERROR
};

typedef enum DavError DavError;

#define DAV_SESSION_ENCRYPT_CONTENT     0x0001
#define DAV_SESSION_ENCRYPT_NAME        0x0002
#define DAV_SESSION_DECRYPT_CONTENT     0x0004
#define DAV_SESSION_DECRYPT_NAME        0x0008

#define DAV_SESSION_CONTENT_ENCRYPTION  0x0005
#define DAV_SESSION_FULL_ENCRYPTION     0x000f


#define DAV_NS "http://www.uap-core.de/"

struct DavNamespace {
    char *prefix;
    char *name;
};

struct DavResource {
    DavSession    *session;
    DavResource   *prev;
    DavResource   *next;
    DavResource   *parent;
    DavResource   *children;
    char          *name;
    char          *path;
    char          *href;
    uint64_t      contentlength;
    char          *contenttype;
    time_t        creationdate;
    time_t        lastmodified;
    void          *data;
    int           iscollection;
};

struct DavSession {
    DavContext    *context;
    CURL          *handle;
    char          *base_url;
    UcxMempool    *mp;
    UcxMap        *pathcache;
    DavKey        *key;
    uint32_t      flags;
    DavError      error;
    const char    *errorstr;
};

struct DavContext {
    UcxMap  *namespaces;
    UcxMap  *keys;
    UcxList *sessions;
    DavProxy *http_proxy;
    DavProxy *https_proxy;
};

struct DavProxy {
    char *url;
    char *username;
    char *password;
    char *no_proxy;
};

struct DavProperty {
    DavNamespace *ns;
    char         *name;
    char         *value;
};

struct DavPropName {
    char *ns;
    char *name;
};

#define DAV_KEY_AES128     0
#define DAV_KEY_AES256     1
#define DAV_KEY_PASSWORD   2

struct DavKey {
    char    *name;
    int     type;
    void    *data;
    size_t  length;
};

DavContext* dav_context_new();
void dav_context_destroy(DavContext *ctx);

void dav_context_add_key(DavContext *context, DavKey *key);
DavKey* dav_context_get_key(DavContext *context, char *name);

int dav_add_namespace(DavContext *context, char *prefix, char *ns);
DavNamespace* dav_get_namespace(DavContext *context, char *prefix);

DavSession* dav_session_new(DavContext *context, char *base_url);
DavSession* dav_session_new_auth(
        DavContext *context,
        char *base_url,
        char *user,
        char *password);
void dav_session_set_auth(DavSession *sn, char *user, char *password);
void dav_session_enable_encryption(DavSession *sn, DavKey *key, int flags);

void dav_session_destroy(DavSession *sn);

void* dav_session_malloc(DavSession *sn, size_t size);
void* dav_session_calloc(DavSession *sn, size_t nelm, size_t size);
void* dav_session_realloc(DavSession *sn, void *ptr, size_t size);
void  dav_session_free(DavSession *sn, void *ptr);
char* dav_session_strdup(DavSession *sn, char *str);


DavResource* dav_get(DavSession *sn, char *path, char *properties);

DavResource* dav_query_get(DavSession *sn, DavGetQuery *query);

UcxList* parse_properties_string(DavContext *context, sstr_t str);

DavResource* dav_query(DavSession *sn, char *query, ...);

UcxKey dav_property_key(char *ns, char *name);
void dav_get_property_namespace(
        DavContext *ctx,
        char *prefixed_name,
        char **ns,
        char **name);

/* ------------------------ resource functions ------------------------ */

DavResource* dav_resource_new(DavSession *sn, char *path);
DavResource* dav_resource_new_child(DavSession *sn, DavResource *parent, char *name);
DavResource* dav_resource_new_href(DavSession *sn, char *href);

void dav_resource_free(DavResource *res);
void dav_resource_free_all(DavResource *res);

char* dav_resource_get_href(DavResource *resource);

DavResource* dav_create_child(DavResource *parent, char *name);
int dav_delete(DavResource *res);
int dav_create(DavResource *res);
int dav_exists(DavResource *res);

char* dav_get_property(DavResource *res, char *name);
char* dav_get_property_ns(DavResource *res, char *ns, char *name);
void dav_set_property(DavResource *res, char *name, char *value);
void dav_set_property_ns(DavResource *res, char *ns, char *name, char *value);
void dav_remove_property(DavResource *res, char *name);
void dav_remove_property_ns(DavResource *res, char *ns, char *name);

DavPropName* dav_get_property_names(DavResource *res, size_t *count);

void dav_set_content(DavResource *res, void *stream, dav_read_func read_func);
void dav_set_content_data(DavResource *res, char *content, size_t length);

int dav_load(DavResource *res);
int dav_store(DavResource *res);
int dav_get_content(DavResource *res, void *stream, dav_write_func write_func);

// private
int dav_propfind(DavSession *sn, DavResource *root, UcxBuffer *rqbuf, DavQOp *cond, size_t len);

#ifdef	__cplusplus
}
#endif

#endif	/* WEBDAV_H */

mercurial