示例#1
0
        public static void ListenSocketHandler(Object state) //have to confirm to delegate signature
        {
            UInt32      handler     = (UInt32)state;
            HttpRequest httpRequest = new HttpRequest();

            try
            {
                int runCountOfBytesRecvd = 0;

                String data = null;

                byte[] bytes = new Byte[Magic_Number_Buffer];

                // An incoming connection needs to be processed.
                Boolean BodyProcessed = false;
                Int32   bytesRec;
                while (false == BodyProcessed)
                {
                    if (Program.m_maxPayLoad <= runCountOfBytesRecvd)
                    {
                        break; //error
                    }
                    //int bytesRec = handler.Receive(bytes);
                    unsafe
                    {
                        fixed(Byte *hPtr = bytes)
                        {
                            bytesRec = TCPSocket.FetchData(handler, hPtr, Magic_Number_Buffer);
                        }
                    }

                    runCountOfBytesRecvd += bytesRec;
                    data += Encoding.UTF8.GetString(bytes, 0, bytesRec);

                    if (m_parser.IsMatch(data))
                    {
                        //we may have read past the header end. If so, we need to split at the exact
                        //header end. Rest of the contents belong to body if any
                        var splitted = m_parser.Split(data);
                        httpRequest.ConcatenateRawHeaderContent(splitted[0]);
                        int   BodyLength  = httpRequest.ExtractHeaders().GetLengthOfBody();
                        Int32 headerBytes = System.Text.Encoding.UTF8.GetBytes(splitted[0]).Length + 4;
                        //httpRequest Body processing - START
                        BodyProcessed = true;
                        ExtractBody(bytes, bytesRec, handler, httpRequest, headerBytes, BodyLength);
                    }
                    else
                    {
                        httpRequest.ConcatenateRawHeaderContent(data);
                    }
                }


                Byte[] ret = Program.Processor.Process(httpRequest);
                int    sent = 0, start = 0, remaining = ret.Length;

                unsafe
                {
                    fixed(Byte *sptr = ret)
                    {
                        TCPSocket.SendReply(handler, sptr, ret.Length);
                    }
                }
            }
            catch (Exception err)
            {
                String        rr      = "Error occurred" + err.StackTrace;
                StringBuilder strBldr = new StringBuilder();
                strBldr.Append("HTTP/1.1 200 OK\r\n");
                strBldr.Append("accept-ranges: bytes\r\n");
                strBldr.Append("vary: Accept-Encoding, Origin\r\n");
                strBldr.Append("content-encoding: gzip\r\n");
                strBldr.Append("content-type: text/javascript; charset=UTF-8\r\n");
                strBldr.Append("content-length:" + rr.Length * 2 + "\r\n");
                strBldr.Append("date: Fri, 15 Jun 2018 07:47:08 GMT\r\n");
                strBldr.Append("expires: Sat, 15 Jun 2019 07:47:08 GMT\r\n");
                strBldr.Append("last-modified: Wed, 13 Jun 2018 16:59:27 GMT\r\n");
                strBldr.Append("x-content-type-options: nosniff\r\n");
                strBldr.Append("server: sffe\r\n");
                strBldr.Append("x-xss-protection: 1; mode=block\r\n");
                strBldr.Append("cache-control: public, immutable, max-age=31536000\r\n");
                strBldr.Append("age: 51722\r\n");
                strBldr.Append("X-Firefox-Spdy: h2\r\n\r\n");
                strBldr.Append(rr);
                byte[] msg = System.Text.Encoding.ASCII.GetBytes(strBldr.ToString());
                //handler.Send(msg);
                unsafe
                {
                    fixed(Byte *sptr = msg)
                    {
                        TCPSocket.SendReply(handler, sptr, msg.Length);
                    }
                }
            }
            finally
            {
                try
                {
                    TCPSocket.CloseClientSocket(handler);
                }
                catch (Exception err)
                {
                    System.Console.WriteLine("error disconnecting the socket::" + err.Message);
                }
            }
        }