@Test public void testExtractHttpRequestResponseInfo() throws IOException{ Session session = Mockito.mock(Session.class); PacketInfo pi1 = Mockito.mock(PacketInfo.class); Mockito.when(pi1.getTimeStamp()).thenReturn(1000.0d); Mockito.when(pi1.getTcpFlagString()).thenReturn("F"); TCPPacket mTcpPacket = Mockito.mock(TCPPacket.class); PacketInfo pi2 = Mockito.mock(PacketInfo.class); Mockito.when(pi2.getPacket()).thenReturn(mTcpPacket); Mockito.when(pi2.getTcpFlagString()).thenReturn(""); Mockito.when(session.isUdpOnly()).thenReturn(false); TreeMap<Integer, PacketInfo> dlPacketOffsets = new TreeMap<Integer, PacketInfo>(); TreeMap<Integer, PacketInfo> ulPacketOffsets = new TreeMap<Integer, PacketInfo>(); dlPacketOffsets.put(14, pi1); ulPacketOffsets.put(14, pi2); List<PacketInfo> packets = new ArrayList<PacketInfo>(); packets.add(pi1); packets.add(pi2); String ulh = "\r\n" + "POST /com.nelsoft.areeba/Services/Login/RequestLoginUser HTTP/1.1" + "\r\n" + "Accept: application/json" + "\r\n" + "Content-type: text/plain" + "\r\n" + "Content-Length: 229" + "\r\n" + "Host: 24.16.97.108:8080" + "\r\n" + "Connection: Keep-Alive" + "\r\n" + "User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)" + "\r\n" + "\r\n" + "" + "\r\n" +"{\"accountID\":0,\"tokenID\":-5,\"userID\":-1,\"status\":\"\",\"serverHashKey\":\"\",\"deviceHash\":\"\",\"userPIN\":\"1234\",\"userName\":\"Barry\",\"deviceIMEI\":\"358239057521132 LGE Model Nexus 5 Version 4.4.2\",\"deviceIMSI\":\" \",\"versionNumber\":\"1.0.0.0\"}" ; Mockito.when(session.getStorageUl()).thenReturn(ulh.getBytes()); Mockito.when(session.getStorageDl()).thenReturn( ("HTTP/1.1 200 OK" + "\r\n" + "Server: Apache-Coyote/1.1" + "\r\n" + "Content-Length: 239" + "\r\n" + "Date: Thu, 22 Jan 2015 01:51:32 GMT" + "\r\n" + "" + "\r\n" +"{\"userName\":\"Barry\",\"userID\":12357,\"userPIN\":\"1234\",\"deviceIMSI\":\" \",\"deviceIMEI\":\"358239057521132 LGE Model Nexus 5 Version 4.4.2\",\"versionNumber\":\"1.0.0.0\",\"deviceHash\":\"\",\"status\":\"ok\",\"tokenID\":539,\"accountID\":4231,\"serverHashKey\":\"\"}" ).getBytes()); Mockito.when(session.getPacketOffsetsDl()).thenReturn(dlPacketOffsets); Mockito.when(session.getPacketOffsetsUl()).thenReturn(ulPacketOffsets); Mockito.when(session.getRemoteHostName()).thenReturn("remoteHostName"); Mockito.when(session.getDnsRequestPacket()).thenReturn(pi1); Mockito.when(session.getDnsResponsePacket()).thenReturn(pi2); Mockito.when(session.getPackets()).thenReturn(packets); Mockito.when(session.getLastSslHandshakePacket()).thenReturn(pi1); rrBuilder.createRequestResponseInfo(session); rrBuilder.extractHttpRequestResponseInfo(PacketDirection.DOWNLINK); List<HttpRequestResponseInfo> bResult = rrBuilder.getResult(); assertTrue(bResult.size() == 4); assertTrue(bResult.get(0).getAllHeaders().equals(" Accept: application/json Content-type: text/plain Content-Length: 229 Host: 24.16.97.108:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)")); Mockito.when(session.getStorageUl()).thenReturn( ("GET /cnn/dam/assets/141210153919-panono-tease-avatar.jpg HTTP/1.1\r\n" + "Host: i2.cdn.turner.com\r\n" + "Connection: keep-alive\r\n" + "Accept: image/webp,*/*;q=0.8\r\n" + "User-Agent: Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.59 Mobile Safari/537.36\r\n" + "Referer: http: + "Accept-Encoding: gzip, deflate, sdch\r\n" + "Accept-Language: en-US,en;q=0.8\r\n" + "\r\n").getBytes()); Mockito.when(session.getStorageDl()).thenReturn( ("HTTP/1.1 200 OK\r\n" + "Server: Apache-Coyote/1.1\r\n" + "Content-Type: image/jpeg\r\n" + "Content-Length: 8443\r\n" + "Cache-Control: max-age=5014\r\n" + "Expires: Thu, 11 Dec 2014 02:20:13 GMT\r\n" + "Date: Thu, 11 Dec 2014 00:56:39 GMT\r\n" + "Connection: keep-alive\r\n").getBytes()); rrBuilder.extractHttpRequestResponseInfo(PacketDirection.DOWNLINK); List<HttpRequestResponseInfo> result = rrBuilder.getResult(); assertTrue(result.size() == 4); assertTrue(result.get(0).getAllHeaders().equals(" Accept: application/json Content-type: text/plain Content-Length: 229 Host: 24.16.97.108:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)")); rrBuilder.extractHttpRequestResponseInfo(PacketDirection.UPLINK); result = rrBuilder.getResult(); assertTrue(result.size() == 5); assertTrue(result.get(0).getAllHeaders().equals(" Accept: application/json Content-type: text/plain Content-Length: 229 Host: 24.16.97.108:8080 Connection: Keep-Alive User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)")); }
public void extractHttpRequestResponseInfo(PacketDirection direction) throws IOException { SortedMap<Integer, PacketInfo> packetOffsets; switch (direction) { case DOWNLINK: storageReader.init(session.getStorageDl()); packetOffsets = session.getPacketOffsetsDl(); break; case UPLINK: storageReader.init(session.getStorageUl()); packetOffsets = session.getPacketOffsetsUl(); break; default: throw new IllegalArgumentException("Direction argument invalid"); } HttpRequestResponseInfo rrInfo = findNextRequestResponse(direction, packetOffsets); String line; while ((line = storageReader.readLine()) != null && rrInfo != null) { if (line.length() == 0) { if (rrInfo.getContentLength() > 0) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); rrInfo.getContentOffsetLength().put(storageReader.getIndex(), rrInfo.getContentLength()); storageReader.skipContent(rrInfo.getContentLength()); } else if (rrInfo.isChunked()) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); 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) { LOGGER.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { rrInfo.getContentOffsetLength().put(storageReader.getIndex(), size); rrInfo.setContentLength(rrInfo.getContentLength() + size); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunk: " + line); } } else { rrInfo.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } } mapPackets(packetOffsets, rrInfo.getRrStart(), storageReader.getIndex() - 1, direction, rrInfo); rrInfo.setRawSize(storageReader.getIndex() - rrInfo.getRrStart()); if (rrInfo.getObjUri() != null && !rrInfo.getObjUri().isAbsolute()) { try { int port = Integer.valueOf(rrInfo.getPort()).equals(wellKnownPorts.get(rrInfo.getScheme())) ? -1 : rrInfo.getPort(); rrInfo.setObjUri(new URI(rrInfo.getScheme().toLowerCase(), null, rrInfo.getHostName(), port, rrInfo.getObjUri().getPath(), rrInfo.getObjUri().getQuery(), rrInfo.getObjUri().getFragment())); } catch (URISyntaxException e) { LOGGER.info("Unexpected exception creating URI for request: " + e.getMessage() + ". Scheme=" + rrInfo.getScheme().toLowerCase() + ",Host name=" + rrInfo.getHostName() + ",Path=" + rrInfo.getObjUri().getPath() + ",Fragment=" + rrInfo.getObjUri().getFragment()); } } result.add(rrInfo); if (rrInfo.getDirection() == null) { LOGGER.warn("Request/response object has unknown direction"); } rrInfo = findNextRequestResponse(direction, packetOffsets); } else { parseHeaderLine.parseHeaderLine(line, rrInfo); } } }
RequestResponseBuilderImpl implements IRequestResponseBuilder { public void extractHttpRequestResponseInfo(PacketDirection direction) throws IOException { SortedMap<Integer, PacketInfo> packetOffsets; switch (direction) { case DOWNLINK: storageReader.init(session.getStorageDl()); packetOffsets = session.getPacketOffsetsDl(); break; case UPLINK: storageReader.init(session.getStorageUl()); packetOffsets = session.getPacketOffsetsUl(); break; default: throw new IllegalArgumentException("Direction argument invalid"); } HttpRequestResponseInfo rrInfo = findNextRequestResponse(direction, packetOffsets); String line; while ((line = storageReader.readLine()) != null && rrInfo != null) { if (line.length() == 0) { if (rrInfo.getContentLength() > 0) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); rrInfo.getContentOffsetLength().put(storageReader.getIndex(), rrInfo.getContentLength()); storageReader.skipContent(rrInfo.getContentLength()); } else if (rrInfo.isChunked()) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); 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) { LOGGER.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { rrInfo.getContentOffsetLength().put(storageReader.getIndex(), size); rrInfo.setContentLength(rrInfo.getContentLength() + size); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunk: " + line); } } else { rrInfo.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } } mapPackets(packetOffsets, rrInfo.getRrStart(), storageReader.getIndex() - 1, direction, rrInfo); rrInfo.setRawSize(storageReader.getIndex() - rrInfo.getRrStart()); if (rrInfo.getObjUri() != null && !rrInfo.getObjUri().isAbsolute()) { try { int port = Integer.valueOf(rrInfo.getPort()).equals(wellKnownPorts.get(rrInfo.getScheme())) ? -1 : rrInfo.getPort(); rrInfo.setObjUri(new URI(rrInfo.getScheme().toLowerCase(), null, rrInfo.getHostName(), port, rrInfo.getObjUri().getPath(), rrInfo.getObjUri().getQuery(), rrInfo.getObjUri().getFragment())); } catch (URISyntaxException e) { LOGGER.info("Unexpected exception creating URI for request: " + e.getMessage() + ". Scheme=" + rrInfo.getScheme().toLowerCase() + ",Host name=" + rrInfo.getHostName() + ",Path=" + rrInfo.getObjUri().getPath() + ",Fragment=" + rrInfo.getObjUri().getFragment()); } } result.add(rrInfo); if (rrInfo.getDirection() == null) { LOGGER.warn("Request/response object has unknown direction"); } rrInfo = findNextRequestResponse(direction, packetOffsets); } else { parseHeaderLine.parseHeaderLine(line, rrInfo); } } } }
RequestResponseBuilderImpl implements IRequestResponseBuilder { public void extractHttpRequestResponseInfo(PacketDirection direction) throws IOException { SortedMap<Integer, PacketInfo> packetOffsets; switch (direction) { case DOWNLINK: storageReader.init(session.getStorageDl()); packetOffsets = session.getPacketOffsetsDl(); break; case UPLINK: storageReader.init(session.getStorageUl()); packetOffsets = session.getPacketOffsetsUl(); break; default: throw new IllegalArgumentException("Direction argument invalid"); } HttpRequestResponseInfo rrInfo = findNextRequestResponse(direction, packetOffsets); String line; while ((line = storageReader.readLine()) != null && rrInfo != null) { if (line.length() == 0) { if (rrInfo.getContentLength() > 0) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); rrInfo.getContentOffsetLength().put(storageReader.getIndex(), rrInfo.getContentLength()); storageReader.skipContent(rrInfo.getContentLength()); } else if (rrInfo.isChunked()) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); 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) { LOGGER.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { rrInfo.getContentOffsetLength().put(storageReader.getIndex(), size); rrInfo.setContentLength(rrInfo.getContentLength() + size); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunk: " + line); } } else { rrInfo.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } } mapPackets(packetOffsets, rrInfo.getRrStart(), storageReader.getIndex() - 1, direction, rrInfo); rrInfo.setRawSize(storageReader.getIndex() - rrInfo.getRrStart()); if (rrInfo.getObjUri() != null && !rrInfo.getObjUri().isAbsolute()) { try { int port = Integer.valueOf(rrInfo.getPort()).equals(wellKnownPorts.get(rrInfo.getScheme())) ? -1 : rrInfo.getPort(); rrInfo.setObjUri(new URI(rrInfo.getScheme().toLowerCase(), null, rrInfo.getHostName(), port, rrInfo.getObjUri().getPath(), rrInfo.getObjUri().getQuery(), rrInfo.getObjUri().getFragment())); } catch (URISyntaxException e) { LOGGER.info("Unexpected exception creating URI for request: " + e.getMessage() + ". Scheme=" + rrInfo.getScheme().toLowerCase() + ",Host name=" + rrInfo.getHostName() + ",Path=" + rrInfo.getObjUri().getPath() + ",Fragment=" + rrInfo.getObjUri().getFragment()); } } result.add(rrInfo); if (rrInfo.getDirection() == null) { LOGGER.warn("Request/response object has unknown direction"); } rrInfo = findNextRequestResponse(direction, packetOffsets); } else { parseHeaderLine.parseHeaderLine(line, rrInfo); } } } RequestResponseBuilderImpl(); }
RequestResponseBuilderImpl implements IRequestResponseBuilder { public void extractHttpRequestResponseInfo(PacketDirection direction) throws IOException { SortedMap<Integer, PacketInfo> packetOffsets; switch (direction) { case DOWNLINK: storageReader.init(session.getStorageDl()); packetOffsets = session.getPacketOffsetsDl(); break; case UPLINK: storageReader.init(session.getStorageUl()); packetOffsets = session.getPacketOffsetsUl(); break; default: throw new IllegalArgumentException("Direction argument invalid"); } HttpRequestResponseInfo rrInfo = findNextRequestResponse(direction, packetOffsets); String line; while ((line = storageReader.readLine()) != null && rrInfo != null) { if (line.length() == 0) { if (rrInfo.getContentLength() > 0) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); rrInfo.getContentOffsetLength().put(storageReader.getIndex(), rrInfo.getContentLength()); storageReader.skipContent(rrInfo.getContentLength()); } else if (rrInfo.isChunked()) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); 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) { LOGGER.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { rrInfo.getContentOffsetLength().put(storageReader.getIndex(), size); rrInfo.setContentLength(rrInfo.getContentLength() + size); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunk: " + line); } } else { rrInfo.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } } mapPackets(packetOffsets, rrInfo.getRrStart(), storageReader.getIndex() - 1, direction, rrInfo); rrInfo.setRawSize(storageReader.getIndex() - rrInfo.getRrStart()); if (rrInfo.getObjUri() != null && !rrInfo.getObjUri().isAbsolute()) { try { int port = Integer.valueOf(rrInfo.getPort()).equals(wellKnownPorts.get(rrInfo.getScheme())) ? -1 : rrInfo.getPort(); rrInfo.setObjUri(new URI(rrInfo.getScheme().toLowerCase(), null, rrInfo.getHostName(), port, rrInfo.getObjUri().getPath(), rrInfo.getObjUri().getQuery(), rrInfo.getObjUri().getFragment())); } catch (URISyntaxException e) { LOGGER.info("Unexpected exception creating URI for request: " + e.getMessage() + ". Scheme=" + rrInfo.getScheme().toLowerCase() + ",Host name=" + rrInfo.getHostName() + ",Path=" + rrInfo.getObjUri().getPath() + ",Fragment=" + rrInfo.getObjUri().getFragment()); } } result.add(rrInfo); if (rrInfo.getDirection() == null) { LOGGER.warn("Request/response object has unknown direction"); } rrInfo = findNextRequestResponse(direction, packetOffsets); } else { parseHeaderLine.parseHeaderLine(line, rrInfo); } } } RequestResponseBuilderImpl(); @Autowired void setByteArrayLineReader(IByteArrayLineReader reader); List<HttpRequestResponseInfo> createRequestResponseInfo(Session session); List<HttpRequestResponseInfo> getResult(); void extractHttpRequestResponseInfo(PacketDirection direction); }
RequestResponseBuilderImpl implements IRequestResponseBuilder { public void extractHttpRequestResponseInfo(PacketDirection direction) throws IOException { SortedMap<Integer, PacketInfo> packetOffsets; switch (direction) { case DOWNLINK: storageReader.init(session.getStorageDl()); packetOffsets = session.getPacketOffsetsDl(); break; case UPLINK: storageReader.init(session.getStorageUl()); packetOffsets = session.getPacketOffsetsUl(); break; default: throw new IllegalArgumentException("Direction argument invalid"); } HttpRequestResponseInfo rrInfo = findNextRequestResponse(direction, packetOffsets); String line; while ((line = storageReader.readLine()) != null && rrInfo != null) { if (line.length() == 0) { if (rrInfo.getContentLength() > 0) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); rrInfo.getContentOffsetLength().put(storageReader.getIndex(), rrInfo.getContentLength()); storageReader.skipContent(rrInfo.getContentLength()); } else if (rrInfo.isChunked()) { rrInfo.setContentOffsetLength(new TreeMap<Integer, Integer>()); 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) { LOGGER.warn("Unexpected begin of the chunk format : " + line); } if (size > 0) { rrInfo.getContentOffsetLength().put(storageReader.getIndex(), size); rrInfo.setContentLength(rrInfo.getContentLength() + size); storageReader.skipForward(size); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunk: " + line); } } else { rrInfo.setChunkModeFinished(true); line = storageReader.readLine(); if (line != null && line.length() > 0) { LOGGER.warn("Unexpected end of chunked data: " + line); } break; } } else { break; } } } mapPackets(packetOffsets, rrInfo.getRrStart(), storageReader.getIndex() - 1, direction, rrInfo); rrInfo.setRawSize(storageReader.getIndex() - rrInfo.getRrStart()); if (rrInfo.getObjUri() != null && !rrInfo.getObjUri().isAbsolute()) { try { int port = Integer.valueOf(rrInfo.getPort()).equals(wellKnownPorts.get(rrInfo.getScheme())) ? -1 : rrInfo.getPort(); rrInfo.setObjUri(new URI(rrInfo.getScheme().toLowerCase(), null, rrInfo.getHostName(), port, rrInfo.getObjUri().getPath(), rrInfo.getObjUri().getQuery(), rrInfo.getObjUri().getFragment())); } catch (URISyntaxException e) { LOGGER.info("Unexpected exception creating URI for request: " + e.getMessage() + ". Scheme=" + rrInfo.getScheme().toLowerCase() + ",Host name=" + rrInfo.getHostName() + ",Path=" + rrInfo.getObjUri().getPath() + ",Fragment=" + rrInfo.getObjUri().getFragment()); } } result.add(rrInfo); if (rrInfo.getDirection() == null) { LOGGER.warn("Request/response object has unknown direction"); } rrInfo = findNextRequestResponse(direction, packetOffsets); } else { parseHeaderLine.parseHeaderLine(line, rrInfo); } } } RequestResponseBuilderImpl(); @Autowired void setByteArrayLineReader(IByteArrayLineReader reader); List<HttpRequestResponseInfo> createRequestResponseInfo(Session session); List<HttpRequestResponseInfo> getResult(); void extractHttpRequestResponseInfo(PacketDirection direction);  }