OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 class _HttpHeaders implements HttpHeaders { | 7 class _HttpHeaders implements HttpHeaders { |
8 _HttpHeaders(String this.protocolVersion) | 8 _HttpHeaders(String this.protocolVersion) |
9 : _headers = new Map<String, List<String>>(); | 9 : _headers = new Map<String, List<String>>(); |
10 | 10 |
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 protocolVersion == "1.1") { | 337 protocolVersion == "1.1") { |
338 contentLength = -1; | 338 contentLength = -1; |
339 } | 339 } |
340 } | 340 } |
341 | 341 |
342 void _finalize() { | 342 void _finalize() { |
343 _synchronize(); | 343 _synchronize(); |
344 _mutable = false; | 344 _mutable = false; |
345 } | 345 } |
346 | 346 |
347 _write(IOSink sink) { | 347 _write(_BufferList buffer) { |
348 final COLONSP = const [_CharCode.COLON, _CharCode.SP]; | 348 final COLONSP = const [_CharCode.COLON, _CharCode.SP]; |
349 final COMMASP = const [_CharCode.COMMA, _CharCode.SP]; | 349 final COMMASP = const [_CharCode.COMMA, _CharCode.SP]; |
350 final CRLF = const [_CharCode.CR, _CharCode.LF]; | 350 final CRLF = const [_CharCode.CR, _CharCode.LF]; |
351 | 351 |
352 var bufferSize = 16 * 1024; | |
353 var buffer = new Uint8List(bufferSize); | |
354 var bufferPos = 0; | |
355 | |
356 void writeBuffer() { | |
357 sink.writeBytes(buffer.getRange(0, bufferPos)); | |
358 bufferPos = 0; | |
359 } | |
360 | |
361 // Format headers. | 352 // Format headers. |
362 _headers.forEach((String name, List<String> values) { | 353 _headers.forEach((String name, List<String> values) { |
363 bool fold = _foldHeader(name); | 354 bool fold = _foldHeader(name); |
364 List<int> nameData; | 355 var nameData = name.codeUnits; |
365 nameData = name.codeUnits; | 356 buffer.add(nameData); |
366 int nameDataLen = nameData.length; | 357 buffer.add(const [_CharCode.COLON, _CharCode.SP]); |
367 if (nameDataLen + 2 > bufferSize - bufferPos) writeBuffer(); | |
368 buffer.setRange(bufferPos, nameDataLen, nameData); | |
369 bufferPos += nameDataLen; | |
370 buffer[bufferPos++] = _CharCode.COLON; | |
371 buffer[bufferPos++] = _CharCode.SP; | |
372 for (int i = 0; i < values.length; i++) { | 358 for (int i = 0; i < values.length; i++) { |
373 List<int> data = values[i].codeUnits; | |
374 int dataLen = data.length; | |
375 // Worst case here is writing the name, value and 6 additional bytes. | |
376 if (nameDataLen + dataLen + 6 > bufferSize - bufferPos) writeBuffer(); | |
377 if (i > 0) { | 359 if (i > 0) { |
378 if (fold) { | 360 if (fold) { |
379 buffer[bufferPos++] = _CharCode.COMMA; | 361 buffer.add(const [_CharCode.COMMA, _CharCode.SP]); |
380 buffer[bufferPos++] = _CharCode.SP; | |
381 } else { | 362 } else { |
382 buffer[bufferPos++] = _CharCode.CR; | 363 buffer.add(const [_CharCode.CR, _CharCode.LF]); |
383 buffer[bufferPos++] = _CharCode.LF; | 364 buffer.add(nameData); |
384 buffer.setRange(bufferPos, nameDataLen, nameData); | 365 buffer.add(const [_CharCode.COLON, _CharCode.SP]); |
385 bufferPos += nameDataLen; | |
386 buffer[bufferPos++] = _CharCode.COLON; | |
387 buffer[bufferPos++] = _CharCode.SP; | |
388 } | 366 } |
389 } | 367 } |
390 buffer.setRange(bufferPos, dataLen, data); | 368 buffer.add(values[i].codeUnits); |
391 bufferPos += dataLen; | |
392 } | 369 } |
393 buffer[bufferPos++] = _CharCode.CR; | 370 buffer.add(const [_CharCode.CR, _CharCode.LF]); |
394 buffer[bufferPos++] = _CharCode.LF; | |
395 }); | 371 }); |
396 writeBuffer(); | |
397 } | 372 } |
398 | 373 |
399 String toString() { | 374 String toString() { |
400 StringBuffer sb = new StringBuffer(); | 375 StringBuffer sb = new StringBuffer(); |
401 _headers.forEach((String name, List<String> values) { | 376 _headers.forEach((String name, List<String> values) { |
402 sb.write(name); | 377 sb.write(name); |
403 sb.write(": "); | 378 sb.write(": "); |
404 bool fold = _foldHeader(name); | 379 bool fold = _foldHeader(name); |
405 for (int i = 0; i < values.length; i++) { | 380 for (int i = 0; i < values.length; i++) { |
406 if (i > 0) { | 381 if (i > 0) { |
(...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
787 | 762 |
788 String name; | 763 String name; |
789 String value; | 764 String value; |
790 DateTime expires; | 765 DateTime expires; |
791 int maxAge; | 766 int maxAge; |
792 String domain; | 767 String domain; |
793 String path; | 768 String path; |
794 bool httpOnly = false; | 769 bool httpOnly = false; |
795 bool secure = false; | 770 bool secure = false; |
796 } | 771 } |
OLD | NEW |