private boolean post( HTTPResponse httpRes, InputStream in, long contentOffset, long contentLength, boolean isOnlyHeader) { httpRes.setDate(Calendar.getInstance()); OutputStream out = getOutputStream(); try { httpRes.setContentLength(contentLength); out.write(httpRes.getHeader().getBytes()); out.write(HTTP.CRLF.getBytes()); if (isOnlyHeader == true) { out.flush(); return true; } boolean isChunkedResponse = httpRes.isChunked(); if (0 < contentOffset) in.skip(contentOffset); int chunkSize = HTTP.getChunkSize(); byte readBuf[] = new byte[chunkSize]; long readCnt = 0; long readSize = (chunkSize < contentLength) ? chunkSize : contentLength; int readLen = in.read(readBuf, 0, (int) readSize); while (0 < readLen && readCnt < contentLength) { if (isChunkedResponse == true) { String chunSizeBuf = Long.toString(readLen); out.write(chunSizeBuf.getBytes()); out.write(HTTP.CRLF.getBytes()); } out.write(readBuf, 0, readLen); if (isChunkedResponse == true) out.write(HTTP.CRLF.getBytes()); readCnt += readLen; readSize = (chunkSize < (contentLength - readCnt)) ? chunkSize : (contentLength - readCnt); readLen = in.read(readBuf, 0, (int) readSize); } if (isChunkedResponse == true) { out.write("0".getBytes()); out.write(HTTP.CRLF.getBytes()); } out.flush(); } catch (Exception e) { // Debug.warning(e); return false; } return true; }
private boolean post( HTTPResponse httpRes, byte content[], long contentOffset, long contentLength, boolean isOnlyHeader) { httpRes.setDate(Calendar.getInstance()); OutputStream out = getOutputStream(); try { httpRes.setContentLength(contentLength); out.write(httpRes.getHeader().getBytes()); out.write(HTTP.CRLF.getBytes()); if (isOnlyHeader == true) { out.flush(); return true; } boolean isChunkedResponse = httpRes.isChunked(); if (isChunkedResponse == true) { String chunSizeBuf = Long.toString(contentLength); out.write(chunSizeBuf.getBytes()); out.write(HTTP.CRLF.getBytes()); } out.write(content, (int) contentOffset, (int) contentLength); if (isChunkedResponse == true) { out.write(HTTP.CRLF.getBytes()); out.write("0".getBytes()); out.write(HTTP.CRLF.getBytes()); } out.flush(); } catch (Exception e) { // Debug.warning(e); return false; } return true; }
protected boolean set(InputStream in, boolean onlyHeaders) { try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); // try{ String firstLine = reader.readLine(); /* * }catch(SocketException se){ System.out.println("[HTTP ERROR START]"); System.out.println(this); * System.out.println("[HTTP ERROR END]"); throw se; } */ if (firstLine == null || firstLine.length() <= 0) return false; setFirstLine(firstLine); // Thanks for Giordano Sassaroli <*****@*****.**> (09/03/03) HTTPStatus httpStatus = new HTTPStatus(firstLine); int statCode = httpStatus.getStatusCode(); if (statCode == HTTPStatus.CONTINUE) { // ad hoc code for managing iis non-standard behaviour // iis sends 100 code response and a 200 code response in the same // stream, so the code should check the presence of the actual // response in the stream. // skip all header lines String headerLine = reader.readLine(); while ((headerLine != null) && (0 < headerLine.length())) { HTTPHeader header = new HTTPHeader(headerLine); if (header.hasName() == true) setHeader(header); headerLine = reader.readLine(); } // look forward another first line String actualFirstLine = reader.readLine(); if ((actualFirstLine != null) && (0 < actualFirstLine.length())) { // this is the actual first line setFirstLine(actualFirstLine); } else { return true; } } String headerLine = reader.readLine(); while ((headerLine != null) && (0 < headerLine.length())) { HTTPHeader header = new HTTPHeader(headerLine); if (header.hasName() == true) setHeader(header); headerLine = reader.readLine(); } if (onlyHeaders == true) { setContent("", false); return true; } boolean isChunkedRequest = isChunked(); long contentLen = 0; if (isChunkedRequest == true) { try { String chunkSizeLine = reader.readLine(); contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length() - 2)); } catch (Exception e) { } ; } else { if (getHeader(HTTP.CONTENT_LENGTH) != null) { contentLen = getContentLength(); } else if ((getHeader(HTTP.CONNECTION) != null) && (getHeader(HTTP.CONNECTION).getValue().toLowerCase().indexOf("keep-alive") != -1) && (getHeader(HTTP.CONTENT_LENGTH) == null)) { contentLen = 0; } else { StringBuffer sb = new StringBuffer(""); int ch; while ((ch = reader.read()) != -1) { sb.append((char) ch); } setContent(sb.toString()); return true; } } StringBuffer contentBuf = new StringBuffer(); while (0 < contentLen) { int chunkSize = HTTP.getChunkSize(); char readBuf[] = new char[chunkSize]; long readCnt = 0; while (readCnt < contentLen) { try { // Thanks for Mark Retallack (02/02/05) long bufReadLen = contentLen - readCnt; if (chunkSize < bufReadLen) bufReadLen = chunkSize; int readLen = reader.read(readBuf, 0, (int) bufReadLen); if (readLen < 0) break; contentBuf.append(new String(readBuf, 0, readLen)); readCnt += readLen; } catch (Exception e) { Debug.warning(e); break; } } if (isChunkedRequest == true) { // skip CRLF long skipLen = 0; do { long skipCnt = reader.skip(HTTP.CRLF.length() - skipLen); if (skipCnt < 0) break; skipLen += skipCnt; } while (skipLen < HTTP.CRLF.length()); // read next chunk size try { String chunkSizeLine = reader.readLine(); contentLen = Long.parseLong(new String(chunkSizeLine.getBytes(), 0, chunkSizeLine.length() - 2)); } catch (Exception e) { contentLen = 0; } ; } else contentLen = 0; } // Thanks for Ralf G. R. Bergs (02/09/04) String contentStr = contentBuf.toString(); setContent(contentStr.getBytes(), false); } catch (Exception e) { Debug.warning(e); return false; } return true; }