Ejemplo n.º 1
0
        public void AfterFirstLineRecvHandler(ref SelectControl selectControl, Socket socket, Buf safeBuffer)
        {
            try
            {
                if (!TryReceive(socket, safeBuffer))
                {
                    selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
                    return;
                }

                String line = lineParser.GetLine();
                if (line == null)
                {
                    return;
                }

                ByteBuilder builder = new ByteBuilder(safeBuffer.array);
                HandleNpcLines(ref selectControl, socket, safeBuffer, builder, line);
            }
            catch (Exception e)
            {
                callback.UnhandledException(clientString, e);
                selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
            }
        }
Ejemplo n.º 2
0
        public void Run()
        {
            try
            {
                ByteBuilder builder = new ByteBuilder(2048);

                //
                // Get first line
                //
                String line = socketLineReader.ReadLine();
                if (line == null)
                {
                    return;
                }

                if (line.StartsWith("GET "))
                {
                    uint start = BuildHttpResponseFromFirstLine(builder, line);
                    if (builder.contentLength > 0)
                    {
                        socket.Send(builder.bytes, (int)start, (int)(builder.contentLength - start), SocketFlags.None);
                    }

                    socket.ShutdownAndDispose();
                }
                else
                {
                    //
                    // Tcp Mode
                    //
                    do
                    {
                        if (!HandleLine(builder, line))
                        {
                            socket.ShutdownAndDispose();
                            break;
                        }
                        if (builder.contentLength > 0)
                        {
                            socket.Send(builder.bytes, (int)builder.contentLength, SocketFlags.None);
                            builder.Clear();
                        }

                        line = socketLineReader.ReadLine();
                    } while (line != null);

                    socket.ShutdownAndDispose();
                }
            }
            catch (Exception e)
            {
                callback.UnhandledException(clientString, e);
                socket.ShutdownAndDispose();
            }
        }
Ejemplo n.º 3
0
        public void InitialRecvHandler(ref SelectControl selectControl, Socket socket, Buf safeBuffer)
        {
            try
            {
                if (!TryReceive(socket, safeBuffer))
                {
                    selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
                    return;
                }

                String line = lineParser.GetLine();
                if (line == null)
                {
                    return;
                }

                ByteBuilder builder = new ByteBuilder(safeBuffer.array);

                // Check if it is an HTTP request
                if (line.StartsWith("GET "))
                {
                    selectControl.RemoveReceiveSocket(socket);
                    uint start = BuildHttpResponseFromFirstLine(builder, line);

                    if (builder.bytes != safeBuffer.array)
                    {
                        safeBuffer.array = builder.bytes; // Update the buffer with the bigger buffer
                    }
                    if (builder.contentLength > 0)
                    {
                        socket.Send(builder.bytes, (int)start, (int)(builder.contentLength - start), SocketFlags.None);
                    }

                    selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
                    return;
                }

                selectControl.UpdateHandler(socket, AfterFirstLineRecvHandler);
                HandleNpcLines(ref selectControl, socket, safeBuffer, builder, line);
            }
            catch (Exception e)
            {
                callback.UnhandledException(clientString, e);
                selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
            }
        }
Ejemplo n.º 4
0
        public UInt32 BuildHttpResponseFromFirstLine(ByteBuilder httpBuilder, String firstLineOfHttpRequest)
        {
            String[] httpStrings = firstLineOfHttpRequest.Split(new Char[] { ' ' }, 3);

            String resourceString = HttpUtility.UrlDecode(httpStrings[1]);

            httpBuilder.AppendAscii(httpStrings[2]);
            httpBuilder.AppendAscii(' ');

            if (resourceString.Equals("/favicon.ico"))
            {
                httpBuilder.AppendAscii("200 OK\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/html\r\nContent-Length: ");
                httpBuilder.AppendNumber(DefaultFavIcon.Length);
                httpBuilder.AppendAscii(Http.DoubleNewline);
                httpBuilder.AppendAscii(DefaultFavIcon);
                return(0);
            }
            else
            {
                return(BuildHttpResponseFromResource(httpBuilder, resourceString));
            }
        }
