public override void Compile(CompileContext context)
        {
            string      innerTypeName = "Repl_" + context.Type.Name;
            ILGenerator il            = context.ILGenerator;
            TypeInfo    newType       = null;

            newType = Process.CompileNewProcessStart(context, innerTypeName);
            Process.Compile(context);
            Process.CompileNewProcessEnd(context);
            Label startLoop = il.DefineLabel();

            il.MarkLabel(startLoop);
            LocalBuilder replCount = il.DeclareLocal(typeof(int));

            //Give the replicated process the variables as they are at this point...
            foreach (string paramName in newType.ConstructorParameters)
            {
                il.Emit(OpCodes.Ldloc, context.Type.GetLocal(paramName));
            }

            LocalBuilder loc = il.DeclareLocal(newType.Builder);

            il.Emit(OpCodes.Newobj, newType.Constructor);
            il.Emit(OpCodes.Stloc, loc);
            il.Emit(OpCodes.Ldloc, loc);

            //Set this process as the parent of the new proc, that allows it to activate this thread
            //again once it is past its first input action.
            il.Emit(OpCodes.Ldarg_0); //load the "this" pointer
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "set_Parent"));

            //start the new instance of the replicated process
            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "Run"));

            //Count how many we've emitted
            il.Emit(OpCodes.Ldloc, replCount);
            il.Emit(OpCodes.Ldc_I4_1);
            il.Emit(OpCodes.Add);
            il.Emit(OpCodes.Stloc, replCount);

            //Print that information.
            il.Emit(OpCodes.Ldstr, "Number of " + innerTypeName + " started: ");
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("Write", new Type[] { typeof(string) }));
            il.Emit(OpCodes.Ldloc, replCount);
            il.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(int) }));

            //Suspend ourselves, we will be woken up by the replicated process once it gets
            //past its first action...
            il.Emit(OpCodes.Call, typeof(Thread).GetMethod("get_CurrentThread"));
            il.Emit(OpCodes.Call, typeof(Thread).GetMethod("Suspend"));
            il.Emit(OpCodes.Br, startLoop);
        }
Beispiel #2
0
 /// <summary>
 ///     Parses a HTTP header from a HTTP/2 header collection
 /// </summary>
 /// <param name="headers">the header collection including pseudo headers</param>
 /// <param name="methodResolver">the method resolver</param>
 /// <returns>an optional used to determine if the invoker should listen for a payload</returns>
 public static HttpParserOptional ParseFrom(HttpHeaderCollection headers,
                                            MethodResolver <HttpMethod> methodResolver)
 {
     return(new HttpParserOptional
     {
         ExpectPayload = methodResolver.GetMethod(Encoding.UTF8.GetBytes(headers[":method"])).ExpectPayload ||
                         headers.ContainsKey("content-length") && headers["content-length"] != "0",
         Headers = headers,
         MethodResolver = methodResolver,
         Payload = null
     });
 }
Beispiel #3
0
        protected void EmitRunProcess(CompileContext context, TypeInfo procType, bool setGuidOnProc, LexicalInfo lexInfo, bool loadVariables)
        {
            if (context.Type == null || context.ILGenerator == null)
            {
                return; //Are at top level and so can't run the process
            }
            ILGenerator il = context.ILGenerator;


            if (loadVariables)
            {
                foreach (string paramName in procType.ConstructorParameters)
                {
                    il.Emit(OpCodes.Ldloc, context.Type.GetLocal(paramName));
                }
            }
            LocalBuilder loc = il.DeclareLocal(typeof(ProcessBase));

            il.Emit(OpCodes.Newobj, procType.Constructor);
            il.Emit(OpCodes.Stloc, loc);
            il.Emit(OpCodes.Ldloc, loc);
            il.Emit(OpCodes.Ldarg_0); //load the "this" pointer

            //The current process doesn't have a restrict or relabel method, no reason for it
            //to continue living, set the parent process of the new proc as our own parent process
            if (!context.Type.IsPreProcessed && !context.Type.IsRestricted && !context.Type.MustLiveOn)
            {
                il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "get_Parent"));
            }
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "set_Parent"));

            if (setGuidOnProc)
            {
                il.Emit(OpCodes.Ldloc, loc);
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("get_SetID"));
                il.Emit(OpCodes.Call, typeof(ProcessBase).GetMethod("set_SetID"));
            }

            il.Emit(OpCodes.Ldloc, loc);
            if (context.Options.Debug && lexInfo != null)
            {
                //context.MarkSequencePoint(lexInfo);
            }
            il.Emit(OpCodes.Call, MethodResolver.GetMethod(typeof(ProcessBase), "Run"));
        }
