| 261 | } |
| 262 | |
| 263 | function parseRawResponse(rawResponse) { |
| 264 | const newline = '\n'; |
| 265 | const lines = rawResponse.split(newline); |
| 266 | |
| 267 | let isHeader = true; |
| 268 | let line = lines.shift(); |
| 269 | const statusCode = parseInt(line, 10); |
| 270 | |
| 271 | const headers = new Map(); |
| 272 | let data = ''; |
| 273 | |
| 274 | while ((line = lines.shift()) !== undefined) { |
| 275 | if (line.trim() === '') { |
| 276 | if (!isHeader) { |
| 277 | // The first empty line should be omitted as it indicates the transition from headers to body. |
| 278 | // All those that follow should be included in the response body. |
| 279 | data += line + newline; |
| 280 | } |
| 281 | isHeader = false; |
| 282 | if (request.headers['if-none-match'] && response.getHeader('ETag') === request.headers['if-none-match']) { |
| 283 | return {statusCode: 304}; |
| 284 | } |
| 285 | continue; |
| 286 | } |
| 287 | |
| 288 | if (isHeader) { |
| 289 | const firstColon = line.indexOf(':'); |
| 290 | let headerValue = line.substring(firstColon + 1).trim(); |
| 291 | headerValue = headerValue.replace('$host_port', `${server.address().port}`); |
| 292 | headers.set(line.substring(0, firstColon), headerValue); |
| 293 | } else { |
| 294 | data += line + newline; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return {statusCode, data, headers}; |
| 299 | } |
| 300 | } |