-
Notifications
You must be signed in to change notification settings - Fork 1
/
func.js
143 lines (104 loc) · 3.52 KB
/
func.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var http = require('http');
var util = require('util');
function onRequest(request, response) {
console.log("Request received.");
request.addListener("end", function() {
getListener(request, response);
} );
}
function getListener(request, response) {
// sleep( 8000 );
httpGet( request, response );
//response.writeHead(200, {"Content-Type": "text/plain"});
//response.write("Hello World");
// response.write( html );
// response.end();
}
function httpGet( request, response ) {
var hostToGet = "www.joemonster.org";
var options = {
host: hostToGet,
port: 80,
path: request.url,
headers: {
'user-agent' : request.headers['user-agent'],
'accept-charset' : request.headers['accept-charset']
},
method: request.method
};
// console.log(options);
// response.writeHead(200, { "Content-Type:": "text/html; charset=utf8 " } );
var html = http.get(options, function(res ) {
// console.log("Got response: " + res.statusCode);
//response.write( res );
}).on('error', function(e) {
// console.log("Got error: " + e.message);
});
var info = '' ;
html.on("response", function(res) {
res.on("data", function(chunk) {
info += chunk;
} );
res.on("end", function() {
// console.log( info );
// console.log( html.res.socket.headers );
//console.log( html.res.socket );
console.log( html.res.headers['content-type'] );
// info = info.replace( 'href="http://www.joemonster.org', 'href="http://localhost:2222' );
// response.write( info );
response.write( util.inspect( html.res.headers['content-type'] ) );
});
} );
// console.log(html)
// console.log( html.output );
// request.content = '';
// request.addListener('data', function(chunk) {
// request.content += chunk;
// });
//
// request.addListener('end', function() {
//
// try {
// var falsy = http.get(options, function(result) {
// response.writeHead(200, result.header);
// response.write(request.content);
//
// result.on('data', function(cont) {
// response.write(cont);
// });
//
// result.on('error', function(e) {
// console.log("Got error: " + e.message);
// });
//
// result.on('end', function() {
// console.log("end");
// response.end();
// });
//
// });
// console.log(falsy);
//
// falsy.end();
//
// } catch(e) {
// console.log( falsy);
// }
//
// });
}
function sleep(milliseconds) {
var start = new Date().getTime();
for (var i = 0; i < 1e7; i++) {
if ((new Date().getTime() - start) > milliseconds){
break;
}
}
};
function doSomethingWithResponse( html , response, info) {
console.log( html.res.socket );
console.log( html.res.socket.headers );
info = info.replace( 'href="http://www.joemonster.org', 'href="http://localhost:2222' );
response.write( info );
}
exports.onRequest = onRequest;