aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/LandingHeader.svelte
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2022-08-22 10:49:04 -0700
committerAdam Soltys <[email protected]>2022-08-22 15:55:42 -0700
commit5f1014b41fab3ecb67e033e270e19060ad2af88f (patch)
tree7cc1d8938231ad6a1366b7d08dadbb24dcfc5299 /src/components/LandingHeader.svelte
parent0ebb60f986682ad8d3538c2fd1d708a861e0b1fb (diff)
downloadcoinos-ui-5f1014b41fab3ecb67e033e270e19060ad2af88f.tar.gz
coinos-ui-5f1014b41fab3ecb67e033e270e19060ad2af88f.zip
get user from data prop
Diffstat (limited to 'src/components/LandingHeader.svelte')
0 files changed, 0 insertions, 0 deletions
alt'>
1bd3e26 ^

e1bc349 ^

1bd3e26 ^




e1bc349 ^
1bd3e26 ^

e1bc349 ^
1bd3e26 ^










e1bc349 ^





1bd3e26 ^



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







                                          
                                    















                                                                                



                                                                           




                                            
                                                                                                          



















                                                                              
                                                            

                                                   

                                                                                                       




                                                                     
                                                                                          

                         
                                       










                                                                                                                                                              





                                                                           



                                                                       
package io.coinos.app;

import android.content.Context;
import android.content.res.AssetManager;
import android.net.Uri;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * The bundled web app, served to the WebView from this APK's assets.
 *
 * <p>The page still lives at https://v3.coinos.io/ as far as the WebView is
 * concerned — same origin, same storage, same links — but every file the
 * page asks for under that origin is answered from this APK's assets, the build
 * that shipped in this APK. Only the site's live endpoints (its Electrum
 * and explorer proxies, LNURL, .well-known) go out to the network, exactly
 * the paths the site's own nginx and service worker treat as live. A path
 * with no file behind it and no extension is a deep link (/g/…, /invite/…,
 * /npub…): it gets index.html, as the site's SPA fallback would.
 *
 * <p>Nothing else under the origin reaches the network: a file the APK
 * doesn't carry (coinos.apk, say) is a 404 here, not a fetch from the live
 * site, so no code this build didn't ship can run in the app's origin.
 */
final class Assets {
  private Assets() {}

  static final String HOST = "v3.coinos.io";
  private static final String[] LIVE = { "/electrum", "/esplora/", "/lnurlp/", "/.well-known/", "/api/" };
  private static final Map<String, String> MIME = new HashMap<>();
  static {
    MIME.put("html", "text/html");
    MIME.put("js", "text/javascript");
    MIME.put("mjs", "text/javascript");
    MIME.put("css", "text/css");
    MIME.put("json", "application/json");
    MIME.put("webmanifest", "application/manifest+json");
    MIME.put("png", "image/png");
    MIME.put("webp", "image/webp");
    MIME.put("svg", "image/svg+xml");
    MIME.put("ico", "image/x-icon");
    MIME.put("woff2", "font/woff2");
    MIME.put("wasm", "application/wasm");
    MIME.put("txt", "text/plain");
  }

  /** The asset answering this request, or null to let the network have it. */
  static WebResourceResponse serve(Context ctx, WebResourceRequest req) {
    Uri u = req.getUrl();
    if (u == null || !HOST.equals(u.getHost())) return null;
    String path = u.getPath();
    if (path == null || path.isEmpty()) path = "/";
    if ("https".equals(u.getScheme())) for (String live : LIVE) if (path.startsWith(live)) return null;
    if (!"GET".equalsIgnoreCase(req.getMethod()) || !"https".equals(u.getScheme())) return missing();
    String rel = path.equals("/") ? "index.html" : path.substring(1);
    AssetManager am = ctx.getAssets();
    InputStream in = open(am, rel);
    if (in == null) {
      String last = rel.substring(rel.lastIndexOf('/') + 1);
      if (last.contains(".")) return missing(); // a real file we don't carry (coinos.apk)
      rel = "index.html";
      in = open(am, rel);
      if (in == null) return missing();
    }
    String ext = rel.substring(rel.lastIndexOf('.') + 1).toLowerCase();
    String mime = MIME.containsKey(ext) ? MIME.get(ext) : "application/octet-stream";
    boolean text = mime.startsWith("text/") || mime.startsWith("application/json") || mime.startsWith("application/manifest") || mime.equals("image/svg+xml");
    WebResourceResponse res = new WebResourceResponse(mime, text ? "utf-8" : null, in);
    Map<String, String> headers = new HashMap<>();
    headers.put("Cache-Control", "no-store");
    res.setResponseHeaders(headers);
    return res;
  }

  /** Fail closed: the origin's files are the APK's or nobody's. */
  private static WebResourceResponse missing() {
    return new WebResourceResponse("text/plain", "utf-8", 404, "Not Found",
        new HashMap<>(), new ByteArrayInputStream(new byte[0]));
  }

  private static InputStream open(AssetManager am, String rel) {
    try { return am.open(rel); } catch (IOException e) { return null; }
  }
}