Github user shukitchan commented on a diff in the pull request: https://github.com/apache/trafficserver/pull/1516#discussion_r103590528 --- Diff: plugins/esi/combo_handler.cc --- @@ -187,6 +207,94 @@ InterceptData::~InterceptData() } } +void +CacheControlHeader::update(TSMBuffer bufp, TSMLoc hdr_loc) +{ + vector<string> values; + bool found_immutable = false; + bool found_private = false; + + // Load each value from the Cache-Control header into the vector values + TSMLoc field_loc = TSMimeHdrFieldFind(bufp, hdr_loc, TS_MIME_FIELD_CACHE_CONTROL, TS_MIME_LEN_CACHE_CONTROL); + if (field_loc != TS_NULL_MLOC) { + int n_values = TSMimeHdrFieldValuesCount(bufp, hdr_loc, field_loc); + if ((n_values != TS_ERROR) && (n_values > 0)) { + for (int i = 0; i < n_values; i++) { + // Grab this current header value + int _val_len = 0; + const char *_val = TSMimeHdrFieldValueStringGet(bufp, hdr_loc, field_loc, i, &_val_len); + string val(_val, _val_len); + + // We want the header value to be lowercase since CC headers are case insensitive + transform(val.begin(), val.end(), val.begin(), ::tolower); + values.push_back(val); + } + } else { + TSHandleMLocRelease(bufp, hdr_loc, field_loc); + return; + } + TSHandleMLocRelease(bufp, hdr_loc, field_loc); + } else { + return; + } + + for (auto const &val : values) { + // Update max-age if necessary + if (val.find(TS_HTTP_VALUE_MAX_AGE) != string::npos) { + int max_age = -1; + char *ptr = const_cast<char *>(val.c_str()); + ptr += TS_HTTP_LEN_MAX_AGE; + while ((*ptr == ' ') || (*ptr == '\t')) + ptr++; + if (*ptr == '=') { + ptr++; + max_age = atoi(ptr); + } + if (max_age > 0 && max_age < _max_age) { + _max_age = max_age; + } + // If we find even a single occurrence of private, the whole response must be private + } else if (val.find(TS_HTTP_VALUE_PRIVATE) != string::npos) { + found_private = true; + // Every requested document must have immutable for the final response to be immutable + } else if (val.find(HTTP_IMMUTABLE) != string::npos) { + found_immutable = true; + } + } + + if (!found_immutable) { + LOG_DEBUG("Did not see an immutable cache control. The response will be not be immutable"); + _immutable = false; + } + + if (found_private) { + LOG_DEBUG("Saw a private cache control. The response will be private"); + _public = false; + _private = true; + } +} + +string +CacheControlHeader::generate() const +{ + char line_buf[256]; + const char *publicity; + const char *immutable; + + if (_public) { + publicity = TS_HTTP_VALUE_PUBLIC; + } else if (_private) { + publicity = TS_HTTP_VALUE_PRIVATE; + } else { + // Default is public + publicity = TS_HTTP_VALUE_PUBLIC; + } + immutable = (_immutable ? ", " HTTP_IMMUTABLE : ""); + + sprintf(line_buf, "Cache-Control: max-age=%d, %s%s\r\n", _max_age, publicity, immutable); --- End diff -- if we happen to support and have a case of having a really large (!) number for max age, we might have buffer overflow. Then we might need to consider snprintf or something safer?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---