I have created a hello FastCGI prog in C
#include <fcgi_stdio.h>
#include <stdlib.h>
int count;
void initialize(void)
{
count=0;
}
int main(void)
{
initialize();
while (FCGI_Accept() >= 0)
{
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello! (C, fcgi_stdio library)</title>"
"<h1>FastCGI Hello! (C, fcgi_stdio library)</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("REMOTE_HOST"));
}
return 1;
}
Then I compiled the program using "gcc -o hello1 hello1.c -lfcgi"
This created "hello1" executable file in my home directory (in ubuntu)
When I ran this file, I got output as:
Content-type: text/html
<title>FastCGI Hello! (C, fcgi_stdio library)</title><h1>FastCGI Hello! (C, fcgi_stdio library)</h1>Request number 1 running on host <i>(null)</i>
I want to run this file from firefox. Since I am new to this, I dont have any idea about it. Can any one, provide me with detailed ans, what all steps I need to follow to run it through web browser.
I tried typing the URL as "http://localhost/fcgi-bin/hello1" after copying the 'hello1" file to /etc/apache/fcgi-bin/hello1.fcgi but it gave 404 error
You'd still need to include the .fcgi extension on the url:
http://localhost/fcgi-bin/hello1.fcgi
Related
I am making a simple arduino app that send a GET request to a HTTPS site. The code I'm using is exactly the same as the code in the MKRGSM library examples (GsmSslWebClient). But for whatever reason I always get the same response when connecting to a HTTPS site: "301 moved permanently". I kind of know what that means, I am aware that you are supposed to just make another request to the location specified in the header. But I don't know what I have to change to be able to address a https site. I'm sorry for my ignorance and I do know that in the example it clearly states that it connects to http://www.arduino.cc/asciilogo.txt but why is it then any different than the normal http example?
I would also point out here, that I have tried changing port to 80 and client settings, to work for unprotected http which works just fine. So its just the https that doesn't work.
this is the code:
/*
Web client
This sketch connects to a website using SSL through a MKR GSM 1400 board. Specifically,
this example downloads the URL "http://www.arduino.cc/asciilogo.txt" and
prints it to the Serial monitor.
Circuit:
* MKR GSM 1400 board
* Antenna
* SIM card with a data plan
created 8 Mar 2012
by Tom Igoe
*/
// libraries
#include <MKRGSM.h>
#include "arduino_secrets.h"
// Please enter your sensitive data in the Secret tab or arduino_secrets.h
// PIN Number
const char PINNUMBER[] = SECRET_PINNUMBER;
// APN data
const char GPRS_APN[] = SECRET_GPRS_APN;
const char GPRS_LOGIN[] = SECRET_GPRS_LOGIN;
const char GPRS_PASSWORD[] = SECRET_GPRS_PASSWORD;
// initialize the library instance
GSMSSLClient client;
GPRS gprs;
GSM gsmAccess;
// URL, path and port (for example: arduino.cc)
char server[] = "arduino.cc";
char path[] = "/asciilogo.txt";
int port = 443; // port 443 is the default for HTTPS
void setup() {
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Starting Arduino web client.");
// connection state
bool connected = false;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (!connected) {
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &&
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)) {
connected = true;
} else {
Serial.println("Not connected");
delay(1000);
}
}
Serial.println("connecting...");
// if you get a connection, report back via serial:
if (client.connect(server, port)) {
Serial.println("connected");
// Make a HTTP request:
client.print("GET ");
client.print(path);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.available() && !client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for (;;)
;
}
}
and this is the output:
Starting Arduino web client.
connecting...
connected
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Thu, 19 Nov 2020 20:24:07 GMT
Content-Type: text/html
Content-Length: 178
Connection: close
Location: https://www.arduino.cc/asciilogo.txt
Strict-Transport-Security: max-age=500; includeSubDomains
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
<html>
<head><title>301 Moved Permanently</title></head>
<body bgcolor="white">
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center>
</body>
</html>
disconnecting.
It is also possible that I'm just stupid and the answer is clear as day, but if somebody could please tell me exactly what to do, maybe even put an example, that would be greatly appreciated.
I haven't really tried much except changing the setting to see if they work for normal http and some other libraries that worked even worse. Sadly that's all I could find on the internet since I'm not that skilled to be messing with libraries on my own. I am using arduino mkr gsm 1400.
Be sure to call me out if I missed to mention any detail that could help solving this issue
Thanks to anybody that can help me in advance.
To describe my problem I was trying to connect my Arduino UNO to website created by me in Heroku.
Main purpose was to called rest api function in arduino conected to Internet and get a json data.
My Arduino code:
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
void setup() {
// Initialize Serial port
Serial.begin(9600);
while (!Serial) continue;
// Initialize Ethernet library
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
Ethernet.init(8); // use pin 53 for Ethernet CS
if (!Ethernet.begin(mac)) {
Serial.println(F("Failed to configure Ethernet"));
return;
}
delay(1000);
Serial.println(F("Connecting..."));
// Connect to HTTP server
EthernetClient client;
client.setTimeout(10000);
if (!client.connect("https://salty-cliffs-06856.herokuapp.com", 80)) {
Serial.println(F("Connection failed"));
return;
}
Serial.println(F("Connected!"));
// Send HTTP request
client.println(F("GET /api/command/ HTTP/1.1"));
client.println(F("Host: https://salty-cliffs-06856.herokuapp.com"));
client.println(F("Connection: close"));
Serial.println(F("Done"));
if (client.println() == 0) {
Serial.println(F("Failed to send request"));
return;
}
// Check HTTP status
char status[32] = {0};
client.readBytesUntil('\r', status, sizeof(status));
Serial.println(status);
if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
Serial.print(F("Unexpected response: "));
Serial.println(status);
return;
}
// Skip HTTP headers
char endOfHeaders[] = "\r\n\r\n";
if (!client.find(endOfHeaders)) {
Serial.println(F("Invalid response"));
return;
}
// Allocate JsonBuffer
// Use arduinojson.org/assistant to compute the capacity.
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_ARRAY_SIZE(2) + 60;
DynamicJsonBuffer jsonBuffer(capacity);
// Parse JSON object
JsonObject& root = jsonBuffer.parseObject(client);
if (!root.success()) {
Serial.println(F("Parsing failed!"));
return;
}
// Extract values
Serial.println(F("Response:"));
Serial.println(root["command"].as<char*>());
// Disconnect
client.stop();
Everything was working fine with that code when I was trying putting there non-secured HTTP address. After putting there my web powered by Heroku secrued by HTTPS I always got error.
The program annouced error when I was checking HTTP status and in my Arduino port terminal I got response:
Unexpected response: HTTP/1.1 400 Bad Request
I checked my heroku logs but there are not listed any request from Arduino. (Just to be sure I tried to called API from web browser and it works)
Could you help me where could be a problem ? I was thinking that it could be because of secured HTTPS. What do you think ?
Thanks for every help :)
First, change client.connect("https://salty-cliffs-06856.herokuapp.com", 80)
from
`https`
to
`http`
as port 80 is not https port and Ethernet shield does not support SSL.
Secondly, you have a wrong http header for Host. the HTTP 1.1 requires that only the domain name to use used without the protocol (i.e. http://) prefix. So change the line:
client.println(F("Host: https://salty-cliffs-06856.herokuapp.com"));
to:
client.println(F("Host: salty-cliffs-06856.herokuapp.com"));
I followed the Webassembly getting started tutorial http://webassembly.org/getting-started/developers-guide/
It worked fine and displayed the "Hello, world!" message in the browser.
Then I tried a small C++ code, that opens a text file and does the calculation (10 * 20) after reading the file.
emcc compiled the file just fine, no errors.
But when I serve the file over HTTP by running emrun, it cannot open the file.
This is what I see in the emrun web console:
Unable to open file
200
Is there any restrictions to open files from the local disk?
[thiago#terra hello]$ cat pfile.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
int a, b, c;
ifstream myfile("test.txt");
if (myfile.is_open()) {
while (getline (myfile, line)) {
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file" << endl;
a = 10;
b = 20;
c = a * b;
cout << c << endl;
return 0;
}
[thiago#terra hello]$ emcc pfile.cpp -s WASM=1 -o pfile.html -v
INFO:root:(Emscripten: Running sanity checks)
clang version 4.0.0 (https://github.com/kripken/emscripten-fastcomp-clang.git c7c210fee24e0227f882337521b25b1ed9c36d5b) (https://github.com/kripken/emscripten-fastcomp.git 90b726ede4acf47c1bca089de6c79a0b8f2c5d9a) (emscripten 1.37.18 : 1.37.18)
Target: asmjs-unknown-emscripten
Thread model: posix
InstalledDir: /home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin
"/home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin/clang-4.0" -cc1 -triple asmjs-unknown-emscripten -emit-llvm-bc -emit-llvm-uselists -disable-free -main-file-name pfile.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -no-integrated-as -mconstructor-aliases -v -dwarf-column-info -debugger-tuning=gdb -coverage-notes-file /tmp/tmpV3VHOz/pfile_0.gcno -nostdsysteminc -nobuiltininc -resource-dir /home/thiago/Downloads/emsdk/clang/fastcomp/build_incoming_64/bin/../lib/clang/4.0.0 -D __EMSCRIPTEN_major__=1 -D __EMSCRIPTEN_minor__=37 -D __EMSCRIPTEN_tiny__=18 -D _LIBCPP_ABI_VERSION=2 -Werror=implicit-function-declaration -std=c++03 -fdeprecated-macro -fno-dwarf-directory-asm -fdebug-compilation-dir /home/thiago/hello -ferror-limit 19 -fmessage-length 164 -fobjc-runtime=gnustep -fcxx-exceptions -fexceptions -fdiagnostics-show-option -nobuiltininc -nostdsysteminc -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libcxx -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libcxxabi/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/compat -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SSE -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libc -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libc/musl/arch/emscripten -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/local/include -isystem/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SDL -o /tmp/tmpV3VHOz/pfile_0.o -x c++ pfile.cpp
clang -cc1 version 4.0.0 based upon LLVM 4.0.0 default target x86_64-unknown-linux-gnu
#include "..." search starts here:
#include <...> search starts here:
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libcxx
/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libcxxabi/include
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/compat
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SSE
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/libc
/home/thiago/Downloads/emsdk/emscripten/incoming/system/lib/libc/musl/arch/emscripten
/home/thiago/Downloads/emsdk/emscripten/incoming/system/local/include
/home/thiago/Downloads/emsdk/emscripten/incoming/system/include/SDL
End of search list.
[thiago#terra hello]$ emrun --no_browser --port 8080 .
Keep secure — WebAssembly is specified to be run in a safe, sandboxed
execution environment. Like other web code, it will enforce the
browser's same-origin and permissions policies.
So the short answer is — yes, there are restrictions. You have no access to files on disks. You just have block of memory, WASM code could be called from JS and also WASM could call JS functions.
But, there's one interesting feature in Emscripten — in the WASM you can have your own "virtual" file system with files. You can use it to "attach" some const files during compilation time and read them at the execution time.
See https://kripken.github.io/emscripten-site/docs/api_reference/Filesystem-API.html
You can package files or directories into the WASM virtual file system using the --embed-file flag.
In your case this would look like:
emcc pfile.cpp -s WASM=1 -o pfile.html -v --embed-file test.txt
Docs: https://kripken.github.io/emscripten-site/docs/porting/files/packaging_files.html
I am trying to use std.ip which is a part of varnish 4.0 to to return the client IP which should be the first valid IP address in the X-Forwarded-For header, if the example in the documentation is correct.
varnishtest "Test v4 vcl X-Forwarded-For Header logic"
server s1 {
rxreq
expect req.http.X-Real-IP == "2.1.1.1"
expect req.http.X-Forwarded-For == "2.1.1.1, 3.3.3.3, 3.3.3.3, 127.0.0.1"
txresp
} -start
varnish v1 -vcl+backend {
sub vcl_recv {
set req.http.X-Real-IP = std.ip(req.http.X-Forwarded-For, "0.0.0.0");
}
} -start
client c1 {
txreq -url "/" -hdr "X-Forwarded-For: 2.1.1.1, 3.3.3.3, 3.3.3.3"
rxresp
}
client c1 -run
The above dies an ugly death:
...
*** v1 0.9 debug| Assert error in vwk_thread(), waiter/cache_waiter_kqueue.c line 115:\n
*** v1 0.9 debug| Condition(read(vwk->pipe[0], &c, 1) == 1) not true.\n
...
And does not behave as I would want by returning the first IP address.
Updated
Alternatively I have found that the following does work for the same purposes, But still could not get std.ip to work:
varnish v1 -vcl+backend {
sub vcl_recv {
set req.http.X-Real-IP = regsub(req.http.X-Forwarded-For, "\s*,.*$", "");
}
} -start
As a follow up question. This vcl_recv{...} logic actually lives in my default.vcl file, where all my backends, probes are defined. But when I try to test that code by including into a varnishtest file as follows:
varnish v1 -vcl+backend {
include "/path/to/file.vcl";
} -start
The test does not get the expect statements in the server s1. If someone could give some clarity on the following I'd be much ablighed:
Why does std.ip(req.http.X-Forwarded-For, "0.0.0.0") not behave as expected?
How can I test an included default.vcl with expect statements in the set server s1?
Thank you.
You need to add the line
import std;
at the top of your vcl file
I have a code for the Arduino Ethernet Shield that will send a GET request to a server and return a PHP echo statement.
However, most of the time it fails to connect to the server.
When it does connect, I keep getting 403 Forbidden error or it says bad header format for "Host:".
I have checked every forum and all StackOverflow links related to the topic, but none of their solutions worked. My code is attached below.
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "kanishkaganguly.byethost5.com";
IPAddress ip(192,168,0,103);
EthernetClient client;
void setup() {
Serial.begin(9600);
while (!Serial) {
;
}
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET /test.php HTTP/1.1");
client.println("Host: www.arduino.cc");
client.println("User-Agent: arduino-ethernet");
//client.println("User-Agent: Mozilla/5.0");
//This supposedly fixed 403 error for another user
client.println("Connection: close");
client.println();
}else {
Serial.println("connection failed");
}
}
void loop(){
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
for(;;) ;
}
}
I figured out the problem. The client.println() as a new line isn't working for some reason. So, here is the updated code
client.print("GET /checkcontrol.php HTTP/1.1\r\n");
client.print("Host: shahz.webatu.com\r\n");
client.print("User-Agent: arduino-ethernet\r\n");
client.print("Connection: close\r\n\r\n");
The \r\n is the right way to go about adding a new line for the server to recognize.
The Host header specifies the hostname of the site you're connecting to. In this case, you are trying to connect to the domain kanishkaganguly.byethost5.com, but your Host header is set to www.arduino.cc. Usually this is incorrect. The Host header should match the domain, so both should be kanishkaganguly.byethost5.com.
When a client connects to a domain, the client first resolves the domain name to an IP, then makes the connection to that IP. Because the connection is made to the IP, the server does not know what domain name was looked up by the client. Because IPs are a limited resource, many hosting providers host multiple domains on a single IP. Without the Host header, the server would not know which page to return to the client.
The println command sends the text followed by "\r\n" so instead of changing every println for print, you could have added CRLF to the close line.
Client.println("Connection: close\r\n")