Imported Upstream version 1.21.0
[platform/upstream/grpc.git] / src / core / ext / transport / chttp2 / transport / incoming_metadata.cc
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/transport/chttp2/transport/incoming_metadata.h"
22
23 #include <string.h>
24
25 #include "src/core/ext/transport/chttp2/transport/internal.h"
26
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
29
30 grpc_error* grpc_chttp2_incoming_metadata_buffer_add(
31     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_mdelem elem) {
32   buffer->size += GRPC_MDELEM_LENGTH(elem);
33   grpc_linked_mdelem* storage;
34   if (buffer->count < buffer->kPreallocatedMDElem) {
35     storage = &buffer->preallocated_mdelems[buffer->count];
36     buffer->count++;
37   } else {
38     storage = static_cast<grpc_linked_mdelem*>(
39         buffer->arena->Alloc(sizeof(grpc_linked_mdelem)));
40   }
41   return grpc_metadata_batch_add_tail(&buffer->batch, storage, elem);
42 }
43
44 grpc_error* grpc_chttp2_incoming_metadata_buffer_replace_or_add(
45     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_mdelem elem) {
46   for (grpc_linked_mdelem* l = buffer->batch.list.head; l != nullptr;
47        l = l->next) {
48     if (grpc_slice_eq(GRPC_MDKEY(l->md), GRPC_MDKEY(elem))) {
49       GRPC_MDELEM_UNREF(l->md);
50       l->md = elem;
51       return GRPC_ERROR_NONE;
52     }
53   }
54   return grpc_chttp2_incoming_metadata_buffer_add(buffer, elem);
55 }
56
57 void grpc_chttp2_incoming_metadata_buffer_set_deadline(
58     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_millis deadline) {
59   buffer->batch.deadline = deadline;
60 }
61
62 void grpc_chttp2_incoming_metadata_buffer_publish(
63     grpc_chttp2_incoming_metadata_buffer* buffer, grpc_metadata_batch* batch) {
64   grpc_metadata_batch_move(&buffer->batch, batch);
65 }