CGI scripts for bash commands - html

I'm kinda new to all this, so tell me if I'm doing something totally wrong.
I'm doing some gpio stuff with my raspberry pi, and at the moment i'm making something so the gpio pins can be controlled via a web interface. One of the ways I'm doing this is using bash CGI scripts to control the pins, and executing them from the browser.
So far the only way I can get this to work involves the browser loading the page ".../cgi-bin/gpio1.cgi" etc, which contains the code:
#!/bin/bash
echo "Content-type: text/html"
echo ""
...gpio stuff...
This works, but the browser navigates to the blank page created by this script.
Is there a way of executing these scripts without leaving the webpage, and so the scripts aren't writing HTML but instead focusing on the actual gpio stuff?
Thanks

Try this:
#!/bin/bash
echo "Status: 204 No Content"
...gpio stuff...
HTTP responses must start with a status line; webservers will normally add status "200 Ok" if the CGI doesn't specify one. That status must be accompanied by response body, which will form the new web page.
The status you want is 204, which indicates that the request was satisfied but that there is no response and the browser should stay on the same page. Normally, this would be a response to a POST request, not a GET request, but it should work anyway. Since a 204 response does not require a response body (in fact, it doesn't permit one), it should not be necessary to output a blank line following the status line, but you might need one if the script takes a long time to run.

Related

HTML junk returned when JSON is expected

The following code used to work but not anymore and I'm seeing junk HTML with success code of 200 returned.
response = urlopen('https://www.tipranks.com/api/stocks/stockAnalysisOverview/?tickers='+symbol)
data = json.load(response)
If you open the page in chrome you will see the JSON file format. But when opened in python I'm now getting:
f1xx.v1xx=v1xx;f1xx[374148]=window;f1xx[647467]=e8NN(f1xx[374148]);f1xx[125983]=n3EE(f1xx[374148]);f1xx[210876]=(function(){var
P6=2;for(;P6 !== 1;){switch(P6){case 2:return {w3:(function(v3){var
v6=2;for(;v6 !== 10;){switch(v6){case 2:var O3=function(W3){var
u6=2;for(;u6 !== 13;){switch(u6){case 2:var o3=[];u6=1;break;case
14:return E3;break;case 8:U3=o3.H8NN(function(){var Z6=2;for(;Z6 !==
1;){switch(Z6){case 2:return 0.5 - B8NN.P8NN();break;}}.....
What should I be doing to adapt to the new backend change so that I can parse the JSON again.
It is a bot protection, to prevent people from doing what you are doing. This API endpoint is supposed to be used only by the website itself, not by some Python script!
If you delete your site data and then freshly access the page in the browser, you'll see it first loads the HTML page that you see which loads some JavaScript, which then executes a POST to another URL with some data. Somewhere in the process a number of cookies get set and finally the code refreshes the page which then loads the JSON data. At this point visiting the URL directly returns the data because the correct cookies are already set.
If you look at those requests, you'll see the server returns a header server: rhino-core-shield. If you google that, you can see that it's part of the Reblaze DDoS Protection Platform.
You may have luck with a headless browser like ghost.py or pyppetteer but I'm not sure how effective it will be, you'll have to try. The proper way to do this would be to find an official (probably paid) API for getting the information you need instead of relying on non-public endpoints.

Zabbix: Response time for web step indicates until HTTP code is received or everything is loaded?

Response time for web steps (of a web scenario) indicates the time that it takes to load since the request is done until the HTTP code is received (200,302,404, etc) or until all the content is loaded? (images, css, javascripts, etc).
According to the docs: "Response time is counted from the beginning of the request until all information has been transferred." But "information" includes linked content??
Thanks!
Just the page content is evaluated: the HTML body, not the linked content. It's the same time it would take curl or wget to download the page, without recursive options.

Embedded processor web server; passing information from browwer to server

I have a simple web server going on an Atmel embedded processor. It's a Cortex M4 and it's only running FreeRTOS as an OS; this is not a high powered processor.
I can have it [the Atmel processor] serve up a page by typing in Firefox:
192.168.0.200
This renders a page with drop down boxes and a submit button that enables me to pass data back to the server to control the hardware.
I am using the follwing kind of HTML. Please note, it looks slightly odd because it's in a C string:
"<form> \
<select name=\"group\"> \
<option value=\"10\">10</option> \
<option value=\"11\">11</option> \
<option value=\"12\">12</option> \
<option value=\"Broadcast\">255</option> \
</select> \
<input type=\"submit\" value=\"Submit\"> \
</form>"
You can see that, in its address bar, the browser then has something like:
192.168.0.200/?group=4
When the web server on the emebedded processor gets that message in, I can happily parse the line, extract a group number and act on it.
However, I have to send back another page to the browser when I get the
192.168.0.200/?group=4
message into the Atmel processor. Otherwise I get a timeout message. I can happily send the original page back again and it essentially works, but sending the page back resets the values of what the drop down boxes have changed to.
Is there a way of making the browser send a message that the server can parse, but not have to send out the full page again? I guess I'm needing to use something like a POST command, but I don't know how to do that from a web page. I should say that I am experienced in C, but have no HTML knowledge other than what I have learnt in the last few days, so it may be something easy that it completely eluding me from cramming in all this learning this week!
I don't want to/assume I can't use Javascript, because I have such a simple server I need to keep it as simple as possible.
Thanks!
Is there a way of making the browser send a message that the server can parse, but not have to send out the full page again?
Forget about the browser.
Have the server respond with a 204 No Content response instead of a 200 OK response.
If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.
I don't want to/assume I can't use Javascript, because I have such a simple server I need to keep it as simple as possible.
JavaScript runs client side. You don't need the server to do anything complicated to serve JS. You can even embed it in the HTML document.
You could use Ajax to solve your problem instead of a No Content response.

Refresh browser without "Confirm Form Resubmission" in perl

I have a perl script that prints out HTML code and runs actions based on user input in the HTML page. I would like to be able to disable the "Confirm Form Resubmission" warning and prevent the users from repeating the last command ran. I saw some posts that solve this issue in php, but not sure how I would do it with perl.
Thanks!
The usual technique for preventing this is the Post/Get Redirect pattern.
This doesn't stop browsers from behaving as they do, but it redirects users to a new page after a successful POST, so that if they refresh they are not repeating the POST, but repeating the GET instead, which doesn't cause a warning.
Here is an example of the flow:
GET "Products/Create"
User types in some information
POST "Products/Create"
Validation fails, re-display the form with warnings - user corrects the input
POST "Products/Create"
Validation passes, item is saved redirect, using a GET to "Products/View/5"
User refreshes the page, this results in a harmless GET "Products/View/5"
To issue a redirect, you can use:
use CGI;
my $query=new CGI;
print $query->redirect('http://www.example.com/newpage');

How to create pop-up login box?

the box appears in below snapshot is neither alert box,prompt box nor confirm box. then what is this? how can i create the same thing like this?
It's a BasicAuth prompt, if your server return a request for BasicAuth it will get handled by the browser.
It happens when the browser receives a response with a header that looks like this, "insert realm" can be almost anything:
WWW-Authenticate: Basic realm="insert realm"
Usually the web browser handles it by itself and shows that kind of prompt. By the way it's unrelated to the web server as it's part of the protocole. If you happen to run an application server, you'll have to send the header above in a response and expect an Authorization header back from the "web client".
If you run apache, nginx, you can check simply for BasicAuth and you should be able to find documentation on how to set it up.
Read more here: BasicAuth
If you have enough courage you can read the RFCs
This is a simple HTTP Authentification, like the one you can setup with a ".htpasswd" file on Apache and so on.
You can't do it with Javascript (it's on server-side), in PHP it would be like this