MCPcopy Create free account
hub / github.com/Redot-Engine/redot-engine / _send_response

Method _send_response

platform/web/export/editor_http_server.cpp:75–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

73}
74
75void EditorHTTPServer::_send_response() {
76 Vector<String> psa = String((char *)req_buf).split("\r\n");
77 int len = psa.size();
78 ERR_FAIL_COND_MSG(len < 4, "Not enough response headers, got: " + itos(len) + ", expected >= 4.");
79
80 Vector<String> req = psa[0].split(" ", false);
81 ERR_FAIL_COND_MSG(req.size() < 2, "Invalid protocol or status code.");
82
83 // Wrong protocol
84 ERR_FAIL_COND_MSG(req[0] != "GET" || req[2] != "HTTP/1.1", "Invalid method or HTTP version.");
85
86 const int query_index = req[1].find_char('?');
87 const String path = (query_index == -1) ? req[1] : req[1].substr(0, query_index);
88
89 const String req_file = path.get_file();
90 const String req_ext = path.get_extension();
91 const String cache_path = EditorPaths::get_singleton()->get_temp_dir().path_join("web");
92 const String filepath = cache_path.path_join(req_file);
93
94 if (!mimes.has(req_ext) || !FileAccess::exists(filepath)) {
95 String s = "HTTP/1.1 404 Not Found\r\n";
96 s += "Connection: Close\r\n";
97 s += "\r\n";
98 CharString cs = s.utf8();
99 peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
100 return;
101 }
102 const String ctype = mimes[req_ext];
103
104 Ref<FileAccess> f = FileAccess::open(filepath, FileAccess::READ);
105 ERR_FAIL_COND(f.is_null());
106 String s = "HTTP/1.1 200 OK\r\n";
107 s += "Connection: Close\r\n";
108 s += "Content-Type: " + ctype + "\r\n";
109 s += "Access-Control-Allow-Origin: *\r\n";
110 s += "Cross-Origin-Opener-Policy: same-origin\r\n";
111 s += "Cross-Origin-Embedder-Policy: require-corp\r\n";
112 s += "Cache-Control: no-store, max-age=0\r\n";
113 s += "\r\n";
114 CharString cs = s.utf8();
115 Error err = peer->put_data((const uint8_t *)cs.get_data(), cs.size() - 1);
116 if (err != OK) {
117 ERR_FAIL();
118 }
119
120 while (true) {
121 uint8_t bytes[4096];
122 uint64_t read = f->get_buffer(bytes, 4096);
123 if (read == 0) {
124 break;
125 }
126 err = peer->put_data(bytes, read);
127 if (err != OK) {
128 ERR_FAIL();
129 }
130 }
131}
132

Callers

nothing calls this directly

Calls 15

itosFunction · 0.85
splitMethod · 0.80
find_charMethod · 0.80
substrMethod · 0.80
path_joinMethod · 0.80
sizeMethod · 0.65
StringClass · 0.50
get_fileMethod · 0.45
get_extensionMethod · 0.45
get_temp_dirMethod · 0.45
hasMethod · 0.45
utf8Method · 0.45

Tested by

no test coverage detected