[GELÖST] HttpUtils_NonblockingGet und Fileupload

Begonnen von CoolTux, 15 Juni 2020, 11:04:24

Vorheriges Thema - Nächstes Thema

CoolTux

LÖSUNG



Hallo,

Ich möchte gerne mittels HttpUtils_NonblockingGet ein Fileupload machen.
Ein Beispiel für cURL sieht so aus

curl -u username:password -T FHEM-20200615_092411.tar.gz "https://cloud.cooltux.net/remote.php/dav/files/username/Backup/FHEM-20200615_092411.tar.gz"


-u --user <user:password> Server user and password
-T --upload-file <file> Transfer local FILE to destination

Kann ich mein Vorhaben mit HttpUtils_NonblockingGet umsetzten oder sollte ich lieber in einem nonBlocking des cURL Befehl nehmen?



Grüße
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

rudolfkoenig

Ja, das sollte gehen.

Wenn Du mit "nonBlocking" BlockingCall meinst: das fuehrt halt fork() aus, mit Gefahr von "Cannot fork: Not enough memory", und braucht ein curl installiert.
HttpUtils_NonblockingGet ist auch nicht "unschuldig": die Daten muessen komplett als String im Hauptspeicher vorhanden sein, und sie werden in der Routine oefters (2-3-mal?) kopiert.

CoolTux

Zitat von: rudolfkoenig am 15 Juni 2020, 11:24:07
Ja, das sollte gehen.

Wenn Du mit "nonBlocking" BlockingCall meinst: das fuehrt halt fork() aus, mit Gefahr von "Cannot fork: Not enough memory", und braucht ein curl installiert.
HttpUtils_NonblockingGet ist auch nicht "unschuldig": die Daten muessen komplett als String im Hauptspeicher vorhanden sein, und sie werden in der Routine oefters (2-3-mal?) kopiert.

Hattest Du ein Beispiel für $param für die Funktion HttpUtils_NonblockingGet
Mir geht es da gerade um -u und -T

my $param = (
     url       => 'https://cloud.cooltux.net/remote.php/dav/files/username/Backup/FHEM-20200615_092411.tar.gz',
     timeout   => 30,
     method    => 'PUT',
     ?????
     ?????
     .....
);
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

rudolfkoenig

Bin nicht sicher, ob ich alle Auswirkungen von -u und -T ueberblicke.

Benutzer/Passwort ist im URL als http://Benutzer:Passwort@hostname/pfad zu uebergeben (URL-Kodiert), oder als user/pwd im Hash.
Zu schreibende Daten spezifiziert man per $hash->{data}. Welche HTTP Methode verwendet muss, haengt vom Webserver ab, mit Daten ist PUT, ohne Daten ist GET die Voreinstellung.

Eine Auflistung aller ausgewerteten Hash-Eintraege findet man im Modul:
# Parameters in the hash:
#  mandatory:
#    url, callback
#  optional(default):
#    digest(0),hideurl(0),timeout(4),data(""),loglevel(4),header("" or HASH),
#    noshutdown(1),shutdown(0),httpversion("1.0"),ignoreredirects(0)
#    method($data?"POST":"GET"),keepalive(0),sslargs({}),user(),pwd()
#    compress(1), incrementalTimeout(0)

CoolTux

Na dann versuche ich mal mein Glück. Aktuell denke ich es geht nicht. Werde aber mein bestes geben  ;D
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

rudolfkoenig

Bitte um "nicht-geht" Nachweis per FHEM-Log Auszug nach attr global verbose 5.

CoolTux

Ich habe es wie folgt geschafft eine leere Datei auf dem Nextcloudserver zu erstellen

my $param = {
url       => 'https://cloud.cooltux.net/remote.php/dav/files/user/FHEM-Backup/FHEM-20200609_140250.tar.gz',
            timeout   => 30,
            method    => 'PUT',
            data      => '/opt/fhem/backup/FHEM-20200609_140250.tar.gz',
    user   => 'user',
    pwd   => 'password',
            callback  => \&NCerrorHandling,
};


Er legt also den Path https://cloud.cooltux.net/remote.php/dav/files/marko/FHEM-Backup/FHEM-20200609_Marko_140250.tar.gz an, aber wie sage ich ihm das er eine Quelldatei nehmen soll. data => ist ja eher ein String den man senden kann. Ich will eine ganze Datei senden.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

CoolTux

Global verbose 5



2020.06.15 15:07:51.981 5: POST /fhem HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
Content-Length: 75
Cache-Control: max-age=0
Origin: http://t-fhem01.tuxnet.lan:8083
Upgrade-Insecure-Requests: 1
DNT: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:51.982 4: WEB_10.6.9.3_45726 POST /fhem&fw_id=245&room=System&fwcsrf=csrf_613174510879661&cmd=%7B+markoNC%28%29+%7D; BUFLEN:0
2020.06.15 15:07:51.982 5: Cmd: >{ markoNC() }<
2020.06.15 15:07:51.982 5: HttpUtils url=https://cloud.cooltux.net/remote.php/dav/files/marko/FHEM-Backup/FHEM-20200609_Marko_140250.tar.gz
2020.06.15 15:07:51.983 4: IP: cloud.cooltux.net -> 192.168.240.241
2020.06.15 15:07:51.995 5: HttpUtils request header:
PUT /remote.php/dav/files/marko/FHEM-Backup/FHEM-20200609_Marko_140250.tar.gz HTTP/1.0
Host: cloud.cooltux.net
User-Agent: fhem
Accept-Encoding: gzip,deflate
Authorization: Basic bWFya286YXh4VG0tcE1TZ1ItR2tlM04ta3E5YkMtSmZUUzM=
Content-Length: 44
Content-Type: application/x-www-form-urlencoded

