Пример #1
0
 private byte[] createAMFMessage(String... s) {
   String data =
       "0003000000010046636f6d2e627269676874636f76652e706c617965722e72756e74696d652e506c617965724d656469614661636164652e66696e644d6564696142795265666572656e6365496400022f3100000";
   data +=
       "06"
           + "50a00000004020028"
           + JDHexUtils.getHexString(s[0]); // 0x06(String marker) + length + String b
   data += "00428ad6ca5dbb8800020020";
   data += JDHexUtils.getHexString(s[2]);
   data += "0040f9327000000000";
   return JDHexUtils.getByteArray(data);
 }
    public void run() {
      try {
        InputStream requestInputStream = Current_Socket.getInputStream();
        BufferedInputStream reader = new BufferedInputStream(requestInputStream);

        String line = null;
        HashMap<String, String> headers = new HashMap<String, String>();

        while ((line = readline(reader)) != null && line.trim().length() > 0) {
          String key = null;
          String value = null;
          if (line.indexOf(": ") > 0) {
            key = line.substring(0, line.indexOf(": ")).toLowerCase();
            value = line.substring(line.indexOf(": ") + 2);
          } else {
            key = null;
            value = line;
          }
          headers.put(key, value);
        }

        if (headers.containsKey(null)) {
          String Method = headers.get(null).split(" ")[0];
          if (Method.compareToIgnoreCase("get") == 0 || Method.compareToIgnoreCase("post") == 0) {
            /* get oder post header gefunden */
            if (headers.containsKey("content-type")) {
              if (headers
                      .get("content-type")
                      .compareToIgnoreCase("application/x-www-form-urlencoded")
                  == 0) {
                if (headers.containsKey("content-length")) {
                  /*
                   * POST Form Daten in GET Format übersetzen,
                   * damit der RequestParams Parser nicht
                   * geändert werden muss
                   */
                  int post_len = new Integer(headers.get("content-length"));
                  int post_len_toread = post_len;
                  int post_len_read = 0;
                  byte[] cbuf = new byte[post_len];
                  int indexstart = 0;
                  while (post_len_toread > 0) {
                    if ((post_len_read = reader.read(cbuf, indexstart, post_len_toread)) == -1) {
                      break;
                    }
                    indexstart = indexstart + post_len_read;
                    post_len_toread = post_len_toread - post_len_read;
                  }
                  String RequestParams = new String(cbuf).trim();
                  if (indexstart == post_len) {
                    /*
                     * alten POST aus Header Liste holen,
                     * neuen zusammenbauen
                     */
                    String request = headers.get(null);
                    String[] requ = request.split(" ");
                    if (Method.compareToIgnoreCase("post") == 0) {
                      headers.put(
                          null, requ[0] + " " + requ[1] + "?" + RequestParams + " " + requ[2]);
                    } else {
                      logger.severe("POST Daten bei nem GET aufruf???");
                    }
                  } else {
                    logger.severe(
                        "POST Fehler postlen soll = "
                            + post_len
                            + " postlen gelesen = "
                            + post_len_read);
                  }
                }
              } else if (headers.get("content-type").contains("multipart/form-data")) {
                /*
                 * POST Form Daten in GET Format übersetzen,
                 * damit der RequestParams Parser nicht geändert
                 * werden muss
                 *
                 * Zusätzlich das File auslesen (die komplette
                 * Verarbeiten findet auf Hex statt!!)
                 */
                if (headers.containsKey("content-length")) {
                  int post_len = new Integer(headers.get("content-length"));
                  int post_len_toread = post_len;
                  int post_len_read = 0;
                  byte[] cbuf = new byte[post_len];
                  int indexstart = 0;
                  String limiter =
                      new Regex(
                              headers.get("content-type"),
                              Pattern.compile("boundary=(.*)", Pattern.CASE_INSENSITIVE))
                          .getMatch(0);
                  if (limiter != null) {
                    /*
                     * nur weitermachen falls ein limiter
                     * vorhanden ist
                     */
                    limiter = "--" + limiter;
                    limiter = JDHexUtils.getHexString(limiter);
                    while (post_len_toread > 0) {
                      if ((post_len_read = reader.read(cbuf, indexstart, post_len_toread)) == -1) {
                        break;
                      }
                      indexstart = indexstart + post_len_read;
                      post_len_toread = post_len_toread - post_len_read;
                    }
                    if (indexstart == post_len) {
                      String RequestParams = "";
                      /*
                       * momentan wird multipart nur für
                       * containerupload genutzt, daher
                       * form-data parsing unnötig
                       */
                      String MultiPartData[][] =
                          new Regex(
                                  JDHexUtils.getHexString(cbuf),
                                  Pattern.compile(
                                      limiter
                                          + JDHexUtils.getHexString("\r")
                                          + "{0,1}"
                                          + JDHexUtils.getHexString("\n")
                                          + "{0,1}"
                                          + JDHexUtils.REGEX_MATCH_ALL_HEX
                                          + "(?="
                                          + ""
                                          + JDHexUtils.getHexString("\r")
                                          + "{0,1}"
                                          + JDHexUtils.getHexString("\n")
                                          + "{0,1}"
                                          + limiter
                                          + ")",
                                      Pattern.CASE_INSENSITIVE | Pattern.DOTALL))
                              .getMatches();
                      for (String[] element : MultiPartData) {
                        if (element[0].contains(
                            JDHexUtils.getHexString(
                                "Content-Disposition: form-data; name=\"container\""))) {
                          String containertyp =
                              new Regex(
                                      element[0],
                                      Pattern.compile(
                                          JDHexUtils.getHexString("filename=\"")
                                              + JDHexUtils.REGEX_FIND_ALL_HEX
                                              + JDHexUtils.getHexString(".")
                                              + JDHexUtils.REGEX_MATCH_ALL_HEX
                                              + JDHexUtils.getHexString("\""),
                                          Pattern.CASE_INSENSITIVE))
                                  .getMatch(0);
                          if (containertyp != null) {
                            containertyp = new String(JDHexUtils.getByteArray(containertyp));
                          }
                          if (containertyp != null
                              && (containertyp.contains("dlc")
                                  || containertyp.contains("ccf")
                                  || containertyp.contains("rsdf")
                                  || containertyp.contains("jdc"))) {
                            File containerfile =
                                JDUtilities.getResourceFile(
                                    "container/" + System.currentTimeMillis() + "." + containertyp);
                            if (JDIO.saveToFile(
                                containerfile,
                                JDHexUtils.getByteArray(
                                    element[0].substring(
                                        element[0].indexOf(JDHexUtils.getHexString("\r\n\r\n"))
                                            + 8)))) {
                              /*
                               * RequestParameter
                               * zusammenbauen
                               */
                              RequestParams =
                                  "do=Upload&file=" + Encoding.urlEncode(containerfile.getName());
                              break;
                            }
                          } else {
                            if (containertyp != null) {
                              logger.severe("unknown container typ: " + containertyp);
                            }
                          }
                        }
                      }
                      /*
                       * alten POST aus Header Liste
                       * holen, neuen zusammenbauen
                       */
                      String request = headers.get(null);
                      String[] requ = request.split(" ");
                      if (Method.compareToIgnoreCase("post") == 0) {
                        headers.put(
                            null, requ[0] + " " + requ[1] + "?" + RequestParams + " " + requ[2]);
                      } else {
                        logger.severe("POST Daten bei nem GET aufruf???");
                      }
                    } else {
                      logger.severe(
                          "POST Fehler postlen soll = "
                              + post_len
                              + " postlen gelesen = "
                              + post_len_read);
                    }
                  }
                }
              }
            }

            JDSimpleWebserverResponseCreator response = new JDSimpleWebserverResponseCreator();
            JDSimpleWebserverRequestHandler request =
                new JDSimpleWebserverRequestHandler(headers, response);
            OutputStream outputStream = Current_Socket.getOutputStream();
            if (NeedAuth == true) {
                /* need authorization */
              if (headers.containsKey("authorization")) {
                if (JDSimpleWebserver.AuthUser.equals(headers.get("authorization"))) {
                  /*
                   * send authorization granted
                   */
                  /* logger.info("pass stimmt"); */
                  request.handle();

                } else {
                    /* send authorization failed */
                  response.setAuth_failed();
                }
              } else {
                  /* send autorization needed */
                response.setAuth_needed();
              }
            } else {
                /* no autorization needed */
              request.handle();
            }

            response.writeToStream(outputStream);
            outputStream.close();
          }
        } else {
          /* kein get oder post header */
          logger.severe("kein post oder get header");
        }
        Current_Socket.close();

      } catch (SocketException e) {
        logger.severe("WebInterface: Socket error");
      } catch (IOException e) {
        logger.severe("WebInterface: I/O Error");
      }
    }