Repository navigation
Expand file tree
/
Copy pathModuleResponse.cpp
More file actions
126 lines (114 loc) · 4.83 KB
/
Copy pathModuleResponse.cpp
File metadata and controls
126 lines (114 loc) · 4.83 KB
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
#include <cctype>
#include <string>
#include <utility>
#include "http/Internal.h"
#include "ns/Trace.h"
// Module scripts are strict about MIME: the HTML spec's "fetch a single module
// script" fails outright for anything that is not a JavaScript or JSON MIME
// type, where a classic script would sniff and run it anyway. An SPA dev server
// answering an unknown path with `200 text/html` must say so, not hand HTML to
// the parser and produce `Unexpected token '<'` from deep in the graph.
namespace nativescript::common::http {
std::string MimeEssence(const std::string& contentType) {
const size_t semi = contentType.find(';');
std::string essence =
semi == std::string::npos ? contentType : contentType.substr(0, semi);
const size_t begin = essence.find_first_not_of(" \t");
if (begin == std::string::npos) {
return "";
}
const size_t end = essence.find_last_not_of(" \t");
essence = essence.substr(begin, end - begin + 1);
for (char& c : essence) {
c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
return essence;
}
bool IsJavaScriptMimeEssence(const std::string& essence) {
static const char* const kJavaScriptEssences[] = {"application/ecmascript",
"application/javascript",
"application/x-ecmascript",
"application/x-javascript",
"text/ecmascript",
"text/javascript",
"text/javascript1.0",
"text/javascript1.1",
"text/javascript1.2",
"text/javascript1.3",
"text/javascript1.4",
"text/javascript1.5",
"text/jscript",
"text/livescript",
"text/x-ecmascript",
"text/x-javascript"};
for (const char* candidate : kJavaScriptEssences) {
if (essence == candidate) {
return true;
}
}
return false;
}
bool IsJsonMimeEssence(const std::string& essence) {
if (essence == "application/json" || essence == "text/json") {
return true;
}
const std::string suffix = "+json";
return essence.size() > suffix.size() &&
essence.compare(essence.size() - suffix.size(), suffix.size(),
suffix) == 0;
}
void ClassifyModuleResponse(const std::string& url, bool arrived, int status,
const std::string& contentType, std::string& body,
ModuleFetchResult& result) {
result.status = status;
result.contentType = contentType;
if (!arrived) {
result.failureReason = "HTTP import failed: " + url + " (network error)";
return;
}
if (status == 204 || status == 205) {
// "No content" carries no module, which the web treats as a network error
// for a module script rather than as an empty module.
result.failureReason = "HTTP import failed: " + url +
" (status=" + std::to_string(status) +
", no content)";
return;
}
if (status < 200 || status >= 300) {
result.failureReason = "HTTP import failed: " + url +
" (status=" + std::to_string(status) + ")";
return;
}
const std::string essence = MimeEssence(contentType);
if (essence.empty()) {
result.failureReason = "Expected a JavaScript module but '" + url +
"' responded with no MIME type";
return;
}
if (IsJsonMimeEssence(essence)) {
if (body.empty()) {
result.failureReason = "Expected a JSON module but '" + url +
"' responded with an empty body";
return;
}
result.kind = ModuleResponseKind::kJson;
} else if (IsJavaScriptMimeEssence(essence)) {
result.kind = ModuleResponseKind::kJavaScript;
// Type-only TypeScript modules transform to zero runtime code and dev
// servers serve them as empty 200s.
if (body.empty()) {
body = "export {};\n";
Tracef(platform::TraceCategory::kEsm,
"[http-loader] empty 2xx body for %s — serving canonical empty "
"module",
url.c_str());
}
} else {
result.failureReason = "Expected a JavaScript module but '" + url +
"' responded with MIME type '" + essence + "'";
return;
}
result.ok = true;
result.body = std::move(body);
}
} // namespace nativescript::common::http