don't notify about socket reads that fall short of expectations

this will prospectively make the debug output less messy.
This commit is contained in:
Oswald Buddenhagen 2022-06-09 13:27:50 +02:00
parent 96b1e52802
commit ac3b5186b0
3 changed files with 54 additions and 16 deletions

View File

@ -1605,14 +1605,18 @@ imap_socket_read( void *aux )
if (ctx->parse_list_sts.level) { if (ctx->parse_list_sts.level) {
resp = parse_list_continue( ctx ); resp = parse_list_continue( ctx );
listret: listret:
if (resp == LIST_PARTIAL) if (resp == LIST_PARTIAL) {
socket_expect_bytes( &ctx->conn, ctx->parse_list_sts.need_bytes );
return; return;
}
if (resp == LIST_BAD) if (resp == LIST_BAD)
break; break;
continue; continue;
} }
if (!(cmd = socket_read_line( &ctx->conn ))) if (!(cmd = socket_read_line( &ctx->conn ))) {
socket_expect_bytes( &ctx->conn, 0 );
return; return;
}
if (cmd == (void *)~0) { if (cmd == (void *)~0) {
if (!ctx->expectEOF) if (!ctx->expectEOF)
error( "IMAP error: unexpected EOF from %s\n", ctx->conn.name ); error( "IMAP error: unexpected EOF from %s\n", ctx->conn.name );

View File

@ -719,6 +719,33 @@ do_read( conn_t *sock, char *buf, uint len )
return n; return n;
} }
static void
socket_filled( conn_t *conn, uint len )
{
uint off = conn->offset;
uint cnt = conn->bytes + len;
conn->bytes = cnt;
if (conn->wanted) {
// Fulfill as much of the request as still fits into the buffer
if (cnt < conn->wanted && off + cnt < sizeof(conn->buf))
return;
} else {
// Need a full line
char *s = conn->buf + off;
char *p = memchr( s + conn->scanoff, '\n', cnt - conn->scanoff );
if (!p) {
conn->scanoff = cnt;
if (off + cnt == sizeof(conn->buf)) {
memmove( conn->buf, conn->buf + off, cnt );
conn->offset = 0;
}
return;
}
conn->scanoff = (uint)(p - s);
}
conn->read_callback( conn->callback_aux );
}
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
static void static void
socket_fill_z( conn_t *sock ) socket_fill_z( conn_t *sock )
@ -745,10 +772,8 @@ socket_fill_z( conn_t *sock )
if (!sock->in_z->avail_out) if (!sock->in_z->avail_out)
conf_wakeup( &sock->z_fake, 0 ); conf_wakeup( &sock->z_fake, 0 );
if ((len = (uint)((char *)sock->in_z->next_out - buf))) { if ((len = (uint)((char *)sock->in_z->next_out - buf)))
sock->bytes += len; socket_filled( sock, len );
sock->read_callback( sock->callback_aux );
}
} }
#endif #endif
@ -778,8 +803,7 @@ socket_fill( conn_t *sock )
if ((n = do_read( sock, buf, len )) <= 0) if ((n = do_read( sock, buf, len )) <= 0)
return; return;
sock->bytes += (uint)n; socket_filled( sock, (uint)n );
sock->read_callback( sock->callback_aux );
} }
} }
@ -799,6 +823,21 @@ socket_expect_eof( conn_t *sock )
#endif #endif
} }
void
socket_expect_bytes( conn_t *conn, uint len )
{
conn->wanted = len;
uint off = conn->offset;
if (off) {
uint cnt = conn->bytes;
if (off + len > sizeof(conn->buf) ||
off + cnt == sizeof(conn->buf)) {
memmove( conn->buf, conn->buf + off, cnt );
conn->offset = 0;
}
}
}
char * char *
socket_read( conn_t *conn, uint min_len, uint max_len, uint *out_len ) socket_read( conn_t *conn, uint min_len, uint max_len, uint *out_len )
{ {
@ -811,10 +850,6 @@ socket_read( conn_t *conn, uint min_len, uint max_len, uint *out_len )
if (cnt < min_len) { if (cnt < min_len) {
if (conn->state == SCK_EOF) if (conn->state == SCK_EOF)
return (void *)~0; return (void *)~0;
if (off + min_len > sizeof(conn->buf)) {
memmove( conn->buf, conn->buf + off, cnt );
conn->offset = 0;
}
return NULL; return NULL;
} }
uint n = (cnt < max_len) ? cnt : max_len; uint n = (cnt < max_len) ? cnt : max_len;
@ -836,10 +871,6 @@ socket_read_line( conn_t *conn )
if (conn->state == SCK_EOF) if (conn->state == SCK_EOF)
return (void *)~0; return (void *)~0;
conn->scanoff = cnt; conn->scanoff = cnt;
if (off + cnt == sizeof(conn->buf)) {
memmove( conn->buf, conn->buf + off, cnt );
conn->offset = 0;
}
return NULL; return NULL;
} }
uint n = (uint)(p + 1 - s); uint n = (uint)(p + 1 - s);

View File

@ -99,6 +99,7 @@ typedef struct {
uint offset; /* start of filled bytes in buffer */ uint offset; /* start of filled bytes in buffer */
uint bytes; /* number of filled bytes in buffer */ uint bytes; /* number of filled bytes in buffer */
uint scanoff; /* offset to continue scanning for newline at, relative to 'offset' */ uint scanoff; /* offset to continue scanning for newline at, relative to 'offset' */
uint wanted; // try to accumulate that many bytes before calling back; 0 => full line
char buf[100000]; char buf[100000];
#ifdef HAVE_LIBZ #ifdef HAVE_LIBZ
char z_buf[100000]; char z_buf[100000];
@ -121,6 +122,7 @@ static INLINE void socket_init( conn_t *conn,
conn->fd = -1; conn->fd = -1;
conn->name = NULL; conn->name = NULL;
conn->write_buf_append = &conn->write_buf; conn->write_buf_append = &conn->write_buf;
conn->wanted = 1;
} }
void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) ); void socket_connect( conn_t *conn, void (*cb)( int ok, void *aux ) );
void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) ); void socket_start_tls(conn_t *conn, void (*cb)( int ok, void *aux ) );
@ -128,6 +130,7 @@ void socket_start_deflate( conn_t *conn );
void socket_close( conn_t *sock ); void socket_close( conn_t *sock );
void socket_expect_activity( conn_t *sock, int expect ); void socket_expect_activity( conn_t *sock, int expect );
void socket_expect_eof( conn_t *sock ); void socket_expect_eof( conn_t *sock );
void socket_expect_bytes( conn_t *sock, uint len );
// Don't free return values. These functions never wait. // Don't free return values. These functions never wait.
char *socket_read( conn_t *conn, uint min_len, uint max_len, uint *out_len ); char *socket_read( conn_t *conn, uint min_len, uint max_len, uint *out_len );
char *socket_read_line( conn_t *conn ); char *socket_read_line( conn_t *conn );