RonenNess / Serverito

Http framework for C# web apps.
MIT License
9 stars 0 forks source link

Linux headers #1

Open dzinks2009 opened 6 years ago

dzinks2009 commented 6 years ago

When I launch my app on linux, I get these errors:

NEW RAW REQUEST: /index/
URL matching: /index/
Before passing to view: /index/
Missing file: /index/
Undefined URL: /index/
Got Exception! System.InvalidOperationException: Cannot be changed after headers are sent.
  at System.Net.HttpListenerResponse.set_StatusCode (System.Int32 value) [0x00021] in <67546f875ae44445b5aba5311d41f9dc>:0
  at Serverito.ServeritoListener.ServeStaticFile (Serverito.ServeritoContext context, System.String path, System.Boolean serveHtml) [0x00044] in <388b7be8faae4f7490172500dd846de1>:0
  at Serverito.ServeritoListener.ServeHtmlPage (Serverito.ServeritoContext context, System.String path) [0x00000] in <388b7be8faae4f7490172500dd846de1>:0
  at Ts3Bot.Core+<>c__DisplayClass2_0.<Main>b__2 (Serverito.ServeritoContext context) [0x00000] in <388b7be8faae4f7490172500dd846de1>:0
  at Serverito.ServeritoListener.HandleRequest (Serverito.ServeritoContext context) [0x000b5] in <388b7be8faae4f7490172500dd846de1>:0

On the pc it works fine.

RonenNess commented 6 years ago

Hi @dzinks2009 thank you for the report.

If you take the following lines:

            // set and content disposition for serving html
            if (serveHtml)
            {
                context.Context.Response.Headers.Add("Content-Disposition", "inline;filename=\"" + filename + "\"");
            }
            // set and content disposition for file
            else
            {
                context.Context.Response.Headers.Add("Content-Disposition", "attachment;filename=\"" + filename + "\"");
            }

From function public void ServeStaticFile(ServeritoContext context, string path, bool serveHtml = false) (located in Serverito.cs) and move them to be right above the invoke callback line, so that the final function looks like this:

/// <summary>
        /// Serve a static file.
        /// Note: will close request even if 'CloseRequests' is set to false.
        /// </summary>
        /// <param name="context">Context containing the request to serve file for.</param>
        /// <param name="path">Path of file to serve, under 'StaticFilesPath'.</param>
        /// <param name="serveHtml">If true, instead of serving file as a file it will render it as an HTML page.</param>
        public void ServeStaticFile(ServeritoContext context, string path, bool serveHtml = false)
        {
            // static files path not defined? error
            if (StaticFilesPath == null)
            {
                throw new NullReferenceException("To serve static files you must set the 'StaticFilesPath' property.");
            }

            // get file full path
            path = System.IO.Path.Combine(StaticFilesPath, path);

            // file not found? return 404
            if (!System.IO.File.Exists(path))
            {
                // call missing file callback
                if (!InvokeCallbacks(OnMissingFile, context))
                    return;

                // set status code to not found and try closing request
                context.Context.Response.StatusCode = (int)HttpStatusCode.NotFound;
                Utils.TryCloseResponse(context.Context);
                return;
            }

            // set default content type
            context.Context.Response.ContentType = serveHtml ? "text/html" : "application/octet-stream";

            // get filename
            var filename = System.IO.Path.GetFileName(path);

            // set content type based on known mime types
            if (!serveHtml && SetMimeContentType)
            {
                context.Context.Response.ContentType = 
                    Utils.ExtensionToMimeType(System.IO.Path.GetExtension(path)) ?? context.Context.Response.ContentType;
            }

            // set encoding type
            if (StaticFilesEncodingType != EncodingType.Default)
            {
                context.Context.Response.ContentType += _encodingToCharsetString[(int)StaticFilesEncodingType];
            }

            // read file into response
            var fileContent = StaticFilesReader(path);
            context.Context.Response.OutputStream.Write(fileContent, 0, fileContent.Length);

            // set and content disposition for serving html
            if (serveHtml)
            {
                context.Context.Response.Headers.Add("Content-Disposition", "inline;filename=\"" + filename + "\"");
            }
            // set and content disposition for file
            else
            {
                context.Context.Response.Headers.Add("Content-Disposition", "attachment;filename=\"" + filename + "\"");
            }

            // call serving files callback
            if (!InvokeCallbacks(OnServingFile, context))
                return;

            // close response
            Utils.TryCloseResponse(context.Context);
        }

Does it fix the problem? If you could help me with this test it would be great!

Thanks :)

dzinks2009 commented 6 years ago

Yes, I could help you with testing. I will see right when I get home.

dzinks2009 commented 6 years ago

Nope, still not working. Got Exception! System.InvalidOperationException: Cannot be changed after headers are sent. at System.Net.HttpListenerResponse.set_StatusCode (System.Int32 value) [0x00021] in <67546f875ae44445b5aba5311d41f9dc>:0 at Serverito.ServeritoListener.ServeStaticFile (Serverito.ServeritoContext context, System.String path, System.Boolean serveHtml) [0x00044] in <6cdb2e9616634dd3ad997f8c3a2d599f>:0 at Serverito.ServeritoListener.ServeHtmlPage (Serverito.ServeritoContext context, System.String path) [0x00000] in <6cdb2e9616634dd3ad997f8c3a2d599f>:0 at Ts3Bot.Core+<>c__DisplayClass4_1.<Main>b__2 (Serverito.ServeritoContext context) [0x00000] in <6cdb2e9616634dd3ad997f8c3a2d599f>:0 at Serverito.ServeritoListener.HandleRequest (Serverito.ServeritoContext context) [0x000b5] in <6cdb2e9616634dd3ad997f8c3a2d599f>:0