Beispiel #4
0
        /// <summary>
        ///     Instantiates with the given <see cref="HttpHeaderCollection" />, payload, and method resolver.
        /// </summary>
        /// <param name="headers">
        ///     a collection of HTTP/2 headers with pseudo headers included (:method, :scheme, :authority, :path,
        ///     and (optionally), :version
        /// </param>
        /// <param name="payload">the request payload (can be null)</param>
        /// <param name="methodResolver">
        ///     a method resolver to use to map the method name to a server-implemented
        ///     <see cref="HttpMethod" />
        /// </param>
        public HttpRequest(HttpHeaderCollection headers, byte[] payload,
                           MethodResolver <HttpMethod> methodResolver)
        {
            if (payload != null)
            {
                InputStream = new MemoryStream(payload);
            }
            Method      = methodResolver.GetMethod(Encoding.UTF8.GetBytes(headers[":method"].ToUpper()));
            RawMethod   = headers[":method"];
            Url         = new Uri(headers[":scheme"] + "://" + headers[":authority"] + headers[":path"]);
            RawUrl      = headers[":path"];
            Headers     = new HttpHeaderCollection(headers.Where <HeaderField>(x => !x.Name.StartsWith(":")));
            InputStream = payload == null ? new MemoryStream() : new MemoryStream(payload);
#pragma warning disable 618
            Protocol = headers.ContainsKey(":version") ? headers[":version"] : null;
#pragma warning restore 618
            Cookies = new HttpCookieCollection(headers, true);
        }
Beispiel #5
0
        /// <summary>
        ///     Parses a HTTP header from a string
        /// </summary>
        /// <param name="requestHeader">the header to parse</param>
        /// <param name="methodResolver">the method resolver</param>
        /// <param name="connection">
        ///     the connection that the requestHeader was sourced from. Used to determine the scheme
        ///     (http/https)
        /// </param>
        /// <returns>an optional that will determine the next steps of context construction</returns>
        /// <exception cref="BadRequestException">the request was of an invalid structure</exception>
        public static HttpParserOptional ParseFrom(string requestHeader, MethodResolver <HttpMethod> methodResolver,
                                                   IConnection connection)
        {
            var lines = requestHeader.Split(new[] { "\r\n" }, StringSplitOptions.None);

            if (lines.Length < 1)
            {
                throw new BadRequestException("Bad Request (HTTP/1 parser failure)",
                                              new ArgumentException("Request header contains less than one line", nameof(requestHeader)));
            }

            var rparts = lines[0].Split(' ');
            var method = rparts.First();
            var path   = rparts.Where(part => part != rparts[0]).Where(part => part != rparts[rparts.Length - 1])
                         .Aggregate("", (current, part) => current + part + " ").TrimEnd();
            var proto = rparts.Last();

            if (string.IsNullOrEmpty(method) ||
                string.IsNullOrEmpty(path) ||
                string.IsNullOrEmpty(proto))
            {
                throw new BadRequestException("Bad Request (HTTP/1 parser failure)",
                                              new NullReferenceException(
                                                  "The method, path, and/or protocol parameters aren't present in the request line"));
            }

            var headers = new HttpHeaderCollection(new Dictionary <string, string>
            {
                { ":method", method },
                { ":path", path },
                { ":version", proto },
                {
                    ":scheme",
                    connection.GetType().FullName == "Ultz.SimpleServer.Common.SslListener.SecureConnection"
                        ? "https"
                        : "http"
                }
            }.Select(x => new HeaderField {
                Name = x.Key, Value = x.Value
            }));

            for (var i = 1; i < lines.Length; i++)
            {
                var line     = lines[i];
                var colonIdx = line.IndexOf(':');
                if (colonIdx == -1)
                {
                    throw new BadRequestException("Bad Request (HTTP/1 parser failure)");
                }
                var name  = line.Substring(0, colonIdx).Trim().ToLowerInvariant();
                var value = line.Substring(colonIdx + 1).Trim();
                if (name == "host")
                {
                    name = ":authority";
                }
                headers[name] = value;
            }

            return(new HttpParserOptional
            {
                ExpectPayload = methodResolver.GetMethod(Encoding.UTF8.GetBytes(headers[":method"])).ExpectPayload ||
                                headers.ContainsKey("content-length") && headers["content-length"] != "0",
                Headers = headers,
                MethodResolver = methodResolver,
                Payload = null
            });
        }
