Creates a permanent redirect (308) response with the specified target URL Usage: permanent_redirect(target="/new-url")
(
state: &mut State<'gc>,
args: Vec<Value<'gc>>,
)
| 122 | /// Creates a permanent redirect (308) response with the specified target URL |
| 123 | /// Usage: permanent_redirect(target="/new-url") |
| 124 | pub fn permanent_redirect<'gc>( |
| 125 | state: &mut State<'gc>, |
| 126 | args: Vec<Value<'gc>>, |
| 127 | ) -> Result<Value<'gc>, VmError> { |
| 128 | if args.len() != 2 || !matches!(args[0], Value::String(_)) { |
| 129 | return Err(VmError::RuntimeError( |
| 130 | "permanent_redirect() requires a target URL as keyword argument (e.g., permanent_redirect(target=\"/new-url\"))".into() |
| 131 | )); |
| 132 | } |
| 133 | |
| 134 | if args[0].as_string()?.to_str().unwrap() != "target" { |
| 135 | return Err(VmError::RuntimeError( |
| 136 | "permanent_redirect() requires 'target' keyword argument".into(), |
| 137 | )); |
| 138 | } |
| 139 | |
| 140 | let target = args[1].as_string()?; |
| 141 | |
| 142 | // Create headers with Location set |
| 143 | let fields = [(state.intern(b"Location"), Value::String(target))] |
| 144 | .into_iter() |
| 145 | .collect(); |
| 146 | |
| 147 | // Create response with 308 status code |
| 148 | let args = [ |
| 149 | Value::String(state.intern(b"status_code")), |
| 150 | Value::Number(308.0), |
| 151 | Value::String(state.intern(b"headers")), |
| 152 | Value::Object(Gc::new(state, RefLock::new(Object { fields }))), |
| 153 | ] |
| 154 | .into_iter() |
| 155 | .collect(); |
| 156 | |
| 157 | response(state, args) |
| 158 | } |