spikard 0.13.0 → 0.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. checksums.yaml +4 -4
  2. data/Steepfile +6 -0
  3. data/ext/spikard_rb/extconf.rb +1 -2
  4. data/ext/spikard_rb/{Cargo.lock → native/Cargo.lock} +819 -424
  5. data/ext/spikard_rb/native/Cargo.toml +24 -0
  6. data/ext/spikard_rb/src/lib.rs +5366 -3
  7. data/lib/spikard/native.rb +86 -0
  8. data/lib/spikard/version.rb +6 -1
  9. data/lib/spikard.rb +8 -52
  10. data/lib/spikard_rb.so +0 -0
  11. data/sig/types.rbs +427 -0
  12. metadata +14 -243
  13. data/LICENSE +0 -1
  14. data/README.md +0 -285
  15. data/ext/spikard_rb/Cargo.toml +0 -17
  16. data/lib/spikard/app.rb +0 -458
  17. data/lib/spikard/background.rb +0 -58
  18. data/lib/spikard/config.rb +0 -506
  19. data/lib/spikard/converters.rb +0 -13
  20. data/lib/spikard/grpc.rb +0 -232
  21. data/lib/spikard/handler_wrapper.rb +0 -113
  22. data/lib/spikard/provide.rb +0 -315
  23. data/lib/spikard/response.rb +0 -198
  24. data/lib/spikard/schema.rb +0 -243
  25. data/lib/spikard/sse.rb +0 -111
  26. data/lib/spikard/streaming_response.rb +0 -44
  27. data/lib/spikard/testing.rb +0 -474
  28. data/lib/spikard/upload_file.rb +0 -131
  29. data/lib/spikard/websocket.rb +0 -59
  30. data/sig/spikard.rbs +0 -739
  31. data/vendor/crates/spikard-bindings-shared/Cargo.toml +0 -75
  32. data/vendor/crates/spikard-bindings-shared/examples/config_extraction.rs +0 -132
  33. data/vendor/crates/spikard-bindings-shared/src/config_extractor.rs +0 -905
  34. data/vendor/crates/spikard-bindings-shared/src/conversion_traits.rs +0 -210
  35. data/vendor/crates/spikard-bindings-shared/src/di_traits.rs +0 -252
  36. data/vendor/crates/spikard-bindings-shared/src/error_response.rs +0 -404
  37. data/vendor/crates/spikard-bindings-shared/src/grpc_metadata.rs +0 -199
  38. data/vendor/crates/spikard-bindings-shared/src/handler_base.rs +0 -252
  39. data/vendor/crates/spikard-bindings-shared/src/json_conversion.rs +0 -829
  40. data/vendor/crates/spikard-bindings-shared/src/lazy_cache.rs +0 -587
  41. data/vendor/crates/spikard-bindings-shared/src/lib.rs +0 -33
  42. data/vendor/crates/spikard-bindings-shared/src/lifecycle_base.rs +0 -298
  43. data/vendor/crates/spikard-bindings-shared/src/lifecycle_executor.rs +0 -594
  44. data/vendor/crates/spikard-bindings-shared/src/response_builder.rs +0 -743
  45. data/vendor/crates/spikard-bindings-shared/src/response_interpreter.rs +0 -944
  46. data/vendor/crates/spikard-bindings-shared/src/test_client_base.rs +0 -260
  47. data/vendor/crates/spikard-bindings-shared/src/validation_helpers.rs +0 -369
  48. data/vendor/crates/spikard-bindings-shared/tests/config_extractor_behavior.rs +0 -192
  49. data/vendor/crates/spikard-bindings-shared/tests/error_response_edge_cases.rs +0 -383
  50. data/vendor/crates/spikard-bindings-shared/tests/full_coverage.rs +0 -459
  51. data/vendor/crates/spikard-bindings-shared/tests/handler_base_integration.rs +0 -280
  52. data/vendor/crates/spikard-bindings-shared/tests/integration_tests.rs +0 -669
  53. data/vendor/crates/spikard-core/Cargo.toml +0 -55
  54. data/vendor/crates/spikard-core/src/bindings/mod.rs +0 -3
  55. data/vendor/crates/spikard-core/src/bindings/response.rs +0 -130
  56. data/vendor/crates/spikard-core/src/debug.rs +0 -127
  57. data/vendor/crates/spikard-core/src/di/container.rs +0 -711
  58. data/vendor/crates/spikard-core/src/di/dependency.rs +0 -273
  59. data/vendor/crates/spikard-core/src/di/error.rs +0 -118
  60. data/vendor/crates/spikard-core/src/di/factory.rs +0 -548
  61. data/vendor/crates/spikard-core/src/di/graph.rs +0 -507
  62. data/vendor/crates/spikard-core/src/di/mod.rs +0 -192
  63. data/vendor/crates/spikard-core/src/di/resolved.rs +0 -428
  64. data/vendor/crates/spikard-core/src/di/value.rs +0 -282
  65. data/vendor/crates/spikard-core/src/errors.rs +0 -72
  66. data/vendor/crates/spikard-core/src/http.rs +0 -492
  67. data/vendor/crates/spikard-core/src/lib.rs +0 -29
  68. data/vendor/crates/spikard-core/src/lifecycle.rs +0 -1273
  69. data/vendor/crates/spikard-core/src/metadata.rs +0 -378
  70. data/vendor/crates/spikard-core/src/parameters.rs +0 -2546
  71. data/vendor/crates/spikard-core/src/problem.rs +0 -358
  72. data/vendor/crates/spikard-core/src/request_data.rs +0 -1146
  73. data/vendor/crates/spikard-core/src/router.rs +0 -530
  74. data/vendor/crates/spikard-core/src/schema_registry.rs +0 -197
  75. data/vendor/crates/spikard-core/src/type_hints.rs +0 -311
  76. data/vendor/crates/spikard-core/src/validation/error_mapper.rs +0 -710
  77. data/vendor/crates/spikard-core/src/validation/mod.rs +0 -470
  78. data/vendor/crates/spikard-core/tests/bindings_response_tests.rs +0 -136
  79. data/vendor/crates/spikard-core/tests/di_dependency_defaults.rs +0 -37
  80. data/vendor/crates/spikard-core/tests/error_mapper.rs +0 -761
  81. data/vendor/crates/spikard-core/tests/parameters_edge_cases.rs +0 -106
  82. data/vendor/crates/spikard-core/tests/parameters_full.rs +0 -701
  83. data/vendor/crates/spikard-core/tests/parameters_schema_and_formats.rs +0 -301
  84. data/vendor/crates/spikard-core/tests/request_data_roundtrip.rs +0 -67
  85. data/vendor/crates/spikard-core/tests/validation_coverage.rs +0 -250
  86. data/vendor/crates/spikard-core/tests/validation_error_paths.rs +0 -45
  87. data/vendor/crates/spikard-http/Cargo.toml +0 -82
  88. data/vendor/crates/spikard-http/examples/sse-notifications.rs +0 -148
  89. data/vendor/crates/spikard-http/examples/websocket-chat.rs +0 -92
  90. data/vendor/crates/spikard-http/src/auth.rs +0 -301
  91. data/vendor/crates/spikard-http/src/background.rs +0 -1859
  92. data/vendor/crates/spikard-http/src/bindings/mod.rs +0 -3
  93. data/vendor/crates/spikard-http/src/bindings/response.rs +0 -1
  94. data/vendor/crates/spikard-http/src/body_metadata.rs +0 -8
  95. data/vendor/crates/spikard-http/src/cors.rs +0 -1026
  96. data/vendor/crates/spikard-http/src/debug.rs +0 -128
  97. data/vendor/crates/spikard-http/src/di_handler.rs +0 -1672
  98. data/vendor/crates/spikard-http/src/grpc/framing.rs +0 -653
  99. data/vendor/crates/spikard-http/src/grpc/handler.rs +0 -1211
  100. data/vendor/crates/spikard-http/src/grpc/mod.rs +0 -556
  101. data/vendor/crates/spikard-http/src/grpc/service.rs +0 -706
  102. data/vendor/crates/spikard-http/src/grpc/streaming.rs +0 -319
  103. data/vendor/crates/spikard-http/src/handler_response.rs +0 -901
  104. data/vendor/crates/spikard-http/src/handler_trait.rs +0 -1015
  105. data/vendor/crates/spikard-http/src/handler_trait_tests.rs +0 -290
  106. data/vendor/crates/spikard-http/src/jsonrpc/http_handler.rs +0 -502
  107. data/vendor/crates/spikard-http/src/jsonrpc/method_registry.rs +0 -648
  108. data/vendor/crates/spikard-http/src/jsonrpc/mod.rs +0 -60
  109. data/vendor/crates/spikard-http/src/jsonrpc/openrpc.rs +0 -325
  110. data/vendor/crates/spikard-http/src/jsonrpc/protocol.rs +0 -1207
  111. data/vendor/crates/spikard-http/src/jsonrpc/router.rs +0 -2262
  112. data/vendor/crates/spikard-http/src/lib.rs +0 -566
  113. data/vendor/crates/spikard-http/src/lifecycle/adapter.rs +0 -230
  114. data/vendor/crates/spikard-http/src/lifecycle.rs +0 -1193
  115. data/vendor/crates/spikard-http/src/middleware/mod.rs +0 -560
  116. data/vendor/crates/spikard-http/src/middleware/multipart.rs +0 -912
  117. data/vendor/crates/spikard-http/src/middleware/urlencoded.rs +0 -513
  118. data/vendor/crates/spikard-http/src/middleware/validation.rs +0 -768
  119. data/vendor/crates/spikard-http/src/openapi/mod.rs +0 -309
  120. data/vendor/crates/spikard-http/src/openapi/parameter_extraction.rs +0 -535
  121. data/vendor/crates/spikard-http/src/openapi/schema_conversion.rs +0 -1363
  122. data/vendor/crates/spikard-http/src/openapi/spec_generation.rs +0 -667
  123. data/vendor/crates/spikard-http/src/query_parser.rs +0 -793
  124. data/vendor/crates/spikard-http/src/response.rs +0 -720
  125. data/vendor/crates/spikard-http/src/server/fast_router.rs +0 -186
  126. data/vendor/crates/spikard-http/src/server/grpc_routing.rs +0 -1243
  127. data/vendor/crates/spikard-http/src/server/handler.rs +0 -1661
  128. data/vendor/crates/spikard-http/src/server/lifecycle_execution.rs +0 -253
  129. data/vendor/crates/spikard-http/src/server/mod.rs +0 -1717
  130. data/vendor/crates/spikard-http/src/server/request_extraction.rs +0 -871
  131. data/vendor/crates/spikard-http/src/server/routing_factory.rs +0 -618
  132. data/vendor/crates/spikard-http/src/sse.rs +0 -1409
  133. data/vendor/crates/spikard-http/src/testing/form.rs +0 -52
  134. data/vendor/crates/spikard-http/src/testing/multipart.rs +0 -64
  135. data/vendor/crates/spikard-http/src/testing/test_client.rs +0 -825
  136. data/vendor/crates/spikard-http/src/testing.rs +0 -617
  137. data/vendor/crates/spikard-http/src/websocket.rs +0 -1477
  138. data/vendor/crates/spikard-http/tests/auth_integration.rs +0 -645
  139. data/vendor/crates/spikard-http/tests/background_behavior.rs +0 -832
  140. data/vendor/crates/spikard-http/tests/common/grpc_helpers.rs +0 -1012
  141. data/vendor/crates/spikard-http/tests/common/handlers.rs +0 -309
  142. data/vendor/crates/spikard-http/tests/common/mod.rs +0 -33
  143. data/vendor/crates/spikard-http/tests/common/test_builders.rs +0 -628
  144. data/vendor/crates/spikard-http/tests/di_handler_error_responses.rs +0 -162
  145. data/vendor/crates/spikard-http/tests/di_integration.rs +0 -192
  146. data/vendor/crates/spikard-http/tests/doc_snippets.rs +0 -5
  147. data/vendor/crates/spikard-http/tests/grpc_bidirectional_streaming.rs +0 -430
  148. data/vendor/crates/spikard-http/tests/grpc_client_streaming.rs +0 -738
  149. data/vendor/crates/spikard-http/tests/grpc_error_handling_test.rs +0 -652
  150. data/vendor/crates/spikard-http/tests/grpc_integration_test.rs +0 -334
  151. data/vendor/crates/spikard-http/tests/grpc_metadata_test.rs +0 -532
  152. data/vendor/crates/spikard-http/tests/grpc_server_integration.rs +0 -495
  153. data/vendor/crates/spikard-http/tests/grpc_server_streaming.rs +0 -975
  154. data/vendor/crates/spikard-http/tests/lifecycle_execution.rs +0 -1093
  155. data/vendor/crates/spikard-http/tests/middleware_stack_integration.rs +0 -389
  156. data/vendor/crates/spikard-http/tests/multipart_behavior.rs +0 -656
  157. data/vendor/crates/spikard-http/tests/request_extraction_full.rs +0 -513
  158. data/vendor/crates/spikard-http/tests/server_auth_middleware_behavior.rs +0 -328
  159. data/vendor/crates/spikard-http/tests/server_config_builder.rs +0 -335
  160. data/vendor/crates/spikard-http/tests/server_configured_router_behavior.rs +0 -374
  161. data/vendor/crates/spikard-http/tests/server_cors_preflight.rs +0 -83
  162. data/vendor/crates/spikard-http/tests/server_handler_wrappers.rs +0 -464
  163. data/vendor/crates/spikard-http/tests/server_method_router_additional_behavior.rs +0 -286
  164. data/vendor/crates/spikard-http/tests/server_method_router_coverage.rs +0 -118
  165. data/vendor/crates/spikard-http/tests/server_middleware_behavior.rs +0 -99
  166. data/vendor/crates/spikard-http/tests/server_middleware_branches.rs +0 -204
  167. data/vendor/crates/spikard-http/tests/server_openapi_jsonrpc_static.rs +0 -427
  168. data/vendor/crates/spikard-http/tests/server_router_behavior.rs +0 -121
  169. data/vendor/crates/spikard-http/tests/sse_behavior.rs +0 -620
  170. data/vendor/crates/spikard-http/tests/sse_full_behavior.rs +0 -584
  171. data/vendor/crates/spikard-http/tests/sse_handler_behavior.rs +0 -130
  172. data/vendor/crates/spikard-http/tests/test_client_requests.rs +0 -167
  173. data/vendor/crates/spikard-http/tests/testing_helpers.rs +0 -87
  174. data/vendor/crates/spikard-http/tests/testing_module_coverage.rs +0 -155
  175. data/vendor/crates/spikard-http/tests/urlencoded_content_type.rs +0 -82
  176. data/vendor/crates/spikard-http/tests/websocket_behavior.rs +0 -663
  177. data/vendor/crates/spikard-http/tests/websocket_full_behavior.rs +0 -440
  178. data/vendor/crates/spikard-http/tests/websocket_integration.rs +0 -150
  179. data/vendor/crates/spikard-rb/Cargo.toml +0 -63
  180. data/vendor/crates/spikard-rb/build.rs +0 -200
  181. data/vendor/crates/spikard-rb/src/background.rs +0 -63
  182. data/vendor/crates/spikard-rb/src/config/mod.rs +0 -5
  183. data/vendor/crates/spikard-rb/src/config/server_config.rs +0 -401
  184. data/vendor/crates/spikard-rb/src/conversion.rs +0 -688
  185. data/vendor/crates/spikard-rb/src/di/builder.rs +0 -100
  186. data/vendor/crates/spikard-rb/src/di/mod.rs +0 -410
  187. data/vendor/crates/spikard-rb/src/grpc/handler.rs +0 -875
  188. data/vendor/crates/spikard-rb/src/grpc/mod.rs +0 -13
  189. data/vendor/crates/spikard-rb/src/gvl.rs +0 -80
  190. data/vendor/crates/spikard-rb/src/handler.rs +0 -699
  191. data/vendor/crates/spikard-rb/src/integration/mod.rs +0 -3
  192. data/vendor/crates/spikard-rb/src/lib.rs +0 -2268
  193. data/vendor/crates/spikard-rb/src/lifecycle.rs +0 -334
  194. data/vendor/crates/spikard-rb/src/metadata/mod.rs +0 -5
  195. data/vendor/crates/spikard-rb/src/metadata/route_extraction.rs +0 -507
  196. data/vendor/crates/spikard-rb/src/request.rs +0 -439
  197. data/vendor/crates/spikard-rb/src/runtime/mod.rs +0 -5
  198. data/vendor/crates/spikard-rb/src/runtime/server_runner.rs +0 -368
  199. data/vendor/crates/spikard-rb/src/server.rs +0 -304
  200. data/vendor/crates/spikard-rb/src/sse.rs +0 -231
  201. data/vendor/crates/spikard-rb/src/testing/client.rs +0 -698
  202. data/vendor/crates/spikard-rb/src/testing/mod.rs +0 -7
  203. data/vendor/crates/spikard-rb/src/testing/sse.rs +0 -108
  204. data/vendor/crates/spikard-rb/src/testing/websocket.rs +0 -573
  205. data/vendor/crates/spikard-rb/src/websocket.rs +0 -521
  206. data/vendor/crates/spikard-rb-macros/Cargo.toml +0 -20
  207. data/vendor/crates/spikard-rb-macros/src/lib.rs +0 -51
