I'm trying to send a mask file from CGI get request.
I have no compilation errors. But have a error 500 error with Apache2. i'm not shure how to send the data and co-pilot do not help a lot.
But if i can receive the mask SHA512 sum as string variable (it is the mask final filename) (how to get stdout ouput from system in a variable) (execl dont support pipe) maybe i can put an php header() like function in c++ to send the file ?
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <typeinfo>
#include <unistd.h>
using namespace std;
string gen_random(int len)
{
string s;
static const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < len; ++i)
{
s += alphanum[rand() % (sizeof(alphanum) - 1)];
}
return s;
}
std::string get_env_var(std::string const &key)
{
char *val;
val = getenv(key.c_str());
std::string retval = "";
if (val != NULL)
{
retval = val;
}
return retval;
}
int main()
{
string a, b, name, value, action;
int i = 0;
int j = 0;
string v;
int v2;
string line;
string val = get_env_var("REQUEST_METHOD");
if (val == "GET")
{
string q = get_env_var("QUERY_STRING");
stringstream ss2(q);
while (getline(ss2, line, '&'))
{
stringstream ss(line);
string token2;
while (getline(ss, token2, '='))
{
if (i == 0)
{
name = token2;
}
else if (i == 1)
{
value = token;
}
i++;
}
i = 0;
if (name == "action")
{
action = value;
}
else if (name == "value")
{
v = value;
v2 = stoi(v.c_str());
}
}
if (action == "getRandomFile")
{
stringstream ss;
ss << "Content-Type: application/octet-stream" << endl << endl;
ss << "Content-Length: " << v2 << endl << endl;;
int error = 0;
char mychar[1];
char mychar2[1];
fstream fout2;
fout2.open("temp.bin", ios::out | ios::app);
ifstream urandom;
urandom.open("/dev/urandom", ios::in | ios::binary);
// ss << getline(urandom, byte, length);
string s;
const string maskName = "temp.bin";
while (error != 1)
{
while (!(cin.get(mychar[0]) && i < v2))
{
error = 1;
i = 0;
break;
}
if (error == 0)
{
fout2 << (char)mychar2[0];
ss << (char)mychar2[0];
i++;
}
}
fout2.close();
const string s2 = "mv temp.bin $(shasum temp.bin | awk '{print $1}')";
// rename (maskName.c_str(), s3.c_str());
system(s2.c_str());
cout << ss.c_str();
}
}
}
-Wall -Wextra
when building with GCC or Clang) and that you treat all warnings as errors that must be fixed. The compiler is rather good at detecting cases which might be technically allowed by the C++ specification but might lead yo other problems when running (logical problems and even undefined behavior). I also recommend you use sanitizers, for example build and link with the flag-fsanitize=address,undefined
to catch many run-time errors.