Skip to content
Snippets Groups Projects
Select Git revision
  • a4ddf570c9e1fc41be356a60d81563abaeb91563
  • master default protected
  • v1.3.4
  • v1.3.3
  • v1.3.2
  • v1.3.1
  • v1.3.0
  • v1.2.5
  • v1.2.4
  • v1.2.3
  • v1.2.2
  • v1.2.1
  • v1.2.0
  • v1.1.0
  • v1.0.3
  • v1.0.2
  • v1.0.1
  • v1.0.0
18 results

clean-up.nix

Blame
  • handlers.go 4.90 KiB
    package httpbin
    
    import (
    	"encoding/json"
    	"fmt"
    	"net/http"
    	"net/url"
    	"strconv"
    	"strings"
    )
    
    var acceptedMediaTypes = []string{
    	"image/webp",
    	"image/svg+xml",
    	"image/jpeg",
    	"image/png",
    	"image/",
    }
    
    // Index renders an HTML index page
    func (h *HTTPBin) Index(w http.ResponseWriter, r *http.Request) {
    	w.Write(MustAsset("index.html"))
    }
    
    // FormsPost renders an HTML form that submits a request to the /post endpoint
    func (h *HTTPBin) FormsPost(w http.ResponseWriter, r *http.Request) {
    	w.Write(MustAsset("forms-post.html"))
    }
    
    // UTF8 renders an HTML encoding stress test
    func (h *HTTPBin) UTF8(w http.ResponseWriter, r *http.Request) {
    	w.Header().Set("Content-Type", "text/html; charset=utf-8")
    	w.Write(MustAsset("utf8.html"))
    }
    
    // Get handles HTTP GET requests
    func (h *HTTPBin) Get(w http.ResponseWriter, r *http.Request) {
    	args, err := url.ParseQuery(r.URL.RawQuery)
    	if err != nil {
    		http.Error(w, fmt.Sprintf("error parsing query params: %s", err), http.StatusBadRequest)
    		return
    	}
    	resp := &getResponse{
    		Args:    args,
    		Headers: r.Header,
    		Origin:  getOrigin(r),
    		URL:     getURL(r),
    	}
    	body, _ := json.Marshal(resp)
    	writeJSON(w, body, http.StatusOK)
    }
    
    // RequestWithBody handles POST, PUT, and PATCH requests
    func (h *HTTPBin) RequestWithBody(w http.ResponseWriter, r *http.Request) {
    	args, err := url.ParseQuery(r.URL.RawQuery)
    	if err != nil {
    		http.Error(w, fmt.Sprintf("error parsing query params: %s", err), http.StatusBadRequest)
    		return
    	}
    
    	resp := &bodyResponse{
    		Args:    args,
    		Headers: r.Header,
    		Origin:  getOrigin(r),
    		URL:     getURL(r),
    	}
    
    	err = parseBody(w, r, resp, h.options.MaxMemory)
    	if err != nil {
    		http.Error(w, fmt.Sprintf("error parsing request body: %s", err), http.StatusBadRequest)