Skip to content

Commit 23fdae4

Browse files
author
Daniel Herzog
committed
Refactored the way that contexts work so that the Request Crafter can have it's own. Fixes DFL-2755: Requests done through "Make request" are considered as part of the current context. Very drafty around the crafting view, this will change quite much later on. Right now it only behaves a bit different in terms of redirects: We show the last response we got to the crafted request, instead of the first / immediate response to that request. But it will be changed to show the whole flow.
1 parent c95399c commit 23fdae4

File tree

5 files changed

+400
-338
lines changed

5 files changed

+400
-338
lines changed

src/network/network_request_crafting_view.js

Lines changed: 26 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
88
this._input = null;
99
this._output = null;
1010
this._urlfield = null;
11-
this._is_listening = false;
12-
this._listening_for = null;
13-
this._resources = {};
11+
this._crafter_requests = [];
1412
this._uastring = window.services.scope.get_hello_message().userAgent;
1513

1614
this._request_template = [
@@ -26,14 +24,19 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
2624
].join("\r\n");
2725

2826
this._prev_request = this._request_template;
27+
/*
2928
this._prev_response = "No response";
29+
*/
3030
this._prev_url = "";
3131

32+
/*
33+
// todo: will see what do on send, resetting is probably annoying.
3234
this.ondestroy = function()
3335
{
3436
this._prev_url = this._urlfield ? this._urlfield.get_value() : "";
3537
this._prev_request = this._input ? this._input.get_value() : "";
3638
};
39+
*/
3740

3841
this.createView = function(container)
3942
{
@@ -47,10 +50,15 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
4750

4851
this._render_main_view = function(container)
4952
{
53+
var ctx = this._service.get_request_context(this._service.CONTEXT_TYPE_CRAFTER);
54+
var entries = [];
55+
if (ctx)
56+
entries = ctx.get_entries_filtered();
57+
58+
// render entries..
5059
container.clearAndRender(templates.network.request_crafter_main(this._prev_url,
51-
this._is_listening,
5260
this._prev_request,
53-
this._prev_response));
61+
entries));
5462
this._urlfield = new cls.BufferManager(container.querySelector("input"));
5563
this._input = new cls.BufferManager(container.querySelector("textarea"));
5664
this._output = container.querySelector("code");
@@ -72,7 +80,7 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
7280
this._parse_request = function(requeststr)
7381
{
7482
var retval = {};
75-
var lines = requeststr.split("\r\n");
83+
var lines = requeststr.split(/\r?\n/);
7684
var requestline = lines.shift();
7785
var reqparts = requestline.match(/(\w*?) (.*) (.*)/);
7886

@@ -132,59 +140,24 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
132140
return headers;
133141
};
134142

135-
this._send_request = function(requestdata)
136-
{
137-
var url = this._urlfield.get_value();
138-
var windowid = window_manager_data.get_debug_context();
139-
var request = [
140-
windowid,
141-
url,
142-
requestdata.method,
143-
requestdata.headers,
144-
null, // payload
145-
3, // header policy. 2 == overwrite, 3 == replace
146-
2, // reload policy. 2 == no cache, always reload from network
147-
null, // request content mode
148-
[1, 1] // response content mode 1 == string, 1 == decodee
149-
];
150-
this._listening_for = null;
151-
this._resources = [];
152-
this._is_listening = true;
153-
this.ondestroy(); // saves state of in/out
154-
var tag = window.tagManager.set_callback(null, this._on_send_request_bound)
155-
this._service.requestCreateRequest(tag, request);
156-
this.update();
157-
};
158-
159143
this._handle_send_request_bound = function()
160144
{
145+
this._prev_url = this._urlfield.get_value();
161146
var data = this._input.get_value();
162147
var requestdata = this._parse_request(data);
163148
if (requestdata)
164149
{
165-
this._send_request(requestdata);
150+
var ctx = this._service.get_request_context(this._service.CONTEXT_TYPE_CRAFTER, true);
151+
var crafter_request_id = ctx.send_request(this._prev_url, requestdata);
152+
this._crafter_requests.push(crafter_request_id);
166153
}
167154
else
168155
{
169-
this._prev_response = ui_strings.S_INFO_REQUEST_FAILED;
156+
// this._prev_response = ui_strings.S_INFO_REQUEST_FAILED;
170157
this.update();
171158
}
172159
}.bind(this);
173160

174-
this._on_send_request_bound = function(status, msg)
175-
{
176-
if (status == 0)
177-
{
178-
const RESOURCEID = 0;
179-
this._listening_for = msg[RESOURCEID];
180-
}
181-
else
182-
{
183-
this._stop_loading();
184-
this._prev_response = msg[0];
185-
this.update();
186-
}
187-
}.bind(this);
188161

