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
a11c632e3d
commit
4394aa5e0c
5 changed files with 42 additions and 18 deletions
|
@ -23,7 +23,8 @@
|
|||
*/
|
||||
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_SUCCESS = 0,
|
||||
} 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;
|
||||
memcpy(buffer + cur, mod->name.ptr, 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)
|
||||
{
|
||||
buffer[cur++] = ' ';
|
||||
|
@ -61,14 +64,19 @@ size_t psyc_renderModifier (psycModifier *mod, char *buffer)
|
|||
|
||||
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)
|
||||
return PSYC_RENDER_ERROR; // return error if packet doesn't fit in buffer
|
||||
|
||||
// render routing modifiers
|
||||
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
|
||||
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);
|
||||
cur += packet->method.length;
|
||||
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
|
||||
{
|
||||
memcpy(buffer + cur, packet->data.ptr, packet->data.length);
|
||||
cur += packet->data.length;
|
||||
buffer[cur++] = '\n';
|
||||
buffer[cur++] = '\n';
|
||||
}
|
||||
}
|
||||
else if (packet->data.length) // error, we have data but no modifier
|
||||
return PSYC_RENDER_ERROR_METHOD_MISSING;
|
||||
|
||||
// add packet delimiter
|
||||
buffer[cur++] = C_GLYPH_PACKET_DELIMITER;
|
||||
|
|
|
@ -24,6 +24,7 @@ test: ${TARGETS}
|
|||
./isRoutingVar
|
||||
./getVarType
|
||||
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})
|
||||
|
||||
|
@ -37,7 +38,7 @@ netstartv:
|
|||
./testServer ${PORT} -v
|
||||
|
||||
netstartrv:
|
||||
./testServer ${PORT} -r -v
|
||||
./testServer ${PORT} -rv
|
||||
|
||||
nettest:
|
||||
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 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;
|
||||
psycString name, value, elem;
|
||||
psycParseState state;
|
||||
psycParseListState listState;
|
||||
|
||||
if (argc < 2)
|
||||
return -1;
|
||||
int file = open(argv[1],O_RDONLY);
|
||||
if (file < 0)
|
||||
return -1;
|
||||
|
@ -55,7 +59,7 @@ int main (int argc, char **argv)
|
|||
(int)name.length, name.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)
|
||||
printf(">> LIST START\n");
|
||||
|
|
|
@ -42,7 +42,8 @@ void *get_in_addr (struct sockaddr *sa)
|
|||
int main (int argc, char **argv)
|
||||
{
|
||||
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 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]) ?
|
||||
PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH;
|
||||
|
||||
psyc_setPacketLength(&packets[i]);
|
||||
psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE);
|
||||
if (routing_only)
|
||||
packets[i].method = psyc_newString(PSYC_C2ARG("_packet_content"));
|
||||
|
||||
if (send(i, sendbuf, packets[i].length, 0) == -1)
|
||||
perror("send");
|
||||
psyc_setPacketLength(&packets[i]);
|
||||
|
||||
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;
|
||||
break;
|
||||
|
@ -314,7 +322,7 @@ int main (int argc, char **argv)
|
|||
|
||||
if (verbose)
|
||||
{
|
||||
printf("%.*s", (int)pvalue->length, pvalue->ptr);
|
||||
printf("[%.*s]", (int)pvalue->length, pvalue->ptr);
|
||||
if (parsers[i].valueLength > pvalue->length)
|
||||
printf("...");
|
||||
printf("\n");
|
||||
|
|
Loading…
Reference in a new issue