<commit_before>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class _HttpRequestResponseBase {
  _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection)
      : _contentLength = -1,
        _keepAlive = false,
        _headers = new Map();

  int get contentLength() => _contentLength;
  bool get keepAlive() => _keepAlive;
  Map get headers() => _headers;

  void _setHeader(String name, String value) {
    _headers[name.toLowerCase()] = value;
  }

  bool _write(List<int> data, bool copyBuffer) {
    bool allWritten = true;
    if (data.length > 0) {
      if (_contentLength < 0) {
        // Write chunk size if transfer encoding is chunked.
        _writeHexString(data.length);
        _writeCRLF();
        _httpConnection.outputStream.write(data, copyBuffer);
        allWritten = _writeCRLF();
      } else {
        allWritten = _httpConnection.outputStream.write(data, copyBuffer);
      }
    }
    return allWritten;
  }

  bool _writeList(List<int> data, int offset, int count) {
    bool allWritten = true;
    if (count > 0) {
      if (_contentLength < 0) {
        // Write chunk size if transfer encoding is chunked.
        _writeHexString(count);
        _writeCRLF();
        _httpConnection.outputStream.writeFrom(data, offset, count);
        allWritten = _writeCRLF();
      } else {
        allWritten = _httpConnection.outputStream.writeFrom(data, offset, count);
      }
    }
    return allWritten;
  }

  bool _writeDone() {
    bool allWritten = true;
    if (_contentLength < 0) {
      // Terminate the content if transfer encoding is chunked.
      allWritten = _httpConnection.outputStream.write(_Const.END_CHUNKED);
    }
    return allWritten;
  }

  bool _writeHeaders() {
    List<int> data;

    // Format headers.
    _headers.forEach((String name, String value) {
      data = name.charCodes();
      _httpConnection.outputStream.write(data);
      data = ": ".charCodes();
      _httpConnection.outputStream.write(data);
      data = value.charCodes();
      _httpConnection.outputStream.write(data);
      _writeCRLF();
    });
    // Terminate header.
    return _writeCRLF();
  }

  bool _writeHexString(int x) {
    final List<int> hexDigits = [0x30, 0x31, 0x32, 0x33, 0x34,
                                 0x35, 0x36, 0x37, 0x38, 0x39,
                                 0x41, 0x42, 0x43, 0x44, 0x45, 0x46];
    ByteArray hex = new ByteArray(10);
    int index = hex.length;
    while (x > 0) {
      index--;
      hex[index] = hexDigits[x % 16];
      x = x >> 4;
    }
    return _httpConnection.outputStream.writeFrom(hex, index, hex.length - index);
  }

  bool _writeCRLF() {
    final CRLF = const [_CharCode.CR, _CharCode.LF];
    return _httpConnection.outputStream.write(CRLF);
  }

  bool _writeSP() {
    final SP = const [_CharCode.SP];
    return _httpConnection.outputStream.write(SP);
  }

  _HttpConnectionBase _httpConnection;
  Map<String, String> _headers;

  // Length of the content body. If this is set to -1 (default value)
  // when starting to send data chunked transfer encoding will be
  // used.
  int _contentLength;
  bool _keepAlive;
}


// Parsed HTTP request providing information on the HTTP headers.
class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest {
  _HttpRequest(_HttpConnection connection) : super(connection);

  String get method() => _method;
  String get uri() => _uri;
  String get path() => _path;
  String get queryString() => _queryString;
  Map get queryParameters() => _queryParameters;

  InputStream get inputStream() {
    if (_inputStream == null) {
      _inputStream = new _HttpInputStream(this);
    }
    return _inputStream;
  }

  void _onRequestStart(String method, String uri) {
    _method = method;
    _uri = uri;
    _parseRequestUri(uri);
  }

  void _onHeaderReceived(String name, String value) {
    _setHeader(name, value);
  }

  void _onHeadersComplete() {
    // Prepare for receiving data.
    _buffer = new _BufferList();
  }

  void _onDataReceived(List<int> data) {
    _buffer.add(data);
    if (_inputStream != null) _inputStream._dataReceived();
  }

  void _onDataEnd() {
    if (_inputStream != null) _inputStream._closeReceived();
  }

  // Escaped characters in uri are expected to have been parsed.
  void _parseRequestUri(String uri) {
    int position;
    position = uri.indexOf("?", 0);
    if (position == -1) {
      _path = _HttpUtils.decodeUrlEncodedString(_uri);
      _queryString = null;
      _queryParameters = new Map();
    } else {
      _path = _HttpUtils.decodeUrlEncodedString(_uri.substring(0, position));
      _queryString = _uri.substring(position + 1);
      _queryParameters = _HttpUtils.splitQueryString(_queryString);
    }
  }

  // Delegate functions for the HttpInputStream implementation.
  int _streamAvailable() {
    return _buffer.length;
  }

  List<int> _streamRead(int bytesToRead) {
    return _buffer.readBytes(bytesToRead);
  }

  int _streamReadInto(List<int> buffer, int offset, int len) {
    List<int> data = _buffer.readBytes(len);
    buffer.setRange(offset, data.length, data);
  }

  void _streamSetErrorHandler(callback(Exception e)) {
    _streamErrorHandler = callback
  }

  String _method;
  String _uri;
  String _path;
  String _queryString;
  Map<String, String> _queryParameters;<commit_msg>Fix another syntax error in the HTTP library discovered by dart2js<commit_after>// Copyright (c) 2012, the Dart project authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

class _HttpRequestResponseBase {
  _HttpRequestResponseBase(_HttpConnectionBase this._httpConnection)
      : _contentLength = -1,
        _keepAlive = false,
        _headers = new Map();

