beautify socket_read_line() somewhat

- use more appropriate name for socket object
- localize some variable declarations
- denoise the code by using more local variables
- don't pointlessly do stuff when we're failing anyway
This commit is contained in:
Oswald Buddenhagen 2022-06-09 11:00:11 +02:00
parent bc3145617a
commit 69653aafeb

View File

@ -816,27 +816,27 @@ socket_read( conn_t *conn, char *buf, uint len )
} }
char * char *
socket_read_line( conn_t *b ) socket_read_line( conn_t *conn )
{ {
char *p, *s; uint off = conn->offset;
uint n; uint cnt = conn->bytes;
char *s = conn->buf + off;
s = b->buf + b->offset; char *p = memchr( s + conn->scanoff, '\n', cnt - conn->scanoff );
p = memchr( s + b->scanoff, '\n', b->bytes - b->scanoff );
if (!p) { if (!p) {
b->scanoff = b->bytes; if (conn->state == SCK_EOF)
if (b->offset + b->bytes == sizeof(b->buf)) {
memmove( b->buf, b->buf + b->offset, b->bytes );
b->offset = 0;
}
if (b->state == SCK_EOF)
return (void *)~0; return (void *)~0;
conn->scanoff = cnt;
if (off + cnt == sizeof(conn->buf)) {
memmove( conn->buf, conn->buf + off, cnt );
conn->offset = 0;
}
return NULL; return NULL;
} }
n = (uint)(p + 1 - s); uint n = (uint)(p + 1 - s);
b->offset += n; cnt -= n;
b->bytes -= n; conn->offset = off + n;
b->scanoff = 0; conn->bytes = cnt;
conn->scanoff = 0;
if (p != s && p[-1] == '\r') if (p != s && p[-1] == '\r')
p--; p--;
*p = 0; *p = 0;