2019-04-09 13:23:40 +02:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
|
|
|
* Please, refer LICENSE file for legal information
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file query.c
|
|
|
|
* @author Ilja Kartašov <ik@lowenware.com>
|
|
|
|
* @brief HTTP POST/GET query stream parser source file
|
|
|
|
*
|
|
|
|
* @see https://lowenware.com/aisl/
|
|
|
|
*/
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2019-04-09 13:23:40 +02:00
|
|
|
#include "query.h"
|
|
|
|
|
|
|
|
|
|
|
|
aisl_status_t
|
|
|
|
aislx_query_init( aislx_query_t query,
|
2019-04-17 10:17:27 +02:00
|
|
|
size_t total,
|
|
|
|
aislx_query_on_var on_var,
|
|
|
|
void * p_ctx )
|
2019-04-09 13:23:40 +02:00
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
query->on_var = on_var;
|
|
|
|
query->p_ctx = p_ctx;
|
|
|
|
query->data = NULL;
|
|
|
|
query->size = 0;
|
|
|
|
query->total = 0;
|
|
|
|
query->separator = 0;
|
|
|
|
|
|
|
|
return AISL_SUCCESS;
|
2019-04-09 13:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
aislx_query_release(aislx_query_t query)
|
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
if (query->data)
|
|
|
|
free(query->data);
|
2019-04-09 13:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
aislx_query_notify(const char * key, uint32_t k_len,
|
2019-04-17 10:17:27 +02:00
|
|
|
const char * val, uint32_t v_len,
|
|
|
|
aislx_query_t query)
|
2019-04-09 13:23:40 +02:00
|
|
|
{
|
2019-04-17 10:17:27 +02:00
|
|
|
if (query->on_var) {
|
2019-04-16 14:26:37 +02:00
|
|
|
if (query->on_var(key, k_len, val, v_len, query->p_ctx) != 0)
|
2019-04-17 10:17:27 +02:00
|
|
|
query->on_var = NULL;
|
2019-04-16 14:26:37 +02:00
|
|
|
}
|
2019-04-09 13:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int32_t
|
|
|
|
aislx_query_process(aislx_query_t query, const char * data, int32_t length)
|
|
|
|
{
|
2019-04-17 10:17:27 +02:00
|
|
|
const char *i = data,
|
|
|
|
*key = data,
|
|
|
|
*val = NULL;
|
|
|
|
uint32_t k_len = 0;
|
2019-04-16 14:26:37 +02:00
|
|
|
size_t result;
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
for (result=0; result < length; result++) {
|
2019-04-16 14:26:37 +02:00
|
|
|
char c;
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
switch( (c = *i) ) {
|
|
|
|
case '=':
|
|
|
|
if (val)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
k_len = i-key;
|
|
|
|
val = i+1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '&':
|
|
|
|
case ';':
|
|
|
|
if (!val)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!query->separator)
|
|
|
|
query->separator = c;
|
|
|
|
|
|
|
|
if (query->separator == c) {
|
|
|
|
aislx_query_notify(key, k_len, val, i-val, query);
|
|
|
|
/* reset parser */
|
|
|
|
key = (c == ';') ? i+2 : i+1;
|
|
|
|
k_len = 0;
|
|
|
|
val = NULL;
|
2019-04-16 14:26:37 +02:00
|
|
|
break;
|
2019-04-17 10:17:27 +02:00
|
|
|
}
|
2019-04-16 14:26:37 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
if (query->total == result) {
|
2019-04-16 14:26:37 +02:00
|
|
|
aislx_query_notify(key, k_len, val, i-val, query);
|
2019-04-17 10:17:27 +02:00
|
|
|
} else {
|
2019-04-16 14:26:37 +02:00
|
|
|
result = (key > i) ? i - data : key - data;
|
|
|
|
}
|
|
|
|
|
|
|
|
query->total -= result;
|
|
|
|
|
|
|
|
return result;
|
2019-04-09 13:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
aisl_status_t
|
2019-04-17 10:17:27 +02:00
|
|
|
aislx_query_feed(aislx_query_t query, const char *data, int32_t length)
|
2019-04-09 13:23:40 +02:00
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
size_t processed;
|
2019-04-17 10:17:27 +02:00
|
|
|
const char *source;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
if (query->data) {
|
2019-04-16 14:26:37 +02:00
|
|
|
if (!(source = realloc(query->data, length + query->size)))
|
|
|
|
return AISL_MALLOC_ERROR;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
length += query->size;
|
2019-04-17 10:17:27 +02:00
|
|
|
query->data = (char*)source;
|
2019-04-16 14:26:37 +02:00
|
|
|
query->size = length;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
memcpy((char*)&source[query->size], data, length);
|
|
|
|
} else {
|
2019-04-16 14:26:37 +02:00
|
|
|
source = data;
|
2019-04-17 10:17:27 +02:00
|
|
|
}
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
processed = aislx_query_process(query, source, length);
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
if (processed < 0)
|
|
|
|
return AISL_INPUT_ERROR;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-17 10:17:27 +02:00
|
|
|
if (processed < length) {
|
|
|
|
char *buffer;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
length -= processed;
|
|
|
|
if (!(buffer = malloc(length)))
|
|
|
|
return AISL_MALLOC_ERROR;
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
memcpy(buffer, &source[processed], length);
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
if (query->data)
|
|
|
|
free(query->data);
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
query->data = buffer;
|
|
|
|
query->size = length;
|
|
|
|
}
|
2019-04-09 13:23:40 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
return AISL_SUCCESS;
|
2019-04-09 13:23:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|