private boolean handleResponseAndExit(
      final Channel channel,
      final NettyResponseFuture<?> future,
      AsyncHandler<?> handler,
      HttpRequest httpRequest,
      ProxyServer proxyServer,
      HttpResponse response)
      throws Exception {

    // store the original headers so we can re-send all them to
    // the handler in case of trailing headers
    future.setHttpHeaders(response.headers());

    future.setKeepAlive(
        !HttpHeaders.Values.CLOSE.equalsIgnoreCase(
            response.headers().get(HttpHeaders.Names.CONNECTION)));

    HttpResponseStatus status = new ResponseStatus(future.getURI(), response, config);
    int statusCode = response.getStatus().code();
    Request request = future.getRequest();
    Realm realm = request.getRealm() != null ? request.getRealm() : config.getRealm();
    HttpResponseHeaders responseHeaders = new ResponseHeaders(future.getURI(), response.headers());

    return handleResponseFiltersReplayRequestAndExit(channel, future, status, responseHeaders) //
        || handleUnauthorizedAndExit(
            statusCode, realm, request, response, future, proxyServer, channel) //
        || handleContinueAndExit(channel, future, statusCode) //
        || handleProxyAuthenticationRequiredAndExit(
            statusCode, realm, request, response, future, proxyServer)
        || handleConnectOKAndExit(
            statusCode, realm, request, httpRequest, response, future, proxyServer, channel) //
        || handleRedirectAndExit(request, future, response, channel) //
        || handleHanderAndExit(channel, future, handler, status, responseHeaders, response);
  }
Esempio n. 2
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    System.out.println("Channel Read");
    if (msg instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) msg;

      System.err.println("STATUS: " + response.getStatus());
      System.err.println("VERSION: " + response.getProtocolVersion());
      System.err.println();

      if (!response.headers().isEmpty()) {
        for (String name : response.headers().names()) {
          for (String value : response.headers().getAll(name)) {
            System.err.println("HEADER: " + name + " = " + value);
          }
        }
        System.err.println();
      }
    }
    if (msg instanceof HttpContent) {
      HttpContent content = (HttpContent) msg;

      System.err.print(content.content().toString(CharsetUtil.UTF_8));
      System.err.flush();

      if (content instanceof LastHttpContent) {
        System.err.println("} END OF CONTENT");
        ctx.close();
      }
    }
  }
  @Override
  public void sendError(int status, String message) throws IOException {
    if (committed) {
      throw new IllegalStateException();
    }

    final HttpResponseStatus responseStatus;
    if (message != null) {
      responseStatus = new HttpResponseStatus(status, message);
      setStatus(status);
    } else {
      responseStatus = HttpResponseStatus.valueOf(status);
      setStatus(status);
    }
    io.netty.handler.codec.http.HttpResponse response = null;
    if (message != null) {
      ByteBuf byteBuf = ctx.alloc().buffer();
      byteBuf.writeBytes(message.getBytes());

      response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus, byteBuf);
    } else {
      response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, responseStatus);
    }
    if (keepAlive) {
      // Add keep alive and content length if needed
      response.headers().add(Names.CONNECTION, Values.KEEP_ALIVE);
      if (message == null) response.headers().add(Names.CONTENT_LENGTH, 0);
      else response.headers().add(Names.CONTENT_LENGTH, message.getBytes().length);
    }
    ctx.writeAndFlush(response);
    committed = true;
  }
