cgi.c 872 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // CTHTTPD - Simple Web Server - GPLv2
  2. // Chris Dorman, 2012-2014 (cddo@riseup.net)
  3. // Help from Nickolai Vurgaft (slipperygate@gmail.com)
  4. // CGI base by Lisa 'darkrose' Milne
  5. #include "dep.h"
  6. #include "cgi.h"
  7. #include "chttpd.h"
  8. #include "functions.h"
  9. void do_cgi(char fpath[], int fd, char *cgiroot)
  10. {
  11. static char buffer[BUFSIZE+1];
  12. char *cgidir;
  13. char *cgi_p;
  14. cgi_p = malloc(strlen(cgiroot) + strlen(fpath) + 1);
  15. strcpy(cgi_p, cgiroot);
  16. strcat(cgi_p, fpath);
  17. char *cgipath = dirname(fpath);
  18. cgidir = malloc(strlen(cgiroot) + strlen(fpath) + 1);
  19. strcpy(cgidir, cgiroot);
  20. strcat(cgidir, cgipath);
  21. // Write HTTP protocol to socket before executing cgi
  22. sprintf(buffer,"HTTP/1.0 200 OK\r\n");
  23. write(fd,buffer,strlen(buffer));
  24. /*
  25. start parsing the cgi code
  26. */
  27. fd = dup2(fd,STDOUT_FILENO);
  28. chdir(cgidir);
  29. execl("/bin/bash", cgi_p, NULL);
  30. }