/// <summary> /// 创建错误输出数据 /// </summary> protected unsafe virtual void createErrorResponse() { KeyValue <Http.Response, Http.Response>[] errorResponses = new KeyValue <Http.Response, Http.Response> [EnumAttribute <Http.ResponseState> .GetMaxValue(-1) + 1]; int isResponse = 0; try { byte[] path = new byte[9]; fixed(byte *pathFixed = path) { *pathFixed = (byte)'/'; *(int *)(pathFixed + sizeof(int)) = '.' + ('h' << 8) + ('t' << 16) + ('m' << 24); *(pathFixed + sizeof(int) * 2) = (byte)'l'; foreach (Http.ResponseState type in System.Enum.GetValues(typeof(Http.ResponseState))) { Http.ResponseStateAttribute state = EnumAttribute <Http.ResponseState, Http.ResponseStateAttribute> .Array((int)type); if (state != null && state.IsError) { int stateValue = state.Number, value = stateValue / 100; *(pathFixed + 1) = (byte)(value + '0'); stateValue -= value * 100; *(pathFixed + 2) = (byte)((value = stateValue / 10) + '0'); *(pathFixed + 3) = (byte)((stateValue - value * 10) + '0'); Http.Response response = null; FileCache fileCache = file(path, default(SubArray <byte>), ref response, true); if (fileCache == null) { if (response != null) { response.CancelPool(); errorResponses[(int)type].Set(response, response); isResponse = 1; } } else { Http.Response gzipResponse; if ((response = fileCache.Response) == null) { response = Http.Response.New(); gzipResponse = Http.Response.New(); SubArray <byte> data = fileCache.Data, gzipData = fileCache.GZipData; if (FileCacheQueue.IsFileCacheHeader && data.Start == FileCache.HttpHeaderSize) { response.SetCanHeaderSize(ref data); gzipResponse.SetCanHeaderSize(ref gzipData); } else { response.SetBody(ref data); gzipResponse.SetBody(ref gzipData); } gzipResponse.SetContentEncoding(Http.Response.GZipEncoding); } else { gzipResponse = fileCache.GZipResponse ?? response; } response.SetState(type); gzipResponse.SetState(type); errorResponses[(int)type].Set(response, gzipResponse); isResponse = 1; } } } } } catch (Exception error) { RegisterServer.TcpServer.Log.Add(AutoCSer.Log.LogType.Error, error); } if (isResponse != 0) { this.errorResponses = errorResponses; } }
/// <summary> /// HTTP文件请求处理 /// </summary> /// <param name="header">请求头部信息</param> /// <param name="fileCache">文件输出信息</param> /// <param name="response">HTTP响应</param> protected unsafe void file(Http.Header header, FileCache fileCache, ref Http.Response response) { Http.HeaderFlag headerFlag = header.Flag; if (fileCache == null) { if (response != null) { if (response.Type == Http.ResponseType.File) { if ((headerFlag & Http.HeaderFlag.IsRange) != 0 && !header.FormatRange(response.BodySize)) { response = Http.Response.RangeNotSatisfiable416; return; } if ((headerFlag & Http.HeaderFlag.IsVersion) != 0 || isStaticFileCacheControl(header.Path)) { response.CacheControl = AutoCSer.Net.Http.Response.StaticFileCacheControl; } } if ((response.Flag & Http.ResponseFlag.IsPool) != 0 && (headerFlag & Http.HeaderFlag.IsSetOrigin) != 0 && this.isOrigin(header.Origin, (headerFlag & Http.HeaderFlag.IsSsl) != 0)) { response.SetAccessControlAllowOrigin(header.OriginIndex); } } return; } if ((headerFlag & Http.HeaderFlag.IsRange) != 0 && !header.FormatRange(fileCache.Data.Length)) { response = Http.Response.RangeNotSatisfiable416; return; } byte[] cacheControl = (headerFlag & Http.HeaderFlag.IsVersion) != 0 || isStaticFileCacheControl(header.Path) ? AutoCSer.Net.Http.Response.StaticFileCacheControl : this.cacheControl; bool isOrigin = (headerFlag & Http.HeaderFlag.IsSetOrigin) != 0 && this.isOrigin(header.Origin, (headerFlag & Http.HeaderFlag.IsSsl) != 0), isHeader = !isOrigin && (headerFlag & Http.HeaderFlag.IsRange) == 0 && FileCacheQueue.IsFileCacheHeader; if (isHeader && (response = (headerFlag & Http.HeaderFlag.IsGZip) == 0 ? fileCache.Response : fileCache.GZipResponse) != null && response.IsCacheControl(cacheControl)) { return; } SubArray <byte> body = (headerFlag & Http.HeaderFlag.IsGZip) != 0 && (headerFlag & Http.HeaderFlag.IsRange) == 0 ? fileCache.GZipData : fileCache.Data; response = Http.Response.Get(); //response.State = Http.ResponseState.Ok200; if (isHeader && body.Start == FileCache.HttpHeaderSize) { response.SetCanHeaderSize(ref body); } else { response.SetBody(ref body); } response.CacheControl = cacheControl; response.ContentType = fileCache.ContentType; if (body.Array != fileCache.Data.Array) { response.SetContentEncoding(Http.Response.GZipEncoding); } response.SetLastModified(fileCache.LastModified); if (isOrigin) { response.SetAccessControlAllowOrigin(header.OriginIndex); } return; }