HttpHeaderTools.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 "compat/strtoll.h"
13 #include "HttpHdrContRange.h"
14 #include "HttpHeader.h"
15 #include "HttpHeaderTools.h"
16 #include "MemBuf.h"
17 #include "StrList.h"
18 
19 #include <cerrno>
20 
21 static void httpHeaderPutStrvf(HttpHeader * hdr, Http::HdrType id, const char *fmt, va_list vargs);
22 
23 /* same as httpHeaderPutStr, but formats the string using snprintf first */
24 void
25 httpHeaderPutStrf(HttpHeader * hdr, Http::HdrType id, const char *fmt,...)
26 {
27  va_list args;
28  va_start(args, fmt);
29 
30  httpHeaderPutStrvf(hdr, id, fmt, args);
31  va_end(args);
32 }
33 
34 /* used by httpHeaderPutStrf */
35 static void
36 httpHeaderPutStrvf(HttpHeader * hdr, Http::HdrType id, const char *fmt, va_list vargs)
37 {
38  MemBuf mb;
39  mb.init();
40  mb.vappendf(fmt, vargs);
41  hdr->putStr(id, mb.buf);
42  mb.clean();
43 }
44 
46 void
47 httpHeaderAddContRange(HttpHeader * hdr, HttpHdrRangeSpec spec, int64_t ent_len)
48 {
50  assert(hdr && ent_len >= 0);
51  httpHdrContRangeSet(cr, spec, ent_len);
52  hdr->putContRange(cr);
53  delete cr;
54 }
55 
61 bool
62 httpHeaderHasConnDir(const HttpHeader * hdr, const SBuf &directive)
63 {
64  String list;
65 
66  /* what type of header do we have? */
67  if (hdr->getList(Http::HdrType::CONNECTION, &list))
68  return strListIsMember(&list, directive, ',') != 0;
69 
70 #if USE_HTTP_VIOLATIONS
72  return strListIsMember(&list, directive, ',') != 0;
73 #endif
74 
75  // else, no connection header for it to exist in
76  return false;
77 }
78 
83 int
84 httpHeaderParseInt(const char *start, int *value)
85 {
86  assert(value);
87  *value = atoi(start);
88 
89  if (!*value && !xisdigit(*start)) {
90  debugs(66, 2, "failed to parse an int header field near '" << start << "'");
91  return 0;
92  }
93 
94  return 1;
95 }
96 
97 bool
98 httpHeaderParseOffset(const char *start, int64_t *value, char **endPtr)
99 {
100  char *end = nullptr;
101  errno = 0;
102  const int64_t res = strtoll(start, &end, 10);
103  if (errno && !res) {
104  debugs(66, 7, "failed to parse malformed offset in " << start);
105  return false;
106  }
107  if (errno == ERANGE && (res == LLONG_MIN || res == LLONG_MAX)) { // no overflow
108  debugs(66, 7, "failed to parse huge offset in " << start);
109  return false;
110  }
111  if (start == end) {
112  debugs(66, 7, "failed to parse empty offset");
113  return false;
114  }
115  *value = res;
116  if (endPtr)
117  *endPtr = end;
118  debugs(66, 7, "offset " << start << " parsed as " << res);
119  return true;
120 }
121 
char * buf
Definition: MemBuf.h:134
HttpHdrContRange * httpHdrContRangeCreate(void)
@ PROXY_CONNECTION
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
Definition: SBuf.h:93
void httpHdrContRangeSet(HttpHdrContRange *cr, HttpHdrRangeSpec spec, int64_t ent_len)
String getList(Http::HdrType id) const
Definition: HttpHeader.cc:921
void httpHeaderPutStrf(HttpHeader *hdr, Http::HdrType id, const char *fmt,...)
void putContRange(const HttpHdrContRange *cr)
Definition: HttpHeader.cc:1159
bool httpHeaderHasConnDir(const HttpHeader *hdr, const SBuf &directive)
int strListIsMember(const String *list, const SBuf &m, char del)
Definition: StrList.cc:46
Definition: MemBuf.h:23
void clean()
Definition: MemBuf.cc:110
bool httpHeaderParseOffset(const char *start, int64_t *value, char **endPtr)
#define assert(EX)
Definition: assert.h:17
#define xisdigit(x)
Definition: xis.h:18
int64_t strtoll(const char *nptr, char **endptr, int base)
Definition: strtoll.c:61
void vappendf(const char *fmt, va_list ap) override
Definition: MemBuf.cc:251
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1128
int httpHeaderParseInt(const char *start, int *value)
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void httpHeaderAddContRange(HttpHeader *hdr, HttpHdrRangeSpec spec, int64_t ent_len)
static void httpHeaderPutStrvf(HttpHeader *hdr, Http::HdrType id, const char *fmt, va_list vargs)

 

Introduction

Documentation

Support

Miscellaneous