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

HdrUtils.h

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.h
00027 
00028    Description: Convenience routines for dealing with hdrs and
00029                  values
00030 
00031 
00032  ****************************************************************************/
00033 
00034 #ifndef _HDR_UTILS_H_
00035 #define _HDR_UTILS_H_
00036 
00037 #include "ParseRules.h"
00038 #include "MIME.h"
00039 
00040 // csv = comma separated value
00041 class HdrCsvIter
00042 {
00043 public:
00044   // MIME standard separator ',' is used as the default value
00045   // Set-cookie/Cookie uses ';'
00046 HdrCsvIter(const char s = ','):m_value_start(NULL),
00047     m_value_len(0), m_bytes_consumed(0), m_follow_dups(false),
00048     m_csv_start(NULL), m_csv_len(0), m_csv_end(NULL), m_csv_index(0), m_cur_field(NULL), m_separator(s) {
00049   }
00050   const char *get_first(MIMEField * m, int *len, bool follow_dups = true);
00051   const char *get_next(int *len);
00052   const char *get_current(int *len);
00053 
00054   const char *get_nth(MIMEField * m, int *len, int n, bool follow_dups = true);
00055   int count_values(MIMEField * field, bool follow_dups = true);
00056 
00057   int get_index();
00058 
00059   int get_first_int(MIMEField * m, int *valid = NULL);
00060   int get_next_int(int *valid = NULL);
00061 
00062 private:
00063   void find_csv();
00064 
00065   const char *m_value_start;
00066   int m_value_len;
00067   int m_bytes_consumed;
00068   bool m_follow_dups;
00069 
00070   // m_csv_start - the start of the current comma separated value
00071   //                 leading white space, and leading quotes have
00072   //                 been skipped over
00073   // m_csv_len - the length of the current comma separated value
00074   //                 not including leading whitespace, trailing
00075   //                 whitespace (unless quoted) or the terminating
00076   //                 comma, or trailing quotes have been removed
00077   // m_csv_end - the terminating comma of the csv unit.  Either
00078   //                 the terminating comma or the final character
00079   //                 if this is the last csv in the string
00080   // m_cvs_index - the integer index of current csv starting
00081   //                 at zero
00082   const char *m_csv_start;
00083   int m_csv_len;
00084   const char *m_csv_end;
00085   int m_csv_index;
00086   MIMEField *m_cur_field;
00087 
00088   // for the Cookie/Set-cookie headers, the separator is ';'
00089   const char m_separator;
00090 
00091   void field_init(MIMEField * m);
00092 };
00093 
00094 inline void
00095 HdrCsvIter::field_init(MIMEField * m)
00096 {
00097   m_cur_field = m;
00098   m_value_start = m->m_ptr_value;
00099   m_value_len = m->m_len_value;
00100   m_csv_start = m_value_start;
00101 }
00102 
00103 inline const char *
00104 HdrCsvIter::get_first(MIMEField * m, int *len, bool follow_dups)
00105 {
00106 
00107   field_init(m);
00108 
00109   m_follow_dups = follow_dups;
00110 
00111   m_bytes_consumed = 0;
00112   m_csv_index = -1;
00113 
00114   if (m_csv_start) {
00115     find_csv();
00116   } else {
00117     m_csv_len = 0;
00118   }
00119 
00120   *len = m_csv_len;
00121   return m_csv_start;
00122 }
00123 
00124 
00125 inline const char *
00126 HdrCsvIter::get_next(int *len)
00127 {
00128 
00129 
00130   if (m_csv_start) {
00131     // Skip past the current csv
00132     m_csv_start = m_csv_end + 1;
00133     find_csv();
00134   }
00135 
00136   *len = m_csv_len;
00137   return m_csv_start;
00138 }
00139 
00140 inline const char *
00141 HdrCsvIter::get_current(int *len)
00142 {
00143   *len = m_csv_len;
00144   return m_csv_start;
00145 }
00146 
00147 inline int
00148 HdrCsvIter::get_first_int(MIMEField * m, int *valid)
00149 {
00150   int len;
00151   const char *r = get_first(m, &len);
00152 
00153   if (r) {
00154     if (valid)
00155       *valid = 1;
00156     return ink_atoi(r, len);
00157   } else {
00158     if (valid)
00159       *valid = 0;
00160     return 0;
00161   }
00162 }
00163 
00164 inline int
00165 HdrCsvIter::get_next_int(int *valid)
00166 {
00167   int len;
00168   const char *r = get_next(&len);
00169 
00170   if (r) {
00171     if (valid)
00172       *valid = 1;
00173     return ink_atoi(r, len);
00174   } else {
00175     if (valid)
00176       *valid = 0;
00177     return 0;
00178   }
00179 }
00180 
00181 
00182 
00183 #endif

Generated by  doxygen 1.7.1