2011-04-14 20:37:47 +02:00
|
|
|
/*
|
2011-05-09 20:11:40 +02:00
|
|
|
* $Id: query_parser.c 53 2011-05-09 16:55:39Z kaori $
|
2011-04-14 20:37:47 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
|
|
|
|
* Copyright (c) 2002-2011, Professor Benoit Macq
|
2011-10-10 11:50:18 +02:00
|
|
|
* Copyright (c) 2010-2011, Kaori Hagihara
|
|
|
|
* Copyright (c) 2011, Lucian Corlaciu, GSoC
|
2011-04-14 20:37:47 +02:00
|
|
|
* 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 OWNER 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.
|
|
|
|
*/
|
2011-05-09 20:11:40 +02:00
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#define strcasecmp _stricmp
|
2011-05-09 20:11:40 +02:00
|
|
|
#else
|
|
|
|
#include <strings.h>
|
2011-05-08 12:49:06 +02:00
|
|
|
#endif
|
|
|
|
|
2011-05-09 20:11:40 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2011-11-02 00:28:07 +01:00
|
|
|
#include <stdlib.h>
|
2011-05-09 20:11:40 +02:00
|
|
|
#include "query_parser.h"
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
#ifdef SERVER
|
|
|
|
#include "fcgi_stdio.h"
|
|
|
|
#define logstream FCGI_stdout
|
|
|
|
#else
|
|
|
|
#define FCGI_stdout stdout
|
|
|
|
#define FCGI_stderr stderr
|
|
|
|
#define logstream stderr
|
|
|
|
#endif //SERVER
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2011-11-02 01:49:20 +01:00
|
|
|
* Get initialized query parameters
|
2011-04-14 20:37:47 +02:00
|
|
|
*
|
2011-11-02 01:49:20 +01:00
|
|
|
* @return initial query parameters
|
2011-04-14 20:37:47 +02:00
|
|
|
*/
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param_t * get_initquery();
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
/*
|
|
|
|
* get a pair of field name and value from the string starting fieldname=fieldval&... format
|
|
|
|
*
|
|
|
|
* @param[in] stringptr pointer to the beginning of the parsing string
|
|
|
|
* @param[out] fieldname string to copy the field name, if not found, NULL
|
|
|
|
* @param[out] fieldval string to copy the field value, if not found, NULL
|
|
|
|
* @return pointer to the next field string, if there is none, NULL
|
|
|
|
*/
|
|
|
|
char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval);
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
/**
|
|
|
|
* parse string to string array
|
|
|
|
*
|
|
|
|
* @param[in] src src string
|
|
|
|
* @param[out] cclose parsed string array
|
|
|
|
*/
|
|
|
|
void str2cclose( char *src, char cclose[][MAX_LENOFCID]);
|
|
|
|
|
|
|
|
void parse_metareq( char *field, query_param_t *query_param);
|
|
|
|
|
2011-10-10 11:50:18 +02:00
|
|
|
// parse the requested components (parses forms like:a; a,b; a-b; a-b,c; a,b-c)
|
|
|
|
void parse_comps( char *field, query_param_t *query_param);
|
|
|
|
|
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
//! maximum length of field name
|
|
|
|
#define MAX_LENOFFIELDNAME 10
|
|
|
|
|
|
|
|
//! maximum length of field value
|
|
|
|
#define MAX_LENOFFIELDVAL 128
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param_t * parse_query( char *query_string)
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param_t *query_param;
|
2011-05-08 12:49:06 +02:00
|
|
|
char *pquery, fieldname[MAX_LENOFFIELDNAME], fieldval[MAX_LENOFFIELDVAL];
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param = get_initquery();
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
pquery = query_string;
|
|
|
|
|
|
|
|
while( pquery!=NULL) {
|
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
pquery = get_fieldparam( pquery, fieldname, fieldval);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
if( fieldname[0] != '\0'){
|
2011-04-14 20:37:47 +02:00
|
|
|
if( strcasecmp( fieldname, "target") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
strcpy( query_param->target,fieldval);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-08-27 14:53:38 +02:00
|
|
|
else if( strcasecmp( fieldname, "tid") == 0)
|
|
|
|
strcpy( query_param->tid, fieldval);
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
else if( strcasecmp( fieldname, "fsiz") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
sscanf( fieldval, "%d,%d", &query_param->fx, &query_param->fy);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "roff") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
sscanf( fieldval, "%d,%d", &query_param->rx, &query_param->ry);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "rsiz") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
sscanf( fieldval, "%d,%d", &query_param->rw, &query_param->rh);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
2011-11-02 00:28:07 +01:00
|
|
|
else if( strcasecmp( fieldname, "layers") == 0)
|
|
|
|
sscanf( fieldval, "%d", &query_param->layers);
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
else if( strcasecmp( fieldname, "cid") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
strcpy( query_param->cid, fieldval);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "cnew") == 0)
|
|
|
|
query_param->cnew = true;
|
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "cclose") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
str2cclose( fieldval, query_param->cclose);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "metareq") == 0)
|
2011-05-08 12:49:06 +02:00
|
|
|
parse_metareq( fieldval, query_param);
|
2011-10-10 11:50:18 +02:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "comps") == 0)
|
|
|
|
parse_comps( fieldval, query_param);
|
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "type") == 0){
|
|
|
|
if( strncasecmp( fieldval, "jpp-stream", 10) == 0)
|
|
|
|
query_param->return_type = JPPstream;
|
|
|
|
else if( strncasecmp( fieldval, "jpt-stream", 10) == 0)
|
|
|
|
query_param->return_type = JPTstream;
|
|
|
|
}
|
2011-11-02 00:28:07 +01:00
|
|
|
|
|
|
|
else if( strcasecmp( fieldname, "len") == 0)
|
|
|
|
sscanf( fieldval, "%d", &query_param->len);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 01:49:20 +01:00
|
|
|
return query_param;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param_t * get_initquery()
|
2011-04-14 20:37:47 +02:00
|
|
|
{
|
2011-11-02 01:49:20 +01:00
|
|
|
query_param_t *query;
|
2011-05-08 12:49:06 +02:00
|
|
|
int i;
|
|
|
|
|
2011-11-02 01:49:20 +01:00
|
|
|
query = (query_param_t *)malloc( sizeof(query_param_t));
|
|
|
|
|
|
|
|
query->target[0] = '\0';
|
|
|
|
query->tid[0] = '\0';
|
|
|
|
query->fx = -1;
|
|
|
|
query->fy = -1;
|
|
|
|
query->rx = -1;
|
|
|
|
query->ry = -1;
|
|
|
|
query->rw = -1;
|
|
|
|
query->rh = -1;
|
|
|
|
query->layers = -1;
|
|
|
|
query->lastcomp = -1;
|
|
|
|
query->comps = NULL;
|
|
|
|
query->cid[0] = '\0';
|
|
|
|
query->cnew = false;
|
|
|
|
memset( query->cclose, 0, MAX_NUMOFCCLOSE*MAX_LENOFCID);
|
|
|
|
memset( query->box_type, 0, MAX_NUMOFBOX*4);
|
|
|
|
memset( query->limit, 0, MAX_NUMOFBOX*sizeof(int));
|
2011-05-08 12:49:06 +02:00
|
|
|
for( i=0; i<MAX_NUMOFBOX; i++){
|
2011-11-02 01:49:20 +01:00
|
|
|
query->w[i] = false;
|
|
|
|
query->s[i] = false;
|
|
|
|
query->g[i] = false;
|
|
|
|
query->a[i] = false;
|
|
|
|
query->priority[i] = false;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
2011-11-02 01:49:20 +01:00
|
|
|
query->root_bin = 0;
|
|
|
|
query->max_depth = -1;
|
|
|
|
query->metadata_only = false;
|
|
|
|
query->return_type = UNKNOWN;
|
|
|
|
query->len = -1;
|
|
|
|
|
|
|
|
return query;
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
char * get_fieldparam( char *stringptr, char *fieldname, char *fieldval)
|
|
|
|
{
|
|
|
|
char *eqp, *andp, *nexfieldptr;
|
|
|
|
|
|
|
|
if((eqp = strchr( stringptr, '='))==NULL){
|
|
|
|
fprintf( stderr, "= not found\n");
|
|
|
|
strcpy( fieldname, "");
|
|
|
|
strcpy( fieldval, "");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if((andp = strchr( stringptr, '&'))==NULL){
|
|
|
|
andp = strchr( stringptr, '\0');
|
|
|
|
nexfieldptr = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nexfieldptr = andp+1;
|
|
|
|
|
|
|
|
strncpy( fieldname, stringptr, eqp-stringptr);
|
|
|
|
fieldname[eqp-stringptr]='\0';
|
|
|
|
strncpy( fieldval, eqp+1, andp-eqp-1);
|
|
|
|
fieldval[andp-eqp-1]='\0';
|
|
|
|
|
|
|
|
return nexfieldptr;
|
|
|
|
}
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
void print_queryparam( query_param_t query_param)
|
|
|
|
{
|
2011-05-08 12:49:06 +02:00
|
|
|
int i;
|
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
fprintf( logstream, "query parameters:\n");
|
|
|
|
fprintf( logstream, "\t target: %s\n", query_param.target);
|
2011-08-27 14:53:38 +02:00
|
|
|
fprintf( logstream, "\t tid: %s\n", query_param.tid);
|
2011-04-14 20:37:47 +02:00
|
|
|
fprintf( logstream, "\t fx,fy: %d, %d\n", query_param.fx, query_param.fy);
|
|
|
|
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);
|
2011-11-02 00:28:07 +01:00
|
|
|
fprintf( logstream, "\t layers: %d\n", query_param.layers);
|
2011-10-10 11:50:18 +02:00
|
|
|
fprintf( logstream, "\t components: ");
|
|
|
|
if( query_param.lastcomp == -1)
|
|
|
|
fprintf( logstream, "ALL\n");
|
|
|
|
else{
|
|
|
|
for( i=0; i<=query_param.lastcomp; i++)
|
|
|
|
if( query_param.comps[i])
|
|
|
|
fprintf( logstream, "%d ", i);
|
|
|
|
fprintf( logstream, "\n");
|
|
|
|
}
|
2011-04-14 20:37:47 +02:00
|
|
|
fprintf( logstream, "\t cnew: %d\n", query_param.cnew);
|
|
|
|
fprintf( logstream, "\t cid: %s\n", query_param.cid);
|
|
|
|
|
|
|
|
fprintf( logstream, "\t cclose: ");
|
2011-05-08 12:49:06 +02:00
|
|
|
for( i=0; query_param.cclose[i][0]!=0 && i<MAX_NUMOFCCLOSE; i++)
|
2011-04-14 20:37:47 +02:00
|
|
|
fprintf( logstream, "%s ", query_param.cclose[i]);
|
|
|
|
fprintf(logstream, "\n");
|
|
|
|
|
|
|
|
fprintf( logstream, "\t req-box-prop\n");
|
2011-05-08 12:49:06 +02:00
|
|
|
for( i=0; query_param.box_type[i][0]!=0 && i<MAX_NUMOFBOX; i++){
|
2011-04-14 20:37:47 +02:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf( logstream, "\t root-bin: %d\n", query_param.root_bin);
|
|
|
|
fprintf( logstream, "\t max-depth: %d\n", query_param.max_depth);
|
|
|
|
fprintf( logstream, "\t metadata-only: %d\n", query_param.metadata_only);
|
2011-10-10 11:50:18 +02:00
|
|
|
fprintf( logstream, "\t image return type: %d, [JPP-stream=0, JPT-stream=1, UNKNOWN=-1]\n", query_param.return_type);
|
2011-11-02 00:28:07 +01:00
|
|
|
fprintf( logstream, "\t len: %d\n", query_param.len);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void str2cclose( char *src, char cclose[][MAX_LENOFCID])
|
|
|
|
{
|
2011-05-08 12:49:06 +02:00
|
|
|
int i, u, v;
|
2011-05-09 20:11:40 +02:00
|
|
|
|
2011-04-14 20:37:47 +02:00
|
|
|
size_t len = strlen( src);
|
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
for( i=0, u=0, v=0; i<len; i++){
|
2011-04-14 20:37:47 +02:00
|
|
|
if( src[i]==','){
|
|
|
|
u++;
|
|
|
|
v=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cclose[u][v++] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param);
|
|
|
|
|
|
|
|
void parse_metareq( char *field, query_param_t *query_param)
|
|
|
|
{
|
|
|
|
char req_box_prop[20];
|
|
|
|
char *ptr, *src;
|
|
|
|
int numofboxreq = 0;
|
|
|
|
|
2011-05-08 12:49:06 +02:00
|
|
|
memset( req_box_prop, 0, 20);
|
2011-04-14 20:37:47 +02:00
|
|
|
|
|
|
|
// req-box-prop
|
|
|
|
ptr = strchr( field, '[');
|
|
|
|
ptr++;
|
|
|
|
src = ptr;
|
|
|
|
while( *ptr != ']'){
|
|
|
|
if( *ptr == ';'){
|
|
|
|
strncpy( req_box_prop, src, ptr-src);
|
|
|
|
parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
|
|
|
|
ptr++;
|
|
|
|
src = ptr;
|
2011-05-08 12:49:06 +02:00
|
|
|
memset( req_box_prop, 0, 20);
|
2011-04-14 20:37:47 +02:00
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
strncpy( req_box_prop, src, ptr-src);
|
|
|
|
|
|
|
|
parse_req_box_prop( req_box_prop, numofboxreq++, query_param);
|
|
|
|
|
|
|
|
if(( ptr = strchr( field, 'R')))
|
|
|
|
sscanf( ptr+1, "%d", &(query_param->root_bin));
|
|
|
|
|
|
|
|
if(( ptr = strchr( field, 'D')))
|
|
|
|
sscanf( ptr+1, "%d", &(query_param->max_depth));
|
|
|
|
|
|
|
|
if(( ptr = strstr( field, "!!")))
|
|
|
|
query_param->metadata_only = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void parse_req_box_prop( char *req_box_prop, int idx, query_param_t *query_param)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
if( *req_box_prop == '*')
|
|
|
|
query_param->box_type[idx][0]='*';
|
|
|
|
else
|
|
|
|
strncpy( query_param->box_type[idx], req_box_prop, 4);
|
|
|
|
|
|
|
|
if(( ptr = strchr( req_box_prop, ':'))){
|
|
|
|
if( *(ptr+1)=='r')
|
|
|
|
query_param->limit[idx] = -1;
|
|
|
|
else
|
|
|
|
sscanf( ptr+1, "%d", &(query_param->limit[idx]));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(( ptr = strchr( req_box_prop, '/'))){
|
|
|
|
ptr++;
|
|
|
|
while( *ptr=='w' || *ptr=='s' || *ptr=='g' || *ptr=='a'){
|
|
|
|
switch( *ptr){
|
|
|
|
case 'w': query_param->w[idx] = true; break;
|
|
|
|
case 's': query_param->s[idx] = true; break;
|
|
|
|
case 'g': query_param->g[idx] = true; break;
|
|
|
|
case 'a': query_param->a[idx] = true; break;
|
|
|
|
}
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
query_param->g[idx] = true;
|
|
|
|
query_param->s[idx] = true;
|
|
|
|
query_param->w[idx] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((ptr = strchr( req_box_prop, '!')))
|
|
|
|
query_param->priority[idx] = true;
|
|
|
|
|
|
|
|
idx++;
|
|
|
|
}
|
2011-10-10 11:50:18 +02:00
|
|
|
|
|
|
|
void parse_comps( char *field, query_param_t *query_param)
|
|
|
|
{
|
|
|
|
int i,start,stop,aux = -1;
|
|
|
|
char *ptr1,*ptr2;
|
|
|
|
|
|
|
|
ptr1 = strchr( field, '-');
|
|
|
|
ptr2 = strchr( field, ',');
|
|
|
|
|
|
|
|
if( ptr1 && ptr2)
|
|
|
|
if( ptr1 > ptr2)
|
|
|
|
sscanf( field, "%d,%d-%d",&aux, &start, &stop);
|
|
|
|
else
|
|
|
|
sscanf( field, "%d-%d,%d", &start, &stop, &aux);
|
|
|
|
else
|
|
|
|
if(ptr1)
|
|
|
|
sscanf( field, "%d-%d", &start, &stop);
|
|
|
|
else if(ptr2){
|
|
|
|
sscanf( field, "%d,%d", &start, &stop);
|
|
|
|
aux = start;
|
|
|
|
start = stop;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
sscanf( field, "%d", &stop);
|
|
|
|
start = stop;
|
|
|
|
}
|
|
|
|
|
|
|
|
query_param->lastcomp = stop > aux ? stop : aux;
|
|
|
|
query_param->comps = (bool *)calloc( 1, (query_param->lastcomp+1)*sizeof(bool));
|
|
|
|
|
|
|
|
for( i=start; i<=stop; i++)
|
|
|
|
query_param->comps[i]=true;
|
|
|
|
|
|
|
|
if(aux!=-1)
|
|
|
|
query_param->comps[aux] = true;
|
|
|
|
}
|
2011-11-02 01:49:20 +01:00
|
|
|
|
|
|
|
void delete_query( query_param_t **query)
|
|
|
|
{
|
|
|
|
free((*query)->comps);
|
|
|
|
free( *query);
|
|
|
|
}
|