Esempio n. 4
0
 @Override
 public HttpResponse call() throws Exception {
   HttpResponse response = null;
   response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
   response.headers().add(RtspHeaderNames.SERVER, "RtspServer");
   response.headers().add(RtspHeaderNames.CSEQ, this.request.headers().get(RtspHeaderNames.CSEQ));
   response
       .headers()
       .add(RtspHeaderNames.PUBLIC, "SETUP,PLAY,PAUSE,TEARDOWN,GET_PARAMETER,OPTION");
   return response;
 }
 @Override
 public @Nullable FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
     throws Exception {
   QueryStringDecoder decoder = new QueryStringDecoder(request.uri());
   List<String> agentIds = decoder.parameters().get("agent-id");
   if (agentIds == null) {
     agentIds = ImmutableList.of("");
   }
   String agentId = agentIds.get(0);
   List<String> traceIds = decoder.parameters().get("trace-id");
   checkNotNull(traceIds, "Missing trace id in query string: %s", request.uri());
   String traceId = traceIds.get(0);
   // check-live-traces is an optimization so glowroot server only has to check with remote
   // agents when necessary
   List<String> checkLiveTracesParams = decoder.parameters().get("check-live-traces");
   boolean checkLiveTraces = false;
   if (checkLiveTracesParams != null && !checkLiveTracesParams.isEmpty()) {
     checkLiveTraces = Boolean.parseBoolean(checkLiveTracesParams.get(0));
   }
   logger.debug(
       "handleRequest(): agentId={}, traceId={}, checkLiveTraces={}",
       agentId,
       traceId,
       checkLiveTraces);
   TraceExport traceExport = traceCommonService.getExport(agentId, traceId, checkLiveTraces);
   if (traceExport == null) {
     logger.warn("no trace found for id: {}", traceId);
     return new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
   }
   ChunkedInput<HttpContent> in = getExportChunkedInput(traceExport);
   HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
   response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
   response.headers().set(HttpHeaderNames.CONTENT_TYPE, MediaType.ZIP.toString());
   response
       .headers()
       .set("Content-Disposition", "attachment; filename=" + traceExport.fileName() + ".zip");
   boolean keepAlive = HttpUtil.isKeepAlive(request);
   if (keepAlive && !request.protocolVersion().isKeepAliveDefault()) {
     response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
   }
   HttpServices.preventCaching(response);
   ctx.write(response);
   ChannelFuture future = ctx.write(in);
   HttpServices.addErrorListener(future);
   if (!keepAlive) {
     HttpServices.addCloseListener(future);
   }
   // return null to indicate streaming
   return null;
 }
Esempio n. 6
0
 /**
  * Sets the value of response headers after making sure that the response metadata is not already
  * sent.
  *
  * @param headerName The name of the header.
  * @param headerValue The intended value of the header.
  * @throws IllegalArgumentException if any of {@code headerName} or {@code headerValue} is null.
  * @throws IllegalStateException if response metadata has already been written to the channel.
  */
 private void setResponseHeader(String headerName, Object headerValue) {
   if (headerName != null && headerValue != null) {
     long startTime = System.currentTimeMillis();
     if (headerValue instanceof Date) {
       HttpHeaders.setDateHeader(responseMetadata, headerName, (Date) headerValue);
     } else {
       HttpHeaders.setHeader(responseMetadata, headerName, headerValue);
     }
     if (responseMetadataWritten.get()) {
       nettyMetrics.deadResponseAccessError.inc();
       throw new IllegalStateException(
           "Response metadata changed after it has already been written to the channel");
     } else {
       logger.trace(
           "Header {} set to {} for channel {}",
           headerName,
           responseMetadata.headers().get(headerName),
           ctx.channel());
       nettyMetrics.headerSetTimeInMs.update(System.currentTimeMillis() - startTime);
     }
   } else {
     throw new IllegalArgumentException(
         "Header name [" + headerName + "] or header value [" + headerValue + "] null");
   }
 }
 @Override
 public void addHeader(String name, String value) {
   if (setHeaderField(name, value)) {
     return;
   }
   response.headers().add(name, value);
 }
Esempio n. 8
0
 /**
  * Verifies User metadata headers from output, to that sent in during input
  *
  * @param expectedHeaders the expected headers in the response.
  * @param response the {@link HttpResponse} which contains the headers of the response.
  * @param usermetadata if non-null, this is expected to come as the body.
  * @param content the content accompanying the response.
  */
 private void verifyUserMetadata(
     HttpHeaders expectedHeaders,
     HttpResponse response,
     byte[] usermetadata,
     Queue<HttpObject> content) {
   if (usermetadata == null) {
     assertEquals("Content-Length is not 0", 0, HttpHeaders.getContentLength(response));
     for (Map.Entry<String, String> header : expectedHeaders) {
       String key = header.getKey();
       if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
         assertEquals(
             "Value for " + key + " does not match in user metadata",
             header.getValue(),
             HttpHeaders.getHeader(response, key));
       }
     }
     for (Map.Entry<String, String> header : response.headers()) {
       String key = header.getKey();
       if (key.startsWith(RestUtils.Headers.USER_META_DATA_HEADER_PREFIX)) {
         assertTrue(
             "Key " + key + " does not exist in expected headers", expectedHeaders.contains(key));
       }
     }
     discardContent(content, 1);
   } else {
     assertEquals(
         "Content-Length is not as expected",
         usermetadata.length,
         HttpHeaders.getContentLength(response));
     byte[] receivedMetadata = getContent(content, HttpHeaders.getContentLength(response)).array();
     assertArrayEquals("User metadata does not match original", usermetadata, receivedMetadata);
   }
 }
  @Override
  public void fatalError(String message, String stack) {
    if (log.isDebugEnabled()) {
      log.debug("Sending HTTP error due to script error {}", message);
    }

    StringBuilder msg = new StringBuilder(message);
    if (stack != null) {
      msg.append('\n');
      msg.append(stack);
    }
    ByteBuf data = Unpooled.copiedBuffer(msg, Charsets.UTF8);

    response.setStatus(HttpResponseStatus.INTERNAL_SERVER_ERROR);
    response.headers().add("Content-Type", "text/plain");
    response.headers().add("Content-Length", data.readableBytes());
    calculateKeepAlive(true);
    channel.write(response);

    DefaultHttpContent chunk = new DefaultHttpContent(data);
    channel.write(chunk);

    sendLastChunk();
    channel.flush();
    if (!keepAlive) {
      shutDown();
    }
  }
