| 523 | Server.error = -1; |
| 524 | |
| 525 | function server(message, value, etc) { |
| 526 | let socket = this.socket; |
| 527 | if (!socket) return; |
| 528 | |
| 529 | // trace(`HTTP SERVER SOCKET MSG ${message} in state ${this.state}\n`); |
| 530 | |
| 531 | try { |
| 532 | if (2 === message) { |
| 533 | if ((1 == this.state) || (2 == this.state)) { |
| 534 | while (true) { |
| 535 | let line = socket.read(String, "\n"); |
| 536 | if (!line) |
| 537 | return; // out of data. wait for more. |
| 538 | |
| 539 | if (this.line) { |
| 540 | line = this.line + line; |
| 541 | delete this.line; |
| 542 | } |
| 543 | |
| 544 | if (10 != line.charCodeAt(line.length - 1)) { // partial header line, accumulate and wait for more |
| 545 | this.line = line; |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | if ("\r\n" == line) { // empty line is end of headers |
| 550 | delete this.line; |
| 551 | |
| 552 | let request = this.callback(Server.headersComplete); // headers complete... let's see what to do with the request body |
| 553 | if (false === request) |
| 554 | socket.ignore = true; // ignore request body before sending response |
| 555 | |
| 556 | if (undefined !== this.total) { |
| 557 | // start to receive request body |
| 558 | this.state = 5; |
| 559 | |
| 560 | if (undefined !== request) |
| 561 | this.request = request; |
| 562 | |
| 563 | if (true !== this.request) |
| 564 | this.buffers = []; |
| 565 | |
| 566 | value = socket.read(); |
| 567 | } |
| 568 | else { |
| 569 | this.state = 7; // send response headers |
| 570 | message = 3; |
| 571 | value = socket.write(); |
| 572 | } |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | if (1 === this.state) { |
| 577 | // parse status line: GET / HTTP/1.1 |
| 578 | line = line.split(" "); |
| 579 | if (line.length < 3) |
| 580 | throw new Error("unexpected status format"); |
| 581 | const protocol = line[line.length - 1].trim(); |
| 582 | if (("HTTP/1.1" !== protocol) && ("HTTP/1.0" !== protocol)) // http 1.0 for hotspot.html |