query_utils.c

Go to the documentation of this file.
00001 /*
00002    Copyright (C) 2003-2005 Commonwealth Scientific and Industrial Research
00003    Organisation (CSIRO) Australia
00004 
00005    Redistribution and use in source and binary forms, with or without
00006    modification, are permitted provided that the following conditions
00007    are met:
00008 
00009    - Redistributions of source code must retain the above copyright
00010    notice, this list of conditions and the following disclaimer.
00011 
00012    - Redistributions in binary form must reproduce the above copyright
00013    notice, this list of conditions and the following disclaimer in the
00014    documentation and/or other materials provided with the distribution.
00015 
00016    - Neither the name of CSIRO Australia nor the names of its
00017    contributors may be used to endorse or promote products derived from
00018    this software without specific prior written permission.
00019 
00020    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
00023    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
00024    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00025    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00026    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00027    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00029    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 */
00032 
00033 
00034 #include "query_utils.h"
00035 
00036 #include "httpd.h"
00037 #ifdef WIN32
00038 # undef strtoul /* Otherwise Visual Studio .NET 2003 complains */
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   /* If there was no Accept: header, accept all types equally */
00060   if (a == NULL) return 1.0;
00061 
00062   /* Form a 'major/*' mime type range for later comparison */
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   /* Copy the Accept line for tokenization */
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     /*ap_rprintf (r, "%s = %s\n", key, val);*/
00147     apr_table_set (t, key, val);
00148 
00149     key = end;
00150 
00151   } while (end != NULL);
00152 
00153   return t;
00154 }
00155 

Generated on Tue Feb 15 14:54:23 2005 for oggdsf by  doxygen 1.3.9