示例#1
0
        public HTTPResponse(byte[] buffer, int bufLength)
        {
            respHdr    = new HTTPResponseHeader(buffer);
            respLength = bufLength;

            if (respHdr.hdrLength < respLength)
            {
                data = new byte[respLength - respHdr.hdrLength];
                Array.Copy(buffer, respHdr.hdrLength, data, 0, data.Length);
            }
            else
            {
                data = null;
            }
        }
示例#2
0
        public static Socket InitGetConnection(Socket withClient, HTTPRequest initReq, byte[] buffer)
        {
            Socket withServer = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            var    ipAddrs    = Dns.GetHostAddresses(initReq.reqHdr.httpHdrDict["Host"]);

            try
            {
                withServer.Connect(new IPEndPoint(ipAddrs[ipAddrs.Length - 1], 80));
                withServer.Send(initReq.GetRequestBytes());
            }
            catch (SocketException e)
            {
                try
                {
                    withServer.Connect(new IPEndPoint(ipAddrs[ipAddrs.Length - 1], 80));
                }
                catch (SocketException ee)
                {
                    withClient.Close();
                    return(null);
                }
            }

            Array.Clear(buffer, 0, buffer.Length);
            var cnt = withServer.Receive(buffer);

            //Check & make sure get the whole head in the buffer
            int  nowAtIndex = 0;
            bool flagCont   = true;

            while (true)
            {
                while (flagCont)
                {
                    nowAtIndex = Array.IndexOf <byte>(buffer, 0x0a, nowAtIndex + 1);
                    if (nowAtIndex == -1)
                    {
                        break;
                    }
                    if (nowAtIndex + 2 < buffer.Length)
                    {
                        if (buffer[nowAtIndex + 1] != 0x0d)
                        {
                            continue;
                        }
                    }                                                                                          // End of Header found
                    flagCont = false;
                }
                if (!flagCont)
                {
                    break;
                }
                if (cnt == buffer.Length)
                {
                    Array.Resize(ref buffer, 2 * buffer.Length);
                }
                if (withServer.Available != 0)
                {
                    cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                }
            }

            HTTPResponseHeader respHeader;

            if (cnt != 0)
            {
                respHeader = new HTTPResponseHeader(buffer);
            }
            else
            {
                return(null);
            }
            int contentLength = 0;

            switch (respHeader.statusCode)
            {
            case 304:
                break;

            case 200:
                try
                {
                    if (int.TryParse(respHeader.httpHdrDict["Content-Length"], out contentLength))
                    {
                        while (cnt < respHeader.hdrLength + contentLength)
                        {
                            if (cnt == buffer.Length)
                            {
                                Array.Resize(ref buffer, 2 * buffer.Length);
                            }
                            cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                        }
                    }
                }
                catch (KeyNotFoundException e)
                {
                    if (respHeader.httpHdrDict["Transfer-Encoding"] == "chunked")
                    {
                        int currChunkLength  = 0;
                        int headerStartIndex = respHeader.hdrLength;

                        while (true)
                        {
                            // Fetching the head and length of the chunk.
                            try
                            {
                                nowAtIndex = Array.IndexOf <byte>(buffer, 0x0a, headerStartIndex);      // Finding the end of the header.
                                Console.WriteLine(Thread.CurrentThread.Name + ", BufSize = " + buffer.Length +
                                                  ", nowHdrStartIndex = " + headerStartIndex);
                            }
                            catch (Exception eee)
                            {
                                Console.WriteLine(Thread.CurrentThread.Name);
                                Console.WriteLine(eee);
                                Console.WriteLine(headerStartIndex + ", " + buffer.Length);
                            }
                            if (nowAtIndex != -1)
                            {
                                currChunkLength = int.Parse(Encoding.UTF8.GetString(buffer, headerStartIndex,
                                                                                    nowAtIndex - headerStartIndex - 1), System.Globalization.NumberStyles.HexNumber);
                                if (currChunkLength == 0)
                                {
                                    break;
                                }                                               // The chunked data stream ends.
                                nowAtIndex++;
                                headerStartIndex = nowAtIndex;
                                // Now nowAtIndex at the beginning of the chunk.

                                while (cnt < nowAtIndex + currChunkLength)          // The current chunk is not completely buffered
                                {
                                    if (cnt == buffer.Length)
                                    {
                                        Array.Resize(ref buffer, 2 * buffer.Length);
                                    }
                                    cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                                }

                                headerStartIndex += (currChunkLength + 2);        // nowAtIndex now at the start of the next Header
                            }
                            else
                            {
                                if (cnt == buffer.Length)
                                {
                                    Array.Resize(ref buffer, 2 * buffer.Length);
                                }
                                cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                            }
                        }
                    }
                }
                break;
            }

            var response = new HTTPResponse(buffer, cnt);

            withClient.Send(response.GetResponseBytes());
            try
            {
                if (response.respHdr.httpHdrDict["Connection"] == "close")
                {
                    withServer.Close(10);
                    withClient.Close(10);
                    return(null);
                }
            }
            catch (KeyNotFoundException ke)
            {
                if (!(initReq.reqHdr.httpHdrDict["Proxy-Connection"] == "keep-alive"))
                {
                    withServer.Close(10);
                    withClient.Close(10);
                    return(null);
                }
            }



            //Array.Clear(buffer, 0, buffer.Length);
            //var cnt = withServer.Receive(buffer);

            ////Check & make sure get the whole head in the buffer
            //int nowAtIndex = 0;
            //bool flagCont = true;

            //while (true)
            //{
            //    while (flagCont)
            //    {
            //        nowAtIndex = Array.IndexOf<byte>(buffer, 0x0a, nowAtIndex + 1);
            //        if (nowAtIndex == -1) { break; }
            //        if (nowAtIndex + 2 < buffer.Length) { if (buffer[nowAtIndex + 1] != 0x0d) { continue; } }  // End of Header found
            //        flagCont = false;
            //    }
            //    if (!flagCont) { break; }
            //    if (cnt == buffer.Length) { Array.Resize(ref buffer, 2 * buffer.Length); }
            //    if (withServer.Available != 0) { cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0); }
            //}

            //HTTPResponseHeader respHeader;
            //if (cnt != 0) { respHeader = new HTTPResponseHeader(buffer); } else { return null; }
            //int contentLength = 0;

            //switch (respHeader.statusCode)
            //{
            //    case 304:
            //        break;
            //    case 200:
            //        try
            //        {
            //            if (int.TryParse(respHeader.httpHdrDict["Content-Length"], out contentLength))
            //            {
            //                while (cnt < respHeader.hdrLength + contentLength)
            //                {
            //                    if (cnt == buffer.Length)
            //                    {
            //                        Array.Resize(ref buffer, 2 * buffer.Length);
            //                    }
            //                    cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
            //                }
            //            }
            //        }
            //        catch (KeyNotFoundException e)
            //        {
            //            if (respHeader.httpHdrDict["Transfer-Encoding"] == "chunked")
            //            {
            //                int currChunkLength = 0;
            //                int headerStartIndex = respHeader.hdrLength;

            //                while (true)
            //                {
            //                    // Fetching the head and length of the chunk.
            //                    nowAtIndex = Array.IndexOf<byte>(buffer, 0x0a, headerStartIndex);   // Finding the end of the header.
            //                    if (nowAtIndex != -1)
            //                    {
            //                        currChunkLength = int.Parse(Encoding.UTF8.GetString(buffer, headerStartIndex,
            //                                            nowAtIndex - headerStartIndex - 1), System.Globalization.NumberStyles.HexNumber);
            //                        if (currChunkLength == 0) { break; }        // The chunked data stream ends.
            //                        nowAtIndex++;
            //                        headerStartIndex = nowAtIndex;
            //                        // Now nowAtIndex at the beginning of the chunk.
            //                    }

            //                    while (cnt < nowAtIndex + currChunkLength)      // The current chunk is not completely buffered
            //                    {
            //                        if (cnt == buffer.Length) { Array.Resize(ref buffer, 2 * buffer.Length); }
            //                        cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
            //                    }

            //                    headerStartIndex += (currChunkLength + 2);    // nowAtIndex now at the start of the next Header
            //                }
            //            }
            //        }
            //        break;
            //}

            //var response = new HTTPResponse(buffer, cnt);
            //withClient.Send(response.GetResponseBytes());
            //try
            //{
            //    if (response.respHdr.httpHdrDict["Connection"] == "close")
            //    {
            //        withServer.Close(10);
            //        withClient.Close(10);
            //        return null;
            //    }
            //}
            //catch (KeyNotFoundException ke)
            //{
            //    if (!(initReq.reqHdr.httpHdrDict["Proxy-Connection"] == "keep-alive"))
            //    {
            //        withServer.Close(10);
            //        withClient.Close(10);
            //        return null;
            //    }
            //}

            return(withServer);
        }
