public void WorkLoop() { do { _waitHandler.WaitOne(); if (_needToTerminate) { _state = WorkThreadState.Terminated; break; } _action.Invoke(); _waitHandler.Reset(); _state = WorkThreadState.Idle; WorkDone?.Invoke(this); } while (!_needToTerminate); }
/// <summary> /// 解析报文 到 owin 协议 /// </summary> /// <param name="state"></param> private void ProcessRequestData(object state) { WorkThreadState workThreadState = (WorkThreadState)state; int handleSize = workThreadState.HandleSize; int offset = 0; //截取 http方法 请求路径 http版本 byte[] protocol = CommonUtil.GetProtocolBytes(_requestData, 0, handleSize, ref offset); if (protocol == null || offset == 0 || CommonUtil.CheckBadRequest(ref protocol) == 0) { //不存在 http 请求行 byte[] array2 = ResultFactory.FormartBR400(string.Format("Bad Request: '{0}'", (protocol == null) ? "null" : Encoding.ASCII.GetString(protocol))); workThreadState.OwinSocket.Write(array2, new Action <OwinSocket, int, Exception, object>(OnWriteCompleteToClose), null); RequestDataFactory.Recover(_requestData); _requestData = null; return; } //消息报头 byte[] headDomainBytes = new byte[handleSize - offset]; Buffer.BlockCopy(_requestData, offset, headDomainBytes, 0, headDomainBytes.Length); byte[] preLoadedBodyBytes = null; if (handleSize < _requestDataSize) { //如果传输 preLoadedBodyBytes = new byte[_requestDataSize - handleSize]; Buffer.BlockCopy(_requestData, handleSize, preLoadedBodyBytes, 0, preLoadedBodyBytes.Length); } IDictionary <string, string[]> dictionary = CommonUtil.ConvertByteToDic(headDomainBytes); RequestData requestData = RequestData.New(); requestData._preLoadedBody = preLoadedBodyBytes; requestData.HeadDomainDic = dictionary; string protocolStr = Encoding.ASCII.GetString(protocol); int index = protocolStr.IndexOf(' '); requestData.HttpMethod = protocolStr.Substring(0, index).ToUpper(); protocolStr = protocolStr.Substring(index + 1); index = protocolStr.LastIndexOf(' '); requestData.RequestUrl = protocolStr.Substring(0, index).Trim(); requestData.HttpVersion = protocolStr.Substring(index).Trim(); index = requestData.RequestUrl.IndexOf('?'); if (index == 0) { requestData.RequestQueryString = requestData.RequestUrl; requestData.RequestUrl = "/"; } else if (index > 0) { string text2 = requestData.RequestUrl.Substring(0, index).Trim(); string b = requestData.RequestUrl.Substring(index + 1); requestData.RequestUrl = text2; requestData.RequestQueryString = b; if (!string.IsNullOrEmpty(requestData.RequestQueryString)) { requestData.RequestQueryString = requestData.RequestQueryString.Trim(); } } requestData.SafeRequestUrl = GetSafePath(requestData.RequestUrl); string host = (requestData.HeadDomainDic == null || !requestData.HeadDomainDic.ContainsKey("Host")) ? string.Empty : requestData.HeadDomainDic["Host"].FirstOrDefault <string>(); if (string.IsNullOrEmpty(host)) { host = ((requestData.HeadDomainDic == null || !requestData.HeadDomainDic.ContainsKey("host")) ? string.Empty : requestData.HeadDomainDic["host"].FirstOrDefault <string>()); } if (!string.IsNullOrEmpty(host)) { ParseHost(host, ref requestData.Host, ref requestData.Port); } else { requestData.Host = "localhost"; requestData.Port = 80; } requestData.Socket = workThreadState.OwinSocket; RequestDataFactory.Recover(_requestData); _requestData = null; _requestDataOffset = 0; _requestDataSize = 0; OwinTask.ExcuteWorkTask(requestData); }