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-25 13:54:07 +02:00
|
|
|
#include "log.h"
|
2019-04-09 13:23:40 +02:00
|
|
|
#include "query.h"
|
|
|
|
|
|
|
|
|
2019-04-25 13:54:07 +02:00
|
|
|
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;
|
2019-04-25 13:54:07 +02:00
|
|
|
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
|
2019-04-25 13:54:07 +02:00
|
|
|
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
|
2019-04-25 13:54:07 +02:00
|
|
|
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
|
|
|
{
|
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
|
2019-04-25 13:54:07 +02:00
|
|
|
ax_query_process(AxQuery query, const char *data, int32_t length)
|
2019-04-09 13:23:40 +02:00
|
|
|
{
|
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) {
|
2019-04-25 13:54:07 +02:00
|
|
|
ax_query_notify(key, k_len, val, i-val, query);
|
2019-04-17 10:17:27 +02:00
|
|
|
/* 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-25 13:54:07 +02:00
|
|
|
ax_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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-25 13:54:07 +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;
|
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-25 13:54:07 +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
|
|
|
|
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;
|
2019-04-25 13:54:07 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-04-29 09:08:18 +02:00
|
|
|
void
|
|
|
|
ax_query__decode(char *e_str)
|
|
|
|
{
|
|
|
|
char *p = e_str,
|
|
|
|
hex[3] = {0,0,0};
|
|
|
|
do {
|
|
|
|
switch(*e_str) {
|
|
|
|
case '%':
|
|
|
|
hex[0] = *(++e_str);
|
|
|
|
hex[1] = *(++e_str);
|
|
|
|
*p++ = (char)strtol(hex, NULL, 16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '+':
|
|
|
|
*p++ = ' ';
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
*p++ = *e_str;
|
|
|
|
}
|
|
|
|
} while(*e_str++ != 0);
|
|
|
|
}
|
|
|
|
|