All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arangodb.shaded.vertx.core.http.impl.VertxHttp2ClientUpgradeCodec Maven / Gradle / Ivy

There is a newer version: 7.8.0
Show newest version
/*
 * Copyright (c) 2011-2019 Contributors to the Eclipse Foundation
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
 * which is available at https://www.apache.org/licenses/LICENSE-2.0.
 *
 * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
 */

package com.arangodb.shaded.vertx.core.http.impl;

import com.arangodb.shaded.netty.buffer.ByteBuf;
import com.arangodb.shaded.netty.channel.ChannelHandlerContext;
import com.arangodb.shaded.netty.handler.codec.http.FullHttpResponse;
import com.arangodb.shaded.netty.handler.codec.http.HttpClientUpgradeHandler;
import com.arangodb.shaded.netty.handler.codec.http.HttpRequest;
import com.arangodb.shaded.netty.handler.codec.http2.Http2Settings;
import com.arangodb.shaded.netty.util.collection.CharObjectMap;
import com.arangodb.shaded.vertx.core.buffer.Buffer;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static com.arangodb.shaded.netty.handler.codec.http2.Http2CodecUtil.HTTP_UPGRADE_SETTINGS_HEADER;
import static com.arangodb.shaded.netty.util.CharsetUtil.UTF_8;

/**
 * @author Julien Viet
 */
public class VertxHttp2ClientUpgradeCodec implements HttpClientUpgradeHandler.UpgradeCodec {

  private static final List UPGRADE_HEADERS = Collections.singletonList(HTTP_UPGRADE_SETTINGS_HEADER);

  private final com.arangodb.shaded.vertx.core.http.Http2Settings settings;

  public VertxHttp2ClientUpgradeCodec(com.arangodb.shaded.vertx.core.http.Http2Settings settings) {
    this.settings = settings;
  }

  @Override
  public CharSequence protocol() {
    return "h2c";
  }

  @Override
  public Collection setUpgradeHeaders(ChannelHandlerContext ctx, HttpRequest upgradeRequest) {
    Http2Settings nettySettings = new Http2Settings();
    HttpUtils.fromVertxInitialSettings(false, settings, nettySettings);
    Buffer buf = Buffer.buffer();
    for (CharObjectMap.PrimitiveEntry entry : nettySettings.entries()) {
      buf.appendUnsignedShort(entry.key());
      buf.appendUnsignedInt(entry.value());
    }
    String encodedSettings = new String(java.util.Base64.getUrlEncoder().encode(buf.getBytes()), UTF_8);
    upgradeRequest.headers().set(HTTP_UPGRADE_SETTINGS_HEADER, encodedSettings);
    return UPGRADE_HEADERS;
  }

  @Override
  public void upgradeTo(ChannelHandlerContext ctx, FullHttpResponse upgradeResponse) throws Exception {
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy