murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 22 | #include <grpc/grpc.h> |
| 23 | #include <grpc/support/alloc.h> |
murgatroid99 | 36187ab | 2016-01-06 13:23:59 -0800 | [diff] [blame] | 24 | #include <grpc/support/port_platform.h> |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 25 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 26 | #include "src/core/lib/iomgr/error.h" |
ncteisen | bbb3801 | 2017-03-10 14:58:43 -0800 | [diff] [blame] | 27 | #include "src/core/lib/slice/slice_internal.h" |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 28 | #include "src/core/lib/slice/slice_string_helpers.h" |
| 29 | |
| 30 | static grpc_error *conforms_to(grpc_slice slice, const uint8_t *legal_bits, |
| 31 | const char *err_desc) { |
| 32 | const uint8_t *p = GRPC_SLICE_START_PTR(slice); |
| 33 | const uint8_t *e = GRPC_SLICE_END_PTR(slice); |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 34 | for (; p != e; p++) { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 35 | int idx = *p; |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 36 | int byte = idx / 8; |
| 37 | int bit = idx % 8; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 38 | if ((legal_bits[byte] & (1 << bit)) == 0) { |
| 39 | char *dump = grpc_dump_slice(slice, GPR_DUMP_HEX | GPR_DUMP_ASCII); |
| 40 | grpc_error *error = grpc_error_set_str( |
Noah Eisen | 3005ce8 | 2017-03-14 13:38:41 -0700 | [diff] [blame] | 41 | grpc_error_set_int(GRPC_ERROR_CREATE_FROM_COPIED_STRING(err_desc), |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 42 | GRPC_ERROR_INT_OFFSET, |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 43 | p - GRPC_SLICE_START_PTR(slice)), |
ncteisen | bbb3801 | 2017-03-10 14:58:43 -0800 | [diff] [blame] | 44 | GRPC_ERROR_STR_RAW_BYTES, grpc_slice_from_copied_string(dump)); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 45 | gpr_free(dump); |
| 46 | return error; |
| 47 | } |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 48 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 49 | return GRPC_ERROR_NONE; |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 52 | static int error2int(grpc_error *error) { |
| 53 | int r = (error == GRPC_ERROR_NONE); |
| 54 | GRPC_ERROR_UNREF(error); |
| 55 | return r; |
| 56 | } |
| 57 | |
| 58 | grpc_error *grpc_validate_header_key_is_legal(grpc_slice slice) { |
murgatroid99 | d12d5ba | 2016-01-08 13:26:23 -0800 | [diff] [blame] | 59 | static const uint8_t legal_header_bits[256 / 8] = { |
Craig Tiller | 6f87164 | 2016-02-03 16:15:31 -0800 | [diff] [blame] | 60 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0xff, 0x03, 0x00, 0x00, 0x00, |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 61 | 0x80, 0xfe, 0xff, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 62 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 63 | if (GRPC_SLICE_LENGTH(slice) == 0) { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 64 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 65 | "Metadata keys cannot be zero length"); |
Craig Tiller | 296c7bb | 2017-01-10 15:34:02 -0800 | [diff] [blame] | 66 | } |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 67 | if (GRPC_SLICE_START_PTR(slice)[0] == ':') { |
ncteisen | 4b36a3d | 2017-03-13 19:08:06 -0700 | [diff] [blame] | 68 | return GRPC_ERROR_CREATE_FROM_STATIC_STRING( |
| 69 | "Metadata keys cannot start with :"); |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 70 | } |
| 71 | return conforms_to(slice, legal_header_bits, "Illegal header key"); |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 74 | int grpc_header_key_is_legal(grpc_slice slice) { |
| 75 | return error2int(grpc_validate_header_key_is_legal(slice)); |
| 76 | } |
| 77 | |
| 78 | grpc_error *grpc_validate_header_nonbin_value_is_legal(grpc_slice slice) { |
murgatroid99 | d12d5ba | 2016-01-08 13:26:23 -0800 | [diff] [blame] | 79 | static const uint8_t legal_header_bits[256 / 8] = { |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 80 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 81 | 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 82 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 83 | return conforms_to(slice, legal_header_bits, "Illegal header value"); |
Craig Tiller | f2b5b7e | 2017-01-10 08:28:59 -0800 | [diff] [blame] | 84 | } |
| 85 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 86 | int grpc_header_nonbin_value_is_legal(grpc_slice slice) { |
| 87 | return error2int(grpc_validate_header_nonbin_value_is_legal(slice)); |
| 88 | } |
| 89 | |
| 90 | int grpc_is_binary_header(grpc_slice slice) { |
| 91 | if (GRPC_SLICE_LENGTH(slice) < 5) return 0; |
| 92 | return 0 == memcmp(GRPC_SLICE_END_PTR(slice) - 4, "-bin", 4); |
murgatroid99 | c3910ca | 2016-01-06 13:14:23 -0800 | [diff] [blame] | 93 | } |