/// <summary> /// Http 请求处理 /// </summary> /// <param name="context"></param> private bool HttpProcess(HttpServerContext context) { try { var status = HttpStatusCode.OK; if (this.handler != null) { try { this.handler.Process(context); status = context.Status; } catch (Exception e) { status = HttpStatusCode.InternalServerError; context.Content = e.Message; this.OnError(e); } } else if (this.callback != null) { try { status = this.callback(context); } catch (Exception e) { status = HttpStatusCode.InternalServerError; context.Content = e.Message; this.OnError(e); } } else { context.Content = "no set callback or handler"; status = HttpStatusCode.ServiceUnavailable; } //非异步响应 if (context.ChunkWriteStatus == HttpServerChunkStatus.NoBegin) { if (status != HttpStatusCode.OK && string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null) { context.Content = status.ToString(); } context.Response(status); } else { //等待结束写入 context.chunkWriteEvent.WaitOne(); context.chunkWriteEvent.Close(); } } finally { //进行必要的资源清理 context.Clear(); } return(context.KeepAlive && context.Socket.Connected); }
/// <summary> /// receive /// </summary> /// <exception cref="InvalidDataException"></exception> /// <exception cref="IOException"></exception> /// <returns>is success</returns> public bool Receive() { //first boundary var line_data = this.reader.ReadByteLine(); if (this.CompareBoundary(line_data)) { while (this.isEnd == false) { if (this.ParsePart() == false) { return(false); } this.context.UploadedLength = this.reader.ReadAllLength; } } else if (this.CompareBoundaryEnd(line_data)) { } else { throw new InvalidDataException("unknown format of content"); } // this.context.UploadedLength = this.reader.ReadAllLength; //check content length if (this.reader.ReadAllLength == this.contentLength) { return(true); } else { if (string.IsNullOrEmpty(context.Content) && context.ContentBuffer == null) { context.Content = "end of unfinished"; } context.Response(HttpStatusCode.BadRequest); return(false); } }
/// <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); }