Ejemplo n.º 5
0
        public void TestMethod1()
        {
            ByteBuilder builder = new ByteBuilder(0);

            builder.AppendNumber(0, 10);
            Assert.AreEqual((Byte)'0', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(1, 10);
            Assert.AreEqual((Byte)'1', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(9, 10);
            Assert.AreEqual((Byte)'9', builder.bytes[0]);

            builder.Clear();
            builder.AppendNumber(10, 10);
            Assert.AreEqual((Byte)'1', builder.bytes[0]);
            Assert.AreEqual((Byte)'0', builder.bytes[1]);

            builder.Clear();
            builder.AppendNumber(48, 10);
            Assert.AreEqual((Byte)'4', builder.bytes[0]);
            Assert.AreEqual((Byte)'8', builder.bytes[1]);

            builder.Clear();
            builder.AppendNumber(975, 10);
            Assert.AreEqual((Byte)'9', builder.bytes[0]);
            Assert.AreEqual((Byte)'7', builder.bytes[1]);
            Assert.AreEqual((Byte)'5', builder.bytes[2]);

            builder.Clear();
            builder.AppendNumber(0xAF12, 16);
            Assert.AreEqual((Byte)'A', builder.bytes[0]);
            Assert.AreEqual((Byte)'F', builder.bytes[1]);
            Assert.AreEqual((Byte)'1', builder.bytes[2]);
            Assert.AreEqual((Byte)'2', builder.bytes[3]);
        }
Ejemplo n.º 6
0
        // Returns the offset of the http response in the text builder
        public UInt32 BuildHttpResponseFromResource(ByteBuilder httpBuilder, String resourceString)
        {
            // Append the headers (leave space for content length)
            httpBuilder.AppendAscii("200 OK\r\nConnection: close\r\nAccess-Control-Allow-Origin: *\r\nContent-Type: text/html\r\nContent-Length: ??????????\r\n\r\n");

            uint contentOffset = httpBuilder.contentLength;

            BuildHtmlResponse(httpBuilder, resourceString);
            uint contentLength = httpBuilder.contentLength - contentOffset;

            // Insert the content length
            String contentLengthString = contentLength.ToString();

            if (contentLengthString.Length > 10)
            {
                throw new InvalidOperationException(String.Format("CodeBug: content length {0} is too big", contentLengthString));
            }
            for (int i = 0; i < contentLengthString.Length; i++)
            {
                httpBuilder.bytes[contentOffset - 4 - contentLengthString.Length + i] = (byte)contentLengthString[i];
            }

            // Shift the headers
            UInt32 shift = 10 - (uint)contentLengthString.Length; // 10 characters were reserved for the content length

            if (shift > 0)
            {
                for (int i = 0; i <= (contentOffset - 15); i++)
                {
                    httpBuilder.bytes[contentOffset - 5 - contentLengthString.Length - i] =
                        httpBuilder.bytes[contentOffset - 5 - contentLengthString.Length - i - shift];
                }
            }

            return(shift);
        }
Ejemplo n.º 7
0
        void HandleNpcLines(ref SelectControl selectControl, Socket socket, Buf safeBuffer, ByteBuilder builder, String line)
        {
            do
            {
                if (!HandleLine(builder, line))
                {
                    selectControl.ShutdownDisposeAndRemoveReceiveSocket(socket);
                    break;
                }
                if (builder.bytes != safeBuffer.array)
                {
                    safeBuffer.array = builder.bytes; // Update the buffer with the bigger buffer
                }

                if (builder.contentLength > 0)
                {
                    socket.Send(builder.bytes, (int)builder.contentLength, SocketFlags.None);
                    builder.Clear();
                }

                line = lineParser.GetLine();
            } while (line != null);
        }