/*
- * Read binary data from server into memory
+ * Read binary data from server into memory using a series of
+ * server READ commands.
*/
void read_server_binary(char *buffer, size_t total_len) {
char buf[SIZ];
size_t bytes = 0;
size_t thisblock = 0;
+ memset(buffer, 0, total_len);
while (bytes < total_len) {
thisblock = 4095;
if ((total_len - bytes) < thisblock) {
thisblock = total_len - bytes;
+ if (thisblock == 0) return;
}
serv_printf("READ %d|%d", (int)bytes, (int)thisblock);
serv_gets(buf);
bytes += thisblock;
}
else {
- wprintf("Error: %s<BR>\n", &buf[4]);
+ lprintf(3, "Error: %s<BR>\n", &buf[4]);
+ return;
}
}
}
char *bigbuffer;
char content_type[SIZ];
+ lprintf(9, "output_static(%s)\n", what);
sprintf(buf, "static/%s", what);
fp = fopen(buf, "rb");
if (fp == NULL) {
char buf[SIZ];
char *xferbuf = NULL;
off_t bytes;
- off_t thisblock;
- off_t accomplished = 0L;
serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
serv_gets(buf);
xferbuf = malloc(bytes);
/* Read it from the server */
- while (bytes > (off_t) 0) {
- thisblock = 4096;
- if (thisblock > bytes) {
- thisblock = bytes;
- }
- serv_printf("READ %ld|%ld", accomplished, thisblock);
- serv_gets(buf);
- if (buf[0] == '6') {
- thisblock = extract_long(&buf[4], 0);
- serv_read(&xferbuf[accomplished],
- (int) thisblock);
- }
- else {
- memset(&xferbuf[accomplished], 0, thisblock);
- }
- bytes = bytes - thisblock;
- accomplished += thisblock;
- }
+ read_server_binary(xferbuf, bytes);
serv_puts("CLOS");
serv_gets(buf);
/* Write it to the browser */
- http_transmit_thing(xferbuf, (size_t)accomplished,
- "image/gif");
+ http_transmit_thing(xferbuf, (size_t)bytes, "image/gif");
+ free(xferbuf);
} else {
wprintf("HTTP/1.0 404 %s\n", &buf[4]);
);
}
- if (xferbuf) {
- free(xferbuf);
- }
}
void output_mimepart()
{
char buf[SIZ];
- char xferbuf[4096];
off_t bytes;
- off_t thisblock;
- off_t accomplished = 0L;
char content_type[SIZ];
+ char *content = NULL;
serv_printf("OPNA %s|%s", bstr("msgnum"), bstr("partnum"));
serv_gets(buf);
if (buf[0] == '2') {
bytes = extract_long(&buf[4], 0);
+ content = malloc(bytes);
extract(content_type, &buf[4], 3);
output_headers(0);
- wprintf("Content-type: %s\n", content_type);
- wprintf("Content-length: %ld\n", (long) bytes);
- wprintf("\n");
-
- while (bytes > (off_t) 0) {
- thisblock = (off_t) sizeof(xferbuf);
- if (thisblock > bytes) {
- thisblock = bytes;
- }
- serv_printf("READ %ld|%ld", accomplished, thisblock);
- serv_gets(buf);
- if (buf[0] == '6') {
- thisblock = extract_long(&buf[4], 0);
- serv_read(xferbuf, (int) thisblock);
- }
- else {
- memset(xferbuf, 0, thisblock);
- }
- write(WC->http_sock, xferbuf, thisblock);
- bytes = bytes - thisblock;
- accomplished = accomplished + thisblock;
- }
+ read_server_binary(content, bytes);
serv_puts("CLOS");
serv_gets(buf);
+ http_transmit_thing(content, bytes, content_type);
+ free(content);
} else {
wprintf("HTTP/1.0 404 %s\n", &buf[4]);
output_headers(0);
{
char buf[SIZ];
off_t bytes;
- off_t thisblock;
- off_t accomplished = 0L;
char content_type[SIZ];
char *content;
extract(content_type, &buf[4], 3);
content = malloc(bytes + 1);
+ read_server_binary(content, bytes);
- while (bytes > (off_t) 0) {
- thisblock = bytes;
- if (thisblock > 4096L) {
- thisblock = 4096L;
- }
- if (thisblock > bytes) {
- thisblock = bytes;
- }
- serv_printf("READ %ld|%ld", accomplished, thisblock);
- serv_gets(buf);
- if (buf[0] == '6') {
- thisblock = extract_long(&buf[4], 0);
- serv_read(&content[accomplished], (int) thisblock);
- }
- else {
- memset(&content[accomplished], 0, thisblock);
- }
- bytes = bytes - thisblock;
- accomplished = accomplished + thisblock;
- }
serv_puts("CLOS");
serv_gets(buf);
- content[accomplished] = 0; /* null terminate for good measure */
+ content[bytes] = 0; /* null terminate for good measure */
return(content);
}
else {