Forum Discussion
HTTP Response from ILXPlugin
Hi DevCentral community!
I am currently trying to develop an iRule ILXPlugin, which checks a client HTTP request for certain properties. If some condiditions don't match I would like to send an HTTP response error message from the ILXPlugin to the client (without further forwarding to the backend). If everything is valid I want to forward it to the respective backend pool member.
Unfortunately, the ILXTransaction does not provide any real examples (other than the description) on how to use the methods respond and replaceBody to achieve this use case.
You can find my current code below, whereas I am trying to send a response with a custom html body and HTTP status code 404:
'use strict';
var f5 = require('f5-nodejs');
var plugin = new f5.ILXPlugin();
var options = new f5.ILXPluginOptions();
options.handleServerData = false;
options.handleServerResponse = false;
options.handleClientData = false;
options.handleClientOpen = true;
plugin.on('initialized', function () {
console.log('INITIALIZED');
});
plugin.on('connect', function(flow) {
var tmmID = flow.tmmId();
var clientSourceAddress = flow.client.remoteAddress;
flow.client.on('requestComplete', function(request) {
var destinationHost = request.params.headers.host;
var requestMethod = request.params.method;
// Example
if(requestMethod === 'POST') {
request.replaceBody();
request.respond();
// Send response with body: 'ERROR RESPONSE!' and Status Code '404'
flow.client.end();
}
var options = new f5.ILXLbOptions();
options.pool = '/Common/api_pool';
flow.lbSelect(options);
flow.client.allow();
request.complete();
});
flow.client.on('error', function(errorText) {
console.error('client error event: ' + errorText);
});
flow.server.on('error', function(errorText) {
console.error('server error event: ' + errorText);
});
flow.on('error', function(errorText) {
console.error('flow error event: ' + errorText);
});
});
plugin.start(options);
I would appreciate any suggestions.
Thank you in advance.
- Deyan_DonovskiNimbostratus
+2.5
It's the same problem for me too. flow.client.end or flow.client.write both send back HTTP result code 200 and I don't see a way to change this. Docs do mention that the response object has response.params.status but the scenario here is not to send the request to the backend but to reply directly from the LBA. - Dominik_312933Nimbostratus
Hi Niels!
Thanks a lot for your working example code.
Is there also an option to send a custom response (e.g. 404 Not Found) back to the client, instead of replying with 200 OK?
- gkor_novaNimbostratus
+1
I have the same question.
Is there any feture request for that?
Maybe you can use the example on page 49 of this document:
https://devcentral.f5.com/DesktopModules/Downloads/API/downloadservice/DownloadFile/242?version=340
flow.client.on("data", function(buffer) { flow.client.end( "HTTP/1.0 200 OK\r\n" + "Server: BigIP\r\n" + "Connection: Keep-Alive\r\n" + "Content-Length: " + 4 + "\r\n\r\n" + "abc\n"); });
Or you need to use flow.client.write(data);
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com