commit 17674fcc4257afa53e81fdeab49b21f0f57528c7 Quentin Rameau <quinq@fifth.space> 2021-02-08 22:59:52 +0100 Close connection when a single-dot line is read This approach instead of always warning that something might have been wrong in case the message isn't terminated single-dot line is more forgiving as not all gopher server follow that aspect of the standard.
sacc.c | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
diff --git a/sacc.c b/sacc.c index 0b54ddc..6867963 100644 --- a/sacc.c +++ b/sacc.c @@ -377,8 +377,6 @@ molddiritem(char *raw) s = nl; nl = p+1; } - if (!strcmp(s, ".\r\n") || !strcmp(s, ".\n")) - --nitems; if (!nitems) { diag("Couldn't parse dir item"); return NULL; @@ -421,6 +419,17 @@ getrawitem(int sock) do { bs -= n; buf += n; + + if (buf - raw >= 5) { + if (strncmp(buf-5, "\r\n.\r\n", 5) == 0) { + buf[-3] = '\0'; + break; + } + } else if (buf - raw == 3 && strncmp(buf-3, ".\r\n", 3)) { + buf[-3] = '\0'; + break; + } + if (bs < 1) { raw = xreallocarray(raw, ++bn, BUFSIZ); buf = raw + (bn-1) * BUFSIZ; @@ -611,10 +620,6 @@ fetchitem(Item *item) if (raw == NULL || !*raw) { diag("Empty response from server"); clear(&raw); - } else if ((r = strrchr(raw, '.')) == NULL || strcmp(r, ".\r\n")) { - diag("Incomplete response from server"); - } else { - *r = '\0'; } return ((item->raw = raw) != NULL);