Esempio n. 10
0
  @SuppressWarnings("deprecation")
  private SpdySynReplyFrame createSynReplyFrame(HttpResponse httpResponse) throws Exception {
    // Get the Stream-ID from the headers
    final HttpHeaders httpHeaders = httpResponse.headers();
    int streamID = httpHeaders.getInt(Names.STREAM_ID);
    httpHeaders.remove(Names.STREAM_ID);

    // The Connection, Keep-Alive, Proxy-Connection, and Transfer-Encoding
    // headers are not valid and MUST not be sent.
    httpHeaders.remove(HttpHeaders.Names.CONNECTION);
    httpHeaders.remove(HttpHeaders.Names.KEEP_ALIVE);
    httpHeaders.remove(HttpHeaders.Names.PROXY_CONNECTION);
    httpHeaders.remove(HttpHeaders.Names.TRANSFER_ENCODING);

    SpdySynReplyFrame spdySynReplyFrame = new DefaultSpdySynReplyFrame(streamID);
    SpdyHeaders frameHeaders = spdySynReplyFrame.headers();
    // Unfold the first line of the response into name/value pairs
    frameHeaders.set(STATUS, httpResponse.status());
    frameHeaders.set(VERSION, httpResponse.protocolVersion());

    // Transfer the remaining HTTP headers
    for (Map.Entry<String, String> entry : httpHeaders) {
      spdySynReplyFrame.headers().add(entry.getKey(), entry.getValue());
    }

    currentStreamId = streamID;
    spdySynReplyFrame.setLast(isLast(httpResponse));

    return spdySynReplyFrame;
  }
  @Test
  public void testCompatibleExtensionTogetherSuccess() {
    // initialize
    expect(mainHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("main")))
        .andReturn(mainExtensionMock)
        .anyTimes();
    expect(mainHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("fallback")))
        .andReturn(null)
        .anyTimes();
    replay(mainHandshakerMock);

    expect(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("fallback")))
        .andReturn(fallbackExtensionMock)
        .anyTimes();
    expect(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("main")))
        .andReturn(null)
        .anyTimes();
    replay(fallbackHandshakerMock);

    expect(mainExtensionMock.rsv()).andReturn(WebSocketExtension.RSV1).anyTimes();
    expect(mainExtensionMock.newReponseData())
        .andReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()))
        .once();
    expect(mainExtensionMock.newExtensionEncoder()).andReturn(new DummyEncoder()).once();
    expect(mainExtensionMock.newExtensionDecoder()).andReturn(new DummyDecoder()).once();
    replay(mainExtensionMock);

    expect(fallbackExtensionMock.rsv()).andReturn(WebSocketExtension.RSV2).anyTimes();
    expect(fallbackExtensionMock.newReponseData())
        .andReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()))
        .once();
    expect(fallbackExtensionMock.newExtensionEncoder()).andReturn(new Dummy2Encoder()).once();
    expect(fallbackExtensionMock.newExtensionDecoder()).andReturn(new Dummy2Decoder()).once();
    replay(fallbackExtensionMock);

    // execute
    EmbeddedChannel ch =
        new EmbeddedChannel(
            new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));

    HttpRequest req = newUpgradeRequest("main, fallback");
    ch.writeInbound(req);

    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);

    HttpResponse res2 = ch.readOutbound();
    List<WebSocketExtensionData> resExts =
        WebSocketExtensionUtil.extractExtensions(
            res2.headers().getAndConvert(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));

    // test
    assertEquals(2, resExts.size());
    assertEquals("main", resExts.get(0).name());
    assertEquals("fallback", resExts.get(1).name());
    assertNotNull(ch.pipeline().get(DummyDecoder.class));
    assertNotNull(ch.pipeline().get(DummyEncoder.class));
    assertNotNull(ch.pipeline().get(Dummy2Decoder.class));
    assertNotNull(ch.pipeline().get(Dummy2Encoder.class));
  }
  @Test
  public void testNoneExtensionMatchingSuccess() {
    // initialize
    expect(mainHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("unknown")))
        .andReturn(null)
        .anyTimes();
    expect(mainHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("unknown2")))
        .andReturn(null)
        .anyTimes();
    replay(mainHandshakerMock);

    expect(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("unknown")))
        .andReturn(null)
        .anyTimes();
    expect(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataEqual("unknown2")))
        .andReturn(null)
        .anyTimes();
    replay(fallbackHandshakerMock);

    // execute
    EmbeddedChannel ch =
        new EmbeddedChannel(
            new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));

    HttpRequest req = newUpgradeRequest("unknown, unknown2");
    ch.writeInbound(req);

    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);

    HttpResponse res2 = ch.readOutbound();

    // test
    assertFalse(res2.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
  }
  /**
   * Sets the Date and Cache headers for the HTTP Response
   *
   * @param response HTTP response
   * @param fileToCache file to extract content type
   */
  private static void setDateAndCacheHeaders(HttpResponse response, File fileToCache) {
    SimpleDateFormat dateFormatter = new SimpleDateFormat(HTTP_DATE_FORMAT, Locale.US);
    dateFormatter.setTimeZone(TimeZone.getTimeZone(HTTP_DATE_GMT_TIMEZONE));

    // Date header
    Calendar time = new GregorianCalendar();
    response.headers().set(DATE, dateFormatter.format(time.getTime()));

    // Add cache headers
    time.add(Calendar.SECOND, HTTP_CACHE_SECONDS);
    response.headers().set(EXPIRES, dateFormatter.format(time.getTime()));
    response.headers().set(CACHE_CONTROL, "private, max-age=" + HTTP_CACHE_SECONDS);
    response
        .headers()
        .set(LAST_MODIFIED, dateFormatter.format(new Date(fileToCache.lastModified())));
  }
    public HttpResponse clientToProxyRequest(HttpObject httpObject) {
      if (httpObject instanceof DefaultHttpRequest) {

        DefaultHttpRequest fullreq = (DefaultHttpRequest) httpObject;
        Content c = null;
        // only return if content exist & at least one quality is
        // available
        if ((c = content.get(fullreq.getUri())) != null && c.getQualities().size() > 0) {

          LOGGER.debug("Cached resource found {}", c.getUri());

          HttpResponse response =
              new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.TEMPORARY_REDIRECT);
          Collections.shuffle(c.getQualities());

          String redirectUri =
              UriBuilder.fromPath(
                      "http://" + config.getFrontalHostName() + ":" + config.getFrontalPort())
                  .path("api")
                  .path("content")
                  .path(c.getId())
                  .path(c.getQualities().get(0))
                  .build()
                  .toString();
          response.headers().add("Location", redirectUri);
          LOGGER.debug("Redirecting it to ", redirectUri);
          return response;
        }
      }
      return null;
    }
 @Override
 public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
   logger.debug("================进入客户端InBoundHandler channelRead============");
   // 如果是response
   if (msg instanceof HttpResponse) {
     HttpResponse response = (HttpResponse) msg;
     logger.debug(
         "客户端收到的响应CONTENT_TYPE:" + response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
     if (HttpHeaders.isContentLengthSet(response)) {
       reader = new ByteBufToBytes((int) HttpHeaders.getContentLength(response));
     }
   }
   if (msg instanceof HttpContent) {
     HttpContent httpContent = (HttpContent) msg;
     ByteBuf content = httpContent.content();
     reader.reading(content);
     content.release();
     if (reader.isEnd()) {
       String resultStr = new String(reader.readFull());
       logger.debug("收到的服务端的消息是:" + resultStr);
       ctx.close();
     }
   }
   logger.debug("================出客户端InBoundHandler channelRead============");
 }
Esempio n. 16
0
 /**
  * Gets the headers of the blob with blob ID {@code blobId} and verifies them against what is
  * expected.
  *
  * @param blobId the blob ID of the blob to HEAD.
  * @param range the {@link ByteRange} for the request.
  * @param expectedHeaders the expected headers in the response.
  * @throws ExecutionException
  * @throws InterruptedException
  */
 private void getHeadAndVerify(String blobId, ByteRange range, HttpHeaders expectedHeaders)
     throws ExecutionException, InterruptedException, RestServiceException {
   HttpHeaders headers = null;
   if (range != null) {
     headers =
         new DefaultHttpHeaders()
             .add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
   }
   FullHttpRequest httpRequest = buildRequest(HttpMethod.HEAD, blobId, headers, null);
   Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
   HttpResponse response = (HttpResponse) responseParts.poll();
   assertEquals(
       "Unexpected response status",
       range == null ? HttpResponseStatus.OK : HttpResponseStatus.PARTIAL_CONTENT,
       response.getStatus());
   checkCommonGetHeadHeaders(response.headers());
   long contentLength = Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE));
   if (range != null) {
     Pair<String, Long> rangeAndLength =
         RestUtils.buildContentRangeAndLength(range, contentLength);
     assertEquals(
         "Content-Range header not set correctly",
         rangeAndLength.getFirst(),
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
     contentLength = rangeAndLength.getSecond();
   } else {
     assertNull(
         "Content-Range header should not be set",
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
   }
   assertEquals(
       "Accept-Ranges not set correctly",
       "bytes",
       response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
   assertEquals(
       RestUtils.Headers.CONTENT_LENGTH + " does not match expected",
       contentLength,
       HttpHeaders.getContentLength(response));
   assertEquals(
       RestUtils.Headers.CONTENT_TYPE + " does not match " + RestUtils.Headers.AMBRY_CONTENT_TYPE,
       expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
       HttpHeaders.getHeader(response, HttpHeaders.Names.CONTENT_TYPE));
   verifyBlobProperties(expectedHeaders, response);
   discardContent(responseParts, 1);
   assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
 }
  private static void testPerformOpeningHandshake0(boolean subProtocol) {
    EmbeddedChannel ch =
        new EmbeddedChannel(
            new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());

    FullHttpRequest req =
        ReferenceCountUtil.releaseLater(
            new DefaultFullHttpRequest(
                HTTP_1_1,
                HttpMethod.GET,
                "/chat",
                Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII)));

    req.headers().set(HttpHeaderNames.HOST, "server.example.com");
    req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET);
    req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade");
    req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1  46546xW%0l 1 5");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1  .P00");
    req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat");

    if (subProtocol) {
      new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE)
          .handshake(ch, req);
    } else {
      new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE)
          .handshake(ch, req);
    }

    EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder());
    ch2.writeInbound(ch.readOutbound());
    HttpResponse res = ch2.readInbound();

    Assert.assertEquals(
        "ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION));

    if (subProtocol) {
      Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    } else {
      Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL));
    }
    LastHttpContent content = ch2.readInbound();

    Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII));
    content.release();
  }
Esempio n. 18
0
 @Override
 public MultiMap headers() {
   synchronized (conn) {
     if (headers == null) {
       headers = new HeadersAdaptor(response.headers());
     }
     return headers;
   }
 }
  private boolean handleProxyAuthenticationRequiredAndExit(
      int statusCode,
      Realm realm,
      final Request request,
      HttpResponse response,
      final NettyResponseFuture<?> future,
      ProxyServer proxyServer)
      throws Exception {
    if (statusCode == PROXY_AUTHENTICATION_REQUIRED.code() && realm != null) {
      List<String> proxyAuthenticateHeaders =
          response.headers().getAll(HttpHeaders.Names.PROXY_AUTHENTICATE);
      if (!proxyAuthenticateHeaders.isEmpty() && !future.getAndSetAuth(true)) {
        LOGGER.debug("Sending proxy authentication to {}", request.getUrl());

        future.setState(NettyResponseFuture.STATE.NEW);
        Realm newRealm = null;

        boolean negociate = proxyAuthenticateHeaders.contains("Negotiate");
        if (!proxyAuthenticateHeaders.contains("Kerberos")
            && (isNTLM(proxyAuthenticateHeaders) || negociate)) {
          newRealm =
              ntlmProxyChallenge(
                  proxyAuthenticateHeaders,
                  request,
                  proxyServer,
                  request.getHeaders(),
                  realm,
                  future);
          // SPNEGO KERBEROS
        } else if (negociate) {
          newRealm =
              kerberosChallenge(
                  proxyAuthenticateHeaders,
                  request,
                  proxyServer,
                  request.getHeaders(),
                  realm,
                  future);
          if (newRealm == null) return true;
        } else {
          newRealm = future.getRequest().getRealm();
        }

        future.setReuseChannel(true);
        future.setConnectAllowed(true);
        requestSender.sendNextRequest(
            new RequestBuilder(future.getRequest())
                .setHeaders(request.getHeaders())
                .setRealm(newRealm)
                .build(),
            future);
        return true;
      }
    }
    return false;
  }
Esempio n. 20
0
 private void handleCookies(HttpResponse response) {
   String cookieString = request.headers().get(HttpHeaderNames.COOKIE);
   boolean i18nextFound = false;
   if (cookieString != null) {
     Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(cookieString);
     if (!cookies.isEmpty()) {
       // Reset the sessions if necessary.
       boolean findSession = false;
       for (Cookie cookie : cookies) {
         if (cookie
             .name()
             .equalsIgnoreCase(R66SESSION + Configuration.configuration.getHOST_ID())) {
           if (newSession) {
             findSession = false;
           } else {
             findSession = true;
             response
                 .headers()
                 .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
           }
         } else if (cookie.name().equalsIgnoreCase(I18NEXT)) {
           i18nextFound = true;
           cookie.setValue(lang);
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
         } else {
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
         }
       }
       if (!i18nextFound) {
         Cookie cookie = new DefaultCookie(I18NEXT, lang);
         response
             .headers()
             .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
       }
       newSession = false;
       if (!findSession) {
         if (admin != null) {
           response
               .headers()
               .add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
           logger.debug("AddSession: " + uriRequest + ":{}", admin);
         }
       }
     }
   } else {
     Cookie cookie = new DefaultCookie(I18NEXT, lang);
     response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(cookie));
     if (admin != null) {
       logger.debug("AddSession: " + uriRequest + ":{}", admin);
       response.headers().add(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.LAX.encode(admin));
     }
   }
 }
Esempio n. 21
0
 private boolean exitAfterHandlingHeaders(
     Channel channel,
     NettyResponseFuture<?> future,
     HttpResponse response,
     AsyncHandler<?> handler,
     HttpResponseHeaders responseHeaders,
     HttpRequest httpRequest)
     throws IOException, Exception {
   return !response.headers().isEmpty()
       && handler.onHeadersReceived(responseHeaders) != State.CONTINUE;
 }
Esempio n. 22
0
 /**
  * Gets the blob info of the blob with blob ID {@code blobId} and verifies them against what is
  * expected.
  *
  * @param blobId the blob ID of the blob to HEAD.
  * @param expectedHeaders the expected headers in the response.
  * @param usermetadata if non-null, this is expected to come as the body.
  * @throws ExecutionException
  * @throws InterruptedException
  */
 private void getBlobInfoAndVerify(String blobId, HttpHeaders expectedHeaders, byte[] usermetadata)
     throws ExecutionException, InterruptedException {
   FullHttpRequest httpRequest =
       buildRequest(HttpMethod.GET, blobId + "/" + RestUtils.SubResource.BlobInfo, null, null);
   Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
   HttpResponse response = (HttpResponse) responseParts.poll();
   assertEquals("Unexpected response status", HttpResponseStatus.OK, response.getStatus());
   checkCommonGetHeadHeaders(response.headers());
   verifyBlobProperties(expectedHeaders, response);
   verifyUserMetadata(expectedHeaders, response, usermetadata, responseParts);
   assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
 }
  public void channelRead(final ChannelHandlerContext remoteChannelCtx, final Object msg)
      throws Exception {
    LoggerUtil.debug(
        logger, uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY), "Remote msg", msg);

    remainMsgCount++;

    if (remainMsgCount <= 5) {
      remoteChannelCtx.read();
    }

    HttpObject ho = (HttpObject) msg;

    if (ho instanceof HttpResponse) {
      HttpResponse httpResponse = (HttpResponse) ho;

      LoggerUtil.info(
          forwardRestLogger,
          uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
          httpResponse.getStatus(),
          httpResponse.getProtocolVersion());

      httpResponse.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
      httpResponse.headers().set("Proxy-Connection", HttpHeaders.Values.KEEP_ALIVE);
    }

    if (uaChannel.isActive()) {
      uaChannel
          .writeAndFlush(ho)
          .addListener(
              new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                  LoggerUtil.debug(
                      logger,
                      uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
                      "Write to UA finished: " + future.isSuccess());
                  if (future.isSuccess()) {
                    remainMsgCount--;
                    remoteChannelCtx.read();
                    LoggerUtil.debug(
                        logger,
                        uaChannel.attr(ApnProxyConnectionAttribute.ATTRIBUTE_KEY),
                        "Fire read again");
                  } else {
                    remoteChannelCtx.close();
                  }
                }
              });
    } else {
      remoteChannelCtx.close();
    }
  }
