2011-05-14 19:16:11 +00:00
|
|
|
#include <ctype.h>
|
2011-05-14 00:18:01 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-05-15 09:51:30 +00:00
|
|
|
#include <stdint.h>
|
2011-05-14 00:18:01 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
2011-05-14 19:16:11 +00:00
|
|
|
#include <getopt.h>
|
2011-05-14 00:18:01 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <psyc.h>
|
|
|
|
#include <psyc/parse.h>
|
|
|
|
#include <psyc/render.h>
|
|
|
|
#include <psyc/syntax.h>
|
|
|
|
|
2011-05-16 18:13:10 +00:00
|
|
|
#include "test.c"
|
2011-05-14 00:18:01 +00:00
|
|
|
|
|
|
|
// max size for routing & entity header
|
|
|
|
#define ROUTING_LINES 16
|
|
|
|
#define ENTITY_LINES 32
|
|
|
|
|
2011-05-14 16:21:00 +00:00
|
|
|
// cmd line args
|
2011-05-14 19:16:11 +00:00
|
|
|
char *filename, *port = "4440";
|
2011-05-15 19:12:17 +00:00
|
|
|
uint8_t verbose, stats;
|
|
|
|
uint8_t multiple, single, routing_only, no_render, quiet, progress;
|
2011-05-18 20:00:28 +00:00
|
|
|
size_t count = 1, recv_buf_size;
|
2011-05-14 16:21:00 +00:00
|
|
|
|
2011-10-31 19:26:47 +00:00
|
|
|
PsycParseState parsers[NUM_PARSERS];
|
|
|
|
PsycPacket packets[NUM_PARSERS];
|
|
|
|
PsycModifier routing[NUM_PARSERS][ROUTING_LINES];
|
|
|
|
PsycModifier entity[NUM_PARSERS][ENTITY_LINES];
|
2011-05-14 16:21:00 +00:00
|
|
|
|
2011-05-15 19:12:17 +00:00
|
|
|
int contbytes, exit_code;
|
2011-05-14 00:18:01 +00:00
|
|
|
|
2011-05-16 18:13:10 +00:00
|
|
|
static inline
|
2011-10-31 19:26:47 +00:00
|
|
|
void resetString (PsycString *s, uint8_t freeptr);
|
2011-05-16 18:13:10 +00:00
|
|
|
|
2011-05-15 20:48:51 +00:00
|
|
|
// initialize parser & packet variables
|
2011-05-14 00:18:01 +00:00
|
|
|
void test_init (int i) {
|
|
|
|
// reset parser state & packet
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_parse_state_init(&parsers[i], routing_only ?
|
|
|
|
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
|
2011-05-14 00:18:01 +00:00
|
|
|
|
2011-10-31 19:26:47 +00:00
|
|
|
memset(&packets[i], 0, sizeof(PsycPacket));
|
|
|
|
memset(&routing[i], 0, sizeof(PsycModifier) * ROUTING_LINES);
|
|
|
|
memset(&entity[i], 0, sizeof(PsycModifier) * ENTITY_LINES);
|
2011-05-14 00:18:01 +00:00
|
|
|
packets[i].routing.modifiers = routing[i];
|
|
|
|
packets[i].entity.modifiers = entity[i];
|
|
|
|
}
|
|
|
|
|
2011-05-15 20:48:51 +00:00
|
|
|
// parse & render input
|
2011-05-14 16:21:00 +00:00
|
|
|
int test_input (int i, char *recvbuf, size_t nbytes) {
|
2011-05-14 17:59:08 +00:00
|
|
|
int j, ret, retl, r;
|
2011-05-14 16:21:00 +00:00
|
|
|
char sendbuf[SEND_BUF_SIZE];
|
2011-05-15 20:48:51 +00:00
|
|
|
char *parsebuf = recvbuf - contbytes;
|
|
|
|
/* We have a buffer with pointers pointing to various parts of it:
|
|
|
|
* *contbuf-vv
|
|
|
|
* buffer: [ ccrrrr]
|
|
|
|
* *recvbuf---^^^^
|
|
|
|
* *parsebuf-^^^^^^
|
|
|
|
*
|
|
|
|
* New data is in recvbuf, if it contains an incomplete packet then remaining
|
|
|
|
* unparsed data is copied to contbuf that will be parsed during the next call
|
|
|
|
* to this function together with the new data.
|
|
|
|
*/
|
2011-05-14 16:21:00 +00:00
|
|
|
|
2011-10-31 19:26:47 +00:00
|
|
|
PsycParseState *parser = &parsers[i];
|
|
|
|
PsycPacket *packet = &packets[i];
|
2011-10-13 22:31:43 +00:00
|
|
|
|
2011-05-14 17:59:08 +00:00
|
|
|
char oper;
|
2011-10-31 19:26:47 +00:00
|
|
|
PsycString name, value, elem;
|
|
|
|
PsycString *pname = NULL, *pvalue = NULL;
|
|
|
|
PsycModifier *mod = NULL;
|
|
|
|
PsycParseListState listState;
|
2011-05-14 17:59:08 +00:00
|
|
|
size_t len;
|
|
|
|
|
2011-05-15 20:48:51 +00:00
|
|
|
// Set buffer with data for the parser.
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_parse_buffer_set(parser, parsebuf, contbytes + nbytes);
|
2011-05-14 00:18:01 +00:00
|
|
|
contbytes = 0;
|
|
|
|
oper = 0;
|
|
|
|
name.length = 0;
|
|
|
|
value.length = 0;
|
|
|
|
|
|
|
|
do {
|
2011-06-02 11:25:48 +00:00
|
|
|
if (verbose >= 3)
|
2011-11-01 11:06:58 +00:00
|
|
|
printf("\n# buffer = [%.*s]\n# part = %d\n", (int)parser->buffer.length, parser->buffer.data, parser->part);
|
2011-05-15 20:48:51 +00:00
|
|
|
// Parse the next part of the packet (a routing/entity modifier or the body)
|
2011-10-13 22:31:43 +00:00
|
|
|
ret = exit_code = psyc_parse(parser, &oper, &name, &value);
|
2011-05-14 00:18:01 +00:00
|
|
|
if (verbose >= 2)
|
|
|
|
printf("# ret = %d\n", ret);
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ROUTING:
|
2011-10-13 22:31:43 +00:00
|
|
|
assert(packet->routing.lines < ROUTING_LINES);
|
|
|
|
mod = &(packet->routing.modifiers[packet->routing.lines]);
|
2011-05-14 00:18:01 +00:00
|
|
|
pname = &mod->name;
|
|
|
|
pvalue = &mod->value;
|
|
|
|
mod->flag = PSYC_MODIFIER_ROUTING;
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->routing.lines++;
|
2011-05-14 00:18:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PSYC_PARSE_ENTITY_START:
|
|
|
|
case PSYC_PARSE_ENTITY_CONT:
|
|
|
|
case PSYC_PARSE_ENTITY_END:
|
|
|
|
case PSYC_PARSE_ENTITY:
|
2011-10-13 22:31:43 +00:00
|
|
|
assert(packet->entity.lines < ENTITY_LINES);
|
|
|
|
mod = &(packet->entity.modifiers[packet->entity.lines]);
|
2011-05-14 00:18:01 +00:00
|
|
|
pname = &mod->name;
|
|
|
|
pvalue = &mod->value;
|
|
|
|
|
|
|
|
if (ret == PSYC_PARSE_ENTITY || ret == PSYC_PARSE_ENTITY_END) {
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->entity.lines++;
|
2011-10-31 19:04:16 +00:00
|
|
|
mod->flag = psyc_parse_value_length_found(parser) ?
|
2011-05-14 00:18:01 +00:00
|
|
|
PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSYC_PARSE_BODY_START:
|
|
|
|
case PSYC_PARSE_BODY_CONT:
|
|
|
|
case PSYC_PARSE_BODY_END:
|
|
|
|
case PSYC_PARSE_BODY:
|
2011-10-13 22:31:43 +00:00
|
|
|
pname = &(packet->method);
|
|
|
|
pvalue = &(packet->data);
|
2011-05-14 00:18:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case PSYC_PARSE_COMPLETE:
|
|
|
|
if (verbose)
|
|
|
|
printf("# Done parsing.\n");
|
|
|
|
else if (progress)
|
2011-05-14 17:59:08 +00:00
|
|
|
r = write(1, ".", 1);
|
2011-05-15 19:12:17 +00:00
|
|
|
if ((filename && !multiple) || (!filename && single))
|
2011-05-14 00:18:01 +00:00
|
|
|
ret = -1;
|
|
|
|
|
|
|
|
if (!no_render) {
|
2011-10-31 19:04:16 +00:00
|
|
|
packet->flag = psyc_parse_content_length_found(parser) ?
|
2011-05-14 00:18:01 +00:00
|
|
|
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
|
|
|
|
|
|
|
|
if (routing_only) {
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->content = packet->data;
|
|
|
|
resetString(&(packet->data), 0);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_packet_length_set(packet);
|
2011-05-14 00:18:01 +00:00
|
|
|
|
2011-10-13 22:31:43 +00:00
|
|
|
if (psyc_render(packet, sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) {
|
2011-05-15 09:51:30 +00:00
|
|
|
if (!quiet) {
|
2011-10-13 22:31:43 +00:00
|
|
|
if (filename && write(1, sendbuf, packet->length) == -1) {
|
2011-05-15 09:51:30 +00:00
|
|
|
perror("write");
|
|
|
|
ret = -1;
|
2011-10-13 22:31:43 +00:00
|
|
|
} else if (!filename && send(i, sendbuf, packet->length, 0) == -1) {
|
2011-05-15 09:51:30 +00:00
|
|
|
perror("send");
|
|
|
|
ret = -1;
|
|
|
|
}
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-05-14 17:59:08 +00:00
|
|
|
printf("# Render error");
|
2011-05-14 00:18:01 +00:00
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset packet
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->routingLength = 0;
|
|
|
|
packet->contentLength = 0;
|
|
|
|
packet->length = 0;
|
|
|
|
packet->flag = 0;
|
|
|
|
|
|
|
|
for (j = 0; j < packet->routing.lines; j++) {
|
|
|
|
resetString(&(packet->routing.modifiers[j].name), 1);
|
|
|
|
resetString(&(packet->routing.modifiers[j].value), 1);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->routing.lines = 0;
|
2011-05-14 00:18:01 +00:00
|
|
|
|
|
|
|
if (routing_only) {
|
2011-10-13 22:31:43 +00:00
|
|
|
resetString(&(packet->content), 1);
|
2011-05-14 00:18:01 +00:00
|
|
|
} else {
|
2011-10-13 22:31:43 +00:00
|
|
|
for (j = 0; j < packet->entity.lines; j++) {
|
|
|
|
resetString(&(packet->entity.modifiers[j].name), 1);
|
|
|
|
resetString(&(packet->entity.modifiers[j].value), 1);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
2011-10-13 22:31:43 +00:00
|
|
|
packet->entity.lines = 0;
|
2011-05-14 00:18:01 +00:00
|
|
|
|
2011-10-13 22:31:43 +00:00
|
|
|
resetString(&(packet->method), 1);
|
|
|
|
resetString(&(packet->data), 1);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PSYC_PARSE_INSUFFICIENT:
|
|
|
|
if (verbose >= 2)
|
|
|
|
printf("# Insufficient data.\n");
|
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
contbytes = psyc_parse_remaining_length(parser);
|
2011-05-14 00:18:01 +00:00
|
|
|
|
|
|
|
if (contbytes > 0) { // copy end of parsebuf before start of recvbuf
|
2011-06-02 13:07:11 +00:00
|
|
|
if (verbose >= 3)
|
2011-10-31 19:04:16 +00:00
|
|
|
printf("# remaining = [%.*s]\n", (int)contbytes, psyc_parse_remaining_buffer(parser));
|
2011-05-14 16:21:00 +00:00
|
|
|
assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer
|
2011-10-31 19:04:16 +00:00
|
|
|
memmove(recvbuf - contbytes, psyc_parse_remaining_buffer(parser), contbytes);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("# Error while parsing: %i\n", ret);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ENTITY_START:
|
|
|
|
case PSYC_PARSE_ENTITY_CONT:
|
|
|
|
case PSYC_PARSE_BODY_START:
|
|
|
|
case PSYC_PARSE_BODY_CONT:
|
|
|
|
ret = 0;
|
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
case PSYC_PARSE_ENTITY_END:
|
|
|
|
case PSYC_PARSE_ROUTING:
|
|
|
|
case PSYC_PARSE_BODY:
|
|
|
|
case PSYC_PARSE_BODY_END:
|
|
|
|
if (oper) {
|
|
|
|
mod->oper = oper;
|
|
|
|
if (verbose >= 2)
|
|
|
|
printf("%c", oper);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.length) {
|
2011-11-01 11:06:58 +00:00
|
|
|
pname->data = malloc(name.length);
|
2011-05-14 00:18:01 +00:00
|
|
|
pname->length = name.length;
|
|
|
|
|
2011-11-01 11:06:58 +00:00
|
|
|
assert(pname->data != NULL);
|
|
|
|
memcpy((void*)pname->data, name.data, name.length);
|
2011-05-14 00:18:01 +00:00
|
|
|
name.length = 0;
|
|
|
|
|
|
|
|
if (verbose >= 2)
|
2011-11-01 11:06:58 +00:00
|
|
|
printf("%.*s = ", (int)pname->length, pname->data);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.length) {
|
|
|
|
if (!pvalue->length) {
|
2011-10-31 19:04:16 +00:00
|
|
|
if (psyc_parse_value_length_found(parser))
|
|
|
|
len = psyc_parse_value_length(parser);
|
2011-05-14 00:18:01 +00:00
|
|
|
else
|
|
|
|
len = value.length;
|
2011-11-01 11:06:58 +00:00
|
|
|
pvalue->data = malloc(len);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
2011-11-01 11:06:58 +00:00
|
|
|
assert(pvalue->data != NULL);
|
|
|
|
memcpy((void*)pvalue->data + pvalue->length, value.data, value.length);
|
2011-05-14 00:18:01 +00:00
|
|
|
pvalue->length += value.length;
|
|
|
|
value.length = 0;
|
|
|
|
|
|
|
|
if (verbose >= 2) {
|
2011-11-01 11:06:58 +00:00
|
|
|
printf("[%.*s]", (int)pvalue->length, pvalue->data);
|
2011-10-13 22:31:43 +00:00
|
|
|
if (parser->valueLength > pvalue->length)
|
2011-05-14 00:18:01 +00:00
|
|
|
printf("...");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (verbose)
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (verbose >= 3)
|
|
|
|
printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n",
|
|
|
|
pname->length, pvalue->length,
|
2011-10-13 22:31:43 +00:00
|
|
|
parser->contentParsed, parser->routingLength);
|
2011-05-14 00:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (ret) {
|
|
|
|
case PSYC_PARSE_ROUTING:
|
|
|
|
case PSYC_PARSE_ENTITY:
|
|
|
|
case PSYC_PARSE_ENTITY_END:
|
|
|
|
oper = 0;
|
|
|
|
name.length = 0;
|
|
|
|
value.length = 0;
|
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
if (psyc_var_is_list(PSYC_S2ARG(*pname))) {
|
2011-05-14 00:18:01 +00:00
|
|
|
if (verbose >= 2)
|
|
|
|
printf("## LIST START\n");
|
|
|
|
|
2011-10-31 19:04:16 +00:00
|
|
|
psyc_parse_list_state_init(&listState);
|
|
|
|
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(*pvalue));
|
2011-05-14 00:18:01 +00:00
|
|
|
|
|
|
|
do {
|
2011-10-31 19:04:16 +00:00
|
|
|
retl = psyc_parse_list(&listState, &elem);
|
2011-05-14 00:18:01 +00:00
|
|
|
switch (retl) {
|
|
|
|
case PSYC_PARSE_LIST_END:
|
|
|
|
retl = 0;
|
|
|
|
case PSYC_PARSE_LIST_ELEM:
|
|
|
|
if (verbose >= 2) {
|
2011-11-01 11:06:58 +00:00
|
|
|
printf("|%.*s\n", (int)elem.length, elem.data);
|
2011-05-14 00:18:01 +00:00
|
|
|
if (ret == PSYC_PARSE_LIST_END)
|
|
|
|
printf("## LIST END");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("# Error while parsing list: %i\n", retl);
|
|
|
|
ret = retl = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (retl > 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (ret > 0);
|
2011-05-14 16:21:00 +00:00
|
|
|
|
|
|
|
if (progress)
|
2011-05-14 17:59:08 +00:00
|
|
|
r = write(1, " ", 1);
|
2011-05-14 16:21:00 +00:00
|
|
|
|
2011-05-14 00:18:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2011-05-15 20:48:51 +00:00
|
|
|
|
2011-05-20 00:53:56 +00:00
|
|
|
static inline
|
2011-10-31 19:26:47 +00:00
|
|
|
void resetString (PsycString *s, uint8_t freeptr)
|
2011-05-15 20:48:51 +00:00
|
|
|
{
|
|
|
|
if (freeptr && s->length)
|
2011-11-01 11:06:58 +00:00
|
|
|
free((void*)s->data);
|
2011-05-15 20:48:51 +00:00
|
|
|
|
2011-11-01 11:06:58 +00:00
|
|
|
s->data = NULL;
|
2011-05-15 20:48:51 +00:00
|
|
|
s->length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main (int argc, char **argv) {
|
|
|
|
int c;
|
2011-05-16 22:27:26 +00:00
|
|
|
while ((c = getopt (argc, argv, "f:p:b:c:mnqrsvPSh")) != -1) {
|
2011-05-15 20:48:51 +00:00
|
|
|
switch (c) {
|
2011-05-16 22:27:26 +00:00
|
|
|
CASE_f CASE_p CASE_b CASE_c
|
|
|
|
CASE_m CASE_n CASE_q CASE_r
|
|
|
|
CASE_s CASE_v CASE_S CASE_P
|
2011-05-15 20:48:51 +00:00
|
|
|
case 'h':
|
|
|
|
printf(
|
2011-11-09 18:01:05 +00:00
|
|
|
HELP_FILE("test_psyc", "mnqrSsvP")
|
|
|
|
HELP_PORT("test_psyc", "nqrsvP")
|
2011-05-16 22:27:26 +00:00
|
|
|
HELP_f HELP_p HELP_b HELP_c
|
|
|
|
HELP_m HELP_n HELP_r
|
|
|
|
HELP_q HELP_S HELP_s
|
|
|
|
HELP_v HELP_P HELP_h,
|
2011-05-15 20:48:51 +00:00
|
|
|
port, RECV_BUF_SIZE);
|
|
|
|
exit(0);
|
|
|
|
case '?': exit(-1);
|
|
|
|
default: abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename)
|
|
|
|
test_file(filename, count, recv_buf_size);
|
|
|
|
else
|
|
|
|
test_server(port, count, recv_buf_size);
|
|
|
|
|
|
|
|
return exit_code;
|
|
|
|
}
|