/* $Id: ghtml-gtkhtml.c,v 1.43 2010/12/09 22:32:27 khorben Exp $ */
/* Copyright (c) 2010 Pierre Pronchery */
/* This file is part of DeforaOS Desktop Surfer */
/* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see . */
/* TODO:
* - fix URL generation for relative path
* - implement selection
* - more meaningful status updates
* - implement cookies (beware same-origin policy)
* - implement referer
* - implement history
* - implement anchors
* - probably need to implement html_stream_cancel
* - need to take care of CSRF? eg remotely load local files */
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define GNET_EXPERIMENTAL
#include
#include
#include "ghtml.h"
#include "callbacks.h"
#include "../config.h"
#include "common/history.c"
#include "common/url.c"
#define _(string) gettext(string)
#define max(a, b) ((a) > (b) ? (a) : (b))
/* GHtml */
/* private */
typedef struct _GHtmlConn GHtmlConn;
typedef struct _GHtml
{
Surfer * surfer;
char * source; /* XXX keep the size */
/* history */
GList * history;
GList * current;
/* connections */
struct _GHtmlConn ** conns;
size_t conns_cnt;
gdouble progress;
char * status;
/* html widget */
HtmlDocument * html_document;
gchar * html_title;
GtkWidget * html_view;
} GHtml;
struct _GHtmlConn
{
GHtml * ghtml;
char * url;
char const * anchor;
guint64 content_length;
guint64 data_received;
HtmlStream * stream;
int direct;
int image;
/* file */
GIOChannel * file;
guint64 file_size;
guint64 file_read;
/* http */
GConnHttp * http;
GConnHttpMethod http_method;
gchar * http_post;
};
/* prototypes */
static GHtmlConn * _ghtmlconn_new(GHtml * ghtml, HtmlStream * stream,
gchar const * url, gchar const * post);
static void _ghtmlconn_delete(GHtmlConn * ghtmlconn);
static char const * _ghtml_get_base(GHtml * ghtml);
static int _ghtml_set_base(GHtml * ghtml, char const * url);
static void _ghtml_set_progress(GHtml * ghtml, gdouble progress);
static void _ghtml_set_status(GHtml * ghtml, char const * status);
static int _ghtml_document_load(GHtml * ghtml, gchar const * url,
gchar const * post);
static int _ghtml_document_reload(GHtml * ghtml);
static gchar * _ghtml_make_url(gchar const * base, gchar const * url);
static int _ghtml_source_append(GHtml * ghtml, char const * buf, size_t size);
static void _ghtml_stop(GHtml * ghtml);
static GHtmlConn * _ghtml_stream_load(GHtml * ghtml, HtmlStream * stream,
gchar const * url, gchar const * post);
/* callbacks */
static gboolean _on_button_press_event(GtkWidget* widget,
GdkEventButton * event, gpointer data);
static void _on_link_clicked(HtmlDocument * document, const gchar * url);
static void _on_request_url(HtmlDocument * document, const gchar * url,
HtmlStream * stream);
static void _on_set_base(HtmlDocument * document, const gchar * url);
static void _on_submit(HtmlDocument * document, const gchar * url,
const gchar * method, const gchar * encoding);
static void _on_title_changed(HtmlDocument * document, const gchar * title);
static void _on_url(HtmlView * view, const gchar * url);
/* public */
/* functions */
GtkWidget * ghtml_new(Surfer * surfer)
{
GHtml * ghtml;
GtkWidget * widget;
if((ghtml = malloc(sizeof(*ghtml))) == NULL)
return NULL;
ghtml->surfer = surfer;
ghtml->source = NULL;
ghtml->history = NULL;
ghtml->current = NULL;
ghtml->conns = NULL;
ghtml->conns_cnt = 0;
ghtml->progress = -1.0;
ghtml->status = NULL;
ghtml->html_view = html_view_new();
g_object_set_data(G_OBJECT(ghtml->html_view), "ghtml", ghtml);
g_signal_connect(G_OBJECT(ghtml->html_view), "button-press-event",
G_CALLBACK(_on_button_press_event), ghtml);
g_signal_connect(G_OBJECT(ghtml->html_view), "on-url", G_CALLBACK(
_on_url), NULL);
ghtml->html_document = html_document_new();
ghtml->html_title = NULL;
g_object_set_data(G_OBJECT(ghtml->html_document), "ghtml", ghtml);
g_signal_connect(G_OBJECT(ghtml->html_document), "link-clicked",
G_CALLBACK(_on_link_clicked), NULL);
g_signal_connect(G_OBJECT(ghtml->html_document), "request-url",
G_CALLBACK(_on_request_url), NULL);
g_signal_connect(G_OBJECT(ghtml->html_document), "set-base", G_CALLBACK(
_on_set_base), NULL);
g_signal_connect(G_OBJECT(ghtml->html_document), "submit", G_CALLBACK(
_on_submit), NULL);
g_signal_connect(G_OBJECT(ghtml->html_document), "title-changed",
G_CALLBACK(_on_title_changed), NULL);
html_view_set_document(HTML_VIEW(ghtml->html_view),
ghtml->html_document);
widget = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(widget),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
g_object_set_data(G_OBJECT(widget), "ghtml", ghtml);
gtk_container_add(GTK_CONTAINER(widget), ghtml->html_view);
return widget;
}
/* ghtml_delete */
void ghtml_delete(GtkWidget * widget)
{
GHtml * ghtml;
size_t i;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
free(ghtml->source);
free(ghtml->status);
for(i = 0; i < ghtml->conns_cnt; i++)
if(ghtml->conns[i] != NULL)
_ghtmlconn_delete(ghtml->conns[i]);
free(ghtml->conns); /* may not be free()'d by _ghtmlconn_delete() */
free(ghtml->html_title);
free(ghtml);
}
/* accessors */
/* ghtml_can_go_back */
gboolean ghtml_can_go_back(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return _history_can_go_back(ghtml->current);
}
/* ghtml_can_go_forward */
gboolean ghtml_can_go_forward(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return _history_can_go_forward(ghtml->current);
}
/* ghtml_get_link_message */
char const * ghtml_get_link_message(GtkWidget * ghtml)
{
/* FIXME implement */
return NULL;
}
/* ghtml_get_location */
char const * ghtml_get_location(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return _history_get_location(ghtml->current);
}
/* ghtml_get_progress */
gdouble ghtml_get_progress(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return ghtml->progress;
}
/* ghtml_get_source */
char const * ghtml_get_source(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return ghtml->source;
}
/* ghtml_get_status */
char const * ghtml_get_status(GtkWidget * widget)
{
/* FIXME really implement */
return NULL;
}
/* ghtml_get_title */
char const * ghtml_get_title(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return ghtml->html_title;
}
/* ghtml_set_proxy */
int ghtml_set_proxy(GtkWidget * ghtml, SurferProxyType type, char const * http,
unsigned int http_port)
{
if(type == SPT_NONE)
return 0;
/* FIXME really implement */
return -error_set_code(1, "%s", strerror(ENOSYS));;
}
/* useful */
/* ghtml_execute */
void ghtml_execute(GtkWidget * ghtml, char const * code)
{
/* FIXME implement */
}
/* ghtml_find */
gboolean ghtml_find(GtkWidget * ghtml, char const * text, gboolean sensitive,
gboolean wrap)
{
/* FIXME implement */
return FALSE;
}
/* ghtml_go_back */
gboolean ghtml_go_back(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
if(ghtml->current == NULL || ghtml->current->prev == NULL)
return FALSE;
ghtml->current = ghtml->current->prev;
ghtml_refresh(widget);
return (ghtml->current->prev != NULL) ? TRUE : FALSE;
}
/* ghtml_go_forward */
gboolean ghtml_go_forward(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
if(ghtml->current == NULL || ghtml->current->next == NULL)
return FALSE;
ghtml->current = ghtml->current->next;
ghtml_refresh(widget);
return (ghtml->current->next != NULL) ? TRUE : FALSE;
}
/* ghtml_load_url */
void ghtml_load_url(GtkWidget * widget, char const * url)
{
GHtml * ghtml;
gchar * link;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(\"%s\")\n", __func__, url);
#endif
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
if((link = _ghtml_make_url(NULL, url)) != NULL)
url = link;
_ghtml_document_load(ghtml, url, NULL);
g_free(link);
}
/* ghtml_print */
void ghtml_print(GtkWidget * ghtml)
{
/* FIXME implement */
}
/* ghtml_refresh */
void ghtml_refresh(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
_ghtml_document_reload(ghtml);
}
/* ghtml_reload */
void ghtml_reload(GtkWidget * ghtml)
{
ghtml_refresh(ghtml);
}
/* ghtml_select_all */
void ghtml_select_all(GtkWidget * widget)
{
#if 0 /* FIXME does not work */
GHtml * ghtml;
DomNode * node;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
if((node = html_document_find_anchor(ghtml->html_document, "")) != NULL)
html_selection_set(HTML_VIEW(ghtml->html_view), node, 0, -1);
#endif
}
/* ghtml_stop */
void ghtml_stop(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
_ghtml_stop(ghtml);
}
/* ghtml_unselect_all */
void ghtml_unselect_all(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
html_selection_clear(HTML_VIEW(ghtml->html_view));
}
/* ghtml_zoom_in */
void ghtml_zoom_in(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
html_view_zoom_in(HTML_VIEW(ghtml->html_view));
}
/* ghtml_zoom_out */
void ghtml_zoom_out(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
html_view_zoom_out(HTML_VIEW(ghtml->html_view));
}
/* ghtml_zoom_reset */
void ghtml_zoom_reset(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
html_view_zoom_reset(HTML_VIEW(ghtml->html_view));
}
/* private */
/* functions */
static GHtmlConn * _ghtmlconn_new(GHtml * ghtml, HtmlStream * stream,
gchar const * url, gchar const * post)
{
GHtmlConn ** p;
GHtmlConn * c;
char * q = NULL;
/* FIXME leaks memory: records are not re-used */
if((p = realloc(ghtml->conns, sizeof(*p) * (ghtml->conns_cnt + 1)))
== NULL)
return NULL;
ghtml->conns = p;
if((c = malloc(sizeof(*c))) == NULL)
return NULL;
ghtml->conns[ghtml->conns_cnt] = c;
c->ghtml = ghtml;
c->url = strdup(url);
/* look for an anchor */
/* XXX should it really be done here? */
if(c->url != NULL && (q = strrchr(c->url, '#')) != NULL)
*q = '\0';
c->anchor = (q != NULL) ? ++q : NULL;
#ifdef DEBUG
if(c->anchor != NULL)
fprintf(stderr, "DEBUG: %s() anchor=\"%s\"\n", __func__,
c->anchor);
#endif
c->content_length = 0;
c->data_received = 0;
c->stream = stream;
c->direct = 0;
c->image = 0;
c->file = NULL;
c->file_size = 0;
c->file_read = 0;
c->http = NULL;
c->http_method = (post == NULL) ? GNET_CONN_HTTP_METHOD_GET
: GNET_CONN_HTTP_METHOD_POST;
c->http_post = (post != NULL) ? g_strdup(post) : NULL;
if(c->url == NULL || (post != NULL && c->http_post == NULL))
{
_ghtmlconn_delete(c);
return NULL;
}
ghtml->conns_cnt++;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%p, %p, \"%s\") => %p\n", __func__, ghtml,
stream, url, c);
#endif
return c;
}
/* ghtmlconn_delete */
static void _ghtmlconn_delete_file(GHtmlConn * ghtmlconn);
static void _ghtmlconn_delete_http(GHtmlConn * ghtmlconn);
static void _ghtmlconn_delete(GHtmlConn * ghtmlconn)
{
GHtml * ghtml = ghtmlconn->ghtml;
size_t i;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%p)\n", __func__, ghtmlconn);
#endif
for(i = 0; i < ghtml->conns_cnt; i++)
if(ghtml->conns[i] == ghtmlconn)
{
ghtml->conns[i] = NULL; /* don't double free later */
break;
}
if(ghtmlconn->file != NULL)
_ghtmlconn_delete_file(ghtmlconn);
else if(ghtmlconn->http != NULL)
_ghtmlconn_delete_http(ghtmlconn);
free(ghtmlconn->url);
if(ghtmlconn->stream != NULL)
html_stream_close(ghtmlconn->stream);
free(ghtmlconn);
/* free the connections array if possible */
for(i = 0; i < ghtml->conns_cnt; i++)
if(ghtml->conns[i] != NULL)
return;
free(ghtml->conns);
ghtml->conns = NULL;
ghtml->conns_cnt = 0;
}
static void _ghtmlconn_delete_file(GHtmlConn * ghtmlconn)
{
if(ghtmlconn->file != NULL)
g_io_channel_unref(ghtmlconn->file);
}
static void _ghtmlconn_delete_http(GHtmlConn * ghtmlconn)
{
gnet_conn_http_delete(ghtmlconn->http);
g_free(ghtmlconn->http_post);
}
/* ghtml_get_base */
static char const * _ghtml_get_base(GHtml * ghtml)
{
History * h;
if(ghtml->current == NULL || (h = ghtml->current->data) == NULL)
return NULL;
return (h->base != NULL) ? h->base : h->url;
}
/* ghtml_set_base */
static int _ghtml_set_base(GHtml * ghtml, char const * url)
{
History * h;
if(ghtml->current == NULL || (h = ghtml->current->data) == NULL)
return 1;
free(h->base);
if(url == NULL)
h->base = NULL;
else if((h->base = strdup(url)) == NULL)
return 1;
return 0;
}
/* ghtml_set_progress */
static void _ghtml_set_progress(GHtml * ghtml, gdouble progress)
{
surfer_set_progress(ghtml->surfer, progress);
ghtml->progress = progress;
}
/* ghtml_set_status */
static void _ghtml_set_status(GHtml * ghtml, char const * status)
{
free(ghtml->status);
/* XXX may fail */
ghtml->status = (status != NULL) ? strdup(status) : NULL;
surfer_set_status(ghtml->surfer, status);
}
/* ghtml_document_load */
static int _ghtml_document_load(GHtml * ghtml, gchar const * url,
gchar const * post)
{
char const * q;
GHtmlConn * gc;
History * h;
_ghtml_stop(ghtml);
if((q = _history_get_location(ghtml->current)) == NULL
|| strcmp(q, url) != 0)
{
if((h = _history_new(url, post)) == NULL)
return 1;
ghtml->current = _history_append(h, ghtml->current);
ghtml->history = g_list_first(ghtml->current);
}
surfer_set_location(ghtml->surfer, url);
surfer_set_title(ghtml->surfer, NULL);
html_document_open_stream(ghtml->html_document, "text/html");
if((gc = _ghtml_stream_load(ghtml, ghtml->html_document->current_stream,
url, post)) != NULL)
gc->direct = 1;
return gc != NULL ? 0 : 1;
}
static int _ghtml_document_reload(GHtml * ghtml)
{
GHtmlConn * gc;
History * h;
_ghtml_stop(ghtml);
if(ghtml->current == NULL || (h = ghtml->current->data) == NULL)
return 0;
surfer_set_location(ghtml->surfer, h->url);
surfer_set_title(ghtml->surfer, NULL);
html_document_open_stream(ghtml->html_document, "text/html");
/* FIXME warn if h->post is set */
if((gc = _ghtml_stream_load(ghtml, ghtml->html_document->current_stream,
h->url, h->post)) != NULL)
gc->direct = 1;
return gc != NULL ? 0 : 1;
}
/* ghtml_source_append */
static int _ghtml_source_append(GHtml * ghtml, char const * buf, size_t size)
{
size_t len = (ghtml->source != NULL) ? strlen(ghtml->source) : 0;
char * p;
/* FIXME this may lose data (eg if it contains NULL bytes) */
if((p = realloc(ghtml->source, len + size + 1)) == NULL)
return 1; /* XXX report error */
ghtml->source = p;
memcpy(p + len, buf, size);
p[len + size] = '\0';
return 0;
}
/* ghtml_stop */
static void _ghtml_stop(GHtml * ghtml)
{
size_t i;
free(ghtml->source);
ghtml->source = NULL;
for(i = 0; i < ghtml->conns_cnt; i++)
if(ghtml->conns[i] != NULL)
_ghtmlconn_delete(ghtml->conns[i]);
free(ghtml->conns);
ghtml->conns = NULL;
ghtml->conns_cnt = 0;
}
/* ghtml_stream_load */
static gboolean _stream_load_idle(gpointer data);
static gboolean _stream_load_idle_directory(GHtmlConn * conn);
static gboolean _stream_load_idle_file(GHtmlConn * conn);
static gboolean _stream_load_watch_file(GIOChannel * source,
GIOCondition condition, gpointer data);
static gboolean _stream_load_idle_http(GHtmlConn * conn);
static void _stream_load_watch_http(GConnHttp * connhttp,
GConnHttpEvent * event, gpointer data);
static void _http_connected(GHtmlConn * conn);
static void _http_data_complete(GConnHttpEventData * event, GHtmlConn * conn);
static void _http_data_partial(GConnHttpEventData * event, GHtmlConn * conn);
static void _http_data_progress(GConnHttpEventData * event, GHtmlConn * conn);
static void _http_error(GConnHttpEventError * event, GHtmlConn * conn);
static void _http_redirect(GConnHttpEventRedirect * event, GHtmlConn * conn);
static void _http_resolved(GConnHttpEventResolved * event, GHtmlConn * conn);
static void _http_response(GConnHttpEventResponse * event, GHtmlConn * conn);
static void _http_timeout(GHtmlConn * conn);
static GHtmlConn * _ghtml_stream_load(GHtml * ghtml, HtmlStream * stream,
gchar const * url, gchar const * post)
{
GHtmlConn * conn;
if((conn = _ghtmlconn_new(ghtml, stream, url, post)) == NULL)
return NULL;
g_idle_add(_stream_load_idle, conn);
return conn;
}
static gboolean _stream_load_idle(gpointer data)
{
GHtmlConn * conn = data;
#ifdef DEBUG
fprintf(stderr, "DEBUG: %s(%p) \"%s\"\n", __func__, data, conn->url);
#endif
html_view_jump_to_anchor(HTML_VIEW(conn->ghtml->html_view),
(conn->anchor != NULL) ? conn->anchor : "");
if(conn->url[0] == '/')
return _stream_load_idle_file(conn);
if(strncmp(conn->url, "file:", 5) == 0)
{
strcpy(conn->url, &conn->url[5]); /* XXX no corruption? */
return _stream_load_idle_file(conn);
}
if(strncmp(conn->url, "http:", 5) == 0)
return _stream_load_idle_http(conn);
surfer_error(conn->ghtml->surfer, _("Unknown protocol"), 0);
_ghtmlconn_delete(conn);
return FALSE;
}
static gboolean _stream_load_idle_directory(GHtmlConn * conn)
{
const char tail[] = "\n
\n