Execute() public method

public Execute ( ) : void
return void
Exemplo n.º 1
0
        public void Curl(IManosContext ctx, string url, string method, string auth, string [] header_keys = null)
        {
            // TODO: Should we set the Host header automagically?
            Uri u = null;
            if (!Uri.TryCreate (url, UriKind.Absolute, out u)) {
                CurlError (ctx, "Url is invalid: {0}", url);
                return;
            }

            if (u.Scheme == "https") {
                CurlError (ctx, "https connections are not supported yet.");
                return;
            }

            {
                // TODO: Manos will handle this DNS stuff internally soon.
                IPAddress [] addrs = Dns.GetHostAddresses (u.Host);
                if (addrs.Length == 0) {
                    CurlError (ctx, "Could not resolve host: {0}", u.Host);
                    return;
                }

                UriBuilder builder = new UriBuilder (u);
                builder.Host = addrs [0].ToString ();

                url = builder.ToString ();
            }

            HttpRequest r = new HttpRequest (url) {
                Method = GetHttpMethod (method),
            };

            r.Headers.SetNormalizedHeader ("Host", u.Host);

            if (header_keys != null) {
                var header_vals = ctx.Request.Data.GetList ("header_vals");
                for (int i = 0; i < header_keys.Length; i++) {
               		r.Headers.SetHeader (header_keys [i], header_vals [i].SafeValue);
                }
            }

            if (auth == "basic")
                HttpUtility.AddBasicAuthentication (r, ctx.Request.Data ["username"], ctx.Request.Data ["password"]);

            AddBody (ctx, r);

            r.OnResponse += (response) => {

                var res = new Dictionary<object,object> ();
                var response_md = new StringBuilder ();
                var request_md = new StringBuilder ();

                r.WriteMetadata (request_md);
                response.WriteMetadata (response_md);

                res ["header"] = PrettyPrintMetadata (response_md.ToString ());
                res ["request"]  = PrettyPrintMetadata (request_md.ToString ()) + PrintBody (r.GetBody ());
                res ["body"] = "<div><pre>" + HttpUtility.HtmlEncode (response.PostBody) + "</pre></div>";

                ctx.Response.End (JSON.JsonEncode (res));
            };

            try {
                r.Execute ();
            } catch (Exception e) {
                CurlError (ctx, e.Message);
            }
        }
Exemplo n.º 2
0
        public HelloWorld()
        {
            Route ("/", ctx => ctx.Response.End ("Hello, World"));

            Route ("/shutdown", ctx => System.Environment.Exit (0));

            Route ("/info", ctx => {
                ctx.Response.Write ("Host: {0}", ctx.Request.Headers ["Host"]);
                ctx.Response.Write ("Remote Address: {0}", ctx.Request.Socket.Address);
                ctx.Response.Write ("Referer: '{0}'", ctx.Request.Headers ["Referer"]);
                ctx.Response.End ();
            });

            Route ("/timeout", ctx => {
                ctx.Response.WriteLine ("Hello");
                AddTimeout (TimeSpan.FromSeconds (2), (app, data) => {
                    Console.WriteLine ("writing world.");
                    ctx.Response.WriteLine ("World");
                    ctx.Response.End ();
                });
            });

            Route ("/encoding", ctx => {
                ctx.Response.ContentEncoding = System.Text.Encoding.UTF8;
                ctx.Response.SetHeader ("Content-Type","text/html; charset=UTF-8");
                ctx.Response.Write ("À");
                Console.WriteLine ("Writing: '{0}'", "À");

                ctx.Response.End ();
            });

            Get ("/upload", ctx => {
                ctx.Response.Write ("<html><head></head><body>");
                ctx.Response.Write ("<a href='/info'>a link</a>");
                ctx.Response.Write ("<form method=\"POST\">");
                ctx.Response.Write ("<input type=\"text\" name=\"some_name\"><br>");
                ctx.Response.Write ("<input type=\"file\" name=\"the_file\" >");
                ctx.Response.Write ("<input type=\"file\" name=\"the_other_file\" >");
                ctx.Response.Write ("<input type=\"submit\">");
                ctx.Response.Write ("</form>");
                ctx.Response.Write ("</body></html>");

                ctx.Response.End ();
            });

            Put ("/put", ctx => {
                Console.WriteLine ("got the PUT request");
                ctx.Response.End ("this is some stuff!");
            });

            Get ("/request", ctx => {

                var r = new HttpRequest ("http://127.0.0.1:8080/put");

                r.Method = HttpMethod.HTTP_PUT;

                Console.WriteLine ("got the request");
                r.OnResponse += (response) => {
                    Console.WriteLine ("got the response");
                    ctx.Response.End (response.PostBody);

                };
                r.Execute ();
            });

            int count = 0;
            Get ("/request_loop", ctx => {

                if (++count == 3) {
                    ctx.Response.End ("Got this page three times!");
                    return;
                }
            });

            Get ("/sync", ctx => {
                new System.Threading.Thread (() => AsyncCounter (ctx)).Start ();
            });

            Post ("/upload", ctx => {
                ctx.Response.End ("handled upload!");
            });
        }