• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

HdrUtils.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /****************************************************************************
00025 
00026    HdrUtils.cc
00027 
00028    Description: Convenience routines for dealing with hdrs and
00029                  values
00030 
00031 
00032  ****************************************************************************/
00033 
00034 #include "libts.h"
00035 #include "HdrUtils.h"
00036 
00037 #define GETNEXT() {      \
00038     cur += 1;            \
00039     if (cur >= end) {    \
00040         goto done;       \
00041     }                    \
00042 }
00043 
00044 void
00045 HdrCsvIter::find_csv()
00046 {
00047 
00048   const char *cur, *end, *last_data, *csv_start;
00049 
00050 RETRY:
00051 
00052   cur = m_csv_start;
00053   end = m_value_start + m_value_len;
00054   last_data = NULL;
00055   csv_start = NULL;
00056 
00057   if (cur >= end) {
00058     goto done;
00059   }
00060 skip_leading_whitespace:
00061   if (ParseRules::is_ws(*cur)) {
00062     GETNEXT();
00063     goto skip_leading_whitespace;
00064   }
00065   csv_start = cur;
00066 parse_value:
00067   // ink_assert((',' > '"') && (',' > ' ') && (',' > '\t'));
00068   // Cookie/Set-Cookie use ';' as the separator
00069   if (m_separator == ',')
00070     while ((cur<end - 1) && (*cur> ',')) {
00071       last_data = cur;
00072       cur++;
00073     }
00074 
00075   if (*cur == m_separator) {
00076     goto done;
00077   }
00078   if (*cur == '\"') {
00079     // If the quote come before any text
00080     //   skip it
00081     if (cur == csv_start) {
00082       csv_start++;
00083     }
00084     GETNEXT();
00085     goto parse_value_quote;
00086   }
00087   if (!ParseRules::is_ws(*cur)) {
00088     last_data = cur;
00089   }
00090   GETNEXT();
00091   goto parse_value;
00092 parse_value_quote:
00093   if ((*cur == '\"') && (cur[-1] != '\\')) {
00094     GETNEXT();
00095     goto parse_value;
00096   }
00097   last_data = cur;
00098   GETNEXT();
00099   goto parse_value_quote;
00100 done:
00101   m_csv_end = cur;
00102   m_csv_start = csv_start;
00103 
00104   if (last_data) {
00105     m_csv_len = (int) (last_data - csv_start) + 1;
00106   } else {
00107     // Nothing found.  See if there is another
00108     //    field in the dup list
00109     if (m_cur_field->m_next_dup && m_follow_dups) {
00110       field_init(m_cur_field->m_next_dup);
00111       goto RETRY;
00112     }
00113 
00114     m_csv_len = 0;
00115   }
00116 }
00117 
00118 const char *
00119 HdrCsvIter::get_nth(MIMEField * field, int *len, int n, bool follow_dups)
00120 {
00121   const char *s;
00122   int i, l;
00123 
00124   ink_assert(n >= 0);
00125   i = 0;
00126 
00127   s = get_first(field, &l, follow_dups);        // get index 0
00128   while (s && (n > i)) {
00129     s = get_next(&l);
00130     i++;
00131   }                             // if had index i, but want n > i, get next
00132   *len = (s ? l : 0);           // length is zero if NULL string
00133   return (s);
00134 }
00135 
00136 
00137 int
00138 HdrCsvIter::count_values(MIMEField * field, bool follow_dups)
00139 {
00140   const char *s;
00141   int count, l;
00142 
00143   count = 0;
00144   s = get_first(field, &l, follow_dups);        // get index 0
00145   while (s) {
00146     s = get_next(&l);
00147     ++count;
00148   }                             // get next
00149   return (count);
00150 }
00151 
00152 
00153 /*
00154 int main() {
00155 
00156     char* tests[] = {"\"I\", \"hate\", \"strings\"",
00157                      "This, is a , test",
00158                      "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p",
00159                      "\"This is,\" a test"
00160     };
00161 
00162     for (int i = 0; i < 4; i++) {
00163         printf ("Testing: %s\n", tests[i]);
00164 
00165         HdrCsvIter iter;
00166         int len;
00167         const char* v = iter.get_first(tests[i], strlen(tests[i]), &len);
00168 
00169         while (v) {
00170             char* str_v = (char*)ats_malloc(len+1);
00171             memcpy(str_v, v, len);
00172             str_v[len] = '\0';
00173             printf ("%s|", str_v);
00174             v = iter.get_next(&len);
00175         }
00176 
00177         printf("\n\n");
00178     }
00179 
00180 }
00181 */

Generated by  doxygen 1.7.1