internal.go 1.3 KB

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