00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #include "query_utils.h"
00035
00036 #include "httpd.h"
00037 #ifdef WIN32
00038 # undef strtoul
00039 #endif
00040 #include "apr_strings.h"
00041
00042
00050 float get_accept_quality (request_rec * r, char * content_type)
00051 {
00052 char * a, * accept, *next, * last, * pnext, * plast;
00053 float q = 0.0, type_q = 0.0, all_q = 0.0;
00054 char * m_sep, * m_major;
00055 apr_size_t m_major_len;
00056
00057 a = (char *)apr_table_get (r->headers_in, (const char *)"Accept");
00058
00059
00060 if (a == NULL) return 1.0;
00061
00062
00063 m_sep = strchr (content_type, '/');
00064 m_major_len = (apr_size_t)(m_sep - content_type);
00065 m_major = apr_pstrndup (r->pool, content_type, m_major_len + 2);
00066 *(m_major+m_major_len+1) = '*';
00067 *(m_major+m_major_len+2) = '\0';
00068
00069
00070 accept = apr_pstrdup (r->pool, a);
00071
00072 apr_collapse_spaces (accept, accept);
00073
00074 next = apr_strtok (accept, ",", &last);
00075 while (next) {
00076 pnext = apr_strtok (next, ";", &plast);
00077
00078 if (!strcmp (pnext, content_type)) {
00079 while (pnext) {
00080 pnext = apr_strtok (NULL, ";", &plast);
00081 if (pnext && sscanf (pnext, "q=%f", &q) == 1) {
00082 return q;
00083 }
00084 }
00085 return 1.0;
00086 } else if (!strcmp (pnext, "*/*")) {
00087 while (pnext) {
00088 pnext = apr_strtok (NULL, ";", &plast);
00089 if (pnext && sscanf (pnext, "q=%f", &q) == 1) {
00090 all_q = q;
00091 }
00092 }
00093 all_q = 1.0;
00094 } else if (!strcmp (pnext, m_major)) {
00095 while (pnext) {
00096 pnext = apr_strtok (NULL, ";", &plast);
00097 if (pnext && sscanf (pnext, "q=%f", &q) == 1) {
00098 type_q = q;
00099 }
00100 }
00101 type_q = 1.0;
00102 }
00103 next = apr_strtok (NULL, ",", &last);
00104 }
00105
00106 if (q > 0.0) return q;
00107 else if (type_q > 0.0) return type_q;
00108 else return all_q;
00109 }
00110
00111
00118 apr_table_t *make_cgi_table (request_rec * r, char * query)
00119 {
00120 apr_table_t * t;
00121 char * key, * val, * end;
00122
00123 t = apr_table_make (r->pool, 3);
00124
00125 if (!query) return t;
00126
00127 key = query;
00128
00129 do {
00130 val = strchr (key, '=');
00131 end = strchr (key, '&');
00132
00133 if (end) {
00134 if (val) {
00135 if (val < end) {
00136 *val++ = '\0';
00137 } else {
00138 val = NULL;
00139 }
00140 }
00141 *end++ = '\0';
00142 } else {
00143 if (val) *val++ = '\0';
00144 }
00145
00146
00147 apr_table_set (t, key, val);
00148
00149 key = end;
00150
00151 } while (end != NULL);
00152
00153 return t;
00154 }
00155