如何正确地向这个内容添加校验和头部?

huangapple go评论77阅读模式
英文:

How do I properly add a checksum header to this?

问题

当我从HTTP服务器开始下载文件时,我想要知道某种文件校验和(例如SHA-256哈希或其他任何形式)。它可以作为HTTP响应头之一进行传输。

我知道HTTP ETag是类似的东西,但我正在学习的是Golang,虽然我已经查阅了一些文档,但我仍然一头雾水。以下是我目前的代码:

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"strconv"
)

const (
	crlf       = "\r\n"
	colonspace = ": "
)

func Checksum(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		

		func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
	})
}

// 请不要更改此函数。
func main() {
	var listenAddr = flag.String("http", ":8080", "address to listen on for HTTP")
	flag.Parse()

	http.Handle("/", Checksum(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-Foo", "bar")
		w.Header().Set("Content-Type", "text/plain")
		w.Header().Set("Date", "Sun, 08 May 2016 14:04:53 GMT")
		msg := "Curiosity is insubordination in its purest form.\n"
		w.Header().Set("Content-Length", strconv.Itoa(len(msg)))
		fmt.Fprintf(w, msg)
	})))

	log.Fatal(http.ListenAndServe(*listenAddr, nil))
}
英文:

I'd like to know some kind of file checksum (like SHA-256 hash, or anything else) when I start downloading a file from HTTP server. It could be transferred as one of HTTP response headers.

I know http etag is something similar, I think, but this is Golang which I am new to learning and although I have looked through some documentation, I am still clueless. This is what I have so far:

package main

import (
	"flag"
	"fmt"
	"log"
	"net/http"
	"strconv"
)

const (
	crlf       = "\r\n"
	colonspace = ": "
)

func Checksum(h http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		

		func HandleFunc(pattern string, handler func(ResponseWriter, *Request))
	})
}

// Do not change this function.
func main() {
	var listenAddr = flag.String("http", ":8080", "address to listen on for HTTP")
	flag.Parse()

	http.Handle("/", Checksum(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("X-Foo", "bar")
		w.Header().Set("Content-Type", "text/plain")
		w.Header().Set("Date", "Sun, 08 May 2016 14:04:53 GMT")
		msg := "Curiosity is insubordination in its purest form.\n"
		w.Header().Set("Content-Length", strconv.Itoa(len(msg)))
		fmt.Fprintf(w, msg)
	})))

	log.Fatal(http.ListenAndServe(*listenAddr, nil))
}

答案1

得分: 2

编写一个包装器,用于捕获响应体和状态的 http.ResponseWriter:

type rwWrapper struct {
   http.ResponseWriter
   buf bytes.Buffer
   status int
}

func (w *rwWrapper) Write(p []byte) (int, error) {
   return rw.buf.Write(p)
}

func (w *rwWrapper) WriteHeader(status int) {
   rw.status = status
}

在处理程序返回后,对响应体进行校验和,设置头部,然后将响应体写入底层的响应写入器:

func Checksum(h http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    ww := &rwWrapper{ResponseWriter: w, status:200}
    h.ServeHTTP(ww, r)
    // 在这里计算 ww.buf.Bytes() 的校验和
    w.Header().Set("nameOfHeader", checksum)
    w.WriteHeader(ww.status)
    w.Write(ww.buf.Bytes())
  })
}
英文:

Write a wrapper around an http.ResponseWriter to capture the response body and status:

type rwWrapper struct {
   http.ResponseWriter
   buf bytes.Buffer
   status int
}

func (w *rwWrapper) Write(p []byte) (int, error) {
   return rw.buf.Write(p)
}
func (w *rwWrapper) WriteHeader(status int) {
   rw.status = status
}

After the handler returns, checksum the body, set the header and then write the body to the underlying response writer:

func Checksum(h http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    ww := &rwWrapper{ResponseWriter: w, status:200}
    h.ServeHttp(ww, r)
    // compute checksum of ww.buf.Bytes() here
    w.Header().Set("nameOfHeader", checksum)
    w.WriteHeader(ww.status)
    w.Write(ww.buf.Bytes())
  })
}

huangapple
  • 本文由 发表于 2017年1月27日 03:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/41881843.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定