Example #1
0
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody,
                IHttpResponseDelegate response)
            {
                if (request.Uri == "/")
                {
                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", "20" },
                    }
                    };
                    var body = new BufferedProducer("Hello world.\r\nHello.");

                    response.OnResponse(headers, body);
                }
                else if (request.Uri == "/bufferedecho")
                {
                    // when you subecribe to the request body before calling OnResponse,
                    // the server will automatically send 100-continue if the client is
                    // expecting it.
                    requestBody.Connect(new BufferedConsumer(bufferedBody =>
                    {
                        var headers = new HttpResponseHead()
                        {
                            Status = "200 OK",
                            Headers = new Dictionary<string, string>()
                                {
                                    { "Content-Type", "text/plain" },
                                    { "Content-Length", request.Headers["Content-Length"] },
                                    { "Connection", "close" }
                                }
                        };
                        response.OnResponse(headers, new BufferedProducer(bufferedBody));
                    }, error =>
                    {
                        // XXX
                        // uh oh, what happens?
                    }));
                }
                else if (request.Uri == "/echo")
                {
                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Content-Length", request.Headers["Content-Length"] },
                            { "Connection", "close" }
                        }
                    };

                    // if you call OnResponse before subscribing to the request body,
                    // 100-continue will not be sent before the response is sent.
                    // per rfc2616 this response must have a 'final' status code,
                    // but the server does not enforce it.
                    response.OnResponse(headers, requestBody);
                }
                else
                {
                    var responseBody = "The resource you requested ('" + request.Uri + "') could not be found.";
                    var headers = new HttpResponseHead()
                    {
                        Status = "404 Not Found",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                    };
                    var body = new BufferedProducer(responseBody);

                    response.OnResponse(headers, body);
                }
            }
Example #2
0
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody,
                IHttpResponseDelegate response)
            {
                if (request.Method.ToUpperInvariant() == "POST" && request.Uri.StartsWith("/bufferedecho"))
                {
                    // when you subecribe to the request body before calling OnResponse,
                    // the server will automatically send 100-continue if the client is
                    // expecting it.
                    requestBody.Connect(new BufferedConsumer(bufferedBody =>
                    {
                        var headers = new HttpResponseHead()
                        {
                            Status = "200 OK",
                            Headers = new Dictionary<string, string>()
                                {
                                    { "Content-Type", "text/plain" },
                                    { "Content-Length", request.Headers["Content-Length"] },
                                    { "Connection", "close" }
                                }
                        };
                        response.OnResponse(headers, new BufferedProducer(bufferedBody));
                    }, error =>
                    {
                        // XXX
                        // uh oh, what happens?
                    }));
                }
                else if (request.Method.ToUpperInvariant() == "POST" && request.Uri.StartsWith("/echo"))
                {
                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                        {
                            { "Content-Type", "text/plain" },
                            { "Connection", "close" }
                        }
                    };
                    if (request.Headers.ContainsKey("Content-Length"))
                        headers.Headers["Content-Length"] = request.Headers["Content-Length"];

                    // if you call OnResponse before subscribing to the request body,
                    // 100-continue will not be sent before the response is sent.
                    // per rfc2616 this response must have a 'final' status code,
                    // but the server does not enforce it.
                    response.OnResponse(headers, requestBody);
                }
                else if (request.Uri.StartsWith("/"))
                {
                    var body = string.Format(
                        "Hello world.\r\nHello.\r\n\r\nUri: {0}\r\nPath: {1}\r\nQuery:{2}\r\nFragment: {3}\r\n",
                        request.Uri,
                        request.Path,
                        request.QueryString,
                        request.Fragment);

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else
                {
                    var responseBody = "The resource you requested ('" + request.Uri + "') could not be found.";
                    var headers = new HttpResponseHead()
                    {
                        Status = "404 Not Found",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                    };
                    var body = new BufferedProducer(responseBody);

                    response.OnResponse(headers, body);
                }
            }
