grpc 1.6.7 → 1.7.0.pre1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of grpc might be problematic. Click here for more details.

Files changed (277) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +579 -77
  3. data/include/grpc/byte_buffer.h +1 -63
  4. data/include/grpc/compression.h +27 -5
  5. data/include/grpc/fork.h +24 -0
  6. data/include/grpc/grpc.h +12 -6
  7. data/include/grpc/grpc_security.h +28 -7
  8. data/include/grpc/impl/codegen/atm.h +1 -0
  9. data/include/grpc/impl/codegen/byte_buffer.h +86 -0
  10. data/include/grpc/impl/codegen/compression_types.h +63 -5
  11. data/include/grpc/impl/codegen/fork.h +48 -0
  12. data/include/grpc/impl/codegen/grpc_types.h +26 -9
  13. data/include/grpc/impl/codegen/port_platform.h +11 -4
  14. data/include/grpc/impl/codegen/slice.h +6 -1
  15. data/include/grpc/impl/codegen/sync.h +3 -1
  16. data/include/grpc/impl/codegen/sync_custom.h +36 -0
  17. data/include/grpc/module.modulemap +75 -3
  18. data/include/grpc/slice.h +1 -5
  19. data/include/grpc/support/sync_custom.h +24 -0
  20. data/src/core/ext/census/base_resources.c +14 -14
  21. data/src/core/ext/census/context.c +7 -5
  22. data/src/core/ext/census/grpc_filter.c +12 -14
  23. data/src/core/ext/census/mlog.c +2 -1
  24. data/src/core/ext/census/resource.c +13 -9
  25. data/src/core/ext/filters/client_channel/channel_connectivity.c +15 -8
  26. data/src/core/ext/filters/client_channel/client_channel.c +418 -439
  27. data/src/core/ext/filters/client_channel/client_channel_factory.c +4 -5
  28. data/src/core/ext/filters/client_channel/client_channel_plugin.c +2 -2
  29. data/src/core/ext/filters/client_channel/http_connect_handshaker.c +7 -5
  30. data/src/core/ext/filters/client_channel/http_proxy.c +17 -21
  31. data/src/core/ext/filters/client_channel/lb_policy.c +1 -1
  32. data/src/core/ext/filters/client_channel/lb_policy/grpclb/client_load_reporting_filter.c +7 -7
  33. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.c +371 -257
  34. data/src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb_client_stats.c +7 -5
  35. data/src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.c +25 -14
  36. data/src/core/ext/filters/client_channel/lb_policy/pick_first/pick_first.c +16 -16
  37. data/src/core/ext/filters/client_channel/lb_policy/round_robin/round_robin.c +33 -28
  38. data/src/core/ext/filters/client_channel/lb_policy_factory.c +10 -8
  39. data/src/core/ext/filters/client_channel/lb_policy_factory.h +1 -1
  40. data/src/core/ext/filters/client_channel/proxy_mapper_registry.c +1 -1
  41. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.c +7 -6
  42. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c +62 -28
  43. data/src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.c +29 -23
  44. data/src/core/ext/filters/client_channel/resolver/fake/fake_resolver.c +25 -14
  45. data/src/core/ext/filters/client_channel/retry_throttle.c +9 -6
  46. data/src/core/ext/filters/client_channel/subchannel.c +30 -30
  47. data/src/core/ext/filters/client_channel/subchannel.h +1 -4
  48. data/src/core/ext/filters/client_channel/subchannel_index.c +31 -15
  49. data/src/core/ext/filters/client_channel/subchannel_index.h +7 -0
  50. data/src/core/ext/filters/client_channel/uri_parser.c +4 -3
  51. data/src/core/ext/filters/deadline/deadline_filter.c +78 -39
  52. data/src/core/ext/filters/deadline/deadline_filter.h +7 -1
  53. data/src/core/ext/filters/http/client/http_client_filter.c +14 -14
  54. data/src/core/ext/filters/http/http_filters_plugin.c +1 -1
  55. data/src/core/ext/filters/http/message_compress/message_compress_filter.c +240 -175
  56. data/src/core/ext/filters/http/server/http_server_filter.c +48 -36
  57. data/src/core/ext/filters/load_reporting/{load_reporting_filter.c → server_load_reporting_filter.c} +11 -12
  58. data/src/core/ext/filters/load_reporting/{load_reporting_filter.h → server_load_reporting_filter.h} +6 -5
  59. data/src/core/ext/filters/load_reporting/{load_reporting.c → server_load_reporting_plugin.c} +19 -13
  60. data/src/core/ext/filters/load_reporting/{load_reporting.h → server_load_reporting_plugin.h} +4 -3
  61. data/src/core/ext/filters/max_age/max_age_filter.c +2 -3
  62. data/src/core/ext/filters/message_size/message_size_filter.c +4 -2
  63. data/src/core/ext/filters/workarounds/workaround_cronet_compression_filter.c +0 -1
  64. data/src/core/ext/transport/chttp2/client/chttp2_connector.c +5 -5
  65. data/src/core/ext/transport/chttp2/client/insecure/channel_create.c +1 -1
  66. data/src/core/ext/transport/chttp2/client/insecure/channel_create_posix.c +1 -1
  67. data/src/core/ext/transport/chttp2/server/chttp2_server.c +20 -18
  68. data/src/core/ext/transport/chttp2/transport/chttp2_plugin.c +1 -0
  69. data/src/core/ext/transport/chttp2/transport/chttp2_transport.c +493 -210
  70. data/src/core/ext/transport/chttp2/transport/chttp2_transport.h +1 -0
  71. data/src/core/ext/transport/chttp2/transport/flow_control.c +9 -8
  72. data/src/core/ext/transport/chttp2/transport/frame_data.c +2 -2
  73. data/src/core/ext/transport/chttp2/transport/frame_goaway.c +2 -2
  74. data/src/core/ext/transport/chttp2/transport/frame_ping.c +5 -4
  75. data/src/core/ext/transport/chttp2/transport/frame_rst_stream.c +1 -1
  76. data/src/core/ext/transport/chttp2/transport/frame_settings.c +10 -9
  77. data/src/core/ext/transport/chttp2/transport/frame_window_update.c +9 -5
  78. data/src/core/ext/transport/chttp2/transport/hpack_encoder.c +62 -41
  79. data/src/core/ext/transport/chttp2/transport/hpack_parser.c +52 -8
  80. data/src/core/ext/transport/chttp2/transport/hpack_table.c +2 -2
  81. data/src/core/ext/transport/chttp2/transport/incoming_metadata.c +3 -2
  82. data/src/core/ext/transport/chttp2/transport/internal.h +60 -30
  83. data/src/core/ext/transport/chttp2/transport/parsing.c +16 -5
  84. data/src/core/ext/transport/chttp2/transport/stream_lists.c +36 -16
  85. data/src/core/ext/transport/chttp2/transport/stream_map.c +6 -4
  86. data/src/core/ext/transport/chttp2/transport/writing.c +133 -105
  87. data/src/core/ext/transport/inproc/inproc_transport.c +61 -65
  88. data/src/core/lib/channel/channel_args.c +112 -12
  89. data/src/core/lib/channel/channel_args.h +31 -0
  90. data/src/core/lib/channel/channel_stack.c +1 -15
  91. data/src/core/lib/channel/channel_stack.h +3 -10
  92. data/src/core/lib/channel/channel_stack_builder.c +41 -10
  93. data/src/core/lib/channel/channel_stack_builder.h +10 -0
  94. data/src/core/lib/channel/connected_channel.c +94 -23
  95. data/src/core/lib/channel/handshaker.c +8 -6
  96. data/src/core/lib/channel/handshaker_registry.c +1 -1
  97. data/src/core/lib/compression/algorithm_metadata.h +14 -0
  98. data/src/core/lib/compression/compression.c +101 -1
  99. data/src/core/lib/compression/stream_compression.c +32 -146
  100. data/src/core/lib/compression/stream_compression.h +28 -4
  101. data/src/core/lib/compression/stream_compression_gzip.c +228 -0
  102. data/src/core/lib/{iomgr/ev_epoll_thread_pool_linux.h → compression/stream_compression_gzip.h} +5 -7
  103. data/src/core/lib/compression/stream_compression_identity.c +94 -0
  104. data/src/core/lib/{iomgr/ev_epoll_limited_pollers_linux.h → compression/stream_compression_identity.h} +7 -8
  105. data/src/core/lib/debug/stats.c +174 -0
  106. data/src/core/lib/debug/stats.h +61 -0
  107. data/src/core/lib/debug/stats_data.c +687 -0
  108. data/src/core/lib/debug/stats_data.h +470 -0
  109. data/src/core/lib/debug/trace.c +3 -3
  110. data/src/core/lib/debug/trace.h +1 -1
  111. data/src/core/lib/http/format_request.c +1 -1
  112. data/src/core/lib/http/httpcli.c +8 -7
  113. data/src/core/lib/http/httpcli_security_connector.c +2 -1
  114. data/src/core/lib/http/parser.c +4 -3
  115. data/src/core/lib/iomgr/call_combiner.c +202 -0
  116. data/src/core/lib/iomgr/call_combiner.h +121 -0
  117. data/src/core/lib/iomgr/closure.c +18 -4
  118. data/src/core/lib/iomgr/combiner.c +11 -4
  119. data/src/core/lib/iomgr/error.c +26 -24
  120. data/src/core/lib/iomgr/ev_epoll1_linux.c +395 -212
  121. data/src/core/lib/iomgr/ev_epollex_linux.c +141 -128
  122. data/src/core/lib/iomgr/ev_epollsig_linux.c +44 -41
  123. data/src/core/lib/iomgr/ev_poll_posix.c +99 -75
  124. data/src/core/lib/iomgr/ev_posix.c +5 -9
  125. data/src/core/lib/iomgr/ev_posix.h +1 -1
  126. data/src/core/lib/iomgr/exec_ctx.h +6 -1
  127. data/src/core/lib/iomgr/executor.c +142 -36
  128. data/src/core/lib/iomgr/executor.h +6 -1
  129. data/src/core/lib/iomgr/fork_posix.c +88 -0
  130. data/src/core/lib/iomgr/fork_windows.c +39 -0
  131. data/src/core/lib/iomgr/iocp_windows.c +2 -0
  132. data/src/core/lib/iomgr/iomgr.c +2 -8
  133. data/src/core/lib/iomgr/is_epollexclusive_available.c +6 -6
  134. data/src/core/lib/iomgr/load_file.c +2 -1
  135. data/src/core/lib/iomgr/polling_entity.c +9 -9
  136. data/src/core/lib/iomgr/polling_entity.h +7 -1
  137. data/src/core/lib/iomgr/pollset.h +1 -1
  138. data/src/core/lib/iomgr/pollset_uv.c +1 -1
  139. data/src/core/lib/iomgr/pollset_windows.c +3 -3
  140. data/src/core/lib/iomgr/port.h +4 -0
  141. data/src/core/lib/iomgr/resolve_address_posix.c +8 -7
  142. data/src/core/lib/iomgr/resolve_address_windows.c +1 -1
  143. data/src/core/lib/iomgr/resource_quota.c +24 -19
  144. data/src/core/lib/iomgr/socket_factory_posix.c +4 -4
  145. data/src/core/lib/iomgr/socket_mutator.c +4 -4
  146. data/src/core/lib/iomgr/socket_utils_windows.c +0 -4
  147. data/src/core/lib/iomgr/tcp_client_posix.c +5 -4
  148. data/src/core/lib/iomgr/tcp_posix.c +181 -20
  149. data/src/core/lib/iomgr/tcp_server_posix.c +8 -7
  150. data/src/core/lib/iomgr/tcp_server_utils_posix_common.c +1 -1
  151. data/src/core/lib/iomgr/timer.h +4 -0
  152. data/src/core/lib/iomgr/timer_generic.c +138 -3
  153. data/src/core/lib/iomgr/timer_generic.h +3 -0
  154. data/src/core/lib/iomgr/timer_heap.c +4 -4
  155. data/src/core/lib/iomgr/timer_manager.c +2 -2
  156. data/src/core/lib/iomgr/timer_uv.c +2 -0
  157. data/src/core/lib/iomgr/udp_server.c +10 -8
  158. data/src/core/lib/iomgr/unix_sockets_posix.c +4 -2
  159. data/src/core/lib/iomgr/wakeup_fd_cv.c +9 -8
  160. data/src/core/lib/iomgr/wakeup_fd_cv.h +2 -2
  161. data/src/core/lib/json/json.c +1 -1
  162. data/src/core/lib/json/json_string.c +13 -13
  163. data/src/core/lib/profiling/timers.h +18 -8
  164. data/src/core/lib/security/credentials/composite/composite_credentials.c +4 -10
  165. data/src/core/lib/security/credentials/google_default/google_default_credentials.c +2 -1
  166. data/src/core/lib/security/credentials/jwt/jwt_verifier.c +11 -6
  167. data/src/core/lib/security/credentials/oauth2/oauth2_credentials.c +4 -4
  168. data/src/core/lib/security/credentials/plugin/plugin_credentials.c +132 -50
  169. data/src/core/lib/security/credentials/plugin/plugin_credentials.h +2 -0
  170. data/src/core/lib/security/transport/client_auth_filter.c +68 -135
  171. data/src/core/lib/security/transport/secure_endpoint.c +110 -90
  172. data/src/core/lib/security/transport/secure_endpoint.h +8 -3
  173. data/src/core/lib/security/transport/security_connector.c +10 -12
  174. data/src/core/lib/security/transport/security_handshaker.c +45 -24
  175. data/src/core/lib/security/transport/server_auth_filter.c +71 -20
  176. data/src/core/lib/slice/b64.c +2 -2
  177. data/src/core/lib/slice/slice.c +16 -14
  178. data/src/core/lib/slice/slice_buffer.c +5 -4
  179. data/src/core/lib/slice/slice_hash_table.c +3 -2
  180. data/src/core/lib/slice/slice_intern.c +8 -5
  181. data/src/core/lib/support/block_annotate.h +22 -0
  182. data/src/core/lib/support/fork.c +62 -0
  183. data/src/core/lib/support/fork.h +35 -0
  184. data/src/core/lib/support/log_linux.c +1 -1
  185. data/src/core/lib/support/string.c +15 -1
  186. data/src/core/lib/support/string.h +3 -0
  187. data/src/core/lib/support/thd_internal.h +6 -0
  188. data/src/core/lib/support/thd_posix.c +56 -0
  189. data/src/core/lib/support/thd_windows.c +2 -0
  190. data/src/core/lib/surface/alarm.c +22 -15
  191. data/src/core/lib/surface/byte_buffer.c +4 -2
  192. data/src/core/lib/surface/call.c +442 -141
  193. data/src/core/lib/surface/call.h +6 -6
  194. data/src/core/lib/surface/call_log_batch.c +1 -1
  195. data/src/core/lib/surface/call_test_only.h +12 -0
  196. data/src/core/lib/surface/channel.c +39 -4
  197. data/src/core/lib/surface/channel_init.c +6 -6
  198. data/src/core/lib/surface/channel_ping.c +2 -2
  199. data/src/core/lib/surface/completion_queue.c +56 -57
  200. data/src/core/lib/surface/init.c +17 -3
  201. data/src/core/lib/surface/init_secure.c +5 -1
  202. data/src/core/lib/surface/lame_client.cc +9 -10
  203. data/src/core/lib/surface/server.c +81 -72
  204. data/src/core/lib/surface/version.c +2 -2
  205. data/src/core/lib/transport/byte_stream.c +1 -0
  206. data/src/core/lib/transport/byte_stream.h +3 -1
  207. data/src/core/lib/transport/connectivity_state.c +2 -1
  208. data/src/core/lib/transport/metadata.c +7 -4
  209. data/src/core/lib/transport/metadata_batch.c +18 -16
  210. data/src/core/lib/transport/metadata_batch.h +1 -0
  211. data/src/core/lib/transport/service_config.c +5 -3
  212. data/src/core/lib/transport/static_metadata.c +395 -614
  213. data/src/core/lib/transport/static_metadata.h +165 -133
  214. data/src/core/lib/transport/status_conversion.c +1 -1
  215. data/src/core/lib/transport/transport.c +20 -20
  216. data/src/core/lib/transport/transport.h +8 -5
  217. data/src/core/lib/transport/transport_impl.h +0 -3
  218. data/src/core/lib/transport/transport_op_string.c +8 -1
  219. data/src/core/plugin_registry/grpc_plugin_registry.c +4 -4
  220. data/src/core/tsi/fake_transport_security.c +133 -2
  221. data/src/core/tsi/fake_transport_security.h +5 -0
  222. data/src/core/tsi/ssl_transport_security.c +105 -8
  223. data/src/core/tsi/ssl_transport_security.h +30 -7
  224. data/src/core/tsi/transport_security.h +8 -2
  225. data/src/core/tsi/transport_security_grpc.c +20 -13
  226. data/src/core/tsi/transport_security_grpc.h +13 -9
  227. data/src/ruby/ext/grpc/rb_call_credentials.c +6 -2
  228. data/src/ruby/ext/grpc/rb_grpc.c +1 -1
  229. data/src/ruby/ext/grpc/rb_grpc_imports.generated.c +30 -20
  230. data/src/ruby/ext/grpc/rb_grpc_imports.generated.h +50 -35
  231. data/src/ruby/lib/grpc.rb +1 -0
  232. data/src/ruby/lib/grpc/generic/active_call.rb +34 -9
  233. data/src/ruby/lib/grpc/generic/bidi_call.rb +19 -10
  234. data/src/ruby/lib/grpc/generic/client_stub.rb +95 -38
  235. data/src/ruby/lib/grpc/generic/interceptor_registry.rb +53 -0
  236. data/src/ruby/lib/grpc/generic/interceptors.rb +186 -0
  237. data/src/ruby/lib/grpc/generic/rpc_desc.rb +66 -20
  238. data/src/ruby/lib/grpc/generic/rpc_server.rb +15 -3
  239. data/src/ruby/lib/grpc/google_rpc_status_utils.rb +1 -2
  240. data/src/ruby/lib/grpc/version.rb +1 -1
  241. data/src/ruby/pb/grpc/testing/duplicate/echo_duplicate_services_pb.rb +1 -0
  242. data/src/ruby/spec/channel_connection_spec.rb +1 -34
  243. data/src/ruby/spec/client_server_spec.rb +188 -82
  244. data/src/ruby/spec/generic/active_call_spec.rb +65 -11
  245. data/src/ruby/spec/generic/client_interceptors_spec.rb +153 -0
  246. data/src/ruby/spec/generic/interceptor_registry_spec.rb +65 -0
  247. data/src/ruby/spec/generic/rpc_desc_spec.rb +38 -0
  248. data/src/ruby/spec/generic/rpc_server_spec.rb +1 -34
  249. data/src/ruby/spec/generic/server_interceptors_spec.rb +218 -0
  250. data/src/ruby/spec/spec_helper.rb +4 -0
  251. data/src/ruby/spec/support/helpers.rb +73 -0
  252. data/src/ruby/spec/support/services.rb +147 -0
  253. data/third_party/cares/ares_build.h +21 -62
  254. data/third_party/cares/cares/ares.h +23 -1
  255. data/third_party/cares/cares/ares__close_sockets.c +2 -2
  256. data/third_party/cares/cares/ares_create_query.c +3 -3
  257. data/third_party/cares/cares/ares_expand_name.c +6 -2
  258. data/third_party/cares/cares/ares_expand_string.c +1 -1
  259. data/third_party/cares/cares/ares_getnameinfo.c +27 -7
  260. data/third_party/cares/cares/ares_init.c +407 -39
  261. data/third_party/cares/cares/ares_library_init.c +10 -0
  262. data/third_party/cares/cares/ares_library_init.h +2 -1
  263. data/third_party/cares/cares/ares_nowarn.c +6 -6
  264. data/third_party/cares/cares/ares_nowarn.h +2 -2
  265. data/third_party/cares/cares/ares_parse_naptr_reply.c +6 -1
  266. data/third_party/cares/cares/ares_private.h +11 -0
  267. data/third_party/cares/cares/ares_process.c +126 -37
  268. data/third_party/cares/cares/ares_version.h +2 -2
  269. data/third_party/cares/cares/ares_writev.c +2 -2
  270. data/third_party/cares/cares/config-win32.h +8 -34
  271. data/third_party/cares/cares/inet_net_pton.c +2 -2
  272. data/third_party/cares/cares/setup_once.h +5 -5
  273. data/third_party/cares/config_darwin/ares_config.h +98 -196
  274. data/third_party/cares/config_linux/ares_config.h +103 -203
  275. metadata +47 -20
  276. data/src/core/lib/iomgr/ev_epoll_limited_pollers_linux.c +0 -1957
  277. data/src/core/lib/iomgr/ev_epoll_thread_pool_linux.c +0 -1182
