--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/dav/davql.c Sat Aug 17 12:04:04 2013 +0200 @@ -0,0 +1,139 @@ +/* + * 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. + */ + +#include <stdio.h> +#include <stdlib.h> + +#include "davql.h" +#include "methods.h" +#include "webdav.h" +#include "utils.h" + +DavQuery dav_ql_parse(char *query, va_list ap) { + DavQuery davquery; + davquery.command = DAV_QUERY_ERROR; + davquery.command_data = NULL; + sstr_t q = sstr(query); + q = sstrtrim(q); + + // get query command + sstr_t cmd; + cmd.ptr = NULL; + int i; + for(i=0;i<q.length;i++) { + if(q.ptr[i] == ' ') { + cmd = sstrsubsl(q, 0, i); + break; + } + } + if(!cmd.ptr) { + fprintf(stderr, "DQL syntax error\n"); + return davquery; + } + + cmd = sstrtrim(cmd); + q = sstrtrim(sstrsubs(q, i)); + if(!sstrcmp(cmd, S("get"))) { + davquery.command = DAV_QUERY_GET; + davquery.command_data = dav_ql_parse_get(q, ap); + } + + return davquery; +} + +DavGetQuery* dav_ql_parse_get(sstr_t q, va_list ap) { + sstr_t property_query = q; + q = util_getsubstr_until_token(q, S("from"), &property_query); + + sstr_t from_query = q; + sstr_t cond = util_getsubstr_until_token(q, S("where"), &from_query); + + // insert variable values + UcxBuffer *fbuf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); + int var = 0; + for(int i=0;i<from_query.length;i++) { + char c = from_query.ptr[i]; + if(c == '%') { + if(var) { + ucx_buffer_putc(fbuf, '%'); // previous '%' + } else { + var = 1; + } + } else if(var) { + switch(c) { + case 's': { + char *arg = va_arg(ap, char*); + ucx_buffer_puts(fbuf, arg); + break; + } + default: { + ucx_buffer_putc(fbuf, '%'); + ucx_buffer_putc(fbuf, c); + } + } + var = 0; + } else { + ucx_buffer_putc(fbuf, c); + } + } + + DavGetQuery *getquery = malloc(sizeof(DavGetQuery)); + getquery->properties = sstrdup(property_query); + getquery->from = sstrn(fbuf->space, fbuf->pos); + // TODO: condition + return getquery; +} + +void free_get_query(DavGetQuery *q) { + free(q->from.ptr); + free(q->properties.ptr); + free(q); +} + +int parse_path_query(sstr_t query, char **path, int *depth) { + if(query.length == 1) { + if(query.ptr[0] == '/') { + *path = sstrdup(query).ptr; + *depth = 1; + return 0; + } else { + *path = NULL; + return 1; + } + } + + if(query.ptr[query.length-1] == '*') { + *depth = -1; + *path = sstrdup(sstrsubsl(query, 0, query.length-1)).ptr; + } else { + *path = sstrdup(query).ptr; + *depth = 1; + } + + return 0; +}