blob: 02f179d6a3a5cd420844b9944972627f198228a1 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
Robbie Shade710d2422016-07-13 15:15:38 -040019#include "src/core/lib/transport/timeout_encoding.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080020
21#include <stdio.h>
22#include <string.h>
23
Jan Tattermusch4b3ecdf2015-12-04 09:33:05 -080024#include <grpc/support/port_platform.h>
Craig Tiller9533d042016-03-25 17:11:06 -070025#include "src/core/lib/support/string.h"
Craig Tiller985463d2015-01-23 11:13:10 -080026
Craig Tiller7536af02015-12-22 13:49:30 -080027static int64_t round_up(int64_t x, int64_t divisor) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080028 return (x / divisor + (x % divisor != 0)) * divisor;
29}
30
31/* round an integer up to the next value with three significant figures */
Craig Tiller7536af02015-12-22 13:49:30 -080032static int64_t round_up_to_three_sig_figs(int64_t x) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080033 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 Tiller985463d2015-01-23 11:13:10 -080044static void enc_tiny(char *buffer) { memcpy(buffer, "1n", 3); }
45
Craig Tiller7536af02015-12-22 13:49:30 -080046static void enc_ext(char *buffer, int64_t value, char ext) {
47 int n = int64_ttoa(value, buffer);
Craig Tiller985463d2015-01-23 11:13:10 -080048 buffer[n] = ext;
Yang Gao5fd0d292015-01-26 00:19:48 -080049 buffer[n + 1] = 0;
Craig Tiller985463d2015-01-23 11:13:10 -080050}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080051
Craig Tiller7536af02015-12-22 13:49:30 -080052static void enc_seconds(char *buffer, int64_t sec) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053 if (sec % 3600 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080054 enc_ext(buffer, sec / 3600, 'H');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080055 } else if (sec % 60 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080056 enc_ext(buffer, sec / 60, 'M');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080057 } else {
Craig Tiller985463d2015-01-23 11:13:10 -080058 enc_ext(buffer, sec, 'S');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080059 }
60}
61
Craig Tiller7536af02015-12-22 13:49:30 -080062static void enc_nanos(char *buffer, int64_t x) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080063 x = round_up_to_three_sig_figs(x);
64 if (x < 100000) {
65 if (x % 1000 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080066 enc_ext(buffer, x / 1000, 'u');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080067 } else {
Craig Tiller985463d2015-01-23 11:13:10 -080068 enc_ext(buffer, x, 'n');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080069 }
70 } else if (x < 100000000) {
71 if (x % 1000000 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080072 enc_ext(buffer, x / 1000000, 'm');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080073 } else {
Craig Tiller985463d2015-01-23 11:13:10 -080074 enc_ext(buffer, x / 1000, 'u');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080075 }
76 } else if (x < 1000000000) {
Craig Tiller985463d2015-01-23 11:13:10 -080077 enc_ext(buffer, x / 1000000, 'm');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078 } 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 Tiller985463d2015-01-23 11:13:10 -080082 memcpy(buffer, "1S", 3);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080083 }
84}
85
Craig Tiller7536af02015-12-22 13:49:30 -080086static void enc_micros(char *buffer, int64_t x) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080087 x = round_up_to_three_sig_figs(x);
88 if (x < 100000) {
89 if (x % 1000 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080090 enc_ext(buffer, x / 1000, 'm');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080091 } else {
Craig Tiller985463d2015-01-23 11:13:10 -080092 enc_ext(buffer, x, 'u');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080093 }
94 } else if (x < 100000000) {
95 if (x % 1000000 == 0) {
Craig Tiller985463d2015-01-23 11:13:10 -080096 enc_ext(buffer, x / 1000000, 'S');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080097 } else {
Craig Tiller985463d2015-01-23 11:13:10 -080098 enc_ext(buffer, x / 1000, 'm');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080099 }
100 } else {
Craig Tiller985463d2015-01-23 11:13:10 -0800101 enc_ext(buffer, x / 1000000, 'S');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800102 }
103}
104
Robbie Shade710d2422016-07-13 15:15:38 -0400105void grpc_http2_encode_timeout(gpr_timespec timeout, char *buffer) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106 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 Tiller7536af02015-12-22 13:49:30 -0800112 (int64_t)(timeout.tv_sec * 1000000) +
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800113 (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 Tiller7c70b6c2017-01-23 07:48:42 -0800119static int is_all_whitespace(const char *p, const char *end) {
120 while (p != end && *p == ' ') p++;
121 return p == end;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800122}
123
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800124int grpc_http2_decode_timeout(grpc_slice text, gpr_timespec *timeout) {
Jan Tattermusch66336472016-01-19 17:55:40 -0800125 int32_t x = 0;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800126 const uint8_t *p = GRPC_SLICE_START_PTR(text);
127 const uint8_t *end = GRPC_SLICE_END_PTR(text);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800128 int have_digit = 0;
129 /* skip whitespace */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800130 for (; p != end && *p == ' '; p++)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800131 ;
132 /* decode numeric part */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800133 for (; p != end && *p >= '0' && *p <= '9'; p++) {
Jan Tattermusch66336472016-01-19 17:55:40 -0800134 int32_t digit = (int32_t)(*p - (uint8_t)'0');
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800135 have_digit = 1;
Jan Tattermusch66336472016-01-19 17:55:40 -0800136 /* 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 Tillerd2624542016-03-21 14:31:01 -0700139 *timeout = gpr_inf_future(GPR_TIMESPAN);
Jan Tattermusch66336472016-01-19 17:55:40 -0800140 return 1;
141 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800142 }
Jan Tattermusch66336472016-01-19 17:55:40 -0800143 x = x * 10 + digit;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144 }
145 if (!have_digit) return 0;
146 /* skip whitespace */
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800147 for (; p != end && *p == ' '; p++)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800148 ;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800149 if (p == end) return 0;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800150 /* decode unit specifier */
151 switch (*p) {
152 case 'n':
Craig Tiller58bbc862015-07-13 09:51:17 -0700153 *timeout = gpr_time_from_nanos(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800154 break;
155 case 'u':
Craig Tiller58bbc862015-07-13 09:51:17 -0700156 *timeout = gpr_time_from_micros(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800157 break;
158 case 'm':
Craig Tiller58bbc862015-07-13 09:51:17 -0700159 *timeout = gpr_time_from_millis(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800160 break;
161 case 'S':
Craig Tiller58bbc862015-07-13 09:51:17 -0700162 *timeout = gpr_time_from_seconds(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800163 break;
164 case 'M':
Craig Tiller58bbc862015-07-13 09:51:17 -0700165 *timeout = gpr_time_from_minutes(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800166 break;
167 case 'H':
Craig Tiller58bbc862015-07-13 09:51:17 -0700168 *timeout = gpr_time_from_hours(x, GPR_TIMESPAN);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800169 break;
170 default:
171 return 0;
172 }
173 p++;
Craig Tiller7c70b6c2017-01-23 07:48:42 -0800174 return is_all_whitespace((const char *)p, (const char *)end);
Craig Tiller190d3602015-02-18 09:23:38 -0800175}