Example #1
0
  // URIと設定から呼び出し先QUEUEを決める。
  private MappingEntry mappingQueue(HttpContext httpContext) {
    String uri = httpContext.getRequestUri();
    if (MappingEntry.controllerEntry.matches(uri)) { // controllerへのリクエストか?
      httpContext.setAttribute(HttpContext.ATTRIBUTE_MAPPING_ENTRY, MappingEntry.controllerEntry);
      return MappingEntry.controllerEntry;
    }
    String clientIp = httpContext.getClientIp();

    // リクエストがReplay対象か?
    if (config.isReplay(clientIp, uri)) {
      return MappingEntry.replayEntry;
    }
    if (httpContext.isProxyRequest()) {
      // TODO 強制的にWebサーバとして動作するモード要,
      // requestLineがhttp://から始まってもクライアントが間違って送ってきたと判断する
      return MappingEntry.proxyEntry;
    }
    MappingEntry mappingEntry = config.mapping(uri);
    if (mappingEntry == null) {
      return null;
    }

    // マッピング先がReplay対象か?
    if (config.isReplay(clientIp, mappingEntry.getDestination())) {
      return MappingEntry.replayEntry;
    }
    httpContext.setAttribute(HttpContext.ATTRIBUTE_MAPPING_ENTRY, mappingEntry);
    return mappingEntry;
  }
Example #2
0
 private MappingEntry responseWebAuthenticationForm(HttpContext httpContext, String orgPath) {
   // 認証画面をVelocityPageから出力する
   if (orgPath == null) {
     orgPath = httpContext.getRequestUri();
   }
   httpContext.setAttribute("orgPath", orgPath);
   httpContext.setAttribute(HttpContext.ATTRIBUTE_VELOCITY_PAGE, webAuthenticateForm);
   return MappingEntry.velopageEntry;
 }
    public void doReadLoop() {
      HttpContext context = new BasicHttpContext();
      context.setAttribute(CX_SOCKET, rawSocket);

      while (!Thread.interrupted() && this.htConn.isOpen() && HttpServer.this.shouldRun) {
        // Clear the context from any auth settings; since this is done
        // anew on each connection..
        context.removeAttribute(CX_AUTH);

        try {
          HttpServer.this.httpService.handleRequest(htConn, context);
        } catch (ConnectionClosedException ex_closed) {
          break;
        } catch (IOException ex) {
          if (!closeRequested) {
            ex.printStackTrace();
          }
          break;
        } catch (HttpException ex) {
          ex.printStackTrace();
          break;
        } catch (ResponseHandledException ex) {
          break;
        }
      }
      bail();
    }
  /**
   * 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();
    }
  }
 public void setAttribute(String s, Object obj) {
   context.setAttribute(s, obj);
 }