-
Notifications
You must be signed in to change notification settings - Fork 3
/
NodeLoader.hx
167 lines (139 loc) · 3.61 KB
/
NodeLoader.hx
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package yloader.impl.js;
import js.node.buffer.Buffer;
import js.node.http.ClientRequest;
import js.node.http.IncomingMessage;
import js.node.Http;
import js.node.Https;
import yloader.ILoader;
import yloader.util.ParameterUtil;
import yloader.util.StatusCodeUtil;
import yloader.util.UrlUtil;
import yloader.valueObject.Parameter;
import yloader.valueObject.ParsedUrl;
import yloader.valueObject.Request;
import yloader.valueObject.Response;
class NodeLoader implements ILoader
{
public var onResponse:Response->Void;
public var request:Request;
public var responseEncoding = "utf-8";
var clientRequest:ClientRequest;
var response:IncomingMessage;
var responseString:String;
public function new(?request:Request)
{
this.request = request;
}
public function load():Void
{
cancel();
clientRequest = createClientRequest();
addClientRequestListeners();
writeData();
endRequest();
}
public function cancel():Void
{
if (clientRequest != null)
{
removeClientRequestListeners();
clientRequest.abort();
}
clientRequest = null;
response = null;
responseString = "";
}
function createClientRequest():ClientRequest
{
var url = UrlUtil.parse(request.urlWithQuery);
var options = createOptions(url);
if (url.protocol == "http:")
return Http.request(options, onRequestResponse);
if (url.protocol == "https:")
return Https.request(options, onRequestResponse);
return null;
}
function createOptions(url:ParsedUrl):HttpsRequestOptions
{
return {
host: url.host,
port: Std.parseInt(url.port),
path: url.path,
method: Std.string(request.method),
headers: ParameterUtil.toObject(createHeaders())
};
}
function createHeaders():Array<Parameter>
{
var result = request.headers.copy();
if (requestHasData() && ParameterUtil.getContentLength(result) == null)
ParameterUtil.setContentLength(Buffer.byteLength(request.data), result);
return result;
}
function addClientRequestListeners():Void
{
clientRequest.on("error", onClientRequestError);
}
function removeClientRequestListeners():Void
{
clientRequest.removeListener("error", onClientRequestError);
}
function writeData():Void
{
if (requestHasData())
clientRequest.write(request.data);
}
function endRequest():Void
{
clientRequest.end();
}
function requestHasData():Bool
{
return request.data != null && request.data.length > 0;
}
function handleResponse(responseObject:Dynamic)
{
if(onResponse == null || response == null)
return;
var response = getResponse(responseObject);
onResponse(response);
}
function getResponse(responseObject:Dynamic):Response
{
return new Response(isSuccess(response.statusCode), responseObject, response.statusCode,
response.statusMessage, rawHeadersToParameters(response.rawHeaders));
}
function isSuccess(statusCode:Int):Bool
{
return StatusCodeUtil.isSuccess(statusCode);
}
function rawHeadersToParameters(source:Array<String>):Array<Parameter>
{
var i = 0, result:Array<Parameter> = [];
while (i < source.length)
result.push(new Parameter(source[i++], source[i++]));
return result;
}
function onRequestResponse(response:IncomingMessage):Void
{
this.response = response;
response.setEncoding(responseEncoding);
response.on("data", onIncomingMessageData);
response.on("end", onIncomingMessageEnd);
}
function onIncomingMessageData(data:String):Void
{
responseString += data;
}
function onIncomingMessageEnd(data:Dynamic):Void
{
handleResponse(responseString);
cancel();
}
function onClientRequestError(error:Dynamic):Void
{
if (onResponse != null)
onResponse(new Response(false, null, 0, null, null));
cancel();
}
}