internal.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package oauth2
  2. import (
  3. "errors"
  4. "net/http"
  5. )
  6. //Common functions
  7. func sendHTMLResponse(w http.ResponseWriter, msg string) {
  8. w.Header().Set("Content-Type", "text/html")
  9. w.Write([]byte(msg))
  10. }
  11. //Common functions
  12. func sendTextResponse(w http.ResponseWriter, msg string) {
  13. w.Write([]byte(msg))
  14. }
  15. //Send JSON response, with an extra json header
  16. func sendJSONResponse(w http.ResponseWriter, json string) {
  17. w.Header().Set("Content-Type", "application/json")
  18. w.Write([]byte(json))
  19. }
  20. func sendErrorResponse(w http.ResponseWriter, errMsg string) {
  21. w.Header().Set("Content-Type", "application/json")
  22. w.Write([]byte("{\"error\":\"" + errMsg + "\"}"))
  23. }
  24. func sendOK(w http.ResponseWriter) {
  25. w.Header().Set("Content-Type", "application/json")
  26. w.Write([]byte("\"OK\""))
  27. }
  28. func mv(r *http.Request, getParamter string, postMode bool) (string, error) {
  29. if !postMode {
  30. //Access the paramter via GET
  31. keys, ok := r.URL.Query()[getParamter]
  32. if !ok || len(keys[0]) < 1 {
  33. //log.Println("Url Param " + getParamter +" is missing")
  34. return "", errors.New("GET paramter " + getParamter + " not found or it is empty")
  35. }
  36. // Query()["key"] will return an array of items,
  37. // we only want the single item.
  38. key := keys[0]
  39. return string(key), nil
  40. } else {
  41. //Access the parameter via POST
  42. r.ParseForm()
  43. x := r.Form.Get(getParamter)
  44. if len(x) == 0 || x == "" {
  45. return "", errors.New("POST paramter " + getParamter + " not found or it is empty")
  46. }
  47. return string(x), nil
  48. }
  49. }