Creates a temporary redirect (307) response with the specified target URL Usage: temporary_redirect(target="/new-url")
(
state: &mut State<'gc>,
args: Vec<Value<'gc>>,
)
| 86 | /// Creates a temporary redirect (307) response with the specified target URL |
| 87 | /// Usage: temporary_redirect(target="/new-url") |
| 88 | pub fn temporary_redirect<'gc>( |
| 89 | state: &mut State<'gc>, |
| 90 | args: Vec<Value<'gc>>, |
| 91 | ) -> Result<Value<'gc>, VmError> { |
| 92 | if args.len() != 2 || !matches!(args[0], Value::String(_)) { |
| 93 | return Err(VmError::RuntimeError( |
| 94 | "temporary_redirect() requires a target URL as keyword argument (e.g., temporary_redirect(target=\"/new-url\"))".into() |
| 95 | )); |
| 96 | } |
| 97 | |
| 98 | if args[0].as_string()?.to_str().unwrap() != "target" { |
| 99 | return Err(VmError::RuntimeError( |
| 100 | "temporary_redirect() requires 'target' keyword argument".into(), |
| 101 | )); |
| 102 | } |
| 103 | |
| 104 | let target = args[1].as_string()?; |
| 105 | |
| 106 | // Create headers with Location set |
| 107 | let fields = [(state.intern(b"Location"), Value::String(target))] |
| 108 | .into_iter() |
| 109 | .collect(); |
| 110 | // Create response with 307 status code |
| 111 | let args = [ |
| 112 | Value::String(state.intern(b"status_code")), |
| 113 | Value::Number(307.0), |
| 114 | Value::String(state.intern(b"headers")), |
| 115 | Value::Object(Gc::new(state, RefLock::new(Object { fields }))), |
| 116 | ] |
| 117 | .into_iter() |
| 118 | .collect(); |
| 119 | response(state, args) |
| 120 | } |
| 121 | |
| 122 | /// Creates a permanent redirect (308) response with the specified target URL |
| 123 | /// Usage: permanent_redirect(target="/new-url") |