示例#1
0
 /// <summary>
 /// not found action
 /// </summary>
 /// <param name="httpContext"></param>
 /// <returns></returns>
 protected virtual HttpStatusCode NotFoundAction(HttpServerContext httpContext)
 {
     httpContext.Content = "Not Found";
     return(HttpStatusCode.NotFound);
 }
示例#2
0
 public HttpServerFileParameter Create(string name, string fileName, string contentType, HttpServerContext context)
 {
     return(new HttpServerFileParameter(name, fileName, contentType));
 }
示例#3
0
 /// <summary>
 /// 初始化新实例
 /// </summary>
 /// <param name="context"></param>
 public HttpServerEventArgs(HttpServerContext context)
 {
     this.Context = context;
 }
示例#4
0
        /// <summary>
        /// 解析POST数扰
        /// </summary>
        /// <param name="socket"></param>
        /// <param name="context"></param>
        /// <returns>是否成功</returns>
        protected virtual bool ParsePostData(Socket socket, HttpServerContext context)
        {
            var success       = false;
            var contentLength = 0;

            int.TryParse(context.RequestHeader["Content-Length"], out contentLength);
            if (contentLength > context.MaxRequestContentLength)
            {
                //请求主体超过限制
                if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                {
                    context.Content = "Request Entity Too Large";
                }
                context.Response(HttpStatusCode.RequestEntityTooLarge);
            }
            else if (contentLength > 0)
            {
                string contentType = context.RequestHeader["Content-Type"];
                if (contentType == null)
                {
                    context.PostData = SocketHelper.Receive(socket, contentLength);
                    success          = true;
                }
                else if ("application/x-www-form-urlencoded".Equals(contentType, StringComparison.OrdinalIgnoreCase))
                {
                    var data     = SocketHelper.Receive(socket, contentLength);
                    var postdata = this.encoding.GetString(data);
                    UriHelper.ParseQueryString(postdata, this.encoding, context.Form);
                    success = true;
                }
                else if (contentType.IndexOf("multipart/form-data", StringComparison.OrdinalIgnoreCase) != -1)
                {
                    //"multipart/form-data; boundary=---------------------boundary123data"
                    var boundary = contentType.Substring(contentType.IndexOf("boundary=") + 9);
                    var receiver = new HttpServerMultipartReceiver(socket, boundary, context, contentLength);
                    try
                    {
                        return(receiver.Receive());
                    }
                    catch (Exception exception)
                    {
                        //请求主体超过限制
                        if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                        {
                            context.Content = "Paser Post Data Failure " + exception.Message;
                        }
                        context.Response(HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    context.PostData = SocketHelper.Receive(socket, contentLength);
                    success          = true;
                }
            }
            else
            {
                //要求必要的 Content-Length
                if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null)
                {
                    context.Content = "Require Content-Length";
                }
                context.Response(HttpStatusCode.LengthRequired);
            }

            return(success);
        }