Skip to content

Lxu wu #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions Makefile

This file was deleted.

5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# 42_webserv

Chere Lxu-Wu
Si on termine pas ds une semaine, cela veut dire que tu as menti :)

mushumartial
ON VA LE FAIRE TODAY I FINISH CGI
Binary file added a.out
Binary file not shown.
20 changes: 20 additions & 0 deletions a.out.dSYM/Contents/Info.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleIdentifier</key>
<string>com.apple.xcode.dsym.a.out</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundlePackageType</key>
<string>dSYM</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
</plist>
Binary file added a.out.dSYM/Contents/Resources/DWARF/a.out
Binary file not shown.
208 changes: 208 additions & 0 deletions cgi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
#include "cgi.hpp"

std::string fileExtent(std::string filePwd)
{
size_t i = 0;

while (filePwd[i])
i++;
while(i && filePwd[i] != '.')
i--;
if (!strcmp(&filePwd[i], ".py"))
return "/usr/bin/python2.7";
if (!strcmp(&filePwd[i], ".pl"))
return "/usr/bin/perl";
return "";
}

std::string searchExec(std::string filePwd, char **envp)
{
(void)envp;
std::string path;
std::string token;
std::string const exec = fileExtent(filePwd);

if (exec == "")
{
std::cerr << "uncompatible CGI-script" << std::endl;
return (0);
}

if (!access(exec.c_str(), X_OK))
return exec;
return (0);
}

std::vector<std::string> newEnv(std::string filePwd, char **envp, Requete req)
{
std::vector<std::string> my_env;

size_t i = 0;

while (envp[i])
i++;

i = 0;
while (envp[i])
{
my_env.push_back(envp[i]);
i++;
}
my_env.push_back("CONTENT_TYPE=" + req.getType());
my_env.push_back("GATEWAY_INTERFACE=CGI/1.1");
// my_env.push_back("PATH_TRANSLATED=" + req.getPath());
// my_env.push_back("QUERY_STRING=" + req.getQS);//getQS pour querry string
my_env.push_back("REMOTE_ADDR=127.0.0.1");
my_env.push_back("REQUEST_METHOD=" + req.getMethod());
my_env.push_back("CONTENT_LENGTH=" + std::to_string(req.getLen()));
my_env.push_back("SERVER_SOFTWARE=" + req.getProtocol());
my_env.push_back("SERVER_NAME=127.0.0.1");
my_env.push_back("HTTP_ACCEPT=" + req.getHeader()["Accept:"]);
my_env.push_back("HTTP_ACCEPT_LANGUAGE=" + req.getHeader()["Accept-Language:"]);
my_env.push_back("HTTP_USER_AGENT=" + req.getHeader()["User-Agent:"]);
my_env.push_back("SCRIPT_NAME=" + filePwd);
return (my_env);
}

char **vecToTab(std::vector<std::string> vec)
{
char **tab;
int i = 0;

tab = (char **)malloc(sizeof(char *) * (vec.size() + 1));
if (!tab)
{
perror("malloc vecToTab");
exit(1);
}

for (std::vector<std::string>::iterator it = vec.begin(); it != vec.end(); it++)
{
tab[i++] = (char *)(*it).c_str();
}
tab[i] = 0;
return tab;
}

std::string execCGI(std::string filePwd, char **envp, Requete req)
{
filePwd = "." + filePwd;
std::string execPwd = searchExec(filePwd, envp);
if (execPwd == "")
{
std::cerr << "Bad file" << std::endl;
return (0);
}

int fdIn;
int fd_in[2];
int fd_out[2];
char *tab[3];

tab[0] = (char *)execPwd.c_str();
tab[1] = (char *)filePwd.c_str();
tab[2] = 0;

char **my_env;

my_env = vecToTab(newEnv(filePwd, envp, req));

pipe(fd_in);
pipe(fd_out);
pid_t pid = fork();

if (pid == -1)
{
perror("fork()");
exit(1);
}



if (pid == 0)
{
if (dup2(fd_in[0], 0) == -1)
{
perror("dup2");
exit(1);
}
if (dup2(fd_out[1], 1) == -1)
{
perror("dup2");
exit(1);
}
close(fd_out[0]);
close(fd_in[1]);
execve(tab[0], tab, my_env);
perror("execve");
close(fd_out[1]);
close(fd_in[0]);
exit(1);
}




else
{
fdIn = dup(0);
if (fdIn == 0)
{
perror("dup");
exit(1);
}
if (dup2(fd_in[0], 0) == -1)
{
perror("dup2");
exit(1);
}
// if (!req.getBody().empty())
// {
// write(fd_in[0], req.getBody().c_str(), req.getLen());//req.getBody ou req.getBodyComplet
// }
write(fd_in[1], "bon", 3);
waitpid(pid, 0, 0);
close(fd_in[0]);
close(fd_in[1]);
if (dup2(fdIn, 0) == -1)
{
perror("dup2");
exit(1);
}
free(my_env);


char buff[32768] = {0};
std::string ret = "";
int i;




close(fd_out[1]);
i = read(fd_out[0], buff, 32767);
if (i == -1)
{
perror("read");
exit(1);
}
ret += std::string(buff);
while (i < 0)
{
i = read(fd_out[0], buff, 32767);
if (i == -1)
{
perror("read");
exit(1);

}
buff[i] = 0;
ret += std::string(buff);
}
close(fd_out[0]);
std::cout << ret << "\n" ;
return ret;
}
return "";

}
14 changes: 14 additions & 0 deletions cgi.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef CGI_HPP
# define CGI_HPP

#include <unistd.h>
#include <iostream>
#include <cstring>
#include <vector>
#include <stdlib.h>
#include <climits>
#include <sys/stat.h>



#endif
Loading