-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_direct.js
90 lines (83 loc) · 2.33 KB
/
proto_direct.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
81
82
83
84
85
86
87
88
89
/*
Licensed using GNU GPL terms
(c) 2010, Andrey Smirnov
TODO: proper (c) boilerplate
*/
// x-direct protocol is simulation of http/https for the purpose of simulating existing WWW infrastructure
// class for x-direct protocol handler
function Proto_direct( agent )
{
this.agent = agent;
this.document_root = {};
}
Proto_direct.prototype = {
schema: 'x-direct',
log: function ( msg )
{
this.agent.log( this.schema + ": " + msg );
},
log_error: function ( msg )
{
this.agent.log_error( this.schema + ": " + msg );
},
add_document: function ( path, document, mime_type )
{
// TODO support mime-types
this.document_root[ path ] = document;
},
handle_request: function ( args )
{
var uri = new URI( args.url );
this.log( uri.path );
if ( this.document_root[ uri.path ] )
return new Response( this.document_root[ uri.path ], 200 );
return new Response( null, 404 );
},
query_WWW: function ( method, url, args )
{
var uri = new URI( url );
if ( uri.schema != this.schema )
{
this.agent.log_error( 'Tried to process request for wrong schema ' + uri.schema );
return new Response( null, 501 );
}
if ( method != 'GET' )
{
this.agent.log_error( 'Methods other than GET not supported' );
return new Response( null, 501 );
}
if ( ! uri.authority )
{
this.agent.log_error( 'This protocol requires authority' );
return new Response( null, 501 );
}
// Resolve to address of server
this.log( uri.authority_is_address() );
var address = uri.authority;
if ( ! uri.authority_is_address() )
{
//this.log( '---: ' + 'x-dns:' + uri.authority );
var res_dns = this.agent.query_WWW( 'GET', 'x-dns:' + uri.authority );
//this.log( '------' );
if ( res_dns.is_error() )
return new Response( null, 501 ); // TODO correct code
address = res_dns.get_value( 'A', uri.authority );
}
// Request actual server
var res = this.agent.make_protocol_request( address, this.schema, { method: method, url: url } );
return res;
/*
// Find URI to content
var res_dns = this.agent.query_WWW( 'GET', 'x-dns:' + parts[1] + "?type=URI" );
if ( res_dns.is_ok() )
{
var uri = res_dns.get_value( 'URI', parts[1] );
//this.agent.log( '---' );
if ( ! uri ) return new Response( null, 404 );
var res_uri = this.agent.query_WWW( method, uri );
return res_uri;
}
*/
return new Response( null, 404 );
},
};