Skip to content

Commit

Permalink
consolidate test-ip definition, added missing Access-Control-Allow-Or…
Browse files Browse the repository at this point in the history
…igin (#1741)

Co-authored-by: CaCO3 <caco@ruinelli.ch>
  • Loading branch information
caco3 and CaCO3 committed Jan 2, 2023
1 parent 485e55e commit 4d823b3
Show file tree
Hide file tree
Showing 27 changed files with 266 additions and 222 deletions.
14 changes: 8 additions & 6 deletions code/components/jomjol_fileserver_ota/server_file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ static esp_err_t http_resp_dir_html(httpd_req_t *req, const char *dirpath, const
return ESP_FAIL;
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

/* Send HTML file header */
httpd_resp_sendstr_chunk(req, "<!DOCTYPE html><html><body>");

Expand Down Expand Up @@ -344,12 +346,6 @@ static esp_err_t send_datafile(httpd_req_t *req, bool send_full_file)

ESP_LOGD(TAG, "uri: %s, filename: %s, filepath: %s", req->uri, filename, filepath);

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");


// Since the log file is still could open for writing, we need to close it first
LogFile.CloseLogFileAppendHandle();

fd = fopen(currentfilename.c_str(), "r");
if (!fd) {
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Failed to read existing file: " + std::string(filepath) +"!");
Expand Down Expand Up @@ -434,6 +430,8 @@ static esp_err_t send_logfile(httpd_req_t *req, bool send_full_file)

ESP_LOGD(TAG, "uri: %s, filename: %s, filepath: %s", req->uri, filename, filepath);

// Since the log file is still could open for writing, we need to close it first
LogFile.CloseLogFileAppendHandle();

fd = fopen(currentfilename.c_str(), "r");
if (!fd) {
Expand Down Expand Up @@ -614,6 +612,8 @@ static esp_err_t upload_post_handler(httpd_req_t *req)
FILE *fd = NULL;
struct stat file_stat;

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

/* Skip leading "/upload" from URI to get filename */
/* Note sizeof() counts NULL termination hence the -1 */
const char *filename = get_path_from_uri(filepath, ((struct file_server_data *)req->user_ctx)->base_path,
Expand Down Expand Up @@ -764,6 +764,8 @@ static esp_err_t delete_post_handler(httpd_req_t *req)
std::string directory;
std::string zw;

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
{
ESP_LOGD(TAG, "Query: %s", _query);
Expand Down
2 changes: 1 addition & 1 deletion code/components/jomjol_fileserver_ota/server_ota.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ esp_err_t handler_reboot(httpd_req_t *req)

LogFile.WriteToFile(ESP_LOG_DEBUG, TAG, "handler_reboot");
ESP_LOGI(TAG, "!!! System will restart within 5 sec!!!");
const char* resp_str = "<body style='font-family: arial'> <h3 id=t></h3></body><script>var h='Rebooting!<br>The page will automatically reload in around 25..60s<br>(in case of a firmware update it can take up to 180s).<br>'; document.getElementById('t').innerHTML=h; setInterval(function (){h +='.'; document.getElementById('t').innerHTML=h; fetch('/reboot_page.html',{mode: 'no-cors'}).then(r=>{parent.location.href=('/index.html');})}, 1000);</script>";
const char* resp_str = "<body style='font-family: arial'> <h3 id=t></h3></body><script>var h='Rebooting!<br>The page will automatically reload in around 25..60s<br>(in case of a firmware update it can take up to 180s).<br>'; document.getElementById('t').innerHTML=h; setInterval(function (){h +='.'; document.getElementById('t').innerHTML=h; fetch('reboot_page.html',{mode: 'no-cors'}).then(r=>{parent.location.href=('index.html');})}, 1000);</script>";
httpd_resp_send(req, resp_str, strlen(resp_str));

doReboot();
Expand Down
5 changes: 5 additions & 0 deletions code/components/jomjol_tfliteclass/server_tflite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ esp_err_t handler_init(httpd_req_t *req)
doInit();

resp_str = "Init done<br>";
httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");
httpd_resp_send(req, resp_str, strlen(resp_str));
/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);
Expand Down Expand Up @@ -179,6 +180,8 @@ esp_err_t handler_flow_start(httpd_req_t *req) {
httpd_resp_send(req, resp_str, strlen(resp_str));
}

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

/* Respond with an empty chunk to signal HTTP response completion */
httpd_resp_send_chunk(req, NULL, 0);

Expand Down Expand Up @@ -421,6 +424,8 @@ esp_err_t handler_editflow(httpd_req_t *req)
char _valuechar[30];
string _task;

httpd_resp_set_hdr(req, "Access-Control-Allow-Origin", "*");

if (httpd_req_get_url_query_str(req, _query, 200) == ESP_OK)
{
if (httpd_query_key_value(_query, "task", _valuechar, 30) == ESP_OK)
Expand Down
10 changes: 4 additions & 6 deletions sd-card/html/backup.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,18 @@ <h2>Restore Configuration</h2>
</body>


<script src="common.js"></script>
<script src="jszip.min.js"></script>
<script src="FileSaver.min.js"></script>
<script>

//var domain = "http://192.168.1.153"; // Testing
var domain = "";

function startBackup() {
document.getElementById("progress").innerHTML = "Creating backup...<br>\n";

// Get hostname
try {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", domain + "/info?type=Hostname", false);
xhttp.open("GET", getDomainname() + "/info?type=Hostname", false);
xhttp.send();
hostname = xhttp.responseText;
}
Expand All @@ -72,7 +70,7 @@ <h2>Restore Configuration</h2>
setStatus("Fetching File List...");
try {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", domain + "/fileserver/config/", false);
xhttp.open("GET", getDomainname() + "/fileserver/config/", false);
xhttp.send();

var parser = new DOMParser();
Expand All @@ -88,7 +86,7 @@ <h2>Restore Configuration</h2>

for (a of list) {
url = a.getAttribute("href");
urls.push(domain + url);
urls.push(getDomainname() + url);
}

// Pack as zip and download
Expand Down
59 changes: 52 additions & 7 deletions sd-card/html/common.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,52 @@

var basepath = "http://192.168.178.22";
/* The UI can also be run locally, but you have to set the IP of your devide accordingly.
* And you also might have to disable CORS in your webbrowser! */
var domainname_for_testing = "192.168.1.153";



function gethost_Version(){
return "1.0.0 - 20200910";
}


/* Returns the domainname with prepended protocol.
Eg. http://watermeter.fritz.box or http://192.168.1.5 */
function getDomainname(){
var host = window.location.hostname;
if (((host == "127.0.0.1") || (host == "localhost") || (host == ""))
// && ((window.location.port == "80") || (window.location.port == ""))
)

{
console.log("Using pre-defined domainname for testing: " + domainname_for_testing);
domainname = "http://" + domainname_for_testing
}
else
{
domainname = window.location.protocol + "//" + host;
if (window.location.port != "") {
domainname = domainname + ":" + window.location.port;
}
}

return domainname;
}

function UpdatePage(_dosession = true){
var zw = location.href;
zw = zw.substr(0, zw.indexOf("?"));
if (_dosession) {
window.location = zw + '?session=' + Math.floor((Math.random() * 1000000) + 1);
}
else {
window.location = zw;
}
}


function LoadHostname() {
_basepath = getbasepath();
_domainname = getDomainname();


var xhttp = new XMLHttpRequest();
Expand All @@ -19,7 +63,7 @@ function LoadHostname() {

// var xhttp = new XMLHttpRequest();
try {
url = _basepath + '/info?type=Hostname';
url = _domainname + '/info?type=Hostname';
xhttp.open("GET", url, true);
xhttp.send();

Expand All @@ -35,7 +79,7 @@ var fwVersion = "";
var webUiVersion = "";

function LoadFwVersion() {
_basepath = getbasepath();
_domainname = getDomainname();

var xhttp = new XMLHttpRequest();
xhttp.addEventListener('load', function(event) {
Expand All @@ -52,7 +96,7 @@ function LoadFwVersion() {
});

try {
url = _basepath + '/info?type=FirmwareVersion';
url = _domainname + '/info?type=FirmwareVersion';
xhttp.open("GET", url, true);
xhttp.send();
}
Expand All @@ -62,7 +106,7 @@ function LoadFwVersion() {
}

function LoadWebUiVersion() {
_basepath = getbasepath();
_domainname = getDomainname();

var xhttp = new XMLHttpRequest();
xhttp.addEventListener('load', function(event) {
Expand All @@ -78,7 +122,8 @@ function LoadWebUiVersion() {
});

try {
url = _basepath + '/info?type=HTMLVersion';
url = _domainname + '/info?type=HTMLVersion';
console.log("url");
xhttp.open("GET", url, true);
xhttp.send();
}
Expand Down
17 changes: 9 additions & 8 deletions sd-card/html/data.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@
font-size: small;
}
</style>
<script type="text/javascript" src="common.js"></script>
</head>
<body>
<div class="box">
<div class="row header">
<button onClick="reload();">Reload</button>
<button onClick="window.open('datafileact');">Show full data</button>
<button onClick="window.location.href = 'fileserver/log/data/'">Show older data files</button>
<a href="graph.html" target="_self">Show graph</a>
<button onClick="window.open(getDomainname() + '/datafileact');">Show full data</button>
<button onClick="window.location.href = getDomainname() + '/fileserver/log/data/'">Show older data files</button>
<button onClick="window.location.href = 'graph.html'">Show graph</button>
</div>
<div class="row content" id="data"><br><br><br><b>Loading Data file, please wait...</b></div>
<div class="row footer">
<button onClick="reload();">Reload</button>
<button onClick="window.open('datafileact');">Show full data</button>
<button onClick="window.location.href = 'fileserver/log/data/'">Show older data files</button>
<a href="graph.html" target="_self">Show graph</a>
<button onClick="window.open(getDomainname() + '/datafileact');">Show full data</button>
<button onClick="window.location.href = getDomainname() + '/fileserver/log/data/'">Show older data files</button>
<button onClick="window.location.href = 'graph.html'">Show graph</button>
</div>
</div>
</body>
Expand All @@ -53,7 +54,7 @@
function reload() {
document.getElementById('data').innerHTML += "<br><b>Reloading...<b><br><br>";
window.scrollBy(0,document.body.scrollHeight);
funcRequest('data');
funcRequest(getDomainname() + '/data');
}

async function funcRequest(url){
Expand All @@ -76,7 +77,7 @@
});
}

funcRequest('data');
funcRequest(getDomainname() + '/data');

</script>
</html>
31 changes: 15 additions & 16 deletions sd-card/html/edit_alignment.html
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ <h2>Define Alignment Structure in Reference Image</h2>
</tr>
</table>

<script type="text/javascript" src="./gethost.js"></script>
<script type="text/javascript" src="./readconfigcommon.js"></script>
<script type="text/javascript" src="./readconfigparam.js"></script>
<script type="text/javascript" src="common.js"></script>
<script type="text/javascript" src="readconfigcommon.js"></script>
<script type="text/javascript" src="readconfigparam.js"></script>

<script language="JavaScript">
var canvas = document.getElementById('canvas'),
Expand All @@ -107,13 +107,12 @@ <h2>Define Alignment Structure in Reference Image</h2>
refInfo,
enhanceCon = false,
param;
basepath = "http://192.168.178.26";
basepath = "",
domainname = getDomainname();
param;

function doReboot() {
if (confirm("Are you sure you want to reboot? Did you save the config?")) {
var stringota = "/reboot";
var stringota = getDomainname() + "/reboot";
window.location = stringota;
window.location.href = stringota;
window.location.assign(stringota);
Expand All @@ -128,8 +127,8 @@ <h2>Define Alignment Structure in Reference Image</h2>

function SaveToConfig(){
WriteConfigININew();
UpdateConfigReference(basepath)
SaveConfigToServer(basepath);
UpdateConfigReference(domainname)
SaveConfigToServer(domainname);
alert("Config.ini is updated!");
}

Expand All @@ -141,7 +140,7 @@ <h2>Define Alignment Structure in Reference Image</h2>
refInfo[aktindex]["dy"] = document.getElementById("refdy").value;

enhanceCon = true;
MakeContrastImageZW(refInfo[aktindex], enhanceCon, basepath);
MakeContrastImageZW(refInfo[aktindex], enhanceCon, domainname);
UpdateReference();
}

Expand All @@ -158,11 +157,11 @@ <h2>Define Alignment Structure in Reference Image</h2>

_filenameurl = refInfo[aktindex]["name"].replace("/config/", "/img_tmp/");

var url = basepath + "/fileserver" + _filenameurl + "?" + Date.now();
var url = domainname + "/fileserver" + _filenameurl + "?" + Date.now();
document.getElementById("img_ref").src = url;

_filenameurl = _filenameurl.replace(".jpg", "_org.jpg");
var url = basepath + "/fileserver" + _filenameurl + "?" + Date.now();
var url = domainname + "/fileserver" + _filenameurl + "?" + Date.now();
document.getElementById("img_ref_org").src = url;

document.getElementById("name").value = refInfo[aktindex]["name"];
Expand Down Expand Up @@ -214,17 +213,17 @@ <h2>Define Alignment Structure in Reference Image</h2>


function init() {
basepath = getbasepath();
loadConfig(basepath);
domainname = getDomainname();
loadConfig(domainname);
ParseConfig();
param = getConfigParameters();

canvas.addEventListener('mousedown', mouseDown, false);
canvas.addEventListener('mouseup', mouseUp, false);
canvas.addEventListener('mousemove', mouseMove, false);
loadCanvas(basepath + "/fileserver/config/reference.jpg");
loadCanvas(domainname + "/fileserver/config/reference.jpg");

CopyReferenceToImgTmp(basepath);
CopyReferenceToImgTmp(domainname);
refInfo = GetReferencesInfo();

UpdateReference();
Expand All @@ -247,7 +246,7 @@ <h2>Define Alignment Structure in Reference Image</h2>
refInfo[aktindex]["y"] = document.getElementById("refy").value;
refInfo[aktindex]["dx"] = document.getElementById("refdx").value;
refInfo[aktindex]["dy"] = document.getElementById("refdy").value;
MakeRefZW(refInfo[aktindex], basepath);
MakeRefZW(refInfo[aktindex], domainname);
UpdateReference();
document.getElementById("enhancecontrast").disabled = false;
}
Expand Down

0 comments on commit 4d823b3

Please sign in to comment.