2020.06.15 15:07:52.008 5: GET /fhem?room=System&fw_id=245 HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
DNT: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.008 4: WEB_10.6.9.3_45726 GET /fhem?room=System&fw_id=245; BUFLEN:0
2020.06.15 15:07:52.063 4: WEB: /fhem?room=System&fw_id=245 / RL:5840 / text/html; charset=UTF-8 / Content-Encoding: gzip
/ Cache-Control: no-cache, no-store, must-revalidate

2020.06.15 15:07:52.082 5: GET /fhem/pgm2/jquery.min.js HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933951"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: */*
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.082 4: WEB_10.6.9.3_45726 GET /fhem/pgm2/jquery.min.js; BUFLEN:0
2020.06.15 15:07:52.082 4: WEB_10.6.9.3_45726 => 304 Not Modified
2020.06.15 15:07:52.083 5: GET /fhem/pgm2/doif.js HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933951"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: */*
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.083 4: WEB_10.6.9.3_45732 GET /fhem/pgm2/doif.js; BUFLEN:0
2020.06.15 15:07:52.083 4: WEB_10.6.9.3_45732 => 304 Not Modified
2020.06.15 15:07:52.083 5: GET /fhem/codemirror/fhem_codemirror.js HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933951"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: */*
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.083 4: WEB_10.6.9.3_45736 GET /fhem/codemirror/fhem_codemirror.js; BUFLEN:0
2020.06.15 15:07:52.084 4: WEB_10.6.9.3_45736 => 304 Not Modified
2020.06.15 15:07:52.084 5: GET /fhem/pgm2/fhemweb.js HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1591181989"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: */*
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.084 4: WEB_10.6.9.3_45734 GET /fhem/pgm2/fhemweb.js; BUFLEN:0
2020.06.15 15:07:52.084 4: WEB_10.6.9.3_45734 => 304 Not Modified
2020.06.15 15:07:52.084 5: GET /fhem/pgm2/jquery-ui.min.js HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933951"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: */*
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.084 4: WEB_10.6.9.3_45730 GET /fhem/pgm2/jquery-ui.min.js; BUFLEN:0
2020.06.15 15:07:52.084 4: WEB_10.6.9.3_45730 => 304 Not Modified
2020.06.15 15:07:52.085 5: GET /fhem/images/default/icoEverything.png HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933953"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.085 4: WEB_10.6.9.3_45728 GET /fhem/images/default/icoEverything.png; BUFLEN:0
2020.06.15 15:07:52.085 4: WEB_10.6.9.3_45728 => 304 Not Modified
2020.06.15 15:07:52.089 5: GET /fhem/pgm2/darkCommon.css HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1580714083"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: text/css,*/*;q=0.1
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.089 4: WEB_10.6.9.3_45732 GET /fhem/pgm2/darkCommon.css; BUFLEN:0
2020.06.15 15:07:52.089 4: WEB_10.6.9.3_45732 => 304 Not Modified
2020.06.15 15:07:52.098 5: GET /fhem/pgm2/dashboard_darkstyle.css HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933951"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: text/css,*/*;q=0.1
Referer: http://t-fhem01.tuxnet.lan:8083/fhem?room=System&fw_id=245
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.098 4: WEB_10.6.9.3_45732 GET /fhem/pgm2/dashboard_darkstyle.css; BUFLEN:0
2020.06.15 15:07:52.098 4: WEB_10.6.9.3_45732 => 304 Not Modified
2020.06.15 15:07:52.121 4: https://cloud.cooltux.net/remote.php/dav/files/marko/FHEM-Backup/FHEM-20200609_Marko_140250.tar.gz: HTTP response code 204
2020.06.15 15:07:52.121 5: HttpUtils https://cloud.cooltux.net/remote.php/dav/files/marko/FHEM-Backup/FHEM-20200609_Marko_140250.tar.gz: Got data, length: 0
2020.06.15 15:07:52.122 5: HttpUtils response header:
HTTP/1.0 204 No Content
date: Mon, 15 Jun 2020 13:07:51 GMT
server: Apache
referrer-policy: no-referrer
x-content-type-options: nosniff
x-download-options: noopen
x-frame-options: SAMEORIGIN
x-permitted-cross-domain-policies: none
x-robots-tag: none
x-xss-protection: 1; mode=block
set-cookie: 0d6b13b01c9d6=6pb3f16g4c4pv8b1hdbaqlnvu0; path=/; secure; HttpOnly
expires: Thu, 19 Nov 1981 08:52:00 GMT
cache-control: no-store, no-cache, must-revalidate
pragma: no-cache
set-cookie: oc_sessionPassphrase=meXPTxLhTf1RtF2m5np0J%2FLI6OR8WmJr4y%2F8yULvTlKkHAfucNRqGSST%2BGncAOhvWpjNhNgSsDA%2Fw4Efp7knocDU%2BZRlbegy3c7TisgAXLLoBhy4kfcVoTsw34oT9XKo; path=/; secure; HttpOnly
content-security-policy: default-src 'none';
set-cookie: __Host-nc_sameSiteCookielax=true; path=/; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=lax
set-cookie: __Host-nc_sameSiteCookiestrict=true; path=/; httponly;secure; expires=Fri, 31-Dec-2100 23:59:59 GMT; SameSite=strict
set-cookie: 0d6b13b01c9d6=2hlqnlj041t2bhsvfqddpipfsq; path=/; secure; HttpOnly
oc-fileid: 001301900d6b13b01c9d6
etag: "6db89b0d683b897049c315be1782d946"
oc-etag: "6db89b0d683b897049c315be1782d946"
strict-transport-security: max-age=15768000; preload
NC - Error:
NC - Data:
2020.06.15 15:07:52.131 5: GET /fhem/images/default/fhemicon_dark.png HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: keep-alive
DNT: 1
If-None-Match: "1550933953"
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Accept: image/webp,image/apng,image/*,*/*;q=0.8
Referer: http://t-fhem01.tuxnet.lan:8083/fhem/pgm2/style.css?v=1592183815
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
2020.06.15 15:07:52.131 4: WEB_10.6.9.3_45732 GET /fhem/images/default/fhemicon_dark.png; BUFLEN:0
2020.06.15 15:07:52.131 4: WEB_10.6.9.3_45732 => 304 Not Modified
2020.06.15 15:07:52.242 4: Connection accepted from WEB_10.6.9.3_45750
2020.06.15 15:07:52.243 5: GET /fhem?XHR=1&inform=type=status;filter=room=System;since=1592226471;fmt=JSON&fw_id=245&timestamp=1592226472230 HTTP/1.1
Host: t-fhem01.tuxnet.lan:8083
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.162 Safari/537.36
Upgrade: websocket
Origin: http://t-fhem01.tuxnet.lan:8083
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate
Accept-Language: de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7
Sec-WebSocket-Key: 5mFwOSDxdThf9n8lVGwJ6w==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

