github.com/kvattikuti/drone@v0.2.1-0.20140603034306-d400229a327a/pkg/handler/util.go (about)

     1  package handler
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/dchest/authcookie"
    10  	"github.com/drone/drone/pkg/template"
    11  )
    12  
    13  // -----------------------------------------------------------------------------
    14  // Rendering Functions
    15  
    16  // Renders the named template for the specified data type
    17  // and write the output to the http.ResponseWriter.
    18  func RenderTemplate(w http.ResponseWriter, name string, data interface{}) error {
    19  	w.Header().Add("Content-Type", "text/html; charset=utf-8")
    20  	return template.ExecuteTemplate(w, name, data)
    21  }
    22  
    23  // Renders the 404 template for the specified data type
    24  // and write the output to the http.ResponseWriter.
    25  func RenderNotFound(w http.ResponseWriter) error {
    26  	w.Header().Add("Content-Type", "text/html; charset=utf-8")
    27  	w.WriteHeader(http.StatusNotFound)
    28  	return template.ExecuteTemplate(w, "404.amber", nil)
    29  }
    30  
    31  // Renders the 403 template for the specified data type
    32  // and write the output to the http.ResponseWriter.
    33  func RenderForbidden(w http.ResponseWriter) error {
    34  	w.Header().Add("Content-Type", "text/html; charset=utf-8")
    35  	w.WriteHeader(http.StatusNotFound)
    36  	return template.ExecuteTemplate(w, "403.amber", nil)
    37  }
    38  
    39  // RenderJson renders a JSON representation of resource v and
    40  // writes to the http.ResposneWriter.
    41  func RenderJson(w http.ResponseWriter, data interface{}) error {
    42  	w.Header().Add("Content-Type", "application/json")
    43  	w.WriteHeader(http.StatusOK)
    44  	return json.NewEncoder(w).Encode(data)
    45  }
    46  
    47  // RenderText write the plain text string to the http.ResposneWriter.
    48  func RenderText(w http.ResponseWriter, text string, code int) error {
    49  	w.Header().Add("Content-Type", "text/plain")
    50  	w.WriteHeader(code)
    51  	w.Write([]byte(text))
    52  	return nil
    53  }
    54  
    55  // RenderError renders a text representation of the Error message.
    56  func RenderError(w http.ResponseWriter, err error, code int) error {
    57  	return RenderText(w, err.Error(), code)
    58  }
    59  
    60  // -----------------------------------------------------------------------------
    61  // Cookie Helper functions
    62  
    63  // SetCookie signs and writes the cookie value.
    64  func SetCookie(w http.ResponseWriter, r *http.Request, name, value string) {
    65  	sec := IsHttps(r)
    66  	str := authcookie.New(value, time.Now().Add(time.Hour*24), secret)
    67  	cookie := http.Cookie{
    68  		Name:     name,
    69  		Value:    str,
    70  		Path:     "/",
    71  		Domain:   r.URL.Host,
    72  		HttpOnly: true,
    73  		Secure:   sec,
    74  	}
    75  
    76  	http.SetCookie(w, &cookie)
    77  }
    78  
    79  func IsHttps(r *http.Request) bool {
    80  	if r.URL.Scheme == "https" {
    81  		return true
    82  	}
    83  	if strings.HasPrefix(r.Proto, "HTTPS") {
    84  		return true
    85  	}
    86  	if r.Header.Get("X-Forwarded-Proto") == "https" {
    87  		return true
    88  	}
    89  	return false
    90  }
    91  
    92  // GetCookie retrieves and verifies the signed cookie value.
    93  func GetCookie(r *http.Request, name string) string {
    94  	cookie, err := r.Cookie(name)
    95  	if err != nil {
    96  		return ""
    97  	}
    98  	return authcookie.Login(cookie.Value, secret)
    99  }
   100  
   101  // DelCookie deletes a secure cookie.
   102  func DelCookie(w http.ResponseWriter, r *http.Request, name string) {
   103  	cookie := http.Cookie{
   104  		Name:   name,
   105  		Value:  "deleted",
   106  		Path:   "/",
   107  		Domain: r.URL.Host,
   108  		MaxAge: -1,
   109  	}
   110  
   111  	http.SetCookie(w, &cookie)
   112  }