You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

208 lines
7.6 KiB

  1. #include <lighttpd/base.h>
  2. #include <lighttpd/plugin_core.h>
  3. #include <lighttpd/utils.h>
  4. static void _hash_free_gstring(gpointer data) {
  5. g_string_free((GString*) data, TRUE);
  6. }
  7. void li_environment_init(liEnvironment *env) {
  8. env->table = g_hash_table_new_full(
  9. (GHashFunc) g_string_hash, (GEqualFunc) g_string_equal,
  10. _hash_free_gstring, _hash_free_gstring);
  11. }
  12. void li_environment_reset(liEnvironment *env) {
  13. g_hash_table_remove_all(env->table);
  14. }
  15. void li_environment_clear(liEnvironment *env) {
  16. g_hash_table_destroy(env->table);
  17. env->table = NULL;
  18. }
  19. void li_environment_set(liEnvironment *env, const gchar *key, size_t keylen, const gchar *val, size_t valuelen) {
  20. GString *skey = g_string_new_len(key, keylen);
  21. GString *sval = g_string_new_len(val, valuelen);
  22. g_hash_table_insert(env->table, skey, sval);
  23. }
  24. void li_environment_insert(liEnvironment *env, const gchar *key, size_t keylen, const gchar *val, size_t valuelen) {
  25. GString *sval = li_environment_get(env, key, keylen), *skey;
  26. if (!sval) {
  27. skey = g_string_new_len(key, keylen);
  28. sval = g_string_new_len(val, valuelen);
  29. g_hash_table_insert(env->table, skey, sval);
  30. }
  31. }
  32. void li_environment_remove(liEnvironment *env, const gchar *key, size_t keylen) {
  33. const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
  34. g_hash_table_remove(env->table, &skey);
  35. }
  36. GString* li_environment_get(liEnvironment *env, const gchar *key, size_t keylen) {
  37. const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
  38. return (GString*) g_hash_table_lookup(env->table, &skey);
  39. }
  40. liEnvironmentDup* li_environment_make_dup(liEnvironment *env) {
  41. GHashTableIter i;
  42. GHashTable *tdst;
  43. gpointer key, val;
  44. liEnvironmentDup *envdup = g_slice_new0(liEnvironmentDup);
  45. envdup->table = tdst = g_hash_table_new((GHashFunc) g_string_hash, (GEqualFunc) g_string_equal);
  46. g_hash_table_iter_init(&i, env->table);
  47. while (g_hash_table_iter_next(&i, &key, &val)) {
  48. g_hash_table_insert(tdst, key, val);
  49. }
  50. return envdup;
  51. }
  52. void li_environment_dup_free(liEnvironmentDup *envdup) {
  53. g_hash_table_destroy(envdup->table);
  54. g_slice_free(liEnvironmentDup, envdup);
  55. }
  56. GString* li_environment_dup_pop(liEnvironmentDup *envdup, const gchar *key, size_t keylen) {
  57. const GString skey = li_const_gstring(key, keylen); /* fake a constant GString */
  58. GString *sval = (GString*) g_hash_table_lookup(envdup->table, &skey);
  59. if (sval) g_hash_table_remove(envdup->table, &skey);
  60. return sval;
  61. }
  62. static void add_env_var(liEnvironmentDup *envdup, liAddEnvironmentCB callback, gpointer param, const gchar *key, size_t keylen, const gchar *val, size_t valuelen) {
  63. GString *sval;
  64. if (NULL != (sval = li_environment_dup_pop(envdup, key, keylen))) {
  65. callback(param, key, keylen, GSTR_LEN(sval));
  66. } else {
  67. callback(param, key, keylen, val, valuelen);
  68. }
  69. }
  70. static void cgi_fix_header_name(GString *str) {
  71. guint i, len = str->len;
  72. gchar *s = str->str;
  73. for (i = 0; i < len; i++) {
  74. if (g_ascii_isalpha(s[i])) {
  75. s[i] = g_ascii_toupper(s[i]);
  76. } else if (!g_ascii_isdigit(s[i])) {
  77. s[i] = '_';
  78. }
  79. }
  80. }
  81. void li_environment_dup2cgi(liVRequest *vr, liEnvironmentDup *envdup, liAddEnvironmentCB callback, gpointer param) {
  82. liConInfo *coninfo = vr->coninfo;
  83. GString *tmp = vr->wrk->tmp_str;
  84. /* SCGI needs this as first variable */
  85. if (vr->request.content_length >= 0) {
  86. g_string_printf(tmp, "%" LI_GOFFSET_MODIFIER "i", vr->request.content_length);
  87. add_env_var(envdup, callback, param, CONST_STR_LEN("CONTENT_LENGTH"), GSTR_LEN(tmp));
  88. }
  89. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_SOFTWARE"), GSTR_LEN(CORE_OPTIONPTR(LI_CORE_OPTION_SERVER_TAG).string));
  90. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_NAME"), GSTR_LEN(vr->request.uri.host));
  91. add_env_var(envdup, callback, param, CONST_STR_LEN("GATEWAY_INTERFACE"), CONST_STR_LEN("CGI/1.1"));
  92. {
  93. guint port = 0;
  94. switch (coninfo->local_addr.addr->plain.sa_family) {
  95. case AF_INET: port = coninfo->local_addr.addr->ipv4.sin_port; break;
  96. #ifdef HAVE_IPV6
  97. case AF_INET6: port = coninfo->local_addr.addr->ipv6.sin6_port; break;
  98. #endif
  99. }
  100. if (port) {
  101. g_string_printf(tmp, "%u", htons(port));
  102. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_PORT"), GSTR_LEN(tmp));
  103. }
  104. }
  105. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_ADDR"), GSTR_LEN(coninfo->local_addr_str));
  106. {
  107. guint port = 0;
  108. switch (coninfo->remote_addr.addr->plain.sa_family) {
  109. case AF_INET: port = coninfo->remote_addr.addr->ipv4.sin_port; break;
  110. #ifdef HAVE_IPV6
  111. case AF_INET6: port = coninfo->remote_addr.addr->ipv6.sin6_port; break;
  112. #endif
  113. }
  114. if (port) {
  115. g_string_printf(tmp, "%u", htons(port));
  116. add_env_var(envdup, callback, param, CONST_STR_LEN("REMOTE_PORT"), GSTR_LEN(tmp));
  117. }
  118. }
  119. add_env_var(envdup, callback, param, CONST_STR_LEN("REMOTE_ADDR"), GSTR_LEN(coninfo->remote_addr_str));
  120. add_env_var(envdup, callback, param, CONST_STR_LEN("SCRIPT_NAME"), GSTR_LEN(vr->request.uri.path));
  121. add_env_var(envdup, callback, param, CONST_STR_LEN("PATH_INFO"), GSTR_LEN(vr->physical.pathinfo));
  122. if (vr->physical.pathinfo->len) {
  123. g_string_truncate(tmp, 0);
  124. g_string_append_len(tmp, GSTR_LEN(vr->physical.doc_root)); /* TODO: perhaps an option for alternative doc-root? */
  125. g_string_append_len(tmp, GSTR_LEN(vr->physical.pathinfo));
  126. add_env_var(envdup, callback, param, CONST_STR_LEN("PATH_TRANSLATED"), GSTR_LEN(tmp));
  127. }
  128. add_env_var(envdup, callback, param, CONST_STR_LEN("SCRIPT_FILENAME"), GSTR_LEN(vr->physical.path));
  129. add_env_var(envdup, callback, param, CONST_STR_LEN("DOCUMENT_ROOT"), GSTR_LEN(vr->physical.doc_root));
  130. add_env_var(envdup, callback, param, CONST_STR_LEN("REQUEST_URI"), GSTR_LEN(vr->request.uri.raw_orig_path));
  131. if (!g_string_equal(vr->request.uri.raw_orig_path, vr->request.uri.raw_path)) {
  132. add_env_var(envdup, callback, param, CONST_STR_LEN("REDIRECT_URI"), GSTR_LEN(vr->request.uri.raw_path));
  133. }
  134. add_env_var(envdup, callback, param, CONST_STR_LEN("QUERY_STRING"), GSTR_LEN(vr->request.uri.query));
  135. add_env_var(envdup, callback, param, CONST_STR_LEN("REQUEST_METHOD"), GSTR_LEN(vr->request.http_method_str));
  136. add_env_var(envdup, callback, param, CONST_STR_LEN("REDIRECT_STATUS"), CONST_STR_LEN("200")); /* if php is compiled with --force-redirect */
  137. switch (vr->request.http_version) {
  138. case LI_HTTP_VERSION_1_1:
  139. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_PROTOCOL"), CONST_STR_LEN("HTTP/1.1"));
  140. break;
  141. case LI_HTTP_VERSION_1_0:
  142. default:
  143. add_env_var(envdup, callback, param, CONST_STR_LEN("SERVER_PROTOCOL"), CONST_STR_LEN("HTTP/1.0"));
  144. break;
  145. }
  146. if (coninfo->is_ssl) {
  147. add_env_var(envdup, callback, param, CONST_STR_LEN("HTTPS"), CONST_STR_LEN("on"));
  148. add_env_var(envdup, callback, param, CONST_STR_LEN("REQUEST_SCHEME"), CONST_STR_LEN("https"));
  149. } else {
  150. add_env_var(envdup, callback, param, CONST_STR_LEN("REQUEST_SCHEME"), CONST_STR_LEN("http"));
  151. }
  152. {
  153. GList *i;
  154. for (i = vr->request.headers->entries.head; NULL != i; i = i->next) {
  155. liHttpHeader *h = (liHttpHeader*) i->data;
  156. const GString hkey = li_const_gstring(h->data->str, h->keylen);
  157. g_string_truncate(tmp, 0);
  158. if (!li_strncase_equal(&hkey, CONST_STR_LEN("CONTENT-TYPE"))) {
  159. g_string_append_len(tmp, CONST_STR_LEN("HTTP_"));
  160. }
  161. g_string_append_len(tmp, h->data->str, h->keylen);
  162. cgi_fix_header_name(tmp);
  163. add_env_var(envdup, callback, param, GSTR_LEN(tmp), h->data->str + h->keylen+2, h->data->len - (h->keylen+2));
  164. }
  165. }
  166. {
  167. GHashTableIter i;
  168. gpointer key, val;
  169. g_hash_table_iter_init(&i, envdup->table);
  170. while (g_hash_table_iter_next(&i, &key, &val)) {
  171. callback(param, GSTR_LEN((GString*) key), GSTR_LEN((GString*) val));
  172. }
  173. }
  174. li_environment_dup_free(envdup);
  175. }