/* $Id: ghtml-gtkhtml.c,v 1.23 2009/04/27 04:08:42 khorben Exp $ */
/* Copyright (c) 2009 Pierre Pronchery */
/* This file is part of DeforaOS Desktop Surfer */
/* Surfer is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 as published by the Free
* Software Foundation.
*
* Surfer 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
* Surfer; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA */
/* 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
#define GNET_EXPERIMENTAL
#include
#include
#include "ghtml.h"
#include "callbacks.h"
#include "../config.h"
#define max(a, b) ((a) > (b) ? (a) : (b))
/* GHtml */
/* private */
typedef struct _GHtmlConn GHtmlConn;
typedef struct _GHtmlHistory GHtmlHistory;
typedef struct _GHtml
{
Surfer * surfer;
/* history */
GList * history;
GList * current;
/* connections */
struct _GHtmlConn ** conns;
size_t conns_cnt;
/* 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 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 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);
/* GHtmlHistory */
/* types */
struct _GHtmlHistory
{
gchar * base;
gchar * url;
gchar * post;
};
/* prototypes */
static GHtmlHistory * _ghtmlhistory_new(char const * url, char const * post);
static void _ghtmlhistory_delete(GHtmlHistory * h);
/* public */
/* functions */
GtkWidget * ghtml_new(Surfer * surfer)
{
GHtml * ghtml;
GtkWidget * widget;
if((ghtml = malloc(sizeof(*ghtml))) == NULL)
return NULL;
ghtml->surfer = surfer;
ghtml->history = NULL;
ghtml->current = NULL;
ghtml->conns = NULL;
ghtml->conns_cnt = 0;
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;
}
/* accessors */
/* ghtml_can_go_back */
gboolean ghtml_can_go_back(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return ghtml->current != NULL && ghtml->current->prev != NULL
? TRUE : FALSE;
}
/* ghtml_can_go_forward */
gboolean ghtml_can_go_forward(GtkWidget * widget)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return ghtml->current != NULL && ghtml->current->next != NULL
? TRUE : FALSE;
}
/* 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;
GHtmlHistory * h;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
if(ghtml == NULL || ghtml->current == NULL
|| ghtml->current->data == NULL)
return NULL;
h = ghtml->current->data;
return h->url;
}
/* 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_base */
int ghtml_set_base(GtkWidget * widget, char const * url)
{
GHtml * ghtml;
ghtml = g_object_get_data(G_OBJECT(widget), "ghtml");
return _ghtml_set_base(ghtml, url);
}
/* useful */
/* 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_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)
{
GHtmlHistory * 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)
{
GHtmlHistory * 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_document_load */
static int _ghtml_document_load(GHtml * ghtml, gchar const * url,
gchar const * post)
{
GHtmlConn * gc;
GHtmlHistory * h;
_ghtml_stop(ghtml);
if((h = _ghtmlhistory_new(url, post)) == NULL)
return 1;
ghtml->history = g_list_append(ghtml->history, h);
ghtml->current = g_list_last(ghtml->history);
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;
GHtmlHistory * 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_make_url */
static gchar * _ghtml_make_url(gchar const * base, gchar const * url)
{
char * b;
char * p;
if(url == NULL)
return NULL;
/* XXX use a more generic protocol finder (strchr(':')) */
if(strncmp("ftp://", url, 6) == 0)
return g_strdup(url);
if(strncmp("http://", url, 7) == 0)
return g_strdup(url);
if(strncmp("https://", url, 8) == 0)
return g_strdup(url);
if(strncmp("mailto:", url, 7) == 0)
return g_strdup(url);
if(base != NULL)
{
if(url[0] == '/')
{
if(strncmp("http://", base, 7) == 0)
{
if((b = g_strdup(base)) == NULL)
return NULL;
if((p = strchr(&b[7], '/')) != NULL)
{
*p = '\0';
p = g_strdup_printf("%s%s", b, url);
free(b);
return p;
}
free(b);
}
/* FIXME implement other protocols */
return g_strdup_printf("%s%s", base, url);
}
/* construct from basename */
if((b = strdup(base)) == NULL)
return NULL;
p = b;
/* FIXME implement other protocols */
if(strncmp("http://", p, 7) == 0)
p += 7;
if((p = strrchr(p, '/')) != NULL)
*p = '\0';
p = g_strdup_printf("%s/%s", b, url);
free(b);
return p;
}
/* base is NULL, url is not NULL */
if(url[0] == '/')
return g_strdup(url);
/* guess protocol */
if(strncmp("ftp", url, 3) == 0)
return g_strdup_printf("%s%s", "ftp://", url);
/* FIXME guess http only for "www.*"? we're already in GNet...? */
return g_strdup_printf("%s%s", "http://", url);
}
/* ghtml_stop */
static void _ghtml_stop(GHtml * ghtml)
{
size_t i;
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