PcapDotNet/Pcap.Net

HTTP Response Headers are NULL but for Requests its almost OK

Closed this issue · 1 comments

`static void Main(string[] args)
{
// Retrieve the device list from the local machine
IList allDevices = LivePacketDevice.AllLocalMachine;

        if (allDevices.Count == 0)
        {
            Console.WriteLine("No interfaces found! Make sure WinPcap is installed.");
            return;
        }

        // Print the list
        for (int i = 0; i != allDevices.Count; ++i)
        {
            LivePacketDevice device = allDevices[i];
            Console.Write((i + 1) + ". " + device.Name);
            if (device.Description != null)
                Console.WriteLine(" (" + device.Description + ")");
            else
                Console.WriteLine(" (No description available)");
        }

        int deviceIndex = 0;
        do
        {
            Console.WriteLine("Enter the interface number (1-" + allDevices.Count + "):");
            string deviceIndexString = Console.ReadLine();
            if (!int.TryParse(deviceIndexString, out deviceIndex) ||
                deviceIndex < 1 || deviceIndex > allDevices.Count)
            {
                deviceIndex = 0;
            }
        } while (deviceIndex == 0);

        // Take the selected adapter
        PacketDevice selectedDevice = allDevices[deviceIndex - 1];

        // Open the device
        using (PacketCommunicator communicator =
            selectedDevice.Open(65536,                                  // portion of the packet to capture
                                                                        // 65536 guarantees that the whole packet will be captured on all the link layers
                                PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                1000))                                  // read timeout
        {
            // Check the link layer. We support only Ethernet for simplicity.
            if (communicator.DataLink.Kind != DataLinkKind.Ethernet)
            {
                Console.WriteLine("This program works only on Ethernet networks.");
                return;
            }

            // Compile the filter
            using (BerkeleyPacketFilter filter = communicator.CreateFilter("ip and tcp"))
            {
                // Set the filter
                communicator.SetFilter(filter);
            }

            Console.WriteLine("Listening on " + selectedDevice.Description + "...");

            // start the capture
            communicator.ReceivePackets(0, PacketHandler);
        }
    }

    // Callback function invoked by libpcap for every incoming packet
    private static void PacketHandler(Packet packet)
    {
        // print timestamp and length of the packet
        //Console.WriteLine(packet.Timestamp.ToString("yyyy-MM-dd hh:mm:ss.fff") + " length:" + packet.Length);

        IpV4Datagram ip = packet.Ethernet.IpV4;
        // UdpDatagram udp = ip.Udp;
        TcpDatagram tcp = ip.Tcp;

        if (tcp == null)
        {
            return;
        }

        // print ip addresses and udp ports
        //Console.WriteLine(ip.Source + ":" + tcp.SourcePort + " -> " + ip.Destination + ":" + tcp.DestinationPort);

        if (tcp.DestinationPort == 80)
        {
            HttpDatagram http = tcp.Http;

            if (http.IsValid && http.IsRequest)
            {
                if(http.Header != null)
                    Console.WriteLine(http.Header.ToString());
            }
        }
    }`

OK I found my BUG.
CHANGED if (tcp.DestinationPort == 80)
TO if (tcp.DestinationPort == 80 || tcp.SourcePort == 80)