GCC Code Coverage Report
Directory: ../src/ Exec Total Coverage
File: /home/node-core-coverage/node-core-coverage/workdir/node/out/../src/inspector_socket.cc Lines: 0 295 0.0 %
Date: 2016-12-18 Branches: 0 151 0.0 %

Line Branch Exec Source
1
#include "inspector_socket.h"
2
#include "util.h"
3
#include "util-inl.h"
4
5
#define NODE_WANT_INTERNALS 1
6
#include "base64.h"
7
8
#include "openssl/sha.h"  // Sha-1 hash
9
10
#include <string.h>
11
#include <vector>
12
13
#define ACCEPT_KEY_LENGTH base64_encoded_size(20)
14
#define BUFFER_GROWTH_CHUNK_SIZE 1024
15
16
#define DUMP_READS 0
17
#define DUMP_WRITES 0
18
19
namespace node {
20
namespace inspector {
21
22
static const char CLOSE_FRAME[] = {'\x88', '\x00'};
23
24
enum ws_decode_result {
25
  FRAME_OK, FRAME_INCOMPLETE, FRAME_CLOSE, FRAME_ERROR
26
};
27
28
#if DUMP_READS || DUMP_WRITES
29
static void dump_hex(const char* buf, size_t len) {
30
  const char* ptr = buf;
31
  const char* end = ptr + len;
32
  const char* cptr;
33
  char c;
34
  int i;
35
36
  while (ptr < end) {
37
    cptr = ptr;
38
    for (i = 0; i < 16 && ptr < end; i++) {
39
      printf("%2.2X  ", static_cast<unsigned char>(*(ptr++)));
40
    }
41
    for (i = 72 - (i * 4); i > 0; i--) {
42
      printf(" ");
43
    }
44
    for (i = 0; i < 16 && cptr < end; i++) {
45
      c = *(cptr++);
46
      printf("%c", (c > 0x19) ? c : '.');
47
    }
48
    printf("\n");
49
  }
50
  printf("\n\n");
51
}
52
#endif
53
54
static void remove_from_beginning(std::vector<char>* buffer, size_t count) {
55
  buffer->erase(buffer->begin(), buffer->begin() + count);
56
}
57
58
static void dispose_inspector(uv_handle_t* handle) {
59
  InspectorSocket* inspector = inspector_from_stream(handle);
60
  inspector_cb close =
61
      inspector->ws_mode ? inspector->ws_state->close_cb : nullptr;
62
  inspector->buffer.clear();
63
  delete inspector->ws_state;
64
  inspector->ws_state = nullptr;
65
  if (close) {
66
    close(inspector, 0);
67
  }
68
}
69
70
static void close_connection(InspectorSocket* inspector) {
71
  uv_handle_t* socket = reinterpret_cast<uv_handle_t*>(&inspector->client);
72
  if (!uv_is_closing(socket)) {
73
    uv_read_stop(reinterpret_cast<uv_stream_t*>(socket));
74
    uv_close(socket, dispose_inspector);
75
  }
76
}
77
78
struct WriteRequest {
79
  WriteRequest(InspectorSocket* inspector, const char* data, size_t size)
80
      : inspector(inspector)
81
      , storage(data, data + size)
82
      , buf(uv_buf_init(&storage[0], storage.size())) {}
83
84
  static WriteRequest* from_write_req(uv_write_t* req) {
85
    return node::ContainerOf(&WriteRequest::req, req);
86
  }
87
88
  InspectorSocket* const inspector;
89
  std::vector<char> storage;
90
  uv_write_t req;
91
  uv_buf_t buf;
92
};
93
94
// Cleanup
95
static void write_request_cleanup(uv_write_t* req, int status) {
96
  delete WriteRequest::from_write_req(req);
97
}
98
99
static int write_to_client(InspectorSocket* inspector,
100
                           const char* msg,
101
                           size_t len,
102
                           uv_write_cb write_cb = write_request_cleanup) {
103
#if DUMP_WRITES
104
  printf("%s (%ld bytes):\n", __FUNCTION__, len);
105
  dump_hex(msg, len);
106
#endif
107
108
  // Freed in write_request_cleanup
109
  WriteRequest* wr = new WriteRequest(inspector, msg, len);
110
  uv_stream_t* stream = reinterpret_cast<uv_stream_t*>(&inspector->client);
111
  return uv_write(&wr->req, stream, &wr->buf, 1, write_cb) < 0;
112
}
113
114
// Constants for hybi-10 frame format.
115
116
typedef int OpCode;
117
118
const OpCode kOpCodeContinuation = 0x0;
119
const OpCode kOpCodeText = 0x1;
120
const OpCode kOpCodeBinary = 0x2;
121
const OpCode kOpCodeClose = 0x8;
122
const OpCode kOpCodePing = 0x9;
123
const OpCode kOpCodePong = 0xA;
124
125
const unsigned char kFinalBit = 0x80;
126
const unsigned char kReserved1Bit = 0x40;
127
const unsigned char kReserved2Bit = 0x20;
128
const unsigned char kReserved3Bit = 0x10;
129
const unsigned char kOpCodeMask = 0xF;
130
const unsigned char kMaskBit = 0x80;
131
const unsigned char kPayloadLengthMask = 0x7F;
132
133
const size_t kMaxSingleBytePayloadLength = 125;
134
const size_t kTwoBytePayloadLengthField = 126;
135
const size_t kEightBytePayloadLengthField = 127;
136
const size_t kMaskingKeyWidthInBytes = 4;
137
138
static std::vector<char> encode_frame_hybi17(const char* message,
139
                                             size_t data_length) {
140
  std::vector<char> frame;
141
  OpCode op_code = kOpCodeText;
142
  frame.push_back(kFinalBit | op_code);
143
  if (data_length <= kMaxSingleBytePayloadLength) {
144
    frame.push_back(static_cast<char>(data_length));
145
  } else if (data_length <= 0xFFFF) {
146
    frame.push_back(kTwoBytePayloadLengthField);
147
    frame.push_back((data_length & 0xFF00) >> 8);
148
    frame.push_back(data_length & 0xFF);
149
  } else {
150
    frame.push_back(kEightBytePayloadLengthField);
151
    char extended_payload_length[8];
152
    size_t remaining = data_length;
153
    // Fill the length into extended_payload_length in the network byte order.
154
    for (int i = 0; i < 8; ++i) {
155
      extended_payload_length[7 - i] = remaining & 0xFF;
156
      remaining >>= 8;
157
    }
158
    frame.insert(frame.end(), extended_payload_length,
159
                 extended_payload_length + 8);
160
    ASSERT_EQ(0, remaining);
161
  }
162
  frame.insert(frame.end(), message, message + data_length);
163
  return frame;
164
}
165
166
static ws_decode_result decode_frame_hybi17(const std::vector<char>& buffer,
167
                                            bool client_frame,
168
                                            int* bytes_consumed,
169
                                            std::vector<char>* output,
170
                                            bool* compressed) {
171
  *bytes_consumed = 0;
172
  if (buffer.size() < 2)
173
    return FRAME_INCOMPLETE;
174
175
  auto it = buffer.begin();
176
177
  unsigned char first_byte = *it++;
178
  unsigned char second_byte = *it++;
179
180
  bool final = (first_byte & kFinalBit) != 0;
181
  bool reserved1 = (first_byte & kReserved1Bit) != 0;
182
  bool reserved2 = (first_byte & kReserved2Bit) != 0;
183
  bool reserved3 = (first_byte & kReserved3Bit) != 0;
184
  int op_code = first_byte & kOpCodeMask;
185
  bool masked = (second_byte & kMaskBit) != 0;
186
  *compressed = reserved1;
187
  if (!final || reserved2 || reserved3)
188
    return FRAME_ERROR;  // Only compression extension is supported.
189
190
  bool closed = false;
191
  switch (op_code) {
192
    case kOpCodeClose:
193
      closed = true;
194
      break;
195
    case kOpCodeText:
196
      break;
197
    case kOpCodeBinary:        // We don't support binary frames yet.
198
    case kOpCodeContinuation:  // We don't support binary frames yet.
199
    case kOpCodePing:          // We don't support binary frames yet.
200
    case kOpCodePong:          // We don't support binary frames yet.
201
    default:
202
      return FRAME_ERROR;
203
  }
204
205
  // In Hybi-17 spec client MUST mask its frame.
206
  if (client_frame && !masked) {
207
    return FRAME_ERROR;
208
  }
209
210
  uint64_t payload_length64 = second_byte & kPayloadLengthMask;
211
  if (payload_length64 > kMaxSingleBytePayloadLength) {
212
    int extended_payload_length_size;
213
    if (payload_length64 == kTwoBytePayloadLengthField) {
214
      extended_payload_length_size = 2;
215
    } else if (payload_length64 == kEightBytePayloadLengthField) {
216
      extended_payload_length_size = 8;
217
    } else {
218
      return FRAME_ERROR;
219
    }
220
    if ((buffer.end() - it) < extended_payload_length_size)
221
      return FRAME_INCOMPLETE;
222
    payload_length64 = 0;
223
    for (int i = 0; i < extended_payload_length_size; ++i) {
224
      payload_length64 <<= 8;
225
      payload_length64 |= static_cast<unsigned char>(*it++);
226
    }
227
  }
228
229
  static const uint64_t max_payload_length = 0x7FFFFFFFFFFFFFFFull;
230
  static const size_t max_length = SIZE_MAX;
231
  if (payload_length64 > max_payload_length ||
232
      payload_length64 > max_length - kMaskingKeyWidthInBytes) {
233
    // WebSocket frame length too large.
234
    return FRAME_ERROR;
235
  }
236
  size_t payload_length = static_cast<size_t>(payload_length64);
237
238
  if (buffer.size() - kMaskingKeyWidthInBytes < payload_length)
239
    return FRAME_INCOMPLETE;
240
241
  std::vector<char>::const_iterator masking_key = it;
242
  std::vector<char>::const_iterator payload = it + kMaskingKeyWidthInBytes;
243
  for (size_t i = 0; i < payload_length; ++i)  // Unmask the payload.
244
    output->insert(output->end(),
245
                   payload[i] ^ masking_key[i % kMaskingKeyWidthInBytes]);
246
247
  size_t pos = it + kMaskingKeyWidthInBytes + payload_length - buffer.begin();
248
  *bytes_consumed = pos;
249
  return closed ? FRAME_CLOSE : FRAME_OK;
250
}
251
252
static void invoke_read_callback(InspectorSocket* inspector,
253
                                 int status, const uv_buf_t* buf) {
254
  if (inspector->ws_state->read_cb) {
255
    inspector->ws_state->read_cb(
256
        reinterpret_cast<uv_stream_t*>(&inspector->client), status, buf);
257
  }
258
}
259
260
static void shutdown_complete(InspectorSocket* inspector) {
261
  close_connection(inspector);
262
}
263
264
static void on_close_frame_written(uv_write_t* req, int status) {
265
  WriteRequest* wr = WriteRequest::from_write_req(req);
266
  InspectorSocket* inspector = wr->inspector;
267
  delete wr;
268
  inspector->ws_state->close_sent = true;
269
  if (inspector->ws_state->received_close) {
270
    shutdown_complete(inspector);
271
  }
272
}
273
274
static void close_frame_received(InspectorSocket* inspector) {
275
  inspector->ws_state->received_close = true;
276
  if (!inspector->ws_state->close_sent) {
277
    invoke_read_callback(inspector, 0, 0);
278
    write_to_client(inspector, CLOSE_FRAME, sizeof(CLOSE_FRAME),
279
                    on_close_frame_written);
280
  } else {
281
    shutdown_complete(inspector);
282
  }
283
}
284
285
static int parse_ws_frames(InspectorSocket* inspector) {
286
  int bytes_consumed = 0;
287
  std::vector<char> output;
288
  bool compressed = false;
289
290
  ws_decode_result r =  decode_frame_hybi17(inspector->buffer,
291
                                            true /* client_frame */,
292
                                            &bytes_consumed, &output,
293
                                            &compressed);
294
  // Compressed frame means client is ignoring the headers and misbehaves
295
  if (compressed || r == FRAME_ERROR) {
296
    invoke_read_callback(inspector, UV_EPROTO, nullptr);
297
    close_connection(inspector);
298
    bytes_consumed = 0;
299
  } else if (r == FRAME_CLOSE) {
300
    close_frame_received(inspector);
301
    bytes_consumed = 0;
302
  } else if (r == FRAME_OK && inspector->ws_state->alloc_cb
303
             && inspector->ws_state->read_cb) {
304
    uv_buf_t buffer;
305
    size_t len = output.size();
306
    inspector->ws_state->alloc_cb(
307
        reinterpret_cast<uv_handle_t*>(&inspector->client),
308
        len, &buffer);
309
    CHECK_GE(buffer.len, len);
310
    memcpy(buffer.base, &output[0], len);
311
    invoke_read_callback(inspector, len, &buffer);
312
  }
313
  return bytes_consumed;
314
}
315
316
static void prepare_buffer(uv_handle_t* stream, size_t len, uv_buf_t* buf) {
317
  *buf = uv_buf_init(new char[len], len);
318
}
319
320
static void reclaim_uv_buf(InspectorSocket* inspector, const uv_buf_t* buf,
321
                           ssize_t read) {
322
  if (read > 0) {
323
    std::vector<char>& buffer = inspector->buffer;
324
    buffer.insert(buffer.end(), buf->base, buf->base + read);
325
  }
326
  delete[] buf->base;
327
}
328
329
static void websockets_data_cb(uv_stream_t* stream, ssize_t nread,
330
                               const uv_buf_t* buf) {
331
  InspectorSocket* inspector = inspector_from_stream(stream);
332
  reclaim_uv_buf(inspector, buf, nread);
333
  if (nread < 0 || nread == UV_EOF) {
334
    inspector->connection_eof = true;
335
    if (!inspector->shutting_down && inspector->ws_state->read_cb) {
336
      inspector->ws_state->read_cb(stream, nread, nullptr);
337
    }
338
  } else {
339
    #if DUMP_READS
340
      printf("%s read %ld bytes\n", __FUNCTION__, nread);
341
      if (nread > 0) {
342
        dump_hex(inspector->buffer.data() + inspector->buffer.size() - nread,
343
                 nread);
344
      }
345
    #endif
346
    // 2. Parse.
347
    int processed = 0;
348
    do {
349
      processed = parse_ws_frames(inspector);
350
      // 3. Fix the buffer size & length
351
      if (processed > 0) {
352
        remove_from_beginning(&inspector->buffer, processed);
353
      }
354
    } while (processed > 0 && !inspector->buffer.empty());
355
  }
356
}
357
358
int inspector_read_start(InspectorSocket* inspector,
359
                          uv_alloc_cb alloc_cb, uv_read_cb read_cb) {
360
  ASSERT(inspector->ws_mode);
361
  ASSERT(!inspector->shutting_down || read_cb == nullptr);
362
  inspector->ws_state->close_sent = false;
363
  inspector->ws_state->alloc_cb = alloc_cb;
364
  inspector->ws_state->read_cb = read_cb;
365
  int err =
366
      uv_read_start(reinterpret_cast<uv_stream_t*>(&inspector->client),
367
                    prepare_buffer,
368
                    websockets_data_cb);
369
  if (err < 0) {
370
    close_connection(inspector);
371
  }
372
  return err;
373
}
374
375
void inspector_read_stop(InspectorSocket* inspector) {
376
  uv_read_stop(reinterpret_cast<uv_stream_t*>(&inspector->client));
377
  inspector->ws_state->alloc_cb = nullptr;
378
  inspector->ws_state->read_cb = nullptr;
379
}
380
381
static void generate_accept_string(const std::string& client_key,
382
                                   char (*buffer)[ACCEPT_KEY_LENGTH]) {
383
  // Magic string from websockets spec.
384
  static const char ws_magic[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
385
  std::string input(client_key + ws_magic);
386
  char hash[SHA_DIGEST_LENGTH];
387
  SHA1(reinterpret_cast<const unsigned char*>(&input[0]), input.size(),
388
       reinterpret_cast<unsigned char*>(hash));
389
  node::base64_encode(hash, sizeof(hash), *buffer, sizeof(*buffer));
390
}
391
392
static int header_value_cb(http_parser* parser, const char* at, size_t length) {
393
  static const char SEC_WEBSOCKET_KEY_HEADER[] = "Sec-WebSocket-Key";
394
  auto inspector = static_cast<InspectorSocket*>(parser->data);
395
  auto state = inspector->http_parsing_state;
396
  state->parsing_value = true;
397
  if (state->current_header.size() == sizeof(SEC_WEBSOCKET_KEY_HEADER) - 1 &&
398
      node::StringEqualNoCaseN(state->current_header.data(),
399
                               SEC_WEBSOCKET_KEY_HEADER,
400
                               sizeof(SEC_WEBSOCKET_KEY_HEADER) - 1)) {
401
    state->ws_key.append(at, length);
402
  }
403
  return 0;
404
}
405
406
static int header_field_cb(http_parser* parser, const char* at, size_t length) {
407
  auto inspector = static_cast<InspectorSocket*>(parser->data);
408
  auto state = inspector->http_parsing_state;
409
  if (state->parsing_value) {
410
    state->parsing_value = false;
411
    state->current_header.clear();
412
  }
413
  state->current_header.append(at, length);
414
  return 0;
415
}
416
417
static int path_cb(http_parser* parser, const char* at, size_t length) {
418
  auto inspector = static_cast<InspectorSocket*>(parser->data);
419
  auto state = inspector->http_parsing_state;
420
  state->path.append(at, length);
421
  return 0;
422
}
423
424
static void handshake_complete(InspectorSocket* inspector) {
425
  uv_read_stop(reinterpret_cast<uv_stream_t*>(&inspector->client));
426
  handshake_cb callback = inspector->http_parsing_state->callback;
427
  inspector->ws_state = new ws_state_s();
428
  inspector->ws_mode = true;
429
  callback(inspector, kInspectorHandshakeUpgraded,
430
           inspector->http_parsing_state->path);
431
}
432
433
static void cleanup_http_parsing_state(InspectorSocket* inspector) {
434
  delete inspector->http_parsing_state;
435
  inspector->http_parsing_state = nullptr;
436
}
437
438
static void report_handshake_failure_cb(uv_handle_t* handle) {
439
  dispose_inspector(handle);
440
  InspectorSocket* inspector = inspector_from_stream(handle);
441
  handshake_cb cb = inspector->http_parsing_state->callback;
442
  cleanup_http_parsing_state(inspector);
443
  cb(inspector, kInspectorHandshakeFailed, std::string());
444
}
445
446
static void close_and_report_handshake_failure(InspectorSocket* inspector) {
447
  uv_handle_t* socket = reinterpret_cast<uv_handle_t*>(&inspector->client);
448
  if (uv_is_closing(socket)) {
449
    report_handshake_failure_cb(socket);
450
  } else {
451
    uv_read_stop(reinterpret_cast<uv_stream_t*>(socket));
452
    uv_close(socket, report_handshake_failure_cb);
453
  }
454
}
455
456
static void then_close_and_report_failure(uv_write_t* req, int status) {
457
  InspectorSocket* inspector = WriteRequest::from_write_req(req)->inspector;
458
  write_request_cleanup(req, status);
459
  close_and_report_handshake_failure(inspector);
460
}
461
462
static void handshake_failed(InspectorSocket* inspector) {
463
  const char HANDSHAKE_FAILED_RESPONSE[] =
464
      "HTTP/1.0 400 Bad Request\r\n"
465
      "Content-Type: text/html; charset=UTF-8\r\n\r\n"
466
      "WebSockets request was expected\r\n";
467
  write_to_client(inspector, HANDSHAKE_FAILED_RESPONSE,
468
                  sizeof(HANDSHAKE_FAILED_RESPONSE) - 1,
469
                  then_close_and_report_failure);
470
}
471
472
// init_handshake references message_complete_cb
473
static void init_handshake(InspectorSocket* inspector);
474
475
static int message_complete_cb(http_parser* parser) {
476
  InspectorSocket* inspector = static_cast<InspectorSocket*>(parser->data);
477
  struct http_parsing_state_s* state = inspector->http_parsing_state;
478
  if (parser->method != HTTP_GET) {
479
    handshake_failed(inspector);
480
  } else if (!parser->upgrade) {
481
    if (state->callback(inspector, kInspectorHandshakeHttpGet, state->path)) {
482
      init_handshake(inspector);
483
    } else {
484
      handshake_failed(inspector);
485
    }
486
  } else if (state->ws_key.empty()) {
487
    handshake_failed(inspector);
488
  } else if (state->callback(inspector, kInspectorHandshakeUpgrading,
489
                             state->path)) {
490
    char accept_string[ACCEPT_KEY_LENGTH];
491
    generate_accept_string(state->ws_key, &accept_string);
492
    const char accept_ws_prefix[] = "HTTP/1.1 101 Switching Protocols\r\n"
493
                                    "Upgrade: websocket\r\n"
494
                                    "Connection: Upgrade\r\n"
495
                                    "Sec-WebSocket-Accept: ";
496
    const char accept_ws_suffix[] = "\r\n\r\n";
497
    std::string reply(accept_ws_prefix, sizeof(accept_ws_prefix) - 1);
498
    reply.append(accept_string, sizeof(accept_string));
499
    reply.append(accept_ws_suffix, sizeof(accept_ws_suffix) - 1);
500
    if (write_to_client(inspector, &reply[0], reply.size()) >= 0) {
501
      handshake_complete(inspector);
502
      inspector->http_parsing_state->done = true;
503
    } else {
504
      close_and_report_handshake_failure(inspector);
505
    }
506
  } else {
507
    handshake_failed(inspector);
508
  }
509
  return 0;
510
}
511
512
static void data_received_cb(uv_stream_s* client, ssize_t nread,
513
                             const uv_buf_t* buf) {
514
#if DUMP_READS
515
  if (nread >= 0) {
516
    printf("%s (%ld bytes)\n", __FUNCTION__, nread);
517
    dump_hex(buf->base, nread);
518
  } else {
519
    printf("[%s:%d] %s\n", __FUNCTION__, __LINE__, uv_err_name(nread));
520
  }
521
#endif
522
  InspectorSocket* inspector = inspector_from_stream(client);
523
  reclaim_uv_buf(inspector, buf, nread);
524
  if (nread < 0 || nread == UV_EOF) {
525
    close_and_report_handshake_failure(inspector);
526
  } else {
527
    http_parsing_state_s* state = inspector->http_parsing_state;
528
    http_parser* parser = &state->parser;
529
    http_parser_execute(parser, &state->parser_settings,
530
                        inspector->buffer.data(), nread);
531
    remove_from_beginning(&inspector->buffer, nread);
532
    if (parser->http_errno != HPE_OK) {
533
      handshake_failed(inspector);
534
    }
535
    if (inspector->http_parsing_state->done) {
536
      cleanup_http_parsing_state(inspector);
537
    }
538
  }
539
}
540
541
static void init_handshake(InspectorSocket* inspector) {
542
  http_parsing_state_s* state = inspector->http_parsing_state;
543
  CHECK_NE(state, nullptr);
544
  state->current_header.clear();
545
  state->ws_key.clear();
546
  state->path.clear();
547
  state->done = false;
548
  http_parser_init(&state->parser, HTTP_REQUEST);
549
  state->parser.data = inspector;
550
  http_parser_settings* settings = &state->parser_settings;
551
  http_parser_settings_init(settings);
552
  settings->on_header_field = header_field_cb;
553
  settings->on_header_value = header_value_cb;
554
  settings->on_message_complete = message_complete_cb;
555
  settings->on_url = path_cb;
556
}
557
558
int inspector_accept(uv_stream_t* server, InspectorSocket* inspector,
559
                     handshake_cb callback) {
560
  ASSERT_NE(callback, nullptr);
561
  CHECK_EQ(inspector->http_parsing_state, nullptr);
562
563
  inspector->http_parsing_state = new http_parsing_state_s();
564
  uv_stream_t* client = reinterpret_cast<uv_stream_t*>(&inspector->client);
565
  int err = uv_tcp_init(server->loop, &inspector->client);
566
567
  if (err == 0) {
568
    err = uv_accept(server, client);
569
  }
570
  if (err == 0) {
571
    init_handshake(inspector);
572
    inspector->http_parsing_state->callback = callback;
573
    err = uv_read_start(client, prepare_buffer,
574
                        data_received_cb);
575
  }
576
  if (err != 0) {
577
    uv_close(reinterpret_cast<uv_handle_t*>(client), NULL);
578
  }
579
  return err;
580
}
581
582
void inspector_write(InspectorSocket* inspector, const char* data,
583
                     size_t len) {
584
  if (inspector->ws_mode) {
585
    std::vector<char> output = encode_frame_hybi17(data, len);
586
    write_to_client(inspector, &output[0], output.size());
587
  } else {
588
    write_to_client(inspector, data, len);
589
  }
590
}
591
592
void inspector_close(InspectorSocket* inspector,
593
    inspector_cb callback) {
594
  // libuv throws assertions when closing stream that's already closed - we
595
  // need to do the same.
596
  ASSERT(!uv_is_closing(reinterpret_cast<uv_handle_t*>(&inspector->client)));
597
  ASSERT(!inspector->shutting_down);
598
  inspector->shutting_down = true;
599
  inspector->ws_state->close_cb = callback;
600
  if (inspector->connection_eof) {
601
    close_connection(inspector);
602
  } else {
603
    inspector_read_stop(inspector);
604
    write_to_client(inspector, CLOSE_FRAME, sizeof(CLOSE_FRAME),
605
                    on_close_frame_written);
606
    inspector_read_start(inspector, nullptr, nullptr);
607
  }
608
}
609
610
bool inspector_is_active(const InspectorSocket* inspector) {
611
  const uv_handle_t* client =
612
      reinterpret_cast<const uv_handle_t*>(&inspector->client);
613
  return !inspector->shutting_down && !uv_is_closing(client);
614
}
615
616
void InspectorSocket::reinit() {
617
  http_parsing_state = nullptr;
618
  ws_state = nullptr;
619
  buffer.clear();
620
  ws_mode = false;
621
  shutting_down = false;
622
  connection_eof = false;
623
}
624
625
}  // namespace inspector
626
}  // namespace node