rudolfkoenig

$hash->{data} ist nicht "Name der Datei, deren Inhalt ich senden will", sondern "Daten, die ich senden will".

CoolTux

Ja das habe ich auch gerade festgestellt  ;D Hab mal ne Textdatei angelegt und Text in $hash->{data} geschrieben.
Frage ist halt nur wie sende ich jetzt eine ganze Datei hoch.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

CoolTux

Ich denke ein read und write eines binary files könnte hier helfen. Was denkst Du
open my $in, '<', $infile or die;
binmode $in;


Ich befürchte  nur das dies FHEM zu lange blockiert und dann kann ich auch gleich auf BlockingCall mit cURL gehen.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

CoolTux

#11
LÖSUNG:


sub uploadNC {

open my $in, '<', '/opt/fhem/backup/FHEM-20200609_140250.tar.gz'
            or return Log(1,'can\'t open file');

binmode $in;    # ist (nur) fuer Windows relevant

my $cont = '';

while (1) {
        my $success = read $in, $cont, 100, length($cont);
        return $! if not defined $success;
        last if not $success;
}

close $in;

my $param = {
url       => 'https://cloud.cooltux.net/remote.php/dav/files/user/FHEM-Backup/FHEM-20200609_140250.tar.gz',
            timeout     => 30,
            method      => 'PUT',
            data        => $cont,
    user => 'user',
    pwd => 'password',
            callback    => \&NCerrorHandling,
};

HttpUtils_NonblockingGet($param);
}

sub NCerrorHandling {
my $param = shift;
my $err = shift;
my $data = shift;

print 'NC - Error: ' . $err . "\n";
print 'NC - Data: ' . $data . "\n";
}


Bei 50MB hat FHEM nicht spürbar blockiert.
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net

rudolfkoenig

Wobei:
- die() ist in FHEM (um es hoeflich zu sagen) stark verpoent
- die Daten kann man auch ohne explizite Schleife mit "my $data = join("", <$in>);" reinlesen. Diese Variante hat aber den doppelten Speicherbedarf gegenueber deine.
- binmode ist (nur) fuer Windows relevant.

CoolTux

Hallo Rudi,

Vielen Dank für Deine Anmerkungen. Ich werde das ganze für andere entsprechend an passen.


Grüße
Marko
Du musst nicht wissen wie es geht! Du musst nur wissen wo es steht, wie es geht.
Support me to buy new test hardware for development: https://www.paypal.com/paypalme/MOldenburg
My FHEM Git: https://git.cooltux.net/FHEM/
Das TuxNet Wiki:
https://www.cooltux.net