@Test public void getContent() { Session session = null; session = mock(Session.class); HttpRequestResponseInfo req = null; req = mock(HttpRequestResponseInfo.class); String stringData = "this was compressed"; byte[] data = stringData.getBytes(); byte[] gzipped_data = null; try { ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(data.length / 2); GZIPOutputStream gzipOutput = new GZIPOutputStream(byteOutput); gzipOutput.write(data); gzipOutput.close(); gzipped_data = byteOutput.toByteArray(); } catch (IOException e1) { e1.printStackTrace(); } SortedMap<Integer, Integer> contentOffsetTreeMap = new TreeMap<Integer, Integer>(); contentOffsetTreeMap.put(0, gzipped_data == null ? 0 : gzipped_data.length); Mockito.when(req.getContentEncoding()).thenReturn("gzip"); Mockito.when(req.getContentOffsetLength()).thenReturn(contentOffsetTreeMap); Mockito.when(req.getPacketDirection()).thenReturn(PacketDirection.DOWNLINK); Mockito.when(session.getStorageDl()).thenReturn(gzipped_data); Mockito.when(session.getStorageUl()).thenReturn(gzipped_data); try { assertEquals(stringData, httpRequestResponseHelper.getContentString(req, session)); Mockito.when(session.getStorageDl()).thenReturn(data); } catch (Exception e) { assertEquals(null, e.getMessage()); } assertNotNull(gzipped_data); gzipped_data[20] = 42; Mockito.when(session.getStorageDl()).thenReturn(gzipped_data); Mockito.when(session.getStorageUl()).thenReturn(gzipped_data); try { String thisWasFromCorruptedZip = httpRequestResponseHelper.getContentString(req, session); assertEquals("this was bwmpressed", thisWasFromCorruptedZip); } catch (Exception e) { } }
public byte[] getContent(HttpRequestResponseInfo request, Session session) throws Exception { LOG.debug("getContent(Req, Session) :" + request.toString()); String contentEncoding = request.getContentEncoding(); byte[] payload; ByteArrayOutputStream output; boolean logFlag = false; if(request.getAssocReqResp()==null) { logFlag = true; } payload = request.getPayloadData().toByteArray(); if (request.isChunked()) { storageReader.init(payload); String line; output = new ByteArrayOutputStream(); while (true) { line = storageReader.readLine(); if (line != null) { String[] str = line.split(";"); int size = 0; try { String trim = str[0].trim(); size = Integer.parseInt(trim, 16); } catch (NumberFormatException e) { LOG.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { output.write( Arrays.copyOfRange(payload, storageReader.getIndex(), storageReader.getIndex() + size)); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunk: " + line); } } else { request.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } if (request.isChunkModeFinished()) { payload = output.toByteArray(); } else { throw new Exception(String.format("Unexpected Chunk End at request time: %.3f, request.getAssocReqResp() is %s The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams())); } } else if (payload.length < request.getContentLength()) { request.setExtractable(false); int percentage = payload.length / request.getContentLength() * 100; throw new Exception(String.format("PayloadException At request time: %.3f, request.getAssocReqResp() is %s . The content may be corrupted. Buffer exceeded: only %d percent arrived", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams(), percentage)); } if ("gzip".equals(contentEncoding) && payload != null) { GZIPInputStream gzip = null; output = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(payload)); try { byte[] buffer = new byte[2048]; int len; while ((len = gzip.read(buffer)) >= 0) { output.write(buffer, 0, len); } gzip.close(); } catch (IOException ioe) { LOG.error("Error Extracting Content from Request"); throw new Exception(String.format("Zip Extract Exception At request time: %.3f, request.getAssocReqResp() is %s. The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp())); } } else { return payload; } if (output.size() > 0) { return output.toByteArray(); } else { request.setExtractable(false); return new byte[0]; } }
HttpRequestResponseHelperImpl implements IHttpRequestResponseHelper { public byte[] getContent(HttpRequestResponseInfo request, Session session) throws Exception { LOG.debug("getContent(Req, Session) :" + request.toString()); String contentEncoding = request.getContentEncoding(); byte[] payload; ByteArrayOutputStream output; boolean logFlag = false; if(request.getAssocReqResp()==null) { logFlag = true; } payload = request.getPayloadData().toByteArray(); if (request.isChunked()) { storageReader.init(payload); String line; output = new ByteArrayOutputStream(); while (true) { line = storageReader.readLine(); if (line != null) { String[] str = line.split(";"); int size = 0; try { String trim = str[0].trim(); size = Integer.parseInt(trim, 16); } catch (NumberFormatException e) { LOG.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { output.write( Arrays.copyOfRange(payload, storageReader.getIndex(), storageReader.getIndex() + size)); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunk: " + line); } } else { request.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } if (request.isChunkModeFinished()) { payload = output.toByteArray(); } else { throw new Exception(String.format("Unexpected Chunk End at request time: %.3f, request.getAssocReqResp() is %s The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams())); } } else if (payload.length < request.getContentLength()) { request.setExtractable(false); int percentage = payload.length / request.getContentLength() * 100; throw new Exception(String.format("PayloadException At request time: %.3f, request.getAssocReqResp() is %s . The content may be corrupted. Buffer exceeded: only %d percent arrived", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams(), percentage)); } if ("gzip".equals(contentEncoding) && payload != null) { GZIPInputStream gzip = null; output = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(payload)); try { byte[] buffer = new byte[2048]; int len; while ((len = gzip.read(buffer)) >= 0) { output.write(buffer, 0, len); } gzip.close(); } catch (IOException ioe) { LOG.error("Error Extracting Content from Request"); throw new Exception(String.format("Zip Extract Exception At request time: %.3f, request.getAssocReqResp() is %s. The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp())); } } else { return payload; } if (output.size() > 0) { return output.toByteArray(); } else { request.setExtractable(false); return new byte[0]; } } }
HttpRequestResponseHelperImpl implements IHttpRequestResponseHelper { public byte[] getContent(HttpRequestResponseInfo request, Session session) throws Exception { LOG.debug("getContent(Req, Session) :" + request.toString()); String contentEncoding = request.getContentEncoding(); byte[] payload; ByteArrayOutputStream output; boolean logFlag = false; if(request.getAssocReqResp()==null) { logFlag = true; } payload = request.getPayloadData().toByteArray(); if (request.isChunked()) { storageReader.init(payload); String line; output = new ByteArrayOutputStream(); while (true) { line = storageReader.readLine(); if (line != null) { String[] str = line.split(";"); int size = 0; try { String trim = str[0].trim(); size = Integer.parseInt(trim, 16); } catch (NumberFormatException e) { LOG.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { output.write( Arrays.copyOfRange(payload, storageReader.getIndex(), storageReader.getIndex() + size)); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunk: " + line); } } else { request.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } if (request.isChunkModeFinished()) { payload = output.toByteArray(); } else { throw new Exception(String.format("Unexpected Chunk End at request time: %.3f, request.getAssocReqResp() is %s The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams())); } } else if (payload.length < request.getContentLength()) { request.setExtractable(false); int percentage = payload.length / request.getContentLength() * 100; throw new Exception(String.format("PayloadException At request time: %.3f, request.getAssocReqResp() is %s . The content may be corrupted. Buffer exceeded: only %d percent arrived", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams(), percentage)); } if ("gzip".equals(contentEncoding) && payload != null) { GZIPInputStream gzip = null; output = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(payload)); try { byte[] buffer = new byte[2048]; int len; while ((len = gzip.read(buffer)) >= 0) { output.write(buffer, 0, len); } gzip.close(); } catch (IOException ioe) { LOG.error("Error Extracting Content from Request"); throw new Exception(String.format("Zip Extract Exception At request time: %.3f, request.getAssocReqResp() is %s. The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp())); } } else { return payload; } if (output.size() > 0) { return output.toByteArray(); } else { request.setExtractable(false); return new byte[0]; } }  }
HttpRequestResponseHelperImpl implements IHttpRequestResponseHelper { public byte[] getContent(HttpRequestResponseInfo request, Session session) throws Exception { LOG.debug("getContent(Req, Session) :" + request.toString()); String contentEncoding = request.getContentEncoding(); byte[] payload; ByteArrayOutputStream output; boolean logFlag = false; if(request.getAssocReqResp()==null) { logFlag = true; } payload = request.getPayloadData().toByteArray(); if (request.isChunked()) { storageReader.init(payload); String line; output = new ByteArrayOutputStream(); while (true) { line = storageReader.readLine(); if (line != null) { String[] str = line.split(";"); int size = 0; try { String trim = str[0].trim(); size = Integer.parseInt(trim, 16); } catch (NumberFormatException e) { LOG.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { output.write( Arrays.copyOfRange(payload, storageReader.getIndex(), storageReader.getIndex() + size)); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunk: " + line); } } else { request.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } if (request.isChunkModeFinished()) { payload = output.toByteArray(); } else { throw new Exception(String.format("Unexpected Chunk End at request time: %.3f, request.getAssocReqResp() is %s The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams())); } } else if (payload.length < request.getContentLength()) { request.setExtractable(false); int percentage = payload.length / request.getContentLength() * 100; throw new Exception(String.format("PayloadException At request time: %.3f, request.getAssocReqResp() is %s . The content may be corrupted. Buffer exceeded: only %d percent arrived", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams(), percentage)); } if ("gzip".equals(contentEncoding) && payload != null) { GZIPInputStream gzip = null; output = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(payload)); try { byte[] buffer = new byte[2048]; int len; while ((len = gzip.read(buffer)) >= 0) { output.write(buffer, 0, len); } gzip.close(); } catch (IOException ioe) { LOG.error("Error Extracting Content from Request"); throw new Exception(String.format("Zip Extract Exception At request time: %.3f, request.getAssocReqResp() is %s. The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp())); } } else { return payload; } if (output.size() > 0) { return output.toByteArray(); } else { request.setExtractable(false); return new byte[0]; } }  @Autowired void setByteArrayLineReader(IByteArrayLineReader reader); boolean isCss(String contentType); boolean isHtml(String contentType); boolean isJSON(String contentType); boolean isJavaScript(String contentType); String getContentString(HttpRequestResponseInfo req, Session session); byte[] getContent(HttpRequestResponseInfo request, Session session); boolean isSameContent(HttpRequestResponseInfo left, HttpRequestResponseInfo right, Session session, Session sessionRight); long getActualByteCount(HttpRequestResponseInfo item, Session session); }
HttpRequestResponseHelperImpl implements IHttpRequestResponseHelper { public byte[] getContent(HttpRequestResponseInfo request, Session session) throws Exception { LOG.debug("getContent(Req, Session) :" + request.toString()); String contentEncoding = request.getContentEncoding(); byte[] payload; ByteArrayOutputStream output; boolean logFlag = false; if(request.getAssocReqResp()==null) { logFlag = true; } payload = request.getPayloadData().toByteArray(); if (request.isChunked()) { storageReader.init(payload); String line; output = new ByteArrayOutputStream(); while (true) { line = storageReader.readLine(); if (line != null) { String[] str = line.split(";"); int size = 0; try { String trim = str[0].trim(); size = Integer.parseInt(trim, 16); } catch (NumberFormatException e) { LOG.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { output.write( Arrays.copyOfRange(payload, storageReader.getIndex(), storageReader.getIndex() + size)); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunk: " + line); } } else { request.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOG.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } if (request.isChunkModeFinished()) { payload = output.toByteArray(); } else { throw new Exception(String.format("Unexpected Chunk End at request time: %.3f, request.getAssocReqResp() is %s The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams())); } } else if (payload.length < request.getContentLength()) { request.setExtractable(false); int percentage = payload.length / request.getContentLength() * 100; throw new Exception(String.format("PayloadException At request time: %.3f, request.getAssocReqResp() is %s . The content may be corrupted. Buffer exceeded: only %d percent arrived", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp().getObjNameWithoutParams(), percentage)); } if ("gzip".equals(contentEncoding) && payload != null) { GZIPInputStream gzip = null; output = new ByteArrayOutputStream(); gzip = new GZIPInputStream(new ByteArrayInputStream(payload)); try { byte[] buffer = new byte[2048]; int len; while ((len = gzip.read(buffer)) >= 0) { output.write(buffer, 0, len); } gzip.close(); } catch (IOException ioe) { LOG.error("Error Extracting Content from Request"); throw new Exception(String.format("Zip Extract Exception At request time: %.3f, request.getAssocReqResp() is %s. The content may be corrupted.", request.getTimeStamp(), logFlag?"N/A":request.getAssocReqResp())); } } else { return payload; } if (output.size() > 0) { return output.toByteArray(); } else { request.setExtractable(false); return new byte[0]; } }  @Autowired void setByteArrayLineReader(IByteArrayLineReader reader); boolean isCss(String contentType); boolean isHtml(String contentType); boolean isJSON(String contentType); boolean isJavaScript(String contentType); String getContentString(HttpRequestResponseInfo req, Session session); byte[] getContent(HttpRequestResponseInfo request, Session session); boolean isSameContent(HttpRequestResponseInfo left, HttpRequestResponseInfo right, Session session, Session sessionRight); long getActualByteCount(HttpRequestResponseInfo item, Session session);  }