[GIS] OpenLayers: the proxy truncating response

openlayers-2PROXYwfs

I'm using OpenLayers on IIS 7.5 and I want to get my proxy (OpenLayers.ProxyHost) working on my localhost machine. So i copy/pasted the source for this example and have it running on localhost. However my WFS layer is not showing up.

I took a proxy code from here i am using:
http://code.google.com/p/iisproxy/issues/detail?id=8

Now everything seems to work fine except my response seems to be truncated. Using firebug i can see my request goes through perfect and my RESPONSE is truncated for some reason.

What's going on and HOW CAN I FIX THIS?

enter image description here
enter image description here
enter image description here

Here is my proxy code source: (I've commented out credential stuff as i'm using forms authentication).

<%@ WebHandler Language="C#" Class="proxy" %>


using System;
using System.Configuration;
using System.IO;
using System.Net;
using System.Web;

/// <summary>
/// adapted from: http://code.google.com/p/iisproxy
/// </summary>
public class proxy : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        var request = context.Request;
        var remoteUrl = request.QueryString["url"];

        var req = (HttpWebRequest)WebRequest.Create(remoteUrl);
        req.AllowAutoRedirect = false;
        req.Method = request.HttpMethod;
        req.ContentType = request.ContentType;
        req.UserAgent = request.UserAgent;
        //var basicPwd = ConfigurationSettings.AppSettings.Get("basicPwd");
        //req.Credentials = basicPwd == null ?
        //    CredentialCache.DefaultCredentials :
        //    new NetworkCredential(HttpContext.Current.User.Identity.Name, basicPwd);
        req.PreAuthenticate = true;
        req.Headers["Remote-User"] = HttpContext.Current.User.Identity.Name;
        foreach (string each in request.Headers)
            if (!WebHeaderCollection.IsRestricted(each) && each != "Remote-User")
                req.Headers.Add(each, request.Headers.Get(each));
        if (request.HttpMethod == "POST")
        {
            var outputStream = req.GetRequestStream();
            CopyStream(request.InputStream, outputStream);
            outputStream.Close();
        }

        HttpWebResponse response;
        try
        {
            response = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException we)
        {
            response = (HttpWebResponse)we.Response;
            if (response == null)
            {
                context.Response.StatusCode = 13;
                context.Response.Write("Could not contact back-end site");
                context.Response.End();
                return;
            }
        }

        context.Response.StatusCode = (int)response.StatusCode;
        context.Response.StatusDescription = response.StatusDescription;
        context.Response.ContentType = response.ContentType;
        //if (response.Headers.Get("Location") != null)
        //{
        //    var urlSuffix = response.Headers.Get("Location");
        //    if (urlSuffix.ToLower().StartsWith(ConfigurationSettings.AppSettings["ProxyUrl"].ToLower()))
        //        urlSuffix = urlSuffix.Substring(ConfigurationSettings.AppSettings["ProxyUrl"].Length);
        //    context.Response.AddHeader("Location", request.Url.GetLeftPart(UriPartial.Authority) + urlSuffix);
        //}
        foreach (string each in response.Headers)
            if (each != "Location" && !WebHeaderCollection.IsRestricted(each))
                context.Response.AddHeader(each, response.Headers.Get(each));

        CopyStream(response.GetResponseStream(), context.Response.OutputStream);
        response.Close();
        context.Response.End();
    }

    static public void CopyStream(Stream input, Stream output)
    {
        var buffer = new byte[1024];
        int bytes;
        while ((bytes = input.Read(buffer, 0, 1024)) > 0)
            output.Write(buffer, 0, bytes);
    }

    public bool IsReusable
    {
        get { return false; }
    }
}

Best Answer

I had the same issues and increased the byte size, see http://geographika.co.uk/a-proxy-for-iis-and-net for links to modified code and details.

Source in BitBucket at https://bitbucket.org/geographika/openlayers/src/tip/iisproxy/

Key change is:

    //updated to read gzip compressed streams correctly
    byte[] buffer = new byte[32768];

It also allows for PUT requests if you are using REST interfaces.