LWIP CGI handler - Sending data back to webpage instead of returning a filename - cgi

I'm running an LWIP http webserver on an STM32F4 discovery board. It displays a webpage when its IP address is entered on a web broswer. I need to send a string back to the webpage when the LWIP CGI handler is called. The javascript code in the html page is expecting this. how do I do that? The only thing I can send to the web page is a URL to another local html page.
here is the setup:
the project was created using the STM32 cubeIDE. Its using the LWIP stack with FreeRTOS. This web page sends a CGI command to the stm32 board using the http GET method. Then LWIP CGI handler is automatically called and executes. I can retrieve the data sent by the webpage.
This is the what the CGI handler looks like:
const char *my_cgi_handler(int iIndex, int iNumParams, char *pcParam[],
char *pcValue[])
{
//do some stuff with retrieved data
// return a filename
return "/index.html";
}
here is a simple LWIP CGI example code
This handler does not give the socket descriptor so that I can directly send data to the web page. Also The LWIP documentation says I should not write directly to the web page.
The simple CGI support offered here works with GET method requests
only and can handle up to 16 parameters encoded into the URI. The
handler function may not write directly to the HTTP output but must
return a filename that the HTTP server will send to the browser as a
response to the incoming CGI request.
How will I solve this problem without editing the HTML page?

You can use lwIP SSI (Server-Side Includes) for this. See documentation on configuration #define switches. The HTTP server documentation has some information on SSI, and there's also some SSI example code.
Basically,
#define LWIP_HTTPD_SSI in your configuration header
add a file to the lwIP HTTP server which only contains /*#TagName*/
register a callback with http_set_ssi_handler. This callback will be called when a http client requests the new file. The callback will receive information about the tag and you can write an arbitrary answer to a supplied buffer. By default, the response is limited to LWIP_HTTPD_MAX_TAG_INSERT_LEN bytes. If that's not sufficient, you can #define LWIP_HTTPD_SSI_MULTIPART to make longer responses.

Related

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

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()?

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 can a CGI script decode a multipart/form-data

Let's say an HTTP POST request is made with this header
Content-Type: multipart/form-data; boundary=...
and then, the body is built accordingly.
If I understand correctly, when the web server transmits the request to a CGI application, it sets some environments variables and the body is sent as stdin. So, the CGI app does not have access to the headers (except through some environment variables).
Then, how can a CGI application decode the body (stdin) if it does not have access to the header (Content-Type)?
There is a CONTENT-TYPE as part of the environment variable that a CGI application has access to. This link explains in details how a CGI application can read a multipart form.

How to monitor node http requests in node-webkit

How can I monitor http requests that were made by node in node-webkit? When I make a request I don't see it in the developer tools. I can see the request only when I make it with jquery or xmlhttprequest.
I've binded a console.log on the 'end' event and I can see that node-webkit is actually doing the request and it's returning me a correct response. But doing this blind requests is very hard because there is absolutely no way to debug them (except for logging the params that I've called the request method with).
Such low-level procedures can only be handled by the supported node.js so one of these node.js functions is bound to solve your HTTP header monitoring problem:
https://nodejs.org/dist/latest-v6.x/docs/api/http.html#http_event_checkexpectation

What is a pass-through request?

This is spoken in the context of a guide on express.js. Here is the quote:
Static middleware enables pass-through requests for static assets.
Googling doesn't yield a definition like I thought it would.
I think "pass-through" in this context means that static files on disk "pass through" express without any processing or modification and are sent directly to the browser, in contrast to dynamic requests which involve generating new and dynamic HTML in response to a browser request (using templates and databases, perhaps).
For example, we have this code at http://example.org
app.use(express.static(__dirname + '/public'));
app.get('*',function(req, res){
res.send('Hello');
});
For example, we open by browser the url http://example.org/somefile.txt
if there is file of somefile.txt in folder of public/ , it is returned, but if the file doesn't exists, the request is passed to other controllers, in this case to app.get('*',...)
So, the client recieves the response with Hello