/// <summary>Get the raw file data (based on the PowerUI FileProtocol system).</summary> public override void OnGetDataNow(ContentPackage package) { // The endpoint (this is e.g. /v1/hello): string endpointPath = package.location.pathname; if (endpointPath.StartsWith("/v1")) { if (!endpointPath.EndsWith("/")) { endpointPath += "/"; } // The request payload (can be null): Json.JSObject request = Json.JSON.Parse(package.requestText); package.responseHeaders["content-type"] = "application/json; charset=utf-8"; // Get an endpoint: Endpoint ep = Endpoint.Get(endpointPath); if (ep == null) { // Endpoint not found. package.Failed(404); } else { Response response = new Response(); // Pass the request to the endpoint: ep.OnRequest(package, response, request); // Output the response: package.ReceivedText(response.ToString()); } return; } // Serve a file from PowerTools. // The browser embedded in the Unity editor (not PowerUI) // is webkit which doesn't allow ajax to file:// uri's. if (!File.Exists(PowerToolsPath + endpointPath)) { // Doesn't exist: package.Failed(404); } else { // Currently always HTML files down here: package.responseHeaders["content-type"] = "text/html"; // Read the file: byte[] data = File.ReadAllBytes(PowerToolsPath + endpointPath); // Serve it up: package.ReceivedData(data, 0, data.Length); } }
/// <summary>Checks if headers are available yet and handles them if they are.</summary> private void HandleHeaders() { #if UNITY_2017_1_OR_NEWER // Back to the old format, which unfortunately drops headers like Set-Cookie and builds the header set repeatedly. var headers = WWWRequest.GetResponseHeaders(); if (headers == null || headers.Count == 0) { return; } Package.responseHeaders.LoadFrom(headers, (int)WWWRequest.responseCode); Package.ReceivedHeaders(); bool redirect = false; #else string rawHeaderString = ResponseHeaderString; if (string.IsNullOrEmpty(rawHeaderString)) { // Not available yet. return; } // Received headers: bool redirect = Package.ReceivedHeaders(rawHeaderString); #endif if (redirect) { // Redirection. We'll most likely be making another request, unless we've redirected too many times: RedirectionCount++; if (RedirectionCount >= 20) { // Failed. Too many redirects. Package.statusCode = ErrorHandlers.TooManyRedirects; } else { // Redirect now (note that ready state was unchanged - redirects are supposed to be silent): Duration = 0f; // Get the location: string redirectedTo = Package.responseHeaders["location"]; // Set redir to: Package.redirectedTo = new Location(redirectedTo, location); // Get absolute: redirectedTo = Package.redirectedTo.absoluteNoHash; if (string.IsNullOrEmpty(redirectedTo) || redirectedTo.Trim() == "") { // Pop it from the update queue: Remove(); // Failed! Package.Failed(500); } else { if (Package.statusCode == 307 || Package.statusCode == 308) { // Resend as-is to the new URI: BeginRequest(redirectedTo, Package.request, RequestHeaders); } else { // GET request to the new URI: BeginRequest(redirectedTo, null, RequestHeaders); } return; } } } #if !MOBILE && !UNITY_WEBGL && !UNITY_2017_1_OR_NEWER && !UNITY_TVOS // We might be streaming video content. if (ContentType == "video/ogg") { Movie = WWWRequest.movie; } #endif }
/// <summary> /// Called when this endpoint receives a request. /// </summary> public virtual void OnRequest(ContentPackage package, Response output, JSObject request) { // Not implemented - error: package.Failed(500); }
/// <summary>Checks if headers are available yet and handles them if they are.</summary> private void HandleHeaders() { string rawHeaderString = ResponseHeaderString; if (string.IsNullOrEmpty(rawHeaderString)) { // Not available yet. return; } // Received headers: bool redirect = Package.ReceivedHeaders(rawHeaderString); if (redirect) { // Redirection. We'll most likely be making another request, unless we've redirected too many times: RedirectionCount++; if (RedirectionCount >= 20) { // Failed. Too many redirects. Package.statusCode = ErrorHandlers.TooManyRedirects; } else { // Redirect now (note that ready state was unchanged - redirects are supposed to be silent): Duration = 0f; // Get the location: string redirectedTo = Package.responseHeaders["location"]; // Set redir to: Package.redirectedTo = new Location(redirectedTo, location); // Get absolute: redirectedTo = Package.redirectedTo.absoluteNoHash; if (string.IsNullOrEmpty(redirectedTo) || redirectedTo.Trim() == "") { // Pop it from the update queue: Remove(); // Failed! Package.Failed(500); } else { if (Package.statusCode == 307 || Package.statusCode == 308) { // Resend as-is to the new URI: BeginRequest(redirectedTo, Package.request, RequestHeaders); } else { // GET request to the new URI: BeginRequest(redirectedTo, null, RequestHeaders); } return; } } } #if !MOBILE && !UNITY_WEBGL // We might be streaming video content. if (ContentType == "video/ogg") { Movie = WWWRequest.GetMovieTexture(); } #endif }