Exemplo 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;

            //Multipart
            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 NFX.IO.NonClosingStreamWrap(Request.InputStream),
                                                          Request.ContentEncoding);
            }
            else//JSON
            if (ctp.IndexOf(ContentType.JSON) >= 0)
            {
                result = JSONReader.DeserializeDataObject(new NFX.IO.NonClosingStreamWrap(Request.InputStream),
                                                          Request.ContentEncoding) as JSONDataMap;
            }

            return(result);
        }
Exemplo n.º 2
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);
            }
        }