Golang Context.Header Examples

Golang Context.Header - 30 examples found. These are the top rated real world Golang examples of github.com/gin-gonic/gin.Context.Header extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
func DownloadDtD(c *gin.Context) {
	c.Header("Content-Type", "text/xml; charset=\"utf-8\"")
	url := c.Request.URL.RequestURI()[1:]
	timeout := time.Duration(5 * time.Second)
	client := http.Client{
		Timeout: timeout,
	}
	resp, err := client.Get(fmt.Sprintf("http://%s", url))
	if resp.StatusCode != 200 {
		c.String(http.StatusServiceUnavailable, "fail to reach baclend")
		return
	}
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		c.String(http.StatusInternalServerError, "fail to read body")
	}
	defer resp.Body.Close()
	if len(body) == 0 {
		c.String(http.StatusServiceUnavailable, "fail to read body")
		return
	}
	c.Header("Cache-Control", "max-age=436800")
	c.String(http.StatusOK, string(body))
	filePath := fmt.Sprintf("%s%s", *cachePath, c.Request.URL.Path)
	paths := strings.Split(filePath, "/")
	err = os.MkdirAll(strings.Join(paths[:len(paths)-1], "/"), os.ModeDir|os.ModePerm)
	err = ioutil.WriteFile(filePath, body, 0644)
	if err != nil {
		return
	}
}
// PreAbort will set the appropriate HTTP Basic header.
func (m HTTPBasicAuth) PreAbort(c *gin.Context, auth *AuthInfo, err *AuthErr) {
	if m.Realm == "" {
		m.Realm = "Authorization Required"
	}
	m.Realm = "Basic realm=" + strconv.Quote(m.Realm)
	c.Header("WWW-Authenticate", m.Realm)
}
Example #3
0
func view(c *gin.Context) {
	id := c.Param("uniuri")
	key := c.Param("key")
	re := models.ResourceEntry{}
	remote := c.ClientIP()

	db.Where(&models.ResourceEntry{Key: id}).First(&re)
	if re.Key == "" {
		log.Printf("[INFO][%s]\tNot found : %s", remote, id)
		c.AbortWithStatus(http.StatusNotFound)
		return
	}
	log.Printf("[INFO][%s]\tFetched %s file and entry\n", remote, id)
	f, err := os.Open(path.Join(conf.C.UploadDir, re.Key))
	if err != nil {
		log.Printf("[ERROR][%s]\tWhile opening %s file\n", remote, id)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	block, err := aes.NewCipher([]byte(key))
	if err != nil {
		log.Printf("[ERROR][%s]\tDuring Cipher creation : %s\n", remote, err)
		c.String(http.StatusInternalServerError, "Something went wrong on the server side. Try again later.")
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	var iv [aes.BlockSize]byte
	stream := cipher.NewCFBDecrypter(block, iv[:])
	reader := &cipher.StreamReader{S: stream, R: f}
	c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
	io.Copy(c.Writer, reader)
}
Example #4
0
func badgeImageScwBuildEndpoint(c *gin.Context) {
	name := c.Param("name")
	images := cache.GetImageByName(name)
	left := "build"
	c.Header("Content-Type", "image/svg+xml;charset=utf-8")
	switch len(images) {
	case 0:
		c.String(http.StatusNotFound, devhub.ErrBadge(left, fmt.Errorf("no such image")))
	case 1:
		image := images[0]
		if image.Objects.Api == nil {
			c.String(http.StatusInternalServerError, devhub.ErrBadge(left, fmt.Errorf("invalid resource")))
			return
		}
		creationDate, err := time.Parse(time.RFC3339, image.Objects.Api.CreationDate)
		if err != nil {
			c.String(http.StatusInternalServerError, devhub.ErrBadge(left, fmt.Errorf("invalid-date")))
			return
		}

		humanTime := humanize.Time(creationDate)
		humanTime = strings.Replace(humanTime, " ago", "", -1)

		badge, err := devhub.GetBadge(left, humanTime, "green")
		if err != nil {
			c.String(http.StatusInternalServerError, badge)
			return
		}
		c.String(http.StatusOK, badge)
	default:
		c.String(http.StatusNotFound, devhub.ErrBadge(left, fmt.Errorf("ambiguous name")))
	}
}
Example #5
0
// View handles the file views
func View(c *gin.Context) {
	var err error

	id := c.Param("uniuri")
	re := models.Resource{}

	if err = re.Get(id); err != nil || re.Key == "" {
		logger.InfoC(c, "server", "Not found", id)
		c.AbortWithStatus(http.StatusNotFound)
		return
	}
	re.LogFetched(c)
	f, err := os.Open(path.Join(conf.C.UploadDir, re.Key))
	if err != nil {
		logger.ErrC(c, "server", fmt.Sprintf("Couldn't open %s", re.Key), err)
		c.AbortWithStatus(http.StatusInternalServerError)
		return
	}
	if conf.C.AlwaysDownload {
		c.Header("Content-Type", "application/octet-stream")
	}
	c.Header("Content-Disposition", "filename=\""+re.Name+"\"")
	io.Copy(c.Writer, f)
	if re.Once {
		re.Delete()
		re.LogDeleted(c)
	}
}
Example #6
0
func Generate8bit(c *gin.Context) {
	name := c.Param("name")
	gender := c.Param("gender")

	switch {
	case gender == "m":
		gender = "male"
	case gender == "f":
		gender = "female"
	case gender == "male" || gender == "female":
		//do nothing
	default:
		c.AbortWithError(http.StatusInternalServerError, errors.New("Invalid parameters"))
		return
	}

	log.Println(name)
	InitAssets()
	img := GenerateIdenticon8bits(gender, []byte(name))
	c.Header("Content-Type", "image/png")
	c.Stream(func(w io.Writer) bool {
		png.Encode(w, img)
		return false
	})
}