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
parent f47daa2291
commit 8d4f44f2f1
3 changed files with 87 additions and 15 deletions

View File

@ -224,6 +224,12 @@ get_cache_header_content_type(struct cache_entry *cached)
return NULL;
}
unsigned char *
get_default_content_type(void)
{
return get_default_mime_type();
}
unsigned char *
get_content_type(struct cache_entry *cached)
{

View File

@ -20,6 +20,9 @@ extern struct module mime_module;
* scanning the uri for extensions. */
unsigned char *get_content_type(struct cache_entry *cached);
/* Default mime type */
unsigned char *get_default_content_type(void);
/* Guess content type by looking at configurations of the given @extension */
unsigned char *get_extension_content_type(unsigned char *extension);

View File

@ -27,6 +27,7 @@
#include "intl/gettext/libintl.h"
#include "main/module.h"
#include "main/select.h"
#include "mime/mime.h"
#include "network/connection.h"
#include "network/socket.h"
#include "osdep/osdep.h"
@ -135,6 +136,8 @@ fsp_directory(FSP_SESSION *ses, struct uri *uri)
if (!uristring || !data || !init_string(&buf))
fsp_error("Out of memory");
fprintf(stderr, "text/html");
fclose(stderr);
add_html_to_string(&buf, uristring, strlen(uristring));
printf("<html><head><title>%s</title><base href=\"%s\">"
@ -176,6 +179,31 @@ end:
exit(0);
}
static unsigned char *
get_content_type_uri(struct uri *uri)
{
unsigned char *extension = get_extension_from_uri(uri);
if (extension) {
unsigned char *ctype;
/* XXX: A little hack for making extension handling case
* insensitive. We could probably do it better by making
* guess_encoding() case independent the real problem however
* is with default (via option system) and mimetypes resolving
* doing that option and hash lookup will not be easy to
* convert. --jonas */
convert_to_lowercase(extension, strlen(extension));
ctype = get_extension_content_type(extension);
if (ctype && *ctype) {
return ctype;
}
}
return get_default_content_type();
}
#define READ_SIZE 4096
static void
@ -200,6 +228,8 @@ do_fsp(struct connection *conn)
FSP_FILE *file = fsp_fopen(ses, data, "r");
int r;
fprintf(stderr, "%s", get_content_type_uri(uri));
fclose(stderr);
if (!file)
fsp_error("fsp_fopen error.");
@ -222,14 +252,14 @@ fsp_got_data(struct socket *socket, struct read_buffer *rb)
return;
}
if (len == 0) {
if (!len) {
if (conn->from)
normalize_cache_entry(conn->cached, conn->from);
abort_connection(conn, S_OK);
return;
}
conn->socket->state = SOCKET_END_ONCLOSE;
socket->state = SOCKET_END_ONCLOSE;
conn->received += len;
if (add_fragment(conn->cached, conn->from, rb->data, len) == 1)
conn->tries = 0;
@ -239,6 +269,35 @@ fsp_got_data(struct socket *socket, struct read_buffer *rb)
read_from_socket(socket, rb, S_TRANS, fsp_got_data);
}
static void
fsp_got_header(struct socket *socket, struct read_buffer *rb)
{
int len = rb->length;
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 (len <= 0) goto end;
rb->data[len] = '\0';
mem_free_set(&conn->cached->content_type, stracpy(rb->data));
end:
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
@ -246,23 +305,19 @@ void
fsp_protocol_handler(struct connection *conn)
{
int fsp_pipe[2] = { -1, -1 };
int header_pipe[2] = { -1, -1 };
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;
if (fsp_pipe[0] >= 0) close(fsp_pipe[0]);
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);
return;
}
conn->cached = get_cache_entry(conn->uri);
if (!conn->cached) {
abort_connection(conn, S_OUT_OF_MEM);
return;
}
conn->from = 0;
conn->unrestartable = 1;
@ -272,6 +327,8 @@ fsp_protocol_handler(struct connection *conn)
close(fsp_pipe[0]);
close(fsp_pipe[1]);
close(header_pipe[0]);
close(header_pipe[1]);
retry_connection(conn, -s_errno);
return;
}
@ -282,21 +339,27 @@ fsp_protocol_handler(struct connection *conn)
close(0);
dup2(open("/dev/null", O_RDONLY), 0);
close(2);
dup2(open("/dev/null", O_RDONLY), 2);
dup2(header_pipe[1], 2);
close(fsp_pipe[0]);
close(header_pipe[0]);
close_all_non_term_fd();
do_fsp(conn);
} 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]);
buf = alloc_read_buffer(conn->socket);
if (!buf) {
close(header_pipe[1]);
buf2 = alloc_read_buffer(conn->socket);
if (!buf2) {
close(fsp_pipe[0]);
close(header_pipe[0]);
abort_connection(conn, S_OUT_OF_MEM);
return;
}
read_from_socket(conn->socket, buf, S_CONN, fsp_got_data);
read_from_socket(conn->socket, buf2, S_CONN, fsp_got_header);
}
}