2 * $Id: query_parser.c 53 2011-05-09 16:55:39Z kaori $
4 * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5 * Copyright (c) 2002-2011, Professor Benoit Macq
6 * Copyright (c) 2010-2011, Kaori Hagihara
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
34 #define strcasecmp _stricmp
41 #include "query_parser.h"
44 #include "fcgi_stdio.h"
45 #define logstream FCGI_stdout
47 #define FCGI_stdout stdout
48 #define FCGI_stderr stderr
49 #define logstream stderr
54 * initialize query parameters
56 * @param[in,out] query_param query parameters
58 void init_queryparam( query_param_t *query_param);
61 * get a pair of field name and value from the string starting fieldname=fieldval&... format
63 * @param[in] stringptr pointer to the beginning of the parsing string
64 * @param[out] fieldname string to copy the field name, if not found, NULL
65 * @param[out] fieldval string to copy the field value, if not found, NULL
66 * @return pointer to the next field string, if there is none, NULL
68 char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval);
71 * parse string to string array
73 * @param[in] src src string
74 * @param[out] cclose parsed string array
76 void str2cclose( char *src, char cclose[][MAX_LENOFCID]);
78 void parse_metareq( char *field, query_param_t *query_param);
80 //! maximum length of field name
81 #define MAX_LENOFFIELDNAME 10
83 //! maximum length of field value
84 #define MAX_LENOFFIELDVAL 128
86 void parse_query( char *query_string, query_param_t *query_param)
88 char *pquery, fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL];
90 init_queryparam( query_param);
92 pquery = query_string;
94 while( pquery!=NULL) {
96 pquery = get_fieldparam( pquery, fieldname, fieldval);
98 if( fieldname[0] != '\0'){
99 if( strcasecmp( fieldname, "target") == 0)
100 strcpy( query_param->target,fieldval);
102 else if( strcasecmp( fieldname, "fsiz") == 0)
103 sscanf( fieldval, "%d,%d", &query_param->fx, &query_param->fy);
105 else if( strcasecmp( fieldname, "roff") == 0)
106 sscanf( fieldval, "%d,%d", &query_param->rx, &query_param->ry);
108 else if( strcasecmp( fieldname, "rsiz") == 0)
109 sscanf( fieldval, "%d,%d", &query_param->rw, &query_param->rh);
111 else if( strcasecmp( fieldname, "cid") == 0)
112 strcpy( query_param->cid, fieldval);
114 else if( strcasecmp( fieldname, "cnew") == 0)
115 query_param->cnew = true;
117 else if( strcasecmp( fieldname, "cclose") == 0)
118 str2cclose( fieldval, query_param->cclose);
120 else if( strcasecmp( fieldname, "metareq") == 0)
121 parse_metareq( fieldval, query_param);
126 void init_queryparam( query_param_t *query_param)
130 query_param->target[0]='\0';
137 query_param->cid[0]='\0';
138 query_param->cnew=false;
139 memset( query_param->cclose, 0, MAX_NUMOFCCLOSE*MAX_LENOFCID);
140 memset( query_param->box_type, 0, MAX_NUMOFBOX*4);
141 memset( query_param->limit, 0, MAX_NUMOFBOX*sizeof(int));
142 for( i=0; i<MAX_NUMOFBOX; i++){
143 query_param->w[i] = false;
144 query_param->s[i] = false;
145 query_param->g[i] = false;
146 query_param->a[i] = false;
147 query_param->priority[i] = false;
149 query_param->root_bin = 0;
150 query_param->max_depth = -1;
151 query_param->metadata_only = false;
155 char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval)
157 char *eqp, *andp, *nexfieldptr;
159 if((eqp = strchr( stringptr, '='))==NULL){
160 fprintf( stderr, "= not found\n");
161 strcpy( fieldname, "");
162 strcpy( fieldval, "");
165 if((andp = strchr( stringptr, '&'))==NULL){
166 andp = strchr( stringptr, '\0');
170 nexfieldptr = andp+1;
172 strncpy( fieldname, stringptr, eqp-stringptr);
173 fieldname[eqp-stringptr]='\0';
174 strncpy( fieldval, eqp+1, andp-eqp-1);
175 fieldval[andp-eqp-1]='\0';
180 void print_queryparam( query_param_t query_param)
184 fprintf( logstream, "query parameters:\n");
185 fprintf( logstream, "\t target: %s\n", query_param.target);
186 fprintf( logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy);
187 fprintf( logstream, "\t rx,ry: %d, %d \t rw,rh: %d, %d\n", query_param.rx, query_param.ry, query_param.rw, query_param.rh);
188 fprintf( logstream, "\t cnew: %d\n", query_param.cnew);
189 fprintf( logstream, "\t cid: %s\n", query_param.cid);
191 fprintf( logstream, "\t cclose: ");
192 for( i=0; query_param.cclose[i][0]!=0 && i<MAX_NUMOFCCLOSE; i++)
193 fprintf( logstream, "%s ", query_param.cclose[i]);
194 fprintf(logstream, "\n");
196 fprintf( logstream, "\t req-box-prop\n");
197 for( i=0; query_param.box_type[i][0]!=0 && i<MAX_NUMOFBOX; i++){
198 fprintf( logstream, "\t\t box_type: %.4s limit: %d w:%d s:%d g:%d a:%d priority:%d\n", query_param.box_type[i], query_param.limit[i], query_param.w[i], query_param.s[i], query_param.g[i], query_param.a[i], query_param.priority[i]);
201 fprintf( logstream, "\t root-bin: %d\n", query_param.root_bin);
202 fprintf( logstream, "\t max-depth: %d\n", query_param.max_depth);
203 fprintf( logstream, "\t metadata-only: %d\n", query_param.metadata_only);
206 void str2cclose( char *src, char cclose[][MAX_LENOFCID])
210 size_t len = strlen( src);
212 for( i=0, u=0, v=0; i<len; i++){
218 cclose[u][v++] = src[i];
222 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param);
224 void parse_metareq( char *field, query_param_t *query_param)
226 char req_box_prop[20];
230 memset( req_box_prop, 0, 20);
233 ptr = strchr( field, '[');
238 strncpy( req_box_prop, src, ptr-src);
239 parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
242 memset( req_box_prop, 0, 20);
246 strncpy( req_box_prop, src, ptr-src);
248 parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
250 if(( ptr = strchr( field, 'R')))
251 sscanf( ptr+1, "%d", &(query_param->root_bin));
253 if(( ptr = strchr( field, 'D')))
254 sscanf( ptr+1, "%d", &(query_param->max_depth));
256 if(( ptr = strstr( field, "!!")))
257 query_param->metadata_only = true;
260 void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param)
264 if( *req_box_prop == '*')
265 query_param->box_type[idx][0]='*';
267 strncpy( query_param->box_type[idx], req_box_prop, 4);
269 if(( ptr = strchr( req_box_prop, ':'))){
271 query_param->limit[idx] = -1;
273 sscanf( ptr+1, "%d", &(query_param->limit[idx]));
276 if(( ptr = strchr( req_box_prop, '/'))){
278 while( *ptr=='w' || *ptr=='s' || *ptr=='g' || *ptr=='a'){
280 case 'w': query_param->w[idx] = true; break;
281 case 's': query_param->s[idx] = true; break;
282 case 'g': query_param->g[idx] = true; break;
283 case 'a': query_param->a[idx] = true; break;
289 query_param->g[idx] = true;
290 query_param->s[idx] = true;
291 query_param->w[idx] = true;
294 if((ptr = strchr( req_box_prop, '!')))
295 query_param->priority[idx] = true;