StrList.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 66 HTTP Header Tools */
10 
11 #include "squid.h"
12 #include "base/TextException.h"
13 #include "sbuf/SBuf.h"
14 #include "SquidString.h"
15 #include "StrList.h"
16 
18 void
19 strListAdd(String * str, const char *item, char del)
20 {
21  assert(str && item);
22  const auto itemSize = strlen(item);
23  if (str->size()) {
24  char buf[3];
25  buf[0] = del;
26  buf[1] = ' ';
27  buf[2] = '\0';
28  Must(str->canGrowBy(2));
29  str->append(buf, 2);
30  }
31  Must(str->canGrowBy(itemSize));
32  str->append(item, itemSize);
33 }
34 
36 int
37 strListIsMember(const String * list, const SBuf &m, char del)
38 {
39  const char *pos = NULL;
40  const char *item;
41  int ilen = 0;
42 
43  assert(list);
44  int mlen = m.plength();
45  while (strListGetItem(list, del, &item, &ilen, &pos)) {
46  if (mlen == ilen && m.caseCmp(item, ilen) == 0)
47  return 1;
48  }
49  return 0;
50 }
51 
53 int
54 strListIsSubstr(const String * list, const char *s, char del)
55 {
56  assert(list && del);
57  return (list->find(s) != String::npos);
58 
66 }
67 
76 int
77 strListGetItem(const String * str, char del, const char **item, int *ilen, const char **pos)
78 {
79  size_t len;
80  /* ',' is always enabled as field delimiter as this is required for
81  * processing merged header values properly, even if Cookie normally
82  * uses ';' as delimiter.
83  */
84  static char delim[3][8] = {
85  "\"?,",
86  "\"\\",
87  " ?,\t\r\n"
88  };
89  int quoted = 0;
90  assert(str && item && pos);
91 
92  delim[0][1] = del;
93  delim[2][1] = del;
94 
95  if (!*pos) {
96  *pos = str->termedBuf();
97 
98  if (!*pos)
99  return 0;
100  }
101 
102  /* skip leading whitespace and delimiters */
103  *pos += strspn(*pos, delim[2]);
104 
105  *item = *pos; /* remember item's start */
106 
107  /* find next delimiter */
108  do {
109  *pos += strcspn(*pos, delim[quoted]);
110  if (**pos == '"') {
111  quoted = !quoted;
112  *pos += 1;
113  } else if (quoted && **pos == '\\') {
114  *pos += 1;
115  if (**pos)
116  *pos += 1;
117  } else {
118  break; /* Delimiter found, marking the end of this value */
119  }
120  } while (**pos);
121 
122  len = *pos - *item; /* *pos points to del or '\0' */
123 
124  /* rtrim */
125  while (len > 0 && xisspace((*item)[len - 1]))
126  --len;
127 
128  if (ilen)
129  *ilen = len;
130 
131  return len > 0;
132 }
133 
int strListGetItem(const String *str, char del, const char **item, int *ilen, const char **pos)
Definition: StrList.cc:77
#define assert(EX)
Definition: assert.h:17
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition: SBuf.h:272
static const size_type npos
Definition: SquidString.h:39
Definition: SBuf.h:86
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:69
void strListAdd(String *str, const char *item, char del)
Definition: StrList.cc:19
#define xisspace(x)
Definition: xis.h:17
int strListIsMember(const String *list, const SBuf &m, char del)
Definition: StrList.cc:37
void append(char const *buf, int len)
Definition: String.cc:161
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:123
int strListIsSubstr(const String *list, const char *s, char del)
Definition: StrList.cc:54
int plength() const
Definition: SBuf.h:411
char const * termedBuf() const
Definition: SquidString.h:91
int unsigned int const char *desc STUB void int len
Definition: stub_fd.cc:20
void const char * buf
Definition: stub_helper.cc:16
size_type find(char const ch) const
Definition: String.cc:522
size_type size() const
Definition: SquidString.h:72
#define NULL
Definition: types.h:166

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors