FILE *fp;
struct stat statbuf;
off_t bytes;
+ off_t count = 0;
+ size_t res;
char *bigbuffer;
char content_type[128];
int len;
fp = fopen(what, "rb");
if (fp == NULL) {
lprintf(9, "output_static('%s') -- NOT FOUND --\n", what);
- wprintf("HTTP/1.1 404 %s\n", strerror(errno));
+ wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
wprintf("Content-Type: text/plain\r\n");
wprintf("\r\n");
- wprintf("Cannot open %s: %s\n", what, strerror(errno));
+ wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
} else {
len = strlen (what);
if (!strncasecmp(&what[len - 4], ".gif", 4))
else
safestrncpy(content_type, "application/octet-stream", sizeof content_type);
- fstat(fileno(fp), &statbuf);
+ if (fstat(fileno(fp), &statbuf) == -1) {
+ lprintf(9, "output_static('%s') -- FSTAT FAILED --\n", what);
+ wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
+ wprintf("Content-Type: text/plain\r\n");
+ wprintf("\r\n");
+ wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
+ return;
+ }
+
+ count = 0;
bytes = statbuf.st_size;
- bigbuffer = malloc(bytes + 2);
- fread(bigbuffer, bytes, 1, fp);
+ if ((bigbuffer = malloc(bytes + 2)) == NULL) {
+ lprintf(9, "output_static('%s') -- MALLOC FAILED (%s) --\n", what, strerror(errno));
+ wprintf("HTTP/1.1 500 internal server error\r\n");
+ wprintf("Content-Type: text/plain\r\n");
+ wprintf("\r\n");
+ return;
+ }
+ while (count < bytes) {
+ if ((res = fread(bigbuffer + count, 1, bytes - count, fp)) == 0) {
+ lprintf(9, "output_static('%s') -- FREAD FAILED (%s) %zu bytes of %zu --\n", what, strerror(errno), bytes - count, bytes);
+ wprintf("HTTP/1.1 500 internal server error \r\n");
+ wprintf("Content-Type: text/plain\r\n");
+ wprintf("\r\n");
+ return;
+ }
+ count += res;
+ }
+
fclose(fp);
lprintf(9, "output_static('%s') %s\n", what, content_type);
i = 1;
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
- fcntl(s, F_SETFL, O_NONBLOCK);/// TODO
+ fcntl(s, F_SETFL, O_NONBLOCK); /* maide: this statement is incorrect
+ there should be a preceding F_GETFL
+ and a bitwise OR with the previous
+ fd flags */
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
lprintf(1, "Can't bind: %s\n", strerror(errno));
*/
ssize_t client_write(const void *buf, size_t count)
{
- char *newptr;
- size_t newalloc;
+ char *newptr;
+ size_t newalloc;
+ size_t bytesWritten = 0;
+ ssize_t res;
+ fd_set wset;
+ int fdflags;
if (WC->burst != NULL) {
if ((WC->burst_len + count) >= WC->burst_alloc) {
write(2, buf, count);
write(2, "\033[30m", 5);
#endif
- return (write(WC->http_sock, buf, count));
+
+ while (bytesWritten < count) {
+ if ((fdflags & O_NONBLOCK) == O_NONBLOCK) {
+ FD_ZERO(&wset);
+ FD_SET(WC->http_sock, &wset);
+ if (select(1, NULL, &wset, NULL, NULL) == -1) {
+ lprintf(2, "client_write: Socket select failed (%s)\n", strerror(errno));
+ return -1;
+ }
+ }
+
+ if ((res = write(WC->http_sock, (char*)buf + bytesWritten,
+ count - bytesWritten)) == -1) {
+ lprintf(2, "client_write: Socket write failed (%s)\n", strerror(errno));
+ return res;
+ }
+ bytesWritten += res;
+ }
+
+ return bytesWritten;
}
/*