# HTTP Proxy Encapsulator v10/v11

**URL:** <https://community.f5.com/t/http-proxy-encapsulator-v10-v11/66174>\
**Category:** Community CodeShare\
**Tags:** irules, devops\
**Created:** [March 18, 2015, 6:35pm UTC](https://community.f5.com/t/http-proxy-encapsulator-v10-v11/66174 "2015-03-18T18:35:01Z")\
**Posts on this page:** 3\
**Page:** 1

<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:** [March 18, 2015, 6:35pm UTC](https://community.f5.com/t/http-proxy-encapsulator-v10-v11/66174/1 "2015-03-18T18:35:01Z")

</div>

**Problem this snippet solves:**

Summary: Allows certain applications to use the HTTP proxy which normally otherwise do not have way to point to the proxy

## Contribution

- Bhattman at gmail dot com (original version)
- michel.belleau at malaiwah dot com (updated version with fixes for more generic protocol support, i.e.: protocols that do not wait for servers answer to request)

## Use Case

Blackberry Enterprise Server does not have proxy settings. However, it does need to access Blackberry BES system on the internet. Thus it can then simply aim a particular IP address either via hostname or the application itself. The IP Address in this case is a :any VIP. The VIP will run the iRule below. The VIP will contain a pool associated to SQUID or Ironport OR Webtrends OR Bluecoat proxy.

Please post questions or fixes for this iRule in iRules forum to get the fastest response

**Code :**

```auto
# Tested on v10.2 version and up

when RULE_INIT {
    # Change to "1" to enable debugging log statements
    set static::proxydebug 0
}

when CLIENT_ACCEPTED {
    if { $static::proxydebug != 0 } { log local0. "Client connected" }
    set bypass 0
    set bufferdata ""
    # <skipbytes> 0 to make sure the server-side connection is opened right away
    TCP::collect 0 0
}

when CLIENT_DATA {
    if { $static::proxydebug != 0 } { log local0. "CLIENT_DATA before is |[TCP::payload]|" }
    
    # accumulate until ready, release when connected
    if { $bypass eq 1 } {
         TCP::payload replace 0 [string length $bufferdata] ""
        TCP::release   
        return
    }
    set bufferdata [TCP::payload]
    TCP::collect
}

when SERVER_CONNECTED {
    serverside {TCP::respond "CONNECT <hostname fqdn="" or="" ip="" address="">:[TCP::local_port clientside] HTTP/1.0\r\n\r\n"}
    TCP::collect
}

when SERVER_DATA {
    if { $bypass eq 1 } {
        TCP::release
        return
    }
    if { $static::proxydebug != 0 } { log local0. "PAYLOAD before is |[TCP::payload]|" }

    # You might need HTTP/1.1 for your proxy, my version of squid was 1.0 
    if { [TCP::payload] starts_with "HTTP/1.0 200 Connection established\r\n\r\n" } {
        TCP::payload replace 0 39 ""
        if { $static::proxydebug != 0 } { log local0. "PAYLOAD after is |[TCP::payload]|" }

        TCP::respond $bufferdata
        TCP::release
         
        set bypass 1

    } else {
        TCP::close
    }  
}</hostname></skipbytes>

```

**Tested this on version:**

10.2

---

<div class="post-metadata">

**Author:** ![NiHo\_202842](https://avatars.discourse-cdn.com/v4/letter/n/9de053/32.png) [@NiHo\_202842](https://community.f5.com/u/NiHo_202842)\
**Post date:** [June 17, 2015, 7:46am UTC](https://community.f5.com/t/http-proxy-encapsulator-v10-v11/66174/2 "2015-06-17T07:46:35Z")

</div>

My CLIENT\_DATA before shows just gibberish. Any ideas?

---

<div class="post-metadata">

**Author:** ![Ernesto\_Mayol\_2](https://avatars.discourse-cdn.com/v4/letter/e/3ab097/32.png) [@Ernesto\_Mayol\_2](https://community.f5.com/u/Ernesto_Mayol_2)\
**Post date:** [January 17, 2018, 11:15am UTC](https://community.f5.com/t/http-proxy-encapsulator-v10-v11/66174/3 "2018-01-17T11:15:36Z")

</div>

I have not had any luck getting this code to work. Could you share information on the following:

- Is this code only useful to proxy http traffic?
- If https can also be proxied with this irule, could you share any details on the configuration required on the Virtual Server where this irule will be associated?

Thanks
