Debian: Declare compliance with Debian Policy 4.1.1
[conkeror.git] / modules / mime-type-override.js
blob140ede384cf1b6140eb67bedf547bf3278694e9f
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Portions of this file are derived from the "Open in Browser" extension,
5  * (C) Copyright 2006 Sylvain Pasche.
6  *
7  * Use, modification, and distribution are subject to the terms specified in the
8  * COPYING file.
9  **/
11 /**
12  * This module was inspired by the "Open in Browser" extension by Sylvain
13  * Pasche, available at <http://www.spasche.net/openinbrowser/>.
14  *
15  * This module implements (in a somewhat hacky way) overriding of the
16  * server-specified mime type for specified URLs. It works by registering an
17  * observer for the "http-on-examine-response" and
18  * "http-on-examine-merged-response" topics and changes the response MIME type
19  * of the nsIHTTPChannel object passed as the subject, as well as clearing any
20  * "Content-disposition" header.
21  *
22  * Unfortunately, due to the design, this hack does not work for FTP.
23  *
24  **/
25 { // new scope
27     let EXAMINE_TOPIC = "http-on-examine-response";
28     let EXAMINE_MERGED_TOPIC = "http-on-examine-merged-response";
30     let table = {};
32     let timeout = 10000; // 10000 milliseconds = 10 seconds
34     let table_size = 0;
36     // uri must be an instance of nsIURI
37     var can_override_mime_type_for_uri =
38         function can_override_mime_type_for_uri (uri) {
39             return uri.schemeIs("http") || uri.schemeIs("https");
40         }
42     let clear_override = function(uri_string) {
43         delete table[uri_string];
44         table_size--;
46         if (table_size == 0) {
47             observer_service.removeObserver(observer, EXAMINE_TOPIC);
48             observer_service.removeObserver(observer, EXAMINE_MERGED_TOPIC);
49         }
50     }
52     let observer = {
53         observe: function (subject, topic, data) {
54             if (topic != EXAMINE_TOPIC && topic != EXAMINE_MERGED_TOPIC)
55                 return;
57             subject.QueryInterface(Ci.nsIHttpChannel);
59             var uri_string = subject.originalURI.spec;
60             var obj = table[uri_string];
61             if (!obj)
62                 return;
64             obj.timer.cancel();
66             subject.contentType = obj.mime_type;
68             // drop content-disposition header
69             subject.setResponseHeader("Content-Disposition", "", false);
71             clear_override(uri_string);
72         }
73     };
77     // uri must be an instance of nsIURI and can_override_mime_type_for_uri must
78     // return true for the passed uri.
80     // mime_type must be a string
81     var override_mime_type_for_next_load =
82         function override_mime_type_for_next_load(uri, mime_type) {
83             yield cache_entry_clear(CACHE_SESSION_HTTP, uri);
85             var obj = table[uri.spec];
86             if (obj)
87                 obj.timer.cancel();
88             else
89                 table_size++;
91             obj = { mime_type: mime_type };
93             obj.timer = call_after_timeout(function () {
94                 clear_override(uri.spec);
95             }, timeout);
97             if (table_size == 1) {
98                 observer_service.addObserver(observer, EXAMINE_TOPIC, false);
99                 observer_service.addObserver(observer, EXAMINE_MERGED_TOPIC, false);
100             }
101             table[uri.spec] = obj;
102         }
104 provide("mime-type-override");