@@ -1,439 +0,0 @@
1
- //! Native Request object for Ruby handlers.
2
- //!
3
- //! Ruby benchmarks frequently access only a subset of request fields. Building a full
4
- //! Ruby Hash for every request eagerly converts headers/cookies/query/etc even when
5
- //! unused. This module provides a native `Spikard::Native::Request` that lazily
6
- //! materialises Ruby values on demand and caches them for subsequent access.
7
-
8
- #![deny(clippy::unwrap_used)]
9
-
10
- use bytes::Bytes;
11
- use magnus::prelude::*;
12
- use magnus::value::InnerValue;
13
- use magnus::value::LazyId;
14
- use magnus::value::Opaque;
15
- use magnus::{Error, RHash, RString, Ruby, Symbol, Value, gc::Marker};
16
- use serde_json::{Map as JsonMap, Value as JsonValue};
17
- use spikard_http::RequestData;
18
- use std::cell::RefCell;
19
- use std::collections::HashMap;
20
- use std::sync::Arc;
21
-
22
- use crate::conversion::{json_to_ruby_with_uploads, map_to_ruby_hash, multimap_to_ruby_hash};
23
- use crate::metadata::json_to_ruby;
24
-
25
- #[derive(Default)]
26
- struct RequestCache {
27
- method: Option<Opaque<Value>>,
28
- path: Option<Opaque<Value>>,
29
- path_params: Option<Opaque<Value>>,
30
- query: Option<Opaque<Value>>,
31
- raw_query: Option<Opaque<Value>>,
32
- headers: Option<Opaque<Value>>,
33
- cookies: Option<Opaque<Value>>,
34
- body: Option<Opaque<Value>>,
35
- raw_body: Option<Opaque<Value>>,
36
- params: Option<Opaque<Value>>,
37
- to_h: Option<Opaque<Value>>,
38
- }
39
-
40
- #[magnus::wrap(class = "Spikard::Native::Request", free_immediately, mark)]
41
- pub struct NativeRequest {
42
- method: String,
43
- path: String,
44
- path_params: Arc<HashMap<String, String>>,
45
- query_params: JsonValue,
46
- raw_query_params: Arc<HashMap<String, Vec<String>>>,
47
- body: JsonValue,
48
- raw_body: Option<Bytes>,
49
- headers: Arc<HashMap<String, String>>,
50
- cookies: Arc<HashMap<String, String>>,
51
- validated_params: Option<JsonValue>,
52
- /// Upload file class for wrapping file upload objects in the body.
53
- /// When present, `json_to_ruby_with_uploads` is used instead of `json_to_ruby`.
54
- upload_file_class: Option<Opaque<Value>>,
55
- cache: RefCell<RequestCache>,
56
- }
57
-
58
- static KEY_METHOD: LazyId = LazyId::new("method");
59
- static KEY_PATH: LazyId = LazyId::new("path");
60
- static KEY_PATH_PARAMS: LazyId = LazyId::new("path_params");
61
- static KEY_QUERY: LazyId = LazyId::new("query");
62
- static KEY_RAW_QUERY: LazyId = LazyId::new("raw_query");
63
- static KEY_HEADERS: LazyId = LazyId::new("headers");
64
- static KEY_COOKIES: LazyId = LazyId::new("cookies");
65
- static KEY_BODY: LazyId = LazyId::new("body");
66
- static KEY_RAW_BODY: LazyId = LazyId::new("raw_body");
67
- static KEY_PARAMS: LazyId = LazyId::new("params");
68
-
69
- impl NativeRequest {
70
- /// Convert RequestData to NativeRequest with Arc unwrapping for lazy cache.
71
- ///
72
- /// # Arc Unwrapping Strategy
73
- ///
74
- /// `spikard_http::RequestData` has Arc-wrapped fields for cheap cloning:
75
- /// - `query_params: Arc<Value>`
76
- /// - `body: Arc<Value>`
77
- /// - `validated_params: Option<Arc<Value>>`
78
- ///
79
- /// This method unwraps these Arc fields into plain Values for storage in NativeRequest,
80
- /// using `Arc::try_unwrap()` to eliminate the clone when the Arc has a unique reference.
81
- ///
82
- /// ## Pattern: Arc::try_unwrap Optimization
83
- ///
84
- /// ```text
85
- /// Arc::try_unwrap(arc)
86
- /// → Ok(Value) if Arc has unique ref (no other clones)
87
- /// → Err(Arc) if Arc has multiple refs
88
- ///
89
- /// Result: eliminates guaranteed clone ~95% of time (single request flow)
90
- /// Fallback: clones only when Arc is shared (rare case)
91
- /// ```
92
- ///
93
- /// ## Why This Works with Lazy Caching
94
- ///
95
- /// The lazy cache pattern in this struct caches converted Ruby values, not the original JSON.
96
- /// Once unwrapped here, the Arc-wrapped Values are never unwrapped again:
97
- ///
98
- /// 1. `RequestData` arrives with Arc-wrapped JSON (from HTTP layer)
99
- /// 2. `from_request_data()` unwraps Arc → stores plain JsonValue
100
- /// 3. Cache stores converted Ruby values (not JSON)
101
- /// 4. No further Arc operations needed in cache methods
102
- ///
103
- /// This is a **one-time operation per request**, not repeated per field access.
104
- ///
105
- /// ## Performance Impact
106
- ///
107
- /// - Typical: 5-10% faster (eliminates clone for ~95% of requests)
108
- /// - Worst case: Same as clone (if Arc is shared, which rarely happens)
109
- /// - Best case: Pure move, zero copy (Arc has unique reference)
110
- pub(crate) fn from_request_data(
111
- request_data: RequestData,
112
- validated_params: Option<JsonValue>,
113
- upload_file_class: Option<Opaque<Value>>,
114
- ) -> Self {
115
- let RequestData {
116
- path_params,
117
- query_params,
118
- raw_query_params,
119
- body,
120
- raw_body,
121
- headers,
122
- cookies,
123
- method,
124
- path,
125
- ..
126
- } = request_data;
127
-
128
- Self {
129
- method,
130
- path,
131
- path_params,
132
- // Arc::try_unwrap eliminates clone when possible (most requests have unique Arc ref)
133
- query_params: Arc::try_unwrap(query_params).unwrap_or_else(|arc| (*arc).clone()),
134
- raw_query_params,
135
- // Arc::try_unwrap eliminates clone when possible (most requests have unique Arc ref)
136
- body: Arc::try_unwrap(body).unwrap_or_else(|arc| (*arc).clone()),
137
- raw_body,
138
- headers,
139
- cookies,
140
- validated_params,
141
- upload_file_class,
142
- cache: RefCell::new(RequestCache::default()),
143
- }
144
- }
145
-
146
- fn cache_get(cache: Option<&Opaque<Value>>, ruby: &Ruby) -> Option<Value> {
147
- cache.map(|v| v.get_inner_with(ruby))
148
- }
149
-
150
- fn cache_set(slot: &mut Option<Opaque<Value>>, value: Value) -> Value {
151
- *slot = Some(Opaque::from(value));
152
- value
153
- }
154
-
155
- pub(crate) fn method(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
156
- if let Some(value) = {
157
- let cache = this.cache.borrow();
158
- Self::cache_get(cache.method.as_ref(), ruby)
159
- } {
160
- return Ok(value);
161
- }
162
- let value = ruby.str_new(&this.method).as_value();
163
- let mut cache = this.cache.borrow_mut();
164
- Ok(Self::cache_set(&mut cache.method, value))
165
- }
166
-
167
- pub(crate) fn path(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
168
- if let Some(value) = {
169
- let cache = this.cache.borrow();
170
- Self::cache_get(cache.path.as_ref(), ruby)
171
- } {
172
- return Ok(value);
173
- }
174
- let value = ruby.str_new(&this.path).as_value();
175
- let mut cache = this.cache.borrow_mut();
176
- Ok(Self::cache_set(&mut cache.path, value))
177
- }
178
-
179
- pub(crate) fn path_params(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
180
- if let Some(cached) = {
181
- let cache = this.cache.borrow();
182
- Self::cache_get(cache.path_params.as_ref(), ruby)
183
- } {
184
- return Ok(cached);
185
- }
186
- if let Some(validated) = &this.validated_params
187
- && let Some(validated_map) = validated.as_object()
188
- {
189
- let mut subset = JsonMap::new();
190
- for key in this.path_params.keys() {
191
- if let Some(value) = validated_map.get(key) {
192
- subset.insert(key.clone(), value.clone());
193
- }
194
- }
195
- if !subset.is_empty() {
196
- let value = json_to_ruby(ruby, &JsonValue::Object(subset))?;
197
- let mut cache = this.cache.borrow_mut();
198
- return Ok(Self::cache_set(&mut cache.path_params, value));
199
- }
200
- }
201
- let value = map_to_ruby_hash(ruby, this.path_params.as_ref())?;
202
- let mut cache = this.cache.borrow_mut();
203
- Ok(Self::cache_set(&mut cache.path_params, value))
204
- }
205
-
206
- pub(crate) fn query(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
207
- if let Some(cached) = {
208
- let cache = this.cache.borrow();
209
- Self::cache_get(cache.query.as_ref(), ruby)
210
- } {
211
- return Ok(cached);
212
- }
213
- if let Some(validated) = &this.validated_params
214
- && let Some(validated_map) = validated.as_object()
215
- {
216
- let mut subset = JsonMap::new();
217
- if !this.raw_query_params.is_empty() {
218
- for key in this.raw_query_params.keys() {
219
- if let Some(value) = validated_map.get(key) {
220
- subset.insert(key.clone(), value.clone());
221
- }
222
- }
223
- } else if let Some(query_map) = this.query_params.as_object() {
224
- for key in query_map.keys() {
225
- if let Some(value) = validated_map.get(key) {
226
- subset.insert(key.clone(), value.clone());
227
- }
228
- }
229
- }
230
-
231
- if !subset.is_empty() {
232
- let value = json_to_ruby(ruby, &JsonValue::Object(subset))?;
233
- let mut cache = this.cache.borrow_mut();
234
- return Ok(Self::cache_set(&mut cache.query, value));
235
- }
236
- }
237
- let value = json_to_ruby(ruby, &this.query_params)?;
238
- let mut cache = this.cache.borrow_mut();
239
- Ok(Self::cache_set(&mut cache.query, value))
240
- }
241
-
242
- pub(crate) fn raw_query(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
243
- if let Some(cached) = {
244
- let cache = this.cache.borrow();
245
- Self::cache_get(cache.raw_query.as_ref(), ruby)
246
- } {
247
- return Ok(cached);
248
- }
249
- let value = multimap_to_ruby_hash(ruby, this.raw_query_params.as_ref())?;
250
- let mut cache = this.cache.borrow_mut();
251
- Ok(Self::cache_set(&mut cache.raw_query, value))
252
- }
253
-
254
- pub(crate) fn headers(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
255
- if let Some(cached) = {
256
- let cache = this.cache.borrow();
257
- Self::cache_get(cache.headers.as_ref(), ruby)
258
- } {
259
- return Ok(cached);
260
- }
261
- let value = map_to_ruby_hash(ruby, this.headers.as_ref())?;
262
- let mut cache = this.cache.borrow_mut();
263
- Ok(Self::cache_set(&mut cache.headers, value))
264
- }
265
-
266
- pub(crate) fn cookies(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
267
- if let Some(cached) = {
268
- let cache = this.cache.borrow();
269
- Self::cache_get(cache.cookies.as_ref(), ruby)
270
- } {
271
- return Ok(cached);
272
- }
273
- let value = map_to_ruby_hash(ruby, this.cookies.as_ref())?;
274
- let mut cache = this.cache.borrow_mut();
275
- Ok(Self::cache_set(&mut cache.cookies, value))
276
- }
277
-
278
- pub(crate) fn body(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
279
- if let Some(cached) = {
280
- let cache = this.cache.borrow();
281
- Self::cache_get(cache.body.as_ref(), ruby)
282
- } {
283
- return Ok(cached);
284
- }
285
- let upload_cls = this.upload_file_class.as_ref().map(|o| o.get_inner_with(ruby));
286
- let value = json_to_ruby_with_uploads(ruby, &this.body, upload_cls.as_ref())?;
287
- let mut cache = this.cache.borrow_mut();
288
- Ok(Self::cache_set(&mut cache.body, value))
289
- }
290
-
291
- pub(crate) fn raw_body(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
292
- if let Some(cached) = {
293
- let cache = this.cache.borrow();
294
- Self::cache_get(cache.raw_body.as_ref(), ruby)
295
- } {
296
- return Ok(cached);
297
- }
298
- let value = match &this.raw_body {
299
- Some(bytes) => ruby.str_from_slice(bytes.as_ref()).as_value(),
300
- None => ruby.qnil().as_value(),
301
- };
302
- let mut cache = this.cache.borrow_mut();
303
- Ok(Self::cache_set(&mut cache.raw_body, value))
304
- }
305
-
306
- pub(crate) fn params(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
307
- if let Some(value) = {
308
- let cache = this.cache.borrow();
309
- Self::cache_get(cache.params.as_ref(), ruby)
310
- } {
311
- return Ok(value);
312
- }
313
-
314
- let value = if let Some(validated) = &this.validated_params {
315
- json_to_ruby(ruby, validated)?
316
- } else {
317
- let params = ruby.hash_new();
318
- if let Some(hash) = RHash::from_value(Self::path_params(ruby, this)?) {
319
- let _: Value = params.funcall("merge!", (hash,))?;
320
- }
321
- if let Some(hash) = RHash::from_value(Self::query(ruby, this)?) {
322
- let _: Value = params.funcall("merge!", (hash,))?;
323
- }
324
- if let Some(hash) = RHash::from_value(Self::headers(ruby, this)?) {
325
- let _: Value = params.funcall("merge!", (hash,))?;
326
- }
327
- if let Some(hash) = RHash::from_value(Self::cookies(ruby, this)?) {
328
- let _: Value = params.funcall("merge!", (hash,))?;
329
- }
330
- params.as_value()
331
- };
332
-
333
- let mut cache = this.cache.borrow_mut();
334
- Ok(Self::cache_set(&mut cache.params, value))
335
- }
336
-
337
- pub(crate) fn to_h(ruby: &Ruby, this: &Self) -> Result<Value, Error> {
338
- if let Some(value) = {
339
- let cache = this.cache.borrow();
340
- Self::cache_get(cache.to_h.as_ref(), ruby)
341
- } {
342
- return Ok(value);
343
- }
344
-
345
- let hash = ruby.hash_new_capa(10);
346
- hash.aset(ruby.intern("method"), Self::method(ruby, this)?)?;
347
- hash.aset(ruby.intern("path"), Self::path(ruby, this)?)?;
348
- hash.aset(ruby.intern("path_params"), Self::path_params(ruby, this)?)?;
349
- hash.aset(ruby.intern("query"), Self::query(ruby, this)?)?;
350
- hash.aset(ruby.intern("raw_query"), Self::raw_query(ruby, this)?)?;
351
- hash.aset(ruby.intern("headers"), Self::headers(ruby, this)?)?;
352
- hash.aset(ruby.intern("cookies"), Self::cookies(ruby, this)?)?;
353
- hash.aset(ruby.intern("body"), Self::body(ruby, this)?)?;
354
- hash.aset(ruby.intern("raw_body"), Self::raw_body(ruby, this)?)?;
355
- hash.aset(ruby.intern("params"), Self::params(ruby, this)?)?;
356
-
357
- let mut cache = this.cache.borrow_mut();
358
- Ok(Self::cache_set(&mut cache.to_h, hash.as_value()))
359
- }
360
-
361
- pub(crate) fn index(ruby: &Ruby, this: &Self, key: Value) -> Result<Value, Error> {
362
- if let Ok(sym) = Symbol::try_convert(key) {
363
- return if sym == KEY_METHOD {
364
- Self::method(ruby, this)
365
- } else if sym == KEY_PATH {
366
- Self::path(ruby, this)
367
- } else if sym == KEY_PATH_PARAMS {
368
- Self::path_params(ruby, this)
369
- } else if sym == KEY_QUERY {
370
- Self::query(ruby, this)
371
- } else if sym == KEY_RAW_QUERY {
372
- Self::raw_query(ruby, this)
373
- } else if sym == KEY_HEADERS {
374
- Self::headers(ruby, this)
375
- } else if sym == KEY_COOKIES {
376
- Self::cookies(ruby, this)
377
- } else if sym == KEY_BODY {
378
- Self::body(ruby, this)
379
- } else if sym == KEY_RAW_BODY {
380
- Self::raw_body(ruby, this)
381
- } else if sym == KEY_PARAMS {
382
- Self::params(ruby, this)
383
- } else {
384
- Ok(ruby.qnil().as_value())
385
- };
386
- }
387
-
388
- if let Ok(text) = RString::try_convert(key) {
389
- // SAFETY: We only borrow the slice for the duration of this match
390
- // block and never mutate the RString. The GVL is held, so no other
391
- // Ruby thread can modify or move the string's backing memory.
392
- let slice = unsafe { text.as_slice() };
393
- return match slice {
394
- b"method" => Self::method(ruby, this),
395
- b"path" => Self::path(ruby, this),
396
- b"path_params" => Self::path_params(ruby, this),
397
- b"query" => Self::query(ruby, this),
398
- b"raw_query" => Self::raw_query(ruby, this),
399
- b"headers" => Self::headers(ruby, this),
400
- b"cookies" => Self::cookies(ruby, this),
401
- b"body" => Self::body(ruby, this),
402
- b"raw_body" => Self::raw_body(ruby, this),
403
- b"params" => Self::params(ruby, this),
404
- _ => Ok(ruby.qnil().as_value()),
405
- };
406
- }
407
-
408
- Ok(ruby.qnil().as_value())
409
- }
410
-
411
- #[allow(dead_code)]
412
- pub(crate) fn mark(&self, marker: &Marker) {
413
- if let Ok(ruby) = Ruby::get() {
414
- let cache = self.cache.borrow();
415
- for handle in [
416
- &cache.method,
417
- &cache.path,
418
- &cache.path_params,
419
- &cache.query,
420
- &cache.raw_query,
421
- &cache.headers,
422
- &cache.cookies,
423
- &cache.body,
424
- &cache.raw_body,
425
- &cache.params,
426
- &cache.to_h,
427
- ]
428
- .into_iter()
429
- .filter_map(|value| value.as_ref())
430
- {
431
- marker.mark(handle.get_inner_with(&ruby));
432
- }
433
-
434
- if let Some(ref cls) = self.upload_file_class {
435
- marker.mark(cls.get_inner_with(&ruby));
436
- }
437
- }
438
- }
439
- }
@@ -1,5 +0,0 @@
1
- //! HTTP server runtime initialization and management.
2
-
3
- pub mod server_runner;
4
-
5
- pub use server_runner::{normalize_route_metadata, run_server};