Apache proxy module: how to put back data read with ap_get_client_block()? - apache

I am modifying an Apache proxy module mod_proxy_http. I want to inspect the content prior to proxying the request. I am able to use ap_setup_client_block(), ap_should_client_block() and ap_get_client_block() in a loop to retrieve the client request message body. Unfortunately, it also depletes the message body and when the final message reached the API server, the message body is gone.
Is there a non-destructive version of ap_get_client_block() or is there a way to put back the content I read using ap_get_client_block()?

Related

Error in Response body in the redirection URL in Jmeter

I am trying to do a performance testing for a project where the main URL performs a redirection and the redirection URL returns the token needed to proceed further.
I recorded the script with Jmeter and i can see that the main URL does the redirection with status code as "302" as expected and has the "Location" header in the response body.
But when the redirection URL is hit after the main URL as the sub sample, it returns "307 temporary redirection" in response header and an error message in the response body as "java.lang.IllegalArgumentException: Missing location header in redirect for GET "
I checked the same scenario in Neoload and i could notice that request sent in Neoload for rediection URL is same as the one sent in JMeter, the response header also matches. But Neoload was able to display the response body with the expected token. In Jmeter, it returned the error message.
Can someone clarify why the response body is not displayed in Jmeter if the same request can work in Neoload. I made sure there is no difference in the request header/body sent for the main URL.
I am pretty sure the issue is related to Jmeter but not sure how to get the proper response body.
Maybe there is more than one redirect and JMeter doesn't follow it.
You can play with Redirect automatically and/or Follow redirects boxes in HTTP Request sampler
also you can use a third-party sniffer tool like Wireshark or Fiddler to inspect which requests are actually being sent, pay attention to literally everything: URL, headers, body, etc.
It should also be possible to disable handling redirects in JMeter completely and extracting the redirect target from the Location header using Regular Expression Extractor or Boundary Extractor and manually add the next request giving the extracted URL in the "Path" field of the HTTP Request sampler

How to pass original URI, with arguments, to Traefik ErrorPage handler specified in `query`?

I'm trying to use nginx to serve a custom error page using the Error Page middleware so that 404 requests to a lambda service (which I don't control) can be handled with a custom error page. I want to be able to get the context of this original request on that error page, either in Nginx for further forwarding, or else as a header for further handling e.g. in PHP or whatnot so I can provide contextual links on the 404 page.
However, right now after the redirection to Nginx in Traefik's ErrorPage middleware it seems the request has lost all the headers and data from the original service query.
The relevant part of my dockerfile:
traefik.port=8080
traefik.protocol=http
traefik.docker.network=proxy
traefik.frontend.rule=PathPrefix:/myservice;ReplacePathRegex:^/myservice/(.*) /newprefix/$$1
traefik.frontend.errors.myservice.status=404
traefik.frontend.errors.myservice.service=nginx
traefik.frontend.errors.myservice.query=/myservice-{status}
Nginx receives the forwarded 404 request, but the request URI comes through as nothing more than the path /myservice-404 specified in query (or /, if I omit traefik.frontend.errors.myservice.query). After the ReplacePathRegex I have the path of the original request available in the HTTP_X_REPLACED_PATH header, but any query arguments are no longer accessible in any header, and nginx can't see anything else about the original URI. For example, if I requested mysite.com/myservice/some/subpath?with=parameters, the HTTP_X_REPLACED_PATH header will show /myservice/some/subpath but not include the parameters.
Is it possible in Traefik to pass another service the complete context about the original request?
What I'm really looking for is something like try_files, where I could say "if this traefik request fails, try this other path instead", but I'd settle for being able to access the original, full request arguments within the handling backend server. If there was a way to send Nginx a request with the full path and query received by Traefik, that would be ideal.
tl;dr:
I am routing a request to a specific service in Traefik
If that request 404s, I want to be able to pass that request to Nginx for further processing / a contextual error page
I want Nginx and/or the page which receives the ErrorPage redirect to be able to know about the request that 404'd in the service
Unfortunately this is not possible with Traefik. I tried to achieve something similar but I realized that the only information that we are able to pass to the error page is the HTTP code, that's it.
The only options available are mentioned in their docs: https://doc.traefik.io/traefik/middlewares/errorpages/

Download from a http location using apache camel

I need to download a file from an http location to my local system using apache camel. When I gave the below code
from("http://url/filename.xml")
.to("file://C:location")
it has worked for ftp but not working when the url is "http". That is, it is not downloading the file from the http location to the local address provided in the "to()".
This should work.
from("direct:abc")
.setHeader("Accept", simple("application/xml"))//Change it according to the file content
.setHeader(Exchange.HTTP_METHOD, constant("GET"))
.to("http://url/filename.xml")
.to("file:///tmp/?fileName=yourFileName.xml");
You cannot use from("Some url"). Above route is triggered whenever there is a message on direct:abc endpoint. You can change the yourFileName.xml to whatever filename you want it to be stored as.
Instead of a trigger from route, you can as well use a timer or any other means of self triggering.
The reason you cannot consume from a rest enpoint like this
from("http://url/filename.xml")
is you cannot consume from http endpoint. So there needs to be a trigger. Infact the exception message is pretty clear when you do like this. It says
org.apache.camel.spring.boot.CamelSpringBootInitializationException: org.apache.camel.FailedToCreateRouteException: Failed to create route route1: Route(route1)[[From[http://url/filename.xml]] -> [To[... because of Cannot consume from http endpoint
the http component cannot be used as a consumer ie. you cannot have a route as from("http://...")
you need to use a consumer component that will start the route.
You could try something like this
from("timer:foo?fixedRate=true&period=5000")
.to("http://url/filename.xml")
.to("file://C:location")

How to modify the response header based on the content of response body in IBM http server?

I have a scenario in which the IHS server is used as a reverse proxy. Any request from the browser reaches the webserver (ie IHS) and IHS redirects the request to the corresponding URL. Similarly the response will return back to IHS and then to the browser. Now I need to include the user ID in the response header. This user ID which I need to include in the header needs to be obtained from the body of the response.
Please help if this is possible as I am new to IHS.
You can't do this in an Apache-based proxy server. Headers are sent before the body, and the body is not buffered and scanned for content in any standard Apache module.
If you need to set a response header to the logged in user, do it in your application.

how does one force apache to flush HTTP headers on POST requests

In my case I have an http client that works well when connecting directly with a Tomcat server that the clinet makes a POST request to the server and sends a large ammount of data as "chunked" html. Tomcat seems to flush the headers back to the client when the servlet responds to the request.
However in the case of an apache redirect, the desire is to get the headers without having to close the connection. In ither words have apache respond after recieving just the request headers and send it's reply when ithas recieved the requests headers ( but not yet the data )
how does one do this or is it one of the you can't do that things :)