1
votes

EDIT:

Simple version of the question: I want to create server variables in the .NET httpmodule and read them in my classic ASP code.

Is this possible? or the wrong approach?

ENDEDIT:

So I have taken over a classic asp application and the author wrote an ISASPI Filter dll and he used it to set server variables for his classic asp applications. I read on the IIS forums that the custom ISAPI filters are a bad idea and I should be using http modules if I'm going to move it forward.

So I pulled this method off of the internet that lets me set the server variables in my httpmodule, which seems to work for adding the item to the server variable collection... however I can't read it from my classic asp code.

Do I have the wrong approach?

       BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; 

        var context = app.Context;

        MethodInfo addStatic = null;
        MethodInfo makeReadOnly = null;
        MethodInfo makeReadWrite = null;

        Type type = app.Request.ServerVariables.GetType();
        MethodInfo[] methods = type.GetMethods(temp);

        foreach(var method in methods)
            {
                switch( method.Name)
                {
                    case "MakeReadWrite":
                        makeReadWrite = method;
                        break;
                    case "MakeReadOnly":
                        makeReadOnly = method;
                        break;
                    case "AddStatic":
                        addStatic = method;
                        break;
                }
            }

        makeReadWrite.Invoke(context.Request.ServerVariables, null);
        string[] values = { "DaveVar", "hehehe" };
        addStatic.Invoke(context.Request.ServerVariables, values);
        makeReadOnly.Invoke(context.Request.ServerVariables, null);

Which seems to set them correctly; however, when I try to read them from my classic asp page they do not appear...

CLASSIC ASP:

<html>
<%
  for each x in Request.ServerVariables
        response.write("<h2>"& x & "</h2>")
  next
%>
<h2>hello!</h2>
</html>

ASP.NET ASPX Page where they do appear:

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<% 
    foreach (var x in Request.ServerVariables)
   {
     %>
     <div>
     <%= x.ToString() %>
     </div>
     <%
   }
        %>    
    </div>
    </form>
</body>
</html>

full http module:

namespace PlatoModules 
{
    public class PlatoModule : IHttpModule
    {

        public void Init(HttpApplication context)
        {
            context.BeginRequest += (s, e) => BeginRequest(s, e);
            context.EndRequest += (s, e) => EndRequest(s, e); 
        }

        public String ModuleName
        {
            get { return "test"; }
        }

        public void Dispose()
        {
        }

        private void BeginRequest(Object source,
         EventArgs e)
        {
               HttpApplication application = (HttpApplication)source;
                HttpContext context = application.Context;
            try
            {
                System.Diagnostics.Debugger.Launch();
                // Create HttpApplication and HttpContext objects to access
                // request and response properties.
                string filePath = context.Request.FilePath;
                string fileExtension =
                    VirtualPathUtility.GetExtension(filePath);
                /*      if (fileExtension.Equals(".aspx"))
                      {
                          context.Response.Write("<h1><font color=red>" +
                              "HelloWorldModule: Beginning of Request" +
                              "</font></h1><hr>");
                      }*/

                BlackMagicSetServerVariables(application);
                if (fileExtension.Equals(".asp"))
                {
                    string content = @"<h1><font color=red>" +
                        "BeginReq" +
                        @"</font></h1><br>";
                    context.Response.Write(content);
                    context.Response.Flush();
                }
            }
            catch (Exception ex)
            {
                context.Response.Write(@"<h1><font color=red>" +
                        "error" + ex.Message +
                        @"</font></h1><br>");
            }
        }


        private void EndRequest(Object source, EventArgs e)
        {

            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            context.Response.Write(@"<br><h1><font color=red>" +
                  @"Enter endreq </font></h1>");
            string filePath = context.Request.FilePath;
            string fileExtension =
                VirtualPathUtility.GetExtension(filePath);
            if (fileExtension.Equals(".asp"))
            {
                context.Response.Write(@"<br><h1><font color=red>" +
                    @"EndReq </font></h1>");
            }
            context.Response.Flush();
        }

        void BlackMagicSetServerVariables(HttpApplication app)
        {
            BindingFlags temp = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; 

            var context = app.Context;

            MethodInfo addStatic = null;
            MethodInfo makeReadOnly = null;
            MethodInfo makeReadWrite = null;

            Type type = app.Request.ServerVariables.GetType();
            MethodInfo[] methods = type.GetMethods(temp);

            foreach(var method in methods)
                {
                    switch( method.Name)
                    {
                        case "MakeReadWrite":
                            makeReadWrite = method;
                            break;
                        case "MakeReadOnly":
                            makeReadOnly = method;
                            break;
                        case "AddStatic":
                            addStatic = method;
                            break;
                    }
                }

            makeReadWrite.Invoke(context.Request.ServerVariables, null);
            string[] values = { "DaveVar", "hehehe" };
            addStatic.Invoke(context.Request.ServerVariables, values);
            makeReadOnly.Invoke(context.Request.ServerVariables, null);

        }

    }


}
2
You are using an integrated pipeline?AnthonyWJones
Yeah, in the application pool I have set the managed pipeline mode to IntegratedDave Hanson

2 Answers

1
votes
<%
for each x in Request.ServerVariables
  response.write(x & "<br />")
next
%>

The code sample you have for listing all server variables in Classic ASP is broken. It only gives you a list of all the available variables, but not their values.

Try this:

<%
for each x in Request.ServerVariables
  response.write(x & " = " & Request.ServerVariables(x) & "<br />")
next
%>
0
votes

Solution ...

Tentatively -

I cannot modify server variables and have them be 'picked up' by the classic asp code on the request; however I could get this code add in headers, so I'm going to use headers. If this is an awful way to be doing things please let me know in the comments! Thanks for helping me out Frank!

SO here's the code:

CLASSIC ASP:

<%


for each x in Request.ServerVariables
  Response.Write("<h2>"& x  & ":" & Request.ServerVariables(x) & "</h2>") 
next

 Response.Write("<h2> DAVE: " & Request.ServerVariables("HTTP_DAVE") & "</h2>")
 Response.Flush()
%>
<h2>hello!</h2>
</html>

HTTPMODULE:

namespace PlatoModules
{
    public class PlatoModule : IHttpModule
    {

        public void Init(HttpApplication context)
        {
            context.BeginRequest += (s, e) => BeginRequest(s, e);
            context.EndRequest += (s, e) => EndRequest(s, e);
        }

        public String ModuleName
        {
            get { return "test"; }
        }

        public void Dispose()
        {
        }

        // Your BeginRequest event handler.
        private void BeginRequest(Object source, EventArgs e)
        {
            Debugger.Launch();
            HttpApplication application = (HttpApplication)source;

            HttpContext context = application.Context;
            try
            {

                context.Response.Write(
                    "<h1><font color=red>HelloWorldModule: Beginning of Request</font></h1><hr>");

                context.Request.Headers.Add("DAVE", "DAVOLIO");
                context.Response.Flush();
            }
            catch (Exception ex)
            {
                context.Response.Write(
                    ex.Message);
            }
        }


        private void EndRequest(object source, EventArgs e)
        {
            HttpApplication application = (HttpApplication)source;
            HttpContext context = application.Context;
            var content = @"<hr><h1><font color=red>HelloWorldModule: End of Request</font></h1>";
            context.Response.Write(content);
            context.Response.Flush();
        }


    }


}

Relevent web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true"> 
        <add name="PlatoModule" type="PlatoModules.PlatoModule" /> 
        </modules>
...
    </system.webserver>