Exemplo n.º 1
0
  private void doGet(OutputStream os, String url) throws IOException {
    if ("/".equals(url)) url = "/index.html";

    List<String> headers = new ArrayList<String>();
    headers.add("HTTP/1.1 200 OK\r\n");

    byte[] content = fm.get(url);

    if (content == null) {
      returnStatusCode(404, os);
      return;
    }

    ProcessorsList pl = new ProcessorsList();
    pl.add(new Compressor(6));
    // pl.add(new Chunker(30)); // comment
    content = pl.process(content, headers);

    if (content == null) {
      returnStatusCode(500, os);
      return;
    }

    // uncomment next line
    headers.add("Content-Length: " + content.length + "\r\n");
    headers.add("Connection: close\r\n\r\n");
    os.write(getBinaryHeaders(headers));
    os.write(content);
  }
Exemplo n.º 2
0
  private void doPost(OutputStream os, String request) throws IOException {
    // String content = "Test POST reply";
    String boundary = "";

    int boundaryStart = request.indexOf("boundary=") + "boundary=".length();
    if (boundaryStart != -1)
      boundary = request.substring(boundaryStart, request.indexOf("\r\n", boundaryStart));

    boundary = "--" + boundary;
    int payloadStart = request.indexOf(boundary);
    payloadStart += ((boundary).length() + 2);
    int payloadEnd = request.indexOf(boundary + "--") - 3;
    String[] payloads = request.substring(payloadStart, payloadEnd).split(boundary);
    String[] fileNames = new String[payloads.length];
    for (int i = 0; i < payloads.length; i++) fileNames[i] = getFileName(payloads[i]);
    int contentLength = 0;
    for (int i = 0; i < payloads.length; i++) {
      int payLoadBeginIndex = payloads[i].indexOf("\r\n\r\n") + 4;
      if (payLoadBeginIndex != -1) {
        payloads[i] = payloads[i].substring(payLoadBeginIndex, payloads[i].length());
        contentLength += payloads[i].length();
      }
    }

    ByteArrayOutputStream zipData = new ByteArrayOutputStream();
    ZipOutputStream zos = new ZipOutputStream(zipData);

    for (int i = 0; i < payloads.length; i++) {
      zos.putNextEntry(new ZipEntry(fileNames[i]));
      zos.write(payloads[i].getBytes(), 0, payloads[i].length());
      zos.closeEntry();
    }
    zos.flush();
    zos.close();
    byte[] content = zipData.toByteArray();

    List<String> headers = new ArrayList<String>();
    headers.add("HTTP/1.1 200 OK\r\n");
    headers.add("Content-Transfer-Encoding:binary\r\n");
    headers.add("Content-Type:application/zip\r\n");
    headers.add("Content-Disposition:attachment; filename=\"_" + fileNames[0] + ".zip\"\r\n");
    ProcessorsList pl = new ProcessorsList();
    pl.add(new Compressor(9));
    pl.add(new Chunker(30));
    content = pl.process(content, headers);

    headers.add("Connection: close\r\n\r\n");
    os.write(getBinaryHeaders(headers));
    os.write(content);
  }