cgi_test.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #if defined(_WIN32) || defined(WIN32) || defined(WINDOWS)
  5. #include <fcntl.h>
  6. #include <io.h>
  7. #endif
  8. int
  9. main(int argc, char *argv[])
  10. {
  11. char buf[1024];
  12. size_t rec_len = 0;
  13. const char *response_header = "Content-Type: text/plain\r\n"
  14. "Connection: close\r\n"
  15. "\r\n";
  16. const char *req_method = getenv("REQUEST_METHOD");
  17. const char *con_length = getenv("CONTENT_LENGTH");
  18. #if defined(_WIN32) || defined(WIN32) || defined(WINDOWS)
  19. _setmode(_fileno(stdin), _O_BINARY);
  20. _setmode(_fileno(stdout), _O_BINARY);
  21. #endif
  22. /* Write the response header with \r\n */
  23. fwrite(response_header, 1, strlen(response_header), stdout);
  24. /* Headline for generated reply: */
  25. printf("Got message:\n Method: %s\n Content-Length: %s\n Content: ",
  26. req_method,
  27. con_length ? con_length : "not set");
  28. /* Read all data from stdin and send it to stdout */
  29. while ((rec_len = fread(buf, 1, sizeof(buf) - 1, stdin)) > 0) {
  30. fwrite(buf, 1, rec_len, stdout);
  31. }
  32. return 0;
  33. }