~ubuntu-branches/ubuntu/maverick/totem-pl-parser/maverick

« back to all changes in this revision

Viewing changes to plparse/totem-pl-parser.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastian Dröge, Josselin Mouette, Sebastian Dröge
  • Date: 2009-05-18 17:38:08 UTC
  • mfrom: (1.1.20 upstream)
  • mto: (1.5.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 31.
  • Revision ID: james.westby@ubuntu.com-20090518173808-ve0i7iirzbyarsfk
Tags: 2.26.2-1
[ Josselin Mouette ]
* Build-depend on libglib2.0-doc to ensure proper xrefs.

[ Sebastian Dröge ]
* New upstream bugfix release.
* debian/control.in:
  + Update Standards-Version to 3.8.1.
  + Move debug package to Section debug.

Show diffs side-by-side

added added

removed removed

Lines of Context:
858
858
        return retval;
859
859
}
860
860
 
 
861
static char *
 
862
relative_uri_remove_query (const char *uri, char **query)
 
863
{
 
864
        char *qmark;
 
865
 
 
866
        /* Look for '?' */
 
867
        qmark = strrchr (uri, '?');
 
868
        if (qmark == NULL)
 
869
                return NULL;
 
870
 
 
871
        if (query != NULL)
 
872
                *query = g_strdup (qmark);
 
873
        return g_strndup (uri, qmark - uri);
 
874
}
 
875
 
 
876
static const char *suffixes[] = {
 
877
        ".jsp",
 
878
        ".php",
 
879
        ".asp"
 
880
};
 
881
 
 
882
static gboolean
 
883
is_probably_dir (const char *filename)
 
884
{
 
885
        gboolean ret;
 
886
        char *content_type, *short_name;
 
887
 
 
888
        short_name = relative_uri_remove_query (filename, NULL);
 
889
        if (short_name == NULL)
 
890
                short_name = g_strdup (filename);
 
891
        content_type = g_content_type_guess (short_name, NULL, -1, NULL);
 
892
        if (g_content_type_is_unknown (content_type) != FALSE) {
 
893
                guint i;
 
894
                for (i = 0; i < G_N_ELEMENTS (suffixes); i++) {
 
895
                        if (g_str_has_suffix (short_name, suffixes[i]) != FALSE) {
 
896
                                g_free (content_type);
 
897
                                g_free (short_name);
 
898
                                return FALSE;
 
899
                        }
 
900
                }
 
901
                ret = TRUE;
 
902
        } else {
 
903
                ret = FALSE;
 
904
        }
 
905
        g_free (content_type);
 
906
        g_free (short_name);
 
907
 
 
908
        return ret;
 
909
}
 
910
 
 
911
char *
 
912
totem_pl_parser_resolve_uri (GFile *base_gfile,
 
913
                             const char *relative_uri)
 
914
{
 
915
        char *uri, *scheme, *query, *new_relative_uri, *base_uri;
 
916
        GFile *base_parent_gfile, *resolved_gfile;
 
917
 
 
918
        if (relative_uri == NULL) {
 
919
                if (base_gfile == NULL)
 
920
                        return NULL;
 
921
                return g_file_get_uri (base_gfile);
 
922
        }
 
923
 
 
924
        if (base_gfile == NULL)
 
925
                return g_strdup (relative_uri);
 
926
 
 
927
        /* If |relative_uri| has a scheme, it's a full URI, just return it */
 
928
        scheme = g_uri_parse_scheme (relative_uri);
 
929
        if (scheme != NULL) {
 
930
                g_free (scheme);
 
931
                return g_strdup (relative_uri);
 
932
        }
 
933
 
 
934
        /* Check whether we need to get the parent for the base or not */
 
935
        base_uri = g_file_get_path (base_gfile);
 
936
        if (base_uri == NULL)
 
937
                base_uri = g_file_get_uri (base_gfile);
 
938
        if (is_probably_dir (base_uri) == FALSE)
 
939
                base_parent_gfile = g_file_get_parent (base_gfile);
 
940
        else
 
941
                base_parent_gfile = g_object_ref (base_gfile);
 
942
        g_free (base_uri);
 
943
 
 
944
        if (base_parent_gfile == NULL) {
 
945
                resolved_gfile = g_file_resolve_relative_path (base_gfile, relative_uri);
 
946
                uri = g_file_get_uri (resolved_gfile);
 
947
                g_object_unref (resolved_gfile);
 
948
                return uri;
 
949
        }
 
950
 
 
951
        /* Remove the query portion of the URI, to transplant it again
 
952
         * if there is any */
 
953
        query = NULL;
 
954
        new_relative_uri = relative_uri_remove_query (relative_uri, &query);
 
955
 
 
956
        if (new_relative_uri) {
 
957
                char *tmpuri;
 
958
 
 
959
                resolved_gfile = g_file_resolve_relative_path (base_parent_gfile, new_relative_uri);
 
960
                g_object_unref (base_parent_gfile);
 
961
                if (!resolved_gfile) {
 
962
                        char *base_uri;
 
963
                        base_uri = g_file_get_uri (base_gfile);
 
964
                        g_warning ("Failed to resolve relative URI '%s' against base '%s'\n", relative_uri, base_uri);
 
965
                        g_free (base_uri);
 
966
                        g_free (new_relative_uri);
 
967
                        g_free (query);
 
968
                        return NULL;
 
969
                }
 
970
 
 
971
                tmpuri = g_file_get_uri (resolved_gfile);
 
972
                g_object_unref (resolved_gfile);
 
973
                uri = g_strdup_printf ("%s%s", tmpuri, query);
 
974
 
 
975
                g_free (tmpuri);
 
976
                g_free (new_relative_uri);
 
977
                g_free (query);
 
978
 
 
979
                return uri;
 
980
        } else {
 
981
                resolved_gfile = g_file_resolve_relative_path (base_parent_gfile, relative_uri);
 
982
                g_object_unref (base_parent_gfile);
 
983
                if (!resolved_gfile) {
 
984
                        char *base_uri;
 
985
                        base_uri = g_file_get_uri (base_gfile);
 
986
                        g_warning ("Failed to resolve relative URI '%s' against base '%s'\n", relative_uri, base_uri);
 
987
                        g_free (base_uri);
 
988
                        return NULL;
 
989
                }
 
990
 
 
991
                uri = g_file_get_uri (resolved_gfile);
 
992
                g_object_unref (resolved_gfile);
 
993
 
 
994
                return uri;
 
995
        }
 
996
}
 
