-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsimplesoup.js
130 lines (123 loc) · 3.96 KB
/
simplesoup.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
/*
* simplesoup.js - simple libsoup wrapper
*/
const Lang = imports.lang;
const Signals = imports.signals;
const Format = imports.format;
String.prototype.format = Format.format;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const Soup = imports.gi.Soup;
const SimpleSoupError = function(errormsg) {
this.message = errormsg;
}
SimpleSoupError.prototype = new Error();
var SimpleSoup = new Lang.Class({
Name: "SimpleSoup",
Signals: {
'progress': {}
},
_init: function(props) {
this.user_agent = props.user_agent || 'simplesoup.js';
this.chunk_size = props.chunk_size || 4098;
this.debug = props.debug || GLib.getenv("SOUP_DEBUG");
},
_emit_progress: function(part, whole) {
if(whole) {
this.emit('progress', part/(typeof(whole) == "string" ? parseFloat(whole) : whole));
}
},
call: function(method, url, request, cancellable, callback) {
cancellable = cancellable || null;
let soup = new Soup.Session();
let souprequest = soup.request_http(method, url);
let message = souprequest.get_message();
let headers = message['request-headers'];
let response = {"headers": {}};
if(this.debug) {
let debuglevel = {"minimal": Soup.LoggerLogLevel.MINIMAL,
"headers": Soup.LoggerLogLevel.HEADERS,
"body": Soup.LoggerLogLevel.BODY};
let debug = debuglevel[this.debug] || debuglevel['body'];
soup['add-feature'] = Soup.Logger.new(debug, -1);
}
headers.append("User-Agent", this.user_agent);
message.connect("got-headers", Lang.bind(response, function(message) {
this.statuscode = message['status-code'];
message['response-headers'].foreach(Lang.bind(this, function(name, value) {
this.headers[name] = value;
}));
}));
if(request) {
if(request.headers) {
for(let header in request.headers) {
headers.append(header, request.headers[header]);
}
}
if(request.body) {
message.set_request(headers.get('Content-Type') || 'text/plain',
Soup.MemoryUse.COPY,
request.body);
}
}
this._emit_progress(0.02, 1);
if(callback) {
souprequest.send_async(cancellable,
Lang.bind(this, function(soup,
asyncresult,
cancellable,
callback,
response) {
let stream = soup.send_finish(asyncresult);
stream.result = '';
stream.resultsize = 0;
stream.chunk_size = this.chunk_size;
stream.callback = Lang.bind(this, function(stream,
asyncresult,
cancellable,
callback,
response) {
let result = stream.read_bytes_finish(asyncresult);
let size = result.get_size();
if(size > 0) {
stream.resultsize += size;
this._emit_progress(stream.resultsize, response.headers['Content-Length']);
stream.result += result.get_data().toString();
stream.read_bytes_async(stream.chunk_size,
GLib.PRIORITY_DEFAULT,
cancellable,
stream.callback);
}
else {
response.body = stream.result;
this._emit_progress(1, 1);
callback(response);
}
}, cancellable, callback, response);
stream.read_bytes_async(stream.chunk_size,
GLib.PRIORITY_DEFAULT,
cancellable,
stream.callback);
}, cancellable, callback, response));
return true;
}
else {
let stream = souprequest.send(cancellable);
stream.result = '';
stream.resultsize = 0;
stream.chunk_size = response.chunk_size;
while(true) {
let result = stream.read_bytes(stream.chunk_size, cancellable);
let size = result.get_size();
if(size <= 0) break;
stream.resultsize += size;
this._emit_progress(stream.resultsize, response.headers['Content-Length']);
stream.result += result.get_data().toString();
}
response.body = stream.result;
this._emit_progress(1, 1);
return response;
}
}
});
Signals.addSignalMethods(SimpleSoup.prototype);