How to transfer image from server to client with node http header size restrictions

177 views Asked by At

Transferring image (base64 encoded, created with Mapguide server) to client. I am able to output the image to the console and test it is correct. Using Node with npm and Vite for develpment web server. When I try to set imgLegend.src = data; I get this error "431 (Request Header Fields Too Large)" I believe it is the Node default max-http-header-size causing the problem. Have attempted to set --max-http-header-size=80000 with no luck. I am starting my dev server in package.json file like this: "start": "vite --host 0.0.0.0",

Does anyone know of a way around this or a better way to transfer the image from server to client? here is the relevant code.

Client side:

//add legend
    const mapVpHeight = document.getElementById('map').clientHeight;
    var url = mgServer + "/Cid_Map/LayerManager.aspx/GetLegendImage";
    var values = JSON.stringify({ sessionId: sessionId, mgMapName: mapName, mapVpHeight: mapVpHeight });
    var imgLegend = new Image();
    //console.log(values);
    $.ajax({
        url: url,
        type: "POST",
        contentType: "application/json; charset=utf-8",
        data: values,
        dataType: 'html',
        success: function (data) {
            console.log(data); //
            imgLegend.src = data;  //node.js won't allow http header as large as this image, about 18kb
        },
        error: function (xhr, textStatus, error) {
            console.log(textStatus);
        }
    });

Server Side:

[WebMethod]
    public static string GetLegendImage(string sessionId, string mgMapName, int mapVpHeight)
    {
        string tempDir = System.Configuration.ConfigurationManager.AppSettings["tempDir"];
        string legFilePath = tempDir + sessionId + "Legend.png";

        string configPath = @"C:\Program Files\OSGeo\MapGuide\Web\www\webconfig.ini";
        MapGuideApi.MgInitializeWebTier(configPath);

        MgUserInformation userInfo = new MgUserInformation(sessionId);
        MgSiteConnection siteConnection = new MgSiteConnection();
        siteConnection.Open(userInfo);
        MgMap map = new MgMap(siteConnection);
        MgResourceService resourceService = (MgResourceService)siteConnection.CreateService(MgServiceType.ResourceService);
        map.Open(resourceService, mgMapName);

        MgColor color = new MgColor(226, 226, 226);

        MgRenderingService renderingService = (MgRenderingService)siteConnection.CreateService(MgServiceType.RenderingService);
        MgByteReader byteReader = renderingService.RenderMapLegend(map, 200, mapVpHeight, color, "PNG");
        MgByteSink byteSink = new MgByteSink(byteReader);
        byteSink.ToFile(legFilePath);
        //try this
        //byte[] buffer = new byte[byteReader.GetLength()]; //something doesn't work here byteReader doesn't give comeplete image
        //byteReader.Read(buffer, buffer.Length);
        //loading image file just created, converting to base64 image gives correct image
        string legendImageURL = "";
        using (Stream fs = File.OpenRead(legFilePath))
        {
            BinaryReader br = new System.IO.BinaryReader(fs);
            byte[] bytes = br.ReadBytes((int)fs.Length);
            string strLegendImage = Convert.ToBase64String(bytes, 0, bytes.Length);
            legendImageURL = "data:image/png;base64," + strLegendImage;
        }
        byteReader.Dispose();
        byteSink.Dispose();
        return legendImageURL;
        //return buffer;
    }
1

There are 1 answers

2
code1x1.de On

The 431 status code complains about the header length of your request ..

trace the request in your browsers dev tool network tab and study the header fields in your request in some special cases if your cookies get set to often with unique key value pairs this could be the problem...

May be you can copy and share the request response from your browsers network tab to provide some detailed information... especially the request response of the endpoint and look up the cookie/session storage maybe you find some suspicious stuff. Good look :)