| 50 | */ |
| 51 | |
| 52 | export class Request { |
| 53 | constructor(dictionary) { |
| 54 | if (dictionary.socket) { |
| 55 | this.socket = dictionary.socket; // server re-using Request |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | dictionary = {port: 80, method: "GET", path: "/", Socket, ...dictionary}; |
| 60 | |
| 61 | this.method = dictionary.method; |
| 62 | this.path = dictionary.path; |
| 63 | this.host = dictionary.host ? dictionary.host : dictionary.address; |
| 64 | if (dictionary.headers) |
| 65 | this.headers = dictionary.headers; |
| 66 | if (dictionary.body) |
| 67 | this.body = dictionary.body; |
| 68 | if (dictionary.response) |
| 69 | this.response = dictionary.response; |
| 70 | this.state = 0; |
| 71 | |
| 72 | this.socket = new (dictionary.Socket)(dictionary); |
| 73 | this.socket.callback = callback.bind(this); |
| 74 | } |
| 75 | |
| 76 | read(type, limit) { |
| 77 | let available = this.socket.read(); |
| 78 | |
| 79 | if (undefined !== this.chunk) { |
| 80 | if (undefined === limit) |
| 81 | limit = this.chunk; |
| 82 | else if (typeof limit !== "number") |
| 83 | throw new Error("http limit only supports number"); |
| 84 | |
| 85 | if (undefined === type) |
| 86 | return limit; |
| 87 | |
| 88 | if (type === Number) |
| 89 | limit = 1; |
| 90 | else if (limit > this.chunk) |
| 91 | limit = this.chunk; |
| 92 | |
| 93 | if (available < limit) |
| 94 | limit = available; |
| 95 | |
| 96 | if (!limit) |
| 97 | return; |
| 98 | |
| 99 | this.chunk -= limit; |
| 100 | } |
| 101 | else if (undefined === type) |
| 102 | return available; |
| 103 | |
| 104 | const result = this.socket.read(type, limit); |
| 105 | if (this.total) { |
| 106 | this.total -= (available - this.socket.read()); |
| 107 | if (!this.total) { |
| 108 | this.done = Timer.set(() => { |
| 109 | if (this.done) |
nothing calls this directly
no outgoing calls
no test coverage detected