Esempio n. 1
0
        /// <summary>
        /// Queues a <see cref="ResourceExecutionContext"/> that is ready to be processed.
        /// </summary>
        /// <param name="context">The <see cref="ResourceExecutionContext"/> to add to the pending queue.</param>
        protected void QueueNewPendingContext(ResourceExecutionContext context)
        {
            Contract.Requires(context != null);

            this.OnContextPending(new ResourceExecutionContextEventArgs
            {
                Context = context
            });
        }
Esempio n. 2
0
        /// <summary>
        /// Overridden.
        /// </summary>
        /// <param name="buffer"></param>
        /// <param name="startIndex"></param>
        /// <param name="count"></param>
        protected override void ProcessBufferContents(byte[] buffer, int startIndex, int count)
        {
            Contract.Requires(buffer != null);

            int x = -2;
            //string debug = Encoding.ASCII.GetString(buffer, startIndex, count);
            List<string> lines = new List<string>();
            List<int> headerBreaks = new List<int>();
            List<int> sectionBreaks = new List<int>();

            unsafe
            {
                fixed (byte* b = &buffer[0])
                {
                    byte* n = b;
                    sbyte* s = (sbyte*)b;
                    int m = count;
                    for (int i = 0; i < m; ++i)
                    {
                        var w8 = *n;
                        if (w8 == ':')
                        {
                            headerBreaks.Add(i - x);
                        }
                        else if (w8 == '\r')
                        {
                            var w16 = *((ushort*)n);
                            if (w16 == HttpConnection.SystemSingleEol)
                            {
                                var w32 = *((uint*)n);
                                if (w32 == HttpConnection.SystemDoubleEol)
                                {
                                    sectionBreaks.Add(i);
                                }
                                else
                                    lines.Add(new string(s, x + 2, i - x - 2));
                            }
                            x = i;
                        }
                        ++n;
                    }

                }
                // Check if we received the whole request. The last line should be empty.
                if (lines.Count > 0 && lines[lines.Count - 1] == "")
                {
                    var line0Tokens = lines[0].Split(' ');

                    var context = new ResourceExecutionContext
                    {
                        Request = new Request
                        {
                            RawMethod = line0Tokens[0],
                            RawUrl = line0Tokens[1],
                        },
                        Response = new Response(),
                        Connection = this,
                    };

                    for (int i = 1; i < (lines.Count - 1); ++i)
                        context.Request.Headers.Add(new Header(lines[i].Substring(0, headerBreaks[i - 1] -2), lines[i].Substring(headerBreaks[i - 1])));

                    var host = context.Request.Headers["Host"].Value;
                    ushort port = 80;

                    if (host.IndexOf(':') > 0)
                    {
                        var portstring = host.Substring(host.IndexOf(':') + 1);
                        port = ushort.Parse(portstring);
                        host = host.Substring(0, host.IndexOf(':'));
                    }

                    var urlb = new UriBuilder("http", host);
                    urlb.Port = port;
                    urlb.Path = line0Tokens[1];
                    context.Request.Url = urlb.Uri;

                    this.QueueNewPendingContext(context);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
        }