Пример #1
0
 static String toString(HttpRequest request) {
   return StringUtil.image(request.toString(), REQUEST_DUMP_MAX);
 }
Пример #2
0
  public void handle(
      String requestPath, String pathParams, HttpRequest request, HttpResponse response)
      throws IOException {
    String threadID = Thread.currentThread().toString();

    HCFile file = null;
    long startTime = System.currentTimeMillis();

    if (logger.isLoggable(Level.INFO)) {
      String sAddr = request.getHttpConnection().getRemoteHost();
      int sPort = getClientPort(request);
      logger.info("StartRequest " + sAddr + ":" + sPort + " " + toString(request) + " " + threadID);
    }

    String sMethod = null;
    int method = 0;
    try {
      sMethod = request.getMethod();
      Integer m = (Integer) methods.get(sMethod);
      method = m.intValue();
    } catch (Exception e) {
      logger.info("Bad method \"" + sMethod + "\": 501 Not implemented");
      response.sendError(HttpResponse.__501_Not_Implemented, sMethod);
      request.setHandled(true);
      return;
    }

    if (!pathSanityCheck(requestPath)) {
      response.sendError(HttpResponse.__404_Not_Found, requestPath + ":  no such file");
      request.setHandled(true);
      return;
    }

    // Remove the root path from the front of the path leaving a
    // relative path

    HCFile root = HCFile.getRoot();
    String rootPath = root.fileName();
    if (rootPath.equals("/")) rootPath = "";
    if (!requestPath.startsWith(rootPath + "/")) {
      response.sendError(HttpResponse.__404_Not_Found, "Unknown prefix in \"" + requestPath + "\"");
      request.setHandled(true);
      return;
    }

    String encodedPath = request.getEncodedPath();
    String prefix = ProtocolConstants.WEBDAV_PATH + rootPath + "/";
    if (!encodedPath.startsWith(prefix)) {
      response.sendError(
          HttpResponse.__500_Internal_Server_Error, "Inconsistent path \"" + encodedPath + "\"");
      request.setHandled(true);
      return;
    }
    String relativePath = encodedPath.substring(prefix.length());

    // Split path into components

    String[] path = FSCache.split(relativePath);
    if (logger.isLoggable(Level.FINE))
      logger.fine(
          "Instr parsed \""
              + FSCache.combine(path)
              + "\" @ "
              + (System.currentTimeMillis() - startTime)
              + " ms)");

    // Load into cache

    try {
      boolean writing = (method == M_PUT || method == M_MOVE || method == M_MKCOL);

      file = CacheLoader.load(path, writing);

      if (logger.isLoggable(Level.FINE)) {
        long d = System.currentTimeMillis() - startTime;
        String f = file.fileName();
        logger.fine("instr " + d + " LOOKUP \"" + f + "\" " + threadID);
        logger.fine(
            "View "
                + file.getViewName()
                + " is "
                + (file.isViewCollapsingNulls() ? "" : "not ")
                + "collapsing trailing nulls");
      }

      String[] extras = null;
      if (file.fileType() == FSCacheObject.ROOTFILETYPE) extras = path;
      else {
        // The distance of the node from the root == the number
        // of components in the full path since "/" has 0 path
        // components.

        int numExtraComps = path.length - file.rootDistance();
        if (numExtraComps > 0) {
          extras = new String[numExtraComps];
          for (int i = file.rootDistance(), j = 0; j < extras.length; i++, j++) extras[j] = path[i];
        }
      }

      if (file.isFile())
        switch (method) {
          case M_GET:
          case M_HEAD:
          case M_OPTIONS:
          case M_PROPFIND:
            response.setField("ETag", file.getOID().toExternalHexString());
        }

      response.setStatus(HttpResponse.__200_OK);

      if (logger.isLoggable(Level.FINE)) {
        String s = "Instr handling " + file + " {";
        if (extras != null) for (int i = 0; i < extras.length; i++) s += " \"" + extras[i] + "\"";
        logger.fine(s + " } @ " + (System.currentTimeMillis() - startTime) + " ms");
      }

      handlers[method].handle(
          file, extras, request, response, request.getInputStream(), response.getOutputStream());
    } catch (FSCacheException e) {
      if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, "FSCacheException: " + e, e);
      response.sendError(e.getError(), e.getMessage());
    } catch (HttpException e) {
      if (logger.isLoggable(Level.FINE)) logger.log(Level.FINE, "HTTP exception: " + e, e);
      response.sendError(e.getCode(), e.toString());
    } catch (Exception e) {
      String s = "Couldn't handle " + toString(request);
      logger.log(Level.SEVERE, s + "; " + StringUtil.image(file), e);
      response.sendError(HttpResponse.__500_Internal_Server_Error, s + ": " + StringUtil.image(e));
    }

    long reqTime = System.currentTimeMillis() - startTime;
    if (reqTime > MAX_REQ_TIME) logger.warning("LATE! " + reqTime + "ms " + toString(request));
    if (logger.isLoggable(Level.INFO))
      logger.info(
          "INSTR "
              + reqTime
              + " SUM "
              + response.getStatus()
              + " "
              + toString(request)
              + " "
              + threadID);

    if (logger.isLoggable(Level.FINEST)) logger.finest(HCFile.fileCache.toString());

    request.setHandled(true);
  }