-
Notifications
You must be signed in to change notification settings - Fork 0
/
contentType.js
61 lines (53 loc) · 1.91 KB
/
contentType.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* This file is part of ABP-Proxy <https://github.com/FlowerCode/abp-proxy>,
* Copyright (C) 2014 FlowerCode
*
* ABP-Proxy is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* ABP-Proxy 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 ABP-Proxy. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var URI = require('URIjs');
module.exports.contentTypeFromRequest = function (req) {
var accept = req.headers['accept'] || '';
accept = accept.toLowerCase().split(',')[0];
if (accept.indexOf('script') != -1) {
return "SCRIPT";
} else if (accept.indexOf('image') != -1) {
return "IMAGE";
} else if (accept.indexOf('css') != -1) {
return "STYLESHEET";
} else if (accept.indexOf('text') != -1) {
if (new URI(req.url).path() == '/') {
return "DOCUMENT";
} else {
return "SUBDOCUMENT";
}
} else {
var requestedWith = req.headers['x-requested-with'] || '';
if (requestedWith.toLowerCase() == 'xmlhttprequest') {
return "XMLHTTPREQUEST";
}
var url = new URI(req.url).path().toLowerCase();
if (/\.js$/.test(url)) {
return "SCRIPT";
} else if (/\.css$/.test(url)) {
return "STYLESHEET";
} else if (/\.(?:gif|png|jpe?g|bmp|ico)$/.test(url)) {
return "IMAGE";
} else if (/\.(?:ttf|woff)$/.test(url)) {
return "FONT";
} else if (/\.swf$/.test(url)) {
return "OBJECT";
}
return "OTHER";
}
};