mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
render: added error codes for missing method & modifier name; fixes for test tools
This commit is contained in:
parent
c9402a5a42
commit
e47139f059
5 changed files with 42 additions and 18 deletions
|
@ -23,7 +23,8 @@
|
||||||
*/
|
*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
//PSYC_RENDER_ERROR_ROUTING = -2,
|
PSYC_RENDER_ERROR_METHOD_MISSING = -3, ///< method missing, but data present
|
||||||
|
PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING = -2, ///< modifier name missing
|
||||||
PSYC_RENDER_ERROR = -1,
|
PSYC_RENDER_ERROR = -1,
|
||||||
PSYC_RENDER_SUCCESS = 0,
|
PSYC_RENDER_SUCCESS = 0,
|
||||||
} psycRenderRC;
|
} psycRenderRC;
|
||||||
|
|
26
src/render.c
26
src/render.c
|
@ -45,6 +45,9 @@ size_t psyc_renderModifier (psycModifier *mod, char *buffer)
|
||||||
buffer[cur++] = mod->oper;
|
buffer[cur++] = mod->oper;
|
||||||
memcpy(buffer + cur, mod->name.ptr, mod->name.length);
|
memcpy(buffer + cur, mod->name.ptr, mod->name.length);
|
||||||
cur += mod->name.length;
|
cur += mod->name.length;
|
||||||
|
if (cur <= 1)
|
||||||
|
return cur; // error, name can't be empty
|
||||||
|
|
||||||
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||||
{
|
{
|
||||||
buffer[cur++] = ' ';
|
buffer[cur++] = ' ';
|
||||||
|
@ -61,14 +64,19 @@ size_t psyc_renderModifier (psycModifier *mod, char *buffer)
|
||||||
|
|
||||||
psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
|
psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
|
||||||
{
|
{
|
||||||
size_t i, cur = 0;
|
size_t i, cur = 0, len;
|
||||||
|
|
||||||
if (packet->length > buflen)
|
if (packet->length > buflen)
|
||||||
return PSYC_RENDER_ERROR; // return error if packet doesn't fit in buffer
|
return PSYC_RENDER_ERROR; // return error if packet doesn't fit in buffer
|
||||||
|
|
||||||
// render routing modifiers
|
// render routing modifiers
|
||||||
for (i = 0; i < packet->routing.lines; i++)
|
for (i = 0; i < packet->routing.lines; i++)
|
||||||
cur += psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur);
|
{
|
||||||
|
len = psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur);
|
||||||
|
cur += len;
|
||||||
|
if (len <= 1)
|
||||||
|
return PSYC_RENDER_ERROR_MODIFIER_NAME_MISSING;
|
||||||
|
}
|
||||||
|
|
||||||
// add length if needed
|
// add length if needed
|
||||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
|
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
|
||||||
|
@ -86,14 +94,16 @@ psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen)
|
||||||
memcpy(buffer + cur, packet->method.ptr, packet->method.length);
|
memcpy(buffer + cur, packet->method.ptr, packet->method.length);
|
||||||
cur += packet->method.length;
|
cur += packet->method.length;
|
||||||
buffer[cur++] = '\n';
|
buffer[cur++] = '\n';
|
||||||
}
|
if (packet->data.length) // add data\n
|
||||||
|
{
|
||||||
|
memcpy(buffer + cur, packet->data.ptr, packet->data.length);
|
||||||
|
cur += packet->data.length;
|
||||||
|
|
||||||
if (packet->data.length) // add data\n
|
buffer[cur++] = '\n';
|
||||||
{
|
}
|
||||||
memcpy(buffer + cur, packet->data.ptr, packet->data.length);
|
|
||||||
cur += packet->data.length;
|
|
||||||
buffer[cur++] = '\n';
|
|
||||||
}
|
}
|
||||||
|
else if (packet->data.length) // error, we have data but no modifier
|
||||||
|
return PSYC_RENDER_ERROR_METHOD_MISSING;
|
||||||
|
|
||||||
// add packet delimiter
|
// add packet delimiter
|
||||||
buffer[cur++] = C_GLYPH_PACKET_DELIMITER;
|
buffer[cur++] = C_GLYPH_PACKET_DELIMITER;
|
||||||
|
|
|
@ -24,6 +24,7 @@ test: ${TARGETS}
|
||||||
./isRoutingVar
|
./isRoutingVar
|
||||||
./getVarType
|
./getVarType
|
||||||
for f in packets/full-*; do echo ">> $$f"; ./testParser $$f; done
|
for f in packets/full-*; do echo ">> $$f"; ./testParser $$f; done
|
||||||
|
for f in packets/full-*; do echo ">> $$f"; ./testParser $$f -r; done
|
||||||
|
|
||||||
testServer: CFLAGS := $(subst -std=c99,,${CFLAGS})
|
testServer: CFLAGS := $(subst -std=c99,,${CFLAGS})
|
||||||
|
|
||||||
|
@ -37,7 +38,7 @@ netstartv:
|
||||||
./testServer ${PORT} -v
|
./testServer ${PORT} -v
|
||||||
|
|
||||||
netstartrv:
|
netstartrv:
|
||||||
./testServer ${PORT} -r -v
|
./testServer ${PORT} -rv
|
||||||
|
|
||||||
nettest:
|
nettest:
|
||||||
for f in packets/full-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | diff -u $$f -; done
|
for f in packets/full-*; do echo ">> $$f"; cat $$f | nc localhost ${PORT} | diff -u $$f -; done
|
||||||
|
|
|
@ -7,12 +7,16 @@
|
||||||
|
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
int idx, ret, routing_only = argc > 2, verbose = argc > 3;
|
uint8_t routing_only = argc > 2 && memchr(argv[2], (int)'r', strlen(argv[2]));
|
||||||
|
uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2]));
|
||||||
|
int idx, ret;
|
||||||
char buffer[2048], oper;
|
char buffer[2048], oper;
|
||||||
psycString name, value, elem;
|
psycString name, value, elem;
|
||||||
psycParseState state;
|
psycParseState state;
|
||||||
psycParseListState listState;
|
psycParseListState listState;
|
||||||
|
|
||||||
|
if (argc < 2)
|
||||||
|
return -1;
|
||||||
int file = open(argv[1],O_RDONLY);
|
int file = open(argv[1],O_RDONLY);
|
||||||
if (file < 0)
|
if (file < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -55,7 +59,7 @@ int main (int argc, char **argv)
|
||||||
(int)name.length, name.ptr,
|
(int)name.length, name.ptr,
|
||||||
(int)value.length, value.ptr);
|
(int)value.length, value.ptr);
|
||||||
|
|
||||||
if (memcmp(name.ptr, "_list", 5) == 0)
|
if (name.length >= 5 && memcmp(name.ptr, "_list", 5) == 0)
|
||||||
{
|
{
|
||||||
if (verbose)
|
if (verbose)
|
||||||
printf(">> LIST START\n");
|
printf(">> LIST START\n");
|
||||||
|
|
|
@ -42,7 +42,8 @@ void *get_in_addr (struct sockaddr *sa)
|
||||||
int main (int argc, char **argv)
|
int main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
char *port = argc > 1 ? argv[1] : "4440";
|
char *port = argc > 1 ? argv[1] : "4440";
|
||||||
uint8_t routing_only = argc > 2, verbose = argc > 3;
|
uint8_t routing_only = argc > 2 && memchr(argv[2], (int)'r', strlen(argv[2]));
|
||||||
|
uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2]));
|
||||||
|
|
||||||
fd_set master; // master file descriptor list
|
fd_set master; // master file descriptor list
|
||||||
fd_set read_fds; // temp file descriptor list for select()
|
fd_set read_fds; // temp file descriptor list for select()
|
||||||
|
@ -249,11 +250,18 @@ int main (int argc, char **argv)
|
||||||
packets[i].flag = psyc_isParseContentLengthFound(&parsers[i]) ?
|
packets[i].flag = psyc_isParseContentLengthFound(&parsers[i]) ?
|
||||||
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
|
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
|
||||||
|
|
||||||
psyc_setPacketLength(&packets[i]);
|
if (routing_only)
|
||||||
psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE);
|
packets[i].method = psyc_newString(PSYC_C2ARG("_packet_content"));
|
||||||
|
|
||||||
if (send(i, sendbuf, packets[i].length, 0) == -1)
|
psyc_setPacketLength(&packets[i]);
|
||||||
perror("send");
|
|
||||||
|
if (psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS)
|
||||||
|
{
|
||||||
|
if (send(i, sendbuf, packets[i].length, 0) == -1)
|
||||||
|
perror("send error");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
perror("render error");
|
||||||
|
|
||||||
ret = -1;
|
ret = -1;
|
||||||
break;
|
break;
|
||||||
|
@ -314,7 +322,7 @@ int main (int argc, char **argv)
|
||||||
|
|
||||||
if (verbose)
|
if (verbose)
|
||||||
{
|
{
|
||||||
printf("%.*s", (int)pvalue->length, pvalue->ptr);
|
printf("[%.*s]", (int)pvalue->length, pvalue->ptr);
|
||||||
if (parsers[i].valueLength > pvalue->length)
|
if (parsers[i].valueLength > pvalue->length)
|
||||||
printf("...");
|
printf("...");
|
||||||
printf("\n");
|
printf("\n");
|
||||||
|
|
Loading…
Reference in a new issue