示例#3
0
        public static bool ProcGetRequest(Socket withClient, Socket withServer)
        {
            var buffer = new byte[bufferSize];
            int cnt    = 0;

            try
            {
                cnt = withClient.Receive(buffer);
            }
            catch (SocketException e)
            {
                Console.WriteLine(e);
                Console.WriteLine(e.SocketErrorCode);
                throw;
            }


            if (cnt == 0)
            {
                Console.WriteLine("Stopping " + Thread.CurrentThread.Name);
                return(false);
            }

            while (cnt == buffer.Length)
            {
                Array.Resize(ref buffer, 2 * buffer.Length);
                cnt = withClient.Receive(buffer, buffer.Length / 2, buffer.Length / 2, 0);
            }

            var req = new HTTPRequest(buffer, cnt);

            try
            {
                withServer.Send(req.GetRequestBytes());
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e);
                Console.WriteLine(Encoding.UTF8.GetString(buffer, 0, cnt));
            }

            Array.Clear(buffer, 0, buffer.Length);
            cnt = withServer.Receive(buffer);

            //Check & make sure get the whole head in the buffer
            int  nowAtIndex = 0;
            bool flagCont   = true;

            while (true)
            {
                while (flagCont)
                {
                    nowAtIndex = Array.IndexOf <byte>(buffer, 0x0a, nowAtIndex + 1);
                    if (nowAtIndex == -1)
                    {
                        break;
                    }
                    if (nowAtIndex + 2 < buffer.Length)
                    {
                        if (buffer[nowAtIndex + 1] != 0x0d)
                        {
                            continue;
                        }
                    }                                                                                          // End of Header found
                    flagCont = false;
                }
                if (!flagCont)
                {
                    break;
                }
                if (cnt == buffer.Length)
                {
                    Array.Resize(ref buffer, 2 * buffer.Length);
                }
                if (withServer.Available != 0)
                {
                    cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                }
            }

            HTTPResponseHeader respHeader;

            if (cnt != 0)
            {
                respHeader = new HTTPResponseHeader(buffer);
            }
            else
            {
                return(false);
            }
            int contentLength = 0;

            switch (respHeader.statusCode)
            {
            case 304:
                break;

            case 200:
                try
                {
                    if (int.TryParse(respHeader.httpHdrDict["Content-Length"], out contentLength))
                    {
                        while (cnt < respHeader.hdrLength + contentLength)
                        {
                            if (cnt == buffer.Length)
                            {
                                Array.Resize(ref buffer, 2 * buffer.Length);
                            }
                            cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                        }
                    }
                }
                catch (KeyNotFoundException e)
                {
                    if (respHeader.httpHdrDict["Transfer-Encoding"] == "chunked")
                    {
                        int currChunkLength  = 0;
                        int headerStartIndex = respHeader.hdrLength;

                        while (true)
                        {
                            // Fetching the head and length of the chunk.
                            try
                            {
                                nowAtIndex = Array.IndexOf <byte>(buffer, 0x0a, headerStartIndex);      // Finding the end of the header.
                                Console.WriteLine(Thread.CurrentThread.Name + ", BufSize = " + buffer.Length +
                                                  ", nowHdrStartIndex = " + headerStartIndex);
                            }
                            catch (Exception eee)
                            {
                                Console.WriteLine(Thread.CurrentThread.Name);
                                Console.WriteLine(eee);
                                Console.WriteLine(headerStartIndex + ", " + buffer.Length);
                            }
                            if (nowAtIndex != -1)
                            {
                                currChunkLength = int.Parse(Encoding.UTF8.GetString(buffer, headerStartIndex,
                                                                                    nowAtIndex - headerStartIndex - 1), System.Globalization.NumberStyles.HexNumber);
                                if (currChunkLength == 0)
                                {
                                    break;
                                }                                               // The chunked data stream ends.
                                nowAtIndex++;
                                headerStartIndex = nowAtIndex;
                                // Now nowAtIndex at the beginning of the chunk.

                                while (cnt < nowAtIndex + currChunkLength)          // The current chunk is not completely buffered
                                {
                                    if (cnt == buffer.Length)
                                    {
                                        Array.Resize(ref buffer, 2 * buffer.Length);
                                    }
                                    cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                                }

                                headerStartIndex += (currChunkLength + 2);        // nowAtIndex now at the start of the next Header
                            }
                            else
                            {
                                if (cnt == buffer.Length)
                                {
                                    Array.Resize(ref buffer, 2 * buffer.Length);
                                }
                                cnt += withServer.Receive(buffer, cnt, buffer.Length - cnt, 0);
                            }
                        }
                    }
                }
                break;
            }

            var response = new HTTPResponse(buffer, cnt);

            withClient.Send(response.GetResponseBytes());
            try
            {
                if (response.respHdr.httpHdrDict["Connection"] == "close")
                {
                    withServer.Close(10);
                    withClient.Close(10);
                    return(false);
                }
            }
            catch (KeyNotFoundException ke)
            {
                if (!(req.reqHdr.httpHdrDict["Proxy-Connection"] == "keep-alive"))
                {
                    withServer.Close(10);
                    withClient.Close(10);
                    return(false);
                }
            }

            return(true);
        }