mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
fixed content length issues in parser & renderer
This commit is contained in:
parent
4f61f07ff2
commit
b898965e47
5 changed files with 25 additions and 22 deletions
35
src/packet.c
35
src/packet.c
|
@ -113,23 +113,30 @@ inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag f
|
|||
|
||||
inline psycPacketFlag psyc_checkPacketLength(psycPacket *p)
|
||||
{
|
||||
psycPacketFlag flag;
|
||||
|
||||
if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER)
|
||||
flag = PSYC_PACKET_NEED_LENGTH;
|
||||
else if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD)
|
||||
flag = PSYC_PACKET_NEED_LENGTH;
|
||||
else if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
||||
flag = PSYC_PACKET_NEED_LENGTH;
|
||||
else
|
||||
flag = PSYC_PACKET_NO_LENGTH;
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
||||
return flag;
|
||||
if (p->data.length > PSYC_CONTENT_SIZE_THRESHOLD)
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
||||
int i;
|
||||
// if any entity modifiers need length it is possible they contain
|
||||
// a packet terminator, thus the content should have a length as well
|
||||
for (i = 0; i < p->entity.lines; i++)
|
||||
if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
||||
if (memmem(p->data.ptr, p->data.length, PSYC_C2ARG(PSYC_PACKET_DELIMITER)))
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
||||
return PSYC_PACKET_NO_LENGTH;
|
||||
}
|
||||
|
||||
inline size_t psyc_setPacketLength(psycPacket *p)
|
||||
{
|
||||
size_t i;
|
||||
p->routingLength = 0;
|
||||
p->contentLength = 0;
|
||||
|
||||
// add routing header length
|
||||
for (i = 0; i < p->routing.lines; i++)
|
||||
|
@ -145,14 +152,14 @@ inline size_t psyc_setPacketLength(psycPacket *p)
|
|||
if (p->data.length)
|
||||
p->contentLength += p->data.length + 1; // data\n
|
||||
|
||||
// set total length: routing-header \n content |\n
|
||||
p->length = p->routingLength + p->contentLength + sizeof(PSYC_PACKET_DELIMITER) - 2;
|
||||
// set total length: routing-header content |\n
|
||||
p->length = p->routingLength + p->contentLength + 2;
|
||||
|
||||
if (p->contentLength > 0)
|
||||
{
|
||||
p->contentLength--; // subtract the \n from the delimiter, as that doesn't belong to the content part
|
||||
p->length++; // add \n at the start of the content part
|
||||
if (p->flag == PSYC_PACKET_NEED_LENGTH) // add length of length if needed
|
||||
p->length += log10((double)p->data.length) + 1;
|
||||
p->length += log10((double)p->contentLength) + 1;
|
||||
}
|
||||
|
||||
return p->length;
|
||||
|
|
|
@ -416,10 +416,9 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
|
|||
if (state->cursor >= state->buffer.length)
|
||||
return PSYC_PARSE_BODY;
|
||||
|
||||
if (state->buffer.ptr[state->cursor] != '\n')
|
||||
if (state->buffer.ptr[state->cursor] != '|')
|
||||
return PSYC_PARSE_ERROR_BODY;
|
||||
|
||||
state->cursor++;
|
||||
state->part = PSYC_PART_END;
|
||||
return PSYC_PARSE_BODY;
|
||||
}
|
||||
|
|
|
@ -47,7 +47,6 @@ inline size_t psyc_renderModifier(psycModifier *mod, char *buffer)
|
|||
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||
{
|
||||
buffer[cur++] = ' ';
|
||||
//cur += sprintf(buffer + cur, "%ld", mod->value.length);
|
||||
cur += itoa(mod->value.length, buffer + cur, 10);
|
||||
}
|
||||
|
||||
|
@ -71,10 +70,8 @@ psycRenderRC psyc_render(psycPacket *packet, char *buffer, size_t buflen)
|
|||
cur += psyc_renderModifier(&packet->routing.modifiers[i], buffer + cur);
|
||||
|
||||
// add length if needed
|
||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH) {
|
||||
//cur += sprintf(buffer + cur, "%ld", packet->contentLength);
|
||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
|
||||
cur += itoa(packet->contentLength, buffer + cur, 10);
|
||||
}
|
||||
|
||||
if (packet->entity.lines || packet->method.length || packet->data.length)
|
||||
buffer[cur++] = '\n'; // start of content part if there's content
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
:_source psyc://foo/~bar
|
||||
:_target psyc://bar/~baz
|
||||
:_tag sch1828hu3r2cm
|
||||
85
|
||||
86
|
||||
:_foo bar baz
|
||||
:_abc_def 11 foo bar
|
||||
baz
|
||||
|
|
|
@ -99,7 +99,7 @@ _notice_presence\n\
|
|||
if (testList("\
|
||||
:_source psyc://10.100.1000/~ludwig\n\
|
||||
:_context psyc://10.100.1000/~ludwig\n\
|
||||
\n\
|
||||
85\n\
|
||||
:_list_text |foo|bar|baz\n\
|
||||
:_list_binary 21 3 foo|3 b|r|7 baz\n\
|
||||
qux\n\
|
||||
|
|
Loading…
Reference in a new issue