-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver7.js
More file actions
87 lines (83 loc) · 2.82 KB
/
server7.js
File metadata and controls
87 lines (83 loc) · 2.82 KB
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
var http = require('http')
, https = require('https');
//////////////
// test app //
//////////////
http.createServer(function (req, res) {
console.log('WEB HIT');
res.writeHead(200);
res.end
( '<html>\n'
+ '<script>\n'
+ 'function testPut() {\n'
+ ' var token = window.location.hash.substring(14, 74);\n'
+ ' var xhr = new XMLHttpRequest();\n'
+ ' xhr.open(\'PUT\', \'http://myfavouritesandwich.org:9002/\', true);\n'
+ ' xhr.onreadystatechange=function() {\n'
+ ' if(xhr.status=200) {\n'
+ ' alert(xhr.getResponseHeader(\'Location\'));\n'
+ ' }\n'
+ ' };\n'
+ ' xhr.setRequestHeader(\'Authorization\', \'Bearer \'+token);\n'
+ ' xhr.send(\'blabla\');\n'
+ '}\n'
+ '</script>\n'
+ '<a href="" onclick="testPut();">test PUT</a>\n'
+ '<a href="'
+ 'https://accounts.google.com/o/oauth2/auth?'
+ 'client_id=709507725318-4h19nag3k4hv5osj1jvao0j3an3bu43t@developer.gserviceaccount.com&'
+ 'redirect_uri=http://myfavouritesandwich.org:9000/&'
+ 'scope=http://docs.google.com/feeds/&'
+ 'response_type=token'
+ '">click</a>\n'
+ '</html>\n');
}).listen(9000);
////////////////////////////////
// gdata-remoteStorage bridge //
////////////////////////////////
http.createServer(function (req, res) {
console.log('REQ.URL:'+req.url);
console.log('REQ.HEADERS:'+JSON.stringify(req.headers));
var token;
if(req.headers['authorization']) {
token = req.headers['authorization'].substring(7);
} else if(req.headers['Authorization']) {
token = req.headers['Authorization'].substring(7);
}
console.log('TOKEN:'+token);
var options =
{ 'host': 'docs.google.com'
, 'port': 443
, 'method': 'POST'
, 'path': '/feeds/default/private/full?alt=json'
, 'headers':
{ 'GData-Version': '3.0'
, 'Authorization': 'Bearer '+token
, 'Content-Length': '0'
, 'Content-Type': 'text/plain'
, 'Slug': 'LetsRide'
, 'X-Upload-Content-Length': '30'
, 'X-Upload-Content-Type': 'text/plain'
}
};
var req2 = https.request(options, function(res2) {
var responseHeaders = res2.headers;
//add CORS to response:
responseHeaders['Access-Control-Allow-Origin'] = '*';
responseHeaders['Access-Control-Allow-Methods'] = 'GET, PUT, DELETE';
responseHeaders['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Authorization';
//replace status with 200:
//res.writeHead(res2.statusCode, responseHeaders);
responseHeaders['X-Status'] = res2.statusCode;
res.writeHead(200, responseHeaders);
res.write('responding:');
res2.setEncoding('utf8');
res2.on('data', function (chunk) {
console.log(chunk);
res.write(chunk);
res.end(':gnidnopser');
});
});
req2.end();
console.log(JSON.stringify(options.headers));
}).listen(9002);