Beispiel #6
0
        private void GenerateAssemblyLookup(ModuleBuilder module)
        {
            if (_embeddedAssemblies.Count == 0)
            {
                return;
            }
            foreach (Assembly ass in _embeddedAssemblies)
            {
                string shortname = ass.FullName.Substring(0, ass.FullName.IndexOf(","));
                string tempfile  = Path.GetTempFileName();
                File.Copy(new Uri(ass.EscapedCodeBase).LocalPath, tempfile, true);
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(tempfile));
                ms.Seek(0, SeekOrigin.Begin);
                module.DefineManifestResource(shortname, ms, ResourceAttributes.Public);
                File.Delete(tempfile);
            }

            MethodBuilder  resolveAssemblyMethod = module.DefineGlobalMethod("ResolveAssembly", MethodAttributes.Public | MethodAttributes.Static, typeof(Assembly), new Type[] { typeof(object), typeof(System.ResolveEventArgs) });
            ILGenerator    ilResolve             = resolveAssemblyMethod.GetILGenerator();
            CompileContext resolvecontext        = new CompileContext();

            resolvecontext.PushIL(ilResolve);
            LocalBuilder localStream = ilResolve.DeclareLocal(typeof(Stream));
            LocalBuilder localBuf    = ilResolve.DeclareLocal(typeof(byte[]));
            LocalBuilder localName   = ilResolve.DeclareLocal(typeof(string));

            ilResolve.Emit(OpCodes.Ldarg_1);
            ilResolve.Emit(OpCodes.Call, typeof(ResolveEventArgs).GetMethod("get_Name"));
            ilResolve.Emit(OpCodes.Stloc, localName);

            ilResolve.Emit(OpCodes.Ldloc, localName);
            ilResolve.Emit(OpCodes.Ldc_I4_0);
            ilResolve.Emit(OpCodes.Ldloc, localName);
            ilResolve.Emit(OpCodes.Ldstr, ",");
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("IndexOf", new Type[] { typeof(string) }));
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("Substring", new Type[] { typeof(int), typeof(int) }));
            ilResolve.Emit(OpCodes.Stloc, localName);

            Assign(localStream, Call(Call(typeof(Assembly), "GetExecutingAssembly", false), "GetManifestResourceStream", false, localName), resolvecontext);

            Label notNull = ilResolve.DefineLabel();

            ilResolve.Emit(OpCodes.Ldloc, localStream);
            ilResolve.Emit(OpCodes.Brtrue, notNull);
            {
                //Not found, just return null
                ilResolve.Emit(OpCodes.Ldnull);
                ilResolve.Emit(OpCodes.Ret);
            }
            ilResolve.MarkLabel(notNull);
            Call(localStream, "get_Length", false).Compile(resolvecontext);
            ilResolve.Emit(OpCodes.Conv_Ovf_I);
            ilResolve.Emit(OpCodes.Newarr, typeof(System.Byte));
            ilResolve.Emit(OpCodes.Stloc, localBuf);

            ilResolve.Emit(OpCodes.Ldloc, localStream);
            ilResolve.Emit(OpCodes.Ldloc, localBuf);
            ilResolve.Emit(OpCodes.Ldc_I4_0);
            ilResolve.Emit(OpCodes.Ldloc, localBuf);
            ilResolve.Emit(OpCodes.Ldlen);
            ilResolve.Emit(OpCodes.Conv_I4);
            ilResolve.Emit(OpCodes.Callvirt, typeof(Stream).GetMethod("Read", new Type[] { typeof(byte[]), typeof(int), typeof(int) }));
            ilResolve.Emit(OpCodes.Pop);

            //Notify that we loaded this embedded...
            ilResolve.Emit(OpCodes.Ldarg_1);
            ilResolve.Emit(OpCodes.Call, typeof(ResolveEventArgs).GetMethod("get_Name"));
            ilResolve.Emit(OpCodes.Ldstr, " was not found externally, loading embedded version...");
            ilResolve.Emit(OpCodes.Call, typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }));
            ilResolve.Emit(OpCodes.Call, typeof(System.Console).GetMethod("WriteLine", new Type[] { typeof(string) }));

            Call(typeof(Assembly), "Load", false, localBuf).Compile(resolvecontext);
            ilResolve.Emit(OpCodes.Ret);
            resolvecontext.PopIL();

            MethodBuilder moduleInitializer = module.DefineGlobalMethod(".cctor", MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.RTSpecialName, null, new Type[] { });
            ILGenerator   ilStartup         = moduleInitializer.GetILGenerator();

            ilStartup.Emit(OpCodes.Call, typeof(System.AppDomain).GetMethod("get_CurrentDomain"));
            ilStartup.Emit(OpCodes.Ldnull);
            ilStartup.Emit(OpCodes.Ldftn, resolveAssemblyMethod);
            ilStartup.Emit(OpCodes.Newobj, MethodResolver.GetConstructor(typeof(System.ResolveEventHandler)));
            ilStartup.Emit(OpCodes.Callvirt, MethodResolver.GetMethod(typeof(System.AppDomain), "add_AssemblyResolve"));
            ilStartup.Emit(OpCodes.Ret);
        }