Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Robbie Shade | 710d242 | 2016-07-13 15:15:38 -0400 | [diff] [blame] | 19 | #include "src/core/lib/transport/timeout_encoding.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | |
Jan Tattermusch | 4b3ecdf | 2015-12-04 09:33:05 -0800 | [diff] [blame] | 24 | #include <grpc/support/port_platform.h> |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 25 | #include "src/core/lib/support/string.h" |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 26 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 27 | static int64_t round_up(int64_t x, int64_t divisor) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 28 | return (x / divisor + (x % divisor != 0)) * divisor; |
| 29 | } |
| 30 | |
| 31 | /* round an integer up to the next value with three significant figures */ |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 32 | static int64_t round_up_to_three_sig_figs(int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 33 | if (x < 1000) return x; |
| 34 | if (x < 10000) return round_up(x, 10); |
| 35 | if (x < 100000) return round_up(x, 100); |
| 36 | if (x < 1000000) return round_up(x, 1000); |
| 37 | if (x < 10000000) return round_up(x, 10000); |
| 38 | if (x < 100000000) return round_up(x, 100000); |
| 39 | if (x < 1000000000) return round_up(x, 1000000); |
| 40 | return round_up(x, 10000000); |
| 41 | } |
| 42 | |
| 43 | /* encode our minimum viable timeout value */ |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 44 | static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); } |
| 45 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 46 | static void enc_ext(char *buffer, int64_t value, char ext) { |
| 47 | int n = int64_ttoa(value, buffer); |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 48 | buffer[n] = ext; |
Yang Gao | 5fd0d29 | 2015-01-26 00:19:48 -0800 | [diff] [blame] | 49 | buffer[n + 1] = 0; |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 50 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 51 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 52 | static void enc_seconds(char *buffer, int64_t sec) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 53 | if (sec % 3600 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 54 | enc_ext(buffer, sec / 3600, 'H'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 55 | } else if (sec % 60 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 56 | enc_ext(buffer, sec / 60, 'M'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 57 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 58 | enc_ext(buffer, sec, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 62 | static void enc_nanos(char *buffer, int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 63 | x = round_up_to_three_sig_figs(x); |
| 64 | if (x < 100000) { |
| 65 | if (x % 1000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 66 | enc_ext(buffer, x / 1000, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 67 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 68 | enc_ext(buffer, x, 'n'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 69 | } |
| 70 | } else if (x < 100000000) { |
| 71 | if (x % 1000000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 72 | enc_ext(buffer, x / 1000000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 73 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 74 | enc_ext(buffer, x / 1000, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 75 | } |
| 76 | } else if (x < 1000000000) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 77 | enc_ext(buffer, x / 1000000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | } else { |
| 79 | /* note that this is only ever called with times of less than one second, |
| 80 | so if we reach here the time must have been rounded up to a whole second |
| 81 | (and no more) */ |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 82 | memcpy(buffer, "1S", 3); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 86 | static void enc_micros(char *buffer, int64_t x) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 87 | x = round_up_to_three_sig_figs(x); |
| 88 | if (x < 100000) { |
| 89 | if (x % 1000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 90 | enc_ext(buffer, x / 1000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 91 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 92 | enc_ext(buffer, x, 'u'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 93 | } |
| 94 | } else if (x < 100000000) { |
| 95 | if (x % 1000000 == 0) { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 96 | enc_ext(buffer, x / 1000000, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 97 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 98 | enc_ext(buffer, x / 1000, 'm'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 99 | } |
| 100 | } else { |
Craig Tiller | 985463d | 2015-01-23 11:13:10 -0800 | [diff] [blame] | 101 | enc_ext(buffer, x / 1000000, 'S'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Robbie Shade | 710d242 | 2016-07-13 15:15:38 -0400 | [diff] [blame] | 105 | void grpc_http2_encode_timeout(gpr_timespec timeout, char *buffer) { |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | if (timeout.tv_sec < 0) { |
| 107 | enc_tiny(buffer); |
| 108 | } else if (timeout.tv_sec == 0) { |
| 109 | enc_nanos(buffer, timeout.tv_nsec); |
| 110 | } else if (timeout.tv_sec < 1000 && timeout.tv_nsec != 0) { |
| 111 | enc_micros(buffer, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 112 | (int64_t)(timeout.tv_sec * 1000000) + |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 113 | (timeout.tv_nsec / 1000 + (timeout.tv_nsec % 1000 != 0))); |
| 114 | } else { |
| 115 | enc_seconds(buffer, timeout.tv_sec + (timeout.tv_nsec != 0)); |
| 116 | } |
| 117 | } |
| 118 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 119 | static int is_all_whitespace(const char *p, const char *end) { |
| 120 | while (p != end && *p == ' ') p++; |
| 121 | return p == end; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 124 | int grpc_http2_decode_timeout(grpc_slice text, gpr_timespec *timeout) { |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 125 | int32_t x = 0; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 126 | const uint8_t *p = GRPC_SLICE_START_PTR(text); |
| 127 | const uint8_t *end = GRPC_SLICE_END_PTR(text); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 128 | int have_digit = 0; |
| 129 | /* skip whitespace */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 130 | for (; p != end && *p == ' '; p++) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 131 | ; |
| 132 | /* decode numeric part */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 133 | for (; p != end && *p >= '0' && *p <= '9'; p++) { |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 134 | int32_t digit = (int32_t)(*p - (uint8_t)'0'); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 135 | have_digit = 1; |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 136 | /* spec allows max. 8 digits, but we allow values up to 1,000,000,000 */ |
| 137 | if (x >= (100 * 1000 * 1000)) { |
| 138 | if (x != (100 * 1000 * 1000) || digit != 0) { |
Craig Tiller | d262454 | 2016-03-21 14:31:01 -0700 | [diff] [blame] | 139 | *timeout = gpr_inf_future(GPR_TIMESPAN); |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 140 | return 1; |
| 141 | } |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 142 | } |
Jan Tattermusch | 6633647 | 2016-01-19 17:55:40 -0800 | [diff] [blame] | 143 | x = x * 10 + digit; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 144 | } |
| 145 | if (!have_digit) return 0; |
| 146 | /* skip whitespace */ |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 147 | for (; p != end && *p == ' '; p++) |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 148 | ; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 149 | if (p == end) return 0; |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 150 | /* decode unit specifier */ |
| 151 | switch (*p) { |
| 152 | case 'n': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 153 | *timeout = gpr_time_from_nanos(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 154 | break; |
| 155 | case 'u': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 156 | *timeout = gpr_time_from_micros(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 157 | break; |
| 158 | case 'm': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 159 | *timeout = gpr_time_from_millis(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 160 | break; |
| 161 | case 'S': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 162 | *timeout = gpr_time_from_seconds(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 163 | break; |
| 164 | case 'M': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 165 | *timeout = gpr_time_from_minutes(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 166 | break; |
| 167 | case 'H': |
Craig Tiller | 58bbc86 | 2015-07-13 09:51:17 -0700 | [diff] [blame] | 168 | *timeout = gpr_time_from_hours(x, GPR_TIMESPAN); |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 169 | break; |
| 170 | default: |
| 171 | return 0; |
| 172 | } |
| 173 | p++; |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 174 | return is_all_whitespace((const char *)p, (const char *)end); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 175 | } |