Squid Web Cache master
Loading...
Searching...
No Matches
StrList.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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
17void
18strListAdd(String &str, const char *item, const size_t itemSize, const char delimiter)
19{
20 if (str.size()) {
21 const char buf[] = { delimiter, ' ' };
22 const auto bufSize = sizeof(buf);
23 Must(str.canGrowBy(bufSize));
24 str.append(buf, bufSize);
25 }
26 Must(str.canGrowBy(itemSize));
27 str.append(item, itemSize);
28}
29
30void
31strListAdd(String *str, const char *item, const char delimiter)
32{
33 assert(str);
34 assert(item);
35 strListAdd(*str, item, strlen(item), delimiter);
36}
37
38void
39strListAdd(String &str, const SBuf &item, char delimiter)
40{
41 strListAdd(str, item.rawContent(), item.length(), delimiter);
42}
43
45int
46strListIsMember(const String * list, const SBuf &m, char del)
47{
48 const char *pos = nullptr;
49 const char *item;
50 int ilen = 0;
51
52 assert(list);
53 int mlen = m.plength();
54 while (strListGetItem(list, del, &item, &ilen, &pos)) {
55 if (mlen == ilen && m.caseCmp(item, ilen) == 0)
56 return 1;
57 }
58 return 0;
59}
60
62int
63strListIsSubstr(const String * list, const char *s, char del)
64{
65 assert(list && del);
66 return (list->find(s) != String::npos);
67}
68
77int
78strListGetItem(const String * str, char del, const char **item, int *ilen, const char **pos)
79{
80 size_t len;
81 /* ',' is always enabled as field delimiter as this is required for
82 * processing merged header values properly, even if Cookie normally
83 * uses ';' as delimiter.
84 */
85 static char delim[3][8] = {
86 "\"?,",
87 "\"\\",
88 " ?,\t\r\n"
89 };
90 int quoted = 0;
91 assert(str && item && pos);
92
93 delim[0][1] = del;
94 delim[2][1] = del;
95
96 if (!*pos) {
97 *pos = str->termedBuf();
98
99 if (!*pos)
100 return 0;
101 }
102
103 /* skip leading whitespace and delimiters */
104 *pos += strspn(*pos, delim[2]);
105
106 *item = *pos; /* remember item's start */
107
108 /* find next delimiter */
109 do {
110 *pos += strcspn(*pos, delim[quoted]);
111 if (**pos == '"') {
112 quoted = !quoted;
113 *pos += 1;
114 } else if (quoted && **pos == '\\') {
115 *pos += 1;
116 if (**pos)
117 *pos += 1;
118 } else {
119 break; /* Delimiter found, marking the end of this value */
120 }
121 } while (**pos);
122
123 len = *pos - *item; /* *pos points to del or '\0' */
124
125 /* rtrim */
126 while (len > 0 && xisspace((*item)[len - 1]))
127 --len;
128
129 if (ilen)
130 *ilen = len;
131
132 return len > 0;
133}
134
135SBuf
136getListMember(const String &list, const char *key, const char delimiter)
137{
138 const char *pos = nullptr;
139 const char *item = nullptr;
140 int ilen = 0;
141 const auto keyLen = strlen(key);
142 while (strListGetItem(&list, delimiter, &item, &ilen, &pos)) {
143 if (static_cast<size_t>(ilen) > keyLen && strncmp(item, key, keyLen) == 0 && item[keyLen] == '=')
144 return SBuf(item + keyLen + 1, ilen - keyLen - 1);
145 }
146 return SBuf();
147}
148
int strListGetItem(const String *str, char del, const char **item, int *ilen, const char **pos)
Definition StrList.cc:78
void strListAdd(String &str, const char *item, const size_t itemSize, const char delimiter)
Appends the given item of a given size to a delimiter-separated list in str.
Definition StrList.cc:18
int strListIsMember(const String *list, const SBuf &m, char del)
Definition StrList.cc:46
SBuf getListMember(const String &list, const char *key, const char delimiter)
Definition StrList.cc:136
int strListIsSubstr(const String *list, const char *s, char del)
Definition StrList.cc:63
#define Must(condition)
#define assert(EX)
Definition assert.h:17
Definition SBuf.h:94
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition SBuf.h:287
const char * rawContent() const
Definition SBuf.cc:509
size_type length() const
Returns the number of bytes stored in SBuf.
Definition SBuf.h:419
int plength() const
Definition SBuf.h:426
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
size_type find(char const ch) const
Definition String.cc:436
static const size_type npos
Definition SquidString.h:40
char const * termedBuf() const
Definition SquidString.h:93
void append(char const *buf, int len)
Definition String.cc:131
size_type size() const
Definition SquidString.h:74
static const char * delimiter
#define xisspace(x)
Definition xis.h:15