cgienv.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include <sys/mman.h>
  2. #include <sys/stat.h>
  3. #include <err.h>
  4. #include <fcntl.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <unistd.h>
  9. #include "buffer.h"
  10. #include "filehelper.h"
  11. #include "template.h"
  12. #include "tmpl.h"
  13. const char *env_vars[] = {
  14. "SERVER_SOFTWARE",
  15. "SERVER_NAME",
  16. "SERVER_PROTOCOL",
  17. "SERVER_PORT",
  18. "DOCUMENT_ROOT",
  19. "SERVER_ADMIN",
  20. "GATEWAY_INTERFACE",
  21. "REQUEST_METHOD",
  22. "SCRIPT_FILENAME",
  23. "SCRIPT_NAME",
  24. "CONTENT_TYPE",
  25. "CONTENT_LENGTH",
  26. "QUERY_STRING",
  27. "REQUEST_URI",
  28. "PATH_INFO",
  29. "PATH_TRANSLATED",
  30. "AUTH_TYPE",
  31. "REMOTE_HOST",
  32. "REMOTE_ADDR",
  33. "REMOTE_USER",
  34. "HTTP_USER_AGENT",
  35. "HTTP_HOST",
  36. "HTTP_ACCEPT",
  37. "HTTP_ACCEPT_CHARSET",
  38. "HTTP_ACCEPT_ENCODING",
  39. "HTTP_ACCEPT_LANGUAGE",
  40. "HTTP_CONNECTION",
  41. "HTTP_REFERER",
  42. NULL
  43. };
  44. static void
  45. fill_env_data(struct tmpl_loop *_loop)
  46. {
  47. const char **v = env_vars;
  48. while (*v) {
  49. char *value = getenv(*v);
  50. struct tmpl_data *data = tmpl_data_new();
  51. tmpl_data_set_variable(data, "NAME", *v);
  52. tmpl_data_set_variable(data, "VALUE", (value) ? value : "");
  53. tmpl_loop_add_data(_loop, data);
  54. v++;
  55. }
  56. }
  57. int
  58. main(int argc, char **argv)
  59. {
  60. struct tmpl_data *data = tmpl_data_new();
  61. struct tmpl_loop *env_variables = tmpl_data_add_loop(
  62. data, "ENV"
  63. );
  64. fill_env_data(env_variables);
  65. tmpl_data_set_variable(data, "TITLE", "Environment Variables");
  66. struct buffer_list *out = tmpl_parse(env_tmpl, strlen(env_tmpl), data);
  67. tmpl_data_free(data);
  68. char *result = buffer_list_concat_string(out);
  69. printf("Content-type: application/xhtml+xml\r\n\r\n%s", result);
  70. return 0;
  71. }