예제 #1
0
        public static unsafe void Process(HttpContext context)
        {
            var contentLength = (int)context.Request.ContentLength;
            var pctx          = new ParseContext(Buffer.My, contentLength);

            context.ReadBytes(pctx);

            if (!TryParseLikes(ref pctx, out HList <NewLike> result))
            {
                if (result != null)
                {
                    UpdatePool.Return(result);
                }
                BadRequest(context);
                return;
            }

            if (result != null)
            {
                UpdateWorker.AddLikes(result);
            }

            context.Response.StatusCode    = StatusCodes.Status202Accepted;
            context.Response.ContentLength = 2;
            context.Response.Body.Write(NewHandler.Empty);
        }
예제 #2
0
        public static unsafe void Process(HttpContext context, uint accountId)
        {
            var contentLength = (int)context.Request.ContentLength;
            var pctx          = new ParseContext(Buffer.My, contentLength);

            context.ReadBytes(pctx);

            AccountStub stub = UpdatePool.RentStub();

            stub.id = accountId;

            if (!TryParseAccountUpdate(ref pctx, stub))
            {
                BadRequest(context, stub);
                return;
            }

            if (!stub.email.IsEmpty)
            {
                Account account  = Database.GetAccount(accountId);
                Email   previous = account.Email;
                Database.Replace(previous, stub.email);
            }

            UpdateWorker.Update(stub);

            context.Response.StatusCode    = StatusCodes.Status202Accepted;
            context.Response.ContentLength = 2;
            context.Response.Body.Write(NewHandler.Empty);
        }
예제 #3
0
        private static bool TryParseLikes(ref ParseContext pctx, out HList <NewLike> newLikes)
        {
            var arrayStartIndex = pctx.IndexOf((byte)'[');

            if (pctx[arrayStartIndex + 1] == ']')
            {
                newLikes = null;
                return(true);
            }

            pctx.Move(arrayStartIndex + 2); //skip start array + start object

            newLikes = UpdatePool.RentLikes();
            NewLike like = default;

            while (pctx.Length > 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    if (pctx.StartsWith(likerBytes))
                    {
                        pctx.Move(likerBytes.Length);
                        if (!JsonParser.TryReadUInt(ref pctx, out like.Liker) || Database.NotExists(like.Liker))
                        {
                            return(false);
                        }
                    }
                    else if (pctx.StartsWith(likeeBytes))
                    {
                        pctx.Move(likeeBytes.Length);

                        if (!JsonParser.TryReadUInt(ref pctx, out like.Likee) || Database.NotExists(like.Likee))
                        {
                            return(false);
                        }
                    }
                    else if (pctx.StartsWith(tsBytes))
                    {
                        pctx.Move(tsBytes.Length);

                        if (!JsonParser.TryReadInt(ref pctx, out like.Ts))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }

                pctx.Move(2); // skip ",{" or "]}" in the end
                newLikes.Add(like);
            }

            return(true);
        }
예제 #4
0
        public static void Process(HttpContext context)
        {
            var contentLength = (int)context.Request.ContentLength;
            var pctx          = new ParseContext(Buffer.My, contentLength);

            context.ReadBytes(pctx);

            var stub = UpdatePool.RentStub();

            var statusCode = TryParseAccount(ref pctx, stub);

            context.Response.StatusCode = statusCode;
            if (statusCode == 201)
            {
                context.Response.ContentLength = Empty.Length;
                context.Response.Body.Write(Empty);
                UpdateWorker.AddNew(Database.InsertFromPost(stub));
            }
            else
            {
                UpdatePool.Return(stub);
            }
        }
예제 #5
0
 private static void BadRequest(HttpContext context, AccountStub stub)
 {
     context.Response.StatusCode = StatusCodes.Status400BadRequest;
     UpdatePool.Return(stub);
 }