NewRequest parses an http.Request into an imageproxy Request. Options and the remote image URL are specified in the request path, formatted as: {options}/{remote_url}. Options may be omitted, so a request path may simply contain /{remote_url}. The remote URL may be included in plain text without
(r *http.Request, baseURL *url.URL)
| 355 | // http://localhost/x/http%3A%2F%2Fexample.com%2Fimage.jpg |
| 356 | // http://localhost/100x200/aHR0cDovL2V4YW1wbGUuY29tL2ltYWdlLmpwZw |
| 357 | func NewRequest(r *http.Request, baseURL *url.URL) (*Request, error) { |
| 358 | var err error |
| 359 | req := &Request{Original: r} |
| 360 | var enc bool // whether the remote URL was base64 or URL encoded |
| 361 | |
| 362 | path := r.URL.EscapedPath()[1:] // strip leading slash |
| 363 | req.URL, enc, err = parseURL(path, baseURL) |
| 364 | if err != nil || !req.URL.IsAbs() { |
| 365 | // first segment should be options |
| 366 | parts := strings.SplitN(path, "/", 2) |
| 367 | if len(parts) != 2 { |
| 368 | return nil, URLError{"too few path segments", r.URL} |
| 369 | } |
| 370 | |
| 371 | var err error |
| 372 | req.URL, enc, err = parseURL(parts[1], baseURL) |
| 373 | if err != nil { |
| 374 | return nil, URLError{fmt.Sprintf("unable to parse remote URL: %v", err), r.URL} |
| 375 | } |
| 376 | |
| 377 | req.Options = ParseOptions(parts[0]) |
| 378 | } |
| 379 | |
| 380 | if baseURL != nil { |
| 381 | req.URL = baseURL.ResolveReference(req.URL) |
| 382 | } |
| 383 | |
| 384 | if !req.URL.IsAbs() { |
| 385 | return nil, URLError{"must provide absolute remote URL", r.URL} |
| 386 | } |
| 387 | |
| 388 | if req.URL.Scheme != "http" && req.URL.Scheme != "https" { |
| 389 | return nil, URLError{"remote URL must have http or https scheme", r.URL} |
| 390 | } |
| 391 | |
| 392 | if !enc { |
| 393 | // if the remote URL was not base64 or URL encoded, |
| 394 | // then the query string is part of the remote URL |
| 395 | req.URL.RawQuery = r.URL.RawQuery |
| 396 | } |
| 397 | return req, nil |
| 398 | } |
| 399 | |
| 400 | var reCleanedURL = regexp.MustCompile(`^(https?):/+([^/])`) |
| 401 | var reIsEncodedURL = regexp.MustCompile(`^(?i)https?%3A%2F`) |