diff --git a/web/08/labor/loesungen/03/go.mod b/web/08/labor/loesungen/03/go.mod new file mode 100644 index 0000000..bdf4f1f --- /dev/null +++ b/web/08/labor/loesungen/03/go.mod @@ -0,0 +1,5 @@ +module uebung03/cookies + +go 1.24.5 + +require github.com/google/uuid v1.6.0 diff --git a/web/08/labor/loesungen/03/go.sum b/web/08/labor/loesungen/03/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/web/08/labor/loesungen/03/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/web/08/labor/loesungen/03/uebung03.go b/web/08/labor/loesungen/03/uebung03.go new file mode 100644 index 0000000..9589ce1 --- /dev/null +++ b/web/08/labor/loesungen/03/uebung03.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "log" + "net/http" + + "github.com/google/uuid" +) + +func createCookie(w http.ResponseWriter, r *http.Request) { + uuid := uuid.New().String() + cookie := &http.Cookie{ + Name: "keks", + Value: uuid, + } + http.SetCookie(w, cookie) + fmt.Fprintf(w, "Cookie gesetzt: keks = %s\n", uuid) +} + +func showCookie(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("keks") + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + fmt.Fprintf(w, "Cookie gefunden: keks = %s\n", cookie.Value) +} + +func deleteCookie(w http.ResponseWriter, r *http.Request) { + cookie, err := r.Cookie("keks") + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + cookie.MaxAge = -1 + http.SetCookie(w, cookie) + fmt.Fprintf(w, "Cookie gelöscht.\n") +} + +func main() { + http.HandleFunc("/create-cookie", createCookie) + http.HandleFunc("/show-cookie", showCookie) + http.HandleFunc("/delete-cookie", deleteCookie) + log.Fatal(http.ListenAndServe("localhost:8080", nil)) +} diff --git a/web/08/labor/loesungen/04/go.mod b/web/08/labor/loesungen/04/go.mod new file mode 100644 index 0000000..4e61f87 --- /dev/null +++ b/web/08/labor/loesungen/04/go.mod @@ -0,0 +1,5 @@ +module uebung04/sessions + +go 1.24.5 + +require github.com/google/uuid v1.6.0 diff --git a/web/08/labor/loesungen/04/go.sum b/web/08/labor/loesungen/04/go.sum new file mode 100644 index 0000000..7790d7c --- /dev/null +++ b/web/08/labor/loesungen/04/go.sum @@ -0,0 +1,2 @@ +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/web/08/labor/loesungen/04/uebung04.go b/web/08/labor/loesungen/04/uebung04.go new file mode 100644 index 0000000..8355878 --- /dev/null +++ b/web/08/labor/loesungen/04/uebung04.go @@ -0,0 +1,85 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" + "sync" + + "github.com/google/uuid" +) + +type UserData struct { + Username string `json:"username"` + Nickname string `json:"nickname"` + Admin bool `json:"admin"` +} + +var ( + sessions = make(map[string]UserData) + mu sync.Mutex +) + +func checkSessionID(r *http.Request) (string, error) { + cookie, err := r.Cookie("session_id") + if err != nil { + return "", errors.New("keine session-ID") + } + return cookie.Value, nil +} + +func getSessionID(w http.ResponseWriter, r *http.Request) string { + sessionID, err := checkSessionID(r) + if err != nil { + newID := uuid.New().String() + newCookie := http.Cookie{ + Name: "session_id", + Value: newID, + } + http.SetCookie(w, &newCookie) + + mu.Lock() + sessions[newID] = UserData{} + mu.Unlock() + + return newID + } + return sessionID +} + +func signup(w http.ResponseWriter, r *http.Request) { + sessionID := getSessionID(w, r) + var user UserData + err := json.NewDecoder(r.Body).Decode(&user) + if err != nil { + http.Error(w, "Daten konnten nicht verarbeitet werden.", http.StatusBadRequest) + return + } + if user.Nickname == "" || user.Username == "" { + http.Error(w, "Daten unvollständig.", http.StatusBadRequest) + return + } + sessions[sessionID] = user +} + +func whoami(w http.ResponseWriter, r *http.Request) { + sessionID, err := checkSessionID(r) + if err != nil { + fmt.Fprintf(w, "Du bist noch niemand.") + } else { + user := sessions[sessionID] + fmt.Fprintf(w, "Hallo %s!\n", user.Nickname) + if user.Admin { + fmt.Fprintln(w, "Du bist als Admin registriert.") + } else { + fmt.Fprintln(w, "Du bist nicht als Admin registriert.") + } + } +} + +func main() { + http.HandleFunc("/signup", signup) + http.HandleFunc("/whoami", whoami) + http.ListenAndServe("localhost:8080", nil) +} diff --git a/web/08/labor/loesungen/uebung01.go b/web/08/labor/loesungen/uebung01.go new file mode 100644 index 0000000..6d9fb90 --- /dev/null +++ b/web/08/labor/loesungen/uebung01.go @@ -0,0 +1,20 @@ +package main + +import ( + "fmt" + "net/http" +) + +func loginFunc(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Sie wurden erfolgreich eingeloggt.") +} + +func logoutFunc(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Sie wurden erfolgreich ausgeloggt") +} + +func main() { + http.HandleFunc("/login", loginFunc) + http.HandleFunc("/logout", logoutFunc) + http.ListenAndServe("localhost:8080", nil) +} diff --git a/web/08/labor/loesungen/uebung02.go b/web/08/labor/loesungen/uebung02.go new file mode 100644 index 0000000..45f97ed --- /dev/null +++ b/web/08/labor/loesungen/uebung02.go @@ -0,0 +1,149 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "net/http" +) + +type Registrierung struct { + Vorname string `json:"vorname"` + Nachname string `json:"nachname"` + Email string `json:"email"` + Telefon string `json:"telefon"` + Sessions []string `json:"sessions"` + AGBAkzeptiert string `json:"agb"` + Newsletter string `json:"newsletter"` + Equipment string `json:"equipment"` + Format string `json:"format"` +} + +func parseCheckboxValue(value string) string { + if value == "" || (value != "ja" && value != "on") { + return "nein" + } + return "ja" +} + +func parseRadiobuttonValue(value string) (string, error) { + if value == "" || (value != "praesenz" && value != "online") { + return "", errors.New("Auswahl nicht erlaubt.") + } + return value, nil +} + +func registrierungHandler(w http.ResponseWriter, r *http.Request) { + + if r.Method != http.MethodPost { + http.Error(w, "Nur POST-Anfragen erlaubt", http.StatusMethodNotAllowed) + return + } + + var data Registrierung + + contentType := r.Header.Get("Content-Type") + + if contentType == "application/json" { + // JSON-Daten verarbeiten + decoder := json.NewDecoder(r.Body) + if err := decoder.Decode(&data); err != nil { + http.Error(w, "Ungültiges JSON", http.StatusBadRequest) + return + } + if data.Vorname == "" { + http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) + return + } + if data.Nachname == "" { + http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) + return + } + data.AGBAkzeptiert = parseCheckboxValue(data.AGBAkzeptiert) + if data.AGBAkzeptiert == "nein" { + http.Error(w, "AGB wurde nicht akzeptiert.", http.StatusBadRequest) + return + } + data.Newsletter = parseCheckboxValue(data.Newsletter) + data.Equipment = parseCheckboxValue(data.Equipment) + format, err := parseRadiobuttonValue(data.Format) + if err != nil { + http.Error(w, "Ungültige Auswahl des Formats.", http.StatusBadRequest) + return + } + data.Format = format + } else { + // Formulardaten parsen und Überprüfen, ob ungültige Formulardaten gesendet wurde + if err := r.ParseForm(); err != nil { + http.Error(w, "Fehler beim Parsen des Formulars", http.StatusBadRequest) + return + } + + // Überprüfen, ob leeres Formular gesendet wurde + if len(r.PostForm) == 0 { + http.Error(w, "Kein Formular gesendet", http.StatusBadRequest) + return + } + + // Formulardaten auslesen und ggf. überprüfen + vorname := r.PostForm.Get("vorname") + if vorname == "" { + http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) + return + } + nachname := r.PostForm.Get("nachname") + if nachname == "" { + http.Error(w, "Pflichtfeld fehlt.", http.StatusBadRequest) + return + } + email := r.PostForm.Get("email") + telefon := r.PostForm.Get("telefon") + sessions := r.PostForm["sessions"] + agb := parseCheckboxValue(r.PostForm.Get("agb")) + if agb == "" { + http.Error(w, "AGB wurde nicht akzeptiert.", http.StatusBadRequest) + return + } + newsletter := parseCheckboxValue(r.PostForm.Get("newsletter")) + equipment := parseCheckboxValue(r.PostForm.Get("equipment")) + format, err := parseRadiobuttonValue(r.PostForm.Get("format")) + if err != nil { + http.Error(w, "Ungültige Auswahl des Formats.", http.StatusBadRequest) + return + } + + data = Registrierung{ + Vorname: vorname, + Nachname: nachname, + Email: email, + Telefon: telefon, + Sessions: sessions, + AGBAkzeptiert: agb, + Newsletter: newsletter, + Equipment: equipment, + Format: format, + } + } + + // Ausgabe im Terminal + fmt.Fprintf(w, "Neue Registrierung erhalten:\n") + fmt.Fprintf(w, "Vorname: %s\n", data.Vorname) + fmt.Fprintf(w, "Nachname: %s\n", data.Nachname) + fmt.Fprintf(w, "E-Mail: %s\n", data.Email) + fmt.Fprintf(w, "Telefon: %s\n", data.Telefon) + fmt.Fprintf(w, "Bevorzugte Sessions: \n") + for _, session := range data.Sessions { + fmt.Fprintf(w, " - %s\n", session) + } + fmt.Fprintf(w, "AGB akzeptiert: %s\n", data.AGBAkzeptiert) + fmt.Fprintf(w, "Newsletter: %s\n", data.Newsletter) + fmt.Fprintf(w, "Equipment benötigt: %s\n", data.Equipment) + fmt.Fprintf(w, "Teilnahmeformat: %s", data.Format) + +} + +func main() { + http.HandleFunc("/registrierung", registrierungHandler) + fmt.Println("Server läuft auf localhost:8080...") + http.ListenAndServe(":8080", nil) +}