|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 |
|
32 #include "davql.h" |
|
33 #include "methods.h" |
|
34 #include "webdav.h" |
|
35 #include "utils.h" |
|
36 |
|
37 DavQuery dav_ql_parse(char *query, va_list ap) { |
|
38 DavQuery davquery; |
|
39 davquery.command = DAV_QUERY_ERROR; |
|
40 davquery.command_data = NULL; |
|
41 sstr_t q = sstr(query); |
|
42 q = sstrtrim(q); |
|
43 |
|
44 // get query command |
|
45 sstr_t cmd; |
|
46 cmd.ptr = NULL; |
|
47 int i; |
|
48 for(i=0;i<q.length;i++) { |
|
49 if(q.ptr[i] == ' ') { |
|
50 cmd = sstrsubsl(q, 0, i); |
|
51 break; |
|
52 } |
|
53 } |
|
54 if(!cmd.ptr) { |
|
55 fprintf(stderr, "DQL syntax error\n"); |
|
56 return davquery; |
|
57 } |
|
58 |
|
59 cmd = sstrtrim(cmd); |
|
60 q = sstrtrim(sstrsubs(q, i)); |
|
61 if(!sstrcmp(cmd, S("get"))) { |
|
62 davquery.command = DAV_QUERY_GET; |
|
63 davquery.command_data = dav_ql_parse_get(q, ap); |
|
64 } |
|
65 |
|
66 return davquery; |
|
67 } |
|
68 |
|
69 DavGetQuery* dav_ql_parse_get(sstr_t q, va_list ap) { |
|
70 sstr_t property_query = q; |
|
71 q = util_getsubstr_until_token(q, S("from"), &property_query); |
|
72 |
|
73 sstr_t from_query = q; |
|
74 sstr_t cond = util_getsubstr_until_token(q, S("where"), &from_query); |
|
75 |
|
76 // insert variable values |
|
77 UcxBuffer *fbuf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND); |
|
78 int var = 0; |
|
79 for(int i=0;i<from_query.length;i++) { |
|
80 char c = from_query.ptr[i]; |
|
81 if(c == '%') { |
|
82 if(var) { |
|
83 ucx_buffer_putc(fbuf, '%'); // previous '%' |
|
84 } else { |
|
85 var = 1; |
|
86 } |
|
87 } else if(var) { |
|
88 switch(c) { |
|
89 case 's': { |
|
90 char *arg = va_arg(ap, char*); |
|
91 ucx_buffer_puts(fbuf, arg); |
|
92 break; |
|
93 } |
|
94 default: { |
|
95 ucx_buffer_putc(fbuf, '%'); |
|
96 ucx_buffer_putc(fbuf, c); |
|
97 } |
|
98 } |
|
99 var = 0; |
|
100 } else { |
|
101 ucx_buffer_putc(fbuf, c); |
|
102 } |
|
103 } |
|
104 |
|
105 DavGetQuery *getquery = malloc(sizeof(DavGetQuery)); |
|
106 getquery->properties = sstrdup(property_query); |
|
107 getquery->from = sstrn(fbuf->space, fbuf->pos); |
|
108 // TODO: condition |
|
109 return getquery; |
|
110 } |
|
111 |
|
112 void free_get_query(DavGetQuery *q) { |
|
113 free(q->from.ptr); |
|
114 free(q->properties.ptr); |
|
115 free(q); |
|
116 } |
|
117 |
|
118 int parse_path_query(sstr_t query, char **path, int *depth) { |
|
119 if(query.length == 1) { |
|
120 if(query.ptr[0] == '/') { |
|
121 *path = sstrdup(query).ptr; |
|
122 *depth = 1; |
|
123 return 0; |
|
124 } else { |
|
125 *path = NULL; |
|
126 return 1; |
|
127 } |
|
128 } |
|
129 |
|
130 if(query.ptr[query.length-1] == '*') { |
|
131 *depth = -1; |
|
132 *path = sstrdup(sstrsubsl(query, 0, query.length-1)).ptr; |
|
133 } else { |
|
134 *path = sstrdup(query).ptr; |
|
135 *depth = 1; |
|
136 } |
|
137 |
|
138 return 0; |
|
139 } |