@@ -183,12 +183,10 @@ std::unique_ptr<protocol::Network::Response> createResponseFromObject(
183183
184184NetworkAgent::NetworkAgent (NetworkInspector* inspector,
185185 v8_inspector::V8Inspector* v8_inspector,
186- Environment* env,
187- std::shared_ptr<protocol::IoAgent> io_agent)
186+ Environment* env)
188187 : inspector_(inspector),
189188 v8_inspector_ (v8_inspector),
190- env_(env),
191- io_agent_(io_agent) {
189+ env_(env) {
192190 event_notifier_map_[" requestWillBeSent" ] = &NetworkAgent::requestWillBeSent;
193191 event_notifier_map_[" responseReceived" ] = &NetworkAgent::responseReceived;
194192 event_notifier_map_[" loadingFailed" ] = &NetworkAgent::loadingFailed;
@@ -221,144 +219,11 @@ protocol::DispatchResponse NetworkAgent::disable() {
221219 return protocol::DispatchResponse::Success ();
222220}
223221
224- std::tuple<int , std::string, std::string> NetworkAgent::spawnFetchProcess (
225- std::string_view code, Environment* env, std::string_view url) {
226- std::string stdout_result;
227- std::string stderr_result;
228- uv_loop_t * loop = new uv_loop_t ;
229- uv_loop_init (loop);
230- uv_process_t child;
231- uv_pipe_t stdout_pipe;
232- uv_pipe_init (loop, &stdout_pipe, 0 );
233- uv_pipe_t stderr_pipe;
234- uv_pipe_init (loop, &stderr_pipe, 0 );
235-
236- uv_process_options_t uv_process_options;
237- std::string command =
238- env->exec_path () + " --eval \" " + code.data () + " \" -- " + url.data ();
239-
240- const char * file = env->exec_path ().c_str ();
241- char * args[] = {const_cast <char *>(file),
242- const_cast <char *>(" --eval" ),
243- reinterpret_cast <char *>(const_cast <char *>(code.data ())),
244- reinterpret_cast <char *>(const_cast <char *>(url.data ())),
245- nullptr };
246-
247- uv_stdio_container_t stdio[3 ];
248- uv_process_options.file = file;
249- uv_process_options.args = args;
250- uv_process_options.flags = 0 ;
251- uv_process_options.stdio_count = 3 ;
252- uv_process_options.stdio = stdio;
253- uv_process_options.cwd = nullptr ;
254- uv_process_options.env = nullptr ;
255-
256- uv_process_options.exit_cb =
257- [](uv_process_t * req, int64_t exit_status, int term_signal) {
258- uv_close (reinterpret_cast <uv_handle_t *>(req), nullptr );
259- };
260-
261- stdio[0 ].flags = UV_INHERIT_FD;
262- stdio[0 ].data .fd = 0 ;
263- stdio[1 ].flags =
264- static_cast <uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
265- stdio[1 ].data .stream = reinterpret_cast <uv_stream_t *>(&stdout_pipe);
266- stdio[2 ].flags =
267- static_cast <uv_stdio_flags>(UV_CREATE_PIPE | UV_WRITABLE_PIPE);
268- stdio[2 ].data .stream = reinterpret_cast <uv_stream_t *>(&stderr_pipe);
269-
270- int r = uv_spawn (loop, &child, &uv_process_options);
271-
272- if (r != 0 ) {
273- uv_loop_close (loop);
274- delete loop;
275- return {r, stdout_result, stderr_result};
276- }
277-
278- auto alloc_cb =
279- [](uv_handle_t * handle, size_t suggested_size, uv_buf_t * buf) {
280- buf->base = static_cast <char *>(malloc (suggested_size));
281- buf->len = suggested_size;
282- };
283-
284- auto read_cb = [](uv_stream_t * stream, ssize_t nread, const uv_buf_t * buf) {
285- auto * response = static_cast <std::string*>(stream->data );
286- if (nread > 0 ) {
287- response->append (buf->base , nread);
288- } else if (nread < 0 ) {
289- if (!response->empty () && response->back () == ' \n ' ) {
290- response->pop_back ();
291- }
292- uv_close (reinterpret_cast <uv_handle_t *>(stream), nullptr );
293- }
294- if (buf->base ) free (buf->base );
295- };
296-
297- stdout_pipe.data = &stdout_result;
298- uv_read_start (
299- reinterpret_cast <uv_stream_t *>(&stdout_pipe), alloc_cb, read_cb);
300-
301- stderr_pipe.data = &stderr_result;
302- uv_read_start (
303- reinterpret_cast <uv_stream_t *>(&stderr_pipe), alloc_cb, read_cb);
304-
305- uv_run (loop, UV_RUN_DEFAULT);
306-
307- uv_walk (
308- loop,
309- [](uv_handle_t * handle, void *) {
310- if (!uv_is_closing (handle)) {
311- uv_close (handle, nullptr );
312- }
313- },
314- nullptr );
315-
316- uv_run (loop, UV_RUN_DEFAULT);
317-
318- uv_loop_close (loop);
319- delete loop;
320- return {r, stdout_result, stderr_result};
321- }
322-
323222protocol::DispatchResponse NetworkAgent::loadNetworkResource (
324223 const protocol::String& in_url,
325224 std::unique_ptr<protocol::Network::LoadNetworkResourcePageResult>*
326225 out_resource) {
327- if (!env_->options ()->experimental_inspector_network_resource ) {
328- return protocol::DispatchResponse::MethodNotFound (
329- " Network.loadNetworkResource is not supported in this environment. "
330- " Please enable the experimental-inspector-network-resource option." );
331- }
332- DCHECK (io_agent_);
333-
334- std::string code = R"(
335- fetch(process.argv[1], {signal: AbortSignal.timeout(2000) }).then(res => {
336- if (res.ok) {
337- res.text().then(console.log)
338- } else {
339- throw new Error('Network error: ' + res.status);
340- }
341- })
342- )" ;
343-
344- auto [r, response, err] = spawnFetchProcess (code, env_, in_url);
345- if (r == 0 && err.empty ()) {
346- std::string uuid = std::to_string (load_id_counter_);
347- load_id_counter_++;
348- io_agent_->setData (uuid, response);
349- auto result = protocol::Network::LoadNetworkResourcePageResult::create ()
350- .setSuccess (true )
351- .setStream (uuid)
352- .build ();
353- out_resource->reset (result.release ());
354- } else {
355- auto result = protocol::Network::LoadNetworkResourcePageResult::create ()
356- .setSuccess (false )
357- .build ();
358- out_resource->reset (result.release ());
359- }
360-
361- return protocol::DispatchResponse::Success ();
226+ return protocol::DispatchResponse::FallThrough ();
362227}
363228
364229void NetworkAgent::requestWillBeSent (v8::Local<v8::Context> context,
0 commit comments