@Override
  public Result exchangeTerminating(HttpExchange exchange, Result result) {
    if (result.isFailed()) return result;

    HttpResponse response = exchange.getResponse();

    if ((response.getVersion() == HttpVersion.HTTP_1_1)
        && (response.getStatus() == HttpStatus.SWITCHING_PROTOCOLS_101)) {
      String connection = response.getHeaders().get(HttpHeader.CONNECTION);
      if ((connection == null) || !connection.toLowerCase(Locale.US).contains("upgrade")) {
        return new Result(
            result,
            new HttpResponseException(
                "101 Switching Protocols without Connection: Upgrade not supported", response));
      }

      // Upgrade Response
      HttpRequest request = exchange.getRequest();
      if (request instanceof HttpConnectionUpgrader) {
        HttpConnectionUpgrader listener = (HttpConnectionUpgrader) request;
        try {
          listener.upgrade(response, getHttpConnection());
        } catch (Throwable x) {
          return new Result(result, x);
        }
      }
    }

    return result;
  }
Ejemplo n.º 2
0
  @Override
  public void onReply(Stream stream, ReplyInfo replyInfo) {
    HttpExchange exchange = getHttpExchange();
    if (exchange == null) return;

    try {
      HttpResponse response = exchange.getResponse();

      Fields fields = replyInfo.getHeaders();
      short spdy = stream.getSession().getVersion();
      HttpVersion version =
          HttpVersion.fromString(fields.get(HTTPSPDYHeader.VERSION.name(spdy)).getValue());
      response.version(version);
      String[] status = fields.get(HTTPSPDYHeader.STATUS.name(spdy)).getValue().split(" ", 2);

      Integer code = Integer.parseInt(status[0]);
      response.status(code);
      String reason = status.length < 2 ? HttpStatus.getMessage(code) : status[1];
      response.reason(reason);

      if (responseBegin(exchange)) {
        for (Fields.Field field : fields) {
          String name = field.getName();
          if (HTTPSPDYHeader.from(spdy, name) != null) continue;
          // TODO: handle multiple values properly
          HttpField httpField = new HttpField(name, field.getValue());
          responseHeader(exchange, httpField);
        }

        if (responseHeaders(exchange)) {
          if (replyInfo.isClose()) {
            responseSuccess(exchange);
          }
        }
      }
    } catch (Exception x) {
      responseFailure(x);
    }
  }