Example #1
0
  public void inputReady(NHttpServerConnection conn, ContentDecoder decoder) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);

      if (protocolState != ProtocolState.REQUEST_HEAD
          && protocolState != ProtocolState.REQUEST_BODY) {
        handleInvalidState(conn, "Request message body data received");
        return;
      }

      SourceContext.updateState(conn, ProtocolState.REQUEST_BODY);

      SourceRequest request = SourceContext.getRequest(conn);

      int readBytes = request.read(conn, decoder);
      if (readBytes > 0) {
        metrics.incrementBytesReceived(readBytes);
      }
    } catch (IOException e) {
      logIOException(conn, e);

      informReaderError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #2
0
  public void exception(NHttpServerConnection conn, Exception ex) {
    if (ex instanceof IOException) {
      logIOException(conn, (IOException) ex);

      metrics.incrementFaultsReceiving();

      ProtocolState state = SourceContext.getState(conn);
      if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
        informReaderError(conn);
      } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
        informWriterError(conn);
      } else if (state == ProtocolState.REQUEST_DONE) {
        informWriterError(conn);
      } else if (state == ProtocolState.RESPONSE_DONE) {
        informWriterError(conn);
      }

      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } else if (ex instanceof HttpException) {
      try {
        if (conn.isResponseSubmitted()) {
          sourceConfiguration.getSourceConnections().shutDownConnection(conn);
          return;
        }
        HttpContext httpContext = conn.getContext();

        HttpResponse response =
            new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "Bad request");
        response.setParams(
            new DefaultedHttpParams(sourceConfiguration.getHttpParams(), response.getParams()));
        response.addHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);

        // Pre-process HTTP request
        httpContext.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
        httpContext.setAttribute(ExecutionContext.HTTP_REQUEST, null);
        httpContext.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

        sourceConfiguration.getHttpProcessor().process(response, httpContext);

        conn.submitResponse(response);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        conn.close();
      } catch (Exception ex1) {
        log.error(ex.getMessage(), ex);
        SourceContext.updateState(conn, ProtocolState.CLOSED);
        sourceConfiguration.getSourceConnections().shutDownConnection(conn);
      }
    } else {
      log.error("Unexoected error: " + ex.getMessage(), ex);
      SourceContext.updateState(conn, ProtocolState.CLOSED);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #3
0
  public void closed(NHttpServerConnection conn) {
    ProtocolState state = SourceContext.getState(conn);

    if (state == ProtocolState.REQUEST_READY || state == ProtocolState.RESPONSE_DONE) {
      if (log.isDebugEnabled()) {
        log.debug(conn + ": Keep-Alive connection was closed: " + conn);
      }
    } else if (state == ProtocolState.REQUEST_BODY || state == ProtocolState.REQUEST_HEAD) {
      informReaderError(conn);
      log.warn("Connection closed while reading the request: " + conn);
    } else if (state == ProtocolState.RESPONSE_BODY || state == ProtocolState.RESPONSE_HEAD) {
      informWriterError(conn);
      log.warn("Connection closed while writing the response: " + conn);
    } else if (state == ProtocolState.REQUEST_DONE) {
      log.warn("Connection closed by the client after request is read: " + conn);
    }

    metrics.disconnected();

    SourceContext.updateState(conn, ProtocolState.CLOSED);
    sourceConfiguration.getSourceConnections().shutDownConnection(conn);
  }
Example #4
0
  public void responseReady(NHttpServerConnection conn) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);
      if (protocolState.compareTo(ProtocolState.REQUEST_DONE) < 0) {
        return;
      }

      if (protocolState.compareTo(ProtocolState.CLOSING) >= 0) {
        return;
      }

      if (protocolState != ProtocolState.REQUEST_DONE) {
        handleInvalidState(conn, "Writing a response");
        return;
      }

      // because the duplex nature of http core we can reach hear without a actual response
      SourceResponse response = SourceContext.getResponse(conn);
      if (response != null) {
        response.start(conn);

        metrics.incrementMessagesSent();
      }
    } catch (IOException e) {
      logIOException(conn, e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    } catch (HttpException e) {
      log.error(e.getMessage(), e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }
Example #5
0
 private void handleInvalidState(NHttpServerConnection conn, String action) {
   log.warn(
       action + " while the handler is in an inconsistent state " + SourceContext.getState(conn));
   SourceContext.updateState(conn, ProtocolState.CLOSED);
   sourceConfiguration.getSourceConnections().shutDownConnection(conn);
 }
Example #6
0
  public void outputReady(NHttpServerConnection conn, ContentEncoder encoder) {
    try {
      ProtocolState protocolState = SourceContext.getState(conn);

      // special case to handle WSDLs
      if (protocolState == ProtocolState.WSDL_RESPONSE_DONE) {
        // we need to shut down if the shutdown flag is set
        HttpContext context = conn.getContext();
        ContentOutputBuffer outBuf =
            (ContentOutputBuffer) context.getAttribute("synapse.response-source-buffer");
        int bytesWritten = outBuf.produceContent(encoder);
        if (metrics != null && bytesWritten > 0) {
          metrics.incrementBytesSent(bytesWritten);
        }

        conn.requestInput();
        if (outBuf instanceof SimpleOutputBuffer && !((SimpleOutputBuffer) outBuf).hasData()) {
          sourceConfiguration.getSourceConnections().releaseConnection(conn);
        }

        return;
      }

      if (protocolState != ProtocolState.RESPONSE_HEAD
          && protocolState != ProtocolState.RESPONSE_BODY) {
        log.warn(
            "Illegal incoming connection state: "
                + protocolState
                + " . Possibly two send backs "
                + "are happening for the same request");

        handleInvalidState(conn, "Trying to write response body");
        return;
      }

      SourceContext.updateState(conn, ProtocolState.RESPONSE_BODY);

      SourceResponse response = SourceContext.getResponse(conn);

      int bytesSent = response.write(conn, encoder);

      if (encoder.isCompleted()) {
        HttpContext context = conn.getContext();
        if (context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME) != null
            && context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME) != null
            && context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME) != null) {

          if (latencyView != null) {
            latencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          } else if (s2sLatencyView != null) {
            s2sLatencyView.notifyTimes(
                (Long) context.getAttribute(PassThroughConstants.REQ_ARRIVAL_TIME),
                (Long) context.getAttribute(PassThroughConstants.REQ_DEPARTURE_TIME),
                (Long) context.getAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME),
                System.currentTimeMillis());
          }
        }

        context.removeAttribute(PassThroughConstants.REQ_ARRIVAL_TIME);
        context.removeAttribute(PassThroughConstants.REQ_DEPARTURE_TIME);
        context.removeAttribute(PassThroughConstants.RES_HEADER_ARRIVAL_TIME);
      }

      metrics.incrementBytesSent(bytesSent);
    } catch (IOException e) {
      logIOException(conn, e);

      informWriterError(conn);

      SourceContext.updateState(conn, ProtocolState.CLOSING);
      sourceConfiguration.getSourceConnections().shutDownConnection(conn);
    }
  }