* The client sends a request with data. * * @private
(req, res)
| 111 | * @private |
| 112 | */ |
| 113 | onDataRequest(req, res) { |
| 114 | if (this.dataReq) { |
| 115 | // assert: this.dataRes, '.dataReq and .dataRes should be (un)set together' |
| 116 | this.onError("data request overlap from client"); |
| 117 | res.writeStatus("500 Internal Server Error"); |
| 118 | res.end(); |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | const expectedContentLength = Number(req.headers["content-length"]); |
| 123 | |
| 124 | if (!expectedContentLength) { |
| 125 | this.onError("content-length header required"); |
| 126 | res.writeStatus("411 Length Required").end(); |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | if (expectedContentLength > this.maxHttpBufferSize) { |
| 131 | this.onError("payload too large"); |
| 132 | res.writeStatus("413 Payload Too Large").end(); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | const isBinary = "application/octet-stream" === req.headers["content-type"]; |
| 137 | |
| 138 | if (isBinary && this.protocol === 4) { |
| 139 | this.onError("invalid content"); |
| 140 | return res.writeStatus("400 Bad Request").end(); |
| 141 | } |
| 142 | |
| 143 | this.dataReq = req; |
| 144 | this.dataRes = res; |
| 145 | |
| 146 | let buffer; |
| 147 | let offset = 0; |
| 148 | |
| 149 | const headers = { |
| 150 | // text/html is required instead of text/plain to avoid an |
| 151 | // unwanted download dialog on certain user-agents (GH-43) |
| 152 | "Content-Type": "text/html", |
| 153 | }; |
| 154 | |
| 155 | this.headers(req, headers); |
| 156 | for (let key in headers) { |
| 157 | res.writeHeader(key, String(headers[key])); |
| 158 | } |
| 159 | |
| 160 | const onEnd = (buffer) => { |
| 161 | this.onData(buffer.toString()); |
| 162 | this.onDataRequestCleanup(); |
| 163 | res.cork(() => { |
| 164 | res.end("ok"); |
| 165 | }); |
| 166 | }; |
| 167 | |
| 168 | res.onAborted(() => { |
| 169 | this.onDataRequestCleanup(); |
| 170 | this.onError("data request connection closed prematurely"); |
no test coverage detected