189162
this._handle_url_change_bound = function(evt, target)
190163
{
@@ -201,124 +174,26 @@ cls.RequestCraftingView = function(id, name, container_class, html, default_hand
201174
this._input.set_value(current);
202175
};
203176

204-
/**
205-
* Since we might get network events before we know what resource we've
206-
* requested, we need to keep track of all of them until we figure it out.
207-
* This method determines if the event in data is still relevant.
208-
*/
209-
this._is_relevant = function(rid)
210-
{
211-
if (!this._is_listening) { return false; }
212-
else if (this._listening_for !== null && rid != this._listening_for) { return false; }
213-
else if (! (rid in this._resources)) { return false; }
214-
else { return true; }
215-
};
216-
217-
this._on_urlload_bound = function(msg)
218-
{
219-
var data = new cls.ResourceManager["1.2"].UrlLoad(msg);
220-
if (!this._is_listening) { return; }
221-
if (this._listening_for !== null && this._listening_for != data.resourceID) { return; }
222-
this._resources[data.resourceID] = {urlload: data};
223-
}.bind(this);
224-
225-
this._on_response_bound = function(msg)
226-
{
227-
var data = new cls.ResourceManager["1.0"].Response(msg);
228-
if (!this._is_relevant(data.resourceID)) { return; }
229-
this._resources[data.resourceID].response = data;
230-
}.bind(this);
231-
232-
this._on_responseheader_bound = function(msg)
233-
{
234-
var data = new cls.ResourceManager["1.0"].ResponseHeader(msg);
235-
if (!this._is_relevant(data.resourceID)) { return; }
236-
this._resources[data.resourceID].responseheader = data;
237-
}.bind(this);
238-
239-
this._on_responsefinished_bound = function(msg)
240-
{
241-
var data = new cls.ResourceManager["1.0"].ResponseFinished(msg);
242-
if (!this._is_relevant(data.resourceID)) { return; }
243-
this._resources[data.resourceID].responsefinished = data;
244-
}.bind(this);
245-
246-
this._on_urlfinished_bound = function(msg)
177+
this._on_context_established_bound = function(message)
247178
{
248-
var data = new cls.ResourceManager["1.0"].UrlFinished(msg);
249-
if (!this._is_relevant(data.resourceID)) { return; }
250-
this._resources[data.resourceID].urlfinished = data;
251-
if (this._listening_for == data.resourceID)
179+
if (message.context_type === this._service.CONTEXT_TYPE_CRAFTER)
252180
{
253-
this._on_got_relevant_response(data);
181+
var ctx = this._service.get_request_context(message.context_type);
182+
ctx.addListener("resource-update", this.update.bind(this));
254183
}
255184
}.bind(this);
256185

257-
this._on_urlredirect_bound = function(msg)
258-
{
259-
var data = new cls.ResourceManager["1.0"].UrlRedirect(msg);
260-
if (!this._is_relevant(data.fromResourceID)) { return; }
261-
this._resources[data.fromResourceID].urlredirect = data;
262-
if (this._listening_for == data.fromResourceID)
263-
{
264-
this._on_got_relevant_response();
265-
}
266-
}.bind(this);
267-
268-
this._on_got_relevant_response = function()
269-
{
270-
var resource = this._resources[this._listening_for];
271-
this._stop_loading();
272-
273-
var response = "";
274-
275-
if (resource.urlfinished && resource.urlfinished.result != 1) // 1 == success
276-
{
277-
response = ui_strings.S_INFO_REQUEST_FAILED;
278-
}
279-
else
280-
{
281-
response = resource.responseheader.raw;
282-
if (!resource.urlredirect)
283-
{
284-
if (resource.responsefinished &&
285-
resource.responsefinished.data &&
286-
resource.responsefinished.data.content &&
287-
resource.responsefinished.data.content.stringData)
288-
{
289-
response += resource.responsefinished.data.content.stringData;
290-
}
291-
}
292-
}
293-
294-
this._prev_response = response;
295-
this.update();
296-
};
297-
298-
this._stop_loading = function()
299-
{
300-
this._is_listening = false;
301-
this._listening_for = null;
302-
this._resources = {};
303-
};
304-
305186
var eh = window.eventHandlers;
306187
eh.click["request-crafter-send"] = this._handle_send_request_bound;
307188
eh.change["request-crafter-url-change"] = this._handle_url_change_bound;
308189
eh.keyup["request-crafter-url-change"] = this._handle_url_change_bound;
309190

191+
this._service = window.network_logger;
192+
this._service.addListener("context-established", this._on_context_established_bound);
193+
310194
// for onchange and buffermanager eh.click["request-crafter-send"] = this._handle_send_request_bound;
311195

312196
this.required_services = ["resource-manager", "document-manager"];
313-
this._service = window.services['resource-manager'];
314-
this._service.addListener("urlload", this._on_urlload_bound);
315-
this._service.addListener("request", this._on_request_bound);
316-
this._service.addListener("response", this._on_response_bound);
317-
this._service.addListener("responseheader", this._on_responseheader_bound);
318-
this._service.addListener("responsefinished", this._on_responsefinished_bound);
319-
this._service.addListener("urlredirect", this._on_urlredirect_bound);
320-
this._service.addListener("urlfinished", this._on_urlfinished_bound);
321-
322197
this.init(id, name, container_class, html, default_handler);
323198
};
324199
cls.RequestCraftingView.prototype = ViewBase;

0 commit comments

Comments
 (0)