From 7767b80e60c11051fdb858c0004b5a7ae5c8e58f Mon Sep 17 00:00:00 2001 From: Sascha Schulz <sschulz@dh-software.de> Date: Mo, 14 Okt 2024 15:29:23 +0200 Subject: [PATCH] add chapter about http with tcp and http server --- index.html | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 255 insertions(+), 0 deletions(-) diff --git a/index.html b/index.html index 31beec7..2a0d2d1 100644 --- a/index.html +++ b/index.html @@ -4163,6 +4163,261 @@ <p>Aufgabe</p> <p>Implementiere zur vorherigen Verschlüsselung eine Entschlüsselung.</p> </section> + <section> + <p>Lösung</p> + <pre> + <code class="js" data-trim data-line-numbers> + const word = Buffer.from("abcdef"); + const key = Buffer.from("xyz"); + const encrypted = Buffer.alloc(word.length, 0); + + // encrypt + for (let i = 0; i < word.length; i++) { + encrypted[i] = word[i] ^ key[i % key.length]; + } + + // decrypt + for (let i = 0; i < word.length; i++) { + word[i] = encrypted[i] ^ key[i % key.length]; + } + + console.log(word.toString()); // => "abcdef" + </code> + </pre> + </section> + <section> + <h3>Streams</h3> + <p>Konstanter Fluss von (meist) großen Datenmengen in kleinen Paketen</p> + </section> + <section> + <p>Gängige Typen von Streams</p> + <ul> + <li>Standard In / Out</li> + <li>HTTP-Requests / -Responses</li> + <li>TCP-Sockets</li> + <li>Lese- und Schreib-Streams auf Dateien</li> + <li>Große Datenbank-Ergebnisse mittels Cursor</li> + </ul> + </section> + <section> + <pre> + <code class="js" data-trim data-line-numbers> + const readableStream = fs.createReadStream("./source.txt"); + const writableStream = fs.createWriteStream("./target.txt"); + + readableStream.on("data", (data /* Buffer */) => { + // process data here + writableStream.write(data) + }); + + readableStream.on("end", (data) => { + // stream has finished, e.g. EOF (end of file) + }); + + // alternatively pipe content + readableStream.pipe(writableStream); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Lese eine beliebige Datei als Readable Stream, verschlüssele den Inhalt mit der XOR-Methode und schreibe die verschlüsselten Bytes in eine Zieldatei</p> + </section> + <section> + <p>Aufgabe</p> + <p>Ändere das Programm, sodass auf Konsolenebene ein Pipe (|) ermöglicht wird:</p> + <pre> + <code class="bash" data-trim data-line-numbers> + echo "Hello World!" | node index.js + </code> + </pre> + <p>Nutze dafür die implizit vorhandenen Streams von Standard In / Out:</p> + <pre> + <code class="js" data-trim data-line-numbers> + process.stdin; // Readable Stream + process.stdout; // Writable Stream + </code> + </pre> + </section> + <section> + <h3>Kryptographie</h3> + </section> + <section> + Mithilfe des nativen <code>crypto</code>-Moduls ist es möglich, moderne sichere kryptografische Verfahren zu verwenden: + <pre> + <code class="js" data-trim data-line-numbers> + const crypto = require("crypto"); + </code> + </pre> + Hierzu nutzt NodeJS selber die weit verbreitete OpenSSL-Bibliothek + </section> + <section> + Einen MD5-Hash erzeugen: + <pre> + <code class="js" data-trim data-line-numbers> + const crypto = require("crypto"); + + const hash = crypto.createHash("md5"); + const md5 = hash.update("abcdefg") + .digest(); // Buffer (default) + // .digest("hex") // Hexadezimal + // .digest("utf8") // Bytes forciert als Chars + + console.log(md5); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Erzeuge jeweils den md5- und den sha256-Hash als Hex-Wert der beiden Texte in der Datei <code>text-examples.txt</code> und vergleiche jeweils ihren md5- und sha256-Hash miteinander</p> + </section> + <section> + <h3>Zippen</h3> + </section> + <section> + Mit dem nativen Modul <code>zlib</code> ist es möglich, Kompression in seinen Anwendungen zu nutzen. + <pre> + <code class="js" data-trim data-line-numbers> + const zlib = require("zlib"); + + // Komprimieren + zlib.deflate("abc" /* buffer | string */, (err, compressed) => {}); + + // Dekomprimieren + zlib.inflate(compressed /* buffer */, (err, decompressed) => {}); + + // Als Stream + const zipper = zlib.createDeflate(); + const unzipper = zlib.createInflate(); + + source.pipe(zipper).pipe(target); + source.pipe(unzipper).pipe(target); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Nutze die einfachen Varianten <code>zlib.deflate</code> und <code>zlib.inflate</code>, um einen beliebigen Text zu komprimieren und anschließend wieder zu dekomprimieren</p> + </section> + <section> + <p>Aufgabe</p> + <p>Nutze die Stream-Varianten <code>zlib.createDeflate</code> und <code>zlib.createInflate</code>, um jeweils ein getrenntes Programm für die Kompression und Dekompression mit <code>process.stdin</code> und <code>process.stdout</code> zu machen, sodass in der Konsole folgende Nutzung möglich ist:</p> + <pre> + <code class="bash" data-trim data-line-numbers> + echo "abc" | node compress.js | node decompress.js + cat file.txt | node compress.js > file.zipped + cat file.zipped | node decompress.js > file.txt + </code> + </pre> + </section> + <section> + <h3>TCP-Verbindungen</h3> + </section> + <section> + <ul> + <li>Bidirektionale Verbindung</li> + <li>Duplexfähig</li> + <li>Besteht immer aus Server- und Client-Socket</li> + </ul> + </section> + <section> + Mit dem nativen Modul <code>net</code> ist es möglich, eine TCP-Verbindung aufzubauen + <pre> + <code class="js" data-trim data-line-numbers> + /* Server */ + const net = require("net"); + + const server = net.createServer((socket) => { + + // new incoming connection + socket.on("data", (data) => { + // data is a buffer + }); + + socket.write("Hello World!"); + }); + + server.listen(3000); + </code> + </pre> + </section> + <section> + <pre> + <code class="js" data-trim data-line-numbers> + /* Client */ + const net = require("net"); + + const socket = net.connect(3000, () => { + // 'connect' listener + }); + + socket.on("data", (data) => { + // data is a buffer + }); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Implementiere die Clientlogik in der <code>tcp-client.js</code>, um dem Server ein <code>"Hello Server!"</code> zurück zu senden</p> + </section> + <section> + <p>Aufgabe</p> + <p>Sende eine festgelegte Datei vom Server zum Client. Der Server liest die Datei ein und sendet die Inhalte zum Client, welcher diese wiederum in eine Zieldatei schreibt. Eventuell darf auch noch eine Kompression mittels Zip / Deflate implementiert werden.</p> + </section> + <section> + <p>Kurze Wiederholung des HTTP-Protokolls</p> + <p>Request</p> + <pre> + <code class="http" data-trim data-line-numbers> + GET / HTTP/1.1 + Host: localhost:3456 + </code> + </pre> + <p>Response</p> + <pre> + <code class="http" data-trim data-line-numbers> + HTTP/1.1 200 + Content-Type: text/plain + Content-Length: 12 + + Hello World! + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Implementiere mithilfe des TCP-Sockets einen minimalen HTTP-Server, welcher <code>Hello <name></code> als Text zurück gibt, wobei <code><name></code> aus dem Query-Parameter gelesen werden soll</p> + </section> + <section> + <h3>HTTP-Server</h3> + </section> + <section> + <pre> + <code class="js" data-trim data-line-numbers> + const http = require("http"); + + const server = http.createServer((req, res) => { + res.writeHeader(200, { + "Content-Type": "text/plain", /* Put Content-Type here */ + }); + + console.log(req.rawHeaders); + console.log(req.method); + console.log(req.url); + + res.write(/* response content */); + res.end(); /* important if no Content-Length is specified */ + }); + + server.listen(3456); + </code> + </pre> + </section> + <section> + <p>Aufgabe</p> + <p>Experimentiert mit verschiedenen Möglichkeiten von <code>Content-Length</code> und dem Vorhandensein von <code>res.end()</code>, mit längerer oder kürzerer <code>Content-Length</code> als der tatsächliche Body oder teilt den Body in zwei <code>res.write()</code> und verzögert künstlich mit einem setTimeout() den zweiten Teil und schaut euch im Browser bei den Entwickler-Tools die Timings der Response an.</p> + </section> </section> </div> </div> -- Gitblit v1.9.3