Created
April 26, 2017 14:09
-
-
Save deankarn/b304e03e0b9044e201d9dc93752bd793 to your computer and use it in GitHub Desktop.
std net/http is enough PII
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"strings" | |
) | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/user/", userHandler) | |
http.ListenAndServe(":8080", mux) | |
} | |
func userHandler(w http.ResponseWriter, r *http.Request) { | |
// | |
// we need to handle the following: | |
// /user/:id | |
// /user/:id/profile | |
// /user/:id/account/:accountID | |
// | |
// I'm not converting :id or :accountID to integer or GUID... | |
// | |
paramID := r.URL.Path[6:] | |
idx := strings.Index(paramID, "/") | |
if idx == -1 { | |
// url is "/user/:id" handle with logic | |
fmt.Fprintf(w, "/user/:id id=%s", paramID) | |
return | |
} | |
// must be more to the URL | |
static := paramID[idx+1:] | |
// no more params URL was /user/:id/ redirect to /user/:id | |
if len(static) == 0 { | |
http.Redirect(w, r, r.URL.Path[:len(r.URL.Path)-1], http.StatusMovedPermanently) | |
return | |
} | |
paramID = paramID[:idx] | |
idx = strings.Index(static, "/") | |
if idx == -1 { | |
if static != "profile" { | |
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | |
return | |
} | |
// url is "/user/:id/profile" handle with logic | |
fmt.Fprintf(w, "/user/:id/profile id=%s", paramID) | |
return | |
} | |
static2 := static[:idx] | |
static = static[idx+1:] | |
// no more params url was /user/:id/profile/ redirect to /user/:id/profile | |
if len(static) == 0 { | |
http.Redirect(w, r, r.URL.Path[:len(r.URL.Path)-1], http.StatusMovedPermanently) | |
return | |
} | |
idx = strings.Index(static, "/") | |
if idx == -1 { | |
if static2 != "account" { | |
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | |
return | |
} | |
// must be /user/:id/account/:accountID | |
accountID := static | |
fmt.Fprintf(w, "/user/:id/account/:accountID id=%s accountID=%s", paramID, accountID) | |
return | |
} | |
static = static[idx+1:] | |
// no more params url was /user/:id/account/:accountID/ redirect to /user/:id/account/:accountID | |
if len(static) == 0 { | |
http.Redirect(w, r, r.URL.Path[:len(r.URL.Path)-1], http.StatusMovedPermanently) | |
return | |
} | |
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment