public void receive() {
   EndPoint endPoint = connection.getEndPoint();
   HttpClient client = connection.getHttpClient();
   ByteBufferPool bufferPool = client.getByteBufferPool();
   ByteBuffer buffer = bufferPool.acquire(client.getResponseBufferSize(), true);
   try {
     while (true) {
       // Connection may be closed in a parser callback
       if (connection.isClosed()) {
         LOG.debug("{} closed", connection);
         break;
       } else {
         int read = endPoint.fill(buffer);
         LOG.debug("Read {} bytes from {}", read, connection);
         if (read > 0) {
           parse(buffer);
         } else if (read == 0) {
           fillInterested();
           break;
         } else {
           shutdown();
           break;
         }
       }
     }
   } catch (EofException x) {
     LOG.ignore(x);
     failAndClose(x);
   } catch (Exception x) {
     LOG.debug(x);
     failAndClose(x);
   } finally {
     bufferPool.release(buffer);
   }
 }
  @Override
  public boolean headerComplete() {
    if (updateState(State.RECEIVE, State.RECEIVE)) {
      HttpExchange exchange = connection.getExchange();
      // The exchange may be null if it failed concurrently
      if (exchange != null) {
        HttpConversation conversation = exchange.getConversation();
        HttpResponse response = exchange.getResponse();
        LOG.debug("Headers {}", response);
        ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
        notifier.notifyHeaders(conversation.getResponseListeners(), response);

        Enumeration<String> contentEncodings =
            response.getHeaders().getValues(HttpHeader.CONTENT_ENCODING.asString(), ",");
        if (contentEncodings != null) {
          for (ContentDecoder.Factory factory :
              connection.getHttpClient().getContentDecoderFactories()) {
            while (contentEncodings.hasMoreElements()) {
              if (factory.getEncoding().equalsIgnoreCase(contentEncodings.nextElement())) {
                this.decoder = factory.newContentDecoder();
                break;
              }
            }
          }
        }
      }
    }
    return false;
  }
  @Override
  public boolean startResponse(HttpVersion version, int status, String reason) {
    if (updateState(State.IDLE, State.RECEIVE)) {
      HttpExchange exchange = connection.getExchange();
      // The exchange may be null if it failed concurrently
      if (exchange != null) {
        HttpConversation conversation = exchange.getConversation();
        HttpResponse response = exchange.getResponse();

        String method = exchange.getRequest().method();
        parser.setHeadResponse(HttpMethod.HEAD.is(method) || HttpMethod.CONNECT.is(method));
        response.version(version).status(status).reason(reason);

        // Probe the protocol handlers
        HttpClient client = connection.getHttpClient();
        ProtocolHandler protocolHandler =
            client.findProtocolHandler(exchange.getRequest(), response);
        Response.Listener handlerListener = null;
        if (protocolHandler != null) {
          handlerListener = protocolHandler.getResponseListener();
          LOG.debug("Found protocol handler {}", protocolHandler);
        }
        exchange.getConversation().updateResponseListeners(handlerListener);

        LOG.debug("Receiving {}", response);
        ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
        notifier.notifyBegin(conversation.getResponseListeners(), response);
      }
    }
    return false;
  }
 private void storeCookie(URI uri, HttpField field) {
   try {
     String value = field.getValue();
     if (value != null) {
       Map<String, List<String>> header = new HashMap<>(1);
       header.put(field.getHeader().asString(), Collections.singletonList(value));
       connection.getHttpClient().getCookieManager().put(uri, header);
     }
   } catch (IOException x) {
     LOG.debug(x);
   }
 }