# Header Insert of "http" or "https"

**URL:** <https://community.f5.com/t/header-insert-of-http-or-https/26987>\
**Category:** Technical Forums\
**Tags:** dev, irules, devops, application-delivery\
**Created:** [December 3, 2009, 10:33pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987 "2009-12-03T22:33:17Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![raytoles\_75680](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@raytoles\_75680](https://community.f5.com/u/raytoles_75680)\
**Post date:** [December 3, 2009, 10:33pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/1 "2009-12-03T22:33:17Z")

</div>

We’re going to implement a virtual server to handle HTTP and HTTPS requests. The developers need us to insert the protocol into the header. We’re still new to irules and I constantly fear performance issues because of irules. So I’m submitting what I have for some input.

```tcl
when HTTP_REQUEST { 
 if {[TCP::local_port] equals 80}{ 
 HTTP::header insert X-Forwarded-Proto "http" 
 log local0. "HTTP request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]" 
  
  
 } elseif {[TCP::local_port] equals 443}{ 
   HTTP::header insert X-Forwarded-Proto "https" 
 log local0. "HTTPS request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]"   
 } 
 }

```

---

<div class="post-metadata">

**Author:** ![The\_Bhattman](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/the_bhattman/32/13958_2.png) [@The\_Bhattman](https://community.f5.com/u/The_Bhattman)\
**Post date:** [December 4, 2009, 5:57am UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/2 "2009-12-04T05:57:25Z")

</div>

Looks good from what I can tell. The only thing I would suggest is to check for the existence of the header. This way you are not always inserting for returning connections that already have it.

```tcl
when HTTP_REQUEST {  
  if {([TCP::local_port] ==80) and !( [HTTP::header "X-Forwarded-Proto"] eq "http") }{  
  HTTP::header insert X-Forwarded-Proto "http"  
          log local0. "HTTP request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]"  
         } elseif {([TCP::local_port] ==443) and !( [HTTP::header "X-Forwarded-Proto"] eq "https") } {  
          HTTP::header insert X-Forwarded-Proto "https"  
          log local0. "HTTPS request from Client IP: [IP::client_addr] to [IP::local_addr]:[TCP::local_port]"    
  }  
  }

```

I hope this helps

Bhattman

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [December 4, 2009, 8:45am UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/3 "2009-12-04T08:45:22Z")

</div>

A client wouldn’t include a response header in a subsequent request, so the only time that header would exist is if the client injected it. You might actually want to remove any prior instance to ensure a malicious client couldn’t forge the header.

If the VIP is defined on port 0, you might also want to drop any non-80 or non-443 traffic. You can also do the check once in CLIENT\_ACCEPTED instead of on each HTTP request:

```tcl
when CLIENT_ACCEPTED { 
  
     Check the requested port 
    switch [TCP::local_port] { 
       80 { 
          set proto http 
       } 
       443 { 
          set proto https 
       } 
       default { 
           Drop the request 
  drop 
       } 
    } 
 } 
 when HTTP_REQUEST { 
  
     Replace the X-Forwarded-Proto header if it exists 
      If it does not exist, a new instance will be inserted 
    HTTP::header replace X-Forwarded-Proto $proto 
 }

```

Aaron

---

<div class="post-metadata">

**Author:** ![raytoles\_75680](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@raytoles\_75680](https://community.f5.com/u/raytoles_75680)\
**Post date:** [December 4, 2009, 11:05am UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/4 "2009-12-04T11:05:42Z")

</div>

Aaron,

I will be using one of your irules for the HTTPandHTTPS virtual server inconjunction with the XForwardedProto irule.

[http://devcentral.f5.com/wiki/default.aspx/iRules/HttpHttpsSingleVirtualServer.html](http://devcentral.f5.com/wiki/default.aspx/iRules/HttpHttpsSingleVirtualServer.html)

---

<div class="post-metadata">

**Author:** ![raytoles\_75680](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@raytoles\_75680](https://community.f5.com/u/raytoles_75680)\
**Post date:** [December 4, 2009, 12:01pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/5 "2009-12-04T12:01:29Z")

</div>

Everything is good but Safari is doing something weird. It’s appending the url with :443 when it’s an http request, looks like this [http://newsite.apa.org:443](http://newsite.apa.org:443). Might be because the application is doing some switching between http and https. All the rest of the browsers are working w/o any problems.

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [December 4, 2009, 12:17pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/6 "2009-12-04T12:17:03Z")

</div>

You could add the XForwardedProto logic to the single VIP iRule by setting the proto variable to https in this section:

Request was to an HTTPS port, so do nothing for the clientside connection.

And setting it to http for in this section:

Request was to an HTTP port, not an HTTPS port, so disable client SSL profile if one is enabled on the VIP

I’d guess that Safari is getting a redirect to [http://newsite.apa.org:443](http://newsite.apa.org:443) and not actually generating that request itself. You could check this by logging the Location header in redirects or by using a browser plugin to log HTTP request/response headers and payloads. If it is coming from the app, you could add a check in the HTTP\_RESPONSE event to look for [HTTP::is\_redirect]==1 and then rewrite the Location header value using HTTP::header replace Location $new\_location.

Aaron

---

<div class="post-metadata">

**Author:** ![raytoles\_75680](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@raytoles\_75680](https://community.f5.com/u/raytoles_75680)\
**Post date:** [December 4, 2009, 11:36pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/7 "2009-12-04T23:36:48Z")

</div>

Thank YOU kind sirs! I added the following to http\_response and it resolved our Safari issue.

```tcl
when HTTP_RESPONSE { 
 if { [HTTP::header Location] starts_with "http://newsite.apa.org:443"} { 
 log local0. "Location [HTTP::header Location]" 
 HTTP::header replace Location "http://newsite.apa.org" 
 } 
 }

```

---

<div class="post-metadata">

**Author:** ![hoolio](https://avatars.discourse-cdn.com/v4/letter/h/f6c823/32.png) [@hoolio](https://community.f5.com/u/hoolio)\
**Post date:** [December 7, 2009, 10:18am UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/8 "2009-12-07T10:18:58Z")

</div>

It would be more efficient to only check for the Location header on redirects. Also, if you want to preserve the URI in the redirect, you should use string map:

```tcl
when HTTP_RESPONSE { 
    if { [HTTP::is_redirect] and [HTTP::header Location] starts_with "http://newsite.apa.org:443"} { 
       log local0. "Location [HTTP::header Location]" 
       HTTP::header replace Location "[string map "http://newsite.apa.org:443 http://newsite.apa.org" [HTTP::header Location]]" 
    } 
 }

```

Aaron

---

<div class="post-metadata">

**Author:** ![raytoles\_75680](https://avatars.discourse-cdn.com/v4/letter/r/6bbea6/32.png) [@raytoles\_75680](https://community.f5.com/u/raytoles_75680)\
**Post date:** [December 7, 2009, 11:12am UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/9 "2009-12-07T11:12:03Z")

</div>

This exactly what I did once the developers returned with an issue involving the need to preserve the URI.

Took a hint from another iRule I found on DevCentral [http://devcentral.f5.com/wiki/default.aspx/iRules/RewriteHTTPRedirectPort.html](http://devcentral.f5.com/wiki/default.aspx/iRules/RewriteHTTPRedirectPort.html).

---

<div class="post-metadata">

**Author:** ![Nick\_T\_68319](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Nick\_T\_68319](https://community.f5.com/u/Nick_T_68319)\
**Post date:** [September 7, 2010, 9:23pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/10 "2010-09-07T21:23:21Z")

</div>

I did something similar, except with the HTTP profile… i have one profile for non-ssl VS and one with ssl VS. Then each profile does a header insert that is basically SSL true or false, and the application reads this header.

---

<div class="post-metadata">

**Author:** ![Puli](https://avatars.discourse-cdn.com/v4/letter/p/9d8465/32.png) [@Puli](https://community.f5.com/u/Puli)\
**Post date:** [September 14, 2010, 4:06pm UTC](https://community.f5.com/t/header-insert-of-http-or-https/26987/11 "2010-09-14T16:06:03Z")

</div>

I tried the above code, but for some reason, the Location is outputed as empty in the log.

Any ideas?
