1

I have created a Proxy script on my server. This Proxy script is standing between client and geoserver. But Proxy requires Authorize header from user. Openlayers application will send requests to Proxy with Authorize header.

I am setting default headers

OpenLayers.Request.DEFAULT_CONFIG.headers= { Authorization: "BASIC" + "123456" };

But server request header does not contain Authorization header that I set.

        var lines = new OpenLayers.Layer.WMS(
            "My Lines",
            "http://localhost:43561/geoserver/wms",
            { layers: "GIS:my_lines", transparent: true},
            { isBaseLayer: false, layerId: "paths", isFilterEnabled: true }
        );

This is my wms layer.

How can I set header values of OpenLayers request?

3
  • 1
    I don't think it's possible to manipulate headers in WMS requests in OpenLayers. This is because WMS layers are implemented simply as html IMG tags. There is no active AJAX-like Request mechanism involved. When I needed to do something similar, I created an ASP.NET custom handler. This stood between the client and geoserver, and added authentication data to the request. – drunkenwagoner Apr 17 '15 at 6:37
  • I read a cookie to check the user's credentials. Fortunately, cookies are sent with IMG requests. – drunkenwagoner Apr 17 '15 at 6:48
  • @drunkenwagoner I have a custom handler, but this handler is authenticating users via request Authorization parameter. So I can not get parameter from Openlayers client. Can you explain your handler mechanism? – barteloma Apr 17 '15 at 6:56
1

Here's the basic code for the handler. Additionally I would read a cookie from the context.Response object to identify the user and check that the request has come from a valid domain.

using System;
using System.IO;
using System.Net;
using System.Net.Cache;
using System.Web;

namespace Web
{
public class TileAsyncHandler : IHttpAsyncHandler
{
    private HttpWebRequest _request;
    private HttpContext _context;

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
    {
        IAsyncResult res = null;

        _context = context;

        string query = _context.Request.Url.GetComponents(UriComponents.Query, UriFormat.SafeUnescaped);

        string strURL = "http://localhost:8080/geoserver/wms";

        try
        {
            _request = (HttpWebRequest)WebRequest.Create(strURL + "?" + query);

            CredentialCache cc = new CredentialCache();

            cc.Add(new Uri(_context.Request.Url.GetLeftPart(UriPartial.Authority)), 
                "Digest", 
                new NetworkCredential("admin", "geoserver"));

            _request.Credentials = cc;

           res = _request.BeginGetResponse(cb, extraData);       
        }
        catch (Exception ex)
        {
            _context.Response.Write(ex.Message);
        }

        return res;
    }

    public void EndProcessRequest(IAsyncResult result)
    {
        HttpWebResponse response;

        try
        {
            response = (HttpWebResponse)_request.EndGetResponse(result);
        }
        catch (Exception)
        {
            _context.Response.StatusCode = 13;
            _context.Response.Write("Could not contact back-end site");
            _context.Response.End();
            return;
        }

        _context.Response.ContentType = response.ContentType;
        _context.Response.StatusCode = (int)response.StatusCode;
        _context.Response.StatusDescription = response.StatusDescription;

        foreach (string each in response.Headers)
        {
            if (each != "Location" && !WebHeaderCollection.IsRestricted(each))
            {
                _context.Response.AddHeader(each, response.Headers.Get(each));
            }
        }

        _context.Response.Clear();

        using (Stream receiveStream = response.GetResponseStream())
        {
            receiveStream.CopyTo(_context.Response.OutputStream);
        }
    }

    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        throw new NotImplementedException();
    }
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.