Esempio n. 24
0
  private void sendMessage(
      HttpMessage msg, Channel channel, ByteBuf out, String type, ChannelPromise promise) {
    HttpResponse res = new DefaultHttpResponse(HTTP_1_1, HttpResponseStatus.OK);

    res.headers()
        .add(CONTENT_TYPE, type)
        .add("Set-Cookie", "io=" + msg.getSessionId())
        .add(CONNECTION, KEEP_ALIVE);

    addOriginHeaders(channel, res);
    HttpHeaders.setContentLength(res, out.readableBytes());

    // prevent XSS warnings on IE
    // https://github.com/LearnBoost/socket.io/pull/1333
    String userAgent = channel.attr(EncoderHandler.USER_AGENT).get();
    if (userAgent != null && (userAgent.contains(";MSIE") || userAgent.contains("Trident/"))) {
      res.headers().add("X-XSS-Protection", "0");
    }

    sendMessage(msg, channel, out, res, promise);
  }
  private static void parseHeaders(@NotNull HttpResponse response, @NotNull ByteBuf buffer) {
    StringBuilder builder = new StringBuilder();
    while (buffer.isReadable()) {
      builder.setLength(0);

      String key = null;
      boolean valueExpected = true;
      while (true) {
        int b = buffer.readByte();
        if (b < 0 || b == '\n') {
          break;
        }

        if (b != '\r') {
          if (valueExpected && b == ':') {
            valueExpected = false;

            key = builder.toString();
            builder.setLength(0);
            MessageDecoder.skipWhitespace(buffer);
          } else {
            builder.append((char) b);
          }
        }
      }

      if (builder.length() == 0) {
        // end of headers
        return;
      }

      // skip standard headers
      if (StringUtil.isEmpty(key)
          || StringUtilRt.startsWithIgnoreCase(key, "http")
          || StringUtilRt.startsWithIgnoreCase(key, "X-Accel-")) {
        continue;
      }

      String value = builder.toString();
      if (key.equalsIgnoreCase("status")) {
        int index = value.indexOf(' ');
        if (index == -1) {
          LOG.warn("Cannot parse status: " + value);
          response.setStatus(HttpResponseStatus.OK);
        } else {
          response.setStatus(
              HttpResponseStatus.valueOf(Integer.parseInt(value.substring(0, index))));
        }
      } else if (!(key.startsWith("http") || key.startsWith("HTTP"))) {
        response.headers().add(key, value);
      }
    }
  }
Esempio n. 26
0
 @Override
 public List<String> cookies() {
   synchronized (conn) {
     if (cookies == null) {
       cookies = new ArrayList<>();
       cookies.addAll(response.headers().getAll(HttpHeaders.SET_COOKIE));
       if (trailer != null) {
         cookies.addAll(trailer.trailingHeaders().getAll(HttpHeaders.SET_COOKIE));
       }
     }
     return cookies;
   }
 }
Esempio n. 27
0
 /**
  * Gets the blob with blob ID {@code blobId} and verifies that the headers and content match with
  * what is expected.
  *
  * @param blobId the blob ID of the blob to GET.
  * @param range the {@link ByteRange} for the request.
  * @param expectedHeaders the expected headers in the response.
  * @param expectedContent the expected content of the blob.
  * @throws ExecutionException
  * @throws InterruptedException
  */
 private void getBlobAndVerify(
     String blobId, ByteRange range, HttpHeaders expectedHeaders, ByteBuffer expectedContent)
     throws ExecutionException, InterruptedException, RestServiceException {
   HttpHeaders headers = null;
   if (range != null) {
     headers =
         new DefaultHttpHeaders()
             .add(RestUtils.Headers.RANGE, RestTestUtils.getRangeHeaderString(range));
   }
   FullHttpRequest httpRequest = buildRequest(HttpMethod.GET, blobId, headers, null);
   Queue<HttpObject> responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
   HttpResponse response = (HttpResponse) responseParts.poll();
   assertEquals(
       "Unexpected response status",
       range == null ? HttpResponseStatus.OK : HttpResponseStatus.PARTIAL_CONTENT,
       response.getStatus());
   checkCommonGetHeadHeaders(response.headers());
   assertEquals(
       "Content-Type does not match",
       expectedHeaders.get(RestUtils.Headers.AMBRY_CONTENT_TYPE),
       response.headers().get(HttpHeaders.Names.CONTENT_TYPE));
   assertEquals(
       RestUtils.Headers.BLOB_SIZE + " does not match",
       expectedHeaders.get(RestUtils.Headers.BLOB_SIZE),
       response.headers().get(RestUtils.Headers.BLOB_SIZE));
   assertEquals(
       "Accept-Ranges not set correctly",
       "bytes",
       response.headers().get(RestUtils.Headers.ACCEPT_RANGES));
   byte[] expectedContentArray = expectedContent.array();
   if (range != null) {
     long blobSize = Long.parseLong(expectedHeaders.get(RestUtils.Headers.BLOB_SIZE));
     assertEquals(
         "Content-Range header not set correctly",
         RestUtils.buildContentRangeAndLength(range, blobSize).getFirst(),
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
     ByteRange resolvedRange = range.toResolvedByteRange(blobSize);
     expectedContentArray =
         Arrays.copyOfRange(
             expectedContentArray,
             (int) resolvedRange.getStartOffset(),
             (int) resolvedRange.getEndOffset() + 1);
   } else {
     assertNull(
         "Content-Range header should not be set",
         response.headers().get(RestUtils.Headers.CONTENT_RANGE));
   }
   if (expectedContentArray.length < FRONTEND_CONFIG.frontendChunkedGetResponseThresholdInBytes) {
     assertEquals(
         "Content-length not as expected",
         expectedContentArray.length,
         HttpHeaders.getContentLength(response));
   }
   byte[] responseContentArray = getContent(responseParts, expectedContentArray.length).array();
   assertArrayEquals(
       "GET content does not match original content", expectedContentArray, responseContentArray);
   assertTrue("Channel should be active", HttpHeaders.isKeepAlive(response));
 }
  protected void contentType(FullHttpRequest request, HttpResponse response, File resource) {
    String substr;
    String uri = request.getUri();
    int dot = uri.lastIndexOf(".");
    if (dot < 0) {
      substr = resource.toString();
      dot = substr.lastIndexOf(".");
    } else {
      substr = uri;
    }

    if (dot > 0) {
      String ext = substr.substring(dot + 1);
      int queryString = ext.indexOf("?");
      if (queryString > 0) {
        ext.substring(0, queryString);
      }
      String contentType = MimeType.get(ext, defaultContentType);
      response.headers().add(HttpHeaders.Names.CONTENT_TYPE, contentType);
    } else {
      response.headers().add(HttpHeaders.Names.CONTENT_TYPE, defaultContentType);
    }
  }
  @Override
  public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
    if (msg instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) msg;

      System.out.println("STATUS: " + response.getStatus());
      System.out.println("VERSION: " + response.getProtocolVersion());
      System.out.println();

      if (!response.headers().isEmpty()) {
        for (String name : response.headers().names()) {
          for (String value : response.headers().getAll(name)) {
            System.out.println("HEADER: " + name + " = " + value);
          }
        }
        System.out.println();
      }

      if (HttpHeaders.isTransferEncodingChunked(response)) {
        System.out.println("CHUNKED CONTENT {");
      } else {
        System.out.println("CONTENT {");
      }
    }
    if (msg instanceof HttpContent) {
      HttpContent content = (HttpContent) msg;

      System.out.print(content.content().toString(CharsetUtil.UTF_8));
      System.out.flush();

      if (content instanceof LastHttpContent) {
        System.out.println("} END OF CONTENT");
        queue.add(ctx.channel().newSucceededFuture());
      }
    }
  }
  @Override
  public @Nullable FullHttpResponse handleRequest(ChannelHandlerContext ctx, HttpRequest request)
      throws Exception {

    File glowrootLogFile = new File(logDir, "glowroot.log");
    CharSource glowrootLogCharSource = Files.asCharSource(glowrootLogFile, Charsets.UTF_8);

    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
    response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain; charset=UTF-8");
    boolean keepAlive = HttpUtil.isKeepAlive(request);
    if (keepAlive && !request.protocolVersion().isKeepAliveDefault()) {
      response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
    }
    HttpServices.preventCaching(response);
    ctx.write(response);
    ChannelFuture future = ctx.write(ChunkedInputs.from(ChunkSource.from(glowrootLogCharSource)));
    HttpServices.addErrorListener(future);
    if (!keepAlive) {
      HttpServices.addCloseListener(future);
    }
    // return null to indicate streaming
    return null;
  }