|
1 | 1 | use crate::configuration::ExtensionType;
|
2 |
| -use crate::envoy::{CheckRequest, RateLimitDescriptor, RateLimitRequest}; |
| 2 | +use crate::envoy::{ |
| 3 | + CheckRequest, DeniedHttpResponse, OkHttpResponse, RateLimitDescriptor, RateLimitRequest, |
| 4 | + RateLimitResponse, |
| 5 | +}; |
3 | 6 | use crate::service::auth::AuthService;
|
4 | 7 | use crate::service::rate_limit::RateLimitService;
|
5 | 8 | use protobuf::reflect::MessageDescriptor;
|
6 | 9 | use protobuf::{
|
7 |
| - Clear, CodedInputStream, CodedOutputStream, Message, ProtobufResult, UnknownFields, |
| 10 | + Clear, CodedInputStream, CodedOutputStream, Message, ProtobufError, ProtobufResult, |
| 11 | + UnknownFields, |
8 | 12 | };
|
| 13 | +use proxy_wasm::types::Bytes; |
9 | 14 | use std::any::Any;
|
10 | 15 |
|
11 | 16 | #[derive(Clone, Debug)]
|
@@ -126,11 +131,145 @@ impl GrpcMessageRequest {
|
126 | 131 | descriptors: protobuf::RepeatedField<RateLimitDescriptor>,
|
127 | 132 | ) -> Self {
|
128 | 133 | match extension_type {
|
129 |
| - ExtensionType::RateLimit => GrpcMessageRequest::RateLimit(RateLimitService::message( |
130 |
| - domain.clone(), |
131 |
| - descriptors, |
132 |
| - )), |
133 |
| - ExtensionType::Auth => GrpcMessageRequest::Auth(AuthService::message(domain.clone())), |
| 134 | + ExtensionType::RateLimit => GrpcMessageRequest::RateLimit( |
| 135 | + RateLimitService::request_message(domain.clone(), descriptors), |
| 136 | + ), |
| 137 | + ExtensionType::Auth => { |
| 138 | + GrpcMessageRequest::Auth(AuthService::request_message(domain.clone())) |
| 139 | + } |
134 | 140 | }
|
135 | 141 | }
|
136 | 142 | }
|
| 143 | + |
| 144 | +#[derive(Clone, Debug)] |
| 145 | +pub enum GrpcMessageResponse { |
| 146 | + AuthOk(OkHttpResponse), |
| 147 | + AuthDenied(DeniedHttpResponse), |
| 148 | + RateLimit(RateLimitResponse), |
| 149 | +} |
| 150 | + |
| 151 | +impl Default for GrpcMessageResponse { |
| 152 | + fn default() -> Self { |
| 153 | + GrpcMessageResponse::RateLimit(RateLimitResponse::new()) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +impl Clear for GrpcMessageResponse { |
| 158 | + fn clear(&mut self) { |
| 159 | + todo!() |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +impl Message for GrpcMessageResponse { |
| 164 | + fn descriptor(&self) -> &'static MessageDescriptor { |
| 165 | + match self { |
| 166 | + GrpcMessageResponse::AuthOk(res) => res.descriptor(), |
| 167 | + GrpcMessageResponse::AuthDenied(res) => res.descriptor(), |
| 168 | + GrpcMessageResponse::RateLimit(res) => res.descriptor(), |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + fn is_initialized(&self) -> bool { |
| 173 | + match self { |
| 174 | + GrpcMessageResponse::AuthOk(res) => res.is_initialized(), |
| 175 | + GrpcMessageResponse::AuthDenied(res) => res.is_initialized(), |
| 176 | + GrpcMessageResponse::RateLimit(res) => res.is_initialized(), |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + fn merge_from(&mut self, is: &mut CodedInputStream) -> ProtobufResult<()> { |
| 181 | + match self { |
| 182 | + GrpcMessageResponse::AuthOk(res) => res.merge_from(is), |
| 183 | + GrpcMessageResponse::AuthDenied(res) => res.merge_from(is), |
| 184 | + GrpcMessageResponse::RateLimit(res) => res.merge_from(is), |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + fn write_to_with_cached_sizes(&self, os: &mut CodedOutputStream) -> ProtobufResult<()> { |
| 189 | + match self { |
| 190 | + GrpcMessageResponse::AuthOk(res) => res.write_to_with_cached_sizes(os), |
| 191 | + GrpcMessageResponse::AuthDenied(res) => res.write_to_with_cached_sizes(os), |
| 192 | + GrpcMessageResponse::RateLimit(res) => res.write_to_with_cached_sizes(os), |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + fn write_to_bytes(&self) -> ProtobufResult<Vec<u8>> { |
| 197 | + match self { |
| 198 | + GrpcMessageResponse::AuthOk(res) => res.write_to_bytes(), |
| 199 | + GrpcMessageResponse::AuthDenied(res) => res.write_to_bytes(), |
| 200 | + GrpcMessageResponse::RateLimit(res) => res.write_to_bytes(), |
| 201 | + } |
| 202 | + } |
| 203 | + |
| 204 | + fn compute_size(&self) -> u32 { |
| 205 | + match self { |
| 206 | + GrpcMessageResponse::AuthOk(res) => res.compute_size(), |
| 207 | + GrpcMessageResponse::AuthDenied(res) => res.compute_size(), |
| 208 | + GrpcMessageResponse::RateLimit(res) => res.compute_size(), |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + fn get_cached_size(&self) -> u32 { |
| 213 | + match self { |
| 214 | + GrpcMessageResponse::AuthOk(res) => res.get_cached_size(), |
| 215 | + GrpcMessageResponse::AuthDenied(res) => res.get_cached_size(), |
| 216 | + GrpcMessageResponse::RateLimit(res) => res.get_cached_size(), |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + fn get_unknown_fields(&self) -> &UnknownFields { |
| 221 | + match self { |
| 222 | + GrpcMessageResponse::AuthOk(res) => res.get_unknown_fields(), |
| 223 | + GrpcMessageResponse::AuthDenied(res) => res.get_unknown_fields(), |
| 224 | + GrpcMessageResponse::RateLimit(res) => res.get_unknown_fields(), |
| 225 | + } |
| 226 | + } |
| 227 | + |
| 228 | + fn mut_unknown_fields(&mut self) -> &mut UnknownFields { |
| 229 | + match self { |
| 230 | + GrpcMessageResponse::AuthOk(res) => res.mut_unknown_fields(), |
| 231 | + GrpcMessageResponse::AuthDenied(res) => res.mut_unknown_fields(), |
| 232 | + GrpcMessageResponse::RateLimit(res) => res.mut_unknown_fields(), |
| 233 | + } |
| 234 | + } |
| 235 | + |
| 236 | + fn as_any(&self) -> &dyn Any { |
| 237 | + match self { |
| 238 | + GrpcMessageResponse::AuthOk(res) => res.as_any(), |
| 239 | + GrpcMessageResponse::AuthDenied(res) => res.as_any(), |
| 240 | + GrpcMessageResponse::RateLimit(res) => res.as_any(), |
| 241 | + } |
| 242 | + } |
| 243 | + |
| 244 | + fn new() -> Self |
| 245 | + where |
| 246 | + Self: Sized, |
| 247 | + { |
| 248 | + // Returning default value |
| 249 | + GrpcMessageResponse::default() |
| 250 | + } |
| 251 | + |
| 252 | + fn default_instance() -> &'static Self |
| 253 | + where |
| 254 | + Self: Sized, |
| 255 | + { |
| 256 | + #[allow(non_upper_case_globals)] |
| 257 | + static instance: ::protobuf::rt::LazyV2<GrpcMessageResponse> = ::protobuf::rt::LazyV2::INIT; |
| 258 | + instance.get(|| GrpcMessageResponse::RateLimit(RateLimitResponse::new())) |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +impl GrpcMessageResponse { |
| 263 | + pub fn new( |
| 264 | + extension_type: ExtensionType, |
| 265 | + res_body_bytes: &Bytes, |
| 266 | + status_code: u32, |
| 267 | + ) -> GrpcMessageResult<GrpcMessageResponse> { |
| 268 | + match extension_type { |
| 269 | + ExtensionType::RateLimit => RateLimitService::response_message(res_body_bytes), |
| 270 | + ExtensionType::Auth => AuthService::response_message(res_body_bytes, status_code), |
| 271 | + } |
| 272 | + } |
| 273 | +} |
| 274 | + |
| 275 | +pub type GrpcMessageResult<T> = Result<T, ProtobufError>; |
0 commit comments