private void HandleGet(object sender, RequestContainer args)
        {
            string path = args.Context.Request.Url.AbsolutePath;
            // Remove leading slash
            path = path.Substring(1);

            if(path.EndsWith("/")){
                path = path.Substring(0, path.Length-1);
            }

            var qs = args.Context.Request.QueryString;

            Logger.Log("GET {0}?{1}", LogLevel.Debug, path, ConstructQueryString(qs));

            switch (path)
            {
                case "events":
                    HandleGetEvents(args);
                    break;
                case "news":
                    HandleGetNews(args);
                    break;
                case "mapitems":
                    HandleGetMapItems(args);
                    break;
            }
        }
        private void HandleGetNews(RequestContainer args)
        {
            var afterStr = args.Context.Request.QueryString["after"];
            var countStr = args.Context.Request.QueryString["count"];

            int count;
            if (!int.TryParse(countStr, out count) || count < 0) {
                // Use the default value if the request does not specify a value for count, or if the specified value is invalid.
                count = DEFAULT_COUNT;
            }
            DateTime after;
            if (afterStr == null) {
                after = new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                var posts = databaseWrapper.GetNews(count, after);
                args.Writer.WriteLine(posts);
            } else {
                if (!DateTime.TryParse(afterStr, null, System.Globalization.DateTimeStyles.AssumeUniversal, out after)) {
                    args.Context.Response.StatusCode = 400;
                } else {
                    var posts = databaseWrapper.GetNews(count, after);
                    args.Writer.WriteLine(posts);
                }
            }
        }
Ejemplo n.º 3
0
        private void HandleRequest(HttpListenerContext context)
        {
            // Whatever the case, the server will always return UTF-8-encoded JSON, if it returns anything at all.
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.ContentType = "application/json";

            StreamReader reader = new StreamReader(context.Request.InputStream);
            StreamWriter writer = new StreamWriter(context.Response.OutputStream);

            RequestContainer container = new RequestContainer(context, reader, writer);

            switch (context.Request.HttpMethod) {
                case "POST":
                    if (OnPOST != null) OnPOST(this, container);
                    break;
                case "GET":
                    if (OnGET != null) OnGET(this, container);
                    break;
                default:
                    Logger.Log("Unknown HTTP method received: {0}. Returning HTTP 400");
                    context.Response.StatusCode = 400;
                    context.Response.StatusDescription = "Bad Request";
                    break;
            }
            writer.Close();
        }
 private void HandleGetMapItems(RequestContainer args)
 {
     var mapItems = databaseWrapper.GetMapItems();
     args.Writer.WriteLine(mapItems);
 }
 private void HandleGetEvents(RequestContainer args)
 {
     var events = databaseWrapper.GetEvents();
     args.Writer.WriteLine(events);
 }
 private void HandlePost(object sender, RequestContainer args)
 {
 }