aisl-sdk/components/query.c

155 lines
2.7 KiB
C
Raw Normal View History

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/
*/
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include "log.h"
2019-04-09 13:23:40 +02:00
#include "query.h"
AislStatus
ax_query_init(AxQuery query, size_t total, AxQueryHandler 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 = total;
2019-04-16 14:26:37 +02:00
query->separator = 0;
return AISL_SUCCESS;
2019-04-09 13:23:40 +02:00
}
void
ax_query_release(AxQuery query)
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
}
static void
ax_query_notify(const char *key, uint32_t k_len,
const char *val, uint32_t v_len,
AxQuery query)
2019-04-09 13:23:40 +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)
query->on_var = NULL;
2019-04-16 14:26:37 +02:00
}
2019-04-09 13:23:40 +02:00
}
static int32_t
ax_query_process(AxQuery query, const char *data, int32_t length)
2019-04-09 13:23:40 +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;
for (result=0; result < length; result++) {
2019-04-16 14:26:37 +02:00
char c;
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) {
ax_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-16 14:26:37 +02:00
}
i++;
}
if (query->total == result) {
ax_query_notify(key, k_len, val, i-val, query);
} 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
}
AislStatus
ax_query_feed(AxQuery 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;
const char *source;
2019-04-09 13:23:40 +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;
query->data = (char*)source;
2019-04-16 14:26:37 +02:00
query->size = length;
2019-04-09 13:23:40 +02:00
memcpy((char*)&source[query->size], data, length);
} else {
2019-04-16 14:26:37 +02:00
source = data;
}
2019-04-09 13:23:40 +02:00
processed = ax_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
if (processed < length) {
char *buffer;
2019-04-09 13:23:40 +02:00
2019-04-16 14:26:37 +02:00
length -= processed;
2019-04-16 14:26:37 +02:00
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
}