コード例 #1
0
 @Override
 protected void doService(HttpRequest request, HttpResponse response, HttpContext context)
     throws HttpException, IOException {
   response.addHeader(HttpHeaders.CACHE_CONTROL, "must-revalidate"); // Disable caching
   // Normalize the URI
   super.doService(request, response, context);
 }
コード例 #2
0
  /**
   * Handles receives one HTTP request over the given connection within the given execution context
   * and sends a response back to the client.
   *
   * @param conn the active connection to the client
   * @param context the actual execution context.
   * @throws IOException in case of an I/O error.
   * @throws HttpException in case of HTTP protocol violation or a processing problem.
   */
  public void handleRequest(final HttpServerConnection conn, final HttpContext context)
      throws IOException, HttpException {

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);

    HttpResponse response = null;

    try {

      HttpRequest request = conn.receiveRequestHeader();
      request.setParams(new DefaultedHttpParams(request.getParams(), this.params));

      ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
      if (!ver.lessEquals(HttpVersion.HTTP_1_1)) {
        // Downgrade protocol version if greater than HTTP/1.1
        ver = HttpVersion.HTTP_1_1;
      }

      if (request instanceof HttpEntityEnclosingRequest) {

        if (((HttpEntityEnclosingRequest) request).expectContinue()) {
          response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_CONTINUE, context);
          response.setParams(new DefaultedHttpParams(response.getParams(), this.params));

          if (this.expectationVerifier != null) {
            try {
              this.expectationVerifier.verify(request, response, context);
            } catch (HttpException ex) {
              response =
                  this.responseFactory.newHttpResponse(
                      HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
              response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
              handleException(ex, response);
            }
          }
          if (response.getStatusLine().getStatusCode() < 200) {
            // Send 1xx response indicating the server expections
            // have been met
            conn.sendResponseHeader(response);
            conn.flush();
            response = null;
            conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
          }
        } else {
          conn.receiveRequestEntity((HttpEntityEnclosingRequest) request);
        }
      }

      if (response == null) {
        response = this.responseFactory.newHttpResponse(ver, HttpStatus.SC_OK, context);
        response.setParams(new DefaultedHttpParams(response.getParams(), this.params));

        context.setAttribute(ExecutionContext.HTTP_REQUEST, request);
        context.setAttribute(ExecutionContext.HTTP_RESPONSE, response);

        this.processor.process(request, context);
        doService(request, response, context);
      }

      // Make sure the request content is fully consumed
      if (request instanceof HttpEntityEnclosingRequest) {
        HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
        if (entity != null) {
          entity.consumeContent();
        }
      }

    } catch (HttpException ex) {
      response =
          this.responseFactory.newHttpResponse(
              HttpVersion.HTTP_1_0, HttpStatus.SC_INTERNAL_SERVER_ERROR, context);
      response.setParams(new DefaultedHttpParams(response.getParams(), this.params));
      handleException(ex, response);
    }

    this.processor.process(response, context);
    conn.sendResponseHeader(response);
    conn.sendResponseEntity(response);
    conn.flush();

    if (!this.connStrategy.keepAlive(response, context)) {
      conn.close();
    }
  }