Skip to content

Commit

Permalink
physfshttpd: deal with write errors on the socket.
Browse files Browse the repository at this point in the history
  • Loading branch information
icculus committed Aug 6, 2017
1 parent 61bdee4 commit 121ee38
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions extras/physfshttpd.c
Expand Up @@ -78,34 +78,46 @@ static char *txt200 =
"Content-Type: text/plain; charset=utf-8\n"
"\n";

static int writeAll(const int fd, const void *buf, const size_t len)
{
return (write(fd, buf, len) == len);
} /* writeAll */

static void feed_file_http(const char *ipstr, int sock, const char *fname)
{
PHYSFS_File *in = PHYSFS_openRead(fname);
char buffer[1024];

printf("%s: requested [%s].\n", ipstr, fname);
if (in == NULL)
{
printf("%s: Can't open [%s]: %s.\n",
ipstr, fname, PHYSFS_getLastError());
write(sock, txt404, strlen(txt404)); /* !!! FIXME: Check retval */
if (!writeAll(sock, txt404, strlen(txt404)))
printf("%s: Write error to socket.\n", ipstr);
return;
} /* if */
else

if (writeAll(sock, txt200, strlen(txt200)))
{
write(sock, txt200, strlen(txt200)); /* !!! FIXME: Check retval */
do
{
char buffer[1024];
PHYSFS_sint64 br = PHYSFS_readBytes(in, buffer, sizeof (buffer));
if (br == -1)
{
printf("%s: Read error: %s.\n", ipstr, PHYSFS_getLastError());
break;
} /* if */

write(sock, buffer, (int) br); /* !!! FIXME: CHECK THIS RETVAL! */
else if (!writeAll(sock, buffer, (size_t) br))
{
printf("%s: Write error to socket.\n", ipstr);
break;
} /* else if */
} while (!PHYSFS_eof(in));
} /* if */

PHYSFS_close(in);
} /* else */
PHYSFS_close(in);
} /* feed_file_http */


Expand Down

0 comments on commit 121ee38

Please sign in to comment.