Previous Next

Thread: Changing HTTP Header before rewrite module

Last post 07-15-2008 6:22 AM by lonevvolf. 10 replies.

Average Rating Rate It (5)

RSS

Page 1 of 1 (11 items)

Sort Posts:

  • 07-03-2008, 2:37 AM

    Changing HTTP Header before rewrite module

    How can I change the HTTP header, specifically the cookies sent, before the rewrite module is invoked?  I am guessing that the module is hooked in at OnGlobalPreBeginRequest?  Does that mean I would have to write a native module to perform any header changes before the rewriting module executes?

  • 07-03-2008, 3:13 PM In reply to

    • ruslany
    • Top 25 Contributor
    • Joined on 07-01-2007, 7:38 PM
    • Redmond, WA
    • Posts 201

    Re: Changing HTTP Header before rewrite module

    The URL rewriting may happen at different stages in pipeline - it depends on what kind of rules are used.

    • Global rules (defined in <globalRules> collection) are evaluated at OnGlobalPreBeginRequest
    • Distributed rules (defined in <rules> collection) are evaluated at BeginRequest

    When using distributed rules, you can have a simple managed HTTP module hooked to OnGlobalPreBeginRequest event to set the HTTP headers before URL rewriting happens.

    I am curious about what is the scenario where one needs to alter HTTP headers before URL rewriting. Could you provide more details on that? Would it help if URL rewriter allowed you to set HTTP headers?

    http://ruslany.net
  • 07-04-2008, 3:07 AM In reply to

    Re: Changing HTTP Header before rewrite module

    Thanks for the tip, I'll try that out! 

    ruslany:

    I am curious about what is the scenario where one needs to alter HTTP headers before URL rewriting. Could you provide more details on that? Would it help if URL rewriter allowed you to set HTTP headers?

     

    I'm so glad you asked!  :)  The scenario is as follows, and a pretty common one, I think:

    We have a website, stores the current user language in the Session.  Naturally, this is the kiss of death for Google, because it immediately means all languages (except the default) will be completely ignored by the crawler, since the crawler can't really use Sessions.  Ok, so we have to somehow put the language in the URL.

    URL Rewriting seems ideal for this, so we would like to use the following logic (stick with me, it's a bit long-winded):

    1) Check if the language is already in the URL when it comes in (formatted as <domainname>/<lang>/Default.aspx for example).  If not, read customer's language from their cookie and redirect to the same URL with the language added in.  If the cookie isn't there, use default language to do the same.
    2) Read the language from the URL, and set the customer's cookie to that language, so we can read it out in the application later on.  This is important for two reasons:
      a) The customer should be able to set their language by surfing to the appropriate URL
      b) Google will use this method to access the website
    3) When the language is changed on the website itself, we will naturally send this back to the customer in the response cookies (though of course, this doesn't require the URL rewriting)

    So, we need a few features from the URL rewriting process:
    1) Ability to read the language out from the URL - check
    2) Ability to read the language out from the Cookies - check
    3) Ability to change the request Cookies, based on the URL - missing!

    I would love to see this added as a feature!  I think it could really improve the usefulness of the module, if the HTTP Headers were not read-only!

  • 07-04-2008, 6:25 AM In reply to

    Re: Changing HTTP Header before rewrite module

    I've done some more testing to this end, and I've come to the conclusion that something very odd is going on here.  I have written a managed module, which hooks in at BeginRequest (the earliest point for a managed module, as far as I know).  It simply edits the cookies in the Request header, and returns.  Incidentally, it is just above RewriteModule in the ordered module list in IIS.  Then, the rewriter module performs a simple rewrite, and what I see is that when the new request (instantiated by the rewriter module) gets to my application, the Cookies are exactly as before I changed them!  Is the rewriter module throwing away my changes?  Have I forgotten to do something?

  • 07-07-2008, 4:41 PM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 1,210

    Re: Changing HTTP Header before rewrite module

    Can you collect a "failed request tracing" log for the request in question?  Also, what happens if you take the rewriter out of the picture - does changing the Cookie header change the Cookie collection in asp.net?

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
  • 07-08-2008, 3:54 AM In reply to

    Re: Changing HTTP Header before rewrite module

    Ok, I've debugged it to death (including FREB, which only confirmed point 1 below), and here's what's happening:

    1) The execution order is correct.  My module is executed before the rewriter every time.

    2) When the initial request comes in with the language, it works perfectly.  I perform a regex match on the AppRelativeCurrentExecutionPath in my module, it sees the language, and sets the cookie.

    3) The Rewriter then rewrites the path (which had the language) to the path without the language.  Once this happens, the new request comes to my module again - WITHOUT the cookie which I added in step 2!

    This means, the modified cookies are not being handed over on a rewrite.  Indeed, taking the rewriter out (and using a normal path without the language, though setting one in my module) works as intended.

    Can you confirm this?  Is this as intended, or an issue?  I appreciate the quick responses and help!

  • 07-08-2008, 8:55 AM In reply to

    • anilr
    • Top 10 Contributor
    • Joined on 05-23-2006, 10:13 PM
    • Redmond, WA
    • Posts 1,210

    Re: Changing HTTP Header before rewrite module

    Can you post the freb log itself - use the compact view to cut-n-paste.

    Anil Ruia
    Senior Software Design Engineer
    IIS Core Server
  • 07-09-2008, 7:30 AM In reply to

    Re: Changing HTTP Header before rewrite module

    anilr:

    Can you post the freb log itself - use the compact view to cut-n-paste.

    Here is the FREB.  A few notes:

    My module can be seen at step 44.  Notice in step 16 that the COOKIE_CULTURE is "en" (from an earlier session), and the URL being opened uses "de", which my module will read out and set in the cookie.

    No. EventName Details Time
    1. i GENERAL_REQUEST_START
    SiteId="1", AppPoolId="DefaultAppPool", ConnId="1610612924", RawConnId="0", RequestURL="http://vie-dev03:80/Dakota/WebClient/de/SportsBook/Default.aspx", RequestVerb="GET"
    11:26:26.006
    2. — PRE_BEGIN_REQUEST_START
    ModuleName="RequestMonitorModule"
    11:26:26.006
    3. — PRE_BEGIN_REQUEST_END
    ModuleName="RequestMonitorModule", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:26.006
    4. — PRE_BEGIN_REQUEST_START
    ModuleName="IsapiFilterModule"
    11:26:26.006
    5. i FILTER_START
    FilterName="C:\Windows\Microsoft.NET\Framework64\v2.0.50727\aspnet_filter.dll"
    11:26:26.006
    6. i FILTER_PREPROC_HEADERS_START
    11:26:26.006
    7. — GENERAL_SET_REQUEST_HEADER
    HeaderName="AspFilterSessionId", HeaderValue="", Replace="true"
    11:26:26.006
    8. i FILTER_SET_REQ_HEADER
    HeaderName="AspFilterSessionId:", HeaderValue=""
    11:26:26.006
    9. i FILTER_PREPROC_HEADERS_END
    11:26:26.006
    10. i FILTER_END
    11:26:26.006
    11. — PRE_BEGIN_REQUEST_END
    ModuleName="IsapiFilterModule", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:26.006
    12. — PRE_BEGIN_REQUEST_START
    ModuleName="RewriteModule"
    11:26:26.006
    13. — PRE_BEGIN_REQUEST_END
    ModuleName="RewriteModule", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:26.006
    14. — PRE_BEGIN_REQUEST_START
    ModuleName="FailedRequestsTracingModule"
    11:26:26.006
    15. — PRE_BEGIN_REQUEST_END
    ModuleName="FailedRequestsTracingModule", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:26.006
    16. i GENERAL_REQUEST_HEADERS
    Headers="Cache-Control: no-cache
    Connection: Keep-Alive
    Accept: */*
    Accept-Encoding: gzip, deflate
    Accept-Language: de-at
    Cookie: __IW_COOKIE_CULTURE=en; __IW_BANNER_INFO=BANNER=BONUS_T; LASTLOGIN=LASTLOGIN=09.07.2008 10:02:12; LASTLOGINUSERNAME=LASTLOGINUSERNAME=catestergbp; ASP.NET_SessionId=er1pqoyilfprd2inceoizy55
    Host: vie-dev03
    User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0; .NET CLR 3.5.21022)
    UA-CPU: x86
    "
    11:26:26.022
    17. i URL_CACHE_ACCESS_START
    RequestURL="/Dakota/WebClient/de/SportsBook/Default.aspx"
    11:26:26.022
    18. i URL_CACHE_ACCESS_END
    PhysicalPath="", URLInfoFromCache="false", URLInfoAddedToCache="true", ErrorCode="The operation completed successfully.
     (0x0)"
    11:26:26.022
    19. i HANDLER_PRECONDITION_NOT_MATCH
    Name="rules-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    20. i HANDLER_PRECONDITION_NOT_MATCH
    Name="rules-64-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    21. i HANDLER_PRECONDITION_NOT_MATCH
    Name="xoml-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    22. i HANDLER_PRECONDITION_NOT_MATCH
    Name="xoml-64-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    23. i HANDLER_PRECONDITION_NOT_MATCH
    Name="AXD-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    24. i HANDLER_PRECONDITION_NOT_MATCH
    Name="PageHandlerFactory-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    25. i HANDLER_PRECONDITION_NOT_MATCH
    Name="SimpleHandlerFactory-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    26. i HANDLER_PRECONDITION_NOT_MATCH
    Name="HttpRemotingHandlerFactory-rem-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    27. i HANDLER_PRECONDITION_NOT_MATCH
    Name="HttpRemotingHandlerFactory-soap-ISAPI-2.0", Precondition="classicMode,runtimeVersionv2.0,bitness32"
    11:26:26.022
    28. i HANDLER_PRECONDITION_NOT_MATCH
    Name="AXD-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    29. i HANDLER_PRECONDITION_NOT_MATCH
    Name="PageHandlerFactory-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    30. i HANDLER_PRECONDITION_NOT_MATCH
    Name="SimpleHandlerFactory-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    31. i HANDLER_PRECONDITION_NOT_MATCH
    Name="WebServiceHandlerFactory-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    32. i HANDLER_PRECONDITION_NOT_MATCH
    Name="HttpRemotingHandlerFactory-rem-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    33. i HANDLER_PRECONDITION_NOT_MATCH
    Name="HttpRemotingHandlerFactory-soap-ISAPI-2.0-64", Precondition="classicMode,runtimeVersionv2.0,bitness64"
    11:26:26.022
    34. i GENERAL_GET_URL_METADATA
    PhysicalPath="C:\inetpub\wwwroot\Dakota\WebClient\de\SportsBook\Default.aspx", AccessPerms="513"
    11:26:26.022
    35. i VIRTUAL_MODULE_UNRESOLVED
    Name="RoleManager", Type="System.Web.Security.RoleManagerModule"
    11:26:28.788
    36. i VIRTUAL_MODULE_UNRESOLVED
    Name="AnonymousIdentification", Type="System.Web.Security.AnonymousIdentificationModule"
    11:26:28.788
    37. i VIRTUAL_MODULE_UNRESOLVED
    Name="UrlMappingsModule", Type="System.Web.UrlMappingsModule"
    11:26:28.788
    38. — NOTIFY_MODULE_START
    ModuleName="HttpCacheModule", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.788
    39. — NOTIFY_MODULE_END
    ModuleName="HttpCacheModule", Notification="BEGIN_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:28.788
    40. — NOTIFY_MODULE_START
    ModuleName="RequestFilteringModule", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.788
    41. — NOTIFY_MODULE_END
    ModuleName="RequestFilteringModule", Notification="BEGIN_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:28.788
    42. — NOTIFY_MODULE_START
    ModuleName="ConfigurationValidationModule", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.788
    43. — NOTIFY_MODULE_END
    ModuleName="ConfigurationValidationModule", Notification="BEGIN_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:28.788
    44. — NOTIFY_MODULE_START
    ModuleName="SetCookie", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.788
    45. — NOTIFY_MODULE_START
    ModuleName="IsapiFilterModule", Notification="MAP_PATH", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.788
    46. — NOTIFY_MODULE_END
    ModuleName="IsapiFilterModule", Notification="MAP_PATH", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:28.788
    47. — NOTIFY_MODULE_COMPLETION
    ModuleName="SetCookie", Notification="BEGIN_REQUEST", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:28.803
    48. — NOTIFY_MODULE_START
    ModuleName="RewriteModule", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:28.803
    49. i GENERAL_CHILD_REQUEST_START
    SiteId="1", RequestURL="http://vie-dev03/Dakota/WebClient/SportsBook/Default.aspx", RequestVerb="GET", RecursiveLevel="1"
    11:26:28.803
    50. i GENERAL_REQUEST_START
    SiteId="1", AppPoolId="DefaultAppPool", ConnId="1610612924", RawConnId="0", RequestURL="http://vie-dev03/Dakota/WebClient/SportsBook/Default.aspx", RequestVerb="GET"
    11:26:28.803
    51. — NOTIFY_MODULE_START
    ModuleName="RewriteModule", Notification="BEGIN_REQUEST", fIsPostNotification="false", fIsCompletion="true"
    11:26:33.600
    52. — NOTIFY_MODULE_END
    ModuleName="RewriteModule", Notification="BEGIN_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_FINISH_REQUEST"
    11:26:33.600
    53. — NOTIFY_MODULE_START
    ModuleName="AspNetFilterModule", Notification="LOG_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:33.600
    54. — NOTIFY_MODULE_END
    ModuleName="AspNetFilterModule", Notification="LOG_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:33.600
    55. — NOTIFY_MODULE_START
    ModuleName="Session", Notification="END_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:33.600
    56. — NOTIFY_MODULE_END
    ModuleName="Session", Notification="END_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:33.600
    57. — NOTIFY_MODULE_START
    ModuleName="FormsAuthentication", Notification="END_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:33.600
    58. — NOTIFY_MODULE_END
    ModuleName="FormsAuthentication", Notification="END_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:33.600
    59. — NOTIFY_MODULE_START
    ModuleName="Profile", Notification="END_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:33.600
    60. — NOTIFY_MODULE_END
    ModuleName="Profile", Notification="END_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:33.600
    61. — NOTIFY_MODULE_START
    ModuleName="ScriptModule", Notification="END_REQUEST", fIsPostNotification="false", fIsCompletion="false"
    11:26:33.600
    62. — NOTIFY_MODULE_START
    ModuleName="ScriptModule", Notification="END_REQUEST", fIsPostNotification="false", fIsCompletion="true"
    11:26:33.600
    63. — NOTIFY_MODULE_END
    ModuleName="ScriptModule", Notification="END_REQUEST", fIsPostNotificationEvent="false", NotificationStatus="NOTIFICATION_CONTINUE"
    11:26:33.600
    64. i GENERAL_REQUEST_END
    BytesSent="52507", BytesReceived="567", HttpStatus="200", HttpSubStatus="0"
    11:26:33.600

     

     

  • 07-14-2008, 5:04 AM In reply to

    Re: Changing HTTP Header before rewrite module

    Sorry to bug you, but was there any further news on this?  If it's an error on my side, I'd like to solve it as soon as possible, otherwise, if it's an issue with the rewriter, I know I can wait on the next CTP.  Thanks for the help!

  • 07-14-2008, 12:21 PM In reply to

    • DanielVL
    • Top 50 Contributor
    • Joined on 10-07-2006, 6:00 PM
    • Redmond, WA, USA
    • Posts 73

    Re: Changing HTTP Header before rewrite module

    Sorry about that, I hadn't answer because I still need to verify the following information; you will need to modify the raw cookie yourself (the Cookie header), it looks to me that ASP.NET is not storing the new cookie in the HTTP Request Context. Cookie is just a header so in your code you could read the header itself, that is Headers["HTTP_COOKIE"] (it will return a colon separated value), modify it and set it again. That worked to me.

    Daniel Vasquez Lopez
    IIS Team
  • 07-15-2008, 6:22 AM In reply to

    Re: Changing HTTP Header before rewrite module

    Editing it manually does the trick!  Many thanks for the help!

    On a side note, I'd just like to reiterate that in the rewriter, the ability to edit the headers would be extremely useful.

Page 1 of 1 (11 items)
Page view counter