@@ -46,8 +46,21 @@ int grpc_compression_algorithm_parse(grpc_slice name,
46
46
  }
47
47
  }
48
48
 
49
+ int grpc_stream_compression_algorithm_parse(
50
+ grpc_slice name, grpc_stream_compression_algorithm *algorithm) {
51
+ if (grpc_slice_eq(name, GRPC_MDSTR_IDENTITY)) {
52
+ *algorithm = GRPC_STREAM_COMPRESS_NONE;
53
+ return 1;
54
+ } else if (grpc_slice_eq(name, GRPC_MDSTR_GZIP)) {
55
+ *algorithm = GRPC_STREAM_COMPRESS_GZIP;
56
+ return 1;
57
+ } else {
58
+ return 0;
59
+ }
60
+ }
61
+
49
62
  int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
50
- char **name) {
63
+ const char **name) {
51
64
  GRPC_API_TRACE("grpc_compression_algorithm_parse(algorithm=%d, name=%p)", 2,
52
65
  ((int)algorithm, name));
53
66
  switch (algorithm) {
@@ -66,6 +79,24 @@ int grpc_compression_algorithm_name(grpc_compression_algorithm algorithm,
66
79
  return 0;
67
80
  }
68
81
 
82
+ int grpc_stream_compression_algorithm_name(
83
+ grpc_stream_compression_algorithm algorithm, const char **name) {
84
+ GRPC_API_TRACE(
85
+ "grpc_stream_compression_algorithm_parse(algorithm=%d, name=%p)", 2,
86
+ ((int)algorithm, name));
87
+ switch (algorithm) {
88
+ case GRPC_STREAM_COMPRESS_NONE:
89
+ *name = "identity";
90
+ return 1;
91
+ case GRPC_STREAM_COMPRESS_GZIP:
92
+ *name = "gzip";
93
+ return 1;
94
+ case GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT:
95
+ return 0;
96
+ }
97
+ return 0;
98
+ }
99
+
69
100
  grpc_compression_algorithm grpc_compression_algorithm_from_slice(
70
101
  grpc_slice str) {
71
102
  if (grpc_slice_eq(str, GRPC_MDSTR_IDENTITY)) return GRPC_COMPRESS_NONE;
@@ -74,6 +105,13 @@ grpc_compression_algorithm grpc_compression_algorithm_from_slice(
74
105
  return GRPC_COMPRESS_ALGORITHMS_COUNT;
75
106
  }
76
107
 
108
+ grpc_stream_compression_algorithm grpc_stream_compression_algorithm_from_slice(
109
+ grpc_slice str) {
110
+ if (grpc_slice_eq(str, GRPC_MDSTR_IDENTITY)) return GRPC_STREAM_COMPRESS_NONE;
111
+ if (grpc_slice_eq(str, GRPC_MDSTR_GZIP)) return GRPC_STREAM_COMPRESS_GZIP;
112
+ return GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT;
113
+ }
114
+
77
115
  grpc_slice grpc_compression_algorithm_slice(
78
116
  grpc_compression_algorithm algorithm) {
79
117
  switch (algorithm) {
@@ -89,6 +127,19 @@ grpc_slice grpc_compression_algorithm_slice(
89
127
  return grpc_empty_slice();
90
128
  }
91
129
 
130
+ grpc_slice grpc_stream_compression_algorithm_slice(
131
+ grpc_stream_compression_algorithm algorithm) {
132
+ switch (algorithm) {
133
+ case GRPC_STREAM_COMPRESS_NONE:
134
+ return GRPC_MDSTR_IDENTITY;
135
+ case GRPC_STREAM_COMPRESS_GZIP:
136
+ return GRPC_MDSTR_GZIP;
137
+ case GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT:
138
+ return grpc_empty_slice();
139
+ }
140
+ return grpc_empty_slice();
141
+ }
142
+
92
143
  grpc_mdelem grpc_compression_encoding_mdelem(
93
144
  grpc_compression_algorithm algorithm) {
94
145
  switch (algorithm) {
@@ -104,10 +155,25 @@ grpc_mdelem grpc_compression_encoding_mdelem(
104
155
  return GRPC_MDNULL;
105
156
  }
106
157
 
158
+ grpc_mdelem grpc_stream_compression_encoding_mdelem(
159
+ grpc_stream_compression_algorithm algorithm) {
160
+ switch (algorithm) {
161
+ case GRPC_STREAM_COMPRESS_NONE:
162
+ return GRPC_MDELEM_CONTENT_ENCODING_IDENTITY;
163
+ case GRPC_STREAM_COMPRESS_GZIP:
164
+ return GRPC_MDELEM_CONTENT_ENCODING_GZIP;
165
+ default:
166
+ break;
167
+ }
168
+ return GRPC_MDNULL;
169
+ }
170
+
107
171
  void grpc_compression_options_init(grpc_compression_options *opts) {
108
172
  memset(opts, 0, sizeof(*opts));
109
173
  /* all enabled by default */
110
174
  opts->enabled_algorithms_bitset = (1u << GRPC_COMPRESS_ALGORITHMS_COUNT) - 1;
175
+ opts->enabled_stream_compression_algorithms_bitset =
176
+ (1u << GRPC_STREAM_COMPRESS_ALGORITHMS_COUNT) - 1;
111
177
  }
112
178
 
113
179
  void grpc_compression_options_enable_algorithm(
@@ -126,6 +192,13 @@ int grpc_compression_options_is_algorithm_enabled(
126
192
  return GPR_BITGET(opts->enabled_algorithms_bitset, algorithm);
127
193
  }
128
194
 
195
+ int grpc_compression_options_is_stream_compression_algorithm_enabled(
196
+ const grpc_compression_options *opts,
197
+ grpc_stream_compression_algorithm algorithm) {
198
+ return GPR_BITGET(opts->enabled_stream_compression_algorithms_bitset,
199
+ algorithm);
200
+ }
201
+
129
202
  /* TODO(dgq): Add the ability to specify parameters to the individual
130
203
  * compression algorithms */
131
204
  grpc_compression_algorithm grpc_compression_algorithm_for_level(
@@ -181,3 +254,30 @@ grpc_compression_algorithm grpc_compression_algorithm_for_level(
181
254
  abort();
182
255
  };
183
256
  }
257
+
258
+ GRPCAPI grpc_stream_compression_algorithm
259
+ grpc_stream_compression_algorithm_for_level(
260
+ grpc_stream_compression_level level, uint32_t accepted_stream_encodings) {
261
+ GRPC_API_TRACE("grpc_stream_compression_algorithm_for_level(level=%d)", 1,
262
+ ((int)level));
263
+ if (level > GRPC_STREAM_COMPRESS_LEVEL_HIGH) {
264
+ gpr_log(GPR_ERROR, "Unknown compression level %d.", (int)level);
265
+ abort();
266
+ }
267
+
268
+ switch (level) {
269
+ case GRPC_STREAM_COMPRESS_LEVEL_NONE:
270
+ return GRPC_STREAM_COMPRESS_NONE;
271
+ case GRPC_STREAM_COMPRESS_LEVEL_LOW:
272
+ case GRPC_STREAM_COMPRESS_LEVEL_MED:
273
+ case GRPC_STREAM_COMPRESS_LEVEL_HIGH:
274
+ if (GPR_BITGET(accepted_stream_encodings, GRPC_STREAM_COMPRESS_GZIP) ==
275
+ 1) {
276
+ return GRPC_STREAM_COMPRESS_GZIP;
277
+ } else {
278
+ return GRPC_STREAM_COMPRESS_NONE;
279
+ }
280
+ default:
281
+ abort();
282
+ }
283
+ }
@@ -16,176 +16,62 @@
16
16
  *
17
17
  */
18
18
 
19
- #include <grpc/support/alloc.h>
20
19
  #include <grpc/support/log.h>
21
20
 
22
21
  #include "src/core/lib/compression/stream_compression.h"
23
- #include "src/core/lib/iomgr/exec_ctx.h"
24
- #include "src/core/lib/slice/slice_internal.h"
22
+ #include "src/core/lib/compression/stream_compression_gzip.h"
25
23
 
26
- #define OUTPUT_BLOCK_SIZE (1024)
27
-
28
- static bool gzip_flate(grpc_stream_compression_context *ctx,
29
- grpc_slice_buffer *in, grpc_slice_buffer *out,
30
- size_t *output_size, size_t max_output_size, int flush,
31
- bool *end_of_context) {
32
- GPR_ASSERT(flush == 0 || flush == Z_SYNC_FLUSH || flush == Z_FINISH);
33
- /* Full flush is not allowed when inflating. */
34
- GPR_ASSERT(!(ctx->flate == inflate && (flush == Z_FINISH)));
35
-
36
- grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
37
- int r;
38
- bool eoc = false;
39
- size_t original_max_output_size = max_output_size;
40
- while (max_output_size > 0 && (in->length > 0 || flush) && !eoc) {
41
- size_t slice_size = max_output_size < OUTPUT_BLOCK_SIZE ? max_output_size
42
- : OUTPUT_BLOCK_SIZE;
43
- grpc_slice slice_out = GRPC_SLICE_MALLOC(slice_size);
44
- ctx->zs.avail_out = (uInt)slice_size;
45
- ctx->zs.next_out = GRPC_SLICE_START_PTR(slice_out);
46
- while (ctx->zs.avail_out > 0 && in->length > 0 && !eoc) {
47
- grpc_slice slice = grpc_slice_buffer_take_first(in);
48
- ctx->zs.avail_in = (uInt)GRPC_SLICE_LENGTH(slice);
49
- ctx->zs.next_in = GRPC_SLICE_START_PTR(slice);
50
- r = ctx->flate(&ctx->zs, Z_NO_FLUSH);
51
- if (r < 0 && r != Z_BUF_ERROR) {
52
- gpr_log(GPR_ERROR, "zlib error (%d)", r);
53
- grpc_slice_unref_internal(&exec_ctx, slice_out);
54
- grpc_exec_ctx_finish(&exec_ctx);
55
- return false;
56
- } else if (r == Z_STREAM_END && ctx->flate == inflate) {
57
- eoc = true;
58
- }
59
- if (ctx->zs.avail_in > 0) {
60
- grpc_slice_buffer_undo_take_first(
61
- in,
62
- grpc_slice_sub(slice, GRPC_SLICE_LENGTH(slice) - ctx->zs.avail_in,
63
- GRPC_SLICE_LENGTH(slice)));
64
- }
65
- grpc_slice_unref_internal(&exec_ctx, slice);
66
- }
67
- if (flush != 0 && ctx->zs.avail_out > 0 && !eoc) {
68
- GPR_ASSERT(in->length == 0);
69
- r = ctx->flate(&ctx->zs, flush);
70
- if (flush == Z_SYNC_FLUSH) {
71
- switch (r) {
72
- case Z_OK:
73
- /* Maybe flush is not complete; just made some partial progress. */
74
- if (ctx->zs.avail_out > 0) {
75
- flush = 0;
76
- }
77
- break;
78
- case Z_BUF_ERROR:
79
- case Z_STREAM_END:
80
- flush = 0;
81
- break;
82
- default:
83
- gpr_log(GPR_ERROR, "zlib error (%d)", r);
84
- grpc_slice_unref_internal(&exec_ctx, slice_out);
85
- grpc_exec_ctx_finish(&exec_ctx);
86
- return false;
87
- }
88
- } else if (flush == Z_FINISH) {
89
- switch (r) {
90
- case Z_OK:
91
- case Z_BUF_ERROR:
92
- /* Wait for the next loop to assign additional output space. */
93
- GPR_ASSERT(ctx->zs.avail_out == 0);
94
- break;
95
- case Z_STREAM_END:
96
- flush = 0;
97
- break;
98
- default:
99
- gpr_log(GPR_ERROR, "zlib error (%d)", r);
100
- grpc_slice_unref_internal(&exec_ctx, slice_out);
101
- grpc_exec_ctx_finish(&exec_ctx);
102
- return false;
103
- }
104
- }
105
- }
106
-
107
- if (ctx->zs.avail_out == 0) {
108
- grpc_slice_buffer_add(out, slice_out);
109
- } else if (ctx->zs.avail_out < slice_size) {
110
- slice_out.data.refcounted.length -= ctx->zs.avail_out;
111
- grpc_slice_buffer_add(out, slice_out);
112
- } else {
113
- grpc_slice_unref_internal(&exec_ctx, slice_out);
114
- }
115
- max_output_size -= (slice_size - ctx->zs.avail_out);
116
- }
117
- grpc_exec_ctx_finish(&exec_ctx);
118
- if (end_of_context) {
119
- *end_of_context = eoc;
120
- }
121
- if (output_size) {
122
- *output_size = original_max_output_size - max_output_size;
123
- }
124
- return true;
125
- }
24
+ extern const grpc_stream_compression_vtable
25
+ grpc_stream_compression_identity_vtable;
126
26
 
127
27
  bool grpc_stream_compress(grpc_stream_compression_context *ctx,
128
28
  grpc_slice_buffer *in, grpc_slice_buffer *out,
129
29
  size_t *output_size, size_t max_output_size,
130
30
  grpc_stream_compression_flush flush) {
131
- GPR_ASSERT(ctx->flate == deflate);
132
- int gzip_flush;
133
- switch (flush) {
134
- case GRPC_STREAM_COMPRESSION_FLUSH_NONE:
135
- gzip_flush = 0;
136
- break;
137
- case GRPC_STREAM_COMPRESSION_FLUSH_SYNC:
138
- gzip_flush = Z_SYNC_FLUSH;
139
- break;
140
- case GRPC_STREAM_COMPRESSION_FLUSH_FINISH:
141
- gzip_flush = Z_FINISH;
142
- break;
143
- default:
144
- gzip_flush = 0;
145
- }
146
- return gzip_flate(ctx, in, out, output_size, max_output_size, gzip_flush,
147
- NULL);
31
+ return ctx->vtable->compress(ctx, in, out, output_size, max_output_size,
32
+ flush);
148
33
  }
149
34
 
150
35
  bool grpc_stream_decompress(grpc_stream_compression_context *ctx,
151
36
  grpc_slice_buffer *in, grpc_slice_buffer *out,
152
37
  size_t *output_size, size_t max_output_size,
153
38
  bool *end_of_context) {
154
- GPR_ASSERT(ctx->flate == inflate);
155
- return gzip_flate(ctx, in, out, output_size, max_output_size, Z_SYNC_FLUSH,
156
- end_of_context);
39
+ return ctx->vtable->decompress(ctx, in, out, output_size, max_output_size,
40
+ end_of_context);
157
41
  }
158
42
 
159
43
  grpc_stream_compression_context *grpc_stream_compression_context_create(
160
44
  grpc_stream_compression_method method) {
161
- grpc_stream_compression_context *ctx =
162
- gpr_zalloc(sizeof(grpc_stream_compression_context));
163
- int r;
164
- if (ctx == NULL) {
165
- return NULL;
166
- }
167
- if (method == GRPC_STREAM_COMPRESSION_DECOMPRESS) {
168
- r = inflateInit2(&ctx->zs, 0x1F);
169
- ctx->flate = inflate;
170
- } else {
171
- r = deflateInit2(&ctx->zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 0x1F, 8,
172
- Z_DEFAULT_STRATEGY);
173
- ctx->flate = deflate;
174
- }
175
- if (r != Z_OK) {
176
- gpr_free(ctx);
177
- return NULL;
45
+ switch (method) {
46
+ case GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS:
47
+ case GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS:
48
+ return grpc_stream_compression_identity_vtable.context_create(method);
49
+ case GRPC_STREAM_COMPRESSION_GZIP_COMPRESS:
50
+ case GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS:
51
+ return grpc_stream_compression_gzip_vtable.context_create(method);
52
+ default:
53
+ gpr_log(GPR_ERROR, "Unknown stream compression method: %d", method);
54
+ return NULL;
178
55
  }
179
-
180
- return ctx;
181
56
  }
182
57
 
183
58
  void grpc_stream_compression_context_destroy(
184
59
  grpc_stream_compression_context *ctx) {
185
- if (ctx->flate == inflate) {
186
- inflateEnd(&ctx->zs);
60
+ ctx->vtable->context_destroy(ctx);
61
+ }
62
+
63
+ int grpc_stream_compression_method_parse(
64
+ grpc_slice value, bool is_compress,
65
+ grpc_stream_compression_method *method) {
66
+ if (grpc_slice_eq(value, GRPC_MDSTR_IDENTITY)) {
67
+ *method = is_compress ? GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS
68
+ : GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS;
69
+ return 1;
70
+ } else if (grpc_slice_eq(value, GRPC_MDSTR_GZIP)) {
71
+ *method = is_compress ? GRPC_STREAM_COMPRESSION_GZIP_COMPRESS
72
+ : GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS;
73
+ return 1;
187
74
  } else {
188
- deflateEnd(&ctx->zs);
75
+ return 0;
189
76
  }
190
- gpr_free(ctx);
191
77
  }
@@ -24,15 +24,20 @@
24
24
  #include <grpc/slice_buffer.h>
25
25
  #include <zlib.h>
26
26
 
27
+ #include "src/core/lib/transport/static_metadata.h"
28
+
29
+ typedef struct grpc_stream_compression_vtable grpc_stream_compression_vtable;
30
+
27
31
  /* Stream compression/decompression context */
28
32
  typedef struct grpc_stream_compression_context {
29
- z_stream zs;
30
- int (*flate)(z_stream *zs, int flush);
33
+ const grpc_stream_compression_vtable *vtable;
31
34
  } grpc_stream_compression_context;
32
35
 
33
36
  typedef enum grpc_stream_compression_method {
34
- GRPC_STREAM_COMPRESSION_COMPRESS = 0,
35
- GRPC_STREAM_COMPRESSION_DECOMPRESS,
37
+ GRPC_STREAM_COMPRESSION_IDENTITY_COMPRESS = 0,
38
+ GRPC_STREAM_COMPRESSION_IDENTITY_DECOMPRESS,
39
+ GRPC_STREAM_COMPRESSION_GZIP_COMPRESS,
40
+ GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS,
36
41
  GRPC_STREAM_COMPRESSION_METHOD_COUNT
37
42
  } grpc_stream_compression_method;
38
43
 
@@ -43,6 +48,19 @@ typedef enum grpc_stream_compression_flush {
43
48
  GRPC_STREAM_COMPRESSION_FLUSH_COUNT
44
49
  } grpc_stream_compression_flush;
45
50
 
51
+ struct grpc_stream_compression_vtable {
52
+ bool (*compress)(grpc_stream_compression_context *ctx, grpc_slice_buffer *in,
53
+ grpc_slice_buffer *out, size_t *output_size,
54
+ size_t max_output_size, grpc_stream_compression_flush flush);
55
+ bool (*decompress)(grpc_stream_compression_context *ctx,
56
+ grpc_slice_buffer *in, grpc_slice_buffer *out,
57
+ size_t *output_size, size_t max_output_size,
58
+ bool *end_of_context);
59
+ grpc_stream_compression_context *(*context_create)(
60
+ grpc_stream_compression_method method);
61
+ void (*context_destroy)(grpc_stream_compression_context *ctx);
62
+ };
63
+
46
64
  /**
47
65
  * Compress bytes provided in \a in with a given context, with an optional flush
48
66
  * at the end of compression. Emits at most \a max_output_size compressed bytes
@@ -87,4 +105,10 @@ grpc_stream_compression_context *grpc_stream_compression_context_create(
87
105
  void grpc_stream_compression_context_destroy(
88
106
  grpc_stream_compression_context *ctx);
89
107
 
108
+ /**
109
+ * Parse stream compression method based on algorithm name
110
+ */
111
+ int grpc_stream_compression_method_parse(
112
+ grpc_slice value, bool is_compress, grpc_stream_compression_method *method);
113
+
90
114
  #endif
@@ -0,0 +1,228 @@
1
+ /*
2
+ *
3
+ * Copyright 2017 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/alloc.h>
20
+ #include <grpc/support/log.h>
21
+
22
+ #include "src/core/lib/compression/stream_compression_gzip.h"
23
+ #include "src/core/lib/iomgr/exec_ctx.h"
24
+ #include "src/core/lib/slice/slice_internal.h"
25
+
26
+ #define OUTPUT_BLOCK_SIZE (1024)
27
+
28
+ typedef struct grpc_stream_compression_context_gzip {
29
+ grpc_stream_compression_context base;
30
+
31
+ z_stream zs;
32
+ int (*flate)(z_stream *zs, int flush);
33
+ } grpc_stream_compression_context_gzip;
34
+
35
+ static bool gzip_flate(grpc_stream_compression_context_gzip *ctx,
36
+ grpc_slice_buffer *in, grpc_slice_buffer *out,
37
+ size_t *output_size, size_t max_output_size, int flush,
38
+ bool *end_of_context) {
39
+ GPR_ASSERT(flush == 0 || flush == Z_SYNC_FLUSH || flush == Z_FINISH);
40
+ /* Full flush is not allowed when inflating. */
41
+ GPR_ASSERT(!(ctx->flate == inflate && (flush == Z_FINISH)));
42
+
43
+ grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
44
+ int r;
45
+ bool eoc = false;
46
+ size_t original_max_output_size = max_output_size;
47
+ while (max_output_size > 0 && (in->length > 0 || flush) && !eoc) {
48
+ size_t slice_size = max_output_size < OUTPUT_BLOCK_SIZE ? max_output_size
49
+ : OUTPUT_BLOCK_SIZE;
50
+ grpc_slice slice_out = GRPC_SLICE_MALLOC(slice_size);
51
+ ctx->zs.avail_out = (uInt)slice_size;
52
+ ctx->zs.next_out = GRPC_SLICE_START_PTR(slice_out);
53
+ while (ctx->zs.avail_out > 0 && in->length > 0 && !eoc) {
54
+ grpc_slice slice = grpc_slice_buffer_take_first(in);
55
+ ctx->zs.avail_in = (uInt)GRPC_SLICE_LENGTH(slice);
56
+ ctx->zs.next_in = GRPC_SLICE_START_PTR(slice);
57
+ r = ctx->flate(&ctx->zs, Z_NO_FLUSH);
58
+ if (r < 0 && r != Z_BUF_ERROR) {
59
+ gpr_log(GPR_ERROR, "zlib error (%d)", r);
60
+ grpc_slice_unref_internal(&exec_ctx, slice_out);
61
+ grpc_exec_ctx_finish(&exec_ctx);
62
+ return false;
63
+ } else if (r == Z_STREAM_END && ctx->flate == inflate) {
64
+ eoc = true;
65
+ }
66
+ if (ctx->zs.avail_in > 0) {
67
+ grpc_slice_buffer_undo_take_first(
68
+ in,
69
+ grpc_slice_sub(slice, GRPC_SLICE_LENGTH(slice) - ctx->zs.avail_in,
70
+ GRPC_SLICE_LENGTH(slice)));
71
+ }
72
+ grpc_slice_unref_internal(&exec_ctx, slice);
73
+ }
74
+ if (flush != 0 && ctx->zs.avail_out > 0 && !eoc) {
75
+ GPR_ASSERT(in->length == 0);
76
+ r = ctx->flate(&ctx->zs, flush);
77
+ if (flush == Z_SYNC_FLUSH) {
78
+ switch (r) {
79
+ case Z_OK:
80
+ /* Maybe flush is not complete; just made some partial progress. */
81
+ if (ctx->zs.avail_out > 0) {
82
+ flush = 0;
83
+ }
84
+ break;
85
+ case Z_BUF_ERROR:
86
+ case Z_STREAM_END:
87
+ flush = 0;
88
+ break;
89
+ default:
90
+ gpr_log(GPR_ERROR, "zlib error (%d)", r);
91
+ grpc_slice_unref_internal(&exec_ctx, slice_out);
92
+ grpc_exec_ctx_finish(&exec_ctx);
93
+ return false;
94
+ }
95
+ } else if (flush == Z_FINISH) {
96
+ switch (r) {
97
+ case Z_OK:
98
+ case Z_BUF_ERROR:
99
+ /* Wait for the next loop to assign additional output space. */
100
+ GPR_ASSERT(ctx->zs.avail_out == 0);
101
+ break;
102
+ case Z_STREAM_END:
103
+ flush = 0;
104
+ break;
105
+ default:
106
+ gpr_log(GPR_ERROR, "zlib error (%d)", r);
107
+ grpc_slice_unref_internal(&exec_ctx, slice_out);
108
+ grpc_exec_ctx_finish(&exec_ctx);
109
+ return false;
110
+ }
111
+ }
112
+ }
113
+
114
+ if (ctx->zs.avail_out == 0) {
115
+ grpc_slice_buffer_add(out, slice_out);
116
+ } else if (ctx->zs.avail_out < slice_size) {
117
+ slice_out.data.refcounted.length -= ctx->zs.avail_out;
118
+ grpc_slice_buffer_add(out, slice_out);
119
+ } else {
120
+ grpc_slice_unref_internal(&exec_ctx, slice_out);
121
+ }
122
+ max_output_size -= (slice_size - ctx->zs.avail_out);
123
+ }
124
+ grpc_exec_ctx_finish(&exec_ctx);
125
+ if (end_of_context) {
126
+ *end_of_context = eoc;
127
+ }
128
+ if (output_size) {
129
+ *output_size = original_max_output_size - max_output_size;
130
+ }
131
+ return true;
132
+ }
133
+
134
+ static bool grpc_stream_compress_gzip(grpc_stream_compression_context *ctx,
135
+ grpc_slice_buffer *in,
136
+ grpc_slice_buffer *out,
137
+ size_t *output_size,
138
+ size_t max_output_size,
139
+ grpc_stream_compression_flush flush) {
140
+ if (ctx == NULL) {
141
+ return false;
142
+ }
143
+ grpc_stream_compression_context_gzip *gzip_ctx =
144
+ (grpc_stream_compression_context_gzip *)ctx;
145
+ GPR_ASSERT(gzip_ctx->flate == deflate);
146
+ int gzip_flush;
147
+ switch (flush) {
148
+ case GRPC_STREAM_COMPRESSION_FLUSH_NONE:
149
+ gzip_flush = 0;
150
+ break;
151
+ case GRPC_STREAM_COMPRESSION_FLUSH_SYNC:
152
+ gzip_flush = Z_SYNC_FLUSH;
153
+ break;
154
+ case GRPC_STREAM_COMPRESSION_FLUSH_FINISH:
155
+ gzip_flush = Z_FINISH;
156
+ break;
157
+ default:
158
+ gzip_flush = 0;
159
+ }
160
+ return gzip_flate(gzip_ctx, in, out, output_size, max_output_size, gzip_flush,
161
+ NULL);
162
+ }
163
+
164
+ static bool grpc_stream_decompress_gzip(grpc_stream_compression_context *ctx,
165
+ grpc_slice_buffer *in,
166
+ grpc_slice_buffer *out,
167
+ size_t *output_size,
168
+ size_t max_output_size,
169
+ bool *end_of_context) {
170
+ if (ctx == NULL) {
171
+ return false;
172
+ }
173
+ grpc_stream_compression_context_gzip *gzip_ctx =
174
+ (grpc_stream_compression_context_gzip *)ctx;
175
+ GPR_ASSERT(gzip_ctx->flate == inflate);
176
+ return gzip_flate(gzip_ctx, in, out, output_size, max_output_size,
177
+ Z_SYNC_FLUSH, end_of_context);
178
+ }
179
+
180
+ static grpc_stream_compression_context *
181
+ grpc_stream_compression_context_create_gzip(
182
+ grpc_stream_compression_method method) {
183
+ GPR_ASSERT(method == GRPC_STREAM_COMPRESSION_GZIP_COMPRESS ||
184
+ method == GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS);
185
+ grpc_stream_compression_context_gzip *gzip_ctx =
186
+ (grpc_stream_compression_context_gzip *)gpr_zalloc(
187
+ sizeof(grpc_stream_compression_context_gzip));
188
+ int r;
189
+ if (gzip_ctx == NULL) {
190
+ return NULL;
191
+ }
192
+ if (method == GRPC_STREAM_COMPRESSION_GZIP_DECOMPRESS) {
193
+ r = inflateInit2(&gzip_ctx->zs, 0x1F);
194
+ gzip_ctx->flate = inflate;
195
+ } else {
196
+ r = deflateInit2(&gzip_ctx->zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 0x1F, 8,
197
+ Z_DEFAULT_STRATEGY);
198
+ gzip_ctx->flate = deflate;
199
+ }
200
+ if (r != Z_OK) {
201
+ gpr_free(gzip_ctx);
202
+ return NULL;
203
+ }
204
+
205
+ gzip_ctx->base.vtable = &grpc_stream_compression_gzip_vtable;
206
+ return (grpc_stream_compression_context *)gzip_ctx;
207
+ }
208
+
209
+ static void grpc_stream_compression_context_destroy_gzip(
210
+ grpc_stream_compression_context *ctx) {
211
+ if (ctx == NULL) {
212
+ return;
213
+ }
214
+ grpc_stream_compression_context_gzip *gzip_ctx =
215
+ (grpc_stream_compression_context_gzip *)ctx;
216
+ if (gzip_ctx->flate == inflate) {
217
+ inflateEnd(&gzip_ctx->zs);
218
+ } else {
219
+ deflateEnd(&gzip_ctx->zs);
220
+ }
221
+ gpr_free(ctx);
222
+ }
223
+
224
+ const grpc_stream_compression_vtable grpc_stream_compression_gzip_vtable = {
225
+ .compress = grpc_stream_compress_gzip,
226
+ .decompress = grpc_stream_decompress_gzip,
227
+ .context_create = grpc_stream_compression_context_create_gzip,
228
+ .context_destroy = grpc_stream_compression_context_destroy_gzip};