Esempio n. 1
0
 public static void sendHTTPResponse(OutputStream out, int responseCode, boolean hasData)
     throws IOException {
   StringBuffer httpResponse = new StringBuffer();
   httpResponse.append(Integer.toString(responseCode)).append(" ");
   httpResponse.append(HTTPUtil.getHTTPResponseMessage(responseCode));
   httpResponse.append("\r\n");
   StringBuffer response = new StringBuffer("HTTP/1.1 ");
   response.append(httpResponse);
   out.write(response.toString().getBytes());
   if (!hasData) { // if no data will be sent, write the HTTP code
     out.write("\r\n".getBytes());
     out.write(httpResponse.toString().getBytes());
   }
 }
Esempio n. 2
0
  public static byte[] readData(InputStream inStream, OutputStream outStream, Message msg)
      throws IOException, MessagingException {
    byte[] data = null;

    // Get the stream and read in the HTTP request and headers
    BufferedInputStream in = new BufferedInputStream(inStream);
    String[] request = HTTPUtil.readRequest(in);
    msg.setAttribute(MA_HTTP_REQ_TYPE, request[0]);
    msg.setAttribute(MA_HTTP_REQ_URL, request[1]);
    msg.setHeaders(new InternetHeaders(in));
    DataInputStream dataIn = new DataInputStream(in);
    // Retrieve the message content
    if (msg.getHeader("Content-Length") == null) {
      String transfer_encoding = msg.getHeader("Transfer-Encoding");

      if (transfer_encoding != null) {
        if (transfer_encoding.replaceAll("\\s+", "").equalsIgnoreCase("chunked")) {
          int length = 0;
          data = null;
          for (; ; ) {
            // First get hex chunk length; followed by CRLF
            int blocklen = 0;
            for (; ; ) {
              int ch = dataIn.readByte();
              if (ch == '\n') {
                break;
              }
              if (ch >= 'a' && ch <= 'f') {
                ch -= ('a' - 10);
              } else if (ch >= 'A' && ch <= 'F') {
                ch -= ('A' - 10);
              } else if (ch >= '0' && ch <= '9') {
                ch -= '0';
              } else {
                continue;
              }
              blocklen = (blocklen * 16) + ch;
            }
            // Zero length is end of chunks
            if (blocklen == 0) break;
            // Ok, now read new chunk
            int newlen = length + blocklen;
            byte[] newdata = new byte[newlen];
            if (length > 0) System.arraycopy(data, 0, newdata, 0, length);
            dataIn.readFully(newdata, length, blocklen);
            data = newdata;
            length = newlen;
            // And now the CRLF after the chunk;
            while (dataIn.readByte() != '\n') ;
          }
          msg.setHeader("Content-Length", new Integer(length).toString());
        } else {
          if (outStream != null)
            HTTPUtil.sendHTTPResponse(outStream, HttpURLConnection.HTTP_LENGTH_REQUIRED, false);
          throw new IOException("Transfer-Encoding unimplemented: " + transfer_encoding);
        }
      } else if (msg.getHeader("Content-Length") == null) {
        if (outStream != null)
          HTTPUtil.sendHTTPResponse(outStream, HttpURLConnection.HTTP_LENGTH_REQUIRED, false);
        throw new IOException("Content-Length missing");
      }
    } else {
      // Receive the transmission's data
      int contentSize = Integer.parseInt(msg.getHeader("Content-Length"));
      data = new byte[contentSize];
      dataIn.readFully(data);
    }
    return data;
  }