Example #3
0
            public void OnRequest(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response)
            {
                if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/next"))
                {
                    // when you subscribe to the request body before calling OnResponse,
                    // the server will automatically send 100-continue if the client is
                    // expecting it.
                    bool ret = MainWindow.Next();

                    var body = ret ? "Successfully skipped." : "You have to wait for 20 seconds to skip again.";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/pause"))
                {
                    MainWindow.Pause();
                    var body = "Paused.";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/play"))
                {
                    MainWindow.Play();
                    var body = "Playing.";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/toggleplaypause"))
                {
                    var body = "";
                    if (MainWindow._player.Playing)
                    {
                        body = "Paused.";
                    }
                    else
                    {
                        body = "Playing.";
                    }
                    MainWindow.PlayPauseToggle();

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/like"))
                {
                    MainWindow.Like();
                    var body = "Like";
                    if (MainWindow.GetCurrentSong().Loved)
                        body = "Liked";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/dislike"))
                {
                    MainWindow.Dislike();
                    var body = "Disliked.";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/currentsong"))
                {
                    Song s = MainWindow.GetCurrentSong();
                    var body = new JavaScriptSerializer().Serialize(s);

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Method.ToUpperInvariant() == "GET" && request.Uri.StartsWith("/connect"))
                {
                    var body = "true";

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else if (request.Uri.StartsWith("/"))
                {
                    var body = string.Format(
                        "Hello world.\r\nHello.\r\n\r\nUri: {0}\r\nPath: {1}\r\nQuery:{2}\r\nFragment: {3}\r\n",
                        request.Uri,
                        request.Path,
                        request.QueryString,
                        request.Fragment);

                    var headers = new HttpResponseHead()
                    {
                        Status = "200 OK",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", body.Length.ToString() },
                    }
                    };
                    response.OnResponse(headers, new BufferedProducer(body));
                }
                else
                {
                    var responseBody = "The resource you requested ('" + request.Uri + "') could not be found.";
                    var headers = new HttpResponseHead()
                    {
                        Status = "404 Not Found",
                        Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                    };
                    var body = new BufferedProducer(responseBody);

                    response.OnResponse(headers, body);
                }
            }
Example #4
0
            private static void Return404(HttpRequestHead request, IHttpResponseDelegate response)
            {
                var responseBody = "The resource you requested ('" + request.Uri + "') could not be found.";
                var headers = new HttpResponseHead()
                {
                    Status = "404 Not Found",
                    Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                };
                var body = new BufferedProducer(responseBody);

                response.OnResponse(headers, body);
            }
Example #5
0
            private static void Return200(IHttpResponseDelegate response, string content, string contentType)
            {
                var headers = new HttpResponseHead()
                {
                    Status = "200 OK",
                    Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", contentType },
                        { "Content-Length", content.Length.ToString() },
                    }
                };

                var body = new BufferedProducer(content);
                response.OnResponse(headers, body);
            }
Example #6
0
            //Bad request
            private void send400(HttpRequestHead request, IDataProducer requestBody,
                IHttpResponseDelegate response, String msg)
            {
                var responseBody = "The resource you requested ('" + request.Uri + "') could not be executed.";
                if (!msg.Equals(""))
                    responseBody += "\r\nError: " + msg;
                var headers = new HttpResponseHead()
                {
                    Status = "400 Bad Request",
                    Headers = new Dictionary<string, string>()
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                };

                var body = new BufferedProducer(responseBody);

                response.OnResponse(headers, body);
            }
Example #7
0
            //OK + buffered Body
            private void send200(HttpRequestHead request, IDataProducer requestBody, IHttpResponseDelegate response, String responseBody)
            {

                var headers = new HttpResponseHead()
                {
                    Status = "200 OK",
                    Headers = new Dictionary<string, string>() 
                    {
                        { "Content-Type", "text/plain" },
                        { "Content-Length", responseBody.Length.ToString() }
                    }
                };
                var body = new BufferedProducer(responseBody);
                response.OnResponse(headers, body);

            }