  int get contentLength() => _contentLength;
  bool get keepAlive() => _keepAlive;
  Map get headers() => _headers;

  void _setHeader(String name, String value) {
    _headers[name.toLowerCase()] = value;
  }

  bool _write(List<int> data, bool copyBuffer) {
    bool allWritten = true;
    if (data.length > 0) {
      if (_contentLength < 0) {
        // Write chunk size if transfer encoding is chunked.
        _writeHexString(data.length);
        _writeCRLF();
        _httpConnection.outputStream.write(data, copyBuffer);
        allWritten = _writeCRLF();
      } else {
        allWritten = _httpConnection.outputStream.write(data, copyBuffer);
      }
    }
    return allWritten;
  }

  bool _writeList(List<int> data, int offset, int count) {
    bool allWritten = true;
    if (count > 0) {
      if (_contentLength < 0) {
        // Write chunk size if transfer encoding is chunked.
        _writeHexString(count);
        _writeCRLF();
        _httpConnection.outputStream.writeFrom(data, offset, count);
        allWritten = _writeCRLF();
      } else {
        allWritten = _httpConnection.outputStream.writeFrom(data, offset, count);
      }
    }
    return allWritten;
  }

  bool _writeDone() {
    bool allWritten = true;
    if (_contentLength < 0) {
      // Terminate the content if transfer encoding is chunked.
      allWritten = _httpConnection.outputStream.write(_Const.END_CHUNKED);
    }
    return allWritten;
  }

  bool _writeHeaders() {
    List<int> data;

    // Format headers.
    _headers.forEach((String name, String value) {
      data = name.charCodes();
      _httpConnection.outputStream.write(data);
      data = ": ".charCodes();
      _httpConnection.outputStream.write(data);
      data = value.charCodes();
      _httpConnection.outputStream.write(data);
      _writeCRLF();
    });
    // Terminate header.
    return _writeCRLF();
  }

  bool _writeHexString(int x) {
    final List<int> hexDigits = [0x30, 0x31, 0x32, 0x33, 0x34,
                                 0x35, 0x36, 0x37, 0x38, 0x39,
                                 0x41, 0x42, 0x43, 0x44, 0x45, 0x46];
    ByteArray hex = new ByteArray(10);
    int index = hex.length;
    while (x > 0) {
      index--;
      hex[index] = hexDigits[x % 16];
      x = x >> 4;
    }
    return _httpConnection.outputStream.writeFrom(hex, index, hex.length - index);
  }

  bool _writeCRLF() {
    final CRLF = const [_CharCode.CR, _CharCode.LF];
    return _httpConnection.outputStream.write(CRLF);
  }

  bool _writeSP() {
    final SP = const [_CharCode.SP];
    return _httpConnection.outputStream.write(SP);
  }

  _HttpConnectionBase _httpConnection;
  Map<String, String> _headers;

  // Length of the content body. If this is set to -1 (default value)
  // when starting to send data chunked transfer encoding will be
  // used.
  int _contentLength;
  bool _keepAlive;
}


// Parsed HTTP request providing information on the HTTP headers.
class _HttpRequest extends _HttpRequestResponseBase implements HttpRequest {
  _HttpRequest(_HttpConnection connection) : super(connection);

  String get method() => _method;
  String get uri() => _uri;
  String get path() => _path;
  String get queryString() => _queryString;
  Map get queryParameters() => _queryParameters;

  InputStream get inputStream() {
    if (_inputStream == null) {
      _inputStream = new _HttpInputStream(this);
    }
    return _inputStream;
  }

  void _onRequestStart(String method, String uri) {
    _method = method;
    _uri = uri;
    _parseRequestUri(uri);
  }

  void _onHeaderReceived(String name, String value) {
    _setHeader(name, value);
  }

  void _onHeadersComplete() {
    // Prepare for receiving data.
    _buffer = new _BufferList();
  }

  void _onDataReceived(List<int> data) {
    _buffer.add(data);
    if (_inputStream != null) _inputStream._dataReceived();
  }

  void _onDataEnd() {
    if (_inputStream != null) _inputStream._closeReceived();
  }

  // Escaped characters in uri are expected to have been parsed.
  void _parseRequestUri(String uri) {
    int position;
    position = uri.indexOf("?", 0);
    if (position == -1) {
      _path = _HttpUtils.decodeUrlEncodedString(_uri);
      _queryString = null;
      _queryParameters = new Map();
    } else {
      _path = _HttpUtils.decodeUrlEncodedString(_uri.substring(0, position));
      _queryString = _uri.substring(position + 1);
      _queryParameters = _HttpUtils.splitQueryString(_queryString);
    }
  }

  // Delegate functions for the HttpInputStream implementation.
  int _streamAvailable() {
    return _buffer.length;
  }

  List<int> _streamRead(int bytesToRead) {
    return _buffer.readBytes(bytesToRead);
  }

  int _streamReadInto(List<int> buffer, int offset, int len) {
    List<int> data = _buffer.readBytes(len);
    buffer.setRange(offset, data.length, data);
  }

  void _streamSetErrorHandler(callback(Exception e)) {
    _streamErrorHandler = callback;
  }

  String _method;
  String _uri;
  String _path;
  String _queryString;
  Map<String, String> _queryParameters;
{'fraction_non_alphanumeric': 0.10447625951431679, 'fraction_numerical': 0.012595143167814425, 'mean_word_length': 2.9887965305384894, 'pattern_counts': {'":': 2, '<': 29, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2103123', 'n_tokens_mistral': 3525, 'n_tokens_neox': 3336, 'n_words': 903}