3 * Copyright 2015 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/transport/timeout_encoding.h"
26 #include "src/core/lib/gpr/string.h"
28 static int64_t round_up(int64_t x, int64_t divisor) {
29 return (x / divisor + (x % divisor != 0)) * divisor;
32 /* round an integer up to the next value with three significant figures */
33 static int64_t round_up_to_three_sig_figs(int64_t x) {
34 if (x < 1000) return x;
35 if (x < 10000) return round_up(x, 10);
36 if (x < 100000) return round_up(x, 100);
37 if (x < 1000000) return round_up(x, 1000);
38 if (x < 10000000) return round_up(x, 10000);
39 if (x < 100000000) return round_up(x, 100000);
40 if (x < 1000000000) return round_up(x, 1000000);
41 return round_up(x, 10000000);
44 /* encode our minimum viable timeout value */
45 static void enc_tiny(char* buffer) { memcpy(buffer, "1n", 3); }
47 /* encode our maximum timeout value, about 1157 days */
48 static void enc_huge(char* buffer) { memcpy(buffer, "99999999S", 10); }
50 static void enc_ext(char* buffer, int64_t value, char ext) {
51 int n = int64_ttoa(value, buffer);
56 static void enc_seconds(char* buffer, int64_t sec) {
57 sec = round_up_to_three_sig_figs(sec);
58 if (sec % 3600 == 0) {
59 enc_ext(buffer, sec / 3600, 'H');
60 } else if (sec % 60 == 0) {
61 enc_ext(buffer, sec / 60, 'M');
63 enc_ext(buffer, sec, 'S');
67 static void enc_millis(char* buffer, int64_t x) {
68 x = round_up_to_three_sig_figs(x);
69 if (x < GPR_MS_PER_SEC) {
70 enc_ext(buffer, x, 'm');
72 if (x % GPR_MS_PER_SEC == 0) {
73 enc_seconds(buffer, x / GPR_MS_PER_SEC);
75 enc_ext(buffer, x, 'm');
80 void grpc_http2_encode_timeout(grpc_millis timeout, char* buffer) {
81 const grpc_millis kMaxTimeout = 99999999000;
84 } else if (timeout < 1000 * GPR_MS_PER_SEC) {
85 enc_millis(buffer, timeout);
86 } else if (timeout >= kMaxTimeout) {
90 timeout / GPR_MS_PER_SEC + (timeout % GPR_MS_PER_SEC != 0));
94 static int is_all_whitespace(const char* p, const char* end) {
95 while (p != end && *p == ' ') p++;
99 int grpc_http2_decode_timeout(const grpc_slice& text, grpc_millis* timeout) {
101 const uint8_t* p = GRPC_SLICE_START_PTR(text);
102 const uint8_t* end = GRPC_SLICE_END_PTR(text);
104 /* skip whitespace */
105 for (; p != end && *p == ' '; p++)
107 /* decode numeric part */
108 for (; p != end && *p >= '0' && *p <= '9'; p++) {
109 int32_t digit = static_cast<int32_t>(*p - static_cast<uint8_t>('0'));
111 /* spec allows max. 8 digits, but we allow values up to 1,000,000,000 */
112 if (x >= (100 * 1000 * 1000)) {
113 if (x != (100 * 1000 * 1000) || digit != 0) {
114 *timeout = GRPC_MILLIS_INF_FUTURE;
120 if (!have_digit) return 0;
121 /* skip whitespace */
122 for (; p != end && *p == ' '; p++)
124 if (p == end) return 0;
125 /* decode unit specifier */
128 *timeout = x / GPR_NS_PER_MS + (x % GPR_NS_PER_MS != 0);
131 *timeout = x / GPR_US_PER_MS + (x % GPR_US_PER_MS != 0);
137 *timeout = x * GPR_MS_PER_SEC;
140 *timeout = x * 60 * GPR_MS_PER_SEC;
143 *timeout = x * 60 * 60 * GPR_MS_PER_SEC;
149 return is_all_whitespace(reinterpret_cast<const char*>(p),
150 reinterpret_cast<const char*>(end));