HTTP Proxy JavaScript API
HTTP Proxy can use JavaScript to automate operation. To enable JavaScript, the Template Directory must exist and contain a file named defaultRules.js. This file is a JavaScript script that is run on startup and every time you choose Reload JavaScript from the Edit menu.
Any function defined in the file will then be available as an action in the Custom Rules editor. Whenever the corresponding rule matches, the function will be called (see Action Function for details).
Action Function
The action functions are called with three parameters:
Action Function Parameters
|
|
|
|
|
The response. This is an JSHttpResponse object. If the action was called for a request, this will be null. |
|
A boolean value. This is true if the message is a response and the request was held (stopped) by HTTP Proxy. |
The return value of the function is used as the actual action (as defined by Custom Rules). A null or undefined return value is treated as ACTION_NONE.
Scope
Functions are called sequentially in the order of message arrival. All the functions share a global, persistent, scope. Variables defined in the global scope will retain their values until you select Edit->Reload JavaScript; or until HTTP Proxy exits.
Global Properties and Methods
|
|
|
A constant; specifying no action should be taken (the next rule should be run). |
|
A constant; specifying that the message should be automatically passed and no further rules should be checked. |
|
A constant; specifying that the message should be held for the user to view or edit and no further rules should be checked. |
|
A constant; specifying that the message should be deleted and the connections to the server and browser closed. No further rules should be checked. |
|
A method causing a popup to appear (displaying msg). This is mainly useful for debugging. |
Objects
A number of special object types are defined:
JSHttpRequest
JSHttpResponse
JSHeader
JSReqCookie
JSResCookie
JSConnection
JSAddress
JSParam
SSLSession
Example
function someFunction(req, res, wasHeld) {
if (res == null) {
// this is a request
// Add a header to the request
req.headers["NewHeader"] = "SomeValue";
// Change a cookie
req.cookies["SomeCookie"] = "SomeCookieValue";
} else {
// this is a response
// if it has a cookie, change the path of the first one
if (res.cookies.length > 0) {
res.cookies[0].path = "/newpath";
}
}
if (wasHold) {
return ACTION_HOLD;
} else if (shouldPass) {
return ACTION_PASS;
} else if (shouldDelete) {
return ACTION_DELETE;
} else {
return ACTION_NONE;
}
}