997
 
861
998
#ifndef TOTEM_PL_PARSER_MINI
862
999
/**
863
1000
 * totem_pl_parser_write_with_title:
1394
1531
 * Removes HTML comments from a string representing the contents of an XML file.
1395
1532
 * The function modifies the string in place.
1396
1533
 */
1397
 
void
 
1534
static void
1398
1535
totem_pl_parser_cleanup_xml (char *contents)
1399
1536
{
1400
1537
        char *needle;
1414
1551
        }
1415
1552
}
1416
1553
 
 
1554
xml_node_t *
 
1555
totem_pl_parser_parse_xml_relaxed (char *contents,
 
1556
                                   gsize size)
 
1557
{
 
1558
        xml_node_t* doc, *node;
 
1559
        char *encoding, *new_contents;
 
1560
        gsize new_size;
 
1561
 
 
1562
        totem_pl_parser_cleanup_xml (contents);
 
1563
        xml_parser_init (contents, size, XML_PARSER_CASE_INSENSITIVE);
 
1564
        if (xml_parser_build_tree_with_options (&doc, XML_PARSER_RELAXED | XML_PARSER_MULTI_TEXT) < 0)
 
1565
                return NULL;
 
1566
 
 
1567
        encoding = NULL;
 
1568
        for (node = doc; node != NULL; node = node->next) {
 
1569
                if (node->name == NULL || g_str_equal (node->name, "?XML") == FALSE)
 
1570
                        continue;
 
1571
                encoding = g_strdup (xml_parser_get_property (node, "ENCODING"));
 
1572
                break;
 
1573
        }
 
1574
 
 
1575
        if (encoding == NULL || g_str_equal (encoding, "UTF-8") != FALSE) {
 
1576
                g_free (encoding);
 
1577
                return doc;
 
1578
        }
 
1579
 
 
1580
        xml_parser_free_tree (doc);
 
1581
 
 
1582
        new_contents = g_convert (contents, size, "UTF-8", encoding, NULL, &new_size, NULL);
 
1583
        if (new_contents == NULL) {
 
1584
                g_warning ("Failed to convert XML data to UTF-8");
 
1585
                g_free (encoding);
 
1586
                return NULL;
 
1587
        }
 
1588
        g_free (encoding);
 
1589
 
 
1590
        xml_parser_init (new_contents, new_size, XML_PARSER_CASE_INSENSITIVE);
 
1591
        if (xml_parser_build_tree_with_options (&doc, XML_PARSER_RELAXED | XML_PARSER_MULTI_TEXT) < 0) {
 
1592
                g_free (new_contents);
 
1593
                return NULL;
 
1594
        }
 
1595
 
 
1596
        g_free (new_contents);
 
1597
 
 
1598
        return doc;
 
1599
}
 
1600
 
1417
1601
static gboolean
1418
1602
totem_pl_parser_ignore_from_mimetype (TotemPlParser *parser, const char *mimetype)
1419
1603
{