<commit_before>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2016 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "mcbp.h"

#include "buckets.h"
#include "debug_helpers.h"
#include "memcached.h"
#include "utilities/logtags.h"
#include "xattr/utils.h"

#include <mcbp/protocol/framebuilder.h>
#include <platform/compress.h>

static cb::const_byte_buffer mcbp_add_header(Cookie& cookie,
                                             cb::Pipe& pipe,
                                             uint8_t opcode,
                                             uint16_t err,
                                             uint8_t ext_len,
                                             uint16_t key_len,
                                             uint32_t body_len,
                                             uint8_t datatype,
                                             uint32_t opaque,
                                             uint64_t cas) {
    auto wbuf = pipe.wdata();
    auto* header = (protocol_binary_response_header*)wbuf.data();

    header->response.opcode = opcode;
    header->response.extlen = ext_len;
    header->response.datatype = datatype;
    header->response.status = (uint16_t)htons(err);
    header->response.opaque = opaque;
    header->response.cas = htonll(cas);

    if (cookie.isTracingEnabled()) {
        // When tracing is enabled we'll be using the alternative
        // response header where we inject the framing header.
        // For now we'll just hard-code the adding of the bytes
        // for the tracing info.
        //
        // Moving forward we should get a builder for encoding the
        // framing header (but do that the next time we need to add
        // something so that we have a better understanding on how
        // we need to do that (it could be that we need to modify
        // an already existing section etc).
        header->response.magic = uint8_t(cb::mcbp::Magic::AltClientResponse);
        // The framing extras when we just include the tracing information
        // is 3 bytes. 1 byte with id and length, then the 2 bytes
        // containing the actual data.
        const uint8_t framing_extras_size = 0x03;
        const uint8_t tracing_framing_id = 0x02;

        header->bytes[2] = framing_extras_size; // framing header extras 3 bytes
        header->bytes[3] = uint8_t(key_len);
        header->response.setBodylen(body_len + framing_extras_size);

        auto& tracer = cookie.getTracer();
        const auto val = htons(tracer.getEncodedMicros());
        auto* ptr = header->bytes + sizeof(header->bytes);
        *ptr = tracing_framing_id;
        ptr++;
        memcpy(ptr, &val, sizeof(val));<commit_msg>MB-29783: Use MCBP_TRACING_RESPONSE_SIZE in mcbp.cc to ensure consistency<commit_after>/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 *     Copyright 2016 Couchbase, Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
#include "mcbp.h"

#include "buckets.h"
#include "debug_helpers.h"
#include "memcached.h"
#include "utilities/logtags.h"
#include "xattr/utils.h"

#include <mcbp/protocol/framebuilder.h>
#include <platform/compress.h>

static cb::const_byte_buffer mcbp_add_header(Cookie& cookie,
                                             cb::Pipe& pipe,
                                             uint8_t opcode,
                                             uint16_t err,
                                             uint8_t ext_len,
                                             uint16_t key_len,
                                             uint32_t body_len,
                                             uint8_t datatype,
                                             uint32_t opaque,
                                             uint64_t cas) {
    auto wbuf = pipe.wdata();
    auto* header = (protocol_binary_response_header*)wbuf.data();

    header->response.opcode = opcode;
    header->response.extlen = ext_len;
    header->response.datatype = datatype;
    header->response.status = (uint16_t)htons(err);
    header->response.opaque = opaque;
    header->response.cas = htonll(cas);

    if (cookie.isTracingEnabled()) {
        // When tracing is enabled we'll be using the alternative
        // response header where we inject the framing header.
        // For now we'll just hard-code the adding of the bytes
        // for the tracing info.
        //
        // Moving forward we should get a builder for encoding the
        // framing header (but do that the next time we need to add
        // something so that we have a better understanding on how
        // we need to do that (it could be that we need to modify
        // an already existing section etc).
        header->response.magic = uint8_t(cb::mcbp::Magic::AltClientResponse);
        // The framing extras when we just include the tracing information
        // is 3 bytes. 1 byte with id and length, then the 2 bytes
        // containing the actual data.
        const uint8_t framing_extras_size = MCBP_TRACING_RESPONSE_SIZE;
        const uint8_t tracing_framing_id = 0x02;

        header->bytes[2] = framing_extras_size; // framing header extras 3 bytes
        header->bytes[3] = uint8_t(key_len);
        header->response.setBodylen(body_len + framing_extras_size);

        auto& tracer = cookie.getTracer();
        const auto val = htons(tracer.getEncodedMicros());
        auto* ptr = header->bytes + sizeof(header->bytes);
        *ptr = tracing_framing_id;
        ptr++;
        memcpy(ptr, &val, sizeof(val));
{'fraction_non_alphanumeric': 0.08896531100478469, 'fraction_numerical': 0.012559808612440191, 'mean_word_length': 1.938927943760984, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 36, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6108857', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1757, 'n_words': 596}