-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponse.js
81 lines (76 loc) · 1.89 KB
/
response.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
Licensed using GNU GPL terms
(c) 2010, Andrey Smirnov
TODO: proper (c) boilerplate
*/
// class of Response, a convention for DWWW method responses
function Response( content, status )
{
this.content = content;
this.status = status || ( content != null && content != undefined ? 200 : 500 );
this.attrs = {};
}
Response.prototype = {
type: function () // Returns MIME-type of received content
{
// TODO: correct checking and ability to specify type manually
if ( this.is_error() )
return null;
if ( this.content.match( /^<html>/ ) )
{
return 'text/html';
}
return 'text/plain'; // Default
},
is_error: function () { return this.status != 200; },
is_ok: function () { return ! this.is_error(); },
dump: function ()
{
if ( this.is_error )
return "[ERROR RESPONSE status=[" + this.status + "]]";
var content = this.content;
content.replace( /[&]/g, '&' );
content.replace( /["]/g, '"' ); //"
content.replace( /</g, '<' );
content.replace( />/g, '>' );
return content;
}
};
function DNSResponse ()
{
this.info = [];
}
DNSResponse.prototype = new Response();
DNSResponse.prototype.type = function () { return 'application/x-dns-response'; }
DNSResponse.prototype.add_info = function ( name, value, type, class )
{
// 0 1 2
this.info.push( [ name, value, type, ( class || 'IN' ) ] );
this.status = 200;
return this; // Allows chaining
};
DNSResponse.prototype.get_value = function ( type, fqdn )
{
//log( "t" + type + "f" + fqdn );
for ( var i in this.info )
{
//log( "<<<" + this.info[i][1] );
if ( type == this.info[ i ][ 2 ]
&& fqdn == this.info[ i ][ 0 ] )
{
//log( "---" );
return this.info[ i ][ 1 ];
}
}
return null;
};
DNSResponse.prototype.dump = function ()
{
var res = "";
for ( var i in this.info )
{
var i = this.info[ i ];
res += i[3] + "\t" + i[2] + "\t" + i[0] + "\t" + i[1] + "\n";
}
return res;
}