-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest.rs
More file actions
276 lines (231 loc) · 7.99 KB
/
Copy pathrequest.rs
File metadata and controls
276 lines (231 loc) · 7.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::os::raw::c_char;
use magnus::{value::{qnil, ReprValue}, RString, Value, RHash};
use bytes::Bytes;
use hyper::Request as HyperRequest;
use rb_sys::{rb_str_set_len, rb_str_modify, rb_str_modify_expand, rb_str_capacity, RSTRING_PTR, VALUE};
use crate::grpc;
use log::debug;
use form_urlencoded;
// Trait for common buffer filling behavior
trait FillBuffer {
// Get the bytes to be copied into the buffer
fn get_body_bytes(&self) -> Bytes;
// Get the size of the body
fn get_body_size(&self) -> usize;
// Common implementation for filling a Ruby string buffer
fn fill_buffer(&self, buffer: RString) -> i64 {
let body_bytes = self.get_body_bytes();
let body_len: i64 = body_bytes.len().try_into().unwrap();
unsafe {
let rb_value = buffer.as_value();
let inner: VALUE = std::ptr::read(&rb_value as *const _ as *const VALUE);
let existing_capacity = rb_str_capacity(inner) as i64;
if existing_capacity < body_len {
rb_str_modify_expand(inner, body_len - existing_capacity);
} else {
rb_str_modify(inner);
}
if body_len > 0 {
let body_ptr = body_bytes.as_ptr() as *const c_char;
let rb_string_ptr = RSTRING_PTR(inner) as *mut c_char;
std::ptr::copy(body_ptr, rb_string_ptr, body_len as usize);
}
rb_str_set_len(inner, body_len);
}
body_len
}
}
// Base HTTP request type
#[derive(Debug)]
#[magnus::wrap(class = "HyperRuby::Request")]
pub struct Request {
request: HyperRequest<Bytes>
}
// Specialized gRPC request type
#[derive(Debug)]
#[magnus::wrap(class = "HyperRuby::GrpcRequest")]
pub struct GrpcRequest {
request: HyperRequest<Bytes>,
service: String,
method: String
}
impl FillBuffer for Request {
fn get_body_bytes(&self) -> Bytes {
self.request.body().clone()
}
fn get_body_size(&self) -> usize {
self.request.body().len()
}
}
impl FillBuffer for GrpcRequest {
fn get_body_bytes(&self) -> Bytes {
if let Some((_, message)) = grpc::decode_grpc_frame(self.request.body()) {
message
} else {
Bytes::new()
}
}
fn get_body_size(&self) -> usize {
if let Some((_, message)) = grpc::decode_grpc_frame(self.request.body()) {
message.len()
} else {
0
}
}
}
impl Request {
pub fn new(request: HyperRequest<Bytes>) -> Self {
Self { request }
}
pub fn method(&self) -> String {
self.request.method().to_string()
}
pub fn path(&self) -> RString {
RString::new(self.request.uri().path())
}
pub fn host(&self) -> Value {
match self.request.uri().host() {
Some(host) => RString::new(host).as_value(),
// Fallback to Host header if no host in URI object
None => match self.request.headers().get("Host") {
Some(value) => match value.to_str() {
Ok(value) => RString::new(value).as_value(),
Err(_) => qnil().as_value(),
},
None => qnil().as_value(),
}
}
}
pub fn query_params(&self) -> RHash {
let params = RHash::new();
if let Some(query) = self.request.uri().query() {
for (key, value) in form_urlencoded::parse(query.as_bytes()) {
params.aset(key.to_string(), value.to_string()).unwrap();
}
}
params
}
pub fn query_param(&self, key: RString) -> Value {
let key_str = unsafe { key.as_str().unwrap() };
if let Some(query) = self.request.uri().query() {
for (param_key, value) in form_urlencoded::parse(query.as_bytes()) {
if param_key == key_str {
return RString::new(&value).as_value();
}
}
}
qnil().as_value()
}
pub fn header(&self, key: RString) -> Value {
let key_str = unsafe { key.as_str().unwrap() };
match self.request.headers().get(key_str) {
Some(value) => match value.to_str() {
Ok(value) => RString::new(value).as_value(),
Err(_) => qnil().as_value(),
},
None => qnil().as_value(),
}
}
pub fn headers(&self) -> RHash {
let headers = RHash::new();
for (name, value) in self.request.headers() {
if let Ok(value_str) = value.to_str() {
headers.aset(name.to_string(), value_str.to_string()).unwrap();
}
}
headers
}
pub fn body_size(&self) -> usize {
self.get_body_size()
}
pub fn body(&self) -> RString {
let buffer = RString::buf_new(self.body_size());
self.fill_body(buffer);
buffer
}
pub fn fill_body(&self, buffer: RString) -> i64 {
self.fill_buffer(buffer)
}
pub fn inspect(&self) -> RString {
let method = self.request.method().to_string();
let path = self.request.uri().path();
let query = self.request.uri().query().unwrap_or("");
let query_display = if !query.is_empty() { format!("?{}", query) } else { String::new() };
let body_size = self.body_size();
RString::new(&format!("#<HyperRuby::Request method={} path={}{} body_size={}>",
method, path, query_display, body_size))
}
}
impl GrpcRequest {
pub fn new(request: HyperRequest<Bytes>) -> Option<Self> {
debug!("Creating GrpcRequest from path: {}", request.uri().path());
// Path format could be "/Echo" or "/echo.Echo/Echo" - handle both
let path = request.uri().path();
let parts: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
debug!(" Path parts: {:?}", parts);
if parts.is_empty() {
debug!(" Failed: Empty path");
return None;
}
// If we have two parts, use them as service/method
// If we have one part, use it as both
let (service, method) = if parts.len() >= 2 {
(parts[0].to_string(), parts[1].to_string())
} else {
(format!("echo.{}", parts[0]), parts[0].to_string())
};
debug!(" Extracted service: {}, method: {}", service, method);
Some(Self {
request,
service,
method
})
}
pub fn service(&self) -> RString {
RString::new(&self.service)
}
pub fn method(&self) -> RString {
RString::new(&self.method)
}
pub fn header(&self, key: RString) -> Value {
let key_str = unsafe { key.as_str().unwrap() };
match self.request.headers().get(key_str) {
Some(value) => match value.to_str() {
Ok(value) => RString::new(value).as_value(),
Err(_) => qnil().as_value(),
},
None => qnil().as_value(),
}
}
pub fn headers(&self) -> RHash {
let headers = RHash::new();
for (name, value) in self.request.headers() {
if let Ok(value_str) = value.to_str() {
headers.aset(name.to_string(), value_str.to_string()).unwrap();
}
}
headers
}
pub fn body_size(&self) -> usize {
self.get_body_size()
}
pub fn body(&self) -> RString {
let buffer = RString::buf_new(self.body_size());
self.fill_body(buffer);
buffer
}
pub fn fill_body(&self, buffer: RString) -> i64 {
self.fill_buffer(buffer)
}
pub fn is_compressed(&self) -> bool {
if let Some((compressed, _)) = grpc::decode_grpc_frame(self.request.body()) {
compressed
} else {
false
}
}
pub fn inspect(&self) -> RString {
let body_size = self.body_size();
RString::new(&format!("#<HyperRuby::GrpcRequest service={} method={} body_size={}>", self.service, self.method, body_size))
}
}