Introduction

This article covers different rule types’ spec and how each rule is internally stored in storage. This article is mainly intended for Requestly development team but we are open to suggestions from everyone. Please have a look at this and let us know if something can be improved.

Common Keys and Description

  • creationDate – epoch milliseconds. Used to display  `Created On` Column on Index Page. Also used for creating Id of rule
  • id – Rule Id ($RuleType_$creationDate)
  • fileName – File name of the file picked from library. Applicable only in Script and Redirect rule.

Redirect Rule Spec

Redirect Rule (v1)
redirect_rule_v1 = { 
    creationDate: 1404740537779,
    description: 'Testing Redirect Rule',
    destination: 'http://www.facebook.com',
    id: 'Redirect_1404740537779',
    name: 'Redirecting Google to FB',
    ruleType: 'Redirect',
    source: {
      key: 'Url',
      operator: 'Contains',
      values: ['google.com'],
    },
    status: 'Inactive'
  }
Redirect Rule (v2)

To support Multiple Redirect Entries within a single Redirect Rule, Move sourcedestination field inside pairs

redirect_rule_v2 = { 
   creationDate: 1404740537779,
   description: 'Testing Redirect Rule',
   id: 'Redirect_1404740537779',
   name: 'Redirecting Google to FB',
   ruleType: 'Redirect',
   status: 'Inactive',
   pairs: [
     {
       source: {
         key: 'Url',
         operator: 'Contains',
         value: 'mylibrary-1.2.1.js'
       },
       destination: 'http://www.goo.gl/aqsfg',
       fileName: 'mylibrary version 3.4'
     }
   ]
 }

Cancel / Request Blocker Rule Spec

There is no difference between Redirect Rule JSON and Cancel Rule JSON except there is no destination field in Cancel Rule.

Replace Rule Spec

Replace Rule v1
replace_rule_v1 = { 
   creationDate: 1426230518638,
   description: 'Shortcut for JIRA issues. Just write TGT-6638 in browser and see the magic',
   id: 'Replace_1426230518638',
   name: 'My Domain Name shortcuts',
   ruleType: 'Replace',
   status: 'Inactive',
   pairs: [
     {
       from: 'sachin:8880',
       status: 'Inactive',
       to: 'sachin.corp.example.com:8080' 
     },
     {
       from: 'http://appserver/',
       status: 'Inactive',
       to: 'app-mango.corp.example.com:8080' 
      }
   ]
 }
Replace Rule v2

Add source object inside each rule pairs and version field in rule.

replace_rule_v2 = { 
   creationDate: 1426230518638,
   description: 'Shortcut for JIRA issues. Just write TGT-6638 in browser and see the magic',
   id: 'Replace_1426230518638',
   name: 'My Domain Name shortcuts',
   ruleType: 'Replace',
   status: 'Inactive',
   version: 2,
   pairs: [
     {
        from: 'sachin:8880',
        status: 'Inactive',
        to: 'sachin.corp.example.com:8080',
        source: {
          key: 'Url',
          operator: 'Contains',
          value: 'http://sachin:8080/tweet-service'
        },
     },
     {
        from: 'http://appserver/',
        status: 'Inactive',
        to: 'app-mango.corp.example.com:8080',
        source: {
          key: 'Url',
          operator: 'Contains',
          value: ''
        }
      }
   ]
 }

User Agent Rule Spec

user_agent_rule = {
    "creationDate": 1494081686203,
    "description": "Load android mobile version of facebook website",
    "id": "UserAgent_1494081686203",
    "name": "facebook in android mobile",
    "pairs": [
      {
        "env": "android.phone",
        "envType": "device", // envType=device|browser|custom
        "source": {
          "key": "Url",
          "operator": "Contains",
          "requestType": "mainFrame", // requestType=mainFrame|pageRequest
          "value": "facebook.com"
        },
        "userAgent": "Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Mobile Safari/537.36"
      }
    ],
    "ruleType": "UserAgent",
    "status": "Active"
  }

Query Params Rule Spec

query_param_rule = { 
    creationDate: 1426230518638,
    description: 'Add Parameter to example.com',
    id: 'QueryParams_1426230518638',
    name: 'My Domain Name shortcuts',
    ruleType: 'QueryParam',
    status: 'Active',
    pairs: [
      {
        "modifications": [
          {
             "actionWhenParamExists": "Overwrite",
             "param": "a",
             "type": "Add",
             "value": 1
          }
        ],
        "source": {
          "key": "Host",
          "Operator": "Contains",
          "value": "example.com"
        }
      } 
    ]
  }

Notes:

  1. status field inside pairs is not used now.
  2. Empty value in source means it is ignored and rule can apply to any Url.
  3. With source field, you can constrain your replace functionality to certain Urls.
  4. Currently we have modifications inside each pair only in Query Param rule. Eventually we will support the same in Headers, Replace and Cancel Rule as well.

Leave a Reply

Your email address will not be published. Required fields are marked *