/// <summary> /// HTTP请求处理[TRY] /// </summary> /// <param name="socket">HTTP套接字</param> public override unsafe void Request(Http.SocketBase socket) { Http.Response response = file(socket.HttpHeader); if (response != null) { socket.ResponseIdentity(ref response); } else { socket.ResponseErrorIdentity(Http.ResponseState.NotFound404); } }
/// <summary> /// 文件缓存 /// </summary> /// <param name="data">文件数据</param> /// <param name="contentType">HTTP响应输出内容类型</param> /// <param name="cacheControl">缓存控制参数</param> /// <param name="isGZip">是否压缩</param> /// <returns>文件数据字节数</returns> internal int Set(ref SubArray <byte> data, byte[] contentType, byte[] cacheControl, bool isGZip) { int size = data.Length; ContentType = contentType ?? NullValue <byte> .Array; try { this.data = gZipData = data; if (FileCacheQueue.IsFileCacheHeader && data.Start == FileCache.HttpHeaderSize) { size += FileCache.HttpHeaderSize; Http.Response response = Http.Response.New(); //response.State = Http.ResponseState.Ok200; response.SetCanHeaderSize(ref data); response.CacheControl = cacheControl; response.ContentType = contentType; response.SetLastModified(LastModified); this.response = gZipResponse = response; } if (isGZip) { #if DOTNET2 || DOTNET4 if (Http.Response.GetCompress(ref data, ref gZipData, data.Start)) #else if (Http.Response.GetCompress(ref data, ref gZipData, data.Start, Http.SocketBase.Config.IsFileCacheFastestCompressionLevel)) #endif { size += gZipData.Length; if (FileCacheQueue.IsFileCacheHeader && gZipData.Start == FileCache.HttpHeaderSize) { size += FileCache.HttpHeaderSize; Http.Response gZipResponse = Http.Response.New(); //gZipResponse.State = Http.ResponseState.Ok200; gZipResponse.SetCanHeaderSize(ref gZipData); gZipResponse.ContentType = contentType; gZipResponse.SetLastModified(LastModified); gZipResponse.SetContentEncoding(Http.Response.GZipEncoding); this.gZipResponse = gZipResponse; } } else { gZipData = data; } } } finally { PulseAll(); } return(size); }
private static Http.Response ProcessResponse(Http.Response response) { if (response == null) { throw new Exceptions.APIConnectionException("Connection Error: No response received."); } if (response.StatusCode == HttpStatusCode.OK) { return(response); } else { throw new Exceptions.APIConnectionException("Connection Error: Status Code - " + response.StatusCode); } }
/// <summary> /// HTTP请求处理[TRY] /// </summary> /// <param name="socket">HTTP套接字</param> public override unsafe void Request(Http.SocketBase socket) { if ((socket.HttpHeader.Flag & Http.HeaderFlag.IsSetIfModifiedSince) == 0) { Http.Response response = file(socket.HttpHeader); if (response != null) { socket.ResponseIdentity(ref response); } else { socket.ResponseErrorIdentity(Http.ResponseState.NotFound404); } } else { socket.ResponseIdentity(Http.Response.NotChanged304); } }
private void ProcessNextTaskData(Http.Response response) { currentTaskId = response.ResponseText; if (!_lessonsMap.TryGetValue(currentTaskId, out var nextLesson)) { TutorialCompleted.Invoke(); Debug.Log($"tutorial completed!"); return; } if (!_simulationData.TaskDescriptionMap.TryGetValue(currentTaskId, out var description)) { Debug.LogWarning($"no description for taskId: {currentTaskId}"); } Debug.Log(_simulationData.TaskDescriptionMap[currentTaskId]); _currentLesson = nextLesson; _currentLesson.OnStart(); }
/// <summary> /// HTTP文件请求处理 /// </summary> /// <param name="path">请求路径</param> /// <param name="ifModifiedSince">文件修改时间</param> /// <param name="response">HTTP响应输出</param> /// <param name="isCopyPath">是否复制请求路径</param> /// <returns>文件缓存</returns> protected unsafe FileCache file(byte[] path, SubArray <byte> ifModifiedSince, ref Http.Response response, bool isCopyPath) { string cacheFileName = null; try { if (path.Length != 0 && WorkPath.Length + path.Length <= AutoCSer.IO.File.MaxFullNameLength) { byte[] contentType = null; bool isCompress = true; fixed(byte *pathFixed = path) { byte *pathStart = pathFixed, pathEnd = pathStart + path.Length; if (isFile(pathEnd, ref contentType, ref isCompress) == 0) { if (*pathStart == '/') { ++pathStart; } for (byte *formatStart = pathStart; formatStart != pathEnd; ++formatStart) { if (*formatStart == ':') { response = Http.Response.Blank; return(null); } } int cachePathLength = (int)(pathEnd - pathStart); FileCacheKey cacheKey = new FileCacheKey(pathIdentity, path, (int)(pathStart - pathFixed), cachePathLength); FileCache fileCache = FileCacheQueue.Get(ref cacheKey); if (fileCache == null) { cacheFileName = StringExtension.FastAllocateString(WorkPath.Length + cachePathLength); fixed(char *nameFixed = cacheFileName) { char *write = nameFixed + WorkPath.Length; char directorySeparatorChar = Path.DirectorySeparatorChar; StringExtension.CopyNotNull(WorkPath, nameFixed); for (byte *start = pathStart; start != pathEnd; ++start) { *write++ = *start == '/' ? directorySeparatorChar : (char)*start; } } FileInfo file = new FileInfo(cacheFileName); if (file.Exists) { string fileName = file.FullName; if (fileName.Length > WorkPath.Length && WorkPath.equalCaseNotNull(fileName, WorkPath.Length)) { if (fileName.Length <= AutoCSer.IO.File.MaxFullNameLength && file.Length <= FileCacheQueue.MaxFileSize) { if (FileCacheQueue.Get(ref cacheKey, out fileCache, isCopyPath) != 0) { try { fileCache.LastModified = file.LastWriteTimeUtc.UniversalNewBytes(); int extensionNameLength = (int)(pathEnd - getExtensionNameStart(pathEnd)); SubArray <byte> fileData = readCacheFile(new SubString { String = fileName, Start = fileName.Length - extensionNameLength, Length = extensionNameLength }); FileCacheQueue.Set(ref cacheKey, fileCache, fileCache.Set(ref fileData, contentType, cacheControl, isCompress)); if (ifModifiedSince.Length == fileCache.LastModified.Length) { fixed(byte *ifModifiedSinceFixed = ifModifiedSince.Array) { if (Memory.EqualNotNull(fileCache.LastModified, ifModifiedSinceFixed + ifModifiedSince.Start, ifModifiedSince.Length)) { response = Http.Response.NotChanged304; return(null); } } } } finally { if (fileCache.IsData == 0) { fileCache.PulseAll(); fileCache = null; FileCacheQueue.RemoveOnly(ref cacheKey); } } } } else { if (ifModifiedSince.Length == Date.ToByteLength && Date.UniversalByteEquals(file.LastWriteTimeUtc, ifModifiedSince) == 0) { response = Http.Response.NotChanged304; return(null); } response = Http.Response.Get(); //response.State = Http.ResponseState.Ok200; response.SetBodyFile(file); response.CacheControl = cacheControl; response.ContentType = contentType; response.SetLastModified(file.LastWriteTimeUtc.UniversalNewBytes()); return(null); } } } } return(fileCache); } } } } catch (Exception error) { RegisterServer.TcpServer.Log.Add(AutoCSer.Log.LogType.Error, error, cacheFileName); } return(null); }
/// <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; }
protected Http.Response file(Http.Header header) { Http.Response response = null; file(header, file(header, ref response), ref response); return(response); }
/// <summary> /// HTTP请求处理[TRY] /// </summary> /// <param name="socket">HTTP套接字</param> public override unsafe void Request(Http.SocketBase socket) { Http.Header header = socket.HttpHeader; SubArray <byte> path = header.Path; int index; if (header.IsSearchEngine == 0) { byte[] rewritePath; #if !MONO if (WebConfigIgnoreCase) { if ((index = callSearcher.SearchLower(ref path)) >= 0) { call(index, socket); return; } rewritePath = rewritePaths.GetLower(ref path); } else #endif { if ((index = callSearcher.Search(ref path)) >= 0) { call(index, socket); return; } rewritePath = rewritePaths.Get(ref path); } if (rewritePath != null) { Http.Response response = null; file(header, file(rewritePath, (header.Flag & Http.HeaderFlag.IsSetIfModifiedSince) == 0 ? new SubArray <byte>() : header.IfModifiedSince, ref response, false), ref response); if (response != null) { socket.ResponseIdentity(ref response); return; } socket.ResponseErrorIdentity(Http.ResponseState.NotFound404); } } else { #if !MONO if (WebConfigIgnoreCase) { if ((index = rewriteSearcher.SearchLower(ref path)) >= 0) { request(index, socket); return; } if ((index = viewSearcher.SearchLower(ref path)) >= 0) { request(index, socket); return; } if ((index = callSearcher.SearchLower(ref path)) >= 0) { call(index, socket); return; } } else #endif { if ((index = rewriteSearcher.Search(ref path)) >= 0) { request(index, socket); return; } if ((index = viewSearcher.Search(ref path)) >= 0) { request(index, socket); return; } if ((index = callSearcher.Search(ref path)) >= 0) { call(index, socket); return; } } } if (beforeFile(socket)) { base.Request(socket); } }
/// <summary> /// HTTP请求处理[TRY] /// </summary> /// <param name="socket">HTTP套接字</param> public override unsafe void Request(Http.SocketBase socket) { Http.Header header = socket.HttpHeader; if (((uint)header.ContentLength | (uint)header.IsBoundary) == 0) { Http.Response response = Http.Response.Get(); SubBuffer.PoolBufferFull buffer = header.Buffer; byte[] domain = socket.IsSsl ? sslLocationDomain : locationDomain, bufferArray = buffer.Buffer; BufferIndex uri = header.UriIndex; int length = domain.Length + uri.Length; if (uri.Length != 0) { if (bufferArray[buffer.StartIndex + uri.StartIndex] == '/') { uri.Next(); if (uri.Length == 0) { goto END; } --length; } if (length <= header.HeaderEndIndex) { int startIndex = uri.StartIndex - domain.Length; if (startIndex >= 0) { Buffer.BlockCopy(domain, 0, bufferArray, startIndex += buffer.StartIndex, domain.Length); response.SetLocation(bufferArray, startIndex, length, locationState); } else { Buffer.BlockCopy(bufferArray, buffer.StartIndex + uri.StartIndex, bufferArray, buffer.StartIndex + domain.Length, uri.Length); Buffer.BlockCopy(domain, 0, bufferArray, buffer.StartIndex, domain.Length); response.SetLocation(bufferArray, buffer.StartIndex, length, locationState); } socket.ResponseIdentity(ref response); return; //int endIndex = uri.EndIndex; //if (header.HeaderEndIndex - endIndex - 7 >= length) //{ // fixed (byte* bufferFixed = bufferArray) // { // byte* bufferStart = bufferFixed + buffer.StartIndex, write = bufferStart + endIndex; // Memory.SimpleCopyNotNull64(domain, write, domain.Length); // Memory.SimpleCopyNotNull64(bufferStart + uri.StartIndex, write + domain.Length, uri.Length); // } // response.SetLocation(bufferArray, buffer.StartIndex + endIndex, length, locationState); // socket.Response(socketIdentity, ref response); // return; //} } } END: response.SetLocation(domain, locationState); socket.ResponseIdentity(ref response); } else { socket.ResponseErrorIdentity(Http.ResponseState.BadRequest400); } }
private void ParseTasksData(Http.Response response) { _simulationData = JsonConvert.DeserializeObject <SimulationData>(response.ResponseText); }
/// <summary> /// HTTP文件请求处理 /// </summary> /// <param name="header">请求头部</param> /// <param name="response">HTTP响应输出</param> /// <returns>文件缓存</returns> protected unsafe FileCache file(Http.Header header, ref Http.Response response) { SubArray <byte> path = header.Path; string cacheFileName = null; try { if (path.Length != 0 && WorkPath.Length + path.Length <= AutoCSer.IO.File.MaxFullNameLength) { byte[] contentType = null; bool isCompress = true; fixed(byte *pathFixed = path.GetFixedBuffer()) { byte *pathStart = pathFixed + path.Start, pathEnd = pathStart + path.Length; if (isFile(pathEnd, ref contentType, ref isCompress) == 0) { if (*pathStart == '/') { ++pathStart; } for (byte *formatStart = pathStart; formatStart != pathEnd; ++formatStart) { if (*formatStart == ':') { response = Http.Response.Blank; return(null); } #if !MONO if ((uint)(*formatStart - 'A') < 26) { *formatStart |= 0x20; } #endif } int cachePathLength = (int)(pathEnd - pathStart); FileCacheKey cacheKey = new FileCacheKey(pathIdentity, path.Array, (int)(pathStart - pathFixed), cachePathLength); FileCache fileCache = FileCacheQueue.Get(ref cacheKey); if (fileCache == null) { cacheFileName = StringExtension.FastAllocateString(WorkPath.Length + cachePathLength); fixed(char *nameFixed = cacheFileName) { char *write = nameFixed + WorkPath.Length; char directorySeparatorChar = Path.DirectorySeparatorChar; StringExtension.CopyNotNull(WorkPath, nameFixed); for (byte *start = pathStart; start != pathEnd; ++start) { *write++ = *start == '/' ? directorySeparatorChar : (char)*start; } } FileInfo file = new FileInfo(cacheFileName); bool isFileExists = file.Exists, isCopyPath = true; if (!isFileExists && cacheFileName.IndexOf('%') >= WorkPath.Length) { cacheKey.CopyPath(); isCopyPath = false; string newPath = AutoCSer.Net.Http.Header.UnescapeUtf8(pathStart, cachePathLength, path.Array, (int)(pathStart - pathFixed)); if (Path.DirectorySeparatorChar != '/') { newPath.replaceNotNull('/', Path.DirectorySeparatorChar); } FileInfo newFile = new FileInfo(WorkPath + newPath); if (newFile.Exists) { file = newFile; isFileExists = true; } } if (isFileExists) { string fileName = file.FullName; if (fileName.Length > WorkPath.Length && WorkPath.equalCaseNotNull(fileName, WorkPath.Length)) { if (fileName.Length <= AutoCSer.IO.File.MaxFullNameLength && file.Length <= FileCacheQueue.MaxFileSize) { if (FileCacheQueue.Get(ref cacheKey, out fileCache, isCopyPath) != 0) { try { fileCache.LastModified = file.LastWriteTimeUtc.UniversalNewBytes(); int extensionNameLength = (int)(pathEnd - getExtensionNameStart(pathEnd)); SubArray <byte> fileData = readCacheFile(new SubString { String = fileName, Start = fileName.Length - extensionNameLength, Length = extensionNameLength }); FileCacheQueue.Set(ref cacheKey, fileCache, fileCache.Set(ref fileData, contentType, cacheControl, isCompress)); if ((header.Flag & Http.HeaderFlag.IsSetIfModifiedSince) != 0 && header.IfModifiedSinceIndex.Length == fileCache.LastModified.Length) { if (AutoCSer.Memory.Common.EqualNotNull(fileCache.LastModified, pathFixed + header.Buffer.StartIndex + header.IfModifiedSinceIndex.StartIndex, header.IfModifiedSinceIndex.Length)) { response = Http.Response.NotChanged304; return(null); } } } finally { if (fileCache.IsData == 0) { fileCache.PulseAll(); fileCache = null; FileCacheQueue.RemoveOnly(ref cacheKey); } } } } else { if ((header.Flag & Http.HeaderFlag.IsSetIfModifiedSince) != 0 && header.IfModifiedSinceIndex.Length == Date.ToByteLength && Date.UniversalByteEquals(file.LastWriteTimeUtc, header.IfModifiedSince) == 0) { response = Http.Response.NotChanged304; return(null); } response = Http.Response.Get(); //response.State = Http.ResponseState.Ok200; response.SetBodyFile(file); response.CacheControl = cacheControl; response.ContentType = contentType; response.SetLastModified(file.LastWriteTimeUtc.UniversalNewBytes()); return(null); } } } } return(fileCache); } } } } catch (Exception error) { RegisterServer.TcpServer.Log.Exception(error, cacheFileName, LogLevel.Exception | LogLevel.AutoCSer); } return(null); }