File: DefaultFetcher.java

package info (click to toggle)
python-schema-salad 8.9.20251102115403-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,060 kB
  • sloc: python: 19,247; cpp: 2,631; cs: 1,869; java: 1,341; makefile: 187; xml: 184; sh: 103; javascript: 46
file content (48 lines) | stat: -rw-r--r-- 1,521 bytes parent folder | download | duplicates (4)
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
package ${package}.utils;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Scanner;

public class DefaultFetcher implements Fetcher {

  public String urlJoin(final String baseUrl, final String url) {
    if (url.startsWith("_:")) {
      return url;
    }

    final URI baseUri = Uris.toUri(baseUrl);
    final URI uri = Uris.toUri(url);
    if (baseUri.getScheme() != null
        && !baseUri.getScheme().equals("file")
        && "file".equals(uri.getScheme())) {
      throw new ValidationException(
          String.format(
              "Not resolving potential remote exploit %s from base %s".format(url, baseUrl)));
    }
    String result = baseUri.resolve(uri).toString();
    if (result.startsWith("file:")) {
      // Well this is gross - needed for http as well?
      result = "file://" + result.substring("file:".length());
    }
    return result;
  }

  public String fetchText(final String url) {
    final URI uri = Uris.toUri(url);
    final String scheme = uri.getScheme();
    if (Arrays.asList("http", "https", "file").contains(scheme)) {
      Scanner scanner;
      try {
        scanner = new Scanner(uri.toURL().openStream(), "UTF-8").useDelimiter("\\A");
      } catch (IOException e) {
        throw new ValidationException("Error fetching %s: %s.".format(url, e));
      }
      String result = scanner.next();
      scanner.close();
      return result;
    }
    throw new ValidationException("Unsupported scheme in URL: %s".format(url));
  }
}