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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! # urlexpand
//!
//! A small library for expanding ("unshortening") shortened URLs into their final destination.
//!
//! The crate is designed around **resolver modules**, where each resolver knows how to expand
//! one (or a family) of shortener services—especially the ones that don't rely purely on HTTP 3xx
//! redirects and instead use HTML/JS-based redirect pages.
//!
//! ## Goals
//!
//! - **Fast, reliable expansion** for common shorteners
//! - **Extensible** resolver structure (add a new module for a new shortener)
//! - **Non-JS resolution** (no headless browser) using a mix of redirect following + parsing + API lookups
//! - Consistent `Result<T>` / `Error` handling across resolvers
//! - **Unified API** with both async and blocking support via feature flags
//!
//! ## How it works (high level)
//!
//! 1. The caller provides a URL (potentially shortened).
//! 2. The library picks a resolver (or tries several in order).
//! 3. The resolver expands the URL using one of these strategies:
//! - **HTTP redirect following** (3xx chains)
//! - **HTML pattern extraction** (regex-based "click-through" / meta / JS hints)
//! - **Service API lookup** (when the browser normally uses JS to fetch the destination)
//! 4. The final URL is returned as a `String`.
//!
//! ## API Usage
//!
//! The library provides a unified `unshorten()` function that adapts based on feature flags:
//!
//! ### Default (async-only)
//!
//! ```ignore
//! use urlexpand::unshorten;
//! use std::time::Duration;
//!
//! let final_url = unshorten("https://bit.ly/3alqLKi", Some(Duration::from_secs(10))).await?;
//! ```
//!
//! ### With blocking feature
//!
//! ```ignore
//! // Add to Cargo.toml: urlexpand = { version = "...", features = ["blocking"] }
//!
//! use urlexpand::unshorten;
//! use std::time::Duration;
//!
//! // Blocking version
//! let final_url = unshorten("https://bit.ly/3alqLKi", Some(Duration::from_secs(10)))?;
//!
//! // Async version (still available when blocking feature is enabled)
//! let final_url = unshorten_async("https://bit.ly/3alqLKi", Some(Duration::from_secs(10))).await?;
//! ```
//!
//! ## Module layout
//!
//! A common structure looks like this:
//!
//! - `src/lib.rs`
//! - exports `Result` and `Error`
//! - exports the public expansion API
//! - `src/error.rs`
//! - defines `Error` and error conversions (e.g. `From<reqwest::Error>`)
//! - `src/resolvers/`
//! - each file is a shortener-specific resolver (e.g. `tinyurl.rs`, `urlshortdev.rs`, etc.)
//! - `src/resolvers/mod.rs`
//! - re-exports resolver functions and common helper utilities
//!
//! ## Common helper utilities
//!
//! Many resolver modules share helpers such as:
//!
//! - `get_client_builder(timeout)` — returns a configured `reqwest::ClientBuilder`
//! - `from_re(text, pattern)` — returns the first capture group match as `Option<String>`
//!
//! These helpers keep each resolver tiny and consistent.
//!
//! ## Error handling model
//!
//! Resolvers generally return:
//!
//! - `Ok(final_url)` on success
//! - `Err(Error::NoString)` when a redirect page/API response doesn’t contain a destination URL
//! - `Err(Error::...)` for network/HTTP/parse errors
//!
//! To make resolver modules ergonomic, it’s recommended that `Error` implements:
//!
//! - `From<reqwest::Error>`
//! - (optionally) `From<std::io::Error>` or other error conversions you use
//!
//! That lets resolvers freely use `?` or `.map_err(Error::from)`.
//!
//! ## Timeouts and redirect limits
//!
//! Timeouts are typically passed into each resolver (`Option<Duration>`) and applied via the shared
//! HTTP client builder. Redirect limits should also be configured in one place (your builder) so all
//! resolvers behave consistently.
//!
//! ## Adding a new resolver
//!
//! 1. Create `src/resolvers/<service>.rs`
//! 2. Implement:
//!
//! ```ignore
//! pub(crate) async fn unshort(url: &str, timeout: Option<std::time::Duration>) -> crate::Result<String> {
//! // resolve & return final URL
//! }
//! ```
//!
//! 3. Re-export it from `src/resolvers/mod.rs`
//! 4. Add it to your dispatcher/registry if you have one (e.g., “try resolvers in order”).
//!
//! ### Resolver style guideline
//!
//! Keep resolvers small and focused:
//!
//! - follow redirects first
//! - if the service stops on a non-redirect “intermediate page”, use either:
//! - regex extraction (`from_re`) or
//! - a small API call if the browser normally uses JS
//!
//! ## Testing
//!
//! For deterministic tests, consider:
//!
//! - unit testing regex extraction helpers (`from_re`) with fixed strings
//! - using a mock HTTP server (or recorded fixtures) for network calls
//! - keeping “live” integration tests behind a feature flag, since shortener behavior can change
//!
//! ## Security considerations
//!
//! Expanding URLs can lead to untrusted destinations. Consider optional safeguards:
//!
//! - maximum redirect depth
//! - domain allow/deny lists
//! - blocking private IP ranges (SSRF protection) if this runs server-side
//! - request method restrictions (typically GET only)
//! - size limits for downloaded bodies when parsing HTML
use Duration;
use ;
use ;
pub type Error = Error;
pub type Result<T> = Result;
use ;
pub async
pub async
async