Ejemplo n.º 1
0
        /// <summary>
        /// This method is called only once as it touches the input streams
        /// </summary>
        protected virtual JsonDataMap ParseRequestBodyAsJSONDataMap()
        {
            if (!Request.HasEntityBody)
            {
                return(null);
            }

            JsonDataMap result = null;

            var ctp = Request.ContentType;

            //Has body by no content type
            if (ctp == null)
            {
                throw HTTPStatusException.NotAcceptable_406("Missing content-type");
            }

            try
            {
                //Multi-part
                if (ctp.IndexOf(ContentType.FORM_MULTIPART_ENCODED) >= 0)
                {
                    var boundary = Multipart.ParseContentType(ctp);
                    var mp       = Multipart.ReadFromStream(Request.InputStream, ref boundary, Request.ContentEncoding);
                    result = mp.ToJSONDataMap();
                }
                else //Form URL encoded
                if (ctp.IndexOf(ContentType.FORM_URL_ENCODED) >= 0)
                {
                    result = JsonDataMap.FromURLEncodedStream(new Azos.IO.NonClosingStreamWrap(Request.InputStream),
                                                              Request.ContentEncoding);
                }
                else//JSON
                if (ctp.IndexOf(ContentType.JSON) >= 0)
                {
                    result = JsonReader.DeserializeDataObject(new Azos.IO.NonClosingStreamWrap(Request.InputStream),
                                                              Request.ContentEncoding) as JsonDataMap;
                }

                return(result);
            }
            catch (Exception error)
            {
                throw new HTTPStatusException(WebConsts.STATUS_400,
                                              WebConsts.STATUS_400_DESCRIPTION + " body",
                                              error.ToMessageWithType(),
                                              error);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Finds appropriate handler and invokes it. Returns the appropriate handler or null if work was aborted or already handled.
        /// Throws if appropriate handler was not found
        /// </summary>
        public virtual void InvokeHandler(WorkContext work, out WorkHandler handler)
        {
            if (!work.Aborted && !work.Handled)
            {
                handler = GetWorkHandler(work);

                if (handler == null)
                {
                    throw HTTPStatusException.NotFound_404(StringConsts.NO_HANDLER_FOR_WORK_ERROR.Args(work.About));
                }

                handler.FilterAndHandleWork(work);
                return;
            }
            handler = null;
        }