1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

Content-Type handling

This commit is contained in:
2006-01-26 18:20:13 +01:00 committed by Jonas Fonseca
parent b530860e5a
commit bd4be81013

View File

@ -135,6 +135,8 @@ fsp_directory(FSP_SESSION *ses, struct uri *uri)
if (!uristring || !data || !init_string(&buf)) if (!uristring || !data || !init_string(&buf))
fsp_error("Out of memory"); fsp_error("Out of memory");
fprintf(stderr, "text/html");
fclose(stderr);
add_html_to_string(&buf, uristring, strlen(uristring)); add_html_to_string(&buf, uristring, strlen(uristring));
printf("<html><head><title>%s</title><base href=\"%s\">" printf("<html><head><title>%s</title><base href=\"%s\">"
@ -203,8 +205,15 @@ do_fsp(struct connection *conn)
if (!file) if (!file)
fsp_error("fsp_fopen error."); fsp_error("fsp_fopen error.");
/* Use the default way to find the MIME type, so write an
* 'empty' name, since something needs to be written in order
* to avoid socket errors. */
fprintf(stderr, "%c", '\0');
fclose(stderr);
while ((r = fsp_fread(buf, 1, READ_SIZE, file)) > 0) while ((r = fsp_fread(buf, 1, READ_SIZE, file)) > 0)
fwrite(buf, 1, r, stdout); fwrite(buf, 1, r, stdout);
fsp_fclose(file); fsp_fclose(file);
fsp_close_session(ses); fsp_close_session(ses);
exit(0); exit(0);
@ -222,14 +231,14 @@ fsp_got_data(struct socket *socket, struct read_buffer *rb)
return; return;
} }
if (len == 0) { if (!len) {
if (conn->from) if (conn->from)
normalize_cache_entry(conn->cached, conn->from); normalize_cache_entry(conn->cached, conn->from);
abort_connection(conn, S_OK); abort_connection(conn, S_OK);
return; return;
} }
conn->socket->state = SOCKET_END_ONCLOSE; socket->state = SOCKET_END_ONCLOSE;
conn->received += len; conn->received += len;
if (add_fragment(conn->cached, conn->from, rb->data, len) == 1) if (add_fragment(conn->cached, conn->from, rb->data, len) == 1)
conn->tries = 0; conn->tries = 0;
@ -239,6 +248,40 @@ fsp_got_data(struct socket *socket, struct read_buffer *rb)
read_from_socket(socket, rb, S_TRANS, fsp_got_data); read_from_socket(socket, rb, S_TRANS, fsp_got_data);
} }
static void
fsp_got_header(struct socket *socket, struct read_buffer *rb)
{
struct connection *conn = socket->conn;
struct read_buffer *buf;
conn->cached = get_cache_entry(conn->uri);
if (!conn->cached) {
close(socket->fd);
close(conn->data_socket->fd);
abort_connection(conn, S_OUT_OF_MEM);
return;
}
socket->state = SOCKET_END_ONCLOSE;
if (rb->length > 0) {
unsigned char *ctype = memacpy(rb->data, rb->length);
if (ctype && *ctype)
mem_free_set(&conn->cached->content_type, ctype);
else
mem_free_if(ctype);
}
buf = alloc_read_buffer(conn->data_socket);
if (!buf) {
close(socket->fd);
close(conn->data_socket->fd);
abort_connection(conn, S_OUT_OF_MEM);
return;
}
read_from_socket(conn->data_socket, buf, S_CONN, fsp_got_data);
}
#undef READ_SIZE #undef READ_SIZE
@ -246,23 +289,19 @@ void
fsp_protocol_handler(struct connection *conn) fsp_protocol_handler(struct connection *conn)
{ {
int fsp_pipe[2] = { -1, -1 }; int fsp_pipe[2] = { -1, -1 };
int header_pipe[2] = { -1, -1 };
pid_t cpid; pid_t cpid;
struct read_buffer *buf;
if (c_pipe(fsp_pipe)) { if (c_pipe(fsp_pipe) || c_pipe(header_pipe)) {
int s_errno = errno; int s_errno = errno;
if (fsp_pipe[0] >= 0) close(fsp_pipe[0]); if (fsp_pipe[0] >= 0) close(fsp_pipe[0]);
if (fsp_pipe[1] >= 0) close(fsp_pipe[1]); if (fsp_pipe[1] >= 0) close(fsp_pipe[1]);
if (header_pipe[0] >= 0) close(header_pipe[0]);
if (header_pipe[1] >= 0) close(header_pipe[1]);
abort_connection(conn, -s_errno); abort_connection(conn, -s_errno);
return; return;
} }
conn->cached = get_cache_entry(conn->uri);
if (!conn->cached) {
abort_connection(conn, S_OUT_OF_MEM);
return;
}
conn->from = 0; conn->from = 0;
conn->unrestartable = 1; conn->unrestartable = 1;
@ -272,6 +311,8 @@ fsp_protocol_handler(struct connection *conn)
close(fsp_pipe[0]); close(fsp_pipe[0]);
close(fsp_pipe[1]); close(fsp_pipe[1]);
close(header_pipe[0]);
close(header_pipe[1]);
retry_connection(conn, -s_errno); retry_connection(conn, -s_errno);
return; return;
} }
@ -282,21 +323,27 @@ fsp_protocol_handler(struct connection *conn)
close(0); close(0);
dup2(open("/dev/null", O_RDONLY), 0); dup2(open("/dev/null", O_RDONLY), 0);
close(2); close(2);
dup2(open("/dev/null", O_RDONLY), 2); dup2(header_pipe[1], 2);
close(fsp_pipe[0]); close(fsp_pipe[0]);
close(header_pipe[0]);
close_all_non_term_fd(); close_all_non_term_fd();
do_fsp(conn); do_fsp(conn);
} else { } else {
conn->socket->fd = fsp_pipe[0]; struct read_buffer *buf2;
conn->data_socket->fd = fsp_pipe[0];
conn->socket->fd = header_pipe[0];
close(fsp_pipe[1]); close(fsp_pipe[1]);
buf = alloc_read_buffer(conn->socket); close(header_pipe[1]);
if (!buf) { buf2 = alloc_read_buffer(conn->socket);
if (!buf2) {
close(fsp_pipe[0]); close(fsp_pipe[0]);
close(header_pipe[0]);
abort_connection(conn, S_OUT_OF_MEM); abort_connection(conn, S_OUT_OF_MEM);
return; return;
} }
read_from_socket(conn->socket, buf, S_CONN, fsp_got_data); read_from_socket(conn->socket, buf2, S_CONN, fsp_got_header);
} }
} }