<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.facebook.thrift.transport;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.*;

import com.facebook.thrift.protocol.TBinaryProtocol;
import org.iq80.snappy.Snappy;
import org.iq80.snappy.CorruptionException;

public class THeaderTransport extends TFramedTransport {
  public static final int HEADER_MAGIC_MASK = 0xFFFF0000;
  public static final int HEADER_FLAGS_MASK = 0x0000FFFF;

  // 16th and 32nd bits must be 0 to differentiate framed vs unframed.
  public static final int HEADER_MAGIC = 0x0FFF0000;

  // HTTP has different magic
  public static final int HTTP_SERVER_MAGIC = 0x504F5354; // 'POST'

  // Note max frame size is slightly less than HTTP_SERVER_MAGIC
  public static final int MAX_FRAME_SIZE = 0x3FFFFFFF;

  private int zlibBufferSize = 512;

  // Transforms
  public enum Transforms {
    ZLIB_TRANSFORM(0x01),
    HMAC_TRANSFORM(0x02),
    SNAPPY_TRANSFORM(0x03);

    private int value;

    private Transforms(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Infos
  public enum Infos {
    INFO_KEYVALUE(0x01);

    private int value;

    private Infos(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Client types
  public enum ClientTypes {
    HEADERS(0),
    FRAMED_DEPRECATED(1),
    UNFRAMED_DEPRECATED(2),
    HTTP(3),
    UNKNOWN(4);

    private int value;

    private ClientTypes(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  public static final int T_BINARY_PROTOCOL = 0;
  public static final int T_JSON_PROTOCOL = 1;
  public static final int T_COMPACT_PROTOCOL = 2;

  private static final int numClientTypes = 4;

  private int protoId = T_COMPACT_PROTOCOL; // default
  private ClientTypes clientType = ClientTypes.HEADERS;
  private int seqId = 0;
  private int flags = 0;

  private final Boolean[] supportedClients;

  private final List<Transforms> writeTransforms;
  private List<Integer> readTransforms;

  private final HashMap<String, String> readHeaders;
  private final HashMap<String, String> writeHeaders;


  private static final String IDENTITY_HEADER = "identity";
  private static final String ID_VERSION_HEADER = "id_version";
  private static final String ID_VERSION = "1";

  private String identity;

  public interface CryptoCallback {
    public byte[] mac(byte[] data) throws Exception;
    public boolean isValidMac(byte[] data, byte[] mac) throws Exception;
  }

  private CryptoCallback crypto;


  public THeaderTransport(TTransport transport) {
    super(transport);
    writeTransforms = new ArrayList<Transforms>();

    // Always supported headers
    supportedClients = new Boolean[numClientTypes];
    supportedClients[ClientTypes.HEADERS.getValue()] = true;
    writeHeaders = new HashMap<String, String>();
    readHeaders = new HashMap<String, String>();
  }

  public THeaderTransport(TTransport transport, List<ClientTypes> clientTypes) {
    this(transport);

    if (clientTypes != null) {
      for (ClientTypes t : clientTypes) {
        supportedClients[t.getValue()] = true;
      }
    }
  }

  /**
   * Returns the protocol Id we are reading/writing
   *
   * @return protocol id
   */
  public int getProtocolId() {
    if (clientType == ClientTypes.HEADERS) {
      return protoId;
    } else {
      return 0; // Default to binary for all others.
    }
  }

  /**<commit_msg>Fix getLastUsedHost in Java interface when using header transport<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.facebook.thrift.transport;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.*;

import com.facebook.thrift.protocol.TBinaryProtocol;
import org.iq80.snappy.Snappy;
import org.iq80.snappy.CorruptionException;

public class THeaderTransport extends TFramedTransport {
  public static final int HEADER_MAGIC_MASK = 0xFFFF0000;
  public static final int HEADER_FLAGS_MASK = 0x0000FFFF;

  // 16th and 32nd bits must be 0 to differentiate framed vs unframed.
  public static final int HEADER_MAGIC = 0x0FFF0000;

  // HTTP has different magic
  public static final int HTTP_SERVER_MAGIC = 0x504F5354; // 'POST'

  // Note max frame size is slightly less than HTTP_SERVER_MAGIC
  public static final int MAX_FRAME_SIZE = 0x3FFFFFFF;

  private int zlibBufferSize = 512;

  // Transforms
  public enum Transforms {
    ZLIB_TRANSFORM(0x01),
    HMAC_TRANSFORM(0x02),
    SNAPPY_TRANSFORM(0x03);

    private int value;

    private Transforms(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Infos
  public enum Infos {
    INFO_KEYVALUE(0x01);

    private int value;

    private Infos(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  // Client types
  public enum ClientTypes {
    HEADERS(0),
    FRAMED_DEPRECATED(1),
    UNFRAMED_DEPRECATED(2),
    HTTP(3),
    UNKNOWN(4);

    private int value;

    private ClientTypes(int value) {
      this.value = value;
    }

    public int getValue() {
      return value;
    }
  }

  public static final int T_BINARY_PROTOCOL = 0;
  public static final int T_JSON_PROTOCOL = 1;
  public static final int T_COMPACT_PROTOCOL = 2;

  private static final int numClientTypes = 4;

  private int protoId = T_COMPACT_PROTOCOL; // default
  private ClientTypes clientType = ClientTypes.HEADERS;
  private int seqId = 0;
  private int flags = 0;

  private final Boolean[] supportedClients;

  private final List<Transforms> writeTransforms;
  private List<Integer> readTransforms;

  private final HashMap<String, String> readHeaders;
  private final HashMap<String, String> writeHeaders;


  private static final String IDENTITY_HEADER = "identity";
  private static final String ID_VERSION_HEADER = "id_version";
  private static final String ID_VERSION = "1";

  private String identity;

  public interface CryptoCallback {
    public byte[] mac(byte[] data) throws Exception;
    public boolean isValidMac(byte[] data, byte[] mac) throws Exception;
  }

  private CryptoCallback crypto;


  public THeaderTransport(TTransport transport) {
    super(transport);
    writeTransforms = new ArrayList<Transforms>();

    // Always supported headers
    supportedClients = new Boolean[numClientTypes];
    supportedClients[ClientTypes.HEADERS.getValue()] = true;
    writeHeaders = new HashMap<String, String>();
    readHeaders = new HashMap<String, String>();
  }

  public THeaderTransport(TTransport transport, List<ClientTypes> clientTypes) {
    this(transport);

    if (clientTypes != null) {
      for (ClientTypes t : clientTypes) {
        supportedClients[t.getValue()] = true;
      }
    }
  }

  public TTransport getUnderlyingTransport() {
    return this.transport_;
  }

  /**
   * Returns the protocol Id we are reading/writing
   *
   * @return protocol id
   */
  public int getProtocolId() {
    if (clientType == ClientTypes.HEADERS) {
      return protoId;
    } else {
      return 0; // Default to binary for all others.
    }
  }

  /**
{'fraction_non_alphanumeric': 0.08106914071128783, 'fraction_numerical': 0.01480008835873647, 'mean_word_length': 3.5049751243781095, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7393324', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2594, 'n_words': 875}